-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderprogram.cpp
151 lines (118 loc) · 4.42 KB
/
shaderprogram.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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "shaderprogram.h"
//Procedure reads a file into an array of chars
char* ShaderProgram::readFile(const char* fileName) {
int filesize;
FILE *plik;
char* result;
#pragma warning(suppress : 4996) //Turn off an error in Visual Studio stemming from Microsoft not adhering to standards
plik=fopen(fileName,"rb");
if (plik != NULL) {
fseek(plik, 0, SEEK_END);
filesize = ftell(plik);
fseek(plik, 0, SEEK_SET);
result = new char[filesize + 1];
#pragma warning(suppress : 6386) //Turn off an error in Visual Studio stemming from incorrent static code analysis
int readsize=fread(result, 1, filesize, plik);
result[filesize] = 0;
fclose(plik);
return result;
}
return NULL;
}
//The method reads a shader code, compiles it and returns a corresponding handle
GLuint ShaderProgram::loadShader(GLenum shaderType,const char* fileName) {
//Create a shader handle
GLuint shader=glCreateShader(shaderType);//shaderType to GL_VERTEX_SHADER, GL_GEOMETRY_SHADER lub GL_FRAGMENT_SHADER
//Read a shader source file into an array of chars
const GLchar* shaderSource=readFile(fileName);
//Associate source code with the shader handle
glShaderSource(shader,1,&shaderSource,NULL);
//Compile source code
glCompileShader(shader);
//Delete source code from memory (it is no longer needed)
delete []shaderSource;
//Download a compilation error log and display it
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 1) {
infoLog = new char[infologLength];
glGetShaderInfoLog(shader, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
delete []infoLog;
}
//Return shader handle
return shader;
}
ShaderProgram::ShaderProgram(const char* vertexShaderFile,const char* geometryShaderFile,const char* fragmentShaderFile) {
//Load vertex shader
printf("Loading vertex shader...\n");
vertexShader=loadShader(GL_VERTEX_SHADER,vertexShaderFile);
//Load geometry shader
if (geometryShaderFile!=NULL) {
printf("Loading geometry shader...\n");
geometryShader=loadShader(GL_GEOMETRY_SHADER,geometryShaderFile);
} else {
geometryShader=0;
}
//Load fragment shader
printf("Loading fragment shader...\n");
fragmentShader=loadShader(GL_FRAGMENT_SHADER,fragmentShaderFile);
//Generate shader program handle
shaderProgram=glCreateProgram();
//Attach shaders and link shader program
glAttachShader(shaderProgram,vertexShader);
glAttachShader(shaderProgram,fragmentShader);
if (geometryShaderFile!=NULL) glAttachShader(shaderProgram,geometryShader);
glLinkProgram(shaderProgram);
//Download an error log and display it
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 1)
{
infoLog = new char[infologLength];
glGetProgramInfoLog(shaderProgram, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
delete []infoLog;
}
printf("Shader program created \n");
}
ShaderProgram::~ShaderProgram() {
//Detach shaders from program
glDetachShader(shaderProgram, vertexShader);
if (geometryShader!=0) glDetachShader(shaderProgram, geometryShader);
glDetachShader(shaderProgram, fragmentShader);
//Delete shaders
glDeleteShader(vertexShader);
if (geometryShader!=0) glDeleteShader(geometryShader);
glDeleteShader(fragmentShader);
//Delete program
glDeleteProgram(shaderProgram);
}
//Make the shader program active
void ShaderProgram::use() {
glUseProgram(shaderProgram);
}
//Get the slot number corresponding to the uniform variableName
GLuint ShaderProgram::u(const char* variableName) {
return glGetUniformLocation(shaderProgram,variableName);
}
//Get the slot number corresponding to the attribute variableName
GLuint ShaderProgram::a(const char* variableName) {
return glGetAttribLocation(shaderProgram,variableName);
}