istereo2
view src/sdr.c @ 20:2b85d05df3f2
animation controls for easier screenshot grabbing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 02 Oct 2015 04:54:55 +0300 |
parents | |
children | 9d53a4938ce8 |
line source
1 /*
2 Stereoscopic tunnel for iOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <assert.h>
24 #include "opengl.h"
26 #if defined(unix) || defined(__unix__)
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #endif /* unix */
31 #include "sdr.h"
34 unsigned int create_vertex_shader(const char *src)
35 {
36 return create_shader(src, GL_VERTEX_SHADER);
37 }
39 unsigned int create_pixel_shader(const char *src)
40 {
41 return create_shader(src, GL_FRAGMENT_SHADER);
42 }
44 unsigned int create_shader(const char *src, unsigned int sdr_type)
45 {
46 unsigned int sdr;
47 int success, info_len;
48 char *info_str = 0;
49 GLenum err;
51 sdr = glCreateShader(sdr_type);
52 assert(glGetError() == GL_NO_ERROR);
53 glShaderSource(sdr, 1, &src, 0);
54 err = glGetError();
55 assert(err == GL_NO_ERROR);
56 glCompileShader(sdr);
57 assert(glGetError() == GL_NO_ERROR);
59 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
60 assert(glGetError() == GL_NO_ERROR);
61 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
62 assert(glGetError() == GL_NO_ERROR);
64 if(info_len) {
65 if((info_str = malloc(info_len + 1))) {
66 glGetShaderInfoLog(sdr, info_len, 0, info_str);
67 assert(glGetError() == GL_NO_ERROR);
68 }
69 }
71 if(success) {
72 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
73 } else {
74 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
75 glDeleteShader(sdr);
76 sdr = 0;
77 }
79 free(info_str);
80 return sdr;
81 }
83 void free_shader(unsigned int sdr)
84 {
85 glDeleteShader(sdr);
86 }
88 unsigned int load_vertex_shader(const char *fname)
89 {
90 return load_shader(fname, GL_VERTEX_SHADER);
91 }
93 unsigned int load_pixel_shader(const char *fname)
94 {
95 return load_shader(fname, GL_FRAGMENT_SHADER);
96 }
98 unsigned int load_shader(const char *fname, unsigned int sdr_type)
99 {
100 #if defined(unix) || defined(__unix__)
101 struct stat st;
102 #endif
103 unsigned int sdr;
104 size_t filesize;
105 FILE *fp;
106 char *src;
108 if(!(fp = fopen(fname, "r"))) {
109 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
110 return 0;
111 }
113 #if defined(unix) || defined(__unix__)
114 fstat(fileno(fp), &st);
115 filesize = st.st_size;
116 #else
117 fseek(fp, 0, SEEK_END);
118 filesize = ftell(fp);
119 fseek(fp, 0, SEEK_SET);
120 #endif /* unix */
122 if(!(src = malloc(filesize + 1))) {
123 fclose(fp);
124 return 0;
125 }
126 fread(src, 1, filesize, fp);
127 src[filesize] = 0;
128 fclose(fp);
130 fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname);
131 sdr = create_shader(src, sdr_type);
133 free(src);
134 return sdr;
135 }
138 unsigned int get_vertex_shader(const char *fname)
139 {
140 return get_shader(fname, GL_VERTEX_SHADER);
141 }
143 unsigned int get_pixel_shader(const char *fname)
144 {
145 return get_shader(fname, GL_FRAGMENT_SHADER);
146 }
148 unsigned int get_shader(const char *fname, unsigned int sdr_type)
149 {
150 unsigned int sdr;
152 if(!fname || !(sdr = load_shader(fname, sdr_type))) {
153 return 0;
154 }
155 return sdr;
156 }
159 /* ---- gpu programs ---- */
161 unsigned int create_program(void)
162 {
163 unsigned int prog = glCreateProgram();
164 assert(glGetError() == GL_NO_ERROR);
165 return prog;
166 }
168 unsigned int create_program_link(unsigned int vs, unsigned int ps)
169 {
170 unsigned int prog;
172 if(!(prog = create_program())) {
173 return 0;
174 }
176 attach_shader(prog, vs);
177 assert(glGetError() == GL_NO_ERROR);
178 attach_shader(prog, ps);
179 assert(glGetError() == GL_NO_ERROR);
181 if(link_program(prog) == -1) {
182 free_program(prog);
183 return 0;
184 }
185 return prog;
186 }
188 unsigned int create_program_load(const char *vfile, const char *pfile)
189 {
190 unsigned int vs, ps;
192 if(!(vs = get_vertex_shader(vfile)) || !(ps = get_pixel_shader(pfile))) {
193 return 0;
194 }
195 return create_program_link(vs, ps);
196 }
198 void free_program(unsigned int sdr)
199 {
200 glDeleteProgram(sdr);
201 }
203 void attach_shader(unsigned int prog, unsigned int sdr)
204 {
205 glAttachShader(prog, sdr);
206 assert(glGetError() == GL_NO_ERROR);
207 }
209 int link_program(unsigned int prog)
210 {
211 int linked, info_len, retval = 0;
212 char *info_str = 0;
214 glLinkProgram(prog);
215 assert(glGetError() == GL_NO_ERROR);
216 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
217 assert(glGetError() == GL_NO_ERROR);
218 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
219 assert(glGetError() == GL_NO_ERROR);
221 if(info_len) {
222 if((info_str = malloc(info_len + 1))) {
223 glGetProgramInfoLog(prog, info_len, 0, info_str);
224 assert(glGetError() == GL_NO_ERROR);
225 }
226 }
228 if(linked) {
229 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
230 } else {
231 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
232 retval = -1;
233 }
235 free(info_str);
236 return retval;
237 }
239 int bind_program(unsigned int prog)
240 {
241 GLenum err;
243 glUseProgram(prog);
244 if(prog && (err = glGetError()) != GL_NO_ERROR) {
245 /* maybe the program is not linked, try linking first */
246 if(err == GL_INVALID_OPERATION) {
247 if(link_program(prog) == -1) {
248 return -1;
249 }
250 glUseProgram(prog);
251 return glGetError() == GL_NO_ERROR ? 0 : -1;
252 }
253 return -1;
254 }
255 return 0;
256 }
258 /* ugly but I'm not going to write the same bloody code over and over */
259 #define BEGIN_UNIFORM_CODE \
260 int loc, curr_prog; \
261 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
262 if(curr_prog != prog && bind_program(prog) == -1) { \
263 return -1; \
264 } \
265 if((loc = glGetUniformLocation(prog, name)) != -1)
267 #define END_UNIFORM_CODE \
268 if(curr_prog != prog) { \
269 bind_program(curr_prog); \
270 } \
271 return loc == -1 ? -1 : 0
273 int set_uniform_int(unsigned int prog, const char *name, int val)
274 {
275 BEGIN_UNIFORM_CODE {
276 glUniform1i(loc, val);
277 }
278 END_UNIFORM_CODE;
279 }
281 int set_uniform_float(unsigned int prog, const char *name, float val)
282 {
283 BEGIN_UNIFORM_CODE {
284 glUniform1f(loc, val);
285 }
286 END_UNIFORM_CODE;
287 }
289 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
290 {
291 BEGIN_UNIFORM_CODE {
292 glUniform3f(loc, x, y, z);
293 }
294 END_UNIFORM_CODE;
295 }
297 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
298 {
299 BEGIN_UNIFORM_CODE {
300 glUniform4f(loc, x, y, z, w);
301 }
302 END_UNIFORM_CODE;
303 }
305 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
306 {
307 BEGIN_UNIFORM_CODE {
308 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
309 }
310 END_UNIFORM_CODE;
311 }
313 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
314 {
315 BEGIN_UNIFORM_CODE {
316 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
317 }
318 END_UNIFORM_CODE;
319 }
321 int get_attrib_loc(unsigned int prog, const char *name)
322 {
323 int loc, curr_prog;
325 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
326 if(curr_prog != prog && bind_program(prog) == -1) {
327 return -1;
328 }
330 loc = glGetAttribLocation(prog, (char*)name);
332 if(curr_prog != prog) {
333 bind_program(curr_prog);
334 }
335 return loc;
336 }
338 void set_attrib_float3(int attr_loc, float x, float y, float z)
339 {
340 glVertexAttrib3f(attr_loc, x, y, z);
341 }