Question

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?

Was it helpful?

Solution

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
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top