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
#define SHADER_H
#include "glad/glad.h" // 包含glad来获取所有的必须OpenGL头文件
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
@ -33,13 +35,13 @@ public:
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
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
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// activate the shader
// ------------------------------------------------------------------------
void use()
void use() const
{
glUseProgram(ID);
}
@ -87,37 +90,60 @@ public:
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);
}
// ------------------------------------------------------------------------
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
{
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, value);
void setMat2(const std::string &name, const glm::mat2 &mat) const
{
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:
// 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;
char infoLog[1024];
GLint success;
GLchar infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
@ -139,3 +165,4 @@ private:
}
};
#endif

View file

@ -3,10 +3,10 @@
#include "stb_image.h"
#include <GLFW/glfw3.h>
#include <cmath>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
// 四边形顶点
float vertices[] = {
@ -40,7 +40,8 @@ int main() {
// 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) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
@ -147,7 +148,18 @@ int main() {
// 绑定纹理
shader.setInt("texture1", 0);
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)) {
@ -167,11 +179,11 @@ int main() {
// 绘制三角形
shader.use();
// 设置变换矩阵
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.5, 0.5));
trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));
shader.setMat4("trans",glm::value_ptr(trans));
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25,
0.0));
shader.setMat4("trans", trans);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

View file

@ -9,5 +9,5 @@ uniform sampler2D texture2;
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
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 trans;
void main()
{
gl_Position = trans * vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
void main() {
gl_Position = projection * view * model * trans * vec4(aPos, 1.0);
// gl_Position = trans * vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}