【问题标题】:glutWarpPointer not working while application is active应用程序处于活动状态时 glutWarpPointer 不起作用
【发布时间】:2014-01-06 19:38:25
【问题描述】:

我的问题是当我的鼠标在屏幕上时屏幕将不再旋转。仅当应用程序不再处于活动状态时。

(显示正在发生的事情的视频:http://youtu.be/h5Sg3nQoWKs

如果我取出这些扭曲指针,它就会重新开始工作。

这是我的鼠标更新:

mouse.x = x;
mouse.y = y;

if (x > screen.x/2)
{
    player.angle -= 0.05f;
    player.lx = sin(player.angle);
    player.lz = -cos(player.angle);
    glutWarpPointer(screen.x/2,screen.y/2);
}
else if (x < screen.x/2)
{
    player.angle += 0.05f;
    //if (player.vertAngle < 0)
        //player.vertAngle = 0;
    player.lx = sin(player.angle);
    player.lz = -cos(player.angle);
    glutWarpPointer(screen.x/2,screen.y/2);
}
if (y < screen.y/2)
{
    player.vertAngle += 0.05f;
    player.ly = sin(player.vertAngle);
    glutWarpPointer(screen.x/2,screen.y/2);
}
else if (y > screen.y/2)
{
    player.vertAngle -= 0.05f;
    //if (player.vertAngle > 360)
        //player.vertAngle = 360;
    player.ly = sin(player.vertAngle);
    glutWarpPointer(screen.x/2,screen.y/2);
}

【问题讨论】:

  • 这可能无济于事,但更好的球员轮换方法是player.angle = x - screen.x/2。这更好,因为按照您现在的方式,无论鼠标移动 1 个像素还是 100 个像素,您的播放器总是转动相同的量。这解决了这个问题。

标签: c++ opengl camera glut


【解决方案1】:

使用标志忽略glutWarpPointer() 生成的额外运动事件:

#include <GL/glut.h>

#include <iostream>
using namespace std;

bool capture = false;
void keyboard( unsigned char key, int x, int y )
{
    if( key == 'z' )
    {
        capture = !capture;
    }
}

void passiveMotion( int x, int y )
{
    static bool warped = false;
    if( warped )
    {
        warped = false;
        return;
    }

    if( capture )
    {
        warped = true;
        int w = glutGet( GLUT_WINDOW_WIDTH );
        int h = glutGet( GLUT_WINDOW_HEIGHT );
        glutWarpPointer( w / 2, h / 2 );

        int dx = ( w / 2 ) - x;
        int dy = ( h / 2 ) - y;
        cout << dx << " " << dy << endl;
    }
    else
    {
        cout << x << " " << y << endl;
    }
}

void display()
{
    glClear( GL_COLOR_BUFFER_BIT );
    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glutKeyboardFunc( keyboard );
    glutPassiveMotionFunc( passiveMotion );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}

【讨论】:

    猜你喜欢
    • 2011-06-11
    • 2021-06-18
    • 1970-01-01
    • 2020-09-18
    • 2019-07-04
    • 2016-02-04
    • 2018-05-29
    • 1970-01-01
    相关资源
    最近更新 更多