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();
g开发者_Go百科lFlush();
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()
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.
精彩评论