开发者

Why when I render a scene with a revolving camera objects that you'd expect to appear behind others don't do that? OPENGL Objective-c

开发者 https://www.devze.com 2022-12-27 03:02 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号