Alright, so I'm trying to learn OpenGL and I'm just not getting something quite basic: I don't understand how the coordinates work. Take the following code from the NeHe tutorials:
glTranslatef(0.0f,0.0f,-6.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //etc to make a triangle
I understand the first line moves the camera (is that a fair analogy?) 0 units on the x-axis, 0 units on the y-axis, and 6 units IN on the z-axis, but what I DON'T understand is the second line of code. I've tried playing around with the z-axis "camera adjustment" and I've discovered that if you make a triangle with points 1.0f units apart and a 0.0f on the z-camera-adjustment, you can't even see it all, just the colors and whatnot. So what I'm NOT understaning is HOW in the hell do these coordinates work? Am I just ALWAYS going to be sunken in the screen 6.0 "units"?
I believe my confusion stems from this line, which NeHe never really explains:
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
I've adjusted nearly everything on that line to learn what it does, and the only thing that changes anything is the second parameter. Will someone please explain these strange units. Oh, right, a side note: I DID figure out that 0.0, 0,0, 0.0 was the center of the screen, but beyond that, I'm stumped
Thanks guys!
I understand the first line moves the camera (is that a fair analogy?)
You don't move the camera. Instead, you move the rest of the universe Seems somewhat unintuitive, but it explains why you need to translate in the opposite direction of where your camera is.
As for the rest of your questions, you really should look at the OpenGL specification. It lists all the operations that happen to the parameters you pass to glVertex() and how it ends up on screen.
Hmm. I just saw something in NeHe that made me have another question. Right now, I'm drawing things with glVertex3f, but there's a function like glVertex3ub, maybe? That would use regular cartesian coordinates? I just learned about glColor4ub. Very fun stuff! Fast!
Right now, I'm drawing things with glVertex3f, but there's a function like glVertex3ub, maybe? That would use regular cartesian coordinates?
No. All versions of glVertex*() do exactly the same thing. You just get to pick the data type to send to the GL.
They all use regular Carthesian coords. But OpenGL allows you to set up a number of transformations applied to all vertices before drawing. In a standard 3D environment, the necessary transformations are, in this order:
1. The model transformation (where is the current object located?)
2. The camera transformation (where is the camera located?)
3. Frustum (what is the viewing angle, range, and aspect ratio?)
4. Projection (map 3D coordinates onto 2D coordinates)
5. Viewport (map logical units onto pixel values)
You always specify logical 3D units before all of these transformations. OpenGL automatically applies all the above to your coordinates, unlike allegro, where you specify pixel values directly. What you need to understand is what each of the above transformations do and how to set them up properly.
OpenGL uses 4x4 matrix operations to perform the above transformations; 1. and 2. are done through the MODELVIEW matrix, 3. and 4. through the PROJECTION matrix. 5. can be specified through glViewport().
You should write a software renderer. You'll learn ALL about 3d that way;
1) 3d math (vectors and matrices)
2) handling mesh data
3) rasterizing triangles
4) frustum culling
Unfortunately there aren't any really good comprehensive resources around.
edit; aside from reading the sources of whatever software renderers you can find including allegros 3d stuff.
For a good software referece try the following link, its meant for basic but the idea isnt.
[url http://petesqbsite.com/sections/express/issue2/index.html#3d]