简体   繁体   English

OpenGL相机旋转怪异

[英]OpenGL Camera rotation weirdness

I've been working on a small project with FreeGlut and Glew. 我一直在与FreeGlut和Glew一起进行一个小项目。 Now I'm coding a camera system, but there are some things that are simply weird: 现在,我正在为摄影机系统编写代码,但是有些事情简直很奇怪:

  • In fullscreen mode if the mouse moves in lower area of the screen, camera movements are faster than if camera moves in upper areas. 在全屏模式下,如果鼠标在屏幕下部移动,则照相机移动的速度要比在上部区域移动的速度快。

  • The camera makes weird movement, always in same direction, a small 8 figure move move. 相机总是以相同的方向进行奇怪的移动,仅移动了8个数字。

code: 码:

void MouseOps(int x, int y)
{
    // Changes in mousepositions.  Always same direction and 
    // in lower right corner of monitor faster, for some reason.
    deltaX = x - MousePreviousX;
    deltaY = y - MousePreviousY;

    // Also I didn't bother to put * 360 in next equations, 
    // because it would make the camera  jump for crazy. 
    // resx and resy are screen resolutions. 
    // Endresult should be that camera can 
    // rotate once when mouse moves over screen
    yaw = yaw + (((deltaX / resx)) * deginrad);
    pitch = pitch + (((deltaY / resy)) * deginrad);

    //Check clippings (eg. camera wont end upside down etc.)
    if(yaw >= (2 * pi) || yaw <= (-2 * pi)  )
        yaw = 0;
    if(pitch >= (pi / 2))
        pitch = pi / 2;
    if(pitch <= (pi / -2))
        pitch = pi / -2;

    //Calculate x, y, and z coordinates of unit sphere to look at (r = 1)
    cam_normX = cos(yaw) * sin(pitch); 
    cam_normY = sin(yaw) * sin(pitch);
    cam_normZ = cos(yaw);

    // Current x and y to previous
    int MousePreviousX = x;
    int MousePreviousY = y; 
}

I tried to use this http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates system to calculate the point to look at. 我尝试使用此http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates系统计算要看的点。 Then I passed all "cam_norm" variables to 然后,我将所有“ cam_norm”变量传递给

gluLookAt(cam_posX, cam_posY, cam_posZ,
        cam_posX+cam_normX, cam_posY+cam_normY, cam_posZ + cam_normZ,
        cam_upX, cam_upY, cam_upZ); 

I don't know why this works but it fixed all problems: 我不知道为什么会这样,但是解决了所有问题:

bool isCursorWarping = false;

void MouseOps(int x, int y)
{

    if(isCursorWarping == false){
        // Changes in mousepositions. Always same direction and in lower right corner of monitor faster, for some reason.
        deltaX = x - MousePreviousX;
        deltaY = y - MousePreviousY;

        yaw = yaw + ((((deltaX / resx)) * deginrad) * 360);
        pitch = pitch + ((((deltaY / resy)) * deginrad) * 360);


        //Check clippings (eg. camera wont end upside down etc.)

        if(x >= resx - 1 || y >= resy - 1 || x == 0 || y == 0)
        {
            warpCursor();
            MousePreviousX = resx / 2;
            MousePreviousY = resy / 2; 
        }else{
            MousePreviousX = x;
            MousePreviousY = y; 
        }

        if(yaw >= (2 * pi) || yaw <= (-2 * pi)  )
            yaw = 0;
        if(pitch >= (pi / 2))
            pitch = pi / 2;
        if(pitch <= (pi / -2))
            pitch = pi / -2;


        //Calculate x, y, and z coordinates of unit sphere to look at (r = 1)

        cam_normX = cos(pitch) * cos(yaw); 
        cam_normY = sin(pitch) * sin(yaw);
        cam_normZ = cos(pitch) * sin(yaw);



    }
        // Current x and y to previous and cleanup



        isCursorWarping = false;


}

void warpCursor()
{
    isCursorWarping = true;
    glutWarpPointer(resx / 2, resy / 2);


}

Then I pass the "cam_norm" values to: 然后,我将“ cam_norm”值传递给:

gluLookAt(0.0f, 1.0f, 2.0f, 0.0f + cam_normX, 1.0f + cam_normY, 2.0f+ cam_normZ, 0.0f, 0.1f, 0.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM