#include<cmath>#include<iostream>#include<GL/glew.h>#include<GLFW/glfw3.h>#include<SOIL.h>#include<glm/glm.hpp>#include<glm/gtc/type_ptr.hpp>#include<glm/gtc/matrix_transform.hpp>#include "shader.h"#include "camera.h"typedef GLint Int;typedef GLuint Uint;typedef GLchar Char;typedef GLfloat Float;Uint WIDTH = 800, HEIGHT = 600;Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));Float lastX = 400.0;Float lastY = 300.0;bool keys[1025];Float deltaTime = 0.0f;Float lastFrame = 0.0f;bool firstMouse = GL_TRUE;glm::vec3 lightPos(0.5f, 0.4f, 1.0f);void keyboard(GLFWwindow* window, int key, int scancode, int action, int mode){if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)glfwSetWindowShouldClose(window, GL_TRUE);if (action == GLFW_PRESS)keys[key] = true;else if (action == GLFW_RELEASE)keys[key] = false;}void do_movement(){// Camera controlsif(keys[GLFW_KEY_W])camera.ProcessKeyboard(FORWARD, deltaTime);if(keys[GLFW_KEY_S])camera.ProcessKeyboard(BACKWARD, deltaTime);if(keys[GLFW_KEY_A])camera.ProcessKeyboard(LEFT, deltaTime);if(keys[GLFW_KEY_D])camera.ProcessKeyboard(RIGHT, deltaTime);}void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode){//cout << key << endl;if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)glfwSetWindowShouldClose(window, GL_TRUE);if (key >= 0 && key < 1024){if(action == GLFW_PRESS)keys[key] = true;else if(action == GLFW_RELEASE)keys[key] = false;}}void mouse_callback(GLFWwindow* window, double xpos, double ypos){if(firstMouse){lastX = xpos;lastY = ypos;firstMouse = false;}GLfloat xoffset = xpos - lastX;GLfloat yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to leftlastX = xpos;lastY = ypos;camera.ProcessMouseMovement(xoffset, yoffset);}void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){camera.ProcessMouseScroll(yoffset);}GLFWwindow* InitWindow(){glfwInit();glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "transform", nullptr, nullptr);glfwMakeContextCurrent(window);// 键盘回调函数glfwSetKeyCallback(window, keyboard);// 鼠标(光标)回调函数glfwSetCursorPosCallback(window, mouse_callback);// 滚轮回调函数glfwSetScrollCallback(window, scroll_callback);glewExperimental = GL_TRUE;glewInit();glViewport(0, 0, WIDTH, HEIGHT);return window;}void SetContainerAttrib(Shader containerShader, GLuint &containerVAO, GLuint &texture1, GLuint &texture2){GLfloat vertices[] = {// 顶点位置 顶点法向量-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f};Uint VBO;glGenVertexArrays(1,&containerVAO);glGenBuffers(1, &VBO);glBindVertexArray(containerVAO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);glEnableVertexAttribArray(0);glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));glEnableVertexAttribArray(1);glBindVertexArray(0);}void SetLampAttrib(Shader lampShader, GLuint &lampVAO){GLfloat lampvertices[] = {-0.5f, -0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f, 0.5f, -0.5f,0.5f, 0.5f, -0.5f,-0.5f, 0.5f, -0.5f,-0.5f, -0.5f, -0.5f,-0.5f, -0.5f, 0.5f,0.5f, -0.5f, 0.5f,0.5f, 0.5f, 0.5f,0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f, -0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f, -0.5f,-0.5f, -0.5f, -0.5f,-0.5f, -0.5f, -0.5f,-0.5f, -0.5f, 0.5f,-0.5f, 0.5f, 0.5f,0.5f, 0.5f, 0.5f,0.5f, 0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f, -0.5f, 0.5f,0.5f, 0.5f, 0.5f,-0.5f, -0.5f, -0.5f,0.5f, -0.5f, -0.5f,0.5f, -0.5f, 0.5f,0.5f, -0.5f, 0.5f,-0.5f, -0.5f, 0.5f,-0.5f, -0.5f, -0.5f,-0.5f, 0.5f, -0.5f,0.5f, 0.5f, -0.5f,0.5f, 0.5f, 0.5f,0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f, -0.5f};GLuint VBO;glGenVertexArrays(1, &lampVAO);glGenBuffers(1, &VBO);glBindVertexArray(lampVAO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(lampvertices), lampvertices, GL_STATIC_DRAW);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);glEnableVertexAttribArray(0);glBindVertexArray(0);}int main(){GLFWwindow* window = InitWindow();const Char* vertexPath = "E:\\OpenGL\\OpenGL_test1\\Shader\\vertexShader.glsl";const Char* fragmentPath = "E:\\OpenGL\\OpenGL_test1\\Shader\\fragmentShader.glsl";const Char* lampVertex = "E:\\OpenGL\\OpenGL_test1\\Shader\\lampvertexShader.glsl";const Char* lampFragment = "E:\\OpenGL\\OpenGL_test1\\Shader\\lampFragmentShader.glsl";Shader containerShader(vertexPath, fragmentPath);Shader lampShader(lampVertex, lampFragment);GLuint containerVAO, texture1, texture2;SetContainerAttrib(containerShader, containerVAO, texture1, texture2);GLuint lampVAO;SetLampAttrib(lampShader, lampVAO);//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);glEnable(GL_DEPTH_TEST);//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);while (!glfwWindowShouldClose(window)){glfwPollEvents();do_movement();glClearColor(0.0f, 0.0f, 0.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);// 立方体containerShader.Use();lightPos.x = cos(glfwGetTime() / 2.0f)*1.0f;lightPos.y = sin(glfwGetTime() / 2.0f)*1.0f;Uint lightPosLoc = glGetUniformLocation(containerShader.Program, "lightPos");glUniform3f(lightPosLoc, lightPos.x,lightPos.y,lightPos.z);Uint lightLoc = glGetUniformLocation(containerShader.Program, "lightColor");Uint objectLoc = glGetUniformLocation(containerShader.Program, "objectColor");glUniform3f(lightLoc, 1.0f, 0.5f, 0.31f);glUniform3f(objectLoc, 1.0f, 1.0f, 1.0f);Uint viewPosLoc = glGetUniformLocation(containerShader.Program, "viewPos");glUniform3f(viewPosLoc, camera.Position.x, camera.Position.y, camera.Position.z);glm::mat4 views = camera.GetViewMatrix();glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);Uint model_Location = glGetUniformLocation(containerShader.Program, "model");Uint views_Location = glGetUniformLocation(containerShader.Program, "views");Uint projection_Location = glGetUniformLocation(containerShader.Program, "projection");glUniformMatrix4fv(views_Location, 1, GL_FALSE, glm::value_ptr(views));glUniformMatrix4fv(projection_Location, 1, GL_FALSE, glm::value_ptr(projection));glBindVertexArray(containerVAO);glm::mat4 model;model = glm::translate(model, glm::vec3(-0.55f, -0.3f, 0.0f));model = glm::rotate(model, (GLfloat)glm::radians(glfwGetTime()*45.0f), glm::vec3(1.0f, 1.0f, 0.0f));glUniformMatrix4fv(model_Location, 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(0);// 光源绘制lampShader.Use();Uint modelLoc = glGetUniformLocation(lampShader.Program, "model");Uint viewLoc = glGetUniformLocation(lampShader.Program, "view");Uint projectionLoc = glGetUniformLocation(lampShader.Program, "projection");glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(views));glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));model = glm::mat4();model = glm::translate(model, lightPos);model = glm::scale(model, glm::vec3(0.2f));glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));glBindVertexArray(lampVAO);glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(0);glfwSwapBuffers(window);}glDeleteVertexArrays(1, &containerVAO);glfwTerminate();return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。