-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
472 lines (388 loc) · 17 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "Shader.h"
//Constructor
Shader::Shader()
:ShaderId{ 0 },
UniformModel{ 0 },
UniformProjection{ 0 },
UniformView{0},
// uniformDLight{0,0,0,0},
PointLightCount{0},
UniformSpecularIntensity{0},
UniformCameraPosition{0},
dLight{nullptr},
pLight{nullptr},
sLight{ nullptr },
//uniformPLight{0,0,0,0,0,0,0,0},
UniformSpecularShininess{0}{}
//Create shaders from text directly
void Shader::CreateShadersFromText(const char* vShaderCode, const char* fShaderCode)
{
CompileShaders(vShaderCode, fShaderCode);
}
void Shader::CreateShadersFromFiles(const char* vShaderPath, const char* fShaderPath)
{
//Read the strings from files which contain the shaders
std::string VertexCodeCpp = ReadFile(vShaderPath);
std::string FragmentCodeCpp = ReadFile(fShaderPath);
//Convert the Cpp strigs to C strings
const char* vShaderCode = VertexCodeCpp.c_str();
const char* fShadercode = FragmentCodeCpp.c_str();
//Compile shaders
CompileShaders(vShaderCode, fShadercode);
}
void Shader::CreateShadersFromFiles(const char* vShaderPath, const char* gShaderPath, const char* fShaderPath)
{
//Read the strings from files which contain the shaders
std::string VertexCodeCpp = ReadFile(vShaderPath);
std::string GeometryCodeCpp = ReadFile(gShaderPath);
std::string FragmentCodeCpp = ReadFile(fShaderPath);
//Convert the Cpp strigs to C strings
const char* vShaderCode = VertexCodeCpp.c_str();
const char* gShaderCode = GeometryCodeCpp.c_str();
const char* fShadercode = FragmentCodeCpp.c_str();
//Compile shaders
CompileShaders(vShaderCode,gShaderCode, fShadercode);
}
//Reads the shader files and returns cpp string
std::string Shader::ReadFile(const char* FilePath)
{
//Empty string to store the shader code in
std::string content = "";
std::ifstream FileStream(FilePath, std::ios::in);
//Incase path is wrong or file doesnt exist return empty stirng
if (!FileStream.is_open())
{
std::cout << FilePath<<" not found!!\n";
return "";
}
//String to read the file
std::string Line = "";
//While end of file is not reached
while (!FileStream.eof())
{
///Get each line of code from the shader file
std::getline(FileStream, Line);
//Add newline to the end of each line and append it to content
content.append(Line + "\n");
}
return content;
}
//Adds the shaders to the program
GLuint Shader::AddShader(GLuint TheProgram, const char* ShaderCode, GLenum ShaderType) {
GLuint TheShader = glCreateShader(ShaderType);
const GLchar* TheCode[1];
TheCode[0] = ShaderCode;
GLint CodeLength[1];
CodeLength[0] = strlen(ShaderCode);
glShaderSource(TheShader, 1, TheCode, CodeLength);
glCompileShader(TheShader);
GLint result = 0;
GLchar eLog[1024] = { 0 };
glGetShaderiv(TheShader, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
glGetShaderInfoLog(TheShader, sizeof(eLog), NULL, eLog);
std::cout << "Error compiling shader: " << eLog << std::endl;
return NULL;
}
glAttachShader(TheProgram, TheShader);
return TheShader;
}
//Compiles the shaders, checks for errors and gives uniform variable ID
void Shader::CompileShaders(const char* vShaderCode, const char* fShaderCode)
{
ShaderId = glCreateProgram();
if (!ShaderId) {
std::cout << "Program was not sucessfully created!!!\n";
exit(1);
}
// std::cout << "Shader id is " << ShaderId << std::endl;
GLuint vShaderLocation = AddShader(ShaderId, vShaderCode, GL_VERTEX_SHADER);
GLuint fShaderLocation = AddShader(ShaderId, fShaderCode, GL_FRAGMENT_SHADER);
glLinkProgram(ShaderId);
glDeleteShader(vShaderLocation);
glDeleteShader(fShaderLocation);
//ValidateShaders();
GetAllUniforms();
}
void Shader::CompileShaders(const char* vShaderCode, const char* gShaderCode, const char* fShaderCode)
{
ShaderId = glCreateProgram();
if (!ShaderId) {
std::cout << "Program was not sucessfully created!!!\n";
exit(1);
}
// std::cout << "Shader id is " << ShaderId << std::endl;
GLuint vShaderLocation = AddShader(ShaderId, vShaderCode, GL_VERTEX_SHADER);
GLuint gShaderLocation = AddShader(ShaderId, gShaderCode, GL_GEOMETRY_SHADER);
GLuint fShaderLocation = AddShader(ShaderId, fShaderCode, GL_FRAGMENT_SHADER);
glLinkProgram(ShaderId);
glDeleteShader(vShaderLocation);
glDeleteShader(gShaderLocation);
glDeleteShader(fShaderLocation);
// ValidateShaders();
GetAllUniforms();
}
void Shader::ValidateShaders()
{
GLint result = 0;
GLchar eLog[1024] = { 0 };
glGetProgramiv(ShaderId, GL_LINK_STATUS, &result);
if (result == GL_FALSE)
{
glGetProgramInfoLog(ShaderId, sizeof(eLog), NULL, eLog);
std::cout << "Error linking program: " << eLog << std::endl;
return;
}
glValidateProgram(ShaderId);
glGetProgramiv(ShaderId, GL_VALIDATE_STATUS, &result);
if (!result)
{
glGetProgramInfoLog(ShaderId, sizeof(eLog), NULL, eLog);
std::cout << "Error validating program " << eLog << std::endl;
}
}
void Shader::GetAllUniforms()
{
//Finds location ID for the model, the type of projection and the user's view
UniformModel = glGetUniformLocation(ShaderId, "model");
UniformProjection = glGetUniformLocation(ShaderId, "projection");
UniformView = glGetUniformLocation(ShaderId, "view");
//Gets location IDs for the directional light
DirectionalLightUniformContainer.UniformAmbientLightColour = glGetUniformLocation(ShaderId, "dLight.LightData.LightColour");
DirectionalLightUniformContainer.UniformAmbientLightIntensity = glGetUniformLocation(ShaderId, "dLight.LightData.AmbientIntensity");
DirectionalLightUniformContainer.UniformDiffuseDirection = glGetUniformLocation(ShaderId, "dLight.Direction");
DirectionalLightUniformContainer.UniformDiffuseIntensity = glGetUniformLocation(ShaderId, "dLight.LightData.DiffuseIntensity");
//Gets location IDs for the shininess and specular intensity of the material
UniformSpecularIntensity = glGetUniformLocation(ShaderId, "material.SpecularIntensity");
UniformSpecularShininess = glGetUniformLocation(ShaderId, "material.Shininess");
//Gets location IDs for the position of camera in world space
UniformCameraPosition = glGetUniformLocation(ShaderId, "CameraViewPosition");
//Gets location ID for the number of point lights in the scene
UniformPointLightCount = glGetUniformLocation(ShaderId, "PointLightCount");
//Gets location ID for the number of spot lights in the scene
UniformSpotLightCount = glGetUniformLocation(ShaderId, "SpotLightCount");
//Gets location ID for the texture
UniformTex2DSampler = glGetUniformLocation(ShaderId, "theTexture");
//Gets location ID for the shadow map
UniformShadowMap = glGetUniformLocation(ShaderId, "DirectionalShadowMap");
UniformDirectionalLightSpaceTransform = glGetUniformLocation(ShaderId, "directionalLightTransform");
UniformOmniLightPosition =glGetUniformLocation(ShaderId,"LightPosition");
UniformFarPlane =glGetUniformLocation(ShaderId, "farPlane");
//Since PointLightUniformContainer is array we use for loop to fill in the values
for (size_t i = 0; i < MAX_POINT_LIGHTS; i++)
{
char LocationBuffer[100] = { "\0" };
//snprintf is used to store formatted string within char array
snprintf(LocationBuffer, sizeof(LocationBuffer), "pLights[%d].LightData.LightColour", i);
// std::cout << LocationBuffer << std::endl;
PointLightUniformContainer[i].UniformAmbientLightColour = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "pLights[%d].LightData.AmbientIntensity", i);
PointLightUniformContainer[i].UniformAmbientLightIntensity = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "pLights[%d].LightData.DiffuseIntensity", i);
PointLightUniformContainer[i].UniformDiffuseIntensity = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "pLights[%d].LightPosition", i);
PointLightUniformContainer[i].UniformLightPosition = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "pLights[%d].A", i);
PointLightUniformContainer[i].UniformCoeffA = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "pLights[%d].B", i);
PointLightUniformContainer[i].UniformCoeffB = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "pLights[%d].C", i);
PointLightUniformContainer[i].UniformCoeffC = glGetUniformLocation(ShaderId, LocationBuffer);
}
//Since SpotLightUniformContainer is array we use for loop to fill in the values
for (size_t i = 0; i < MAX_SPOT_LIGHTS; i++)
{
char LocationBuffer[100] = { "\0" };
//snprintf is used to store formatted string within char array
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].pLightData.LightData.LightColour", i);
SpotLightUniformContainer[i].UniformAmbientLightColour = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].pLightData.LightData.AmbientIntensity", i);
SpotLightUniformContainer[i].UniformAmbientLightIntensity = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].pLightData.LightData.DiffuseIntensity", i);
SpotLightUniformContainer[i].UniformDiffuseIntensity = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].pLightData.LightPosition", i);
SpotLightUniformContainer[i].UniformLightPosition = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].pLightData.A", i);
SpotLightUniformContainer[i].UniformCoeffA = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].pLightData.B", i);
SpotLightUniformContainer[i].UniformCoeffB = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].pLightData.C", i);
SpotLightUniformContainer[i].UniformCoeffC = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].SpotLightDirection", i);
SpotLightUniformContainer[i].UniformSpotLightDirection = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].Cutoff", i);
SpotLightUniformContainer[i].UniformCutoff = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "sLights[%d].LightStatus", i);
SpotLightUniformContainer[i].UniformSpotLightStatus= glGetUniformLocation(ShaderId, LocationBuffer);
}
//Omni shadow map uniforms
for (size_t i = 0; i <6; i++)
{
char LocationBuffer[100] = { "\0" };
snprintf(LocationBuffer, sizeof(LocationBuffer), "lightMatrices[%d]", i);
UniformLightMatrices[i] = glGetUniformLocation(ShaderId, LocationBuffer);
//std::cout << UniformLightMatrices[i] << std::endl;
}
for (size_t i = 0; i < MAX_POINT_LIGHTS + MAX_SPOT_LIGHTS; i++)
{
char LocationBuffer[100] = { "\0" };
snprintf(LocationBuffer, sizeof(LocationBuffer), "OSMap[%d].ShadowTexture", i);
OmniMapUniformContainer[i].UniformShadowTexture = glGetUniformLocation(ShaderId, LocationBuffer);
snprintf(LocationBuffer, sizeof(LocationBuffer), "OSMap[%d].FarPlane", i);
OmniMapUniformContainer[i].UniformFPlane = glGetUniformLocation(ShaderId, LocationBuffer);
}
}
//Enables shaders
void Shader::EnableShader()
{
glUseProgram(ShaderId);
// std::cout << "Now using " << ShaderId << std::endl;
}
//Unlinks the shaders
void Shader::DisableShader() {
glUseProgram(0);
}
//Resets shader program and uniform variables
void Shader::ClearShaders()
{
if (ShaderId != 0)
{
ShaderId = 0;
}
UniformModel = 0;
UniformProjection = 0;
}
void Shader::SetDirectionalLight(DirectionalLight* TheLight)
{
dLight = TheLight;
}
void Shader::EnableDirectionalLight()
{
dLight->UseLight(DirectionalLightUniformContainer.UniformAmbientLightColour, DirectionalLightUniformContainer.UniformAmbientLightIntensity, DirectionalLightUniformContainer.UniformDiffuseDirection, DirectionalLightUniformContainer.UniformDiffuseIntensity);
}
void Shader::SetPointLight(PointLight* TheLight, GLuint NumberOfPointLights)
{
if (NumberOfPointLights > MAX_POINT_LIGHTS) NumberOfPointLights = MAX_POINT_LIGHTS;
PointLightCount = NumberOfPointLights;
// std::cout << "Poin light count " << PointLightCount << std::endl;
pLight = TheLight;
}
void Shader::EnablePointLight(GLuint TexUnit)
{
glUniform1i(UniformPointLightCount, PointLightCount);
for (size_t i = 0; i < PointLightCount; i++)
{
pLight[i].UseLight(PointLightUniformContainer[i].UniformAmbientLightColour,
PointLightUniformContainer[i].UniformAmbientLightIntensity,
PointLightUniformContainer[i].UniformDiffuseIntensity,
PointLightUniformContainer[i].UniformLightPosition,
PointLightUniformContainer[i].UniformCoeffA,
PointLightUniformContainer[i].UniformCoeffB,
PointLightUniformContainer[i].UniformCoeffC);
pLight[i].GetShadowMap()->Read(GL_TEXTURE0 + TexUnit+i);
glUniform1i(OmniMapUniformContainer[i].UniformShadowTexture,TexUnit+i);
glUniform1f(OmniMapUniformContainer[i].UniformFPlane,pLight[i].GetFarPlane());
}
}
void Shader::SetSpotLight(SpotLight* TheLight, GLuint NumberOfSpotLights)
{
if (NumberOfSpotLights > MAX_SPOT_LIGHTS) NumberOfSpotLights = MAX_SPOT_LIGHTS;
sLight = TheLight;
SpotLightCount = NumberOfSpotLights;
//std::cout << "spot light count " << SpotLightCount << std::endl;
}
void Shader::EnableSpotLight(GLuint TexUnit)
{
glUniform1i(UniformSpotLightCount, SpotLightCount);
for (size_t i = 0; i < SpotLightCount; i++)
{
sLight[i].UseLight(SpotLightUniformContainer[i].UniformAmbientLightColour,
SpotLightUniformContainer[i].UniformAmbientLightIntensity,
SpotLightUniformContainer[i].UniformDiffuseIntensity,
SpotLightUniformContainer[i].UniformLightPosition,
SpotLightUniformContainer[i].UniformCoeffA,
SpotLightUniformContainer[i].UniformCoeffB,
SpotLightUniformContainer[i].UniformCoeffC,
SpotLightUniformContainer[i].UniformSpotLightDirection,
SpotLightUniformContainer[i].UniformCutoff,
SpotLightUniformContainer[i].UniformSpotLightStatus);
sLight[i].GetShadowMap()->Read(GL_TEXTURE0 + TexUnit + i);
glUniform1i(OmniMapUniformContainer[i+PointLightCount].UniformShadowTexture, TexUnit+ i);
glUniform1f(OmniMapUniformContainer[i+PointLightCount].UniformFPlane, sLight[i].GetFarPlane());
}
}
//Returns uniform model
GLuint Shader::GetUniformModel()
{
return UniformModel;
}
//Returns uniform variable
GLuint Shader::GetUniformProjection()
{
return UniformProjection;
}
GLuint Shader::GetUniformView()
{
return UniformView;
}
GLuint Shader::GetUniformAmbientLightColour()
{
return DirectionalLightUniformContainer.UniformAmbientLightColour;
}
GLuint Shader::GetUniformAmbientLightIntensity()
{
return DirectionalLightUniformContainer.UniformAmbientLightIntensity;
}
GLuint Shader::GetUniformDiffuseDirection()
{
return DirectionalLightUniformContainer.UniformDiffuseDirection;
}
GLuint Shader::GetUniformDiffuseIntensity()
{
return DirectionalLightUniformContainer.UniformDiffuseIntensity;
}
GLuint Shader::GetUniformSpecularIntensity()
{
return UniformSpecularIntensity;
}
GLuint Shader::GetUniformSpecularShininess()
{
return UniformSpecularShininess;
}
GLuint Shader::GetUniformCameraPosition()
{
return UniformCameraPosition;
}
GLuint Shader::GetUniformFarPlane()
{
return UniformFarPlane;
}
GLuint Shader::GetUniformOmniLightPosition()
{
return UniformOmniLightPosition;
}
void Shader::SetTexture(GLuint TextureUnit)
{
glUniform1i(UniformTex2DSampler, TextureUnit);
}
void Shader::SetDirectionalShadowMap(GLuint TextureUnit)
{
glUniform1i(UniformShadowMap, TextureUnit);
}
void Shader::SetDirectionalLightTransform(glm::mat4 DirectionalLightTransform)
{
glUniformMatrix4fv(UniformDirectionalLightSpaceTransform,1,GL_FALSE,glm::value_ptr(DirectionalLightTransform));
}
void Shader::SetLightMatrices(std::vector<glm::mat4> lightMatrices)
{
for (size_t i = 0; i < 6; i++)
{
glUniformMatrix4fv(UniformLightMatrices[i], 1, GL_FALSE, glm::value_ptr(lightMatrices[i]));
}
}
//Destructor
Shader::~Shader()
{
ClearShaders();
}