-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShader.cpp
126 lines (99 loc) · 3.1 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
/*
* Shader.cpp
*
* Created on: 21/09/2014
* Author: sam
*/
#include "Shader.h"
// from textfile.cpp
// simple reading and writing for text files
// www.lighthouse3d.com
char *textFileRead(char *fn) {
FILE *fp;
char *content = NULL;
int count=0;
if (fn != NULL) {
fp = fopen(fn,"rt");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
//adapted from https://www.opengl.org/wiki/Shader_Compilation#Shader_error_handling
void compilerLog(GLuint shader) {
GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength includes the NULL character
GLchar* compiler_log = (GLchar*)malloc(maxLength);
glGetShaderInfoLog(shader, maxLength, &maxLength, compiler_log);
printf(compiler_log);
}
}
//NOTE: assumes glew is initialized
Shader::Shader(char* vertSource, char* fragSource) {
char *vs = NULL, *fs = NULL;
// make space, get a handle
vertShader = glCreateShader(GL_VERTEX_SHADER);
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
// read shader files
vs = textFileRead(vertSource);
fs = textFileRead(fragSource);
// attach code to shader
const char *ff = fs;
const char *vv = vs;
glShaderSource(vertShader, 1, &vv,NULL);
glShaderSource(fragShader, 1, &ff,NULL);
// after the files have been loaded by GL we don't need them
free(vs); free(fs);
// compile
glCompileShader(vertShader);
glCompileShader(fragShader);
// use a “program” handle for your shader
program = glCreateProgram();
// attach shader to program
glAttachShader(program, fragShader);
compilerLog(fragShader);
glAttachShader(program, vertShader);
compilerLog(vertShader);
// link the program
glLinkProgram(program);
// get the indexes of uniform variables in the shaders for later use
ModelLocation = glGetUniformLocation(program, "Model");
ViewLocation = glGetUniformLocation(program, "View");
ProjectionLocation = glGetUniformLocation(program, "Projection");
// get the indexes of vertex attributes in the shaders for later use
VertexLocation = glGetAttribLocation(program, "Vertex");
NormalLocation = glGetAttribLocation(program, "Normal");
}
void Shader::setModelMatrix(glm::mat4 m) {
glUseProgram(program);
glUniformMatrix4fv(ModelLocation, 1, GL_FALSE, glm::value_ptr(m));
}
void Shader::setViewMatrix(glm::mat4 m) {
glUseProgram(program);
glUniformMatrix4fv(ViewLocation, 1, GL_FALSE, glm::value_ptr(m));
}
void Shader::setProjectionMatrix(glm::mat4 m) {
glUseProgram(program);
glUniformMatrix4fv(ProjectionLocation, 1, GL_FALSE, glm::value_ptr(m));
}
void Shader::use() {
glUseProgram(program);
}
Shader::~Shader() {
glDeleteShader(vertShader);
glDeleteShader(fragShader);
glDeleteProgram(program);
}