From ebd13441c8ac2de793d20d961ef16babb355a12a Mon Sep 17 00:00:00 2001 From: toulzx Date: Mon, 15 Nov 2021 20:52:39 +0800 Subject: [PATCH] =?UTF-8?q?[Week8-2][Week8-3]=E5=AE=9E=E7=8E=B0=E6=97=8B?= =?UTF-8?q?=E8=BD=AC=E5=85=89=E6=BA=90=E7=85=A7=E5=B0=84=E7=89=A9=E4=BD=93?= =?UTF-8?q?=E7=9A=84=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/HelloCG/Camera.h | 5 + Source/HelloCG/HelloCG.vcxproj | 3 + Source/HelloCG/HelloCG.vcxproj.filters | 9 ++ Source/HelloCG/Light.h | 95 ++++++++++++++++ Source/HelloCG/main.cpp | 149 ++++++++++++++++--------- Source/HelloCG/res/shaders/core.fs | 33 +++++- Source/HelloCG/res/shaders/core.vs | 5 + Source/HelloCG/res/shaders/light.fs | 12 ++ Source/HelloCG/res/shaders/light.vs | 17 +++ 9 files changed, 274 insertions(+), 54 deletions(-) create mode 100644 Source/HelloCG/Light.h create mode 100644 Source/HelloCG/res/shaders/light.fs create mode 100644 Source/HelloCG/res/shaders/light.vs diff --git a/Source/HelloCG/Camera.h b/Source/HelloCG/Camera.h index a9a7889..148979d 100644 --- a/Source/HelloCG/Camera.h +++ b/Source/HelloCG/Camera.h @@ -98,6 +98,11 @@ class Camera return this->zoom; } + glm::vec3 GetPosition() + { + return this->cameraPosition; + } + void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime) { GLfloat velocity = this->movementSpeed * deltaTime; diff --git a/Source/HelloCG/HelloCG.vcxproj b/Source/HelloCG/HelloCG.vcxproj index 891c83b..2cfa49e 100644 --- a/Source/HelloCG/HelloCG.vcxproj +++ b/Source/HelloCG/HelloCG.vcxproj @@ -148,9 +148,12 @@ + + + diff --git a/Source/HelloCG/HelloCG.vcxproj.filters b/Source/HelloCG/HelloCG.vcxproj.filters index 7e28ca7..a07ed36 100644 --- a/Source/HelloCG/HelloCG.vcxproj.filters +++ b/Source/HelloCG/HelloCG.vcxproj.filters @@ -35,6 +35,12 @@ 资源文件\res\shaders + + 资源文件\res\shaders + + + 资源文件\res\shaders + @@ -43,5 +49,8 @@ 头文件 + + 头文件 + \ No newline at end of file diff --git a/Source/HelloCG/Light.h b/Source/HelloCG/Light.h new file mode 100644 index 0000000..f6dd67d --- /dev/null +++ b/Source/HelloCG/Light.h @@ -0,0 +1,95 @@ +#pragma once +#ifndef Light_h +#define Light_h + +#include +#include "Shader.h" + + +// main.cpp Ƶ壬ָĬϰɫ +GLfloat vertices[] = +{ // position + + // z = -0.5 ľ + -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, + + // z = 0.5 ľ + -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, + + // x = -0.5 ľ + -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, + + // x = 0.5 ľ + 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, + + // y = -0.5 ľ + -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, + + // y = 0.5 ľ + -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, +}; + +class Light +{ +public: + Light() + { + this->update(); + } + void Draw() + { + glBindVertexArray(this->VAO); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + } +private: + GLuint VAO, VBO; + void update() + { + glGenVertexArrays(1, &this->VAO); + glGenBuffers(1, &this->VBO); + // connect the VAO and VBO + glBindVertexArray(this->VAO); + glBindBuffer(GL_ARRAY_BUFFER, this->VBO); + // transfer the data + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + // set the attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + } +}; + + +#endif // !Light_h diff --git a/Source/HelloCG/main.cpp b/Source/HelloCG/main.cpp index c5869c7..7751d7a 100644 --- a/Source/HelloCG/main.cpp +++ b/Source/HelloCG/main.cpp @@ -8,9 +8,11 @@ #include #include #include +#include #include "Shader.h" #include "Camera.h" +#include "Light.h" // ڴС @@ -39,6 +41,11 @@ bool firstMouse = true; GLfloat lastX = WIDTH / 2.0f; GLfloat lastY = HEIGHT / 2.0f; +// +const GLfloat DIFFUSE = 0.8f; +// 淴 +const GLfloat SPECULAR = 0.6f; + int main() { @@ -69,7 +76,7 @@ int main() // GLFW עӦ glfwSetKeyCallback(window, KeyCallback); - glfwSetCursorPosCallback(window, MouseCallback); + // glfwSetCursorPosCallback(window, MouseCallback); // עͣչʾЧ glfwSetScrollCallback(window, ScrollCallback); @@ -93,63 +100,63 @@ int main() Shader ourShader = Shader("res/shaders/core.vs", "res/shaders/core.fs"); + Shader lightShader = Shader("res/shaders/light.vs", "res/shaders/light.fs"); - // Week7-1 治ͬɫ + // 治ͬɫ --- 1 GLfloat vertices[] = - { // position // color - - // z = -0.5 ľ // ɫ (1, 0, 0) - -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, - - // z = 0.5 ľ // ɫ (0, 1, 0) - -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, + { // position // color // normal vector + + // z = -0.5 ľ // ɫ (1, 0, 0) + -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.1f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.1f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.1f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.1f, + -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.1f, + -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.1f, + + // z = 0.5 ľ // ɫ (0, 1, 0) + -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.1f, + 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.1f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,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f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f }; - - // VAOVBO Ĵ󶨡á + // VAOVBO Ĵ󶨡á --- 1 GLuint VAO, VBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); @@ -159,15 +166,25 @@ int main() glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(1); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat))); + glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + // 治ͬɫ --- 2 + // VAOVBO Ĵ󶨡á --- 2 + Light lightModel = Light(); + + //ùԴ + glm::vec3 lightPos = glm::vec3(0.0f, 1.5f, 0.0f); + + // ֡ͼ while (!glfwWindowShouldClose(window)) { @@ -175,20 +192,21 @@ int main() // Ӧ keyCallback glfwPollEvents(); - + // ʹƶ DoMovement(); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + + /* ԭ */ + ourShader.Use(); - glm::mat4 transform = glm::mat4(1.0f); - transform = glm::translate(transform, glm::vec3(0.0f, 0.0f, -2.0f)); - transform = glm::rotate(transform, glm::radians(20.0f) * static_cast(glfwGetTime()), glm::vec3(1.0f, 1.0f, 1.0f)); - transform = glm::scale(transform, glm::vec3(0.5f, 0.5f, 0.5f)); + // transform = glm::translate(transform, glm::vec3(0.0f, 0.0f, -2.0f)); + transform = glm::rotate(transform, glm::radians(20.0f), glm::vec3(1.0f, 1.0f, 1.0f)); + // transform = glm::scale(transform, glm::vec3(0.5f, 0.5f, 0.5f)); glUniformMatrix4fv(glGetUniformLocation(ourShader.Program, "transform"), 1, GL_FALSE, glm::value_ptr(transform)); glm::mat4 projection = glm::perspective(glm::radians(camera.GetZoom()), float(screenWidth) / float(screenHeight), 0.1f, 100.0f); @@ -197,11 +215,36 @@ int main() glm::mat4 view = camera.GetViewMatrix(); glUniformMatrix4fv(glGetUniformLocation(ourShader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view)); + glUniform3f(glGetUniformLocation(ourShader.Program, "LightPos"), lightPos.x, lightPos.y, lightPos.z); + glUniform3f(glGetUniformLocation(ourShader.Program, "ViewPos"), camera.GetPosition().x, camera.GetPosition().y, camera.GetPosition().z); + glUniform1f(glGetUniformLocation(ourShader.Program, "material.diffuse"), DIFFUSE); + glUniform1f(glGetUniformLocation(ourShader.Program, "material.specular"), SPECULAR); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); + + + /* Դ */ + + lightShader.Use(); + + glm::mat4 transformLight = glm::mat4(1.0f); + lightPos = glm::rotate(lightPos, glm::radians(0.05f), glm::vec3(1.0f, 1.0f, 1.0f)); + transformLight = glm::translate(transformLight, lightPos); + transformLight = glm::scale(transformLight, glm::vec3(0.1f, 0.1f, 0.1f)); + glUniformMatrix4fv(glGetUniformLocation(lightShader.Program, "transform"), 1, GL_FALSE, glm::value_ptr(transformLight)); + + glm::mat4 projectionLight = glm::perspective(glm::radians(camera.GetZoom()), float(screenWidth) / float(screenHeight), 0.1f, 100.0f); + glUniformMatrix4fv(glGetUniformLocation(lightShader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projectionLight)); + + glm::mat4 viewLight = camera.GetViewMatrix(); + glUniformMatrix4fv(glGetUniformLocation(lightShader.Program, "view"), 1, GL_FALSE, glm::value_ptr(viewLight)); + lightModel.Draw(); + + + // ˫ƣ glfwSwapBuffers(window); } diff --git a/Source/HelloCG/res/shaders/core.fs b/Source/HelloCG/res/shaders/core.fs index dec8dfc..3019629 100644 --- a/Source/HelloCG/res/shaders/core.fs +++ b/Source/HelloCG/res/shaders/core.fs @@ -5,11 +5,42 @@ out vec4 color; in vec3 ourColor; +in vec3 ourNormalVector; +in vec3 FragPos; + + +uniform vec3 LightPos; +uniform vec3 ViewPos; + + +struct Material +{ + float diffuse; + float specular; +}; + +uniform Material material; void main() { - color = vec4(ourColor, 1.0f); + vec3 lightDirection = normalize(LightPos - FragPos); + vec3 viewDirection = normalize(ViewPos - FragPos); + vec3 norm = normalize(ourNormalVector); + + // ambient + vec3 ambient = 0.2f * ourColor; + + // diffuse + float diff = material.diffuse * max(dot(norm, lightDirection), 0.0f); + vec3 diffuse = diff * ourColor; + + // specular 淴 + vec3 halfAngle = normalize(viewDirection + lightDirection); + float spec = material.specular * pow(max(dot(norm, halfAngle), 0.0f), 64.0f); + vec3 specular = spec * ourColor; + + color = vec4(ambient + diffuse + specular, 1.0f); } \ No newline at end of file diff --git a/Source/HelloCG/res/shaders/core.vs b/Source/HelloCG/res/shaders/core.vs index fe7a95c..80ffd52 100644 --- a/Source/HelloCG/res/shaders/core.vs +++ b/Source/HelloCG/res/shaders/core.vs @@ -3,9 +3,12 @@ layout(location = 0) in vec3 position; layout(location = 1) in vec3 vertexColor; +layout(location = 2) in vec3 normalVector; out vec3 ourColor; +out vec3 ourNormalVector; +out vec3 FragPos; uniform mat4 transform; @@ -18,5 +21,7 @@ void main() gl_Position = projection * view * transform * vec4(position, 1.0f); // ע˳ ourColor = vertexColor; + ourNormalVector = mat3(transpose(transform)) * normalVector; // ȡת + FragPos = vec3(transform * vec4(position, 1.0f)); // ϵ¹յĵ } \ No newline at end of file diff --git a/Source/HelloCG/res/shaders/light.fs b/Source/HelloCG/res/shaders/light.fs new file mode 100644 index 0000000..55fe53c --- /dev/null +++ b/Source/HelloCG/res/shaders/light.fs @@ -0,0 +1,12 @@ +#version 330 core + + +out vec4 color; + + +void main() +{ + + color = vec4(1.0f, 1.0f, 1.0f, 1.0f); + +} \ No newline at end of file diff --git a/Source/HelloCG/res/shaders/light.vs b/Source/HelloCG/res/shaders/light.vs new file mode 100644 index 0000000..a8af02c --- /dev/null +++ b/Source/HelloCG/res/shaders/light.vs @@ -0,0 +1,17 @@ +#version 330 core + + +layout(location = 0) in vec3 position; + + +uniform mat4 transform; +uniform mat4 projection; +uniform mat4 view; + + +void main() +{ + + gl_Position = projection * view * transform * vec4(position, 1.0f); + +} \ No newline at end of file