• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 6447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

G''Day all. I''m reading my way through a number of tutorials, examples, etc for OpenGL. I''m trying to understand in THEORY how a simple 3D world *works*. I''ll explain. From my understanding, when an application (lets say a simple game level) is initilised successfully, a playing field and some boxes are randomly placed ontop of the ground / arena. How does the program understand the center of each box / model? Does it need to do this? How does it understand that you can say -> Ok. FOR EACH PLAYER MODEL (eg. soldier that runs around shooting), go into it''s CENTER, then ? I just don''t understand how a simple 3D World knows how to place and handle objects in it''s world, and how it can access them when i needs to? What are the technical explanations here please? So then i can research further what these mean, so I can attempt it. -PK-

Share this post


Link to post
Share on other sites
Advertisement
What i ment was, how do u know how to control various models to move around a (dynamic) world? I had a quick look over witchlord's semi-tutorial, and he/she mentioned matrix's etc.

Is this the key?

what are they used for?

It's pretty basic loading in a few models from an external file. Then placing these models at hardcoded points. That's pretty basic.

But what are the topics i need to research about to learn how to control these models in a 3D environment? How to *jump into* model-1 and move it one unit *forward*, etc....

things like that...



-PK-

Edited by - Pure Krome on June 21, 2000 12:08:00 AM

Share this post


Link to post
Share on other sites
I''m a male

I suggest you read my tutorial a little more thorough this time. It describes exactly what you need. After that and you still have questions I''ll be glad to answer them for you

- WitchLord

Share this post


Link to post
Share on other sites
Thank WitchLord. I will. I too am short on time so i just checked to see if the link worked =)

First thing i noticed was the word matrix.
second thing i noticed was the DIRECTX stuff ... i''m doing this in OpenGL -> but the theory is the same, which is what i''m after =)

thank you again for starting me off in the right direction.

male dude =)



-PK-

Share this post


Link to post
Share on other sites
I don''t think OpenGL uses matrices, but I wouldn''t know, I haven''t tried it. A matrix is a tool for manipulating series of linear equations. What it is typically used for in 3d graphics however is transformations. A point or vector is represented by a 1x4 matrix. like this:

[ x y z 1 ]

which are the x, y, and z coordinates and 1, which is a normalizing factor or something similar, which you need to perform translations. To perform transformations, you need to multiply the matrices. To translate a point, you use a translation matrix, which is:


[ 1 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
[ tx ty tz 1 ]

where tx, ty, and tz are the amounts to translate by.

to multiply them you do:

[ x y z 1 ] * [ 1 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
[ tx ty tz 1 ]

= [ (x*1+y*0+z*0+1*tx) (x*0+y*1+z*0+1*ty) (x*0+y*0+z*1+1*tz) (x*0+y*0+z*0+1*1)]

which evaluates to:

[ (x+tx) (y+ty) (z+tz) 1 ]

which does the translation. You can also combine the operations of matrices by multiplying them together and all sorts of useful things like that.


------------------------------
#pragma twice

Share this post


Link to post
Share on other sites
The way an object is stored it has its world coordinates (one point which is the centre of the object) and its local coodinates which are the coordinates of each point in the object relative the to the world coordinate. To find the position of any point in the object you just add the world coordinates of the object to the local coordinates of the point. (A point also has camera coordinates, which the world coordinates are transformed into by the world to camera coordinate transform). By keeping those separate, the program is able to animate the object in local coordinates and move it about in world coordinates. See?

------------------------------
#pragma twice

Share this post


Link to post
Share on other sites
OpenGL Does use Matrixies, and the High Level Function in GL edit the Matrixies for you(right?).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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-

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement