-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
126 lines (110 loc) · 3.33 KB
/
Copy pathShader.cpp
File metadata and controls
126 lines (110 loc) · 3.33 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
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
116
117
118
119
120
121
122
123
124
125
126
#include "Shader.h"
#include <iostream>
Shader& Shader::Use()
{
glUseProgram(this->ID);
return *this;
}
void Shader::Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource) {
unsigned int sVertex, sFragment, gShader;
/*Vertex Shaders*/
sVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sVertex, 1, &vertexSource, NULL);
glCompileShader(sVertex);
//for debugging purposes we can check for comiple errors here.
//checkCompileError(sVertex, "VERTEX")
/*Fragment Shaders*/
sFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(sFragment, 1, &fragmentSource, NULL);
glCompileShader(sVertex);
//for debugging purposes we can check for comiple errors here.
//checkCompileError(sFragment, "FRAGMENT")
/*Geometry Source Shader*/
if (geometrySource != nullptr)
{
gShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(gShader, 1, &geometrySource, NULL);
glCompileShader(gShader);
//for debugging purposes we can check for comiple errors here.
//checkCompileError(gShader, "GEOMETRY")
}
/*Shader Program*/
this->ID = glCreateProgram();
glAttachShader(this->ID, sVertex);
glAttachShader(this->ID, sFragment);
if (geometrySource != nullptr)
{
glAttachShader(this->ID, gShader);
}
glLinkProgram(this->ID);
//for debugging purposes we can check for comiple errors here.
//checkCompileErrors(this->ID, "PROGRAM");
/*Clean up*/
//Delete Shader because they're linked to program now and no longer needed.
glDeleteShader(sVertex);
glDeleteShader(sFragment);
if (geometrySource != nullptr)
{
glDeleteShader(gShader);
}
}
void Shader::SetFloat(const char* name, float value, bool useShader) {
if (useShader)
{
this->Use();
}
glUniform1f(glGetUniformLocation(this->ID, name), value);
}
void Shader::SetInteger(const char* name, int value, bool useShader) {
if (useShader)
{
this->Use();
}
glUniform1f(glGetUniformLocation(this->ID, name), value);
}
void Shader::SetVector2f(const char* name, float x, float y, bool useShader)
{
if (useShader)
this->Use();
glUniform2f(glGetUniformLocation(this->ID, name), x, y);
}
void Shader::SetVector2f(const char* name, const glm::vec2& value, bool useShader)
{
if (useShader)
this->Use();
glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y);
}
void Shader::SetVector3f(const char* name, float x, float y, float z, bool useShader)
{
if (useShader)
this->Use();
glUniform3f(glGetUniformLocation(this->ID, name), x, y, z);
}
void Shader::SetVector3f(const char* name, const glm::vec3& value, bool useShader)
{
if (useShader)
this->Use();
glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z);
}
void Shader::SetVector4f(const char* name, float x, float y, float z, float w, bool useShader)
{
if (useShader)
this->Use();
glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w);
}
void Shader::SetVector4f(const char* name, const glm::vec4& value, bool useShader)
{
if (useShader)
this->Use();
glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, value.w);
}
void Shader::SetMatrix4(const char* name, const glm::mat4& matrix, bool useShader)
{
if (useShader)
this->Use();
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, false, glm::value_ptr(matrix));
}
void Shader::checkCompileErrors(unsigned int object, std::string type)
{
//TODO::fill this out
}