-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
80 lines (64 loc) · 2.37 KB
/
Copy pathShader.cpp
File metadata and controls
80 lines (64 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Created by ju on 11/21/25.
//
#include "Shader.hpp"
#include "platform/Platform.hpp"
#include <fstream>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <sstream>
std::string readText(const std::filesystem::path &filePath) {
std::ifstream sourceFile(filePath);
if (!sourceFile.is_open()) {
std::cerr << "Error opening file: " << filePath << std::endl;
}
std::stringstream buffer;
buffer << sourceFile.rdbuf();
return buffer.str();
}
void handle_shader_compilation_error(const GLuint shaderID) {
GLint success = 0;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success);
if (!success) {
GLint logLength = 0;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
std::string infoLog;
infoLog.resize(logLength);
glGetShaderInfoLog(shaderID, logLength, nullptr, infoLog.data());
glDeleteShader(shaderID);
throw std::runtime_error(
"Shader compile error:\n" + infoLog
);
}
}
GLuint loadAndCompileShader(GLuint shaderType, const std::filesystem::path &shaderPath) {
auto shaderSource = readText(shaderPath);
auto source = shaderSource.c_str();
auto shaderID = glCreateShader(shaderType);
glShaderSource(shaderID, 1, &source, nullptr);
glCompileShader(shaderID);
handle_shader_compilation_error(shaderID);
return shaderID;
}
void Shader::use() {
glUseProgram(shader_program);
}
Shader::Shader(const std::filesystem::path &full_vertex_path, const std::filesystem::path &full_fragment_path) {
GLuint vertexShader = loadAndCompileShader(GL_VERTEX_SHADER, full_vertex_path);
GLuint fragmentShader =loadAndCompileShader(GL_FRAGMENT_SHADER, full_fragment_path);
shader_program = glCreateProgram();
glAttachShader(shader_program, vertexShader);
glAttachShader(shader_program, fragmentShader);
glLinkProgram(shader_program);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
Shader::~Shader() {
glDeleteProgram(shader_program);
}
void Shader::pass_uniform(const std::string &uniform_name, int value) {
glUniform1i(glGetUniformLocation(shader_program, uniform_name.c_str()), value);
}
void Shader::pass_uniform(const std::string &uniform_name, const glm::mat4& value) {
glUniformMatrix4fv(glGetUniformLocation(shader_program, uniform_name.c_str()), 1, GL_FALSE,glm::value_ptr(value));
}