drawing - Background object is drawn in front of foreground object in OpenGL? -


For test purposes, let me assume that I have attracted 2 teapots with glutSolidTeapot () ,

Like this:
  glColor3f (1.0f, 0.0f, 0.0f); // Red TiaPat Glassware Tape (1.0 F); GlColor3f (0.0 F, 1.0 F, 0.0F); // Green Teapot glTranslatef (0.0 F, 0.0F, 3.0F); GlutWireTeapot (1.0f);   

The camera is located at (x, y, z) = (0.0f, 0.0f, 5.0f) and I Z = - 1 (This camera position is # 1). I'm sure that you understand that green teapot is the closest object of the camera and the red is behind it. The problem is that the red color is drawn as it is in front of the green, which does not look right:

Example:

Now, if I take the camera (X, Y, Z) = (0.0 F, 0.0F, -5.0F) and Z = 1 (this camera position is # 2), I will see red teapot in front of green, this is normal behavior. The red is the object near the camera now and the green is behind a red color, everything is fine.

Example:

I know that this is something to do with the order, if I beat the above code (green caddy first), this will fix the problem. Camera position # 1 , But the problem will now appear in the camera's position # 2.

Why is this happening and how can I put objects behind the other objects, or at all?

You need to allocate a deeper buffer ) And then use glenable (GL_DEPTH_TEST)

because it is being done without deep testing, OpenGL does not respond to different depth relative to the camera only Does. When you enable depth testing, GL will reject all new pixels / pieces which are far more than the current closest.

If you want to rotate in your view, then you will probably miss GL_DEPTH_BUFFER_BIT to glClear , or otherwise the nearest piece from this previous frame.

In addition, your perspective matrix may be bad, it is likely to be special, because red teapot is prepared in front of green, while you attract them that there is a red in green There is a deep buffering. Therefore, the depth value itself is probably poor.

Comments