回想一下初中物理知识: 计算机如何表示颜色(应该算是个很基础的东西了): 回想 OpenGL基础5:第一个正方形 这一章 在这一章的时候,还没有接触到纹理,在颜色方面,也只是随便给他赋了个颜色 在此之后用了纹理,也就是简简单单的把颜色这一属性去掉了 现在重新考虑颜色这一属性,不再简单给物体随便赋颜色了,而是直接开始模拟光照!以给与物体一个真实的“颜色”,在此之前,先用前面的知识生成一个这样的场景: 其中白色的正方体是光源,珊瑚红色的正方体是物体 白色光源的着色器代码如下(LightFShader.txt 和 LightVShader): 珊瑚红物体的着色器如下(ObjFShader.txt 和 ObjVShader): 对于物体的顶点着色器,我们定义了两个uniform变量,分别代表着物体颜色和光照颜色 当我们把光源的颜色与物体的颜色相乘,所得到的就是这个物体所反射该光源的颜色(也就是我们感知到的颜色) 接下来就是主代码(Camera.h 和 Shader.h未改变,可以从前面的章节中找到): 一、RGB三原色
二、光照场景
#version 330 core out vec4 color; void main() { color = vec4(1.0f); } ///////////////////////////////////////////////////// #version 330 core layout (location = 0) in vec3 position; uniform mat4 model; //模型矩阵 uniform mat4 view; //观察矩阵 uniform mat4 projection; //投影矩阵 void main() { gl_Position = projection * view * model * vec4(position, 1.0); }
#version 330 core out vec4 color; uniform vec3 objectColor; uniform vec3 lightColor; in vec2 texIn; uniform sampler2D texOutA; void main() { color = vec4(lightColor * objectColor, 1.0f); } ///////////////////////////////////////////////////// #version 330 core layout (location = 0) in vec3 position; layout (location = 1) in vec2 texture; out vec2 texIn; uniform mat4 model; //模型矩阵 uniform mat4 view; //观察矩阵 uniform mat4 projection; //投影矩阵 void main() { gl_Position = projection * view * model * vec4(position, 1.0); //texIn = vec2(texture.x, 1.0f - texture.y); //暂时屏蔽纹理 }
#include<iostream> #include<opengl/glew.h> #define GLEW_STATIC #include<GLFW/glfw3.h> #include"Camera.h" #include<glm/glm.hpp> #include<glm/gtc/matrix_transform.hpp> #include<glm/gtc/type_ptr.hpp> #include"Shader.h" #include<opengl/freeglut.h> #include<SOIL.h> bool keys[1024]; Camera camera; GLfloat lastX, lastY; bool firstMouse = true; void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void mouse_callback(GLFWwindow* window, double xpos, double ypos); void cameraMove(); glm::vec3 lightPos(1.2f, 1.0f, 2.0f); const GLuint WIDTH = 800, HEIGHT = 600; int main() { 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); GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr); glfwMakeContextCurrent(window); glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glewExperimental = GL_TRUE; glewInit(); int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); Shader shaderObj("ObjVShader.txt", "ObjFShader.txt"); Shader shaderLight("LightVShader.txt", "LightFShader.txt"); 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 }; GLuint VBO, VAO, texture; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenTextures(1, &texture); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindTexture(GL_TEXTURE_2D, texture); 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); int picWidth, picHeight; 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); unsigned char* image = SOIL_load_image("Texture/wood.jpg", &picWidth, &picHeight, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, picWidth, picHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glGenerateMipmap(GL_TEXTURE_2D); SOIL_free_image_data(image); glBindTexture(GL_TEXTURE_2D, 0);*/ GLuint lightVAO; glGenVertexArrays(1, &lightVAO); glBindVertexArray(lightVAO); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); //VBO数据已经绑定且我们就用之前的顶点数据,所以无需再管理VBO glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); glEnable(GL_DEPTH_TEST); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); cameraMove(); //glBindTexture(GL_TEXTURE_2D, texture); shaderObj.Use(); GLint objectColorLoc = glGetUniformLocation(shaderObj.Program, "objectColor"); GLint lightColorLoc = glGetUniformLocation(shaderObj.Program, "lightColor"); glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f); //物体反射颜色 glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f); //光源:默认为白色 glm::mat4 model = glm::mat4(1.0f); glm::mat4 view = glm::mat4(1.0f); glm::mat4 projection = glm::mat4(1.0f); model = glm::rotate(model, glm::radians(57.0f), glm::vec3(-0.5f, 1.0f, 0.0f)); view = camera.GetViewMatrix(); projection = glm::perspective(glm::radians(camera.Zoom), (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f); GLint modelLoc = glGetUniformLocation(shaderObj.Program, "model"); GLint viewLoc = glGetUniformLocation(shaderObj.Program, "view"); GLint projLoc = glGetUniformLocation(shaderObj.Program, "projection"); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection)); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 36); //glBindTexture(GL_TEXTURE_2D, 0); shaderLight.Use(); model = glm::translate(glm::mat4(1.0f), lightPos); model = glm::scale(model, glm::vec3(0.2f)); modelLoc = glGetUniformLocation(shaderLight.Program, "model"); viewLoc = glGetUniformLocation(shaderLight.Program, "view"); projLoc = glGetUniformLocation(shaderLight.Program, "projection"); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection)); glBindVertexArray(lightVAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); glfwSwapBuffers(window); } glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glfwTerminate(); return 0; } GLfloat deltaTime = 0.0f; GLfloat lastFrame = 0.0f; void cameraMove() { GLfloat currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; GLfloat cameraSpeed = 1.0f * deltaTime; if (keys[GLFW_KEY_W]) camera.ProcessKeyboard(Camera_Movement(FORWARD), deltaTime); if (keys[GLFW_KEY_S]) camera.ProcessKeyboard(Camera_Movement(BACKWARD), deltaTime); if (keys[GLFW_KEY_A]) camera.ProcessKeyboard(Camera_Movement(LEFT), deltaTime); if (keys[GLFW_KEY_D]) camera.ProcessKeyboard(Camera_Movement(RIGHT), deltaTime); } 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 (action == GLFW_PRESS) //如果当前是按下操作 keys[key] = true; else if (action == GLFW_RELEASE) //松开键盘 keys[key] = false; } void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { camera.ProcessMouseScroll(yoffset); } 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; lastX = xpos; lastY = ypos; GLfloat sensitivity = 0.05; xoffset *= sensitivity; yoffset *= sensitivity; camera.ProcessMouseMovement(xoffset, yoffset); }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算