如何使用SDL在opengl中使用鼠标输入移动相机

时间:2015-01-07 05:39:00

标签: c++ opengl

你好,我一直在努力学习Opengl,并决定最好先画一个物体,然后让相机在不同的方向上移动它作为练习程序,我画了对象,现在遇到了问题使用鼠标在相机中移动时我没有找到一个使用SDL和Opengl 3.3+的示例我似乎无法弄清楚如何使用相机输入移动鼠标这里是我的相机类

#include <glm\glm.hpp>
#include <GL\glew.h>
#include <glm\gtx\transform.hpp>
class Camera
{
public:
    Camera(GLuint ProgramID)
    {
        this->m_ProgramID = ProgramID;

        //set up Camera
        m_CurrPos= glm::vec3(0, 0, 4);
        m_Center = glm::vec3(0,0,0);
        m_Up = glm::vec3(0,1,0);
        ViewMatrix = glm::lookAt(m_CurrPos, m_Center, m_Up);

        projectionMatrix =
        glm::perspective(
            45.0f /*Field of View Takes in degrees*/
            , 4.0f / 3.0f, //aspect ratio
            0.1f, 
            100.0f);
        Model = glm::mat4(1.0f);

        MVP = projectionMatrix * ViewMatrix* Model;
        //transform camera
        GLuint MatrixID = glGetUniformLocation(m_ProgramID, "MVP");
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
        x_pos = 0.1f;
    }
    inline void transform(glm::vec3 vec)
    {

        m_Center.x += vec.x;
        m_CurrPos.z += vec.z;
        ViewMatrix = glm::lookAt(m_CurrPos, m_Center, m_Up);
        MVP = projectionMatrix * ViewMatrix* Model;
        //transform camera
        GLuint MatrixID = glGetUniformLocation(m_ProgramID, "MVP");
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
    }
protected:
private:
    glm::mat4 projectionMatrix;
    glm::mat4 Model;
    glm::mat4 MVP;
    glm::mat4 ViewMatrix;
    glm::vec3 m_CurrPos;
    glm::vec3 m_Center;
    glm::vec3 m_Up;
    GLuint m_ProgramID;
    float x_pos;

};

这是我的主要文件:

#include <iostream>
#include "Display.h"
#include "Terrain.h"
#include "Shader.h"
#include "Camera.h"
#include <Windows.h>
#include "Keys.h"
#ifdef main
    #undef main
#endif

int main()
{
    Display display(800, 600, "Warp Engine");
    Shader shader("TerrainShader");
    Camera camera(shader.getProgram());
    Terrain terrain(shader.getProgram());

    float color[] = { 0.1f, 0.15f, 1.0f, 1.0f };
    while (!display.IsClosed())
    {
        display.Clear(color);
        terrain.Draw();
        if (GetAsyncKeyState(VK_W))
        {
            camera.transform(glm::vec3(0.0f, 0.0f,-0.5f ));
        }
        if (GetAsyncKeyState(VK_S))
        {
            camera.transform(glm::vec3(0.0f, 0.0f, 0.5f));
        }
        if (GetAsyncKeyState(VK_A))
        {
            camera.transform(glm::vec3(-0.5f, 0.0f, 0.0f));
        }
        if (GetAsyncKeyState(VK_D))
        {
            camera.transform(glm::vec3(0.5f, 0.0f, 0.0f));
        }
        if (display.IsButtonDown())
        {
            if (display.GetAsyncButtonState(RIGHT_CLICK))
            {
                glm::vec2 pos = display.GetMousePos();

                camera.transform(glm::vec3());

            }
        }

        display.Update();
    }

    return 0;
}

我的问题是,当我在大多数游戏中点击并拖动鼠标时,如何使用鼠标位置转换视图?

0 个答案:

没有答案