4 * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #define GL_GLEXT_PROTOTYPES
24 #include "bcpbuffer.h"
25 #include "bcresources.h"
26 #include "bcsignals.h"
27 #include "bcsynchronous.h"
28 #include "bctexture.h"
29 #include "bcwindowbase.h"
42 int VFrame::get_opengl_state()
47 void VFrame::set_opengl_state(int value)
52 int VFrame::get_window_id()
54 return texture ? texture->window_id : -1;
57 int VFrame::get_texture_id()
59 return texture ? texture->texture_id : -1;
62 int VFrame::get_texture_w()
64 return texture ? texture->texture_w : 0;
67 int VFrame::get_texture_h()
69 return texture ? texture->texture_h : 0;
73 int VFrame::get_texture_components()
75 return texture ? texture->texture_components : 0;
88 void VFrame::to_texture()
92 // Must be here so user can create textures without copying data by setting
93 // opengl_state to TEXTURE.
94 BC_Texture::new_texture(&texture, get_w(), get_h(), get_color_model());
96 // Determine what to do based on state
97 switch(opengl_state) {
106 opengl_state = VFrame::TEXTURE;
110 //printf("VFrame::to_texture %d\n", texture_id);
112 switch(color_model) {
115 type = GL_UNSIGNED_BYTE;
121 type = GL_UNSIGNED_BYTE;
137 "VFrame::to_texture: unsupported color model %d.\n",
142 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, get_w(), get_h(),
143 format, type, get_rows()[0]);
144 opengl_state = VFrame::TEXTURE;
148 void VFrame::create_pbuffer()
150 int ww = (get_w()+3) & ~3, hh = (get_h()+3) & ~3;
151 if( pbuffer && (pbuffer->w != ww || pbuffer->h != hh ||
152 pbuffer->window_id != BC_WindowBase::get_synchronous()->current_window->get_id() ) ) {
158 pbuffer = new BC_PBuffer(ww, hh);
162 void VFrame::enable_opengl()
166 pbuffer->enable_opengl();
170 BC_PBuffer* VFrame::get_pbuffer()
176 void VFrame::screen_to_texture(int x, int y, int w, int h)
180 BC_Texture::new_texture(&texture,
181 get_w(), get_h(), get_color_model());
183 glEnable(GL_TEXTURE_2D);
185 // Read canvas into texture, use back texture for DOUBLE_BUFFER
187 // According to the man page, it must be GL_BACK for the onscreen buffer
188 // and GL_FRONT for a single buffered PBuffer. In reality it must be
189 // GL_BACK for a single buffered PBuffer if the PBuffer has alpha and using
190 // GL_FRONT captures the onscreen front buffer.
191 // 10/11/2010 is now generating "illegal operation"
192 glReadBuffer(GL_BACK);
194 glReadBuffer(BC_WindowBase::get_synchronous()->is_pbuffer ?
197 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
198 x >= 0 ? x : 0, y >= 0 ? y : 0,
199 w >= 0 ? w : get_w(), h >= 0 ? h : get_h());
204 void VFrame::screen_to_ram()
208 glReadBuffer(GL_BACK);
209 int type = BC_CModels::is_float(color_model) ? GL_FLOAT : GL_UNSIGNED_BYTE;
210 int format = BC_CModels::has_alpha(color_model) ? GL_RGBA : GL_RGB;
211 glReadPixels(0, 0, get_w(), get_h(), format, type, get_rows()[0]);
212 opengl_state = VFrame::RAM;
216 void VFrame::draw_texture(
217 float in_x1, float in_y1, float in_x2, float in_y2,
218 float out_x1, float out_y1, float out_x2, float out_y2,
222 in_x1 /= get_texture_w(); in_y1 /= get_texture_h();
223 in_x2 /= get_texture_w(); in_y2 /= get_texture_h();
224 float ot_y1 = flip_y ? -out_y1 : -out_y2;
225 float ot_y2 = flip_y ? -out_y2 : -out_y1;
226 texture->draw_texture(
227 in_x1,in_y1, in_x2,in_y2,
228 out_x1,ot_y1, out_x2, ot_y2);
232 void VFrame::draw_texture(int flip_y)
234 draw_texture(0,0, get_w(),get_h(),
235 0,0, get_w(),get_h(), flip_y);
239 void VFrame::bind_texture(int texture_unit, int nearest)
243 texture->bind(texture_unit, nearest);
248 void VFrame::init_screen(int w, int h)
251 glViewport(0, 0, w, h);
252 glMatrixMode(GL_PROJECTION);
256 float frustum_ratio = near / ((near + far)/2);
257 float near_h = (float)h * frustum_ratio;
258 float near_w = (float)w * frustum_ratio;
259 glFrustum(-near_w/2, near_w/2, -near_h/2, near_h/2, near, far);
260 glMatrixMode(GL_MODELVIEW);
262 // Shift down and right so 0,0 is the top left corner
263 glTranslatef(-w/2.f, h/2.f, 0.f);
264 glTranslatef(0.0, 0.0, -(far + near) / 2);
266 glDisable(GL_DEPTH_TEST);
267 glShadeModel(GL_SMOOTH);
268 // Default for direct copy playback
270 glDisable(GL_COLOR_MATERIAL);
271 glDisable(GL_CULL_FACE);
272 glEnable(GL_NORMALIZE);
273 glAlphaFunc(GL_ALWAYS, 0);
274 glDisable(GL_ALPHA_TEST);
275 glDisable(GL_LIGHTING);
277 const GLfloat zero[] = { 0, 0, 0, 0 };
278 // const GLfloat one[] = { 1, 1, 1, 1 };
279 // const GLfloat light_position[] = { 0, 0, -1, 0 };
280 // const GLfloat light_direction[] = { 0, 0, 1, 0 };
282 // glEnable(GL_LIGHT0);
283 // glLightfv(GL_LIGHT0, GL_AMBIENT, zero);
284 // glLightfv(GL_LIGHT0, GL_DIFFUSE, one);
285 // glLightfv(GL_LIGHT0, GL_SPECULAR, one);
286 // glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, 180);
287 // glLightfv(GL_LIGHT0, GL_POSITION, light_position);
288 // glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_direction);
289 // glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
290 // glLightModelfv(GL_LIGHT_MODEL_AMBIENT, zero);
291 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
292 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
293 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zero);
294 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, zero);
295 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0);
299 void VFrame::init_screen()
301 init_screen(get_w(), get_h());
308 static int print_error(const char *text, unsigned int object, int is_program)
310 char info[BCTEXTLEN];
313 glGetProgramInfoLog(object, BCTEXTLEN, &len, info);
315 glGetShaderInfoLog(object, BCTEXTLEN, &len, info);
316 if( len > 0 ) printf("Playback3D::print_error:\n%s\n%s\n", text, info);
320 static char *shader_segs(const char **segs, int n)
322 if( !segs || !n ) return 0;
323 // concat source segs
326 for( int i=0; i<n; ++i ) {
327 const char *text = *segs++;
328 char src[strlen(text) + BCSTRLEN + 1], *sp = src;
329 const char *tp = strstr(text, "main()");
331 // Replace main() with a mainxxx()
333 memcpy(sp, text, n); sp += n;
334 sp += sprintf(sp, "main%03d()", ids++);
335 strcpy(sp, tp+strlen("main()"));
338 char *cp = !ret ? cstrdup(text) : cstrcat(2, ret, text);
339 delete [] ret; ret = cp;
342 // add main() which calls mainxxx() in order
343 char main_prog[BCTEXTLEN];
344 char *cp = main_prog;
345 cp += sprintf(cp, "\nvoid main() {\n");
346 for( int i=0; i < ids; ++i )
347 cp += sprintf(cp, "\tmain%03d();\n", i);
348 cp += sprintf(cp, "}\n");
350 cp = cstrcat(2, ret, main_prog);
351 delete [] ret; ret = cp;
354 ret = cstrdup(main_prog);
358 static int compile_shader(unsigned int &shader, int type, const GLchar *text)
360 shader = glCreateShader(type);
361 glShaderSource(shader, 1, &text, 0);
362 glCompileShader(shader);
363 return print_error(text, shader, 0);
366 static unsigned int build_shader(const char *vert, const char *frag)
369 unsigned int vertex_shader = 0;
370 unsigned int fragment_shader = 0;
371 unsigned int program = glCreateProgram();
373 error = compile_shader(vertex_shader, GL_VERTEX_SHADER, vert);
375 error = compile_shader(fragment_shader, GL_FRAGMENT_SHADER, frag);
376 if( !error && vert ) glAttachShader(program, vertex_shader);
377 if( !error && frag ) glAttachShader(program, fragment_shader);
378 if( !error ) glLinkProgram(program);
379 if( !error ) error = print_error("link", program, 1);
381 BC_WindowBase::get_synchronous()->put_shader(program, vert, frag);
383 glDeleteProgram(program);
392 // make_shader(0, seg1, .., segn, 0);
393 // or make_shader(&seg);
394 // line 1: optional comment // vertex shader
396 unsigned int VFrame::make_shader(const char **segments, ...)
398 unsigned int program = 0;
400 // Construct single source file out of arguments
402 if( !segments ) { // arg list
403 va_list list; va_start(list, segments);
404 while( va_arg(list, char*) != 0 ) ++nb_segs;
407 else { // segment list
408 while( segments[nb_segs] ) ++nb_segs;
410 const char *segs[++nb_segs];
412 va_list list; va_start(list, segments);
413 for( int i=0; i<nb_segs; ++i )
414 segs[i] = va_arg(list, const char *);
419 const char *vert_shaders[nb_segs]; int vert_segs = 0;
420 const char *frag_shaders[nb_segs]; int frag_segs = 0;
421 for( int i=0; segments[i]!=0; ++i ) {
422 const char *seg = segments[i];
423 if( strstr(seg, "// vertex shader") )
424 vert_shaders[vert_segs++] = seg;
426 frag_shaders[frag_segs++] = seg;
429 char *vert = shader_segs(vert_shaders, vert_segs);
430 char *frag = shader_segs(frag_shaders, frag_segs);
431 if( !BC_WindowBase::get_synchronous()->get_shader(&program, vert, frag) )
432 program = build_shader(vert, frag);
439 void VFrame::dump_shader(int shader_id)
441 BC_WindowBase::get_synchronous()->dump_shader(shader_id);
445 void VFrame::clear_pbuffer()
448 int rgb = clear_color>=0 ? clear_color : 0;
449 int a = clear_color>=0 ? clear_alpha : 0;
450 int r = (rgb>>16) & 0xff;
451 int g = (rgb>> 8) & 0xff;
452 int b = (rgb>> 0) & 0xff;
453 if( BC_CModels::is_yuv(get_color_model()) )
454 YUV::yuv.rgb_to_yuv_8(r, g, b);
455 glClearColor(r/255.f, g/255.f, b/255.f, a/255.f);
456 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);