Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
possible duplicate of Access vertex data stored in VBO in the shader –  Byte56 Mar 14 '13 at 18:09
    
@Byte56: It's not quite the same thing. He's talking about getting more arbitrary data, not really per-vertex data. –  Nicol Bolas Mar 15 '13 at 7:44
    
@NicolBolas "...how can I send attributes per vertex..." Sounds like it's per-vertex no? –  Byte56 Mar 15 '13 at 13:18
    
@Byte56: "I want to send the light value of each block" Note the singular "value". A block is not a vertex. –  Nicol Bolas Mar 15 '13 at 13:22
    
@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. –  Byte56 Mar 15 '13 at 13:27
show 3 more comments

1 Answer

up vote 1 down vote accepted
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.

share|improve this answer
    
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! –  Basaa Mar 16 '13 at 13:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.