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

时间:2022-11-28 20:47:58

I am making Game in openGl which contain start menu screen.Problem is when i click start game button (which is a texture image) game start successfully but when i resize window and then click start game button the texture image coordinates change and game did not start how i prevent to change image coordinates after resize window.

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

Here is My Mouse funtion

这是我的鼠标功能

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

Here is my texture Image code

这是我的纹理图像代码

glBegin(GL_QUAD_STRIP);


        glTexCoord2f(0, 0);
        glVertex2f(300, 350);

        glTexCoord2f(1, 0);
        glVertex2f(400, 350);

        glTexCoord2f(0, 1);
        glVertex2f(300, 400);


        glTexCoord2f(1, 1);
        glVertex2f(400,400);

        glEnd();
        glFlush();

Here is my Matrix Projection Code:

这是我的矩阵投影代码:

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

2 个解决方案

#1


1  

Actually, it is the mouse click coordinates that are changing. You will have to convert your mouse click position (x,y) from viewport coordinates to your drawing coordinates (i.e. Ortho2D coordinates).

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

Assuming that width and height variables are provided by the system whenever the window is resized, try the following:

假设每当调整窗口大小时系统都会提供宽度和高度变量,请尝试以下操作:

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

And then test for the button bounds.

然后测试按钮界限。

#2


0  

The correct way to implement this is to use selection buffer.

实现此目的的正确方法是使用选择缓冲区。

With this, you know which polygon is being clicked. This is not affected by the size of the window.

通过此,您可以知道正在单击哪个多边形。这不受窗口大小的影响。

#1


1  

Actually, it is the mouse click coordinates that are changing. You will have to convert your mouse click position (x,y) from viewport coordinates to your drawing coordinates (i.e. Ortho2D coordinates).

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

Assuming that width and height variables are provided by the system whenever the window is resized, try the following:

假设每当调整窗口大小时系统都会提供宽度和高度变量,请尝试以下操作:

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

And then test for the button bounds.

然后测试按钮界限。

#2


0  

The correct way to implement this is to use selection buffer.

实现此目的的正确方法是使用选择缓冲区。

With this, you know which polygon is being clicked. This is not affected by the size of the window.

通过此,您可以知道正在单击哪个多边形。这不受窗口大小的影响。