OpenGL ES - Drawing a simple cube help.
Ok so I am trying to draw a cube in OpenGL and I just can't seem to get something right. When I run the program in the simulator or the phone it looks as if the cube is missing pieces and parts are attached to the wrong places. The whole cube almost seems semi-transparent. It creates an odd looking optical illusion though.
At first I thought it might be the vertices but later I tried exporting a cube model from Blender and I got the exact same results.
Maybe someone else can offer insight into this? Below is the code drawing my cube.
Thanks
At first I thought it might be the vertices but later I tried exporting a cube model from Blender and I got the exact same results.
Maybe someone else can offer insight into this? Below is the code drawing my cube.
Code:
- (void)drawView {
// Replace the implementation of this method to do your own custom drawing
const GLfloat squareVertices[] = {
// FRONT
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
// BACK
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
// LEFT
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
// RIGHT
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
// TOP
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
// BOTTOM
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
};
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glRotatef(3.0f, 3.0f, 0.0f, 1.0f);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glVertexPointer(3, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
glFlush ();
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}Thanks
For depth testing to work, you need to create a depth renderbuffer and attach it to the view's FBO.
Alternatively, for a convex object like a cube, you don't need a depth buffer. Turn on backface culling.
Alternatively, for blended objects, sort your drawing manually.
Alternatively, for a convex object like a cube, you don't need a depth buffer. Turn on backface culling.
Alternatively, for blended objects, sort your drawing manually.
arekkusu Wrote:For depth testing to work, you need to create a depth renderbuffer and attach it to the view's FBO.
Alternatively, for a convex object like a cube, you don't need a depth buffer. Turn on backface culling.
Alternatively, for blended objects, sort your drawing manually.
Thanks for the help mate! Works perfectly now. Such a simple fix too.
Perhaps there is a better place for this, but has anyone else noticed difference between OpenGL on the simulator and on the iPhone?
I am using the OpenGL template application (the one with the spinning square) and just commenting out the line glClear(GL_COLOR_BUFFER_BIT); in EAGLView drawView:
I was making a drawing application, so I don't want to clear the buffer every frame. On the simulator, it works as expected, but on the iPhone it seems to cycle between 3 different buffers.
Any idea what is going on?
I am using the OpenGL template application (the one with the spinning square) and just commenting out the line glClear(GL_COLOR_BUFFER_BIT); in EAGLView drawView:
I was making a drawing application, so I don't want to clear the buffer every frame. On the simulator, it works as expected, but on the iPhone it seems to cycle between 3 different buffers.
Any idea what is going on?
Typically no guarantee is made about preservation of buffer contents (they could be cycled, flipped, copied, etc.). If you need the contents preserved, you might have to draw to an FBO, which will make such guarantees.
Read the documentation for kEAGLDrawablePropertyRetainedBacking.
On the desktop the equivalent is kCGLPFABackingStore.
On the desktop the equivalent is kCGLPFABackingStore.
GLPaint seems to use kEAGLDrawablePropertyRetainedBacking to preserve the buffer, and that works great.
I now have a problem with glReadPixels. The color values from glReadPixels are from a delayed version of what is currently showing onscreen. Almost as if glReadPixels reads from an older buffer.
I tried doing an extra buffer swap right before a did a pixel read, but that seems to have no effect.
Is there a way to sync up the buffers so glReadPixels works? Or perhaps an alternative method of reading a pixel's color from the current buffer?
I now have a problem with glReadPixels. The color values from glReadPixels are from a delayed version of what is currently showing onscreen. Almost as if glReadPixels reads from an older buffer.
I tried doing an extra buffer swap right before a did a pixel read, but that seems to have no effect.
Is there a way to sync up the buffers so glReadPixels works? Or perhaps an alternative method of reading a pixel's color from the current buffer?
mdonahoe Wrote:GLPaint seems to use kEAGLDrawablePropertyRetainedBacking to preserve the buffer, and that works great.
I now have a problem with glReadPixels. The color values from glReadPixels are from a delayed version of what is currently showing onscreen. Almost as if glReadPixels reads from an older buffer.
I tried doing an extra buffer swap right before a did a pixel read, but that seems to have no effect.
Is there a way to sync up the buffers so glReadPixels works? Or perhaps an alternative method of reading a pixel's color from the current buffer?
That sounds like a bug. Please report it at https://bugreport.apple.com. If at all possible, please include an example program that shows this issue.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Simple OpenGL ES problem | soulstorm | 3 | 3,336 |
May 14, 2009 03:53 PM Last Post: AnotherJake |
|
| Cube auto-rotate | sakrist | 2 | 2,622 |
May 5, 2009 03:31 AM Last Post: Ingemar |
|
| Simple OpenGL 2d test - nothing displaying | SaxMan | 8 | 5,395 |
Jan 19, 2009 12:04 AM Last Post: TythosEternal |
|
| opengl text/font drawing from CGContext | fretmunky | 11 | 11,396 |
Dec 7, 2008 11:29 AM Last Post: fretmunky |
|
| Very simple OpenGL problem | evangs | 10 | 4,801 |
Dec 10, 2007 01:32 PM Last Post: wyrmmage |
|

