【问题标题】:GLUT: Wrong Mouse Coordinates?GLUT:错误的鼠标坐标?
【发布时间】:2012-11-15 18:30:39
【问题描述】:

使用 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 坐标小于应有的值。并且在 MAXIMIZED WINDOW(下图)中,当单击同一个角时,返回正确的坐标 40, 40。这两种结果都会出现我的原始源代码和 genpfault 的建议代码。

//原来我不能发布图片:(,而是链接。

non-Maximized Windowed!

Maximized Windowed!

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

函数.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

bool initGL();

void render();

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

#endif

函数.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;

}

常量.h

const int SCREEN_WIDTH = 1200;
const int SCREEN_HEIGHT = 800;

【问题讨论】:

    标签: c++ windows opengl mouse glut


    【解决方案1】:

    这对我在 Aero 和 Classic 的 Windows 7 上运行良好:

    #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;
    }
    

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

    【讨论】:

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

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

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

    【讨论】:

    • 在非最大化窗口模式下移动它仍然返回错误的 y 坐标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    相关资源
    最近更新 更多