Results 1 to 4 of 4

Thread: glRotatef() and glTranslatef()

  1. #1

    glRotatef() and glTranslatef()

    hi everyone,

    like most beginners i have my problems with rotate & translate. i tried to create a rotating cube, which position (x,y,z) one can change by key-commands. each of the functions is working quite fine (moving without rotating and the other way around), but together the cube behaves chaotically.
    i put most of the code in onTimer:



    gluPerspective(45.0f, 1.0, 0.1f, 100.0f);



    if (!moved){
    glTranslatef(x_Achse, y_Achse, z_Achse);
    moved = TRUE;
    glRotatef(1.0f, 0.0f, 1.0f, 1.f);


    for(int i=0; i< 6; i++){
    //Code for the cube, only 'glVertex3f'


    i know i'm doing something wrong with the matrix but i'm not yet knowing how it is correct.
    may anyone help me?

    bye stephan

    (sorry for my english, i'm from germany and not used to post messages in english)

  2. #2

    Re: glRotatef() and glTranslatef()

    There are a couple of things to keep in mind when using glRotate and glTranslate. When you rotate, the translate is relative to the new direction caused by the rotation. Hence, rotating on y-axis 90 degrees, and then translating in the x-axis will actually "appear" to translate in the z-axis.


    wastes cycels because it does nothing useful.

  3. #3

    Re: glRotatef() and glTranslatef()

    When I say "appear", I really mean some complicated stuff for a beginner, but I didn't think you'd want delve into it

  4. #4
    Junior Member Newbie
    Join Date
    Aug 2000
    Johannesburg, Gauteng, South-Africa

    Re: glRotatef() and glTranslatef()

    You seem to not reset your MODELVIEW matrix
    when you make the call to glPushMatrix you are storing the current modelview matrix on the stack. Your call to glLoadIdentity() resets the new matrix and the you glPopMatrix the saved matrix from the stack. This results in a sort of accumulation of the current ModelView matrix, like yoale said it does nothing.
    Consider this: If you start out and translate along the -Z axis by 5.0 units, and do not reset the MODELVIEW matrix, the next time you translate by 5.0 on the -Z you will actually have translated by 10.0 on -Z.

    Thus use:
    //set up projection matrix


    //draw cube

    this might solve your problem. if not scream well try and help some more.

    Oh, BTW, you do not need to set up the projection matrix each time you render a scene, unless you need to change it the whole time. Try setting it up once the app starts, and then redo it only when the window changes.

Similar Threads

  1. Cant glTranslatef
    By ethereal in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-23-2012, 08:04 AM
  2. glTranslatef
    By nick2price in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-11-2009, 11:56 PM
  3. glRotatef,glTranslatef
    By kazmalak in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-15-2005, 05:48 PM
  4. Replace two glRotatef() with one glRotatef()?
    By tgtt in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 02-24-2004, 02:33 PM
  5. glTranslatef
    By G in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 07-01-2003, 09:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean