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