2

I am trying to render a bunch of vertices with an own color for each vertex. The number of vertices is around 1 mio, so I use a Vertex Array Object to render them. It's no problem for me to render the points, but when I add the colors to the data array the points are still rendered white. I tried different tutorials I found, but they all didn't work for me ( like: tut1, tut2 or this ).

I have the data in one array with VCVCVC... 3 floats for the vertex position and 4 floats for the color.

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

I am new to OpenGL, so it is possible that I made a stupid mistake that I can't see.

BTW: the vertex locations are still correct even with the colors in the array

Community
  • 1
  • 1
Schamote
  • 47
  • 7
  • It looks like this offset is wrong: `(char*)NULL + 3);` It should be in bytes - `3*4` – Kromster Mar 06 '14 at 13:56
  • I changed the offset but that didn't change anything. The joke is that the rendering of the vertices is correct, but they are all white (i checked the colors, they are around green/blue). So I guess that I define the location of the colors wrong of something similar. – Schamote Mar 06 '14 at 14:15
  • What values do you use for colors, 0..1 ? How do you apply colors in fragment shader? – Kromster Mar 06 '14 at 14:23
  • I use values in range of 0.0 to 1.0. And I don't have a fragment shader, but I am currently working on it. – Schamote Mar 06 '14 at 14:34
  • If you don't have fragment shader, how is the shader then supposed to know which color the points should be? ;) – Kromster Mar 06 '14 at 14:38
  • Ty for that hint :) That is this stupid mistake I talked about in my Question. – Schamote Mar 06 '14 at 14:43
  • I've done it to color all vertices in the same color with the shaders (color hard coded in the fragment shader), but I don't know how to load the colors from the array. I am currently using these shaders [link](http://pastebin.com/VDQ0GF1a), all the tutrials I found where about 2 vertex buffers, not a single one like I do, but I don't even know if it makes a difference for loading the colors. – Schamote Mar 13 '14 at 13:40

2 Answers2

3

Pick one and only one method of vertex submission:

  1. Vertex arrays (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. Vertex buffer object (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. Vertex array object (VAO) + VBO (requires corresponding shaders):

    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);
    
genpfault
  • 51,148
  • 11
  • 85
  • 139
-1

You need to create two vertex buffer objects(VBOs)

Currently only one is created but you try to access two

// 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 and g_color_buffer_data are the arrays of data to use, you will likely need to set up some shader code and the render code still has to be added but this is how to have more than one VBO(texture coords, colours, normals etc)

const_ref
  • 4,016
  • 3
  • 23
  • 38
  • I noticed this seems to be OpenGL ES and not core profile. My solution is aimed at core profile in which you do need multiple buffers(one for each state) but only one vertex array – const_ref Mar 06 '14 at 13:53
  • I tried your approach, but I only get white vertices ( the colors are != white, I check it ). – Schamote Mar 06 '14 at 14:17
  • Are you using shaders or anything? – const_ref Mar 06 '14 at 14:17
  • No, I use freeglut for creation of a window and thats it. I only want to draw some points, but that seems to be harder than I thought. – Schamote Mar 06 '14 at 14:19
  • This tut is very good and may help you out http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ – const_ref Mar 06 '14 at 14:20