文件名调整及摄像机视角完成

This commit is contained in:
jie 2023-10-16 00:38:17 +08:00
parent e439893fd1
commit 342d1d715a
10 changed files with 382 additions and 30 deletions

View File

@ -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

22
include/matrix.h Normal file
View File

@ -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

View File

@ -8,6 +8,7 @@
#include "glad/glad.h"
#include <filesystem>
#include <type_traits>
#include <glm/glm.hpp>
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<typename T>
requires std::is_same_v<T, bool> || std::is_same_v<T, int> || std::is_same_v<T, float>
requires std::is_same_v<T, bool> || std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, glm::mat4>
void SetUniform(std::string_view name, T value);
};
template<typename T>
requires std::is_same_v<T, bool> || std::is_same_v<T, int> || std::is_same_v<T, float>
requires std::is_same_v<T, bool> || std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, glm::mat4>
void ShaderService::SetUniform(std::string_view name, T value) {
if constexpr (std::is_same_v<T, bool>) {
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<T, float>) {
glUniform1f(glGetUniformLocation(programId, name.data()), value);
}else if constexpr (std::is_same_v<T, glm::mat4>){
const auto index = glGetUniformLocation(this->programId, name.data());
glUniformMatrix4fv(index, 1, GL_FALSE, &value[0][0]);
}
}

View File

@ -14,7 +14,7 @@ namespace TEXTURE {
void processInput(GLFWwindow *window);
void CheckShader(unsigned int shaderIndex, bool isPorgram = false);
void MatrixTest();
}

View File

@ -1,9 +1,11 @@
#include <iostream>
#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 ;
}

9
shaders/matrix.frag Normal file
View File

@ -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);
}

12
shaders/matrix.vert Normal file
View File

@ -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;
}

View File

@ -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;
}

288
src/matrix.cpp Normal file
View File

@ -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<float>(xposIn);
float ypos = static_cast<float>(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<int>(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, 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
};
// 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<int>("ourTexture1", 0);
shader.SetUniform<int>("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<float>(width) / height, .1f, 100.f);
std::cout<<std::format("x: {}, y: {}, z: {}", cameraFront.x, cameraFront.y, cameraFront.z)<<std::endl;
auto view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
shader.SetUniform("mixCount", mixValue);
shader.SetUniform("model", model);
shader.SetUniform("view", view);
shader.SetUniform("projection", projection);
for (unsigned int i = 0; i < 10; i++) {
model = glm::mat4{1.f};
model = glm::translate(model, cubePositions[i]);
float angle = 20.f * i;
model = glm::rotate(model, ((float) glfwGetTime() * i) * glm::radians(50.f), {1.0f, 0.3f, 0.5f});
shader.SetUniform("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
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;
}
}

View File

@ -6,6 +6,12 @@
#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;
#define STB_IMAGE_IMPLEMENTATION
using namespace std::literals::chrono_literals;
@ -21,28 +27,10 @@ namespace TEXTURE {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
if(mixValue < 1.0) mixValue += 0.01f;
if (mixValue < 1.0) mixValue += 0.01f;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
if(mixValue > 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<int>("ourTexture1", 0);
shader.SetUniform<int>("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<float>(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;
}
}