简体   繁体   English

如何使用OpenGL / SDL让我的鼠标像FPS一样控制相机?

[英]How can I make my mouse control the camera like a FPS using OpenGL/SDL?

I've created this basic 3D Demo using OpenGL/SDL. 我使用OpenGL / SDL创建了这个基本的3D演示。 I handled the keyboard callback so I can "strafe" left and right using 'a' and 's' and move forward and backward using 's' and 'w'. 我处理了键盘回调,所以我可以使用'a'和's'左右“左右”并使用's'和'w'向前和向后移动。

However, I would like to now make it so I can control the direction my camera is "looking" based off my mouse movements. 但是,我现在想做到这一点,我可以控制我的相机根据我的鼠标移动“看”的方向。 Just like in a FPS shooter when you move the mouse around it makes the camera look around in various directions. 就像在FPS射击游戏中一样,当你将鼠标移动到它周围时,它会使摄像机向各个方向看。

Does anyone have any idea how I could utilize the mouse callbacks to "points" the camera class correctly when I move the mouse? 当我移动鼠标时,有没有人知道如何利用鼠标回调来正确“指向”相机类?

#include "SDL.h"
#include "Camera.h"

Camera cam;
Scene scn;

//<<<<<<<<<<<<<<<<<myKeyboard>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void myKeyboard(unsigned char key, int x, int y)
{
  switch(key)
    {
    case 's': cam.slide(0.0, 0.0, 0.2); break;
    case 'w': cam.slide(0.0, 0.0, -0.2); break;
    case 'a': cam.yaw(-1.0); break;
    case 'd': cam.yaw(1.0); break;


    case 27: exit(0);
    }
  glClear(GL_COLOR_BUFFER_BIT);
  glutPostRedisplay();
}

void displaySDL( void )
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    scn.drawSceneOpenGL();
    glFlush();
    glutSwapBuffers();
}

int main( int argc, char* argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("SDL Sence With Camera");
    glutKeyboardFunc(myKeyboard);
    glutDisplayFunc(displaySDL);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_NORMALIZE);
    glViewport(0, 0, 640, 480);
    scn.read("fig5_63.dat");
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    scn.makeLightsOpenGL();

    cam.set(2.3, 1.3, 2.0, 0, 0.25, 0, 0, 1, 0);
    cam.setShape(30.0f, 64.0f/48.0f, 0.5f, 50.0f);

    glutMainLoop();
    return 0;
}

This is a tar with my SDL file, and the file I pasted above and my Camera class. 这是我的SDL文件的tar,以及我上面粘贴的文件和我的Camera类。 http://www.filedropper.com/fpsdemotar http://www.filedropper.com/fpsdemotar

If someone can give me some tips for what algorithm I should use when processing mouse callbacks in terms of pointing the camera I would appreciate it. 如果有人能给我一些关于在处理鼠标回调时应该使用哪种算法的技巧,我会很感激。

Thanks! 谢谢!

Mouse moving up/down -> pitch, Mouse moving right/left -> yaw. 鼠标向上/向下移动 - >音高,鼠标向右/向左移动 - >偏航。

I do not believe having your 'a' and 'd' keys be yaw is accurate. 我不相信让你的'a'和'd'键偏航是准确的。

Actually, your whole setup is a bit odd to me, since from a geometric standpoint, I view the coordinate as (x, y, z). 实际上,你的整个设置对我来说有点奇怪,因为从几何角度来看,我将坐标视为(x,y,z)。 You set s and w to go "up" and "down" (z), instead of "forward" and "back" (y). 你将s和w设置为“向上”和“向下”(z),而不是“向前”和“向后”(y)。 I see it as a xy graph that has been set flat on a table and you are looking at it from above. 我把它看作是一张xy图表,它已经平放在桌子上,你从上面看它。 Moving close to it decreases z, which is coming out of the plane. 靠近它移动会减小从飞机出来的z。

Here is how I would have it setup: 这是我如何设置它:

w -> slide(0, 0.2, 0); // y
s -> slide(0, -0.2, 0);
a -> slide(-0.2, 0, 0); // x
d -> slide(0.2, 0, 0);

//The following goes in your mouse event handler or something:
pitch(newMouseLocation.y - oldMouseLocation.y); // mouse y is related to pitch
yaw(newMouseLocaiton.x - oldMouseLocation.x); // mouse x is related to yaw

I realize that you do not need to follow this coordinate convention, but it just seems more intuitive for me. 我意识到你不需要遵循这个坐标惯例,但它对我来说似乎更直观。

I hope this helps. 我希望这有帮助。

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

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