2015-04-03 258 views
0

我對OpenGL非常陌生,現在我想了解VAO和VBO。在每個頂點沒有繪製C++ OpenGL顏色

VAO只是一個VBO的集合。

每個VBO是一個對象的屬性,對象,顏色在每個對象的頂點的頂點的座標等

在下面的代碼,頂點結構定義一個頂點,其是位置的2個屬性和頂點的顏色。因此,需要2個維也納國際組織來存儲這些信息。

我的問題是:我能夠在屏幕上繪製三角形,但每個頂點的顏色似乎沒有繪製。爲什麼會發生?

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <GL/glew.h> 
#include <GLFW/glfw3.h> 
#include <glm/glm.hpp> 

// create vertex buffer 
GLuint vertexbuffer; 
struct Vertex{ 
    GLdouble position[3]; 
    GLfloat color[3]; 
}; 
static const GLfloat vertex_data[] = { 
    -0.7f, -0.7f, 0.0f, 
    0.7f, -0.7f, 0.0f, 
    0.0f, 1.0f, 0.0f 
}; 

void SetupGeometry(){ 
    const struct Vertex triangle[3] = { 
     {{-0.7, -0.7, 0.0}, {1.0f, 0.0f, 0.0f}}, 
     {{0.7, -0.7, 0.0}, {0.0f, 1.0f, 0.0f}}, 
     {{0.0, 1.0, 0.0}, {0.0f, 0.0f, 1.0f}} 
    }; 

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

    //generate 1 buffer, put the resulting identifier in vertex buffer 
    glGenBuffers(1, &vertexbuffer); 

    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 

    // give our vertices to OpenGL 
    glBufferData(GL_ARRAY_BUFFER, 3*sizeof(struct Vertex), triangle, GL_STATIC_DRAW); 

    // GLuint index, GLuint size, GLenum type, GLboolean normalized, GLsizei stride, const void *offset 
    glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, position)); 

    // any newly created VAO disables array access for all attributes 
    // array access is enabled by binding the VAO in SetupGeometry and calling: 
    glEnableVertexAttribArray(0); 

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, color)); 
    glEnableVertexAttribArray(1); 
} 

void SetupShaders(void){ 

} 

void Render(int i){ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glDrawArrays(GL_LINE_LOOP, 0, 3); 
} 

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){ 
    if((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action != GLFW_PRESS){ 
     glfwSetWindowShouldClose(window, GL_TRUE); 
    } 
} 

int main(void) { 
    /* Create a windowed mode window and its OpenGL context */ 
    GLFWwindow* window; 
    if(!glfwInit()) { 
      printf("Failed to start GLFW\n"); 
      exit(EXIT_FAILURE); 
    } 
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 
    if (!window) { 
     glfwTerminate(); 
     printf("GLFW Failed to start\n"); 
     return -1; 
    } 
    /* Make the window's context current */ 
    glfwMakeContextCurrent(window); // IMPORTANT: Must be done so glew recognises OpenGL 
    glfwWindowHint(GLFW_SAMPLES, 4); 
    int err = glewInit(); 
    if (glewInit() != GLEW_OK) { 
     /* Problem: glewInit failed, something is seriously wrong. */ 
     fprintf(stderr, "Error initializing GLEW: %s\n", glewGetErrorString(err)); 
    } 
    fprintf(stderr, "Glew done\n"); 
    fprintf(stderr, "GL VERSION INFO %s\n", glGetString(GL_VERSION)); 
    glfwSetKeyCallback(window, key_callback); 

    SetupGeometry(); 
    while(!glfwWindowShouldClose(window)){ 
     Render(0); 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 
    } 
} 
+0

您不必爲每個頂點屬性具有不同的VBO。您可以將一個結構作爲頂點,併爲所有頂點都有一個VBO。 我明白,那不能回答你的問題;只是覺得你應該知道。 – 2015-04-03 22:47:04

+0

謝謝!我不想成爲一個懶惰的狐狸,所以我只想在深入潛水之前弄清楚事情:) – 2015-04-03 22:51:02

+1

VAO不是VBO的集合。一個VAO實際上是對如何在一組維也納組織中解開數據的說明。如果您有多個屬性,則不需要使用多個VBO,如果需要,可以將所有屬性放在一個VBO中。請參閱:http://stackoverflow.com/questions/21652546/what-is-the-role-of-glbindvertexarrays-vs-glbindbuffer-and-what-is-their-relatio/21652955#21652955 – 2015-04-03 23:20:05

回答

1

您似乎沒有包含任何着色器。您需要使用片段着色器來設置三角形的顏色值。 This link應該可以幫到你。

+2

並且爲了防止鏈接教程開始讓您像對待我一樣受挫,請使用[本系列](https://www.youtube.com/watch?v=hQ2I8D2ogrs&list=PLSPw4ASQYyynKPY0I-QFHK0iJTjnvNUys)。這兩篇教程似乎涵蓋了大部分觀點。 – 2015-04-03 23:20:40