I’ve been doing OpenGL for a while, but have just recently jumped into VBOs. Right now I’m just trying to render a “soup” of unconnected triangles, where each vertex has an xyz position and an xyz normal. I’ve put this into two separate VBOs, which may be a bad idea.
Here’s my current code. It’s in JRuby, but I think it’s still pretty easy to read.
# put the stored data into the their respective VBOs
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, @vertexVBO[0])
gl.glBufferData(GL.GL_ARRAY_BUFFER, 9*numTriangles*BufferUtil::SIZEOF_FLOAT, vertexData, GL.GL_DYNAMIC_DRAW)
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, @normalVBO[0])
gl.glBufferData(GL.GL_ARRAY_BUFFER, 9*numTriangles*BufferUtil::SIZEOF_FLOAT, normalData, GL.GL_DYNAMIC_DRAW)
The vertexData is packed in per vertex like (x1,y1,z1), (x2,y2,z2), etc. and not interleaved. The normals are stored the same way.
And I draw it like this
gl.glBindBuffer(GL::GL_ARRAY_BUFFER, @vertexVBO[0])
vertexLocation = @dummyShader.attribLocation(gl, "vertex")
gl.glVertexAttribPointer(vertexLocation, 3, GL::GL_FLOAT, false, 0, 0)
gl.glEnableVertexAttribArray(vertexLocation)
gl.glBindBuffer(GL::GL_ARRAY_BUFFER, @normalVBO[0])
normalLocation = @dummyShader.attribLocation(gl, "normal")
gl.glVertexAttribPointer(normalLocation, 3, GL::GL_FLOAT, false, 0, 0)
gl.glEnableVertexAttribArray(normalLocation)
gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3*numTriangles)
gl.glDisableVertexAttribArray(vertexLocation)
gl.glDisableVertexAttribArray(normalLocation)
It made sense to me and I was getting the correct vertices on screen, so I assumed it was working, but then I realized my normals weren’t being sent in to my shader. Then I saw I’m calling glBindBuffer twice before calling glDrawArrays, which might be totally incorrect.
Can I use two buffers to draw from or do I need to find a way to interleave the data. If so, how should it be interleaved? Right now I’m not using indices, which I thought would work fine, but I’m not sure if that will work with multiple attributes. Will it? Or is the bind issue not related at all?