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

Thread: How to use OpenGL glTranslate?

  1. #1
    Newbie Newbie
    Join Date
    Mar 2014
    Posts
    1

    How to use OpenGL glTranslate?

    Okay, so my program opens a file, reads in xyz-points, then draws a line strip out of it. I originally had this program written in SharpGL (implemnted as WPF window) and it worked, but not well due to using immediate mode, so I have moved onto OpenGL in C++. I have (somewhat) figured out VBO's and I now I am trying to add mouse functionality now. My problem is I can't move the picture with my mouse, I want to be able to click and 'drag' the picture. My mouseClickFunc and mouseMotion work (my cout statements execute), however it seems like my translate call is never being executed (i.e. the picture starts partially 'clipped' in the scene and I would like the ability to drag it and center it). I know this is a shot in the dark but I am really not sure what to do.

    Code :
    void mouseMotion(int x, int y)
     {
     
    if (moveable)
    {
        xMove += xTransform(x) - xTransform(xDown);
        yMove += yTransform(y) - yTransform(yDown);
        xDown = x;
        yDown = y;
        cout << yMove << "---" << xMove << endl;
        glutSwapBuffers();
        glutPostRedisplay();
     
    }
     
     }

    Code :
    void RenderFunction(void)
    {
    ++FrameCount;
     
     
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    glMatrixMode(GL_PROJECTION);
    glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
    //  Reset the modelview matrix.
    glLoadIdentity();
     
    glMatrixMode(GL_MODELVIEW);
     
     
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(xMove, -yMove, 0);
    //glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
     
     
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    glDrawArrays(GL_LINE_STRIP, 0, 29000);
    glPopMatrix();
     
    glutSwapBuffers();
    glutPostRedisplay();
     
     
    }


    I can see that my xMove and yMove can become as large or small as they want, yet the picture does not move from the corner.


    If there is any other code you would like to view please tell me.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,201
    This is broken:
    Code :
    glMatrixMode(GL_PROJECTION);
    glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
    //  Reset the modelview matrix.
    glLoadIdentity();
     
    glMatrixMode(GL_MODELVIEW);
    What you're doing here is not what your comment says; what this code actually does is:


    • Switch the matrix mode to projection so that all future matrix stack operations will affect the projection matrix.
    • Compute an ortho matrix and multiply the current projection matrix by it, giving a new current projection matrix.
    • Then replace that new current projection matrix with identity.
    • Then switch to modelview.

Posting Permissions

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