add more build controls
[goodguy/history.git] / cinelerra-5.1 / guicast / vframe3d.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #define GL_GLEXT_PROTOTYPES
23
24 #include "bcpbuffer.h"
25 #include "bcresources.h"
26 #include "bcsignals.h"
27 #include "bcsynchronous.h"
28 #include "bctexture.h"
29 #include "bcwindowbase.h"
30 #include "vframe.h"
31
32 #if defined(HAVE_CONFIG_H)
33 #include "config.h"
34 #endif
35 #ifdef HAVE_GL
36 #include <GL/gl.h>
37 #include <GL/glext.h>
38 #endif
39
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 int VFrame::get_opengl_state()
46 {
47         return opengl_state;
48 }
49
50 void VFrame::set_opengl_state(int value)
51 {
52         opengl_state = value;
53 }
54
55 int VFrame::get_window_id()
56 {
57         return texture ? texture->window_id : -1;
58 }
59
60 int VFrame::get_texture_id()
61 {
62         return texture ? texture->texture_id : -1;
63 }
64
65 int VFrame::get_texture_w()
66 {
67         return texture ? texture->texture_w : 0;
68 }
69
70 int VFrame::get_texture_h()
71 {
72         return texture ? texture->texture_h : 0;
73 }
74
75
76 int VFrame::get_texture_components()
77 {
78         return texture ? texture->texture_components : 0;
79 }
80
81
82
83
84
85
86
87
88
89
90
91 void VFrame::to_texture()
92 {
93 #ifdef HAVE_GL
94
95 // Must be here so user can create textures without copying data by setting
96 // opengl_state to TEXTURE.
97         BC_Texture::new_texture(&texture, get_w(), get_h(), get_color_model());
98
99 // Determine what to do based on state
100         switch(opengl_state) {
101         case VFrame::TEXTURE:
102                 return;
103
104         case VFrame::SCREEN:
105                 if((get_w() % 4) || (get_h() % 4)) {
106                         printf("VFrame::to_texture w=%d h=%d\n", get_w(), get_h());
107                         return;
108                 }
109                 if(pbuffer) {
110                         enable_opengl();
111                         screen_to_texture();
112                 }
113                 opengl_state = VFrame::TEXTURE;
114                 return;
115         }
116
117 //printf("VFrame::to_texture %d\n", texture_id);
118         GLenum type, format;
119         switch(color_model) {
120         case BC_RGB888:
121         case BC_YUV888:
122                 type = GL_UNSIGNED_BYTE;
123                 format = GL_RGB;
124                 break;
125
126         case BC_RGBA8888:
127         case BC_YUVA8888:
128                 type = GL_UNSIGNED_BYTE;
129                 format = GL_RGBA;
130                 break;
131
132         case BC_RGB_FLOAT:
133                 type = GL_FLOAT;
134                 format = GL_RGB;
135                 break;
136
137         case BC_RGBA_FLOAT:
138                 format = GL_RGBA;
139                 type = GL_FLOAT;
140                 break;
141
142         default:
143                 fprintf(stderr,
144                         "VFrame::to_texture: unsupported color model %d.\n",
145                         color_model);
146                 return;
147         }
148
149         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, get_w(), get_h(),
150                 format, type, get_rows()[0]);
151         opengl_state = VFrame::TEXTURE;
152 #endif
153 }
154
155 void VFrame::create_pbuffer()
156 {
157         if(pbuffer &&
158                 pbuffer->window_id != BC_WindowBase::get_synchronous()->current_window->get_id())
159         {
160                 delete pbuffer;
161                 pbuffer = 0;
162         }
163
164         if((get_w() % 4) || (get_h() % 4))
165         {
166                 printf("VFrame::create_pbuffer w=%d h=%d\n", get_w(), get_h());
167                 return;
168         }
169
170         if(!pbuffer)
171         {
172                 pbuffer = new BC_PBuffer(get_w(), get_h());
173         }
174 }
175
176 void VFrame::enable_opengl()
177 {
178         create_pbuffer();
179         if(pbuffer)
180         {
181                 pbuffer->enable_opengl();
182         }
183 }
184
185 BC_PBuffer* VFrame::get_pbuffer()
186 {
187         return pbuffer;
188 }
189
190
191 void VFrame::screen_to_texture(int x, int y, int w, int h)
192 {
193 #ifdef HAVE_GL
194 // Create texture
195         BC_Texture::new_texture(&texture,
196                 get_w(), get_h(), get_color_model());
197
198         if(pbuffer) {
199                 glEnable(GL_TEXTURE_2D);
200
201 // Read canvas into texture, use back texture for DOUBLE_BUFFER
202 #if 1
203 // According to the man page, it must be GL_BACK for the onscreen buffer
204 // and GL_FRONT for a single buffered PBuffer.  In reality it must be
205 // GL_BACK for a single buffered PBuffer if the PBuffer has alpha and using
206 // GL_FRONT captures the onscreen front buffer.
207 // 10/11/2010 is now generating "illegal operation"
208                 glReadBuffer(GL_BACK);
209 #else
210                 glReadBuffer(BC_WindowBase::get_synchronous()->is_pbuffer ?
211                         GL_FRONT : GL_BACK);
212 #endif
213                 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
214                         x >= 0 ? x : 0, y >= 0 ? y : 0,
215                         w >= 0 ? w : get_w(), h >= 0 ? h : get_h());
216         }
217 #endif
218 }
219
220 void VFrame::screen_to_ram()
221 {
222 #ifdef HAVE_GL
223         enable_opengl();
224         glReadBuffer(GL_BACK);
225         int type = BC_CModels::is_float(color_model) ? GL_FLOAT : GL_UNSIGNED_BYTE;
226         int format = BC_CModels::has_alpha(color_model) ? GL_RGBA : GL_RGB;
227         glReadPixels(0, 0, get_w(), get_h(), format, type, get_rows()[0]);
228         opengl_state = VFrame::RAM;
229 #endif
230 }
231
232 void VFrame::draw_texture(
233         float in_x1, float in_y1, float in_x2, float in_y2,
234         float out_x1, float out_y1, float out_x2, float out_y2,
235         int flip_y)
236 {
237 #ifdef HAVE_GL
238         in_x1 /= get_texture_w();  in_y1 /= get_texture_h();
239         in_x2 /= get_texture_w();  in_y2 /= get_texture_h();
240         float ot_y1 = flip_y ? -out_y1 : -out_y2;
241         float ot_y2 = flip_y ? -out_y2 : -out_y1;
242         texture->draw_texture(
243                 in_x1,in_y1,  in_x2,in_y2,
244                 out_x1,ot_y1, out_x2, ot_y2);
245 #endif
246 }
247
248 void VFrame::draw_texture(int flip_y)
249 {
250         draw_texture(0,0,  get_w(),get_h(),
251                 0,0, get_w(),get_h(), flip_y);
252 }
253
254
255 void VFrame::bind_texture(int texture_unit)
256 {
257 // Bind the texture
258         if(texture)
259         {
260                 texture->bind(texture_unit);
261         }
262 }
263
264
265
266
267
268
269 void VFrame::init_screen(int w, int h)
270 {
271 #ifdef HAVE_GL
272         glViewport(0, 0, w, h);
273         glMatrixMode(GL_PROJECTION);
274         glLoadIdentity();
275         float near = 1;
276         float far = 100;
277         float frustum_ratio = near / ((near + far)/2);
278         float near_h = (float)h * frustum_ratio;
279         float near_w = (float)w * frustum_ratio;
280         glFrustum(-near_w/2, near_w/2, -near_h/2, near_h/2, near, far);
281         glMatrixMode(GL_MODELVIEW);
282         glLoadIdentity();
283 // Shift down and right so 0,0 is the top left corner
284         glTranslatef(-w/2, h/2, 0.0);
285         glTranslatef(0.0, 0.0, -(far + near) / 2);
286
287         glDisable(GL_DEPTH_TEST);
288         glShadeModel(GL_SMOOTH);
289 // Default for direct copy playback
290         glDisable(GL_BLEND);
291         glDisable(GL_COLOR_MATERIAL);
292         glDisable(GL_CULL_FACE);
293         glEnable(GL_NORMALIZE);
294         glAlphaFunc(GL_ALWAYS, 0);
295         glDisable(GL_ALPHA_TEST);
296         glDisable(GL_LIGHTING);
297
298         const GLfloat zero[] = { 0, 0, 0, 0 };
299 //      const GLfloat one[] = { 1, 1, 1, 1 };
300 //      const GLfloat light_position[] = { 0, 0, -1, 0 };
301 //      const GLfloat light_direction[] = { 0, 0, 1, 0 };
302
303 //      glEnable(GL_LIGHT0);
304 //      glLightfv(GL_LIGHT0, GL_AMBIENT, zero);
305 //      glLightfv(GL_LIGHT0, GL_DIFFUSE, one);
306 //      glLightfv(GL_LIGHT0, GL_SPECULAR, one);
307 //      glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, 180);
308 //      glLightfv(GL_LIGHT0, GL_POSITION, light_position);
309 //      glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_direction);
310 //      glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
311 //      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, zero);
312         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
313         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
314         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zero);
315         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, zero);
316         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0);
317 #endif
318 }
319
320 void VFrame::init_screen()
321 {
322         init_screen(get_w(), get_h());
323 }
324
325 static int print_error(char *source, unsigned int object, int is_program)
326 {
327 #ifdef HAVE_GL
328     char string[BCTEXTLEN];
329         int len = 0;
330     if(is_program)
331                 glGetProgramInfoLog(object, BCTEXTLEN, &len, string);
332         else
333                 glGetShaderInfoLog(object, BCTEXTLEN, &len, string);
334         if(len > 0) printf("Playback3D::print_error:\n%s\n%s\n", source, string);
335         if(len > 0) return 1;
336 #endif
337         return 0;
338 }
339
340
341
342
343
344 unsigned int VFrame::make_shader(int x, ...)
345 {
346         unsigned int result = 0;
347 #ifdef HAVE_GL
348 // Construct single source file out of arguments
349         char *complete_program = 0;
350         int complete_size = 0;
351         int current_shader = 0;
352
353         va_list list;
354         va_start(list, x);
355
356         while(1)
357         {
358                 char *text = va_arg(list, char*);
359                 if(!text) break;
360
361 // Replace one occurrance in each source of main() with a unique id.
362                 char main_replacement[BCTEXTLEN];
363                 sprintf(main_replacement, "main%03d()", current_shader);
364 //printf("VFrame::make_shader %s %s\n", text, main_replacement);
365                 char *source_replacement = new char[strlen(text) + strlen(main_replacement) + 1];
366                 char *ptr = strstr(text, "main()");
367
368                 if(ptr)
369                 {
370                         memcpy(source_replacement, text, ptr - text);
371                         source_replacement[ptr - text] = 0;
372                         strcat(source_replacement, main_replacement);
373                         ptr += strlen("main()");
374                         strcat(source_replacement, ptr);
375                         current_shader++;
376                 }
377                 else
378                 {
379                         memcpy(source_replacement, text, strlen(text));
380                         source_replacement[strlen(text)] = 0;
381                 }
382
383                 if(!complete_program)
384                 {
385                         complete_size = strlen(source_replacement) + 1;
386                         complete_program = (char*)malloc(complete_size);
387                         strcpy(complete_program, source_replacement);
388                 }
389                 else
390                 {
391                         complete_size += strlen(source_replacement);
392                         complete_program = (char*)realloc(complete_program, complete_size);
393                         strcat(complete_program, source_replacement);
394                 }
395
396                 delete [] source_replacement;
397         }
398         va_end(list);
399
400 // Add main() function which calls all the unique main replacements in order
401         char main_function[BCTEXTLEN];
402         sprintf(main_function,
403                 "\n"
404                 "void main()\n"
405                 "{\n");
406
407         for(int i = 0; i < current_shader; i++)
408         {
409                 char main_replacement[BCTEXTLEN];
410                 sprintf(main_replacement, "\tmain%03d();\n", i);
411                 strcat(main_function, main_replacement);
412         }
413
414         strcat(main_function, "}\n");
415         if(!complete_program)
416         {
417                 complete_size = strlen(main_function) + 1;
418                 complete_program = (char*)malloc(complete_size);
419                 strcpy(complete_program, main_function);
420         }
421         else
422         {
423                 complete_size += strlen(main_function);
424                 complete_program = (char*)realloc(complete_program, complete_size);
425                 strcat(complete_program, main_function);
426         }
427
428
429
430
431
432         int got_it = 0;
433         result = BC_WindowBase::get_synchronous()->get_shader(complete_program,
434                 &got_it);
435
436         if(!got_it)
437         {
438                 result = glCreateProgram();
439
440                 unsigned int shader;
441                 shader = glCreateShader(GL_FRAGMENT_SHADER);
442                 const GLchar *text_ptr = complete_program;
443                 glShaderSource(shader, 1, &text_ptr, NULL);
444                 glCompileShader(shader);
445                 int error = print_error(complete_program, shader, 0);
446                 glAttachShader(result, shader);
447                 glDeleteShader(shader);
448
449                 glLinkProgram(result);
450                 if(!error) error = print_error(complete_program, result, 1);
451
452
453 // printf("BC_WindowBase::make_shader: shader=%d window_id=%d\n",
454 // result,
455 // BC_WindowBase::get_synchronous()->current_window->get_id());
456                 BC_WindowBase::get_synchronous()->put_shader(result, complete_program);
457         }
458
459 //printf("VFrame::make_shader\n%s\n", complete_program);
460         free(complete_program);
461         complete_program = NULL;
462
463 #endif
464         return result;
465 }
466
467 void VFrame::dump_shader(int shader_id)
468 {
469         BC_WindowBase::get_synchronous()->dump_shader(shader_id);
470 }
471
472
473 void VFrame::clear_pbuffer()
474 {
475 #ifdef HAVE_GL
476         if(BC_CModels::is_yuv(get_color_model()))
477                 glClearColor(0.0, 0.5, 0.5, 0.0);
478         else
479                 glClearColor(0.0, 0.0, 0.0, 0.0);
480         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
481 #endif
482 }
483