[Newbie] Some help to explain some 3D World Theory

Started by
15 comments, last by Pure Krome 23 years, 10 months ago
Right, OpenGL matrices are handled VIA a high level interface. As for the original question:

I think what you want to know if how you move your objects once they are loaded. It''s basically simple. When you load an object, you load it from a file into a structure or class. Usually, the class will have one or more members that store the objects location( float x; float y; float z; ). Say you wanted to move the object forward in world space. In OpenGL you would say, [ object.z--; ]. Then you use [ glTranslate( object.x, object.y, object.z ); ] just before you render the object.

Better example:

float z; // object''s z position

void MoveForwad()
{
z--;
}

void Render()
{
glPushMatrix(); // Save the original matrix.
glTranslate( 0, 0, z ); // Adjust for the object''s position.
glBegin( GL_TRIANGLES );
// Your triangles here.
glEnd();
glPopMatrix(); // Restore the original matrix from glPushMatrix();
}



---
www.crazycrackerz.org
Advertisement
Of course I left out stuff like clearing the buffer and setting up the modelview matrix, if you don''t understand email me and I''ll try to give you a complete example.
ok. before i get REALLY deep into all this 3D theory...

looking at the 2nd post above this one, you say

glPushMatrix(); // Save the original matrix.

-- OK ... that makes sence ..... --


glTranslate( 0, 0, z ); // Adjust for the object''s position.

Um.. translate what object? don''t u have to say
GRAB OBJECT (obj)

rotate (obj, xaxis, yaxis, zaxis)
translate (obj, x, y, z) ??

glBegin
... Trianges or whatever (all the co-ords of the poly).
glEnd


glPopMatrix(); // Restore the original matrix from glPushMatrix();

-- UM. why restore the matrix?? Doesn''t that overwrite the JUST CHANGED co-ords to the PREVIOUS state?


Please. i appologise if this is really basic stuff, but i just don''t get some of it =)

I will in time, so please bare with my basic questions.


--




-PK-
-PK-
glTranslate() modifies the matrix that is used for transforming all vertices that are sent to OpenGL. This way OpenGL doesn''t need to know anything about your objects. The same goes for glRotate() and all the other matrix functions.

When you render the object between glBegin() and glEnd() the vertices are transformed with the GL_MODELVIEW matrix to first move it into its world position and then into camera coordinates.

It works the same way as I describe in my tutorial, the only difference is that OpenGL has combined the WORLD and VIEW matrices into one.

- WitchLord

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

quote:
Um.. translate what object? don''t u have to say
GRAB OBJECT (obj)

rotate (obj, xaxis, yaxis, zaxis)
translate (obj, x, y, z) ??


no.

you basically ''tell'' opengl to modify all subsequent coords you give it by x. for example:

glTranslatef(1.0f, 0.0f, 0.0f)

will cause everything following it to be moved by 1.0 units on the x axis


as far as i know, glPushMatrix ''saves'' the current offset values, so you can revert to them after calls to glTranslateX and glRotateX by calling glPopMatrix. i may be wrong here (please, correct me if i am) openGL isnt really my thing...
Well my GL knowledge aint that big but I know this:

glpush saves the current "camera" position on the stac and further gltransform (move view to place object i x,y,z), glrotate (rotate using radians in x,y.z) modifice the temporary "camera view" and puts all new glbegins and glends made between glpush and glpop to use the temporary view which is restored to the former "original" as soon as a glpop is made.

This may seem weird but think of it like this:

You know the player (view) is at x=2000, z=-100 and y=10. You plot the scene for this coordinate (a couple of triangles with texture, that you have the coordinates for in a global mapview with its worldcoordninates =) and then you move the viewing matrix with glrotate and gltransform and you got the result you wanted.

Its a pitty that soo few drivers supports GL to its extens.

Just my 2
Death is lifes way saying your fired.
Pure Krome, you better read the Nehe''s Tutorials (excelent OpenGL tutorials) and after that read the Red Book (The OpenGL Programming Guide, ''nuff said). Learning OpenGL here in the message board will be very hard... ;P

And people, OpenGL works exactly like Witchlord said. The matrix commands (like glRotate, glPushMatrix, glTranslate) affect the current matrix (that you set using glMatrixMode). The ModelView matrix affects all the vertices that you send to the card, so when you rotate it, all the subsequent vertices will be rotated (since they will be multiplied by the matrix), and so on. Other things are affected by it as well, like lighting, but I explain REALLY bad, so I better stop right here

Nicodemus.

----
"When everything goes well, something will go wrong." - Murphy
Nicodemus.----"When everything goes well, something will go wrong." - Murphy

This topic is closed to new replies.

Advertisement