c++ - GLUT:错误的鼠标坐标?

标签 c++ windows opengl mouse glut

使用 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(Using windows 7)会导致这个问题,但解释的很少,也没有提供解决方案。

这是我的全部源代码。我已经剥离了从游戏到基础的所有内容,但问题仍然存在 :( 。我添加了两张图片以便人们可以看到我的问题。在非最大化窗口(顶部图片)中,单击右下角时, 返回的坐标是 41,32; y 坐标比它应该的小。在最大化窗口(底部图片)中,当点击同一个角时,它返回正确的坐标 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;

最佳答案

这对我来说在 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()

关于c++ - GLUT:错误的鼠标坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403853/

相关文章:

c++ - GL_POINTS 只绘制一个点而不是多个点

c++ - Eclipse CDT启用c++11问题

c++ - 在 C++ 中使用 auto 声明变量有缺点吗?

c++ - 存储 wchar_t 并在显示时有时会更改一些字符

windows - 只需知道MAC地址即可获取IP

opengl - 如何确定顶点着色器输出的限制?

c++ - 将元素替换为 vector 的特定位置

c++ - 从导致AccessViolationException的线程访问文件缓冲区

ruby - Chef : Insufficient permissions creating a directory in C:

c++ - glutWarpPointer 崩溃