PDA

View Full Version : something like glTranslate (with absolute values) !?



homeruniverse
07-04-2004, 03:35 AM
Sorry, here is a question from me again ...

I change the point where I create an object with "glTranslatef(x,y,z)" (or "glTranslated(x,y,z)").
Is there a way to set this point absolutely without using position of the old point ?

i.e: something like "searched_command(0,0,0)" and the object I create is set in the middle of the screen ..

nexusone
07-04-2004, 03:46 AM
You can reset the matrix by using glLoadIdentity() each time you move an object with glTranslate.
By that not the best way, since will work correctly if you are using a camera.

Better is to use glPushMatrix and glPopMatrix:

glPushMatrix(); Save current matrix
glTranslate(......); Translate object
draw_object(); Draw Object
glPopMatrix(); Restore matrix to state before we did the translate command

repeat for each object you draw with the push/pop matrix at the start and end of the object being drawn.

homeruniverse
07-04-2004, 03:59 AM
hmm...
It does not run as easy as it runs in my opinion.

precise example:
# here the point is somewhere on bottom-left
glPushMatrix()
glTranslated(0.0,0.0,0.0)
glBegin(GL_QUADS)
glVertex3f(-0.064, 0.064, 0.0)
glVertex3f( 0.064, 0.064, 0.0)
glVertex3f( 0.064, -0.064, 0.0)
glVertex3f(-0.064, -0.064, 0.0)
glEnd()
glPopMatrix()

If "glPushMatrix()" is the command I am searching for the quadrat should be shown in the centre of my screen because of the translation to x=0, y=0 and z=0 !?
But there is no difference between using "glPushMatrix()" and not using it.

What I did not understand ?

nexusone
07-04-2004, 04:15 AM
I maybe did not understand you question fully.

There are many factors that effect where an object is drawn on the screen, from the projection setting to the matrix calls like glTranslate.

You may have to adjust your projection settings or view port settings to get what you want.
Post your code and will take a look.

glTranslate(0,0,0) has no effect since it has zero translation.

glPush/Pop does not change the matrix or reset it, it save's and restores it to the state before it was used. It will not make an object center on the screen if you do not have to other settings correct.

nexusone
07-04-2004, 04:25 AM
Try this setup should draw in the center of the screen:

void Draw_scene(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glLoadIdentity(); // Clear the matrix
glOrtho(1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix

glPushMatrix()

glBegin(GL_QUADS)
glVertex3f(-0.064, 0.064, 0.0)
glVertex3f( 0.064, 0.064, 0.0)
glVertex3f( 0.064, -0.064, 0.0)
glVertex3f(-0.064, -0.064, 0.0)
glEnd()
glPopMatrix()

homeruniverse
07-04-2004, 04:49 AM
OK, I try to explain the complete situation:

In the first lines of my DrawScene-function I create a lot of objects:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()

glTranslatef(-0.384, -0.310, -0.925)
glBindTexture(GL_TEXTURE_2D, mMap.textures[0])

mObjects.map2D() #a function only filled with translations and objects
mObjects.menu() #a function only filled with translations and objects, too

The created objects are all in the bottom of the screen and somewhere there is my translated point. Of course I could add all my translations together to "go back" to the point at the beginning of the function but this has to be very hard because there are many translations which syntax I wrote with help of a complex loop. So the simplest way to create an indepentend object is to "go back" to the translation point at the beginning of my draw-function.

nexusone
07-04-2004, 06:18 AM
Yes, the best way to have a indepdent object is to start from a begining.

example draw a group of objects

Lets say we have set 0,0,0 as the center of the screen in the projection matrix.

glLoadIdentity(); // clear matrix

for(x=0; x <10 ; x++)
{
glPushMatrix(); Save matrix
glTranslate( object[x].x, object[x].y, object[x].z); // Location on screen
draw_object( x );
glPopMatrix(); Restore Matrix
}

Now when use glTranslate it only effects the current object, the Push/pop matrix restores the matrix back each time.

Also with Push/Pop matrix you can stack them.

Lets say you have a moon orbiting around a planet, the moon is drawn relative to the planet.

//Draw planet with moon

glPushMatrix();
glTranslate(....); // location of planet
drawPlanet();
glPushMatrix() // Works with multiple moons too
glTranslate(...); // location of moon relative to planet.
drawMoon();
glPopMatrix();
glPushMatrix();Also would suggest you look at some of my examples on my website, should give you some ideas.

homeruniverse
07-05-2004, 02:50 AM
thanks ...
(I added your Page to my "program" favourite-folder ...)