views:

503

answers:

3

I have a model of a snowman that I am loading from a .obj file. Everything works well except that when I use glRotatef() to rotate the model the head of the snowman will always render in front of the body. The nose of the snowman will also always render behind the head. This creates the effect that the snowman changes direction as he is rotating, but really the parts just not rendering in the right z order. Why is this occuring?

NOTE: all parts of the snowman are from the same .obj file created using blender.

rendering the model like this ( in the draw loop)

glVertexPointer(3 ,GL_FLOAT, 0, model_verts);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, model_normals);
glDrawElements(GL_TRIANGLES, num_model_indices*3, GL_UNSIGNED_SHORT, &model_indices);

rotating like this (in touchesMoved)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch = [touches anyObject];
 touchBeginPos = [touch locationInView:self];

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 CGPoint touchEndPos = [[touches anyObject] locationInView:self];
 glMatrixMode(GL_MODELVIEW_MATRIX);
 glRotatef(10, (touchBeginPos.y - touchEndPos.y)/4, -(touchBeginPos.x - touchEndPos.x)/4, 0.0f);
 touchBeginPos = touchEndPos;
}
A: 
eed3si9n
+5  A: 

Check that you haven't (by mistake) applied a negative (mirroring) scale to the scene. Also check that you're using Z buffering, perhaps you're not.

This page talks a bit about Blender's coordinate system, compared to another 3D system's, and is perhaps semi-obscure as a reference but I place a certain amount of confidence in it.

unwind
I addedglGenRenderbuffersOES(1, glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);to the createFramebuffer method. andglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glEnable(GL_DEPTH_TEST);in drawView
Joe Cannatti
@Joe: Thanks. It would be far easier to read that code if you edited it into your question, perhaps at the bottom with an "UPDATE:" headline or something to show that it's new.
unwind
A: 

UIKit coordinates for the textures are upside-down, so you have two options:

  1. adjust the V coord of your texture mappings to (1-vcoord)
  2. flip the image before drawing into the contex that you will later load into gl.

If you're using CG to load the textures like this, you can add the ScaleCTM:

CGContextRef context = CGBitmapContextCreate( imageData, ....
CGContextClearRect( context, ...
CGContextScaleCTM( context, 1.0, -1.0);  // flip the texture vertically
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
cigumo