Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在调整窗口大小时使用鼠标点击获取图像坐标?_C++_Opengl_Graphics - Fatal编程技术网

C++ 如何在调整窗口大小时使用鼠标点击获取图像坐标?

C++ 如何在调整窗口大小时使用鼠标点击获取图像坐标?,c++,opengl,graphics,C++,Opengl,Graphics,我正在openGl中制作包含“开始”菜单屏幕的游戏。问题是当我单击“开始”游戏按钮(这是一个纹理图像)游戏成功启动时,但当我调整窗口大小,然后单击“开始”游戏按钮时,纹理图像坐标发生变化,并且游戏没有启动。如何防止在调整窗口大小后更改图像坐标 这是我的鼠标功能 void mouseClick (int button, int state, int x, int y) { if (!menu) { xMin = 300, xMax = 400, yMin

我正在openGl中制作包含“开始”菜单屏幕的游戏。问题是当我单击“开始”游戏按钮(这是一个纹理图像)游戏成功启动时,但当我调整窗口大小,然后单击“开始”游戏按钮时,纹理图像坐标发生变化,并且游戏没有启动。如何防止在调整窗口大小后更改图像坐标

这是我的鼠标功能

void mouseClick (int button, int state, int x, int  y)    
{
    if (!menu)
    { 
        xMin = 300, xMax = 400, yMin = 350, yMax = 400;

        if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
        {
            y = height - y;
            if (x >= xMin && x <= xMax && y >= yMin && y <= yMax)
            {
                printf_s("starting 2d Game ");
            }
        }
    }
}
这是我的矩阵投影代码:

glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluOrtho2D(0.0, 640.0, 0.0, 440);//dino window
    gluOrtho2D(0.0, 640.0, 0.0, 440);//dino window
    //gluOrtho2D(1.0, 1.0, 1.0,1.0);//house window
    //gluOrtho2D(1.0, 1.0, 1.0, 1.0);//bird window
    glViewport(0, 0, width, height);
    //glViewport(220, 100, 100, 200);

实际上,是鼠标点击坐标在改变。必须将鼠标单击位置(x,y)从视口坐标转换为图形坐标(即正交二维坐标)

假设在调整窗口大小时系统提供了
宽度
高度
变量,请尝试以下操作:

x = x/width * 640;
y = (height -y) / height * 440;

然后测试按钮边界。

实现此功能的正确方法是使用


这样,您就知道正在单击哪个多边形。这不受窗口大小的影响。

感谢您的独特建议,我一定会实现选择缓冲区(Y)
x = x/width * 640;
y = (height -y) / height * 440;