-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
89 lines (71 loc) · 2.27 KB
/
Copy pathShader.cpp
File metadata and controls
89 lines (71 loc) · 2.27 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
81
82
83
84
85
86
87
88
89
#include "Shader.h"
#include <iostream>
enum ShaderType { //work on this another time
Vertex,
Fragment
};
Shader::Shader(const std::string &vertexPath, const std::string &fragmentPath) :
m_program(NULL)
{
std::ifstream vertexFile(vertexPath);
std::ifstream fragmentFile(fragmentPath);
if (!vertexFile)
{
std::cout << "Invalid vertex file path" << std::endl;
}
if (!fragmentFile)
{
std::cout << "Invalid fragment file path" << std::endl;
}
std::stringstream vertexStream;
std::stringstream fragmentStream;
vertexStream << vertexFile.rdbuf();
fragmentStream << fragmentFile.rdbuf();
std::string vertexSrcCode = vertexStream.str();
std::string fragmentSrcCode = fragmentStream.str();
const char* vertexSrc = vertexSrcCode.c_str();
const char* fragmentSrc = fragmentSrcCode.c_str();
unsigned int vShader = glCreateShader(GL_VERTEX_SHADER);
unsigned int fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, &vertexSrc, NULL);
glShaderSource(fShader, 1, &fragmentSrc, NULL);
int vSuccess;
glCompileShader(vShader);
glGetShaderiv(vShader, GL_COMPILE_STATUS, &vSuccess);
if (!vSuccess)
{
std::cout << "Vertex shader error compilation" << std::endl;
}
int fSuccess;
glCompileShader(fShader);
glGetShaderiv(fShader, GL_COMPILE_STATUS, &fSuccess);
if (!fSuccess)
{
std::cout << "Fragment shader error compilation" << std::endl;
}
m_program = glCreateProgram();
glAttachShader(m_program, vShader);
glAttachShader(m_program, fShader);
glLinkProgram(m_program);
glDeleteShader(vShader);
glDeleteShader(fShader);
}
Shader::~Shader()
{
glDeleteProgram(m_program);
}
void Shader::use() const {
glUseProgram(m_program);
}
void Shader::setMat4(const std::string &name, const glm::mat4 &data) const {
glUniformMatrix4fv(glGetUniformLocation(m_program, name.c_str()), 1, GL_FALSE, glm::value_ptr(data));
}
void Shader::setVec3(const std::string& name, const glm::vec3& data) const {
glUniform3fv(glGetUniformLocation(m_program, name.c_str()), 1, glm::value_ptr(data));
}
void Shader::setFloat(const std::string& name, const float value) const {
glUniform1f(glGetUniformLocation(m_program, name.c_str()), value);
}
void Shader::setInt(const std::string& name, const int value) const {
glUniform1i(glGetUniformLocation(m_program, name.c_str()), value);
}