2

我正在尝试为每个顶点渲染一堆具有自己颜色的顶点。顶点的数量大约是 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 的新手,所以我可能犯了一个我看不到的愚蠢错误。

顺便说一句:即使使用数组中的颜色,顶点位置仍然正确

4

2 回答 2

3

选择一种也是唯一一种顶点提交方法:

  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);
    
于 2014-03-06T16:13:42.687 回答
-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_data并且g_color_buffer_data是要使用的数据数组,您可能需要设置一些着色器代码并且仍然必须添加渲染代码,但这是如何拥有多个 VBO(纹理坐标、颜色、法线等)

于 2014-03-06T13:37:05.540 回答