Search code examples
copenglglut

becase glOrtho change, my mouse callback is not working. why?


if glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) then my code is working. But if I change to glOrtho(0.0, 800.0, 0.0, 600.0, -1.0, 1.0) then my code is not working. Maybe mouse callback is not working. Change glOrtho is effort to mouse callback? I don't know what the problem is.

Here is my code:

GLfloat myVertices[10][2];          
GLfloat Width = 800.0;      
GLfloat Height = 600.0;
GLint count = 0;

void Mouse(int button, int state, GLint x, GLint y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        myVertices[count][0] = x / Width;
        myVertices[count][1] = (Height - y) / Height;
        count++;         

        drawScene();      
    }
}

GLvoid drawScene()                                  
{
    GLint index;

    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 0); 
    glPointSize(3.0f); 
    glEnableClientState(GL_VERTEX_ARRAY);     
    if (count > 0)
    {
        for (index = 0; index < count; index++)
        {
            glRectf(myVertices[index][0], myVertices[index][1], myVertices[index][0] + 0.01, myVertices[index][1] + 0.05);
            //glRectf(myVertices[index][0], myVertices[index][1], myVertices[index][0]+50, myVertices[index][1]+50);
        }
    }

    glFlush();                              
}

Solution

  • Your mouse callback is working as before. Your coordinates just don't make much sense:

        myVertices[count][0] = x / Width;
        myVertices[count][1] = (Height - y) / Height;
    

    This will result in values in [0,1], which means they will all lie in the bottom left pixel of the screen when you apply a pixel-perfect ortho projection as you set it up.