GLUT:鼠标坐标错误?

5
使用 SCREEN_WIDTH = 1200 和 SCREEN_HEIGHT = 800。
首先,在屏幕上绘制一个框,位于 (x = 0, y = 0, w = 40, h = 40)。
然后,我使用 handleMouse 函数返回鼠标单击的 x 和 y 坐标。
问题是,当程序在非最大化窗口模式下启动时,当我单击框的右下角时,返回的坐标为 x = 40,y = 32。但我认为应该返回 x = 40,y = 40。
我不知道问题是它没有被正确绘制,还是函数返回了错误的 x/y 坐标。
我相信我理解 openGL 渲染、转换和 glOrth 的工作原理,但我可能完全错了。我看到网上有一些建议说 Windows Decor(使用 Windows 7)会导致这个问题,但很少解释并没有提供解决方案。
这是我的完整源代码。我已经将游戏的所有内容都剥离,只留下基础部分,但问题仍然存在:( 我添加了两张图片,以便人们可以看到我的问题。在非最大化窗口中(上面的图片),当单击右下角时,返回的坐标为41,32; y坐标比应该小。而在最大化窗口中(下面的图片),当单击同一角落时,它返回正确的坐标40,40。这些结果对于我的原始源代码和genpfault建议的代码都是如此。
//结果发现我不能发布图片 :(,只能提供链接。 非最大化窗口! 最大化窗口!
main.cpp
int main( int argc, char* args[] )
{
    //Initialize FreeGLUT
    glutInit( &argc, args );

    //Create OpenGL 2.1 context
    glutInitContextVersion( 2, 1 );

    //Create Double Buffered Window
    glutInitDisplayMode( GLUT_DOUBLE );
    glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
    glutCreateWindow( "OpenGL" );

    //Do post window/context creation initialization
    if( !initGL() )
    {
        printf( "Unable to initialize graphics library!\n" );
        return 1;
    }
    //initGame();


    glutMouseFunc(handleMouse);


    glutDisplayFunc( render );


    glutMainLoop();

    return 0;
}

Functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

bool initGL();

void render();

void handleMouse(int button, int state, int x, int y);

#endif

Functions.cpp

bool initGL()
{

    //Initialize clear color
    glClearColor( 0.f, 0.f, 0.f, 1.f );

    //Check for error
    GLenum error = glGetError();
    if( error != GL_NO_ERROR )
    {
        //cout <<"Error initializing OpenGL! " << gluErrorString( error ) << endl;
        return false;
    }

    return true;
}

void render()
{

    //Clear color buffer
    glClear( GL_COLOR_BUFFER_BIT );
    glColor3f(1.f,1.f,1.f);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBegin(GL_QUADS);
        glVertex2f(0,0);
        glVertex2f(0, 41);
        glVertex2f(41, 41);
        glVertex2f(41, 0);
    glEnd();

    glutSwapBuffers();
}

void handleMouse(int button, int state, int x, int y)
{

    std::cout << x << '\t' << y << std::endl;

}

constants.h

const int SCREEN_WIDTH = 1200;
const int SCREEN_HEIGHT = 800;
2个回答

0

尝试将四边形移离显示器边缘,因为当我尝试做类似的事情时,屏幕边缘非常奇怪。

glBegin(GL_QUADS);
 glVertex2f(20,20);
 glVertex2f(20, 61);
 glVertex2f(61, 61);
 glVertex2f(61, 20);
glEnd();

很遗憾,将其移动时,在非最大化窗口模式下仍然返回错误的y坐标。 - Spoot

0

在 Windows 7 上的 Aero 和 Classic 主题下,这对我来说运行良好:

#include <GL/glut.h>
#include <iostream>

void render()
{
    glClearColor( 0, 0, 0, 1 );
    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho(0, w, h, 0, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3ub( 255, 255, 255 );
    glBegin(GL_QUADS);
        glVertex2f(0,0);
        glVertex2f(41, 0);
        glVertex2f(41, 41);
        glVertex2f(0, 41);
    glEnd();

    glutSwapBuffers();
}

void handleMouse(int button, int state, int x, int y)
{
    std::cout << x << '\t' << y << std::endl;
}

int main( int argc, char* args[] )
{
    glutInit( &argc, args );

    glutInitDisplayMode( GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "OpenGL" );

    glutMouseFunc(handleMouse);
    glutDisplayFunc( render );
    glutMainLoop();
    return 0;
}

最值得注意的是,我在render()中添加了运行时glutGet()窗口大小查询。


即使完全复制您的函数,当屏幕为非最大化窗口时,返回的右下角x、y坐标为40,32。只是为了澄清,如果屏幕是最大化或全屏,返回的坐标是正确的40,40。有什么想法吗? - Spoot
当您运行我的版本时,正方形看起来像矩形,就像您的非最大化截图一样? - genpfault
是的,就是这样。发布的屏幕截图完全是您的代码。我不知道我做错了什么。自从我发布这个问题以来,我一直在不停地工作,但还没有找到任何原因。直到您指出它看起来像一个矩形,我才真正注意到。在我的游戏中(最初发生此问题的地方),最顶部的一行图块不会显示(每个图块的高度为8像素,宽度为12像素,因此顶部的8个像素不会显示),但是当我创建这个示例程序时,问题仍然存在。所以我仍然不知道问题出在哪里... - Spoot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接