-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShader.hpp
96 lines (77 loc) · 3.03 KB
/
Shader.hpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Created by simrat39 on 12/28/23.
//
#pragma once
#include <fstream>
#include <sstream>
#include <iostream>
#include <unordered_map>
#include <epoxy/gl.h>
class Shader {
public:
GLuint ID;
std::unordered_map<std::string, GLuint> uniforms;
Shader(const char* vertex_shader_src_file, const char* fragment_shader_src_file) {
std::ifstream vertex_fstream{vertex_shader_src_file};
std::stringstream vertex_sstream;
vertex_sstream << vertex_fstream.rdbuf();
vertex_fstream.close();
std::ifstream fragment_fstream{fragment_shader_src_file};
std::stringstream fragment_sstream;
fragment_sstream << fragment_fstream.rdbuf();
fragment_fstream.close();
std::string vertex_src_str = vertex_sstream.str();
std::string fragment_src_str = fragment_sstream.str();
const char* vertex_src = vertex_src_str.c_str();
const char* fragment_src = fragment_src_str.c_str();
int success;
char infoLog[512];
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertex_src, nullptr);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "Error: vertex shader couldn;t compile: " << infoLog << std::endl;
}
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragment_src, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
std::cout << "Error: fragment shader couldn;t compile: " << infoLog << std::endl;
}
ID = glCreateProgram();
glAttachShader(ID, vertexShader);
glAttachShader(ID, fragmentShader);
glLinkProgram(ID);
// check for linking errors
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(ID, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
// Deleting cuz we linked to the shader program already
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void use() {
glUseProgram(ID);
}
void register_uniform(std::string key) {
uniforms[key] = glGetUniformLocation(ID, key.c_str());
}
void set_float_array_uniform(std::string key, GLint count, float value[]) {
GLuint location = uniforms.at(key);
glUniform1fv(location, count, value);
}
void set_uint_uniform(std::string key, GLuint value) {
GLuint location = uniforms.at(key);
glUniform1ui(location, value);
}
void set_float_uniform(std::string key, float value) {
GLuint location = uniforms.at(key);
glUniform1f(location, value);
}
};