Skip to main content
gamedev.net
🔒 Locked

Model/View Transformation

Started by mohanp Oct 15, 2008 at 12:47 PM 3 replies 650+ views
Original Post
mohanp
mohanp
Hi, I am drawing a cube (2 units on all sides) that extends from -1 to 1 on Z axis. I have defined a view volume (parallel projection) that covers 3 units on all sides (glOrtho (-3.0, 3.0, -3.0,3.0,-3.0,3.0). The cube faces are differently colored so that i know what faces i am looking at. The camera by default is at (0,0,0). I do the following things. 1) glTranslate on Z axis with 0.0, 1.0 and 2.0, i see the front face. If i move greater than 2.0 units, i see the back face. I am unable to explain this. Isn't the camera pointing towards the -Z axis at the begninning ? Then why am i seeing the front face. Then when i cross 2 units, i see the back face. I am unable to understand this. To see the front face, don't i have to be in front of the cube i.e something like -1.1 ? Also, for seeing the back face, don't i have to move forward at least 1.1 and then turn around ? I am confused. 2) glTranslate on Z axis with values > 4.0 or -4.0, i don't see the cube. I can see that the camera/viewpoint is too far. What is too far ? How do i know that i have gone too far without trying different values ? Interestingly, if i rotate the cube even a little bit, this problem goes away i.e i can see the cube from any distance. Thanks for any help. mohan
mohanp
mohanp
Hi,

I guess i am thouroughly confused. Here is the code..

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.0, 3.0, -3.0,3.0, -3.0,3.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, 2.1f);

glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,-1.0f); /*Top*/
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,1.0f,1.0f);
/* Other Vertices with different colors */
glEnd();
}


My front face is red and back face is yellow. When i try value 2.1f and greater, i see the yellow face. Anything less than that i see the red face. Why is it so ?

I actually started with an opengl es program that draws a square of 1 units extending from -0.5 to 0.5 around the center. It has the default view volume (No glortho calls). The square draws well but the camera is at the origin. When i read the Red book, it says the camera is at the center and you need to move away to see the object. If so, don't you always need glTranslate(0.0, 0.0, -1.0) or some such thing to see the object. I guess i am confused about the Camera poistion and the Viewer. By translate either i am moving the camera or the model. Once the model is drawn and the view volume is defined, where is the viewer ?

Sorry if i am really confusing things.

-thanks
mohan
bzroom
bzroom
The viewer is an imaginary concept, ultimately you're transforming world space geometry (your front and back faces) into view space (which happens to be in your world).

If you had a ball at the origin, and you wanted to see it, the viewer would have to move to some non-zero location and face the origin. Lets say it translated to (X,Y). To transform an object from world space to view space you'd translate the object (-X,-Y).

Your view space is aligned to the -Z axis. Which means in order to see the ball from from the starting location, you'll need to translate the camera in the positive Z direction, which transforms the world in the negative Z direction.

If you used gluLookAt, it'd be From(0,0,D), To(0,0,0), Up(0,1,0)
if you just use glTranslate it'd have parameters (0,0,-D).

I suggest you play around with gluLookAt a bit, it's much more natural. Then replace glLookAt with your own implemenation and you should be able to see how things work.

My thoughts for why you can see the other face with values < 2.0f is that the viewer is now inside the box and you're seeing the other side.

If i were you i'd try something like this:

const float r = 4.0f;const float speed = 1.0f;float angle = 0;void Step(float elapsedTime){ angle += speed * elapsedTime;}void UpdateCamera(){ glLoadIdentity(); gluLookAt( cos(angle) * r, 0, sin(angle) * r,            0,0,0,            0,1,0 );}


Also, I'd highly suggest expanding the near and far planes of your projection (-1000,1000 instead of -3,3) just to make sure it's not going to get clipped.
mohanp
mohanp
>The viewer is an imaginary concept, ultimately you're transforming world space >geometry (your front and back faces) into view space (which happens to be in >your world).

Ok. So, can i assume that the imaginary viewer is behind the camera or together with the camera ?

>If you had a ball at the origin, and you wanted to see it, the viewer would >have to move to some non-zero location and face the origin. Lets say it >translated to (X,Y). To transform an object from world space to view space >you'd translate the object (-X,-Y).

Then, how would you explain the opengl es program i mentioned. Note that this is the default program that Xcode generates when you choose opengl es template. Without any glTranslate (and no gluLookAt in opengl es), how can i see the square ?

>Your view space is aligned to the -Z axis. Which means in order to see the >ball from from the starting location, you'll need to translate the camera in >the positive Z direction, which transforms the world in the negative Z >direction.

>If you used gluLookAt, it'd be From(0,0,D), To(0,0,0), Up(0,1,0)
>if you just use glTranslate it'd have parameters (0,0,-D).

I tried both gluLookAt and glRotate to see the different sides of the cube.

>I suggest you play around with gluLookAt a bit, it's much more natural. Then >replace glLookAt with your own implemenation and you should be able to see how >things work.

>My thoughts for why you can see the other face with values < 2.0f is that the >viewer is now inside the box and you're seeing the other side.

Hmm, if the camera is facing down the -Z axis, should i not be seeing what is in front of me ? That's where i am confused.

>If i were you i'd try something like this:
>
>const float r = 4.0f;
>const float speed = 1.0f;
>float angle = 0;
>
>void Step(float elapsedTime)
>{
> angle += speed * elapsedTime;
>}
>
>void UpdateCamera()
>{
>glLoadIdentity();
>gluLookAt( cos(angle) * r, 0, sin(angle) * r,
> 0,0,0,
> 0,1,0 );
>}

Okay, i will try this.

Thanks
mohan
mohanp
mohanp
I replaced the glTranslate in the code with this:

gluLookAt(0.0,0.0,X, 0.0,0.0,0.0, 0.0,1.0,0.0);

Where X is:

1) X is 0.01, I see the front face.
2) If X is 4.00, I see the front face
3) If X is 4.01, I don't see anything

(1) can be explained if we assume that the front face extends half way through and the back face is other half (See 4) below.
The glortho parameter extends between (-3.0, 3.0) on Z axis. If so, how can i see the cube when i am at (2). Then why not at 3 ? Shouldn't i be already clipped at (2) itself.

4)X is -0.01, I see the back face.
5) X is -4.00, I see the back face
6) X is -4.01, I don't see anything

4 can be explained the same logic as (1). For (2), how am i able to see the back face if my camera is pointing forward ?

Thanks
mohan

thanks
mohan

Topic Locked

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

Sign in to reply to this topic.