From 6ca0e0315ccdc51c9e94789f7364695847f175d1 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Thu, 25 May 2023 18:13:29 +0800 Subject: [PATCH] feat(OpenGL-hello): basic light --- OpenGL/hello/include/source.h | 58 +++++++++++------ OpenGL/hello/src/cube.fs | 25 ++++++++ OpenGL/hello/src/cube.vs | 18 ++++++ OpenGL/hello/src/light.fs | 8 +++ OpenGL/hello/src/light.vs | 11 ++++ OpenGL/hello/src/main.cpp | 113 ++++++++++++---------------------- OpenGL/hello/src/tex.fs | 12 ---- OpenGL/hello/src/tex.vs | 16 ----- 8 files changed, 139 insertions(+), 122 deletions(-) create mode 100644 OpenGL/hello/src/cube.fs create mode 100644 OpenGL/hello/src/cube.vs create mode 100644 OpenGL/hello/src/light.fs create mode 100644 OpenGL/hello/src/light.vs delete mode 100644 OpenGL/hello/src/tex.fs delete mode 100644 OpenGL/hello/src/tex.vs diff --git a/OpenGL/hello/include/source.h b/OpenGL/hello/include/source.h index a657f15..00c1bb8 100644 --- a/OpenGL/hello/include/source.h +++ b/OpenGL/hello/include/source.h @@ -9,30 +9,49 @@ #include #include -float 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, + float 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.0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vec3(2.0f, 5.0f, -15.0f), @@ -47,6 +66,7 @@ float lastFrame = 0.0f; // 上一帧的时间 // settings const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 800; +glm::vec3 lightPos(1.2f, 1.0f, 2.0f); // 创建摄像机 Camera camera = Camera(SCR_HEIGHT / 2.0f, SCR_HEIGHT / 2.0f); diff --git a/OpenGL/hello/src/cube.fs b/OpenGL/hello/src/cube.fs new file mode 100644 index 0000000..105f994 --- /dev/null +++ b/OpenGL/hello/src/cube.fs @@ -0,0 +1,25 @@ +#version 330 core +out vec4 FragColor; + +in vec3 Normal; +in vec3 FragPos; + +uniform vec3 lightPos; +uniform vec3 lightColor; +uniform vec3 objectColor; + +void main() +{ + // ambient + float ambientStrength = 0.1; + vec3 ambient = ambientStrength * lightColor; + + // diffuse + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(lightPos - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = diff * lightColor; + + vec3 result = (ambient + diffuse) * objectColor; + FragColor = vec4(result, 1.0); +} \ No newline at end of file diff --git a/OpenGL/hello/src/cube.vs b/OpenGL/hello/src/cube.vs new file mode 100644 index 0000000..d7ade7b --- /dev/null +++ b/OpenGL/hello/src/cube.vs @@ -0,0 +1,18 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; + +out vec3 FragPos; +out vec3 Normal; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + FragPos = vec3(model * vec4(aPos, 1.0)); + Normal = aNormal; + + gl_Position = projection * view * vec4(FragPos, 1.0); +} \ No newline at end of file diff --git a/OpenGL/hello/src/light.fs b/OpenGL/hello/src/light.fs new file mode 100644 index 0000000..674ab6e --- /dev/null +++ b/OpenGL/hello/src/light.fs @@ -0,0 +1,8 @@ +#version 330 core +out vec4 FragColor; +uniform vec3 lightColor; + +void main() +{ + FragColor = vec4(lightColor, 1.0); // set all 4 vector values to 1.0 +} \ No newline at end of file diff --git a/OpenGL/hello/src/light.vs b/OpenGL/hello/src/light.vs new file mode 100644 index 0000000..752ce21 --- /dev/null +++ b/OpenGL/hello/src/light.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/OpenGL/hello/src/main.cpp b/OpenGL/hello/src/main.cpp index 14af8cf..a0c9606 100644 --- a/OpenGL/hello/src/main.cpp +++ b/OpenGL/hello/src/main.cpp @@ -37,7 +37,8 @@ int main() { glfwSetScrollCallback(window, scroll_callback); // 创建着色器程序 - Shader shader = Shader("src/tex.vs", "src/tex.fs"); + Shader shader = Shader("src/cube.vs", "src/cube.fs"); + Shader lightShader = Shader("src/light.vs", "src/light.fs"); unsigned int VAO, VBO, EBO; // 创建顶点数组对象 @@ -50,80 +51,35 @@ int main() { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // 顶点属性 - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); - // 纹理坐标属性 - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), + // 法向量属性 + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float))); glEnableVertexAttribArray(1); - // 创建纹理 - unsigned int texture1, texture2; - glGenTextures(1, &texture1); - glBindTexture(GL_TEXTURE_2D, texture1); - // 设置纹理环绕方式 - 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_LINEAR); - - // 加载并生成纹理 - int width, height, nrChannels; - // 图片翻转y轴 - stbi_set_flip_vertically_on_load(true); - unsigned char *data = - stbi_load("resources/container.jpg", &width, &height, &nrChannels, 0); - if (data) { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, - GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - } else { - std::cout << "Failed to load texture" << std::endl; - } - stbi_image_free(data); - - glGenTextures(1, &texture2); - glBindTexture(GL_TEXTURE_2D, texture2); - // 设置纹理环绕方式 - 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_LINEAR); - - // 加载并生成纹理 - // 图片翻转y轴 - stbi_set_flip_vertically_on_load(true); - data = - stbi_load("resources/awesomeface.png", &width, &height, &nrChannels, 0); - if (data) { - - // note that the awesomeface.png has transparency and thus an alpha channel, - // so make sure to tell OpenGL the data type is of GL_RGBA - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, - GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - } else { - std::cout << "Failed to load texture" << std::endl; - } - stbi_image_free(data); + // 设置光源 + unsigned int lightVAO; + glGenVertexArrays(1, &lightVAO); + glBindVertexArray(lightVAO); + // 只需要绑定VBO不用再次设置VBO的数据,因为箱子的VBO数据中已经包含了正确的立方体顶点数据 + glBindBuffer(GL_ARRAY_BUFFER, VBO); + // 设置灯立方体的顶点属性(对我们的灯来说仅仅只有位置数据) + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); + glEnableVertexAttribArray(0); // 激活着色器 shader.use(); // 绑定纹理 shader.setInt("texture1", 0); shader.setInt("texture2", 1); + shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f); + shader.setVec3("objectColor", 1.0f, 0.5f, 0.31f); - // create transformations - glm::mat4 view = - glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), - glm::vec3(0.0f, 1.0f, 0.0f)); - glm::mat4 projection = glm::mat4(1.0f); - view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); + lightShader.use(); + lightShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f); + lightShader.setVec3("lightPos", lightPos); // 开启深度测试 glEnable(GL_DEPTH_TEST); @@ -140,18 +96,20 @@ int main() { processInput(window); // 设置为白色 - glClearColor(1.0f, 1.0f, 1.0f, 0.0f); + glClearColor(0.1f, 0.1f, 0.1f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // 绑定纹理 - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, texture2); + glm::mat4 view = camera.GetViewMatrix(); + + glm::mat4 projection = + glm::perspective(glm::radians(camera.Zoom), + (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); // 绘制三角形 shader.use(); + shader.setMat4("projection", projection); + shader.setMat4("view", view); // 设置变换矩阵 glm::mat4 trans(1.0f); trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25, 0.0)); @@ -169,13 +127,18 @@ int main() { glDrawArrays(GL_TRIANGLES, 0, 36); } - float radius = 10.0f; - view = camera.GetViewMatrix(); - shader.setMat4("view", view); + lightShader.use(); + + glm::mat4 model = glm::mat4(1.0f); + model = glm::translate(model, lightPos); + model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube + glBindVertexArray(lightVAO); + lightShader.setMat4("projection", projection); + lightShader.setMat4("view", view); + lightShader.setMat4("model", model); + + glDrawArrays(GL_TRIANGLES, 0, 36); - projection = glm::perspective( - glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); - shader.setMat4("projection", projection); // 检查并调用事件,交换缓冲 glfwSwapBuffers(window); glfwPollEvents(); diff --git a/OpenGL/hello/src/tex.fs b/OpenGL/hello/src/tex.fs deleted file mode 100644 index 91b9fe8..0000000 --- a/OpenGL/hello/src/tex.fs +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 core -out vec4 FragColor; - -in vec2 TexCoord; - -uniform sampler2D texture1; -uniform sampler2D texture2; - -void main() -{ - FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); -} \ No newline at end of file diff --git a/OpenGL/hello/src/tex.vs b/OpenGL/hello/src/tex.vs deleted file mode 100644 index 3cc769f..0000000 --- a/OpenGL/hello/src/tex.vs +++ /dev/null @@ -1,16 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 aPos; -layout(location = 1) in vec2 aTexCoord; - -out vec2 TexCoord; - -uniform mat4 model; -uniform mat4 view; -uniform mat4 projection; -uniform mat4 trans; - -void main() { - gl_Position = projection * view * model * trans * vec4(aPos, 1.0); -// gl_Position = trans * vec4(aPos, 1.0); - TexCoord = aTexCoord; -}