views:

34

answers:

1

Hello, I am rendering a scene in which I have two spheres. I am revolving a camera around one of them. What happens is counter-intuitive. When the camera goes around the sphere the other gets in front of it when you'd expect it to be behind. So it appears as though the spheres aren't revolving around each other and the one the should go around is always upfront. Please help.

Here is the code that renders the scene:

glLoadIdentity();

[self positionCamera];

glutSolidSphere(2, 12,12);

glPushMatrix();
glTranslatef(5, 0, 0);
glutSolidSphere(0.5, 12,12);
glPopMatrix();

glFlush();  

This block is part of a class that gets called on using.

[NSTimer scheduledTimerWithTimeInterval:DEFAULT_ANIMATION_INTERVAL 
                                 target:self 
                               selector:@selector(drawRect) 
                               userInfo:nil 
                                repeats:YES];

And

-(void)positionCamera{}

Contains math do camera revolution and gluLookAt()

+1  A: 

Basically what you're doing now is a back-to-front rendering, where the last primitive drawn will always appear be drawn over the others.

What you want to do is enable the depth test, where the depth of each fragment will be compared before being drawn on screen. glEnable(DEPTH_TEST) should help.

Martin Cote
I have a GL initialization class where I do just that. I measured the depth buffer. int depth; glGetIntegerv(GL_DEPTH_BITS, NSLog(@"%i bits depth", depth)It returns 0. Can this be the problem?
seaworthy
You definitely need to have a depth buffer for depth testing. I'm not sure why it wouldn't exist, but check your initialization code. You can always try explicitly creating a depth format texture, just to make sure it's supported by hardware (I'm not familiar with Objective-C, though, so no code sample).Depending on how far your objects are from the camera, a 16 or 24 bit depth buffer should be plenty. If none of that works, you can always use trig to find out which should be in front and render that one last. But depth testing is definitely the better options.
peachykeen
Great! I've set a 24 bit buffer for my apps window. IB lets you do that easily using the Inspector. It works. Thanks guys!
seaworthy