Moving a drawing around in openGL with mouse
Asked Answered
D

1

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?

Defendant answered 9/11, 2013 at 22:58 Comment(3)
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.Canfield
What's your initial projection?Emunctory
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 blankDefendant
E
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
    ...
Emunctory answered 10/11, 2013 at 8:1 Comment(4)
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 :SDefendant
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).Defendant
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 glRotatefs to give the camera a rotation as well. gluLookAt is a handy function until you get the hang of doing the matrix transformations yourself.Emunctory
@Defendant 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.Emunctory

© 2022 - 2024 — McMap. All rights reserved.