Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: something like glTranslate (with absolute values) !?

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Munster (Germany)
    Posts
    7

    something like glTranslate (with absolute values) !?

    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 ..

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: something like glTranslate (with absolute values) !?

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Munster (Germany)
    Posts
    7

    Re: something like glTranslate (with absolute values) !?

    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 ?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: something like glTranslate (with absolute values) !?

    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.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: something like glTranslate (with absolute values) !?

    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()

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Munster (Germany)
    Posts
    7

    Re: something like glTranslate (with absolute values) !?

    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.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: something like glTranslate (with absolute values) !?

    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.
    Code :
    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.

  8. #8
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Munster (Germany)
    Posts
    7

    Re: something like glTranslate (with absolute values) !?

    thanks ...
    (I added your Page to my "program" favourite-folder ...)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •