Reputation: 2906
I have been struggling with this code for about two weeks now. I have created a new OpenGL project using the standard apple boiler plate code generated when creating a new OpenGL ES project. It usually consists of two spinning cubes.
I decided that I wanted to change the cubes to pyramids (triangle based). I got the code to work except for the fact that the pyramids are black, as the colour is not rendering correctly. At first I thought is was my normals, but I think they are correct. I would be most grateful if someone could help me with this problem. all the coordinates are in a header file I created.
I also discovered that some of the normal mathematics was out
GLfloat norms[36] = {
0, -1, 0,
0, -1, 0,
0, -1, 0,
1, 0.25, -0.5,
1, 0.25, -0.5,
1, 0.25, -0.5,
-1, 0.25, -0.5,
-1, 0.25, -0.5,
-1, 0.25, -0.5,
0, 0.5, 1,
0, 0.5, 1,
0, 0.5, 1
};
These are the correct norms. thanks for all your help GC
Upvotes: 0
Views: 197
Reputation: 16794
You cannot bind 2 vertex arrays at a time (glBindVertexArrayOES). You will have to create only 1 and then put all the data into it. Try this approach:
glGenVertexArraysOES(1, &_vertexArray); //create vertex array
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts) + sizeof(norms), NULL, GL_STATIC_DRAW); //create vertex buffer big enough for both verts and norms and pass NULL as data..
uint8_t *ptr = (uint8_t *)glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES); //map buffer to pass data to it
memcpy(ptr, verts, sizeof(verts)); //copy verts
memcpy(ptr+sizeof(verts), norms, sizeof(norms)); //copy norms to position after verts
glUnmapBufferOES(GL_ARRAY_BUFFER);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); //tell GL where verts are in buffer
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(verts))); //tell GL where norms are in buffer
glBindVertexArrayOES(0);
As for checking or computing the normals:
You can compute and check your normals for sharp edges (whitch is your case) with cross product. For each triangle defined by positions A, B and C:
s1 = B-A //s1 and s2 are just for easier reading
s2 = C-A
N = s1 x s2 //N is normal (not quite ready)
(cross product:)
N.x = s1.y*s2.z - s2.y*s1.z
N.y = s1.z*s2.x - s2.z*s1.x
N.z = s1.x*s2.y - s2.x*s1.y
Since ABC can be oriented in any way you might need to turn normal the other way.. You can use the dot product to get this info.. In your case take a point that is near the center of a shape and does not lie on any of triangles defining it (vector Center(0,0,0) just might do). Now for each A, B, C, N:
vector Average = (A+B+C)/3 //center of a triangle
if(dotProduct(N, Center-Average) > 0) N = N * -1
(dotProduct:)
dot(M,N) = M.x*N.x + M.y*N.y + M.z*N.z
After that you should normalize each normal (divide each component by lenght of a normal resulting in each normal having a lenght of 1) Alternatively you can set normalize flag on: glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_TRUE...
Upvotes: 1