簡體   English   中英

GLUT:錯誤的鼠標坐標?

[英]GLUT: Wrong Mouse Coordinates?

使用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的建議代碼。

//結果我不能發布圖片:(,鏈接而不是。

非最大化的窗口

最大化窗口化

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;

嘗試將四邊形從顯示器的邊緣移開,因為當我嘗試做類似的事情時,屏幕的邊緣非常奇怪

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

這在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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM