3

I am trying to move an image around in openGL while holding left mouse button. i am NOT trying to drag an object around, just move the whole picture. Its a 2d drawing of a fractal and i was told that i can use gluortho2d but i can't find any info or similar tries on how to do it. I am assuming something like

void mouse_callback_func(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    gluOrtho2D(x-250.0, x+250.0, y-250.0,y+250.);
glutPostRedisplay();
}  

for a 500x500 window,but it's not working. The moment i left click the window goes blank. Any ideas?

Deadalus
  • 33
  • 1
  • 4
  • May be there was an overflow and picture went out of screen? Though I dont know the answer, but this should help you debug the problem. Print the values of x and y on the prompt in mouse_callback_func(). Once you get this value, substitute them in gluOrtho2D(x-250.0, x+250.0, y-250.0,y+250.); and render the image normally (do not use mouse now). Check if the picture is rendered correctly on screen. I assume it will not, and then you can try reducing/increasing the values to check what range of x/y values will render to your viewport. – viktorzeid Nov 10 '13 at 03:07
  • What's your initial projection? – jozxyqk Nov 10 '13 at 07:52
  • i tried that ,but it didnt work. I even went as low as x-1,x+1,y-1,y+1 to see what would happen, still the screen went blank – Deadalus Nov 10 '13 at 10:33

1 Answers1

2

gluOrtho2D modifies the current matrix. It's designed to be used with glMatrixMode(GL_PROJECTION), for example:

glMatrixMode(GL_PROJECTION); //start editing the projection matrix
glLoadIdentity(); //remove current projection
gluOrtho2D(...); //create new one
glMatrixMode(GL_MODELVIEW); //back to editing the modelview matrix

It might be more simple to set up a camera concept...

float cameraX, cameraY;
int lastMouseX, lastMouseY;

void mouse_callback_func(int button, int state, int x, int y)
{
    int dx = x - lastMouseX;
    int dy = y - lastMouseY;
    const float speed = 0.1f;
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        cameraX += dx * speed; //or -=, depending on which direction feels more natural to you
        cameraY -= dy * speed; //-= as mouse origin is top left, so +y is moving down
        glutPostRedisplay();
    }
    lastMouseX = x;
    lastMouseX = y;
}

void display()
{
    glLoadIdentity(); //remove transforms from previous display() call
    glTranslatef(-cameraX, -cameraY, 0.0f); //move objects negative = move camera positive
    ...
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • I was told that that's exactly what gluortho2d can do. Instead of actually moving my picture around i can change the viewing window (i.e moving camera). And i was asked to do it with gluortho2d specifically :S – Deadalus Nov 10 '13 at 10:45
  • I toyed around with glMatrixMode(GL_PROJECTION) etc etc what i can't get at all , is the constant redrawing ,even though i include postredisplay.After i drag my mouse with left click held ,nothing changes ,i then have to right click and chose a number(on right click u can change the number of random dots that make up the picture). – Deadalus Nov 10 '13 at 11:04
  • You *can* use the projection matrix to "move the camera", but it won't work if lighting is introduced. OpenGL doesn't have camera concept. The only way to make one is to move everything in the scene. Consider the `glTranslatef` above - by moving everything in the scene left, the camera moves right. This is just how a camera is implemented. You could add `glRotatef`s to give the camera a rotation as well. [`gluLookAt`](http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml) is a handy function until you get the hang of doing the matrix transformations yourself. – jozxyqk Nov 10 '13 at 12:51
  • @Deadalus in regard to redrawing, what does the right click code do that causes a redraw? Try putting prints in all your functions - see when they get called. Maybe for a test try putting `glutPostRedisplay` in an idle function or even the display function itself to force continuous updating. – jozxyqk Nov 11 '13 at 10:30