feat(OpenGL-hello): projection

This commit is contained in:
iridiumR 2023-05-24 16:39:49 +08:00
parent 7a570f83d2
commit 56ca8c2766
4 changed files with 77 additions and 35 deletions

View File

@ -1,14 +1,16 @@
#ifndef SHADER_H #ifndef SHADER_H
#define SHADER_H #define SHADER_H
#include "glad/glad.h" // 包含glad来获取所有的必须OpenGL头文件 #include <glad/glad.h>
#include <glm/glm.hpp>
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
class Shader class Shader
{ {
public: public:
@ -33,13 +35,13 @@ public:
std::stringstream vShaderStream, fShaderStream; std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams // read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf(); vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf(); fShaderStream << fShaderFile.rdbuf();
// close file handlers // close file handlers
vShaderFile.close(); vShaderFile.close();
fShaderFile.close(); fShaderFile.close();
// convert stream into string // convert stream into string
vertexCode = vShaderStream.str(); vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str(); fragmentCode = fShaderStream.str();
} }
catch (std::ifstream::failure& e) catch (std::ifstream::failure& e)
{ {
@ -68,10 +70,11 @@ public:
// delete the shaders as they're linked into our program now and no longer necessary // delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
} }
// activate the shader // activate the shader
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void use() void use() const
{ {
glUseProgram(ID); glUseProgram(ID);
} }
@ -87,37 +90,60 @@ public:
glUniform1i(glGetUniformLocation(ID, name.c_str()), value); glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setFloat1(const std::string &name, float value) const void setFloat(const std::string &name, float value) const
{ {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value); glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setFloat2(const std::string &name, float value1, float value2) const void setVec2(const std::string &name, const glm::vec2 &value) const
{ {
glUniform2f(glGetUniformLocation(ID, name.c_str()), value1, value2); glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec2(const std::string &name, float x, float y) const
{
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setFloat3(const std::string &name, float value1, float value2, float value3) const void setVec3(const std::string &name, const glm::vec3 &value) const
{ {
glUniform3f(glGetUniformLocation(ID, name.c_str()), value1, value2, value3); glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec3(const std::string &name, float x, float y, float z) const
{
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setFloat4(const std::string &name, float value1, float value2, float value3, float value4) const void setVec4(const std::string &name, const glm::vec4 &value) const
{ {
glUniform4f(glGetUniformLocation(ID, name.c_str()), value1, value2, value3, value4); glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec4(const std::string &name, float x, float y, float z, float w) const
{
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setMat4(const std::string &name, float* value) const void setMat2(const std::string &name, const glm::mat2 &mat) const
{ {
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, value); glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
} }
// ------------------------------------------------------------------------
void setMat3(const std::string &name, const glm::mat3 &mat) const
{
glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat4(const std::string &name, const glm::mat4 &mat) const
{
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
private: private:
// utility function for checking shader compilation/linking errors. // utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void checkCompileErrors(unsigned int shader, std::string type) void checkCompileErrors(GLuint shader, std::string type)
{ {
int success; GLint success;
char infoLog[1024]; GLchar infoLog[1024];
if (type != "PROGRAM") if (type != "PROGRAM")
{ {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success); glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
@ -139,3 +165,4 @@ private:
} }
}; };
#endif #endif

View File

@ -3,10 +3,10 @@
#include "stb_image.h" #include "stb_image.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <cmath> #include <cmath>
#include <iostream>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <iostream>
// 四边形顶点 // 四边形顶点
float vertices[] = { float vertices[] = {
@ -40,7 +40,8 @@ int main() {
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// 创建窗口 // 创建窗口
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); GLFWwindow *window =
glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL) { if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl; std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
@ -147,7 +148,18 @@ int main() {
// 绑定纹理 // 绑定纹理
shader.setInt("texture1", 0); shader.setInt("texture1", 0);
shader.setInt("texture2", 1); shader.setInt("texture2", 1);
// 设置变换矩阵
// create transformations
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(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
projection = glm::perspective(
glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
// 循环渲染 // 循环渲染
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
@ -167,11 +179,11 @@ int main() {
// 绘制三角形 // 绘制三角形
shader.use(); shader.use();
// 设置变换矩阵
glm::mat4 trans(1.0f); glm::mat4 trans(1.0f);
trans = glm::translate(trans,glm::vec3(0.5,-0.5,0.0)); trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25,
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.5, 0.5)); 0.0));
trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5)); shader.setMat4("trans", trans);
shader.setMat4("trans",glm::value_ptr(trans));
glBindVertexArray(VAO); glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

View File

@ -9,5 +9,5 @@ uniform sampler2D texture2;
void main() void main()
{ {
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2) * vec4(ourColor, 1.0);
} }

View File

@ -1,16 +1,19 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout(location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout(location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord; layout(location = 2) in vec2 aTexCoord;
out vec3 ourColor; out vec3 ourColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 trans; uniform mat4 trans;
void main() void main() {
{ gl_Position = projection * view * model * trans * vec4(aPos, 1.0);
gl_Position = trans * vec4(aPos, 1.0); // gl_Position = trans * vec4(aPos, 1.0);
ourColor = aColor; ourColor = aColor;
TexCoord = aTexCoord; TexCoord = aTexCoord;
} }