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

发布于 2024-07-17 10:42:21 字数 1914 浏览 7 评论 0原文

我使用 OpenGL/SDL 创建了这个基本的 3D 演示。 我处理了键盘回调,因此我可以使用“a”和“s”向左和向右“扫射”,并使用“s”和“w”向前和向后移动。

但是,我现在想做到这一点,以便我可以根据鼠标移动来控制相机“观看”的方向。 就像在 FPS 射击游戏中一样,当您移动鼠标时,摄像机会向各个方向看去。

有谁知道当我移动鼠标时如何利用鼠标回调正确“指向”相机类?

#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;
}

这是一个包含我的 SDL 文件、我上面粘贴的文件和我的 Camera 类的 tar 文件。 http://www.filedropper.com/fpsdemotar

如果有人可以给我一些关于我使用什么算法的提示在处理鼠标回调时应该使用指向相机的方式,我将不胜感激。

谢谢!

I've created this basic 3D Demo using OpenGL/SDL. 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'.

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.

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.
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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

围归者 2024-07-24 10:42:22

鼠标向上/向下移动 -> 俯仰,鼠标向右/向左移动 -> 偏航。

我不相信让你的“a”和“d”键偏航是准确的。

实际上,你的整个设置对我来说有点奇怪,因为从几何的角度来看,我将坐标视为(x,y,z)。 您将 s 和 w 设置为“向上”和“向下”(z),而不是“向前”和“向后”(y)。 我将其视为平放在桌子上的 xy 图,您从上方查看它。 靠近它会减少 z,它会离开平面。

这是我的设置方式:

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

我意识到您不需要遵循这个坐标约定,但它对我来说似乎更直观。

我希望这有帮助。

Mouse moving up/down -> pitch, Mouse moving right/left -> yaw.

I do not believe having your 'a' and 'd' keys be yaw is accurate.

Actually, your whole setup is a bit odd to me, since from a geometric standpoint, I view the coordinate as (x, y, z). You set s and w to go "up" and "down" (z), instead of "forward" and "back" (y). I see it as a xy graph that has been set flat on a table and you are looking at it from above. Moving close to it decreases z, which is coming out of the plane.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文