因为 glOrtho 发生了变化,我的鼠标回调不起作用。为什么?

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

if glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) 那么我的代码工作正常。但是,如果我更改为 glOrtho(0.0, 800.0, 0.0, 600.0, -1.0, 1.0),那么我的代码将无法正常工作。也许鼠标回调不起作用。改glOrtho是鼠标回调的功夫?我不知道是什么问题。

这是我的代码:

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

您的鼠标回调和以前一样工作。你的坐标没有多大意义:

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

这将导致 [0,1] 中的值,这意味着当您在设置时应用像素完美的正射投影时,它们将全部位于屏幕的左下角像素。