I am working on an object (OBJ File) loader for my app on iOS, currently I have successfully read the vertices and the faces of the object, and I am now just adding colours to the imported models.
I am coming across a major problem now. Out of a cube with six faces, all coloured red apart from two opposite faces which are coloured blue. This cube is set to rotate so I can see all sides, bu开发者_Python百科t the colours do not appear correctly as shown in the video below:
http://youtu.be/0L2AIFkd2Qk
The blue faces only shows when the two blue sections overlap, I cannot figure out why - I am used to OpenGL for PC, is there something I am missing which is causing this strange colouring?
The colouring is achieved by sending a large float array of the format RGBA RGBA RGBA etc for each vertex;
// colours is the name of the array of floats
glColorPointer(colorStride, GL_FLOAT, 0, colors);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(renderStyle, 0, vertexCount);
[EDIT]
Problem has now been solved, I was just drawing all the triangles from the model calling
glColor4f(0.5f, 0.5f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 4, 4);
only once for the whole array, when I split this up for each face in the model it allowed me to use GL_DEPTH_TEST without causing the screen to go blank!
The problem is that GL_DEPTH_TEST is disabled. Opengl is not making any depth testing so all your objects look weird. In order to enable depth testing you must create yourself new render buffer. This is a small snippet on how to crate this buffer:
// Create the depth buffer.
glGenRenderbuffersOES(1, &m_depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES,
GL_DEPTH_COMPONENT16_OES, width, height);
This buffer is created for OpenGL ES 1.1, for 2.0 just delete all "OES" extensions. When you have created depth buffer you may now enable depth testing like this:
glEnable(GL_DEPTH_TEST);
Had this problem few times myself.
精彩评论