3D / FPS相机问题 - SDL2

时间:2014-04-06 17:33:01

标签: c++ opengl camera game-engine sdl-2

我的代码出了问题,我正在尝试制作第一人称3D相机。 我使用SDL_GetKeyboardState(NULL)来获取按下的键。

当我按下其中一个已定义的按键时,没有任何反应,为什么?

相机(Controll):

void Control(float movevel, float mousevel, bool mi, SDL_Window* window)
{
if (mi)  //if the mouse is in the screen
{
    int MidX = 640 / 2;   //middle of the screen
    int MidY = 480 / 2;
    SDL_ShowCursor(SDL_DISABLE);    //we don't show the cursor
    int tmpx, tmpy;
    SDL_GetMouseState(&tmpx, &tmpy); //get the current position of the cursor
    camYaw += mousevel*(MidX - tmpx);   //get the rotation, for example, if the mouse current position is 315, than 5*0.2, this is for Y
    camPitch += mousevel*(MidY - tmpy); //this is for X
    lockCamera();
    //SDL_WarpMouse(MidX, MidY);       //move back the cursor to the center of the screen
    SDL_WarpMouseInWindow(window, MidX, MidY);

    const Uint8* kstate = SDL_GetKeyboardState(NULL);

    if (kstate[SDLK_w])
    {
        if (camPitch != 90 && camPitch != -90)
        {       //if we are facing directly up or down, we don't go forward, it will be commented out, when there will be gravity
            moveCamera(movevel, 0.0);        //move forward
        }

        moveCameraUp(movevel, 0.0);      //move up/down
    }

    if (kstate[SDLK_s])
    {
        //same, just we use 180 degrees, so we move at the different direction (move back)
        if (camPitch != 90 && camPitch != -90)
        {
            moveCamera(movevel, 180.0);
        }

        moveCameraUp(movevel, 180.0);
    }

    if (kstate[SDLK_a])
    {       //move left
        moveCamera(movevel, 90.0);
    }

    if (kstate[SDLK_d])
    {  //move right
        moveCamera(movevel, 270);
    }
}

glRotatef(-camPitch, 1.0, 0.0, 0.0);
glRotatef(-camYaw, 0.0, 1.0, 0.0);
}

主要(循环)

while (running)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    SDL_Event ev;
    while (SDL_PollEvent(&ev))
    {
        switch (ev.type)
        {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_KEYDOWN:
                int x, y;

                SDL_GetMouseState(&x, &y);
                Main::handleKeys(ev.key.keysym.scancode, x, y);
                break;
        }
    }

    Main::update(window);
    Main::render(window);

    SDL_GL_SwapWindow(window);
}

主要(更新):

void Main::update(SDL_Window* window)
{
Control(0.2, 0.2, mousein, window);
UpdateCamera(0.2); //move the camera to the new location
}

2 个答案:

答案 0 :(得分:2)

你应该在翻译相机之前调用glRotatef函数,否则它将围绕原点而不是它的位置旋转

答案 1 :(得分:1)

使用SDL_PumpEvents()更新状态数组。 (来自SDL_GetKeyboardState()wiki)。 我相信如果你这样做:

SDL_PumpEvents();
SDL_GetKeyboardState(NULL);

你应该得到你想要的结果。 (你需要SDL_PumpEvents每个循环以及SDL_GetKeyboardState)

(编辑) 另一种选择:

    case SDL_KEYDOWN:
        switch(event.key.keysym.sym){
            case SDLK_w
                (... code)
                break;
            case SDLK_s:
                (... code)
                break;
            case SDLK_a:
                (... code)
                break;
            case SDLK_d:
                (... code)
                break;

你也可以将event.key.keysym.sym发送给一个函数并使用它们的当然。