OpenGL学习之摄像机移动(一)

代码:

//
//  main.cpp
//  GL_Camera.
//

#include 

#include "GLHeaders.h"
#include "ShaderAdmin.hpp"
#include "TextureAdmin.hpp"


#define WIDTH 800
#define HEIGHT 600
#define TITLE "OpenGL"

using namespace std;

bool keys[1024];

GLfloat camX = 0.0f;
GLfloat camZ = 3.0f;

GLfloat lastFrameTime = 0.0f;
GLfloat deltaTime = 0.0f;

glm::vec3 cameraPos   = glm::vec3(camX, 0.0f,  camZ);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp    = glm::vec3(0.0f, 1.0f,  0.0f);

GLuint VAO,VBO,EBO;
void initGL();
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void renderRectangle();
void do_Movement();

int main(int argc, const char * argv[])
{
    initGL();

    GLFWwindow *window = glfwCreateWindow(WIDTH,HEIGHT,TITLE, nullptr, nullptr);
    if (!window)
    {
        cout << "创建视窗失败" << endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetKeyCallback(window, key_callback);

    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK)
    {
        printf("Failed to initialize GLEW!\n");
        return -1;
    }

    int width , height;
    glfwGetFramebufferSize(window, &width, &height);
    glViewport(0, 0, width, height);
    //开启深度测试
    glEnable(GL_DEPTH_TEST);

    ShaderAdmin shader("Shaders/vShader.vsh","Shaders/fShader.fsh");
    renderRectangle();


    TextureAdmin texture("Image/container.jpg");
    TextureAdmin texture2("Image/awesomeface.png");



    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();
        //检测按键事件
        do_Movement();
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        //激活纹理单元
        glActiveTexture(GL_TEXTURE0);
        texture.BindTexture();
        glUniform1i(glGetUniformLocation(shader.m_program,"ourTexture"),0);

        glActiveTexture(GL_TEXTURE1);
        texture2.BindTexture();
        glUniform1i(glGetUniformLocation(shader.m_program,"ourTexture2"),1);

        shader.Use();
        //创建模型
        glm::mat4 model;
        GLuint modelLoc = glGetUniformLocation(shader.m_program,"model");
        model = glm::translate(model, glm::vec3(0.0f,0.0f,0.0f));
        glUniformMatrix4fv(modelLoc,1,GL_FALSE,glm::value_ptr(model));

        //创建观察矩阵
        glm::mat4 view;
        GLuint viewLoc;
        view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
        viewLoc = glGetUniformLocation(shader.m_program,"view");
        glUniformMatrix4fv(viewLoc,1,GL_FALSE,glm::value_ptr(view));

        //创建投影矩阵
        glm::mat4 projection;
        GLuint projectionLoc;
        projection = glm::perspective(45.0f, (GLfloat)(WIDTH/HEIGHT), 0.1f, 100.0f);
        projectionLoc = glGetUniformLocation(shader.m_program,"projection");
        glUniformMatrix4fv(projectionLoc,1,GL_FALSE,glm::value_ptr(projection));

        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 36);

        glBindVertexArray(0);
        glfwSwapBuffers(window);
    }


    glfwTerminate();
    return 0;
}
void initGL()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
}
void do_Movement()
{
    //保证体验相同,计算移动速度
    GLfloat curFrameTime = glfwGetTime();
    deltaTime = curFrameTime - lastFrameTime;
    lastFrameTime = curFrameTime;
    GLfloat cameraSpeed = 5.0f*deltaTime;
    //根据按下的按键来更新摄像机的值
    if (keys[GLFW_KEY_W])
        cameraPos += cameraSpeed * cameraFront;
    if (keys[GLFW_KEY_S])
        cameraPos -= cameraSpeed * cameraFront;
    if (keys[GLFW_KEY_A])
        cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
    if (keys[GLFW_KEY_D])
        cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
    if (key >= 0 && key < 1024)
    {
    //设置按下/释放键为true或false
        if (action == GLFW_PRESS)
            keys[key] = true;
        else if (action == GLFW_RELEASE)
            keys[key] = false;
    }

}
void renderRectangle()
{
    GLfloat vertices[] = {
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
        0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };
    glGenVertexArrays(1,&VAO);
    glGenBuffers(1,&VBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),(GLvoid *)0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),(GLvoid *)(3*sizeof(GLfloat)));
    glEnableVertexAttribArray(2);

    glBindBuffer(GL_ARRAY_BUFFER,0);
    glBindVertexArray(0);

}

demo

你可能感兴趣的:(opengl)