add BC_SCALE env var for hi def monitors, cleanup theme data
[goodguy/cinelerra.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 #ifdef HAVE_GL
33 #include <GL/gl.h>
34 #include <GL/glext.h>
35 #endif
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 int VFrame::get_opengl_state()
43 {
44         return opengl_state;
45 }
46
47 void VFrame::set_opengl_state(int value)
48 {
49         opengl_state = value;
50 }
51
52 int VFrame::get_window_id()
53 {
54         return texture ? texture->window_id : -1;
55 }
56
57 int VFrame::get_texture_id()
58 {
59         return texture ? texture->texture_id : -1;
60 }
61
62 int VFrame::get_texture_w()
63 {
64         return texture ? texture->texture_w : 0;
65 }
66
67 int VFrame::get_texture_h()
68 {
69         return texture ? texture->texture_h : 0;
70 }
71
72
73 int VFrame::get_texture_components()
74 {
75         return texture ? texture->texture_components : 0;
76 }
77
78
79 void VFrame::to_texture()
80 {
81 #ifdef HAVE_GL
82
83 // Must be here so user can create textures without copying data by setting
84 // opengl_state to TEXTURE.
85         BC_Texture::new_texture(&texture, get_w(), get_h(), get_color_model());
86
87 // Determine what to do based on state
88         switch(opengl_state) {
89         case VFrame::TEXTURE:
90                 return;
91
92         case VFrame::SCREEN:
93                 if(pbuffer) {
94                         enable_opengl();
95                         screen_to_texture();
96                 }
97                 opengl_state = VFrame::TEXTURE;
98                 return;
99         }
100
101 //printf("VFrame::to_texture %d\n", texture_id);
102         GLenum type, format;
103         switch(color_model) {
104         case BC_RGB888:
105         case BC_YUV888:
106                 type = GL_UNSIGNED_BYTE;
107                 format = GL_RGB;
108                 break;
109
110         case BC_RGBA8888:
111         case BC_YUVA8888:
112                 type = GL_UNSIGNED_BYTE;
113                 format = GL_RGBA;
114                 break;
115
116         case BC_RGB_FLOAT:
117                 type = GL_FLOAT;
118                 format = GL_RGB;
119                 break;
120
121         case BC_RGBA_FLOAT:
122                 format = GL_RGBA;
123                 type = GL_FLOAT;
124                 break;
125
126         default:
127                 fprintf(stderr,
128                         "VFrame::to_texture: unsupported color model %d.\n",
129                         color_model);
130                 return;
131         }
132
133         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, get_w(), get_h(),
134                 format, type, get_rows()[0]);
135         opengl_state = VFrame::TEXTURE;
136 #endif
137 }
138
139 void VFrame::create_pbuffer()
140 {
141 #ifdef GLx4
142         int ww = (get_w()+3) & ~3, hh = (get_h()+3) & ~3;
143 #else
144         int ww = get_w(), hh = get_h();
145 #endif
146         if( pbuffer && (pbuffer->w != ww || pbuffer->h != hh ||
147             pbuffer->window_id != BC_WindowBase::get_synchronous()->current_window->get_id() ) ) {
148                 delete pbuffer;
149                 pbuffer = 0;
150         }
151
152         if( !pbuffer ) {
153                 pbuffer = new BC_PBuffer(ww, hh);
154         }
155 }
156
157 void VFrame::enable_opengl()
158 {
159         create_pbuffer();
160         if( pbuffer ) {
161                 pbuffer->enable_opengl();
162         }
163 }
164
165 BC_PBuffer* VFrame::get_pbuffer()
166 {
167         return pbuffer;
168 }
169
170
171 void VFrame::screen_to_texture(int x, int y, int w, int h)
172 {
173 #ifdef HAVE_GL
174 // Create texture
175         BC_Texture::new_texture(&texture,
176                 get_w(), get_h(), get_color_model());
177         if(pbuffer) {
178                 glEnable(GL_TEXTURE_2D);
179
180 // Read canvas into texture, use back texture for DOUBLE_BUFFER
181 #if 1
182 // According to the man page, it must be GL_BACK for the onscreen buffer
183 // and GL_FRONT for a single buffered PBuffer.  In reality it must be
184 // GL_BACK for a single buffered PBuffer if the PBuffer has alpha and using
185 // GL_FRONT captures the onscreen front buffer.
186 // 10/11/2010 is now generating "illegal operation"
187                 glReadBuffer(GL_BACK);
188 #else
189                 glReadBuffer(BC_WindowBase::get_synchronous()->is_pbuffer ?
190                         GL_FRONT : GL_BACK);
191 #endif
192                 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
193                         x >= 0 ? x : 0, y >= 0 ? y : 0,
194                         w >= 0 ? w : get_w(), h >= 0 ? h : get_h());
195         }
196 #endif
197 }
198
199 void VFrame::screen_to_ram()
200 {
201 #ifdef HAVE_GL
202         enable_opengl();
203         glReadBuffer(GL_BACK);
204         int type = BC_CModels::is_float(color_model) ? GL_FLOAT : GL_UNSIGNED_BYTE;
205         int format = BC_CModels::has_alpha(color_model) ? GL_RGBA : GL_RGB;
206         glReadPixels(0, 0, get_w(), get_h(), format, type, get_rows()[0]);
207         opengl_state = VFrame::RAM;
208 #endif
209 }
210
211 void VFrame::draw_texture(
212         float in_x1, float in_y1, float in_x2, float in_y2,
213         float out_x1, float out_y1, float out_x2, float out_y2,
214         int flip_y)
215 {
216 #ifdef HAVE_GL
217         in_x1 /= get_texture_w();  in_y1 /= get_texture_h();
218         in_x2 /= get_texture_w();  in_y2 /= get_texture_h();
219         float ot_y1 = flip_y ? -out_y1 : -out_y2;
220         float ot_y2 = flip_y ? -out_y2 : -out_y1;
221         texture->draw_texture(
222                 in_x1,in_y1,  in_x2,in_y2,
223                 out_x1,ot_y1, out_x2, ot_y2);
224 #endif
225 }
226
227 void VFrame::draw_texture(int flip_y)
228 {
229         draw_texture(0,0,  get_w(),get_h(),
230                 0,0, get_w(),get_h(), flip_y);
231 }
232
233
234 void VFrame::bind_texture(int texture_unit, int nearest)
235 {
236 // Bind the texture
237         if(texture) {
238                 texture->bind(texture_unit, nearest);
239         }
240 }
241
242
243 void VFrame::init_screen(int w, int h)
244 {
245 #ifdef HAVE_GL
246         glViewport(0, 0, w, h);
247         glMatrixMode(GL_PROJECTION);
248         glLoadIdentity();
249         float near = 1;
250         float far = 100;
251         float frustum_ratio = near / ((near + far)/2);
252         float near_h = (float)h * frustum_ratio;
253         float near_w = (float)w * frustum_ratio;
254         glFrustum(-near_w/2, near_w/2, -near_h/2, near_h/2, near, far);
255         glMatrixMode(GL_MODELVIEW);
256         glLoadIdentity();
257 // Shift down and right so 0,0 is the top left corner
258         glTranslatef(-w/2.f, h/2.f, 0.f);
259         glTranslatef(0.0, 0.0, -(far + near) / 2);
260
261         glDisable(GL_DEPTH_TEST);
262         glShadeModel(GL_SMOOTH);
263 // Default for direct copy playback
264         glDisable(GL_BLEND);
265         glDisable(GL_COLOR_MATERIAL);
266         glDisable(GL_CULL_FACE);
267         glEnable(GL_NORMALIZE);
268         glAlphaFunc(GL_ALWAYS, 0);
269         glDisable(GL_ALPHA_TEST);
270         glDisable(GL_LIGHTING);
271
272         const GLfloat zero[] = { 0, 0, 0, 0 };
273 //      const GLfloat one[] = { 1, 1, 1, 1 };
274 //      const GLfloat light_position[] = { 0, 0, -1, 0 };
275 //      const GLfloat light_direction[] = { 0, 0, 1, 0 };
276
277 //      glEnable(GL_LIGHT0);
278 //      glLightfv(GL_LIGHT0, GL_AMBIENT, zero);
279 //      glLightfv(GL_LIGHT0, GL_DIFFUSE, one);
280 //      glLightfv(GL_LIGHT0, GL_SPECULAR, one);
281 //      glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, 180);
282 //      glLightfv(GL_LIGHT0, GL_POSITION, light_position);
283 //      glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_direction);
284 //      glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
285 //      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, zero);
286         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
287         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
288         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zero);
289         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, zero);
290         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0);
291 #ifndef GLx4
292         glPixelStorei(GL_PACK_ALIGNMENT,1);
293         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
294 #endif
295 #endif
296 }
297
298 void VFrame::init_screen()
299 {
300         init_screen(get_w(), get_h());
301 }
302
303
304
305 #ifdef HAVE_GL
306
307 static int print_error(const char *text, unsigned int object, int is_program)
308 {
309         char info[BCTEXTLEN];
310         int len = 0;
311         if( is_program )
312                 glGetProgramInfoLog(object, BCTEXTLEN, &len, info);
313         else
314                 glGetShaderInfoLog(object, BCTEXTLEN, &len, info);
315         if( len > 0 ) printf("Playback3D::print_error:\n%s\n%s\n", text, info);
316         return !len ? 0 : 1;
317 }
318
319 static char *shader_segs(const char **segs, int n)
320 {
321         if( !segs || !n ) return 0;
322 // concat source segs
323         int ids = 0;
324         char *ret = 0;
325         for( int i=0; i<n; ++i ) {
326                 const char *text = *segs++;
327                 char src[strlen(text) + BCSTRLEN + 1], *sp = src;
328                 const char *tp = strstr(text, "main()");
329                 if( tp ) {
330 // Replace main() with a mainxxx()
331                         int n = tp - text;
332                         memcpy(sp, text, n);  sp += n;
333                         sp += sprintf(sp, "main%03d()", ids++);
334                         strcpy(sp, tp+strlen("main()"));
335                         text = src;
336                 }
337                 char *cp = !ret ? cstrdup(text) : cstrcat(2, ret, text);
338                 delete [] ret;  ret = cp;
339         }
340
341 // add main() which calls mainxxx() in order
342         char main_prog[BCTEXTLEN];
343         char *cp = main_prog;
344         cp += sprintf(cp, "\nvoid main() {\n");
345         for( int i=0; i < ids; ++i )
346                 cp += sprintf(cp, "\tmain%03d();\n", i);
347         cp += sprintf(cp, "}\n");
348         if( ret ) {
349                 cp = cstrcat(2, ret, main_prog);
350                 delete [] ret;  ret = cp;
351         }
352         else
353                 ret = cstrdup(main_prog);
354         return ret;
355 }
356
357 static int compile_shader(unsigned int &shader, int type, const GLchar *text)
358 {
359         shader = glCreateShader(type);
360         glShaderSource(shader, 1, &text, 0);
361         glCompileShader(shader);
362         return print_error(text, shader, 0);
363 }
364
365 static unsigned int build_shader(const char *vert, const char *frag)
366 {
367         int error = 0;
368         unsigned int vertex_shader = 0;
369         unsigned int fragment_shader = 0;
370         unsigned int program = glCreateProgram();
371         if( !error && vert )
372                 error = compile_shader(vertex_shader, GL_VERTEX_SHADER, vert);
373         if( !error && frag )
374                 error = compile_shader(fragment_shader, GL_FRAGMENT_SHADER, frag);
375         if( !error && vert ) glAttachShader(program, vertex_shader);
376         if( !error && frag ) glAttachShader(program, fragment_shader);
377         if( !error ) glLinkProgram(program);
378         if( !error ) error = print_error("link", program, 1);
379         if( !error )
380                 BC_WindowBase::get_synchronous()->put_shader(program, vert, frag);
381         else {
382                 glDeleteProgram(program);
383                 program = 0;
384         }
385         return program;
386 }
387
388 #endif
389
390 // call as:
391 //    make_shader(0, seg1, .., segn, 0);
392 // or make_shader(&seg);
393 // line 1: optional comment // vertex shader
394
395 unsigned int VFrame::make_shader(const char **segments, ...)
396 {
397         unsigned int program = 0;
398 #ifdef HAVE_GL
399 // Construct single source file out of arguments
400         int nb_segs = 0;
401         if( !segments ) {  // arg list
402                 va_list list;  va_start(list, segments);
403                 while( va_arg(list, char*) != 0 ) ++nb_segs;
404                 va_end(list);
405         }
406         else { // segment list
407                 while( segments[nb_segs] ) ++nb_segs;
408         }
409         const char *segs[++nb_segs];
410         if( !segments ) {
411                 va_list list;  va_start(list, segments);
412                 for( int i=0; i<nb_segs; ++i )
413                         segs[i] = va_arg(list, const char *);
414                 va_end(list);
415                 segments = segs;
416         }
417
418         const char *vert_shaders[nb_segs];  int vert_segs = 0;
419         const char *frag_shaders[nb_segs];  int frag_segs = 0;
420         for( int i=0; segments[i]!=0; ++i ) {
421                 const char *seg = segments[i];
422                 if( strstr(seg, "// vertex shader") )
423                         vert_shaders[vert_segs++] = seg;
424                 else
425                         frag_shaders[frag_segs++] = seg;
426         }
427
428         char *vert = shader_segs(vert_shaders, vert_segs);
429         char *frag = shader_segs(frag_shaders, frag_segs);
430         if( !BC_WindowBase::get_synchronous()->get_shader(&program, vert, frag) )
431                 program = build_shader(vert, frag);
432         delete [] vert;
433         delete [] frag;
434 #endif
435         return program;
436 }
437
438 void VFrame::dump_shader(int shader_id)
439 {
440         BC_WindowBase::get_synchronous()->dump_shader(shader_id);
441 }
442
443
444 void VFrame::clear_pbuffer()
445 {
446 #ifdef HAVE_GL
447         int rgb = clear_color>=0 ? clear_color : 0;
448         int a = clear_color>=0 ? clear_alpha : 0;
449         int r = (rgb>>16) & 0xff;
450         int g = (rgb>> 8) & 0xff;
451         int b = (rgb>> 0) & 0xff;
452         if( BC_CModels::is_yuv(get_color_model()) )
453                 YUV::yuv.rgb_to_yuv_8(r, g, b);
454         glClearColor(r/255.f, g/255.f, b/255.f, a/255.f);
455         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
456 #endif
457 }
458