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 4 of 4

Thread: glTranslatef improperly used: nothing displayed

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12

    glTranslatef improperly used: nothing displayed

    I am trying to write a simple application with xcode that displaces a quad.For this I am using glTranslatef.The evidenced part of the code is where I try to translate the points.

    Code :
    #import <OpenGL/OpenGL.h>
    #import <GLUT/GLUT.h>
     
     
    int width=500, height=500;
     
     
    void init()
    {
        glViewport(0, 0, width, height);
        glLoadIdentity();
        glOrtho(0, width, height, 0, 0, 1);
    }
     
     
    void display()
    {
        glClearColor(0.9, 0.9, 0.9, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0, 0.0, 0.0);
         glMatrixMode(GL_MODELVIEW);  /************************/
        glLoadIdentity();[/B]          /**************/
        glTranslatef(100,0,0);    /****** trying to translate the vertexes  */
        glBegin(GL_QUADS);
        glVertex2i(100, 100);
        glVertex2i(300, 100);
        glVertex2i(300, 300);
        glVertex2i(100, 300);
        glEnd();
        glFlush();
    }
     
     
    int main(int argc, char * argv[])
    {
        glutInit(&argc, argv);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(width, height);
        glutCreateWindow("Test");
        glutDisplayFunc(display);
        init();
        glutMainLoop();
        return 0;
    }

    The quad should be displaced of 100 units in the widnow.Since in the glOrtho I specify the size of the view, I think that the quad shall be displayed inside the window.But instead I don't see anything: there isn't a quad in the window.So maybe I'm not using properly glTranslatef, but I can't find the error.

  2. #2
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    354
    In the init you were modifing the model matrix, and then overwriting it in the render.
    Code :
    void init()
    {
       glViewport(0, 0, width, height);
       glMatrixMode(GL_PROJECTION); // Add this line
       glLoadIdentity(); 
       glOrtho(0, width, height, 0, 0, 1);
    }

    Tips: Don't use fixed pipeline anymore is deprecated. Don't use Glut is outdated.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12
    Ok but it seems like I'm missing something of how matrixes work, if I add a roratef before the glTranslatef:

    Code :
    glRotatef(0.01, 1.0, 0.0, 0.0);

    Again nothing is displayed, what am I missing (I added the glMatrixMode(GL_PROJECTION); line).

  4. #4
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    354
    However this is a lot trickier.

    Now you are doing
    This is your rotation matrix A
    1 0 0 0
    0 c -s 0
    0 s c 0
    0 0 0 1
    where c = cos(0.01) Degree
    where s = sin(0.01) Degree
    cause sin of 0.01 is really low this is almost an identity

    This is your translation matrix B
    1 0 0 100
    0 1 0 0
    0 0 1 0
    0 0 0 1

    Your transformation is vEye = A* (B * v)

    1 0 0 100
    0 c -s 0
    0 s c 0
    0 0 0 1

    the first point should be in 200 100*c 100*-s 1

    The Z in negative, but your glOrtho specify a near plane in 0. So all de vertexes are culled away.

    Code :
     
        glOrtho(0, width, height, 0, -1, 1);
    should do the trick.

    Mmm... btw, why are you doing random transformation? Is not better to read a book or a tutorial to learn?
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

Posting Permissions

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