metasurf
view examples/metaballs/src/sdr.c @ 0:7aa4627e492b
first commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 25 Oct 2011 07:34:31 +0300 |
parents | |
children | 9ab057fba0c5 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <assert.h>
6 #include <GL/glew.h>
8 #if defined(unix) || defined(__unix__)
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #endif /* unix */
13 #include "sdr.h"
15 unsigned int create_vertex_shader(const char *src)
16 {
17 return create_shader(src, GL_VERTEX_SHADER);
18 }
20 unsigned int create_pixel_shader(const char *src)
21 {
22 return create_shader(src, GL_FRAGMENT_SHADER);
23 }
25 unsigned int create_shader(const char *src, unsigned int sdr_type)
26 {
27 unsigned int sdr;
28 int success, info_len;
29 char *info_str = 0;
30 GLenum err;
32 sdr = glCreateShader(sdr_type);
33 assert(glGetError() == GL_NO_ERROR);
34 glShaderSource(sdr, 1, &src, 0);
35 err = glGetError();
36 assert(err == GL_NO_ERROR);
37 glCompileShader(sdr);
38 assert(glGetError() == GL_NO_ERROR);
40 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
41 assert(glGetError() == GL_NO_ERROR);
42 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
43 assert(glGetError() == GL_NO_ERROR);
45 if(info_len) {
46 if((info_str = malloc(info_len + 1))) {
47 glGetShaderInfoLog(sdr, info_len, 0, info_str);
48 assert(glGetError() == GL_NO_ERROR);
49 }
50 }
52 if(success) {
53 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
54 } else {
55 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
56 glDeleteShader(sdr);
57 sdr = 0;
58 }
60 free(info_str);
61 return sdr;
62 }
64 void free_shader(unsigned int sdr)
65 {
66 glDeleteShader(sdr);
67 }
69 unsigned int load_vertex_shader(const char *fname)
70 {
71 return load_shader(fname, GL_VERTEX_SHADER);
72 }
74 unsigned int load_pixel_shader(const char *fname)
75 {
76 return load_shader(fname, GL_FRAGMENT_SHADER);
77 }
79 unsigned int load_shader(const char *fname, unsigned int sdr_type)
80 {
81 #if defined(unix) || defined(__unix__)
82 struct stat st;
83 #endif
84 unsigned int sdr;
85 size_t filesize;
86 FILE *fp;
87 char *src;
89 if(!(fp = fopen(fname, "r"))) {
90 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
91 return 0;
92 }
94 #if defined(unix) || defined(__unix__)
95 fstat(fileno(fp), &st);
96 filesize = st.st_size;
97 #else
98 fseek(fp, 0, SEEK_END);
99 filesize = ftell(fp);
100 fseek(fp, 0, SEEK_SET);
101 #endif /* unix */
103 if(!(src = malloc(filesize + 1))) {
104 fclose(fp);
105 return 0;
106 }
107 fread(src, 1, filesize, fp);
108 src[filesize] = 0;
109 fclose(fp);
111 fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname);
112 sdr = create_shader(src, sdr_type);
114 free(src);
115 return sdr;
116 }
119 unsigned int get_vertex_shader(const char *fname)
120 {
121 return get_shader(fname, GL_VERTEX_SHADER);
122 }
124 unsigned int get_pixel_shader(const char *fname)
125 {
126 return get_shader(fname, GL_FRAGMENT_SHADER);
127 }
129 unsigned int get_shader(const char *fname, unsigned int sdr_type)
130 {
131 unsigned int sdr;
133 if(!(sdr = load_shader(fname, sdr_type))) {
134 return 0;
135 }
136 return sdr;
137 }
140 /* ---- gpu programs ---- */
142 unsigned int create_program(void)
143 {
144 unsigned int prog = glCreateProgram();
145 assert(glGetError() == GL_NO_ERROR);
146 return prog;
147 }
149 unsigned int create_program_link(unsigned int vs, unsigned int ps)
150 {
151 unsigned int prog;
153 if(!(prog = create_program())) {
154 return 0;
155 }
157 if(vs) {
158 attach_shader(prog, vs);
159 assert(glGetError() == GL_NO_ERROR);
160 }
161 if(ps) {
162 attach_shader(prog, ps);
163 assert(glGetError() == GL_NO_ERROR);
164 }
166 if(link_program(prog) == -1) {
167 free_program(prog);
168 return 0;
169 }
170 return prog;
171 }
173 unsigned int create_program_load(const char *vfile, const char *pfile)
174 {
175 unsigned int vs = 0, ps = 0;
177 if(vfile && !(vs = get_vertex_shader(vfile))) {
178 return 0;
179 }
180 if(pfile && !(ps = get_pixel_shader(pfile))) {
181 return 0;
182 }
183 return create_program_link(vs, ps);
184 }
186 void free_program(unsigned int sdr)
187 {
188 glDeleteProgram(sdr);
189 }
191 void attach_shader(unsigned int prog, unsigned int sdr)
192 {
193 glAttachShader(prog, sdr);
194 assert(glGetError() == GL_NO_ERROR);
195 }
197 int link_program(unsigned int prog)
198 {
199 int linked, info_len, retval = 0;
200 char *info_str = 0;
202 glLinkProgram(prog);
203 assert(glGetError() == GL_NO_ERROR);
204 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
205 assert(glGetError() == GL_NO_ERROR);
206 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
207 assert(glGetError() == GL_NO_ERROR);
209 if(info_len) {
210 if((info_str = malloc(info_len + 1))) {
211 glGetProgramInfoLog(prog, info_len, 0, info_str);
212 assert(glGetError() == GL_NO_ERROR);
213 }
214 }
216 if(linked) {
217 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
218 } else {
219 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
220 retval = -1;
221 }
223 free(info_str);
224 return retval;
225 }
227 int bind_program(unsigned int prog)
228 {
229 GLenum err;
231 glUseProgram(prog);
232 if(prog && (err = glGetError()) != GL_NO_ERROR) {
233 /* maybe the program is not linked, try linking first */
234 if(err == GL_INVALID_OPERATION) {
235 if(link_program(prog) == -1) {
236 return -1;
237 }
238 glUseProgram(prog);
239 return glGetError() == GL_NO_ERROR ? 0 : -1;
240 }
241 return -1;
242 }
243 return 0;
244 }
246 /* ugly but I'm not going to write the same bloody code over and over */
247 #define BEGIN_UNIFORM_CODE \
248 int loc, curr_prog; \
249 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
250 if(curr_prog != prog && bind_program(prog) == -1) { \
251 return -1; \
252 } \
253 if((loc = glGetUniformLocation(prog, name)) != -1)
255 #define END_UNIFORM_CODE \
256 if(curr_prog != prog) { \
257 bind_program(curr_prog); \
258 } \
259 return loc == -1 ? -1 : 0
261 int set_uniform_int(unsigned int prog, const char *name, int val)
262 {
263 BEGIN_UNIFORM_CODE {
264 glUniform1i(loc, val);
265 }
266 END_UNIFORM_CODE;
267 }
269 int set_uniform_float(unsigned int prog, const char *name, float val)
270 {
271 BEGIN_UNIFORM_CODE {
272 glUniform1f(loc, val);
273 }
274 END_UNIFORM_CODE;
275 }
277 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
278 {
279 BEGIN_UNIFORM_CODE {
280 glUniform3f(loc, x, y, z);
281 }
282 END_UNIFORM_CODE;
283 }
285 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
286 {
287 BEGIN_UNIFORM_CODE {
288 glUniform4f(loc, x, y, z, w);
289 }
290 END_UNIFORM_CODE;
291 }
293 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
294 {
295 BEGIN_UNIFORM_CODE {
296 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
297 }
298 END_UNIFORM_CODE;
299 }
301 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
302 {
303 BEGIN_UNIFORM_CODE {
304 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
305 }
306 END_UNIFORM_CODE;
307 }
309 int get_attrib_loc(unsigned int prog, const char *name)
310 {
311 int loc, curr_prog;
313 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
314 if(curr_prog != prog && bind_program(prog) == -1) {
315 return -1;
316 }
318 loc = glGetAttribLocation(prog, (char*)name);
320 if(curr_prog != prog) {
321 bind_program(curr_prog);
322 }
323 return loc;
324 }
326 void set_attrib_float3(int attr_loc, float x, float y, float z)
327 {
328 glVertexAttrib3f(attr_loc, x, y, z);
329 }