glDrawArrays - problem with normals

Started by Darth Revan, September 18, 2012, 16:47:37

Previous topic - Next topic

Darth Revan

Hi, i try to make a Voxel Engine.

I have a VAO that have a VBO. In this are the vertex and the normals of the Cubes.
vaoId = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vaoId);

ByteBuffer verticesBuffer = BufferUtils.createByteBuffer(v);
verticesBuffer.put(verticesAndNormals, 0, numberOfVerices);
verticesBuffer.flip();

vboId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_DYNAMIC_DRAW);
		
GL20.glVertexAttribPointer(0, 3, GL11.GL_BYTE, false, 6, 0); // vertex
GL20.glVertexAttribPointer(1, 3, GL11.GL_BYTE, false, 6, 3); // normals

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);


I can render the cubes like this:
GL30.glBindVertexArray(vaoId);
GL20.glEnableVertexAttribArray(0);

GL11.glDrawArrays(GL11.GL_QUADS, 0, vertexCount);

GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);


But how can I say OpenGL that, the second VBO includes the normals?

TeamworkGuy2

Notice how you defined two vertex attribute pointers.
GL20.glVertexAttribPointer(0, 3, GL11.GL_BYTE, false, 6, 0); // vertex
GL20.glVertexAttribPointer(1, 3, GL11.GL_BYTE, false, 6, 3); // normals


You have to enable both vertex attributes.
GL30.glBindVertexArray(vaoId);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);

GL11.glDrawArrays(GL11.GL_QUADS, 0, vertexCount);

GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);


I'm fairly new to Open GL, so someone else may answer with better advice.
But from what I understand, when using vertex attributes you have to use Shaders!

When you enable a vertex attribute it becomes one of the inputs, "in" variables, to the vertex shader.
So you need to
1. Enable the vertex attribute(s) before calling glDrawArrays (see sample code above)
2. Make sure your vertex shader has the correct input variables (add vertex shader to your program with 2 vec3 inputs)
3. Use the correct vertex attribute locations (set attribute location in program with glBindAttribLocation() or in shader with layout(location = #))

If you don't know how to add shaders to your program, try looking at this website for some working code for setting up shaders: http://openglbook.com/the-book/chapter-2-vertices-and-shapes/
Scroll down the page/search for the method "CreateShaders(void)".
Also look further down the page for the headers "The Vertex Shader" and "The Fragment Shader".

All of this information can be confusing.
If you don't understand what code to actually add to your program, just ask and I or someone else will be happy to help.

Cubic

Quote
But how can I say OpenGL that, the second VBO includes the normals?

As TW pointed out, you don't. These things are just data that's passed to your shader. Then in the shader you decide that those things you got are normals, and then decide that you want to use these normals for something (lighting, for instance).