]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/grgl2.cpp
fix print_screen()
[taylor/freespace2.git] / src / graphics / grgl2.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include "SDL_opengles2.h"
10
11 #include "gropengl.h"
12 #include "gropenglinternal.h"
13 #include "grgl2.h"
14 #include "2d.h"
15 #include "mouse.h"
16 #include "pstypes.h"
17 #include "bmpman.h"
18 #include "grinternal.h"
19 #include "osregistry.h"
20 #include "cfile.h"
21
22
23 int GL_two_inited = 0;
24
25 bool Use_mipmaps = false;
26
27 static GLuint FB_texture = 0;
28 static GLuint FB_id = 0;
29 static GLuint FB_rb_id = 0;
30
31 static GLuint GL_saved_screen_tex = 0;
32 static GLuint GL_stream_tex = 0;
33
34
35 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
36 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
37
38 void opengl2_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
39 {
40         opengl2_set_texture_state(ts);
41
42         if (ab != GL_current_alpha_blend) {
43                 switch (ab) {
44                         case ALPHA_BLEND_NONE:                  // 1*SrcPixel + 0*DestPixel
45                                 glBlendFunc(GL_ONE, GL_ZERO);
46                                 break;
47                         case ALPHA_BLEND_ADDITIVE:              // 1*SrcPixel + 1*DestPixel
48                                 glBlendFunc(GL_ONE, GL_ONE);
49                                 break;
50                         case ALPHA_BLEND_ALPHA_ADDITIVE:        // Alpha*SrcPixel + 1*DestPixel
51                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
52                                 break;
53                         case ALPHA_BLEND_ALPHA_BLEND_ALPHA:     // Alpha*SrcPixel + (1-Alpha)*DestPixel
54                                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
55                                 break;
56                         case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
57                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
58                                 break;
59                         default:
60                                 break;
61                 }
62
63                 GL_current_alpha_blend = ab;
64         }
65
66         if (zt != GL_current_zbuffer_type) {
67                 switch (zt) {
68                         case ZBUFFER_TYPE_NONE:
69                                 glDepthFunc(GL_ALWAYS);
70                                 glDepthMask(GL_FALSE);
71                                 break;
72                         case ZBUFFER_TYPE_READ:
73                                 glDepthFunc(GL_LESS);
74                                 glDepthMask(GL_FALSE);
75                                 break;
76                         case ZBUFFER_TYPE_WRITE:
77                                 glDepthFunc(GL_ALWAYS);
78                                 glDepthMask(GL_TRUE);
79                                 break;
80                         case ZBUFFER_TYPE_FULL:
81                                 glDepthFunc(GL_LESS);
82                                 glDepthMask(GL_TRUE);
83                                 break;
84                         default:
85                                 break;
86                 }
87
88                 GL_current_zbuffer_type = zt;
89         }
90 }
91
92 static void opengl2_init_func_pointers()
93 {
94         gr_screen.gf_flip = gr_opengl2_flip;
95         gr_screen.gf_set_clip = gr_opengl2_set_clip;
96         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
97
98         gr_screen.gf_clear = gr_opengl_clear;
99
100         gr_screen.gf_aabitmap = gr_opengl2_aabitmap;
101         gr_screen.gf_aabitmap_ex = gr_opengl2_aabitmap_ex;
102
103         gr_screen.gf_rect = gr_opengl2_rect;
104         gr_screen.gf_shade = gr_opengl2_shade;
105         gr_screen.gf_string = gr_opengl2_string;
106         gr_screen.gf_circle = gr_opengl2_circle;
107
108         gr_screen.gf_line = gr_opengl2_line;
109         gr_screen.gf_aaline = gr_opengl2_aaline;
110         gr_screen.gf_pixel = gr_opengl2_pixel;
111         gr_screen.gf_scaler = gr_opengl2_scaler;
112         gr_screen.gf_tmapper = gr_opengl2_tmapper;
113
114         gr_screen.gf_gradient = gr_opengl2_gradient;
115
116         gr_screen.gf_print_screen = gr_opengl2_print_screen;
117
118         gr_screen.gf_fade_in = gr_opengl2_fade_in;
119         gr_screen.gf_fade_out = gr_opengl2_fade_out;
120         gr_screen.gf_flash = gr_opengl2_flash;
121
122         gr_screen.gf_zbuffer_clear = gr_opengl2_zbuffer_clear;
123
124         gr_screen.gf_save_screen = gr_opengl2_save_screen;
125         gr_screen.gf_restore_screen = gr_opengl2_restore_screen;
126         gr_screen.gf_free_screen = gr_opengl2_free_screen;
127
128         gr_screen.gf_dump_frame_start = gr_opengl2_dump_frame_start;
129         gr_screen.gf_dump_frame_stop = gr_opengl2_dump_frame_stop;
130         gr_screen.gf_dump_frame = gr_opengl2_dump_frame;
131
132         gr_screen.gf_stream_start = gr_opengl2_stream_start;
133         gr_screen.gf_stream_frame = gr_opengl2_stream_frame;
134         gr_screen.gf_stream_stop = gr_opengl2_stream_stop;
135
136         gr_screen.gf_set_gamma = gr_opengl2_set_gamma;
137
138         gr_screen.gf_lock = gr_opengl_lock;
139         gr_screen.gf_unlock = gr_opengl_unlock;
140
141         gr_screen.gf_fog_set = gr_opengl2_fog_set;
142
143         gr_screen.gf_get_region = gr_opengl2_get_region;
144
145         gr_screen.gf_set_cull = gr_opengl_set_cull;
146
147         gr_screen.gf_cross_fade = gr_opengl2_cross_fade;
148
149         gr_screen.gf_preload_init = gr_opengl2_preload_init;
150         gr_screen.gf_preload = gr_opengl2_preload;
151
152         gr_screen.gf_zbias = gr_opengl_zbias;
153
154         gr_screen.gf_set_viewport = gr_opengl2_set_viewport;
155
156         gr_screen.gf_activate = gr_opengl_activate;
157
158         gr_screen.gf_release_texture = gr_opengl2_release_texture;
159 }
160
161 static int opengl2_create_framebuffer()
162 {
163         // create texture
164         glGenTextures(1, &FB_texture);
165         glBindTexture(GL_TEXTURE_2D, FB_texture);
166
167         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
168         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
169         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
170         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
171
172         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gr_screen.max_w, gr_screen.max_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
173
174         glBindTexture(GL_TEXTURE_2D, 0);
175
176         // create renderbuffer
177         glGenRenderbuffers(1, &FB_rb_id);
178         glBindRenderbuffer(GL_RENDERBUFFER, FB_rb_id);
179
180         glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, gr_screen.max_w, gr_screen.max_h);
181
182         // create framebuffer
183         glGenFramebuffers(1, &FB_id);
184         glBindFramebuffer(GL_FRAMEBUFFER, FB_id);
185
186         // attach texture and renderbuffer
187         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FB_texture, 0);
188         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, FB_rb_id);
189
190         GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
191
192         glBindFramebuffer(GL_FRAMEBUFFER, 0);
193
194         if (status != GL_FRAMEBUFFER_COMPLETE) {
195                 if (FB_texture) {
196                         glDeleteTextures(1, &FB_texture);
197                         FB_texture = 0;
198                 }
199
200                 if (FB_rb_id) {
201                         glDeleteRenderbuffers(1, &FB_rb_id);
202                         FB_rb_id = 0;
203                 }
204
205                 if (FB_id) {
206                         glDeleteFramebuffers(1, &FB_id);
207                         FB_id = 0;
208                 }
209
210                 return 0;
211         }
212
213         return 1;
214 }
215
216 void opengl2_cleanup()
217 {
218         if ( !GL_two_inited ) {
219                 return;
220         }
221
222         glBindFramebuffer(GL_FRAMEBUFFER, 0);
223
224         if (FB_texture) {
225                 glDeleteTextures(1, &FB_texture);
226                 FB_texture = 0;
227         }
228
229         if (FB_rb_id) {
230                 glDeleteRenderbuffers(1, &FB_rb_id);
231                 FB_rb_id = 0;
232         }
233
234         if (FB_id) {
235                 glDeleteFramebuffers(1, &FB_id);
236                 FB_id = 0;
237         }
238
239         opengl2_tcache_cleanup();
240         opengl2_shader_cleanup();
241
242         if (GL_context) {
243                 SDL_GL_DeleteContext(GL_context);
244                 GL_context = NULL;
245         }
246
247         GL_two_inited = 0;
248 }
249
250 int opengl2_init()
251 {
252         if (GL_two_inited) {
253                 return 1;
254         }
255
256         GL_two_inited = 1;
257
258         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
259         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
260         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
261
262         GL_context = SDL_GL_CreateContext(GL_window);
263
264         if ( !GL_context ) {
265                 opengl2_cleanup();
266                 return 0;
267         }
268
269         mprintf(("  Vendor   : %s\n", glGetString(GL_VENDOR)));
270         mprintf(("  Renderer : %s\n", glGetString(GL_RENDERER)));
271         mprintf(("  Version  : %s\n", glGetString(GL_VERSION)));
272
273         // initial viewport setup
274         gr_opengl2_set_viewport(gr_screen.max_w, gr_screen.max_h);
275
276         // set up generic variables
277         opengl_set_variables();
278
279         opengl2_init_func_pointers();
280         opengl2_tcache_init();
281
282         if ( !opengl2_shader_init() ) {
283                 opengl2_cleanup();
284                 return 0;
285         }
286
287         if ( !opengl2_create_framebuffer() ) {
288                 opengl2_cleanup();
289                 return 0;
290         }
291
292         glEnable(GL_DITHER);
293         glEnable(GL_DEPTH_TEST);
294         glEnable(GL_BLEND);
295         glEnable(GL_TEXTURE_2D);
296
297         glDepthRangef(0.0f, 1.0f);
298
299         glPixelStorei(GL_PACK_ALIGNMENT, 1);
300         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
301
302         if ( SDL_GL_ExtensionSupported("GL_OES_texture_npot") ) {
303                 Use_mipmaps     = true;
304         }
305
306         mprintf(("  Mipmaps  : %s\n", Use_mipmaps ? "Enabled" : "Disabled"));
307
308         glFlush();
309
310         gr_opengl_clear();
311         gr_opengl_set_cull(1);
312
313         return 1;
314 }
315
316 void gr_opengl2_flip()
317 {
318         if ( !GL_two_inited ) {
319                 return;
320         }
321
322         glBindFramebuffer(GL_FRAMEBUFFER, 0);
323
324         gr_opengl_reset_clip();
325
326         // set viewport to window size
327         glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h);
328
329         glClear(GL_COLOR_BUFFER_BIT);
330
331         {
332                 float x = 0.0f;
333                 float y = 0.0f;
334                 float w = i2fl(GL_viewport_w);
335                 float h = i2fl(GL_viewport_h);
336
337                 const float tex_coord[] = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f };
338                 const float ver_coord[] = { x, y, x, h, w, y, w, h };
339
340                 opengl2_shader_use(PROG_WINDOW);
341
342                 glEnableVertexAttribArray(SDRI_POSITION);
343                 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &ver_coord);
344
345                 glEnableVertexAttribArray(SDRI_TEXCOORD);
346                 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, &tex_coord);
347
348                 glBindTexture(GL_TEXTURE_2D, FB_texture);
349
350                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
351
352                 glBindTexture(GL_TEXTURE_2D, 0);
353
354                 glDisableVertexAttribArray(SDRI_TEXCOORD);
355                 glDisableVertexAttribArray(SDRI_POSITION);
356         }
357
358         mouse_eval_deltas();
359
360         if ( mouse_is_visible() ) {
361                 int mx, my;
362
363                 mouse_get_pos(&mx, &my);
364
365                 if ( opengl2_tcache_set(Gr_cursor, TCACHE_TYPE_BITMAP_INTERFACE) ) {
366                         opengl2_set_state(TEXTURE_SOURCE_DECAL, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE);
367
368                         int bw, bh;
369                         bm_get_info(Gr_cursor, &bw, &bh);
370
371                         float x = i2fl(mx) * GL_viewport_scale_w;
372                         float y = i2fl(my) * GL_viewport_scale_h;
373                         float w = x + (bw * GL_viewport_scale_w);
374                         float h = y + (bh * GL_viewport_scale_h);
375
376                         const float tex_coord[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
377                         const float ver_coord[] = { x, y, x, h, w, y, w, h };
378
379                         opengl2_shader_use(PROG_WINDOW);
380
381                         glEnableVertexAttribArray(SDRI_POSITION);
382                         glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &ver_coord);
383
384                         glEnableVertexAttribArray(SDRI_TEXCOORD);
385                         glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, &tex_coord);
386
387                         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
388
389                         glDisableVertexAttribArray(SDRI_TEXCOORD);
390                         glDisableVertexAttribArray(SDRI_POSITION);
391                 }
392 #ifndef NDEBUG
393                 else {
394                         gr_set_color(255,255,255);
395                         gr_opengl2_line(mx, my, mx+7, my + 7);
396                         gr_opengl2_line(mx, my, mx+5, my );
397                         gr_opengl2_line(mx, my, mx, my+5);
398                 }
399 #endif
400         }
401
402 #ifndef NDEBUG
403         GLenum error = glGetError();
404
405         if (error != GL_NO_ERROR) {
406                 mprintf(("!!DEBUG!! OpenGL Error: %d\n", error));
407         }
408 #endif
409
410         SDL_GL_SwapWindow(GL_window);
411
412         opengl2_tcache_frame();
413
414         glBindFramebuffer(GL_FRAMEBUFFER, FB_id);
415
416         // set viewport to game screen size
417         glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
418 }
419
420 void gr_opengl2_set_clip(int x, int y, int w, int h)
421 {
422         // check for sanity of parameters
423         CAP(x, 0, gr_screen.max_w - 1);
424         CAP(y, 0, gr_screen.max_h - 1);
425         CAP(w, 0, gr_screen.max_w - x);
426         CAP(h, 0, gr_screen.max_h - y);
427
428         gr_screen.offset_x = x;
429         gr_screen.offset_y = y;
430         gr_screen.clip_left = 0;
431         gr_screen.clip_right = w-1;
432         gr_screen.clip_top = 0;
433         gr_screen.clip_bottom = h-1;
434         gr_screen.clip_width = w;
435         gr_screen.clip_height = h;
436
437         glEnable(GL_SCISSOR_TEST);
438         glScissor(x, gr_screen.max_h-y-h, w, h);
439 }
440
441 void gr_opengl2_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
442 {
443         gr_screen.current_fog_mode = fog_mode;
444
445         if (fog_mode == GR_FOGMODE_NONE) {
446                 return;
447         }
448
449         gr_screen.fog_near = fog_near;
450         gr_screen.fog_far = fog_far;
451
452         gr_init_color(&gr_screen.current_fog_color, r, g, b);
453 }
454
455 void gr_opengl2_zbuffer_clear(int mode)
456 {
457         if (mode) {
458                 Gr_zbuffering = 1;
459                 Gr_zbuffering_mode = GR_ZBUFF_FULL;
460                 Gr_global_zbuffering = 1;
461
462                 opengl2_set_state(TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL);
463                 glClear(GL_DEPTH_BUFFER_BIT);
464         } else {
465                 Gr_zbuffering = 0;
466                 Gr_zbuffering_mode = GR_ZBUFF_NONE;
467                 Gr_global_zbuffering = 0;
468         }
469 }
470
471 void gr_opengl2_print_screen(const char *filename)
472 {
473         char tmp[MAX_FILENAME_LEN];
474         ubyte *buf = NULL;
475
476         SDL_strlcpy( tmp, filename, SDL_arraysize(tmp) );
477         SDL_strlcat( tmp, NOX(".tga"), SDL_arraysize(tmp) );
478
479         int b_size = gr_screen.max_w * gr_screen.max_w;
480
481         buf = (ubyte*)malloc(b_size * 4);
482
483         if (buf == NULL) {
484                 return;
485         }
486
487         CFILE *f = cfopen(tmp, "wb", CFILE_NORMAL, CF_TYPE_ROOT);
488
489         if (f == NULL) {
490                 free(buf);
491                 return;
492         }
493
494         // Write the TGA header
495         cfwrite_ubyte( 0, f );  //      IDLength;
496         cfwrite_ubyte( 0, f );  //      ColorMapType;
497         cfwrite_ubyte( 2, f );  //      ImageType;              // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
498         cfwrite_ushort( 0, f ); // CMapStart;
499         cfwrite_ushort( 0, f ); //      CMapLength;
500         cfwrite_ubyte( 0, f );  // CMapDepth;
501         cfwrite_ushort( 0, f ); //      XOffset;
502         cfwrite_ushort( 0, f ); //      YOffset;
503         cfwrite_ushort( (ushort)gr_screen.max_w, f );   //      Width;
504         cfwrite_ushort( (ushort)gr_screen.max_h, f );   //      Height;
505         cfwrite_ubyte( 24, f ); //PixelDepth;
506         cfwrite_ubyte( 0, f );  //ImageDesc;
507
508         memset(buf, 0, b_size * 4);
509
510         glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_RGBA, GL_UNSIGNED_BYTE, buf);
511
512         int b_offset = 0;
513
514         // convert from rgba to bgr
515         for (int i = 0; i < (b_size * 4); i += 4) {
516                 ubyte r = buf[i];
517                 ubyte g = buf[i+1];
518                 ubyte b = buf[i+2];
519
520                 buf[b_offset++] = b;
521                 buf[b_offset++] = g;
522                 buf[b_offset++] = r;
523         }
524
525         cfwrite(buf, b_size * 3, 1, f);
526
527         cfclose(f);
528
529         free(buf);
530 }
531
532 void gr_opengl2_fade_in(int instantaneous)
533 {
534
535 }
536
537 void gr_opengl2_fade_out(int instantaneous)
538 {
539
540 }
541
542 void gr_opengl2_get_region(int front, int w, int h, ubyte *data)
543 {
544         opengl2_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
545
546         GLenum pxtype = GL_UNSIGNED_SHORT_5_5_5_1;
547
548         if (gr_screen.bytes_per_pixel == 4) {
549                 pxtype = GL_UNSIGNED_BYTE;
550         }
551
552         glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, pxtype, data);
553 }
554
555 int gr_opengl2_save_screen()
556 {
557         gr_opengl_reset_clip();
558
559         if (GL_saved_screen_tex) {
560                 mprintf(( "Screen already saved!\n" ));
561                 return -1;
562         }
563
564         glGenTextures(1, &GL_saved_screen_tex);
565
566         if ( !GL_saved_screen_tex ) {
567                 mprintf(( "Couldn't create texture for saved screen!\n" ));
568                 return -1;
569         }
570
571         glBindTexture(GL_TEXTURE_2D, GL_saved_screen_tex);
572
573         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
574         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
575         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
576         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
577
578         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0,
579                         gr_screen.max_w, gr_screen.max_h, 0);
580
581         glBindTexture(GL_TEXTURE_2D, 0);
582
583         return 0;
584 }
585
586 void gr_opengl2_restore_screen(int)
587 {
588         gr_opengl_reset_clip();
589
590         if ( !GL_saved_screen_tex ) {
591                 gr_opengl_clear();
592                 return;
593         }
594
595         float x = 0.0f;
596         float y = 0.0f;
597         float w = i2fl(gr_screen.max_w);
598         float h = i2fl(gr_screen.max_h);
599
600         const float tex_coord[] = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f };   // y-flipped
601         const float ver_coord[] = { x, y, x, h, w, y, w, h };
602
603         opengl2_shader_use(PROG_TEX);
604
605         glVertexAttrib4f(SDRI_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
606
607         glEnableVertexAttribArray(SDRI_POSITION);
608         glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &ver_coord);
609
610         glEnableVertexAttribArray(SDRI_TEXCOORD);
611         glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, &tex_coord);
612
613         glBindTexture(GL_TEXTURE_2D, GL_saved_screen_tex);
614
615         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
616
617         glBindTexture(GL_TEXTURE_2D, 0);
618
619         glDisableVertexAttribArray(SDRI_TEXCOORD);
620         glDisableVertexAttribArray(SDRI_POSITION);
621 }
622
623 void gr_opengl2_free_screen(int)
624 {
625         if (GL_saved_screen_tex) {
626                 glDeleteTextures(1, &GL_saved_screen_tex);
627                 GL_saved_screen_tex = 0;
628         }
629 }
630
631 void gr_opengl2_dump_frame_start(int first_frame, int frames_between_dumps)
632 {
633
634 }
635
636 void gr_opengl2_dump_frame_stop()
637 {
638
639 }
640
641 void gr_opengl2_dump_frame()
642 {
643
644 }
645
646 static int GL_stream_w = 0;
647 static int GL_stream_h = 0;
648
649 static rb_t GL_stream[4];
650
651 void gr_opengl2_stream_start(int x, int y, int w, int h)
652 {
653         if (GL_stream_tex) {
654                 return;
655         }
656
657         glGenTextures(1, &GL_stream_tex);
658
659         glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
660
661         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
662         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
663         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
664         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
665
666         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, NULL);
667
668         glBindTexture(GL_TEXTURE_2D, 0);
669
670         uint scale = os_config_read_uint("Video", "ScaleMovies", 1);
671
672         int sx, sy;
673         int sw, sh;
674
675         if (x < 0) {
676                 sx = scale ? 0 : ((gr_screen.max_w - w) / 2);
677         } else {
678                 sx = x;
679         }
680
681         float h_factor = scale ? (gr_screen.max_w / i2fl(w)) : 1.0f;
682
683         if (y < 0) {
684                 sy = (gr_screen.max_h - fl2i(h * h_factor)) / 2;
685         } else {
686                 sy = y;
687         }
688
689         GL_stream_w = w;
690         GL_stream_h = h;
691
692         if (scale) {
693                 sw = gr_screen.max_w - (sx * 2);
694                 sh = gr_screen.max_h - (sy * 2);
695         } else {
696                 sw = w;
697                 sh = h;
698         }
699
700         GL_stream[0].x = i2fl(sx);
701         GL_stream[0].y = i2fl(sy);
702         GL_stream[0].u = 0.0f;
703         GL_stream[0].v = 0.0f;
704
705         GL_stream[1].x = i2fl(sx);
706         GL_stream[1].y = i2fl(sy + sh);
707         GL_stream[1].u = 0.0f;
708         GL_stream[1].v = 1.0f;
709
710         GL_stream[2].x = i2fl(sx + sw);
711         GL_stream[2].y = i2fl(sy);
712         GL_stream[2].u = 1.0f;
713         GL_stream[2].v = 0.0f;
714
715         GL_stream[3].x = i2fl(sx + sw);
716         GL_stream[3].y = i2fl(sy + sh);
717         GL_stream[3].u = 1.0f;
718         GL_stream[3].v = 1.0f;
719
720         glDisable(GL_DEPTH_TEST);
721 }
722
723 void gr_opengl2_stream_frame(ubyte *frame)
724 {
725         if ( !GL_stream_tex ) {
726                 return;
727         }
728
729         opengl2_shader_use(PROG_TEX);
730
731         glVertexAttrib4f(SDRI_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
732
733         glEnableVertexAttribArray(SDRI_POSITION);
734         glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &GL_stream[0].x);
735
736         glEnableVertexAttribArray(SDRI_TEXCOORD);
737         glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &GL_stream[0].u);
738
739         glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
740
741         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GL_stream_w, GL_stream_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, frame);
742
743         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
744
745         glBindTexture(GL_TEXTURE_2D, 0);
746
747         glDisableVertexAttribArray(SDRI_TEXCOORD);
748         glDisableVertexAttribArray(SDRI_POSITION);
749 }
750
751 void gr_opengl2_stream_stop()
752 {
753         if (GL_stream_tex) {
754                 glBindTexture(GL_TEXTURE_2D, 0);
755                 glDeleteTextures(1, &GL_stream_tex);
756                 GL_stream_tex = 0;
757
758                 glEnable(GL_DEPTH_TEST);
759         }
760 }
761
762 void gr_opengl2_set_viewport(int width, int height)
763 {
764         int w, h, x, y;
765
766         float ratio = gr_screen.max_w / i2fl(gr_screen.max_h);
767
768         w = width;
769         h = fl2i((width / ratio) + 0.5f);
770
771         if (h > height) {
772                 h = height;
773                 w = fl2i((height * ratio) + 0.5f);
774         }
775
776         x = (width - w) / 2;
777         y = (height - h) / 2;
778
779         GL_viewport_x = x;
780         GL_viewport_y = y;
781         GL_viewport_w = w;
782         GL_viewport_h = h;
783         GL_viewport_scale_w = w / i2fl(gr_screen.max_w);
784         GL_viewport_scale_h = h / i2fl(gr_screen.max_h);
785
786         opengl2_shader_update();
787 }