Skip to main content
gamedev.net
🔒 Locked

Rotate camera vector problem

Started by delbogun 5 replies 1.5k views
Original Post
delbogun
delbogun
I'm trying to implement back-face culling. The problem is that the object flickers all the time when I rotate the object. I'm rotating the camera vector when I rotate the object. Then I use the dot product of that camera vector with the polygon's normal to be able to determine if I need to draw the polygon or not. The thing is that it seems to work fine if I don't rotate the camera vector, so I think that the rotation is wrong. This is how I rotate the camera vector:

// Setup camera location
gluLookAt(0,0,60,0,0,0,0,1,0);
camera.x = 0;
camera.y = 0;
camera.z = 60;

// Rotate camera vector around Y-axis
int x = camera.x, z = camera.z;
camera.x = x * cos(angle)+ z * sin(angle);
camera.z = z * cos(angle)- x * sin(angle);

// Rotate and draw the object
glRotatef(angle,0,1,0);
DrawObject();






Does anyone know a solution to my problem?
Bob Janova
Bob Janova
Well, firstly, if you're using OpenGL you can turn on back-face culling in the environment, so you don't have to do it yourself at all. (I forget the function call, it's been a while, but it's very easy.)

Secondly, if you're doing this for interest and want to do it anyway: the dot product you want is (poly origin-camera position).(poly normal). The direction of the camera is irrelevant (though you might want to do is-in-view culling as well, for which (poly origin-camera position).(camera direction) will tell you if you're behind the camera).
delbogun
delbogun
Thanks.. but I probably used the wrong word. What I'm trying to do is to not render polygons that can't be seen from the view. For example a box have 6 squares. If the camera is looking streight on the box, only 1 of 6 squares is visible on the view, so I don't want to render those other 5 squares. The OpenGL function glEnable(GL_CULL_FACE) only stops rendering those polygons that are inside the box.

Since OpenGL doesn't have world coordinates, I have to work with object coordinates. That's why I try to create a camera vector that I rotate together with the object. Is there a easier way?
haegarr
haegarr
Quote:
Original post by delbogun
Thanks.. but I probably used the wrong word. What I'm trying to do is to not render polygons that can't be seen from the view. For example a box have 6 squares. If the camera is looking streight on the box, only 1 of 6 squares is visible on the view, so I don't want to render those other 5 squares. The OpenGL function glEnable(GL_CULL_FACE) only stops rendering those polygons that are inside the box.

The term "backface culling" is correct. The faces in OpenGL have 2 sides, FRONT and BACK, chosen by the order the vertices are provided to OpenGL. Since internally all faces are triangles, and pixels can be rendered first when all 3 vertices are known, OpenGL is able to drop rendering of the entire face as soon as all vertices are supplied. So, if configured correctly, OpenGL actually will drop those faces that are seen by their BACK, and only FRONT faces will pass.

IMHO OpenGL can do exactly what you want (like delbogun has stated already). Faces "inside the box" can be detected by OpenGL only by depth test, but that is another story.

Quote:
Original post by delbogun
Since OpenGL doesn't have world coordinates, I have to work with object coordinates. That's why I try to create a camera vector that I rotate together with the object. Is there a easier way?

You can, of course, work in another co-ordinate system, but that would be a waste of time, since you have to transform the entire mesh into that system. If, on the other hand, you transform the camera to object local space, you have to transform only a single co-ordinate frame.

[Edited by - haegarr on September 18, 2006 4:59:30 AM]
Bob Janova
Bob Janova
In the case you describe, OpenGL's culling will result in only one face being drawn (even discounting the depth test). I think GL_CULL_FACE does exactly what you want ...

By the way, I should have said this in my first reply, but flickering is almost certainly caused by the way you draw to the screen, and not by the OpenGL options you have set. GL will render a simple scene like you describe in negligible time. Make sure you're not clearing the view surface before drawing the GL render to it.
delbogun
delbogun
So GL_CULL_FACE will also skip polygons that are covered by other polygons? In that case I don't need to implement back-face culling. Could anyone else confirm that GL_CULL_FACE will only draw the polygons directly visible on the screen?

If I turn off the GL_CULL_FACE command, it doesn't actually flicker, but switches between drawing the polygons facing the camera and the polygons facing the other side. Hard to explain what I mean...

Thanks for all help
haegarr
haegarr
Quote:
Original post by delbogun
So GL_CULL_FACE will also skip polygons that are covered by other polygons?

Backface culling drops faces that are seen from the back (if configured so; also faces seen from the front can be dropped). At that moment OpenGL doesn't know anything about whether that face is covered by another face.

Covering is normally detected by comparing the depth of the pixel to write with the depth of the pixel already in the frame buffer. For that purpose the depth buffer and depth comparison function are available. But that happens ATM OpenGL is already writing pixels of the face to the frame buffer. Backface culling has been done prior to this.

Notice that backface culling in general assumes that the mesh is closed and opaque. In such cases all faces pointing away from the camera can be dropped. Faces pointing to the camera but covered by other faces pass the backface culling test but will either be hidden by depth test (if drawn _after_ the covering face) or will later be overwritten (if drawn (chronologically) _before_ the covering face).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.