From 342d1d715a9197a6618c475d5fc5e8d9110434f7 Mon Sep 17 00:00:00 2001 From: jie Date: Mon, 16 Oct 2023 00:38:17 +0800 Subject: [PATCH] =?UTF-8?q?=20=E6=96=87=E4=BB=B6=E5=90=8D=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E5=8F=8A=E6=91=84=E5=83=8F=E6=9C=BA=E8=A7=86=E8=A7=92?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 8 +- include/matrix.h | 22 +++ include/shaderService.h | 10 +- include/texture.h | 2 +- main.cpp | 6 +- shaders/matrix.frag | 9 ++ shaders/matrix.vert | 12 ++ shaders/texture.vert | 5 +- src/matrix.cpp | 288 ++++++++++++++++++++++++++++++++++++++++ src/texture.cpp | 50 ++++--- 10 files changed, 382 insertions(+), 30 deletions(-) create mode 100644 include/matrix.h create mode 100644 shaders/matrix.frag create mode 100644 shaders/matrix.vert create mode 100644 src/matrix.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0be0a8a..e809e67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,10 +35,12 @@ ELSE(WIN32) #glad include_directories(${THIRD_LIB_DIR}/glad/glad3.3/include) set(GLAD_SRCS ${THIRD_LIB_DIR}/glad/glad3.3/src/glad.c - src/triangle.cpp + src/stb_image.cpp src/shaderService.cpp + src/triangle.cpp src/texture.cpp - src/stb_image.cpp) + src/matrix.cpp + ) #glfw include_directories(${THIRD_LIB_DIR}/glfw/glfw-3.3.8.bin.WIN64/glfw-3.3.8.bin.WIN64/include) link_directories(${THIRD_LIB_DIR}/glfw/glfw-3.3.8.bin.WIN64/glfw-3.3.8.bin.WIN64/lib-vc2022) @@ -51,6 +53,8 @@ ELSE(WIN32) ) include_directories(${PROJECT_SOURCE_DIR}/include) file(GLOB_RECURSE PROJECT_SRCS ${PROJECT_SOURCE_DIR}/src/*.cpp) + #glm + include_directories(${THIRD_LIB_DIR}/glm) #link add_executable(${PROJECT_N} main.cpp diff --git a/include/matrix.h b/include/matrix.h new file mode 100644 index 0000000..1793e01 --- /dev/null +++ b/include/matrix.h @@ -0,0 +1,22 @@ +// +// Created by jie on 2023/10/15. +// + +#ifndef LEARNOPENGL_MATRIX_H +#define LEARNOPENGL_MATRIX_H +#include "pch.h" +#include "shaderService.h" +namespace MATRIX { + + int Matrix(); + + void frameBufferSizeChange(GLFWwindow *window, int width, int height); + + void processInput(GLFWwindow *window); + void mouse_callback(GLFWwindow* window, double xpos, double ypos); + void scroll_callback(GLFWwindow* window, double xOffset, double yOffset); + void MatrixTest(); + +} // MATRIX + +#endif //LEARNOPENGL_MATRIX_H diff --git a/include/shaderService.h b/include/shaderService.h index 59429ff..95f418e 100644 --- a/include/shaderService.h +++ b/include/shaderService.h @@ -8,6 +8,7 @@ #include "glad/glad.h" #include #include +#include class ShaderService { private: @@ -16,14 +17,14 @@ public: ShaderService(const std::filesystem::path &vertexShaderPath, const std::filesystem::path &fragShaderPath); bool CheckShader(unsigned int shaderIndex, bool isProgram = false); void Use(); - + inline unsigned int GetId(){return this->programId;} template - requires std::is_same_v || std::is_same_v || std::is_same_v + requires std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v void SetUniform(std::string_view name, T value); }; template -requires std::is_same_v || std::is_same_v || std::is_same_v +requires std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v void ShaderService::SetUniform(std::string_view name, T value) { if constexpr (std::is_same_v) { glUniform1i(glGetUniformLocation(programId, name.data()), (int) value); @@ -31,6 +32,9 @@ void ShaderService::SetUniform(std::string_view name, T value) { glUniform1i(glGetUniformLocation(programId, name.data()), value); } else if constexpr (std::is_same_v) { glUniform1f(glGetUniformLocation(programId, name.data()), value); + }else if constexpr (std::is_same_v){ + const auto index = glGetUniformLocation(this->programId, name.data()); + glUniformMatrix4fv(index, 1, GL_FALSE, &value[0][0]); } } diff --git a/include/texture.h b/include/texture.h index 01ea751..a4d21a8 100644 --- a/include/texture.h +++ b/include/texture.h @@ -14,7 +14,7 @@ namespace TEXTURE { void processInput(GLFWwindow *window); - void CheckShader(unsigned int shaderIndex, bool isPorgram = false); + void MatrixTest(); } diff --git a/main.cpp b/main.cpp index 214b687..d3f908a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,11 @@ #include #include "texture.h" #include "triangle.h" +#include "matrix.h" int main(int argc, char **const argv) { // TRIANGLE::Practice1(); - auto res = TEXTURE::Texture(); - return res ; +// auto res = TEXTURE::Texture(); + MATRIX::Matrix(); + return 0 ; } \ No newline at end of file diff --git a/shaders/matrix.frag b/shaders/matrix.frag new file mode 100644 index 0000000..39eb42a --- /dev/null +++ b/shaders/matrix.frag @@ -0,0 +1,9 @@ +#version 330 core +in vec2 ourTexCoord; +out vec4 fragColor; +uniform sampler2D ourTexture1; +uniform sampler2D ourTexture2; +uniform float mixCount; +void main() { + fragColor = mix(texture(ourTexture1, ourTexCoord), texture(ourTexture2, vec2(-ourTexCoord.x, ourTexCoord.y)), mixCount); +} \ No newline at end of file diff --git a/shaders/matrix.vert b/shaders/matrix.vert new file mode 100644 index 0000000..80ef1a5 --- /dev/null +++ b/shaders/matrix.vert @@ -0,0 +1,12 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; +out vec3 ourColor; +out vec2 ourTexCoord; +void main() { + gl_Position = projection * view * model * vec4(aPos, 1.0f); + ourTexCoord = aTexCoord; +} \ No newline at end of file diff --git a/shaders/texture.vert b/shaders/texture.vert index 11abedd..c669ec7 100644 --- a/shaders/texture.vert +++ b/shaders/texture.vert @@ -2,10 +2,13 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; layout (location = 2) in vec2 aTexCoord; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; out vec3 ourColor; out vec2 ourTexCoord; void main() { - gl_Position=vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0f); ourColor = aColor; ourTexCoord = aTexCoord; } \ No newline at end of file diff --git a/src/matrix.cpp b/src/matrix.cpp new file mode 100644 index 0000000..a2a2e3f --- /dev/null +++ b/src/matrix.cpp @@ -0,0 +1,288 @@ +// +// Created by jie on 2023/10/11. +// + +#include "matrix.h" +#include "iostream" +#include "stb_image.h" +#include "opencv2/opencv.hpp" +#include "glm/glm.hpp" +#include "glm/gtc/matrix_transform.hpp" +#include "glm/gtc/type_ptr.hpp" + +constexpr int width = 800; +constexpr int height = 600; + +using namespace std::literals::chrono_literals; +namespace MATRIX { + + + float mixValue = 0.5f; + glm::vec3 cameraPos = glm::vec3{0.f, 0.f, 3.f}; + glm::vec3 cameraFront = glm::vec3{0.f, 0.f, -1.f}; + glm::vec3 cameraUp = glm::vec3{0.f, 1.f, 0.f}; + float deltaTime = 0.f; + float lastTime = 0.f; + bool firstRender = true; + float lastX = width / 2; + float lastY = height / 2; + float yaw = -90.f; + float pitch = 0.f; + float fov = 45.f; + + void frameBufferSizeChange(GLFWwindow *window, int width, int height) { + glViewport(0, 0, width, height); + } + + void processInput(GLFWwindow *window) { + float cameraSpeed = 2.5f * deltaTime; + + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { + glfwSetWindowShouldClose(window, true); + } + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { + cameraPos += cameraSpeed * cameraFront; + } + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { + cameraPos -= cameraSpeed * cameraFront; + } + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { + cameraPos += glm::normalize(glm::cross(cameraUp, cameraFront)) * cameraSpeed; + } + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { + cameraPos -= glm::normalize(glm::cross(cameraUp, cameraFront)) * cameraSpeed; + } + if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS){ + cameraPos += cameraUp * cameraSpeed; + } + if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS){ + cameraPos -= cameraUp * cameraSpeed; + } + } + + void mouse_callback(GLFWwindow *window, double xposIn, double yposIn) { + float xpos = static_cast(xposIn); + float ypos = static_cast(yposIn); + if (firstRender) { + lastX = xpos; + lastY = ypos; + firstRender = false; + } + float xOffset = xpos - lastX; + float yOffset = lastY - ypos; + lastX = xpos; + lastY = ypos; + float sensitivity = 0.1f; // change this value to your liking + xOffset *= sensitivity; + yOffset *= sensitivity; + yaw += xOffset; + pitch += yOffset; + if (pitch > 89.f) { + pitch = 89.f; + } + if (pitch < -89.f) { + pitch = -89.f; + } + + glm::vec3 front; + front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + front.y = sin(glm::radians(pitch)); + front.z = sin(glm::radians(yaw) * cos(glm::radians(pitch))); + cameraFront = glm::normalize(front); + } + + void scroll_callback(GLFWwindow *window, double xOffset, double yOffset) { + if (fov >= 1.f && fov <= 45.f) { + fov -= yOffset; + } + if (fov <= 1.f) { + fov = 1.f; + } + if (fov >= 45.f) { + fov = 45.f; + } + } + + int Matrix() { + 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, "Windows", nullptr, nullptr); + if (window == nullptr) { + return -1; + } + glfwMakeContextCurrent(window); + glfwSetWindowSizeCallback(window, frameBufferSizeChange); + glfwSetCursorPosCallback(window, mouse_callback); + glfwSetScrollCallback(window, scroll_callback); + if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { + auto error = glad_glGetError(); + return static_cast(error); + } + 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,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+// float vertices[] = { +// 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, +// 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, +// -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, +// -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, +// }; + + unsigned int VAO, VBO; + 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(float), (void *) 0); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) (3 * sizeof(float))); + glEnableVertexAttribArray(1); + glBindVertexArray(0); + auto wall = cv::imread("../../resources/container.jpg"); + auto awesomeFace = cv::imread("../../resources/awesomeface.png"); + cv::flip(awesomeFace, awesomeFace, 0); + unsigned int texture[2]; + glGenTextures(2, texture); + glBindTexture(GL_TEXTURE_2D, texture[0]); + 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); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, wall.cols, wall.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, wall.data); + glGenerateMipmap(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, texture[1]); + 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); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, awesomeFace.cols, awesomeFace.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, + awesomeFace.data); + glGenerateMipmap(GL_TEXTURE_2D); + + auto shader = ShaderService("../../shaders/matrix.vert", + "../../shaders/matrix.frag"); + + shader.Use(); + shader.SetUniform("ourTexture1", 0); + shader.SetUniform("ourTexture2", 1); + + glm::vec3 cubePositions[] = { + glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(2.0f, 5.0f, -15.0f), + glm::vec3(-1.5f, -2.2f, -2.5f), + glm::vec3(-3.8f, -2.0f, -12.3f), + glm::vec3(2.4f, -0.4f, -3.5f), + glm::vec3(-1.7f, 3.0f, -7.5f), + glm::vec3(1.3f, -2.0f, -2.5f), + glm::vec3(1.5f, 2.0f, -2.5f), + glm::vec3(1.5f, 0.2f, -1.5f), + glm::vec3(-1.3f, 1.0f, -1.5f) + }; + + glEnable(GL_DEPTH_TEST); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + while (!glfwWindowShouldClose(window)) { + processInput(window); + float currentFrame = glfwGetTime(); + deltaTime = currentFrame - lastTime; + lastTime = currentFrame; + glClearColor(.2f, .3f, .3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture[0]); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texture[1]); + + shader.Use(); + + glm::mat4 model = glm::mat4{1.f}; + glm::mat4 projection = glm::mat4{1.f}; + projection = glm::perspective(glm::radians(fov), static_cast(width) / height, .1f, 100.f); + std::cout< 0) mixValue -= 0.01f; - } - } - - void CheckShader(unsigned int shaderIndex, bool isPorgram) { - int successful; - char logInfo[512]; - if (isPorgram) { - glad_glGetProgramiv(shaderIndex, GL_LINK_STATUS, &successful); - } else { - glad_glGetShaderiv(shaderIndex, GL_COMPILE_STATUS, &successful); - } - if (!successful) { - if (isPorgram) { - glad_glGetProgramInfoLog(shaderIndex, 512, nullptr, logInfo); - } else { - glad_glGetShaderInfoLog(shaderIndex, 512, nullptr, logInfo); - } - std::cout << logInfo << std::endl; + if (mixValue > 0) mixValue -= 0.01f; } } @@ -52,7 +40,7 @@ namespace TEXTURE { glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow *window = glfwCreateWindow(800, 600, "Windows", nullptr, nullptr); + GLFWwindow *window = glfwCreateWindow(width, height, "Windows", nullptr, nullptr); if (window == nullptr) { return -1; } @@ -122,16 +110,27 @@ namespace TEXTURE { shader.Use(); shader.SetUniform("ourTexture1", 0); shader.SetUniform("ourTexture2", 1); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); while (!glfwWindowShouldClose(window)) { processInput(window); glClearColor(.2f, .3f, .3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); + glm::mat4 view = glm::mat4{1.f}; + glm::mat4 model = glm::mat4{1.f}; + glm::mat4 projection = glm::mat4{1.f}; + model = glm::rotate(model, glm::radians(-55.0f), {1.f, 0.f, 0.f}); + view = glm::translate(view, {0.f, 0.f, -3.f}); + projection = glm::perspective(glm::radians(45.f), static_cast(width) / height, .1f, 100.f); shader.Use(); // std::this_thread::sleep_for(5ms); shader.SetUniform("mixCount", mixValue); + shader.SetUniform("model", model); + shader.SetUniform("view", view); + shader.SetUniform("projection", projection); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture[0]); glActiveTexture(GL_TEXTURE1); @@ -152,4 +151,13 @@ namespace TEXTURE { glfwTerminate(); return 0; } + + + void MatrixTest() { + glm::vec4 vec(1.f, 0.f, 0.f, 1.f); + glm::mat4 trans = glm::mat4(1.0f); + trans = glm::translate(trans, glm::vec3{1.f, 1.f, 0.f}); + vec = trans * vec; + std::cout << vec.x << vec.y << vec.z << std::endl; + } } \ No newline at end of file