Reputation: 429
Hi guys I have a conceptual question in the render function of opengles on the iPhone. Say I have a cube. The cube has 6 faces and consequently 12 triangles. If I calculate a normal for each triangle I have 12 normals, each normal has x,y,z cordinates. Consequently I have an array of 36 floats. In my render function I want to pass the x,y,z cordinates to the vertex for some lighting related calculations. This is my call to achieve that
glVertexAttribPointer(_simpleLightNormalAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float), duplicateNormals);
The indexes 0,1,2 contain the x,y,z cordinates of the first triangle and so on. How does opengl know that indexes 2,3,4 contain the x,y,z cordinates of the 2 triangle?
Any help would be much appreciated/
Upvotes: 0
Views: 104
Reputation: 100612
OpenGL doesn't define a vertex the way you do. In classical fixed-pipeline OpenGL a single vertex is a location and/or a normal and/or a colour and/or a texture coordinate.
So normals associate with vertices, not with polygons. Looking at it another way, if you're supplying normals you generally need to provide each vertex separately for each face that it rests on.
So in the case of a cube, you should supply 24 vertices, in six groups of four. All the vertices in each group will have the same normal but different positions.
Addition:
In ES 2 basically the same rule applies: all properties are per vertex and are interpolated across such faces as you specify. So you're still not really in a position to specify properties per face. The exception to this is that you can change uniforms at will, so in your case you could use a uniform for the normal though you'd end up drawing two triangles, changing the uniform, drawing two more triangles, changing the uniform again, etc, which would be an incredibly inefficient way to proceed.
So you'll probably still need the 24 vertices — one per location per face.
Upvotes: 1
Reputation: 26
If you are passing each triangle's vertices separately (as the OpenGL Game project does and as you seem to be doing) rather than using indices then you need to specify a normal for each vertex. For a cube with sharply defined edges you would probably want to send the same perpendicular normal for each vertex on a face.
If you really want/need to specify fewer normals, you should look into using indices. Then you would define the 8 distinct vertices in the cube, the 8 normals that go with them, and define your triangles using an array of indices for your vertices. Bear in mind that with this approach you will have multiple faces sharing normals. This can be a good thing if you are looking for a smooth lighting effect (like with curves) or a bad thing if you want sharp, distinct edges (like with cubes).
Hope this helps
Upvotes: 0