PDA

View Full Version : Why is my camera moving forward in the negative Z axis?

hashbrown
09-07-2016, 02:19 PM
Let's say my camera is at [0,0,0] of the world, would it be looking towards the negative or positive Z? I know it all depends on how I set up my coordinate system, but what would be the usual way of setting up?

Right now if I walk forward, as soon as I run my application I'd be walking towards negative Z. If I move a mesh forward (towards the camera), that mesh would be moving positive Z. I'm guessing the meshes are truly facing forward relative to the world.

Also, if I move the camera behind the mesh, rotate 180 in order to look at the mesh again, and then move the mesh in the positive X, the mesh moves to the left, instead of the desired right.

I understand the camera views the world inverted, but I'd really like to get everything in the same space. This also affects how I construct my meshes (counter or clock wise triangles). A little confused here sorry :sorrow: Hope this makes sense.

john_connor
09-07-2016, 02:56 PM
Also, if I move the camera behind the mesh, rotate 180 in order to look at the mesh again, and then move the mesh in the positive X, the mesh moves to the left, instead of the desired right.

as it should be
the mesh is located in a "world" coordinate system and the movement is (i assume) described "globally"
if you want your mesh to move to the "positive X axis" of your cameras local coordinate system, you first have to describe "positive X axis" in global coordinates and add this resulting vector to the global position of the mesh
http://www.learnopengl.com/#!Getting-started/Camera
Note: the Z-axis of the camera actually points backwards!

by the way: almost every model i used until now uses counter-clockwise triangles, so:
glFrontFace(GL_CCW);// which is the initial value (https://www.opengl.org/sdk/docs/man2/xhtml/glFrontFace.xml)

hashbrown
09-07-2016, 04:33 PM
as it should be
the mesh is located in a "world" coordinate system and the movement is (i assume) described "globally"
if you want your mesh to move to the "positive X axis" of your cameras local coordinate system, you first have to describe "positive X axis" in global coordinates and add this resulting vector to the global position of the mesh

Note: the Z-axis of the camera actually points backwards!

by the way: almost every model i used until now uses counter-clockwise triangles, so:
glFrontFace(GL_CCW);/

Thanks John, it makes sense now. I'll read the article carefully. One last question though: when creating meshes, should I construct my triangles from the point of view of my camera, or should I create them the other way around? In other words, looking down the negative z. I guessing it should be the other way around (down the positive Z).

GClements
09-07-2016, 05:16 PM
OpenGL conventionally uses a right-handed coordinate system for object space and eye space, while clip-space and NDC use a left-handed coordinate system.

Ultimately, you're free to use whatever conventions you like; numbers are just numbers. But if you don't follow the existing conventions, you're likely to run into issues with existing code, tutorials, textbooks, etc.

john_connor
09-08-2016, 01:07 AM
when creating meshes, should I construct my triangles from the point of view of my camera, or should I create them the other way around? In other words, looking down the negative z. I guessing it should be the other way around (down the positive Z).

if you load meshes from files (like .obj etc), just dont care about it: assume GL_CCW
if it looks a little weird (like half of the model is missing and you see it from within), try GL_CW
if you dont want to care about it, disable culling ..

if you want to create a cube (from 12 triangles) manually, make them in counter-clockwise order viewed from outside
if you want to make a skybox, make the triangles in counter-clockwise order viewed from inside

for example:
float vertices[] = {
0, 0, 0,
1, 0, 0,
0, 1, 0,
};

thats just a triangle, so there is no "inside" or "outside", culling would just make it disappear when you look at it from behind, but if you have 3D models, where you can say: it has a "inside" and a "outside", like a plane / helicopter / car / whatever, just assume GL_CCW, enable culling
if you are not sure, disable culling ..

hashbrown
09-08-2016, 10:42 AM
Now I'm clear, thanks guys. I got it now. :)