-
Notifications
You must be signed in to change notification settings - Fork 5
/
Shaders.cpp
146 lines (121 loc) · 3.39 KB
/
Shaders.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
// routines for creating shaders
#include <GL/glew.h>
#include <stdio.h>
#include "Shaders.h"
#include <stdarg.h>
char *readShaderFile(char *filename) {
FILE *fid;
char *buffer;
int len;
int n;
fid = fopen(filename,"r");
if(fid == NULL) {
printf("can't open shader file: %s\n", filename);
return(0);
}
fseek(fid, 0, SEEK_END);
len = ftell(fid);
rewind(fid);
buffer = new char[len+1];
n = fread(buffer, sizeof(char), len, fid);
buffer[n] = 0;
return buffer;
}
int buildShader(int type, char *filename) {
int shader;
char *source;
int result;
char *buffer;
shader = glCreateShader(type);
source = readShaderFile(filename);
if(source == 0)
return 0;
glShaderSource(shader, 1, (const GLchar **) &source, 0);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if(result != GL_TRUE) {
printf("shader compile error: %s\n",filename);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &result);
buffer = new char[result];
glGetShaderInfoLog(shader, result, 0, buffer);
printf("%s\n", buffer);
delete buffer;
return(0);
}
return(shader);
}
int buildProgram(int first, ...) {
int result;
char *buffer;
int program;
va_list argptr;
int shader;
int vs = 0;
int fs = 0;
int type;
program = glCreateProgram();
if(first != 0) {
glAttachShader(program,first);
glGetShaderiv(first, GL_SHADER_TYPE, &type);
if(type == GL_VERTEX_SHADER)
vs++;
if(type == GL_FRAGMENT_SHADER)
fs++;
}
va_start(argptr,first);
while((shader = va_arg(argptr,int)) != 0) {
glAttachShader(program,shader);
glGetShaderiv(shader, GL_SHADER_TYPE, &type);
if(type == GL_VERTEX_SHADER)
vs++;
if(type == GL_FRAGMENT_SHADER)
fs++;
}
if(vs == 0) {
printf("no vertex shader\n");
}
if(fs == 0) {
printf("no fragment shader\n");
}
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &result);
if(result != GL_TRUE) {
printf("program link error\n");
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &result);
buffer = new char[result];
glGetProgramInfoLog(program, result, 0, buffer);
printf("%s\n",buffer);
delete buffer;
return(0);
}
return(program);
}
void dumpProgram(int program, char *description) {
char name[256];
GLsizei length;
GLint size;
GLenum type;
int uniforms;
int attributes;
int shaders;
int i;
printf("Information for shader: %s\n",description);
if(!glIsProgram(program)) {
printf("not a valid shader program\n");
return;
}
glGetProgramiv(program, GL_ATTACHED_SHADERS, &shaders);
printf("Number of shaders: %d\n",shaders);
glGetProgramiv(program,GL_ACTIVE_UNIFORMS,&uniforms);
printf("uniforms: %d\n",uniforms);
for(i=0; i<uniforms; i++) {
glGetActiveUniform(program, i, 256, &length ,&size ,&type, name);
printf(" name: %s\n",name);
}
glGetProgramiv(program,GL_ACTIVE_ATTRIBUTES,&attributes);
printf("attributes: %d\n",attributes);
for(i=0; i<attributes; i++) {
glGetActiveAttrib(program, i, 256, &length, &size, &type, name);
printf(" name: %s\n",name);
}
}