-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
174 lines (140 loc) · 4.31 KB
/
Copy pathshader.cpp
File metadata and controls
174 lines (140 loc) · 4.31 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "shader.h"
Shader::Shader()
{
m_shaderProg = 0;
}
Shader::~Shader()
{
for (std::vector<GLuint>::iterator it = m_shaderObjList.begin(); it != m_shaderObjList.end(); it++)
{
glDeleteShader(*it);
}
if (m_shaderProg != 0)
{
glDeleteProgram(m_shaderProg);
m_shaderProg = 0;
}
}
bool Shader::Initialize()
{
m_shaderProg = glCreateProgram();
if (m_shaderProg == 0)
{
std::cerr << "Error creating shader program\n";
return false;
}
return true;
}
// Use this method to add shaders to the program. When finished - call finalize()
bool Shader::AddShader(GLenum ShaderType)
{
std::string s;
if (ShaderType == GL_VERTEX_SHADER)
{
s = "#version 460\n \
\
layout (location = 0) in vec3 v_position; \
layout (location = 1) in vec3 v_color; \
\
smooth layout (location = 2) out vec3 color; \
\
uniform mat4 projectionMatrix; \
uniform mat4 viewMatrix; \
uniform mat4 modelMatrix; \
\
void main(void) \
{ \
vec4 v = vec4(v_position, 1.0); \
gl_Position = (projectionMatrix * viewMatrix * modelMatrix) * v; \
color = v_color; \
} \
";
}
else if (ShaderType == GL_FRAGMENT_SHADER)
{
s = "#version 460\n \
\
smooth layout (location = 2) in vec3 color; \
\
out vec4 frag_color; \
\
void main(void) \
{ \
frag_color = vec4(color.rgb, 1.0); \
} \
";
}
GLuint ShaderObj = glCreateShader(ShaderType);
if (ShaderObj == 0)
{
std::cerr << "Error creating shader type " << ShaderType << std::endl;
return false;
}
// Save the shader object - will be deleted in the destructor
m_shaderObjList.push_back(ShaderObj);
const GLchar* p[1];
p[0] = s.c_str();
GLint Lengths[1] = { (GLint)s.size() };
glShaderSource(ShaderObj, 1, p, Lengths);
glCompileShader(ShaderObj);
GLint success;
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
std::cerr << "Error compiling: " << InfoLog << std::endl;
return false;
}
glAttachShader(m_shaderProg, ShaderObj);
return true;
}
// After all the shaders have been added to the program call this function
// to link and validate the program.
bool Shader::Finalize()
{
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
glLinkProgram(m_shaderProg);
glGetProgramiv(m_shaderProg, GL_LINK_STATUS, &Success);
if (Success == 0)
{
glGetProgramInfoLog(m_shaderProg, sizeof(ErrorLog), NULL, ErrorLog);
std::cerr << "Error linking shader program: " << ErrorLog << std::endl;
return false;
}
glValidateProgram(m_shaderProg);
glGetProgramiv(m_shaderProg, GL_VALIDATE_STATUS, &Success);
if (!Success)
{
glGetProgramInfoLog(m_shaderProg, sizeof(ErrorLog), NULL, ErrorLog);
std::cerr << "Invalid shader program: " << ErrorLog << std::endl;
return false;
}
// Delete the intermediate shader objects that have been added to the program
for (std::vector<GLuint>::iterator it = m_shaderObjList.begin(); it != m_shaderObjList.end(); it++)
{
glDeleteShader(*it);
}
m_shaderObjList.clear();
return true;
}
void Shader::Enable()
{
glUseProgram(m_shaderProg);
}
GLint Shader::GetUniformLocation(const char* pUniformName)
{
GLuint Location = glGetUniformLocation(m_shaderProg, pUniformName);
if (Location == INVALID_UNIFORM_LOCATION) {
fprintf(stderr, "Warning! Unable to get the location of uniform '%s'\n", pUniformName);
}
return Location;
}
GLint Shader::GetAttribLocation(const char* pAttribName) {
GLuint Location = glGetAttribLocation(m_shaderProg, pAttribName);
if (Location == GL_INVALID_VALUE) {
fprintf(stderr, "Warning! Unable to get the location of uniform '%s'\n", pAttribName);
}
return Location;
}