交错的顶点数组对象不显示颜色

时间:2014-03-06 13:28:10

标签: opengl vertex-array-object

我正在尝试为每个顶点渲染一组具有自己颜色的顶点。顶点的数量约为1 mio,因此我使用顶点数组对象来渲染它们。 渲染点对我来说没有问题,但是当我将颜色添加到数据数组时,点仍然呈现白色。我尝试了不同的教程,但它们都不适合我(例如:tut1tut2this)。

我将数据放在一个数组中,VCVCVC ...顶点位置有3个浮点数,颜色有4个浮点数。

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

unsigned VAO_ID;
glGenVertexArrays(1, &VAO_ID);
glBindVertexArray(VAO_ID);

unsigned VBO_ID;
glGenBuffers(1, &VBO_ID);

glBindBuffer(GL_ARRAY_BUFFER, VBO_ID);
glBufferData(GL_ARRAY_BUFFER, vertex_count*7*4, vertices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7*4, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7*4, (char*)NULL + 3);
glEnableVertexAttribArray(1);

glVertexPointer(3, GL_FLOAT, 7*4, &vertices[0]);
glColorPointer(4, GL_FLOAT, 7*4, &vertices[3]);

glDrawArrays(GL_POINTS, 0, vertex_count);

我是OpenGL的新手,所以我有可能犯了一个我看不到的愚蠢错误。

BTW:即使数组中的颜色

,顶点位置仍然正确

2 个答案:

答案 0 :(得分:2)

选择一个 且只有一个 顶点提交方法:

  1. 顶点数组(VA):

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    
    glVertexPointer(3, GL_FLOAT, 7*4, &vertices[0]);
    glColorPointer(4, GL_FLOAT, 7*4, &vertices[3]);
    
    glDrawArrays(GL_POINTS, 0, vertex_count);
    
  2. 顶点缓冲区对象(VBO):

    unsigned VBO_ID;
    glGenBuffers(1, &VBO_ID);
    glBindBuffer(GL_ARRAY_BUFFER, VBO_ID);
    glBufferData(GL_ARRAY_BUFFER, vertex_count*7*4, vertices, GL_STATIC_DRAW);
    
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    
    glVertexPointer(3, GL_FLOAT, 7*4, 0);
    glColorPointer(4, GL_FLOAT, 7*4, (char*)NULL + 3);
    
    glDrawArrays(GL_POINTS, 0, vertex_count);
    
  3. 顶点数组对象(VAO)+ VBO(需要相应的着色器):

    unsigned VAO_ID;
    glGenVertexArrays(1, &VAO_ID);
    glBindVertexArray(VAO_ID);
    
    unsigned VBO_ID;
    glGenBuffers(1, &VBO_ID);
    glBindBuffer(GL_ARRAY_BUFFER, VBO_ID);
    glBufferData(GL_ARRAY_BUFFER, vertex_count*7*4, vertices, GL_STATIC_DRAW);
    
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7*4, 0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7*4, (char*)NULL + 3);
    glEnableVertexAttribArray(1);
    

答案 1 :(得分:-1)

您需要创建两个顶点缓冲区对象(VBO)

目前只创建了一个,但您尝试访问两个

// Create one VAO
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

// 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);

// Now repeat for a colour buffer
Gluint colorbuffer;
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);

// just before rendering
// 1st 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
);

// 2nd attribute buffer : colors
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(
    1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
    3,                                // size
    GL_FLOAT,                         // type
    GL_FALSE,                         // normalized?
    0,                                // stride
    (void*)0                          // array buffer offset
);

// Draw triangles etc

g_vertex_buffer_datag_color_buffer_data是要使用的数据数组,您可能需要设置一些着色器代码并且仍然需要添加渲染代码,但这是如何拥有多个VBO(纹理坐标,颜色,法线等)