加入键盘鼠标控制的代码

来源:互联网 发布:沙特军事知乎 编辑:程序博客网 时间:2024/06/06 03:23

以下是简单代码实例,着色器就不放了,比较简单

#define GLEW_STATIC#define STB_IMAGE_IMPLEMENTATION#include#include#include#include"shader.h"#include#include#include#include#includeusing namespace std;bool firstmouse = true;bool keys[1024];//视角GLfloat pitch = 0.0f;GLfloat yaw = -90.0f;GLfloat lastx = 400.0f;GLfloat lasty = 300.0f;GLfloat fov=45.0f;GLfloat deltaTime = 0.0f;  // 当前帧和上一帧之间的时间差GLfloat lastFrame = 0.0f; // 上一帧时间//cameraglm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -2.0f);//方向向量=原点-posglm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);//事件回调函数void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);void do_moment();void mouse_callback(GLFWwindow* window, double xpos, double ypos);void roll_callback(GLFWwindow* window, double xoffset, double yoffset);int main(){//glfw initialif (!glfwInit()){cout << "Fail to initial glfw!" << endl;return -1;}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);//windowGLFWwindow *window = glfwCreateWindow(800, 600, "hello triangle", nullptr, nullptr);if (window == NULL){cout << "Fail to create window!" << endl;glfwTerminate();return -1;}//创建窗口的context指定为当前contextglfwMakeContextCurrent(window);//注册事件回调函数glfwSetKeyCallback(window, key_callback);glfwSetCursorPosCallback(window, mouse_callback);  //监听鼠标函数glfwSetScrollCallback(window, roll_callback);//监听鼠标滚轮函数//glew initialglewExperimental = GL_TRUE;if (glewInit() != GLEW_OK){cout << "Fail to initial glew!" << endl;glfwTerminate();return -1;}//viewportglViewport(0, 0, 800, 600);//dotfloat vertices[] = {-0.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0.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f};unsigned int VBO, VAO;glGenVertexArrays(1, &VAO);glGenBuffers(1, &VBO);glBindVertexArray(VAO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);// position attributeglVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);glEnableVertexAttribArray(0);// texture coord attributeglVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));glEnableVertexAttribArray(1);//texture GB//texture 1unsigned int texture1,texture2;glGenTextures(1, &texture1);glBindTexture(GL_TEXTURE_2D, texture1);// set the texture wrapping parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);// set texture wrapping to GL_REPEAT (default wrapping method)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);// set texture filtering parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//loadint height, width, nrChannel;unsigned char *date = stbi_load("container.jpg", &width, &height, &nrChannel, 0);//checkif (date){glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, date);glGenerateMipmap(GL_TEXTURE_2D);}else{cout << "Failed to load texture" << endl;}stbi_image_free(date);//texture 2glGenTextures(1, &texture2);glBindTexture(GL_TEXTURE_2D, texture2);// set the texture wrapping parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);// set texture wrapping to GL_REPEAT (default wrapping method)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);// set texture filtering parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);date = stbi_load("awesomeface.png", &width, &height, &nrChannel, 0);//checkif (date){glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, date);////////RGBAglGenerateMipmap(GL_TEXTURE_2D);}else{cout << "Failed to load texture2" << endl;}stbi_image_free(date);//positionglm::vec3 cubePositions[] = {glm::vec3(0.0f, 0.0f, 0.0f),glm::vec3(2.0f, 5.0f, -15.0f),glm::vec3(-1.5f, -2.2f, -2.5f),glm::vec3(-3.8f, -2.0f, -12.3f),glm::vec3(2.4f, -0.4f, -3.5f),glm::vec3(-1.7f, 3.0f, -7.5f),glm::vec3(1.3f, -2.0f, -2.5f),glm::vec3(1.5f, 2.0f, -2.5f),glm::vec3(1.5f, 0.2f, -1.5f),glm::vec3(-1.3f, 1.0f, -1.5f)};//准备着色器Shader shader("t.vert", "t.frag");GLint modelLoc = glGetUniformLocation(shader.programId, "model");GLint viewLoc = glGetUniformLocation(shader.programId, "view");GLint projectionLoc = glGetUniformLocation(shader.programId, "projection");//解除绑定glBindBuffer(GL_ARRAY_BUFFER, 0);glBindVertexArray(0);//开启深度测试glEnable(GL_DEPTH_TEST);//隐藏鼠标glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);//显示主循环while (!glfwWindowShouldClose(window)){//平衡设备速度GLfloat currentFrame = glfwGetTime();deltaTime = currentFrame - lastFrame;lastFrame = currentFrame;//记录键盘鼠标等glfwPollEvents();do_moment();//colorglClearColor(0.5f, 0.0f, 1.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//shadershader.use();//active textureglActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, texture1);glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D, texture2);glUniform1i(glGetUniformLocation(shader.programId, "ourTexture"), 0);glUniform1i(glGetUniformLocation(shader.programId, "ourTexture0"), 1);//camera//GLfloat camerax = sin((GLfloat)glfwGetTime())*10.0f;//(glm::radians(10.0f));//GLfloat cameraz = cos((GLfloat)glfwGetTime())*10.0f;// (glm::radians(10.0f));glm::mat4 view;view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);glm::mat4 projection;projection = glm::perspective(fov, (GLfloat)800/(GLfloat)600, 0.1f, 100.0f);//3DglUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));glBindVertexArray(VAO);for (GLuint i = 0; i < 10; i++){glm::mat4 model;//对于每一个model是会变换的,所以每次渲染的时候需要重新申请modelmodel = glm::translate(model, cubePositions[i]);GLfloat angle = 20.0f * i*(glfwGetTime()*50.0f/20);model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);}glBindVertexArray(0);glUseProgram(0);//交换缓存glfwSwapBuffers(window);}glDeleteVertexArrays(1, &VAO);glDeleteBuffers(1, &VBO);glfwTerminate();return 0;}void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){if (action == GLFW_PRESS)keys[key] = true;else if (action == GLFW_RELEASE)keys[key] = false;if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){glfwSetWindowShouldClose(window, GL_TRUE); // 关闭窗口}}void do_moment(){// 摄像机控制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 mouse_callback(GLFWwindow* window, double xpos, double ypos){if (firstmouse){xpos = lastx;ypos = lasty;firstmouse = false;}GLfloat offsetx = xpos - lastx;GLfloat offsety = lasty - ypos;lastx = xpos;lasty = ypos;GLfloat sensitivity = 0.05;offsetx *= sensitivity;offsety *= sensitivity;yaw += offsetx;pitch += offsety;if (pitch > 89.9)pitch = 89.9;if (pitch < -89.9)pitch = -89.9;glm::vec3 front;front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));front.y = sin(glm::radians(pitch));front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));cameraFront = glm::normalize(front);}void roll_callback(GLFWwindow* window, double xoffset, double yoffset){if (fov >= 1.0f && fov <= 45.0f)fov -= yoffset;if (fov <= 1.0f)fov = 1.0f;if (fov >= 45.0f)fov = 45.0f;}

原创粉丝点击