]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/gropengl1.cpp
resolve rebase/merge conflicts
[taylor/freespace2.git] / src / graphics / gropengl1.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 "gropengl.h"
10 #include "gropengl1.h"
11 #include "gropenglinternal.h"
12 #include "2d.h"
13 #include "mouse.h"
14 #include "pstypes.h"
15 #include "cfile.h"
16 #include "bmpman.h"
17 #include "grinternal.h"
18
19
20 int OGL_fog_mode = 0;
21
22 static int GL_one_inited = 0;
23
24
25 volatile int GL_activate = 0;
26 volatile int GL_deactivate = 0;
27
28 static GLuint Gr_saved_screen_tex = 0;
29
30 static int Gr_opengl_mouse_saved = 0;
31 static int Gr_opengl_mouse_saved_x = 0;
32 static int Gr_opengl_mouse_saved_y = 0;
33 static int Gr_opengl_mouse_saved_w = 0;
34 static int Gr_opengl_mouse_saved_h = 0;
35 static ubyte *Gr_opengl_mouse_saved_data = NULL;
36
37
38 PFNGLSECONDARYCOLORPOINTERPROC vglSecondaryColorPointer = NULL;
39
40
41 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
42 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
43
44 void opengl1_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
45 {
46         opengl1_set_texture_state(ts);
47
48         if (ab != GL_current_alpha_blend) {
49                 switch (ab) {
50                         case ALPHA_BLEND_NONE:                  // 1*SrcPixel + 0*DestPixel
51                                 glBlendFunc(GL_ONE, GL_ZERO);
52                                 break;
53                         case ALPHA_BLEND_ADDITIVE:              // 1*SrcPixel + 1*DestPixel
54                                 glBlendFunc(GL_ONE, GL_ONE);
55                                 break;
56                         case ALPHA_BLEND_ALPHA_ADDITIVE:        // Alpha*SrcPixel + 1*DestPixel
57                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
58                                 break;
59                         case ALPHA_BLEND_ALPHA_BLEND_ALPHA:     // Alpha*SrcPixel + (1-Alpha)*DestPixel
60                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
61                                 break;
62                         case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
63                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
64                                 break;
65                         default:
66                                 break;
67                 }
68
69                 GL_current_alpha_blend = ab;
70         }
71
72         if (zt != GL_current_zbuffer_type) {
73                 switch (zt) {
74                         case ZBUFFER_TYPE_NONE:
75                                 glDepthFunc(GL_ALWAYS);
76                                 glDepthMask(GL_FALSE);
77                                 break;
78                         case ZBUFFER_TYPE_READ:
79                                 glDepthFunc(GL_LESS);
80                                 glDepthMask(GL_FALSE);
81                                 break;
82                         case ZBUFFER_TYPE_WRITE:
83                                 glDepthFunc(GL_ALWAYS);
84                                 glDepthMask(GL_TRUE);
85                                 break;
86                         case ZBUFFER_TYPE_FULL:
87                                 glDepthFunc(GL_LESS);
88                                 glDepthMask(GL_TRUE);
89                                 break;
90                         default:
91                                 break;
92                 }
93
94                 GL_current_zbuffer_type = zt;
95         }
96 }
97
98 void opengl1_cleanup()
99 {
100         if ( !GL_one_inited ) {
101                 return;
102         }
103
104         gr_opengl1_reset_clip();
105         gr_opengl1_clear();
106         gr_opengl1_flip();
107
108         opengl1_tcache_cleanup();
109
110         GL_one_inited = 0;
111 }
112
113 static void opengl1_init_func_pointers()
114 {
115         gr_screen.gf_flip = gr_opengl1_flip;
116         gr_screen.gf_set_clip = gr_opengl1_set_clip;
117         gr_screen.gf_reset_clip = gr_opengl1_reset_clip;
118         gr_screen.gf_set_font = grx_set_font;
119
120         gr_screen.gf_set_color = gr_opengl_set_color;
121         gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
122         gr_screen.gf_create_shader = gr_opengl_create_shader;
123         gr_screen.gf_set_shader = gr_opengl_set_shader;
124         gr_screen.gf_clear = gr_opengl1_clear;
125
126         gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
127         gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
128
129         gr_screen.gf_rect = gr_opengl1_rect;
130         gr_screen.gf_shade = gr_opengl1_shade;
131         gr_screen.gf_string = gr_opengl1_string;
132         gr_screen.gf_circle = gr_opengl1_circle;
133
134         gr_screen.gf_line = gr_opengl1_line;
135         gr_screen.gf_aaline = gr_opengl1_aaline;
136         gr_screen.gf_pixel = gr_opengl1_pixel;
137         gr_screen.gf_scaler = gr_opengl1_scaler;
138         gr_screen.gf_tmapper = gr_opengl1_tmapper;
139
140         gr_screen.gf_gradient = gr_opengl1_gradient;
141
142         gr_screen.gf_get_color = gr_opengl_get_color;
143         gr_screen.gf_init_color = gr_opengl_init_color;
144         gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
145         gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
146         gr_screen.gf_print_screen = gr_opengl1_print_screen;
147
148         gr_screen.gf_fade_in = gr_opengl1_fade_in;
149         gr_screen.gf_fade_out = gr_opengl1_fade_out;
150         gr_screen.gf_flash = gr_opengl1_flash;
151
152         gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
153         gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
154         gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
155
156         gr_screen.gf_save_screen = gr_opengl1_save_screen;
157         gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
158         gr_screen.gf_free_screen = gr_opengl1_free_screen;
159
160         gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
161         gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
162         gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
163
164         gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
165
166         gr_screen.gf_lock = gr_opengl1_lock;
167         gr_screen.gf_unlock = gr_opengl1_unlock;
168
169         gr_screen.gf_fog_set = gr_opengl1_fog_set;
170
171         gr_screen.gf_get_region = gr_opengl1_get_region;
172
173         gr_screen.gf_set_cull = gr_opengl1_set_cull;
174
175         gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
176
177         gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;
178
179         gr_screen.gf_preload_init = gr_opengl1_preload_init;
180         gr_screen.gf_preload = gr_opengl1_preload;
181
182         gr_screen.gf_zbias = gr_opengl1_zbias;
183
184         gr_screen.gf_force_windowed = gr_opengl_force_windowed;
185         gr_screen.gf_force_fullscreen = gr_opengl_force_fullscreen;
186         gr_screen.gf_set_viewport = gr_opengl_set_viewport;
187
188         gr_screen.gf_activate = gr_opengl1_activate;
189 }
190
191 void opengl1_init()
192 {
193         if (GL_one_inited) {
194                 return;
195         }
196
197         /*
198           1 = use secondary color ext
199           2 = use opengl linear fog
200          */
201         OGL_fog_mode = 2;
202
203         // only available with OpenGL 1.2+, must get ptr for Windows
204         vglSecondaryColorPointer = (PFNGLSECONDARYCOLORPOINTERPROC)SDL_GL_GetProcAddress("glSecondaryColorPointer");
205
206         if (vglSecondaryColorPointer) {
207                 OGL_fog_mode = 1;
208         }
209
210         mprintf(("  Fog mode: %s\n", (OGL_fog_mode == 1) ? "secondary color" : "linear"));
211
212         glShadeModel(GL_SMOOTH);
213         glEnable(GL_DITHER);
214         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
215         glHint(GL_FOG_HINT, GL_NICEST);
216
217         glEnable(GL_DEPTH_TEST);
218         glEnable(GL_BLEND);
219
220         glEnable(GL_TEXTURE_2D);
221
222         glDepthRange(0.0, 1.0);
223
224         glPixelStorei(GL_PACK_ALIGNMENT, 1);
225         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
226
227         glFlush();
228
229         opengl1_init_func_pointers();
230         opengl1_tcache_init();
231
232         gr_opengl1_clear();
233
234         GL_one_inited = 1;
235 }
236
237 void gr_opengl1_activate(int active)
238 {
239         if (active) {
240                 GL_activate++;
241
242                 // don't grab key/mouse if cmdline says so or if we're fullscreen
243         //      if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
244         //              SDL_WM_GrabInput(SDL_GRAB_ON);
245         //      }
246         } else {
247                 GL_deactivate++;
248
249                 // let go of mouse/keyboard
250         //      SDL_WM_GrabInput(SDL_GRAB_OFF);
251         }
252 }
253
254 void gr_opengl1_clear()
255 {
256         glClearColor(gr_screen.current_clear_color.red / 255.0,
257                 gr_screen.current_clear_color.green / 255.0,
258                 gr_screen.current_clear_color.blue / 255.0, 1.0);
259
260         glClear( GL_COLOR_BUFFER_BIT );
261 }
262
263 void gr_opengl1_flip()
264 {
265         if ( !GL_one_inited ) {
266                 return;
267         }
268
269         gr_reset_clip();
270
271         mouse_eval_deltas();
272
273         Gr_opengl_mouse_saved = 0;
274
275         if ( mouse_is_visible() )       {
276                 int mx, my;
277
278                 gr_reset_clip();
279                 mouse_get_pos( &mx, &my );
280
281                 gr_opengl1_save_mouse_area(mx,my,32,32);
282
283                 if (Gr_cursor == -1) {
284 #ifndef NDEBUG
285                         gr_set_color(255,255,255);
286                         gr_line(mx, my, mx+7, my + 7);
287                         gr_line(mx, my, mx+5, my );
288                         gr_line(mx, my, mx, my+5);
289 #endif
290                 } else {
291                         gr_set_bitmap(Gr_cursor);
292                         gr_bitmap(mx, my);
293                 }
294          }
295
296 #ifndef NDEBUG
297         GLenum error = GL_NO_ERROR;
298
299         do {
300                 error = glGetError();
301
302                 if (error != GL_NO_ERROR) {
303                         nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
304                 }
305         } while (error != GL_NO_ERROR);
306 #endif
307
308         SDL_GL_SwapWindow(GL_window);
309
310         opengl1_tcache_frame();
311
312         int cnt = GL_activate;
313         if ( cnt )      {
314                 GL_activate-=cnt;
315                 opengl1_tcache_flush();
316                 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
317         }
318
319         cnt = GL_deactivate;
320         if ( cnt )      {
321                 GL_deactivate-=cnt;
322                 // gr_opengl_clip_cursor(0);  /* mouse grab, see opengl_activate */
323         }
324 }
325
326 void gr_opengl1_set_clip(int x,int y,int w,int h)
327 {
328         // check for sanity of parameters
329         if (x < 0)
330                 x = 0;
331         if (y < 0)
332                 y = 0;
333
334         if (x >= gr_screen.max_w)
335                 x = gr_screen.max_w - 1;
336         if (y >= gr_screen.max_h)
337                 y = gr_screen.max_h - 1;
338
339         if (x + w > gr_screen.max_w)
340                 w = gr_screen.max_w - x;
341         if (y + h > gr_screen.max_h)
342                 h = gr_screen.max_h - y;
343
344         if (w > gr_screen.max_w)
345                 w = gr_screen.max_w;
346         if (h > gr_screen.max_h)
347                 h = gr_screen.max_h;
348
349         gr_screen.offset_x = x;
350         gr_screen.offset_y = y;
351         gr_screen.clip_left = 0;
352         gr_screen.clip_right = w-1;
353         gr_screen.clip_top = 0;
354         gr_screen.clip_bottom = h-1;
355         gr_screen.clip_width = w;
356         gr_screen.clip_height = h;
357
358         x = fl2i((x * GL_viewport_scale_w) + 0.5f) + GL_viewport_x;
359         y = fl2i((y * GL_viewport_scale_h) + 0.5f) + GL_viewport_y;
360         w = fl2i((w * GL_viewport_scale_w) + 0.5f);
361         h = fl2i((h * GL_viewport_scale_h) + 0.5f);
362
363         glEnable(GL_SCISSOR_TEST);
364         glScissor(x, GL_viewport_h-y-h, w, h);
365 }
366
367 void gr_opengl1_reset_clip()
368 {
369         gr_screen.offset_x = 0;
370         gr_screen.offset_y = 0;
371         gr_screen.clip_left = 0;
372         gr_screen.clip_top = 0;
373         gr_screen.clip_right = gr_screen.max_w - 1;
374         gr_screen.clip_bottom = gr_screen.max_h - 1;
375         gr_screen.clip_width = gr_screen.max_w;
376         gr_screen.clip_height = gr_screen.max_h;
377
378         glDisable(GL_SCISSOR_TEST);
379 }
380
381 void gr_opengl1_print_screen(const char *filename)
382 {
383         char tmp[MAX_FILENAME_LEN];
384         ubyte *buf = NULL;
385
386         strcpy( tmp, filename );
387         strcat( tmp, NOX(".tga"));
388
389         buf = (ubyte*)malloc(GL_viewport_w * GL_viewport_h * 3);
390
391         if (buf == NULL) {
392                 return;
393         }
394
395         CFILE *f = cfopen(tmp, "wb", CFILE_NORMAL, CF_TYPE_ROOT);
396
397         if (f == NULL) {
398                 free(buf);
399                 return;
400         }
401
402         // Write the TGA header
403         cfwrite_ubyte( 0, f );  //      IDLength;
404         cfwrite_ubyte( 0, f );  //      ColorMapType;
405         cfwrite_ubyte( 2, f );  //      ImageType;              // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
406         cfwrite_ushort( 0, f ); // CMapStart;
407         cfwrite_ushort( 0, f ); //      CMapLength;
408         cfwrite_ubyte( 0, f );  // CMapDepth;
409         cfwrite_ushort( 0, f ); //      XOffset;
410         cfwrite_ushort( 0, f ); //      YOffset;
411         cfwrite_ushort( (ushort)GL_viewport_w, f );     //      Width;
412         cfwrite_ushort( (ushort)GL_viewport_h, f );     //      Height;
413         cfwrite_ubyte( 24, f ); //PixelDepth;
414         cfwrite_ubyte( 0, f );  //ImageDesc;
415
416         memset(buf, 0, GL_viewport_w * GL_viewport_h * 3);
417
418         glReadPixels(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h, GL_BGR, GL_UNSIGNED_BYTE, buf);
419
420         cfwrite(buf, GL_viewport_w * GL_viewport_h * 3, 1, f);
421
422         cfclose(f);
423
424         free(buf);
425 }
426
427 void gr_opengl1_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
428 {
429         SDL_assert((r >= 0) && (r < 256));
430         SDL_assert((g >= 0) && (g < 256));
431         SDL_assert((b >= 0) && (b < 256));
432
433         if (fog_mode == GR_FOGMODE_NONE) {
434                 if (gr_screen.current_fog_mode != fog_mode) {
435                         glDisable(GL_FOG);
436
437                         if (OGL_fog_mode == 1) {
438                                 glDisable(GL_COLOR_SUM);
439                         }
440                 }
441
442                 gr_screen.current_fog_mode = fog_mode;
443
444                 return;
445         }
446
447         if (gr_screen.current_fog_mode != fog_mode) {
448                 glEnable(GL_FOG);
449
450                 if (OGL_fog_mode == 1) {
451                         glEnable(GL_COLOR_SUM);
452                 } else if (OGL_fog_mode == 2) {
453                         glFogi(GL_FOG_MODE, GL_LINEAR);
454                 }
455
456                 gr_screen.current_fog_mode = fog_mode;
457         }
458
459         if ( (gr_screen.current_fog_color.red != r) ||
460                         (gr_screen.current_fog_color.green != g) ||
461                         (gr_screen.current_fog_color.blue != b) ) {
462                 GLfloat fc[4];
463
464                 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
465
466                 fc[0] = (float)r/255.0;
467                 fc[1] = (float)g/255.0;
468                 fc[2] = (float)b/255.0;
469                 fc[3] = 1.0;
470
471                 glFogfv(GL_FOG_COLOR, fc);
472         }
473
474         if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
475                         ((fog_near != gr_screen.fog_near) ||
476                         (fog_far != gr_screen.fog_far)) ) {
477                 gr_screen.fog_near = fog_near;
478                 gr_screen.fog_far = fog_far;
479
480                 if (OGL_fog_mode == 2) {
481                         glFogf(GL_FOG_START, fog_near);
482                         glFogf(GL_FOG_END, fog_far);
483                 }
484         }
485 }
486
487 void gr_opengl1_set_cull(int cull)
488 {
489         if (cull) {
490                 glEnable (GL_CULL_FACE);
491                 glFrontFace (GL_CCW);
492         } else {
493                 glDisable (GL_CULL_FACE);
494         }
495 }
496
497 void gr_opengl1_zbuffer_clear(int mode)
498 {
499         if (mode) {
500                 Gr_zbuffering = 1;
501                 Gr_zbuffering_mode = GR_ZBUFF_FULL;
502                 Gr_global_zbuffering = 1;
503
504                 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
505                 glClear ( GL_DEPTH_BUFFER_BIT );
506         } else {
507                 Gr_zbuffering = 0;
508                 Gr_zbuffering_mode = GR_ZBUFF_NONE;
509                 Gr_global_zbuffering = 0;
510         }
511 }
512
513 void gr_opengl1_fade_in(int instantaneous)
514 {
515         // Empty - DDOI
516 }
517
518 void gr_opengl1_fade_out(int instantaneous)
519 {
520         // Empty - DDOI
521 }
522
523 void gr_opengl1_get_region(int front, int w, int h, ubyte *data)
524 {
525         if (front) {
526                 glReadBuffer(GL_FRONT);
527         } else {
528                 glReadBuffer(GL_BACK);
529         }
530
531         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
532
533         glPixelStorei(GL_UNPACK_ROW_LENGTH, GL_viewport_w);
534
535         int x = GL_viewport_x;
536         int y = (GL_viewport_y+GL_viewport_h)-h-1;
537
538         GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
539
540         if (gr_screen.bytes_per_pixel == 4) {
541                 pxtype = GL_UNSIGNED_BYTE;
542         }
543
544         glReadPixels(x, y, w, h, GL_BGRA, pxtype, data);
545
546         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
547 }
548
549 void gr_opengl1_save_mouse_area(int x, int y, int w, int h)
550 {
551         int x1, y1, x2, y2;
552
553         w = fl2i((w * GL_viewport_scale_w) + 0.5f);
554         h = fl2i((h * GL_viewport_scale_h) + 0.5f);
555
556         x1 = x;
557         y1 = y;
558         x2 = x+w-1;
559         y2 = y+h-1;
560
561         CAP(x1, 0, GL_viewport_w);
562         CAP(x2, 0, GL_viewport_w);
563         CAP(y1, 0, GL_viewport_h);
564         CAP(y2, 0, GL_viewport_h);
565
566         Gr_opengl_mouse_saved_x = x1;
567         Gr_opengl_mouse_saved_y = y1;
568         Gr_opengl_mouse_saved_w = x2 - x1 + 1;
569         Gr_opengl_mouse_saved_h = y2 - y1 + 1;
570
571         if ( (Gr_opengl_mouse_saved_w < 1) || (Gr_opengl_mouse_saved_h < 1) ) {
572                 return;
573         }
574
575         if (Gr_opengl_mouse_saved_data == NULL) {
576                 Gr_opengl_mouse_saved_data = (ubyte*)malloc(w * h * gr_screen.bytes_per_pixel);
577
578                 if ( !Gr_opengl_mouse_saved_data ) {
579                         return;
580                 }
581         }
582
583         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
584
585         x1 = GL_viewport_x+Gr_opengl_mouse_saved_x;
586         y1 = (GL_viewport_y+GL_viewport_h)-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
587
588         GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
589
590         if (gr_screen.bytes_per_pixel == 4) {
591                 pxtype = GL_UNSIGNED_BYTE;
592         }
593
594         glReadBuffer(GL_BACK);
595         glReadPixels(x1, y1, Gr_opengl_mouse_saved_w, Gr_opengl_mouse_saved_h,
596                         GL_BGRA, pxtype, Gr_opengl_mouse_saved_data);
597
598         Gr_opengl_mouse_saved = 1;
599 }
600
601 int gr_opengl1_save_screen()
602 {
603         gr_reset_clip();
604
605         if (Gr_saved_screen_tex) {
606                 mprintf(( "Screen already saved!\n" ));
607                 return -1;
608         }
609
610         glGenTextures(1, &Gr_saved_screen_tex);
611
612         if ( !Gr_saved_screen_tex ) {
613                 mprintf(( "Couldn't create texture for saved screen!\n" ));
614                 return -1;
615         }
616
617         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
618
619         glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
620
621         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
622         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
623         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
624         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
625         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
626
627         glReadBuffer(GL_FRONT);
628         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_viewport_x, GL_viewport_y,
629                         GL_viewport_w, GL_viewport_h, 0);
630
631         if (Gr_opengl_mouse_saved) {
632                 int x = Gr_opengl_mouse_saved_x;
633                 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
634
635                 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
636
637                 if (gr_screen.bytes_per_pixel == 4) {
638                         pxtype = GL_UNSIGNED_BYTE;
639                 }
640
641                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
642                                 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
643                                 Gr_opengl_mouse_saved_data);
644         }
645
646         glBindTexture(GL_TEXTURE_2D, 0);
647
648         return 0;
649 }
650
651 void gr_opengl1_restore_screen(int id)
652 {
653         gr_reset_clip();
654
655         if ( !Gr_saved_screen_tex ) {
656                 gr_clear();
657                 return;
658         }
659
660         glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
661
662         if (Gr_opengl_mouse_saved) {
663                 int x = Gr_opengl_mouse_saved_x;
664                 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
665
666                 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
667
668                 if (gr_screen.bytes_per_pixel == 4) {
669                         pxtype = GL_UNSIGNED_BYTE;
670                 }
671
672                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
673                                 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
674                                 Gr_opengl_mouse_saved_data);
675         }
676
677         glMatrixMode(GL_MODELVIEW);
678         glPushMatrix();
679         glLoadIdentity();
680         glScalef(1.0f, -1.0f, 1.0f);
681
682         int tex_coord[] = { 0, 0, 0, 1, 1, 0, 1, 1 };
683         int ver_coord[] = { GL_viewport_x, GL_viewport_y, GL_viewport_x,
684                         GL_viewport_h, GL_viewport_w, GL_viewport_y, GL_viewport_w,
685                         GL_viewport_h
686         };
687
688         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
689         glEnableClientState(GL_VERTEX_ARRAY);
690
691         glTexCoordPointer(2, GL_INT, 0, &tex_coord);
692         glVertexPointer(2, GL_INT, 0, &ver_coord);
693
694         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
695
696         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
697         glDisableClientState(GL_VERTEX_ARRAY);
698
699         glPopMatrix();
700
701         glBindTexture(GL_TEXTURE_2D, 0);
702 }
703
704 void gr_opengl1_free_screen(int id)
705 {
706         if (Gr_saved_screen_tex) {
707                 glDeleteTextures(1, &Gr_saved_screen_tex);
708                 Gr_saved_screen_tex = 0;
709         }
710 }
711
712 void gr_opengl1_dump_frame_start(int first_frame, int frames_between_dumps)
713 {
714         STUB_FUNCTION;
715 }
716
717 void gr_opengl1_dump_frame_stop()
718 {
719         STUB_FUNCTION;
720 }
721
722 void gr_opengl1_dump_frame()
723 {
724         STUB_FUNCTION;
725 }
726
727 uint gr_opengl1_lock()
728 {
729         return 1;
730 }
731
732 void gr_opengl1_unlock()
733 {
734 }
735
736 void gr_opengl1_zbias(int bias)
737 {
738         if (bias) {
739                 glEnable(GL_POLYGON_OFFSET_FILL);
740                 glPolygonOffset(0.0, -bias);
741         } else {
742                 glDisable(GL_POLYGON_OFFSET_FILL);
743         }
744 }