2017-04-02 26 views
0

我想做一個三角形,我使用GLEW,GLFW和GLM作爲OpenGL的擴展。我是否需要爲OpenGL 3.3核心使用着色器?

這裏是我的代碼:

#include <stdlib.h> 
#include <stdio.h> 
#include <gl\glew.h> 
#include <GLFW\glfw3.h> 
#include <glm\glm.hpp> 
using namespace glm; 

int main() 
{ 
    if (!glfwInit()) 
    { 
     fprintf(stderr, "Failed to initialize GLFW\n"); 
     return -1; 
    } 
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

    // Open a window and create its OpenGL context 
    GLFWwindow* window; // (In the accompanying source code, this variable is global) 
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL); 
    if (window == NULL) 
    { 
     fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n"); 
     glfwTerminate(); 
     return -1; 
    } 

    glfwMakeContextCurrent(window); // Initialize GLEW 
    glewExperimental = true; // Needed in core profile 
    if (glewInit() != GLEW_OK) 
    { 
     fprintf(stderr, "Failed to initialize GLEW\n"); 
     return -1; 
    } 
    // Ensure we can capture the escape key being pressed below 
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 

    GLuint VertexArrayID; 
    glGenVertexArrays(1, &VertexArrayID); 
    glBindVertexArray(VertexArrayID); 

    // An array of 3 vectors which represents 3 vertices 
    static const GLfloat g_vertex_buffer_data[] = { 
     -1.0f, -1.0f, 0.0f, 
     1.0f, -1.0f, 0.0f, 
     0.0f, 1.0f, 0.0f, 
    }; 

    // This will identify our vertex buffer 
    GLuint vertexbuffer; 
    // Generate 1 buffer, put the resulting identifier in vertexbuffer 
    glGenBuffers(1, &vertexbuffer); 
    // The following commands will talk about our 'vertexbuffer' buffer 
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
    // Give our vertices to OpenGL. 
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 


    do { 
     // 1rst attribute buffer : vertices 
     glEnableVertexAttribArray(0); 
     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
     glVertexAttribPointer(
      0,     // attribute 0. No particular reason for 0, but must match the layout in the shader. 
      3,     // size 
      GL_FLOAT,   // type 
      GL_FALSE,   // normalized? 
      0,     // stride 
      (void*)0   // array buffer offset 
     ); 
     // Draw the triangle ! 
     glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle 
     glDisableVertexAttribArray(0); 

     // Swap buffers 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 

    } // Check if the ESC key was pressed or the window was closed 
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && 
     glfwWindowShouldClose(window) == 0); 
} 

編輯

的代碼不顯示白色三角形剛剛打開的窗口並顯示空的黑色。 同樣在第一次我添加了我亂碼的代碼,這是缺少部分代碼。但它仍然不會產生白色三角形。

+0

哦,它拋出了約navpi.dll PDB文件中的錯誤,但我看,這並不重要。 – user7688554

+0

你在問什麼?目前還不清楚你想知道什麼。 –

+0

啊哈哈讓我reedit – user7688554

回答

1

我是否需要爲OpenGL 3.3使用着色器?

您需要使用着色器,或者您不會看不到任何東西。如果你確實看到了什麼,那麼這是不明確的行爲。這高度依賴於每個駕駛員。


你可以嘗試用鮮明的色彩亂搞和清除屏幕:

glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 
glClear(GL_COLOR_BUFFER_BIT); 

本教程是能夠得到在黑色背景上的白色三角形。在我的電腦上,情況正好相反。

本教程還指出:

如果你在幸運的話,你可以看到結果(如果你不這樣做不慌)

因此,閱讀其餘該教程,它還包含着色器。

+0

謝謝! :)現在有道理。 – user7688554

+0

不客氣! – Vallentin

相關問題