-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
115 lines (101 loc) · 3.43 KB
/
shader.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
//
// Created by username on 11/09/2021.
//
#include <glad/glad.h>
#include "shader.h"
static void checkShaderCompileSuccess(GLuint shaderId) {
int success;
char infoLog[512];
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shaderId, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX_OR_FRAG::COMPILATION_FAILED\n" << infoLog << std::endl;
}
}
static void checkProgramCompileSuccess(GLuint programId) {
int success;
char infoLog[512];
glGetProgramiv(programId, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(programId, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
}
Shader::Shader(const char *vertexPath, const char *fragmentPath) {
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch(std::ifstream::failure &e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
}
// vertex Shader
GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
{
auto vShaderPtr = vertexCode.c_str();
auto vShaderLen = static_cast<int>(vertexCode.length());
glShaderSource(vertexShaderId, 1, &vShaderPtr, &vShaderLen);
glCompileShader(vertexShaderId);
checkShaderCompileSuccess(vertexShaderId);
}
GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
{
auto fShaderPtr = fragmentCode.c_str();
auto fShaderLen = static_cast<int>(fragmentCode.length());
glShaderSource(fragmentShaderId, 1, &fShaderPtr, &fShaderLen);
glCompileShader(fragmentShaderId);
checkShaderCompileSuccess(fragmentShaderId);
}
ID = glCreateProgram();
glAttachShader(ID, vertexShaderId);
glAttachShader(ID, fragmentShaderId);
glLinkProgram(ID);
checkProgramCompileSuccess(ID);
glDeleteShader(vertexShaderId);
glDeleteShader(fragmentShaderId);
}
void Shader::use() {
glUseProgram(ID);
}
Shader::~Shader() {
glDeleteProgram(ID);
}
void Shader::setBool(const std::string &name, bool value) {
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
}
void Shader::setInt(const std::string &name, int value) {
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value) {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setVec3(const std::string &name, const glm::vec3 &value) {
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void Shader::setMat4(const std::string &name, const glm::mat4 &mat)
{
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}