openGL Lighting Maps
来源:互联网 发布:南昌大学软件学院学费 编辑:程序博客网 时间:2024/04/30 22:46
实现一些光照照射到object上的真实反射
main:
#include <iostream>#include <cmath>// GLEW#define GLEW_STATIC#include <glew.h>// GLFW#include <glfw3.h>// Other Libs#include <SOIL.h>// GLM Mathematics#include <glm/glm.hpp>#include <glm/gtc/matrix_transform.hpp>#include <glm/gtc/type_ptr.hpp>// Other includes#include "Shader.h"#include "Camera.h"// Function prototypesvoid key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);void mouse_callback(GLFWwindow* window, double xpos, double ypos);void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);void do_movement();// Window dimensionsconst GLuint WIDTH = 800, HEIGHT = 600;// CameraCamera camera(glm::vec3(0.0f, 0.0f, 3.0f));GLfloat lastX = WIDTH / 2.0;GLfloat lastY = HEIGHT / 2.0;bool keys[1024];// Light attributesglm::vec3 lightPos(1.2f, 1.0f, 2.0f);// DeltatimeGLfloat deltaTime = 0.0f;// Time between current frame and last frameGLfloat lastFrame = 0.0f; // Time of last frame// The MAIN function, from here we start the application and run the game loopint main(){// Init GLFWglfwInit();// Set all the required options for GLFWglfwWindowHint(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_SAMPLES, 4);// Create a GLFWwindow object that we can use for GLFW's functionsGLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);glfwMakeContextCurrent(window);// Set the required callback functionsglfwSetKeyCallback(window, key_callback);glfwSetCursorPosCallback(window, mouse_callback);glfwSetScrollCallback(window, scroll_callback);// GLFW OptionsglfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensionsglewExperimental = GL_TRUE;// Initialize GLEW to setup the OpenGL Function pointersglewInit();// Define the viewport dimensionsglViewport(0, 0, WIDTH, HEIGHT);// OpenGL optionsglEnable(GL_DEPTH_TEST);// Build and compile our shader programShader lightingShader("lighting.vs", "lighting.frag");Shader lampShader("lamp.vs", "lamp.frag");// Set up vertex data (and buffer(s)) and attribute pointersGLfloat vertices[] = {// Positions // Normals // Texture Coords-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};// First, set the container's VAO (and VBO)GLuint VBO, containerVAO;glGenVertexArrays(1, &containerVAO);glGenBuffers(1, &VBO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);glBindVertexArray(containerVAO);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);glEnableVertexAttribArray(0);glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));glEnableVertexAttribArray(1);glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));glEnableVertexAttribArray(2);glBindVertexArray(0);// Then, we set the light's VAO (VBO stays the same. After all, the vertices are the same for the light object (also a 3D cube))GLuint lightVAO;glGenVertexArrays(1, &lightVAO);glBindVertexArray(lightVAO);// We only need to bind to the VBO (to link it with glVertexAttribPointer), no need to fill it; the VBO's data already contains all we need.glBindBuffer(GL_ARRAY_BUFFER, VBO);// Set the vertex attributes (only position data for the lamp))glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0); // Note that we skip over the other data in our buffer object (we don't need the normals/textures, only positions).glEnableVertexAttribArray(0);glBindVertexArray(0);// Load texturesGLuint diffuseMap, specularMap, emissionMap;glGenTextures(1, &diffuseMap);glGenTextures(1, &specularMap);glGenTextures(1, &emissionMap);int width, height;unsigned char* image;// Diffuse mapimage = SOIL_load_image("container2.png", &width, &height, 0, SOIL_LOAD_RGB);glBindTexture(GL_TEXTURE_2D, diffuseMap);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);glGenerateMipmap(GL_TEXTURE_2D);SOIL_free_image_data(image);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);// Specular mapimage = SOIL_load_image("container2_specular.png", &width, &height, 0, SOIL_LOAD_RGB);glBindTexture(GL_TEXTURE_2D, specularMap);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);glGenerateMipmap(GL_TEXTURE_2D);SOIL_free_image_data(image);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);image = SOIL_load_image("matrix.jpg", &width, &height, 0, SOIL_LOAD_RGB);glBindTexture(GL_TEXTURE_2D, emissionMap);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);glGenerateMipmap(GL_TEXTURE_2D);SOIL_free_image_data(image);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);glBindTexture(GL_TEXTURE_2D, 0);lightingShader.Use();glUniform1i(glGetUniformLocation(lightingShader.Program, "material.diffuse"), 0);glUniform1i(glGetUniformLocation(lightingShader.Program, "material.specular"), 1);glUniform1i(glGetUniformLocation(lightingShader.Program, "material.emission"), 2);// Game loopwhile (!glfwWindowShouldClose(window)){// Calculate deltatime of current frameGLfloat currentFrame = glfwGetTime();deltaTime = currentFrame - lastFrame;lastFrame = currentFrame;// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functionsglfwPollEvents();do_movement();// Clear the colorbufferglClearColor(0.1f, 0.1f, 0.1f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Use cooresponding shader when setting uniforms/drawing objectslightingShader.Use();GLint lightPosLoc = glGetUniformLocation(lightingShader.Program, "light.position");GLint viewPosLoc = glGetUniformLocation(lightingShader.Program, "viewPos");glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);glUniform3f(viewPosLoc, camera.Position.x, camera.Position.y, camera.Position.z);// Set lights propertiesglUniform3f(glGetUniformLocation(lightingShader.Program, "light.ambient"), 0.2f, 0.2f, 0.2f);glUniform3f(glGetUniformLocation(lightingShader.Program, "light.diffuse"), 0.5f, 0.5f, 0.5f);glUniform3f(glGetUniformLocation(lightingShader.Program, "light.specular"), 1.0f, 1.0f, 1.0f);// Set material propertiesglUniform1f(glGetUniformLocation(lightingShader.Program, "material.shininess"), 32.0f);// Create camera transformationsglm::mat4 view;view = camera.GetViewMatrix();glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);// Get the uniform locationsGLint modelLoc = glGetUniformLocation(lightingShader.Program, "model");GLint viewLoc = glGetUniformLocation(lightingShader.Program, "view");GLint projLoc = glGetUniformLocation(lightingShader.Program, "projection");// Pass the matrices to the shaderglUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));// Bind diffuse mapglActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, diffuseMap);// Bind specular mapglActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D, specularMap);glActiveTexture(GL_TEXTURE2);glBindTexture(GL_TEXTURE_2D, emissionMap);// Draw the container (using container's vertex attributes)glBindVertexArray(containerVAO);glm::mat4 model;glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(0);// Also draw the lamp object, again binding the appropriate shaderlampShader.Use();// Get location objects for the matrices on the lamp shader (these could be different on a different shader)modelLoc = glGetUniformLocation(lampShader.Program, "model");viewLoc = glGetUniformLocation(lampShader.Program, "view");projLoc = glGetUniformLocation(lampShader.Program, "projection");// Set matricesglUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));model = glm::mat4();model = glm::translate(model, lightPos);model = glm::scale(model, glm::vec3(0.2f)); // Make it a smaller cubeglUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));// Draw the light object (using light's vertex attributes)glBindVertexArray(lightVAO);glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(0);// Swap the screen buffersglfwSwapBuffers(window);}// Terminate GLFW, clearing any resources allocated by GLFW.glfwTerminate();return 0;}// Is called whenever a key is pressed/released via GLFWvoid 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){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);}bool firstMouse = true;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);}
Lighting.vs:
#version 330 corelayout (location = 0) in vec3 position;layout (location = 1) in vec3 normal;layout (location = 2) in vec2 texCoords;uniform mat4 model;uniform mat4 view;uniform mat4 projection;out vec3 FragPos;out vec3 Normal;out vec2 TexCoords;void main(){ gl_Position = projection * view * model * vec4(position, 1.0f);FragPos = vec3(model * vec4(position, 1.0f));Normal = mat3(transpose(inverse(model))) * normal;TexCoords = texCoords;}
Lighting.frag:
#version 330 corestruct Material { sampler2D diffuse; sampler2D specular; sampler2D emission; float shininess;}; struct Light { vec3 position; vec3 ambient; vec3 diffuse; vec3 specular;};in vec3 FragPos; in vec3 Normal; in vec2 TexCoords; out vec4 color; uniform vec3 viewPos;uniform Material material;uniform Light light;uniform float time;void main(){ // Ambient vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); // Diffuse vec3 norm = normalize(Normal); vec3 lightDir = normalize(light.position - FragPos); float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); // Specular vec3 viewDir = normalize(viewPos - FragPos); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords)); // Emission vec3 emission = vec3(texture(material.emission, TexCoords)); color = vec4(ambient + diffuse + specular + emission, 1.0f); }Lamp.vs:
#version 330 corelayout (location = 0) in vec3 position;uniform mat4 model;uniform mat4 view;uniform mat4 projection;void main(){gl_Position = projection * view * model * vec4(position, 1.0f);}
Lamp.frag:
#version 330 coreout vec4 color;void main(){color = vec4(1.0f);}
0 0
- openGL Lighting Maps
- OpenGL学习脚印: 光照中材质和lighting maps使用(material and lighting maps)
- OpenGL学习: 光照系列2-材质和lighting maps使用
- OpenGL - Lighting
- OpenGL ES 学习教程(九) 油腻的效果 Lighting Maps 高光贴图
- Lighting Models and BRDF Maps
- openGL CG2 vertex lighting
- openGL CG3-Pixel Lighting
- openGL CG4 - Lighting + Texture
- OpenGL 练习11 Lighting
- OpenGL 练习11.1 Lighting
- OpenGL 练习11.2 Lighting
- OpenGL 练习11.3 Lighting
- OpenGL – Lighting
- OpenGL学习之Basic-Lighting
- OpenGL lighting hints for new programmers
- openGL CG 系列教程2 - Vertex Lighting
- openGL CG 系列教程3-Pixel Lighting
- 20170118
- find命令
- IO流_File类的删除功能
- C语言中的typeof关键字
- 原生纯js完美缓冲运动轮播图
- openGL Lighting Maps
- double类型的输出方式
- 除了SLEEP还有没有更高精度的函数啊?
- MFC 有没有显示X509证书的控件
- 电脑资料整理
- VC程序如何保护初始化文件不被别人修改
- Hadoop源码解析之distributedshell
- 【微信小程序】image图片控件摆放案例
- HTML5 内联 SVG