-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader_basic.h
173 lines (143 loc) · 4.54 KB
/
shader_basic.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
#ifndef _SHADER_BASIC_
#define _SHADER_BASIC_
#include <GL/glew.h>
#include <stdio.h>
#include <string>
#include <map>
struct Shader{
GLuint vs, gs, fs, pr;
std::map<std::string,int> uni;
void bind(std::string name){
uni[name] = glGetUniformLocation(pr, name.c_str());
}
int operator[](std::string name){
return uni[name];
}
static char *textFileRead(const 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;
}
static int SetFromString( const GLchar *stringV,const GLchar *stringG, const GLchar *stringF, GLuint & vs,GLuint &gs,GLuint &fs, GLuint &pr){
fs = glCreateShader(GL_FRAGMENT_SHADER);
vs = glCreateShader(GL_VERTEX_SHADER);
gs = glCreateShader(GL_GEOMETRY_SHADER);
if(stringV!=NULL){
glShaderSource(vs, 1, &stringV,NULL);
glCompileShader(vs);
int errV;
glGetShaderiv(vs,GL_COMPILE_STATUS,&errV);
if(errV!=GL_TRUE) return -2;
}else return -1;
if(stringG!=NULL){
glShaderSource(gs, 1, &stringG,NULL);
glCompileShader(gs);
int errG;
glGetShaderiv(gs,GL_COMPILE_STATUS,&errG);
if(errG!=GL_TRUE) return -5;
};
if(stringF!=NULL){
glShaderSource(fs, 1, &stringF,NULL);
glCompileShader(fs);
int errF;
glGetShaderiv(fs,GL_COMPILE_STATUS,&errF);
if(errF!=GL_TRUE) return -4;
}else return -3;
pr = glCreateProgram();
glAttachShader(pr,vs);
glAttachShader(pr,fs);
if(stringG!=NULL)
glAttachShader(pr,gs);
glLinkProgram(pr);
return 0;
}
int SetFromFile( const GLchar *nameV, const char *nameG, const char *nameF){
fs= glCreateShader(GL_FRAGMENT_SHADER);
vs= glCreateShader(GL_VERTEX_SHADER);
char * code;
if(nameV!=NULL){
code = textFileRead(nameV);
glShaderSource(vs, 1, &code,NULL);
glCompileShader(vs);
int n;
char b[65536];
glGetShaderInfoLog(vs, 65536, &n, b);
b[n] = '\0';
int errV;
glGetShaderiv(vs,GL_COMPILE_STATUS,&errV);
if(errV!=GL_TRUE)
return -2;
}else return -1;
if(nameG!=NULL){
gs = glCreateShader(GL_GEOMETRY_SHADER);
code = textFileRead(nameG);
glShaderSource(gs, 1, &code,NULL);
glCompileShader(gs);
int n;
char b[65536];
glGetShaderInfoLog(gs, 65536, &n, b);
b[n] = '\0';
int errG;
glGetShaderiv(vs,GL_COMPILE_STATUS,&errG);
if(errG!=GL_TRUE)
return -5;
};
if(nameF!=NULL){
code = textFileRead(nameF);
glShaderSource(fs, 1, &code,NULL);
glCompileShader(fs);
int n;
char b[65536];
glGetShaderInfoLog(fs, 65536, &n, b);
b[n] = '\0';
int errF;
glGetShaderiv(fs,GL_COMPILE_STATUS,&errF);
if(errF!=GL_TRUE)
return -4;
}else return -3;
pr = glCreateProgram();
if(nameV!=NULL) glAttachShader(pr,vs);
if(nameG!=NULL) glAttachShader(pr,gs);
if(nameF!=NULL) glAttachShader(pr,fs);
glLinkProgram(pr);
glValidateProgram(pr);
GLuint err = 0;
err = glGetError();
char res[65536];
int l;
glGetProgramInfoLog(pr,65536,&l,res);
res[l] = '\0';
return 0;
}
void Validate( ){
int res;
const GLuint & s = this->pr;
glValidateProgram(s);
glGetProgramiv(s,GL_VALIDATE_STATUS,&res);
printf("validation of program %d:%d \n",s,res);
glGetProgramiv(s,GL_LINK_STATUS,&res);
printf("linking of program %d:%d \n",s,res);
glGetProgramiv(s,GL_ACTIVE_ATTRIBUTES,&res);
printf("active attribute of program %d:%d \n",s,res);
glGetProgramiv(s,GL_ACTIVE_UNIFORMS,&res);
printf("active uniform of program %d:%d \n",s,res);
glGetProgramiv(s,GL_ACTIVE_UNIFORM_MAX_LENGTH,&res);
printf("active uniform Max Length of program %d:%d \n",s,res);
}
};
#endif