forked from kevinmoran/GJK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.h
executable file
·176 lines (157 loc) · 5.53 KB
/
Shader.h
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
#pragma once
// #include <stdio.h>
#include <cstring> //strcat
#define SHADER_PATH "Shaders/"
#define VP_ATTRIB_LOC 0
#define VT_ATTRIB_LOC 1
#define VN_ATTRIB_LOC 2
struct Shader {
GLuint id;
GLuint M_loc, V_loc, P_loc;
bool compiled;
};
Shader init_shader(const char* vert_file, const char* frag_file);
static bool load_shader_program(const char* vert_file, const char* frag_file, GLuint* id);
bool reload_shader_program(const char* vert_file, const char* frag_file, Shader* s);
void delete_program(Shader* s);
Shader init_shader(const char* vert_file, const char* frag_file){
Shader temp;
if(!load_shader_program(vert_file, frag_file, &temp.id)){
//handle failure? default shader program?
temp.compiled = false;
return temp;
}
temp.compiled = true;
temp.M_loc = glGetUniformLocation(temp.id, "M");
temp.V_loc = glGetUniformLocation(temp.id, "V");
temp.P_loc = glGetUniformLocation(temp.id, "P");
return temp;
}
static bool load_shader_program(const char* vert_file, const char* frag_file, GLuint* id){
GLuint vs, fs;
//Load vertex shader
char vs_string[65536];
{
vs_string[0] = '\0';
char vert_file_path[64];
sprintf(vert_file_path, "%s%s", SHADER_PATH, vert_file);
FILE* fp = fopen(vert_file_path, "r");
if(!fp){
fprintf(stderr, "ERROR opening vertex shader file: %s\n", vert_file_path);
return false;
}
char line[1024];
while(fgets(line, 1024, fp)){
strcat(vs_string, line);
}
fclose(fp);
}
//Compile vertex shader
{
vs = glCreateShader(GL_VERTEX_SHADER);
const char* vs_pointer = vs_string;
glShaderSource(vs, 1, &vs_pointer, NULL);
glCompileShader(vs);
//Check for compilation errors
GLint params = -1;
glGetShaderiv(vs, GL_COMPILE_STATUS, ¶ms);
if(params!=GL_TRUE){
fprintf(stderr,"ERROR: vertex shader `%s` did not compile\n", vert_file);
int log_length = 0;
char shader_log[2048];
glGetShaderInfoLog(vs, 2048, &log_length, shader_log);
fprintf(stderr, "shader info log for GL index %u: \n%s\n", vs, shader_log);
glDetachShader(*id, vs);
glDeleteShader(vs);
return false;
}
}
//Load fragment shader
char fs_string[65536];
{
fs_string[0] = '\0';
char frag_file_path[64];
sprintf(frag_file_path, "%s%s", SHADER_PATH, frag_file);
FILE* fp = fopen(frag_file_path, "r");
if(!fp){
fprintf(stderr, "ERROR opening fragment shader file: %s\n", frag_file_path);
glDetachShader(*id, vs);
glDeleteShader(vs);
return false;
}
char line[1024];
while(fgets(line, 1024, fp)){
strcat(fs_string, line);
}
fclose(fp);
}
//Compile fragment shader
{
fs = glCreateShader(GL_FRAGMENT_SHADER);
const char* fs_pointer = fs_string;
glShaderSource(fs, 1, &fs_pointer, NULL);
glCompileShader(fs);
//Check for compilation errors
GLint params = -1;
glGetShaderiv(fs, GL_COMPILE_STATUS, ¶ms);
if(params!=GL_TRUE){
fprintf(stderr,"ERROR: fragment shader `%s` did not compile\n", frag_file);
int log_length = 0;
char shader_log[2048];
glGetShaderInfoLog(fs, 2048, &log_length, shader_log);
fprintf(stderr, "shader info log for GL index %u: \n%s\n", fs, shader_log);
glDetachShader(*id, vs);
glDetachShader(*id, fs);
glDeleteShader(vs);
glDeleteShader(fs);
return false;
}
}
//Create and link program
*id = glCreateProgram();
glAttachShader(*id, vs);
glAttachShader(*id, fs);
glBindAttribLocation(*id, VP_ATTRIB_LOC, "vp");
glBindAttribLocation(*id, VT_ATTRIB_LOC, "vt");
glBindAttribLocation(*id, VN_ATTRIB_LOC, "vn");
glLinkProgram(*id);
//Check for linking errors
{
GLint params = -1;
glGetProgramiv(*id, GL_LINK_STATUS, ¶ms);
if(params != GL_TRUE){
fprintf(stderr, "ERROR linking shader program with GL index %u\n", *id);
int log_length = 0;
char prog_log[2048];
glGetProgramInfoLog(*id, 2048, &log_length, prog_log);
fprintf(stderr, "program info log for GL index %u:\n%s\n", *id, prog_log);
return false;
}
}
glDetachShader(*id, vs);
glDetachShader(*id, fs);
glDeleteShader(vs);
glDeleteShader(fs);
return true;
}
bool reload_shader_program(const char* vert_file, const char* frag_file, Shader* s){
delete_program(s);
if(!load_shader_program(vert_file, frag_file, &(s->id))) {
fprintf(stderr, "ERROR in reload_shader_program using vert shader %s and frag shader %s", vert_file, frag_file);
return false;
}
s->M_loc = glGetUniformLocation(s->id, "M");
s->V_loc = glGetUniformLocation(s->id, "V");
s->P_loc = glGetUniformLocation(s->id, "P");
return true;
}
void delete_program(Shader* s){
if(s->compiled) {
glDeleteProgram(s->id);
s->id = -1;
s->M_loc = -1;
s->V_loc = -1;
s->P_loc = -1;
s->compiled = false;
}
}