1
\$\begingroup\$

If I have a buffer with my vertices, normals and texture coordinates, and I use glDrawArrays to draw the VBO to the screen, how can I send attributes per vertex that I placed in an array to the shader program? I'm making a voxel terrain, and every chunk is 1 VBO, this works all great, but I want to send the light value of each block (Just an array filled with int's) to the shader as well, how would I do that?

Edit: After reading some comments, here some clarification: I'm trying to avoid the deprecated glVertexPointer etc, so I'm using Vertex Arrays. Here is the layout of my vertex array:

X, Y, Z, NX, NY, NZ, X, Y, Z, NX, NY, NZ,X, Y, Z, NX, NY, NZ

I'm using this to draw it: (ofcourse with the array buffer bound)

glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 12, 12);

glDrawArrays(GL_TRIANGLES, 0, arraySize / 6);

However I get some weird shapes on my screen. Apart from the light problem, that is essentially the same thing, I don't get why this doesn't just work. When I remove the normals, and use just 1 vertexattribpointer it works fine. Yes, the attributes are bound to in_position and in_normal, and the vertex shader is nothing more than gl_Position = gl_ModelViewProjectionMatrix * vec4(in_position, 1.0);

Thanks!

This is what I get: Problem

(This are 6X6 chunks, so this code is called 6*6 times.)

\$\endgroup\$
7
  • \$\begingroup\$ possible duplicate of Access vertex data stored in VBO in the shader \$\endgroup\$
    – House
    Commented Mar 14, 2013 at 18:09
  • \$\begingroup\$ @Byte56: It's not quite the same thing. He's talking about getting more arbitrary data, not really per-vertex data. \$\endgroup\$ Commented Mar 15, 2013 at 7:44
  • \$\begingroup\$ @NicolBolas "...how can I send attributes per vertex..." Sounds like it's per-vertex no? \$\endgroup\$
    – House
    Commented Mar 15, 2013 at 13:18
  • \$\begingroup\$ @Byte56: "I want to send the light value of each block" Note the singular "value". A block is not a vertex. \$\endgroup\$ Commented Mar 15, 2013 at 13:22
  • \$\begingroup\$ @NicolBolas I suppose we're both correct then. OP may be implementing smooth lighting to have lighting information per vertex, or "blocky lighting" and having lighting per block. We'll just have to wait for more details. \$\endgroup\$
    – House
    Commented Mar 15, 2013 at 13:27

1 Answer 1

1
\$\begingroup\$
glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 12, 12);

This doesn't make sense. Your stride is 12 bytes, so OpenGL will offset the current location for that attribute by 12 bytes for each vertex.

But your data is interleaved. The first 3 floats are a position, and the next three are a normal. Your 12-byte stride means that the positions are tightly packed. So the positions for vertex 0 will be the first 3 floats, the postiions for vertex 1 will be the next three floats. You want it to be 6 floats from the first, not three.

Your stride for both needs to be 24, not 12.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your answer, I got it fixed yesterday already, but this,together with a dumb mistake from my side was indeed the solution. Thanks again! \$\endgroup\$
    – Basaa
    Commented Mar 16, 2013 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .