# Thread: OpenGL - Getting a Grip on Perspectives

1. ## OpenGL - Getting a Grip on Perspectives

Is there any way to back up the camera up or something so that what I'm viewing isn't just like a -1.0 - 1.0 space? Like, I am really bad with opengl terminology and stuff right now but I think what I have right now is like:

Code:
```                Z = 0.0	 ----------->	Z = X

Some
X-Y			Other
/				Space Bound
/	S			That
/	P                     	Might
camera ----	A			Be The
\	C			Other
\	E			Side
\				Of A
Frustrum```
And my XY space at 0.0 is bounded by the top of the frustrum which is a square of -1.0X, -1.0Y to 1.0X, 1.0Y. I just want my XY space to be bounded by something arbitrary like -100.0X, -100.0Y to 100.0X, 100.0Y

I have no idea what functions to use or anything.

2. >Is there any way to back up the camera

glTranslate(0.0f, 0.0f, 10.0f);

3. You need a camera class.

Essentials for camera:

Right vector
Up vector
Look vector

Rotate on X
Rotate a set angle about the up vector. Known as yaw in an airplane.

Rotate on Y
Rotate a set angle about the right vector. Known as pitch in an airplane.

Rotate on Z
Rotate a set angle about the look vector. Known as roll in an airplane.

Create view matrix using final vectors
Right.x,Right.y,Right.z,0.0f
Up.x,Up.y,Up.z,0.0f
Look.x,Look.y,Look.z,0.0f
-DOT(cameraPos,Right),-DOT(cameraPos,Up),-DOT(cameraPos,Look),1.0f

Walk
Translate an arbitrary amount of units along the look vector.

Strafe
Translate an arbitrary amount of units along the right vector.

Fly
Translate an arbitrary amount of units along the up vector.

4. I understand that there's different matrices I could do that translation to. And if I understand corrently I do stuff with the identity matrix when I do -- or -- god this is really wierd because I don't know terminology. So, I say like glloadidenty, gltranslatef, and then glvertex3f points which are within the range (-1.0, -1.0) to (1.0, 1.0), and the gltranslatef moves them around. Can I gltranslatef some other matrix than the identity matrix to make it so that the vertexes I add can be in some range like (-20.0, -20.0) (20.0, 20.0)?

5. It's called translation. Your model starts in model or local space and then you translate to world space, or where it is in the world.

Your camera also has a position in the world and this is represented in the view matrix.

6. I was a little confused applying what you said with my documentation I was reading. It said this

Code:
```glMatrixMode sets the current matrix mode. mode can assume one of three values:

GL_MODELVIEW
Applies subsequent matrix operations to the modelview matrix stack.

GL_PROJECTION
Applies subsequent matrix operations to the projection matrix stack.

GL_TEXTURE
Applies subsequent matrix operations to the texture matrix stack.```
So I put my things in model space which are viewed by the gl_modelview or view matrix? If so, if I translated this modelview matrix, could I view some area that has that (-20.0, -20.0) (20.0, 20.0) model space range? If I translated this modelview matrix, would I glGetMatrixMode(GL_MODELVIEW) instead of glLoadIdentity()? What does the identity matrix have to do with how I view things?

7. Well first off I'm a DirectX guy but we are not talking about API's here. Doing 3D graphics requires at least a basic understanding of matrices, transforms, and vectors.

You should know that matrix multiplication or concatenation is cumulative. This means that the result of one matrix concatenated with the result of another matrix will yield a matrix representing both matrices.

So here is the short and simple of a local or model space to world space transform.

1. Rotate in model space. Rotates around the center of the model.
2. Scale in model space.
3. Translate to world coordinates.
4. Rotate in world space
5. Scale in world space.

Usually you won't have steps 4 or 5 unless you are doing something like planets rotating around a sun or something similar.

Your view matrix is what will transform the world matrices, what I just explained, into view space also known as camera space in some texts.

Your projection matrix is what will transform from view space to screen space. Screen space is the final divide by homogenous w that determines what 2D pixel to use to represent the current 3D orientation of the vertex.

If you want to get out of just viewing one model you must create a camera class. Just using look at matrices is not a very good way of describing a camera. A very good way to describe a camera is the one I showed you. That final matrix in my post is what you would use as your modelview matrix. Then in the render for each object you would create a world matrix to orient the model in the world. This then will be concatenated with the view matrix and then the projection matrix to arrive at the final display.

OpenGL differs somewhat from DirectX in it's 3D orientations but I'm sure either type of orientation can be represented in both API's. So when talking about 3D graphics in the general sense of transforms and vectors it is normally best to refer to fundamental 3D principles rather than a specific API's implementation of the principles.

I recommend getting several books on 3D. I have well over 20 or so with 2 more on the way and more I plan to get. Books are really the only way to begin to understand this stuff and you will need multiple sources because there isn't a single source that will tell you everything.

If you cannot afford books www.gamedev.net is a very good place to learn about matrices and 3D vectors. Prior to stepping into 3D you should understand or have a basic grasp on:

Matrices
• Cumulative properties of matrices
• Matrix concatenation
• Transpose of a matrix
• Inverse of a matrix
• Rotation matrix
• Translation matrix
• Scale matrix
• Axis angle rotation matrix
• View matrix
• Projection matrix
• Shear matrix (rarely used)
• Reflection matrix

3D Vectors
• Dot product, also called the inner product, and it's uses
• Cross product
• Multiplication by a scalar or non-vector quantity
• Vector multiplication
• Vector subtraction
• Length of a vector and how to retrieve it
• Normalized vectors, also called unit-vectors, and how to compute them
• How to transform a vector by a matrix

That's all I can think of at the moment.

8. Okay, I will try to learn these stuffs, they're all familiar terms and I had to do basic stuff with some vectors on the sat physics subject test that I took yesterday, but I should probably become more familiar and buy books. But, if you could help me, I think what I want to do right now is a walk, could you help me try to implement it? I can't tell what I want to perform a translation on? Something to change the perspective. I drew a picture to illustrate my awesome paint skills. Radical perspective changes dude! I just want to draw stuff at on the xy plane z = 0, but I want to move back the camera on the z so that I can see more stuff.

9. I basically just want to do the simple things described here http://www.chaospro.de/documentation...pengl_view.htm to move the camera view around. But I can not figure out what functions to use, and in what order to use them and stuff (this state machine stuff is wierd). These things combine matrix modes and identities and I can't really straighten those things out in my head. Perhaps I want something like gluOrtho2D to make everything flat against the XY plane, and then to move the camera backwards. The attachment illustrates the orthographic projection, which would be good if the near and far clipping planes were brought to z = 0, and the guys head moved back so I could see the XY plane like a big grid.

10. I've been trying stuff like this in an initialization routine. My confusion is clear in the code.

Code:
```    glViewport(0, 0, window.getwidth(), window.getheight());

glMatrixMode(GL_PROJECTION);

//
// Look down at scene at right angle
// Account for rectangularness of window w/ aspect
// Set near clip 1 unit away from camera
// Set far clip 200 unit away from camera
//

gluPerspective
(M_PI / 2, float(window.getwidth()) /
float(window.getheight()), 1.0f, 200.0f);\

glMatrixMode(GL_MODELVIEW);

//
// Camera position at (0, 0, 20)
// Looking at (0, 0, 0)
// And the direction of up is (0, 1, 0)

gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0);

//
// Wierd trial and error finds that things won't render unless
// the translate is there, and that I switch back to the projection
// matrix.
//

glTranslatef(0, 0, 20);
glMatrixMode(GL_PROJECTION);
Things are a few rectangles at z = 0.

11. Ahmahgawd! I think I get it! Edit: Only a half-eureka. Okay. So my problem was that my last way that each object thingy had a draw routine that did a glLoadIdentity, which messed up everything. So, now that I can't do that, how should I place things on my map? As in I do something like this:

Code:
```    std::vector<object>::iterator i = objects.begin();
std::vector<object>::iterator e = objects.end();

...

gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);

while(i != e)
{
i->draw();
i++;
}```
But the thing is, I use translations, rotates, etc in my drawing routines to position the object and stuff. The thing is, now that I don't use glLoadIdentity, all those translations and stuff carry over through each things draw routine. One way I can think of fixing it, is doing that gluLookAt thing inside the draw routine, and use glLoadIdentity also, but this doesn't make all that much sense to me. Is there a better way, like a way to clear the matrices that translate and rotate the object around, without messing up the way I'm viewing things with gluLookAt?

12. Haha. Real error that I got.

Code:
`Unhandled exception at 0x004389af in GL_Window.exe: 0xC0000005: Access violation reading location 0xfeeefeee.`

That is the noise of a dying silicate lifeform. Feeee! Feeee!

13. Awesome. Totally inspired to make a visualization or something.

14. I have heard that rotating with respect to an arbitrary vector is very difficult, and some things have said to translate to the origin and then do the rotation and move back. I have done this and it seems to work just in terms of a rotating shapes. They say that this rotating around an arbitrary axis involves using something called quaternions? Is this thing something that would be worth implementing in a camera class?

15. You can use quaternions or you can use axis angle rotations.

Quaternions have the benefit of smooth interpolation yet lack the intuitiveness of Euler angles.