GLUT:错误的鼠标坐标?

使用SCREEN_WIDTH = 1200和SCREEN_HEIGHT = 800。

首先,我在(x = 0,y = 0,w = 40,h = 40)画一个框到屏幕上。

然后,我使用handleMouse函数返回鼠标点击的x和y坐标。

问题是,当程序以非最大化窗口模式启动时,当我点击(看上去是)框的右下angular时,返回的坐标是x = 40,y = 32。当我认为它应该返回x = 40,y = 40。

我不知道这个问题是不是正确的,或者是函数返回了错误的x / y。

我相信我理解openGL的渲染,转换和glOrth是如何工作的,但我可能完全错误。 我在网上看到一些build议,说Windows Decor(使用Windows 7)可能会导致这个问题,但做了很less的解释,并没有提供任何解决scheme。

这是我的整个源代码。 我已经从我的游戏中删除了一切,直到基本,问题仍然存在:(。我添加了两张图片,以便人们可以看到我的问题。在非最大化窗口(顶部图片),当点击右下angular,返回的坐标是41,32; y坐标小于它应该在MAXIMIZED WINDOW(底部图片),当点击同一个angular落时,它返回正确的坐标40,40。这两个结果都发生在两个我的原始源代码和genpfault的build议代码。

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

非最大化窗口 !

最大化的窗口 !

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; 

这对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()

尝试将四边形从显示器的边缘移开,因为当我试图做这样的事情时,屏幕边缘很奇怪

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