0

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.

MotionFunc:

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

}

 }

Display Function:

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 am sure there is more code that I need to show, this is just where I think the problem is. Any help would be greatly appreciated.

Here is a picture of a console output and the screen (OpenGL context) as I see it. Here is a picture of a console output and the screen (OpenGL context) as I see it.

UPDATE: Updated my code. It looks like my coordinates are moving, but the picture is not if that makes sense. If you look at my output, if I keep 'dragging' the picture, you can see in the console that the variable xMove and yMove can get as large or small as they want, again translate is just never moving it.

You can see my translate coordinates can get as big or small as they want, which would make the object unmoveable, but it doesn't move.

TheBlindSpring
  • 631
  • 5
  • 22

4 Answers4

0

You pop your matrix before drawing things, which resets the matrix to the state of last push matrix. Move glPopMatrix(); below draw call

0

You're popping the matrix before you call glDrawArrays(), so this naturally negates the effect of the translation. It also negates the glOrtho() call, but that should be issued on the projection matrix and not on the modelview matrix in the first place.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

And, of course, the problem is in your code, and not in OpenGL.

In this code excerpt :

glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
glPopMatrix();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawArrays(GL_LINE_STRIP, 0, 29000);

you are :

  1. setting the identity as the view matrix
  2. push it into the queue
  3. modify it by glTranslate
  4. pop it of the stack
  5. render the image

Therefore, your translation is ignored.

This is correct operation :

glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);

//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawArrays(GL_LINE_STRIP, 0, 29000);

glPopMatrix();
BЈовић
  • 62,405
  • 41
  • 173
  • 273
0

You are multiplying projection before translation, remember to always read matrix transformation from bottome to top in OpenGL 1.1 which you should upgrade IMO. Another issue is that you are poping the matrix before drawing.

Correct code:

//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
    glMatrixMode(GL_PROJECTION);

  glLoadIdentity();
    glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
    //  Reset the modelview matrix.


    glMatrixMode(GL_MODELVIEW);


    glLoadIdentity();
    glPushMatrix();
    glTranslatef(xMove, -yMove, 0);


    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDrawArrays(GL_LINE_STRIP, 0, 29000);

    glPopMatrix();
concept3d
  • 2,248
  • 12
  • 21