#include<cmath>#include<map>#include<iostream>#include<GL/glew.h>#include<GLFW/glfw3.h>#include<glm/glm.hpp>#include<glm/gtc/type_ptr.hpp>#include<glm/gtc/matrix_transform.hpp>#include<SOIL.h>#include"shader.h"#include"camera.h"GLuint WIDTH = 800, HEIGHT = 600;Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));char keys[1024];GLboolean firstMouse = true;GLfloat lastX=400,lastY=300;GLfloat deltaTime = 0.0f;GLfloat lastFrame = 0.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] = GL_TRUE;else if(action==GLFW_RELEASE)keys[key] = GL_FALSE;}void Domovement(){if (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 mousecallback(GLFWwindow* window, double xpos, double ypos){if (firstMouse){lastX = xpos;lastY = ypos;firstMouse = false;}GLfloat xoffset = xpos - lastX;GLfloat yoffset = lastY - ypos;camera.ProcessMouseMovement(xoffset, yoffset);}void scrollcallback(GLFWwindow* window, double xoffset, double yoffset){camera.ProcessMouseScroll(xoffset);}GLFWwindow* myCreateWindow(){glfwInit();glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OPENGL", nullptr, nullptr);glfwMakeContextCurrent(window);glfwSetKeyCallback(window, keyboard);glfwSetCursorPosCallback(window, mousecallback);glfwSetScrollCallback(window, scrollcallback);glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);glewExperimental = GL_TRUE;glewInit();glViewport(0, 0, WIDTH, HEIGHT);return window;}GLuint loadTexture(const char* path, GLboolean alpha=GL_FALSE){GLuint texture;GLint width, height;glGenTextures(1, &texture);glBindTexture(GL_TEXTURE_2D, texture);unsigned char* image = SOIL_load_image(path, &width, &height, 0, alpha ? SOIL_LOAD_RGBA : SOIL_LOAD_RGB);glTexImage2D(GL_TEXTURE_2D, 0, alpha ? GL_RGBA : GL_RGB, width, height, 0, alpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, image);glGenerateMipmap(GL_TEXTURE_2D);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, alpha ? GL_CLAMP_TO_EDGE : GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, alpha ? GL_CLAMP_TO_EDGE : GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glBindTexture(GL_TEXTURE_2D, 0);SOIL_free_image_data(image);return texture;}GLuint setVertexAttribArray(GLuint VAO, GLuint VBO, GLfloat vertices[]){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(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));glEnableVertexAttribArray(1);glBindBuffer(GL_ARRAY_BUFFER, 0);glBindVertexArray(0);return 1;}int main(){GLFWwindow* window = myCreateWindow();Shader shader("vertexShader.glsl", "fragmentShader.glsl");Shader screenShader("newVertexShader.glsl", "newFragmentShader.glsl");GLfloat cubeVertices[] = {// Back face-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom-left0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // bottom-right0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left// Front face-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-right0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // top-left-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left// Left face-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-left-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right// Right face0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left// Bottom face-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // top-left0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right// Top face-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left-0.5f, 0.5f, 0.5f, 0.0f, 0.0f // bottom-left};GLfloat planeVertices[] = {// Positions // Texture Coords5.0f, -0.5f, 5.0f, 2.0f, 0.0f,-5.0f, -0.5f, 5.0f, 0.0f, 0.0f,-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,5.0f, -0.5f, 5.0f, 2.0f, 0.0f,-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,5.0f, -0.5f, -5.0f, 2.0f, 2.0f};GLfloat quadVertices[] = {// Positions // TexCoords-1.0f, 1.0f, 0.0f, 1.0f,-1.0f, -1.0f, 0.0f, 0.0f,1.0f, -1.0f, 1.0f, 0.0f,-1.0f, 1.0f, 0.0f, 1.0f,1.0f, -1.0f, 1.0f, 0.0f,1.0f, 1.0f, 1.0f, 1.0f};GLuint cubeVAO, cubeVBO;glGenVertexArrays(1, &cubeVAO);glGenBuffers(1, &cubeVBO);glBindVertexArray(cubeVAO);glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);glEnableVertexAttribArray(0);glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));glEnableVertexAttribArray(1);glBindBuffer(GL_ARRAY_BUFFER, 0);glBindVertexArray(0);GLuint planeVAO, planeVBO;glGenVertexArrays(1, &planeVAO);glGenBuffers(1, &planeVBO);glBindVertexArray(planeVAO);glBindBuffer(GL_ARRAY_BUFFER, planeVBO);glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);glEnableVertexAttribArray(0);glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));glEnableVertexAttribArray(1);glBindBuffer(GL_ARRAY_BUFFER, 0);glBindVertexArray(0);GLuint quadVAO, quadVBO;glGenVertexArrays(1, &quadVAO);glGenBuffers(1, &quadVBO);glBindVertexArray(quadVAO);glBindBuffer(GL_ARRAY_BUFFER, quadVBO);glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);glEnableVertexAttribArray(0);glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));glEnableVertexAttribArray(1);glBindBuffer(GL_ARRAY_BUFFER, 0);glBindVertexArray(0);// 生成一个帧缓存GLuint framebuffer;glGenFramebuffers(1, &framebuffer);glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);GLuint texColorBuffer;glGenTextures(1, &texColorBuffer);glBindTexture(GL_TEXTURE_2D, texColorBuffer);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glBindTexture(GL_TEXTURE_2D, 0);// 把一个纹理对象附加到颜色附件上glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);// 生成一个渲染缓存对象GLuint RBO;glGenRenderbuffers(1, &RBO);glBindRenderbuffer(GL_RENDERBUFFER, RBO);glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, WIDTH, HEIGHT);glBindRenderbuffer(GL_RENDERBUFFER, 0);// 把渲染缓冲对象附加到深度模板附件上glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RBO);if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)cout << "ERROR::FRAMEBUFFER:: framebuffer is not complete" << endl;glBindFramebuffer(GL_FRAMEBUFFER, 0);GLuint cubeTexture = loadTexture("container.jpg");GLuint planeTexture = loadTexture("metal.png");//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);while(!glfwWindowShouldClose(window)){GLfloat currentFrame = glfwGetTime();deltaTime = currentFrame - lastFrame;lastFrame = currentFrame;glfwPollEvents();Domovement();// 绑定帧缓冲--framebufferglBindFramebuffer(GL_FRAMEBUFFER, framebuffer);glClearColor(0.1f, 0.1f, 0.1f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);glEnable(GL_DEPTH_TEST);glm::mat4 model;glm::mat4 view = camera.GetViewMatrix();glm::mat4 projection = glm::perspective(camera.Zoom, (float)WIDTH/(float)HEIGHT, 0.1f, 100.0f);shader.Use();glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));//planeglBindVertexArray(planeVAO);glBindTexture(GL_TEXTURE_2D, planeTexture);model = glm::mat4();glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 6);glBindVertexArray(0);// CubesglBindVertexArray(cubeVAO);glBindTexture(GL_TEXTURE_2D, cubeTexture);model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);model = glm::mat4();model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(0);// 解绑帧缓冲---使用默认的帧缓冲glBindFramebuffer(GL_FRAMEBUFFER, 0);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glClearColor(1.0f, 1.0f, 1.0f, 1.0f);glDisable(GL_DEPTH_TEST);screenShader.Use();glBindVertexArray(quadVAO);glBindTexture(GL_TEXTURE_2D, texColorBuffer);glDrawArrays(GL_TRIANGLES, 0, 6);glBindVertexArray(0);glfwSwapBuffers(window);}glfwTerminate();return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。