]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/gropengl.cpp
GCC 3.4 fixes.
[taylor/freespace2.git] / src / graphics / gropengl.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 /*
10  * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code that uses the OpenGL graphics library
16  *
17  * $Log$
18  * Revision 1.70  2004/09/20 01:31:44  theoddone33
19  * GCC 3.4 fixes.
20  *
21  * Revision 1.69  2004/07/04 11:31:43  taylor
22  * amd64 support, compiler warning fixes, don't use software rendering
23  *
24  * Revision 1.68  2004/06/11 01:01:07  tigital
25  * added FSAA support: switched on in .ini
26  *
27  * Revision 1.67  2003/08/03 15:59:40  taylor
28  * GL_RGB5_A1 as TexImage internal format; cleaner input grab; min window title; cleanup
29  *
30  * Revision 1.66  2003/06/22 12:52:34  taylor
31  * more texture size fixin
32  *
33  * Revision 1.65  2003/06/19 11:52:47  taylor
34  * fix texture size issue with lower detail settings
35  *
36  * Revision 1.64  2003/05/28 06:02:04  taylor
37  * fix transparency in green weapon blobs
38  *
39  * Revision 1.63  2003/05/09 05:05:52  taylor
40  * improve gr_(de)activate, mouse grab, default fullscreen
41  *
42  * Revision 1.62  2003/05/04 04:56:53  taylor
43  * move SDL_Quit to os_deinit to fix fonttool segfault
44  *
45  * Revision 1.61  2003/02/02 21:13:27  relnev
46  * minor updates (made functions static, tried to improve texture sizes)
47  *
48  * Revision 1.60  2003/02/02 20:20:03  relnev
49  * add large texture support (Taylor Richards)
50  *
51  * Revision 1.59  2002/09/04 01:38:09  relnev
52  * revert
53  *
54  * Revision 1.57  2002/08/31 01:39:13  theoddone33
55  * Speed up the renderer a tad
56  *
57  * Revision 1.56  2002/08/01 05:03:11  relnev
58  * minor change
59  *
60  * Revision 1.55  2002/08/01 04:55:45  relnev
61  * experimenting with texture state
62  *
63  * Revision 1.54  2002/07/30 15:00:15  relnev
64  * not use luminance alpha by default
65  *
66  * Revision 1.53  2002/06/22 17:08:16  relnev
67  * more fixes to unused alpha code
68  *
69  * Revision 1.52  2002/06/21 23:59:14  relnev
70  * moved a gr_opengl_set_state to after the gr_tcache_set
71  *
72  * Revision 1.51  2002/06/19 04:52:45  relnev
73  * MacOS X updates (Ryan)
74  *
75  * Revision 1.50  2002/06/18 23:20:44  relnev
76  * fixed _splitpath.
77  *
78  * adjusted line offsets.
79  *
80  * Revision 1.49  2002/06/17 23:11:39  relnev
81  * enable sdl key repeating.
82  *
83  * swap '/` keys.
84  *
85  * Revision 1.48  2002/06/09 04:41:17  relnev
86  * added copyright header
87  *
88  * Revision 1.47  2002/06/09 03:16:04  relnev
89  * added _splitpath.
90  *
91  * removed unneeded asm, old sdl 2d setup.
92  *
93  * fixed crash caused by opengl_get_region.
94  *
95  * Revision 1.46  2002/06/05 04:03:32  relnev
96  * finished cfilesystem.
97  *
98  * removed some old code.
99  *
100  * fixed mouse save off-by-one.
101  *
102  * sound cleanups.
103  *
104  * Revision 1.45  2002/06/03 09:25:37  relnev
105  * implement mouse cursor and screen save/restore
106  *
107  * Revision 1.44  2002/06/02 18:46:59  relnev
108  * updated
109  *
110  * Revision 1.43  2002/06/02 11:34:00  relnev
111  * adjust z coords
112  *
113  * Revision 1.42  2002/06/02 10:28:17  relnev
114  * fix texture handle leak
115  *
116  * Revision 1.41  2002/06/01 09:00:34  relnev
117  * silly debug memmanager
118  *
119  * Revision 1.40  2002/06/01 07:12:33  relnev
120  * a few NDEBUG updates.
121  *
122  * removed a few warnings.
123  *
124  * Revision 1.39  2002/06/01 05:33:15  relnev
125  * copied more code over.
126  *
127  * added scissor clipping.
128  *
129  * Revision 1.38  2002/06/01 03:35:27  relnev
130  * fix typo
131  *
132  * Revision 1.37  2002/06/01 03:32:00  relnev
133  * fix texture loading mistake.
134  *
135  * enable some d3d stuff for opengl also
136  *
137  * Revision 1.36  2002/05/31 23:25:03  relnev
138  * line fixes
139  *
140  * Revision 1.34  2002/05/31 22:15:22  relnev
141  * BGRA
142  *
143  * Revision 1.33  2002/05/31 22:04:55  relnev
144  * use d3d rect_internal
145  *
146  * Revision 1.32  2002/05/31 06:28:23  relnev
147  * more stuff
148  *
149  * Revision 1.31  2002/05/31 06:04:39  relnev
150  * fog
151  *
152  * Revision 1.30  2002/05/31 03:56:11  theoddone33
153  * Change tmapper polygon winding and enable culling
154  *
155  * Revision 1.29  2002/05/31 03:34:02  theoddone33
156  * Fix Keyboard
157  * Add titlebar
158  *
159  * Revision 1.28  2002/05/31 00:06:59  relnev
160  * minor change
161  *
162  * Revision 1.27  2002/05/30 23:46:29  theoddone33
163  * some minor key changes (not necessarily fixes)
164  *
165  * Revision 1.26  2002/05/30 23:33:12  relnev
166  * implemented a few more functions.
167  *
168  * Revision 1.25  2002/05/30 23:01:16  relnev
169  * implement gr_opengl_set_state.
170  *
171  * Revision 1.24  2002/05/30 22:12:57  relnev
172  * finish default texture case
173  *
174  * Revision 1.23  2002/05/30 22:02:30  theoddone33
175  * More gl changes
176  *
177  * Revision 1.22  2002/05/30 21:44:48  relnev
178  * implemented some missing texture stuff.
179  *
180  * enable bitmap polys for opengl.
181  *
182  * work around greenness in bitmaps.
183  *
184  * Revision 1.21  2002/05/30 17:29:30  theoddone33
185  * Fix some more stubs, change at least one polygon winding since culling is now
186  * enabled.
187  *
188  * Revision 1.20  2002/05/30 16:50:24  theoddone33
189  * Keyboard partially fixed
190  *
191  * Revision 1.19  2002/05/30 08:13:14  relnev
192  * fonts are fixed
193  *
194  * Revision 1.18  2002/05/29 23:37:36  relnev
195  * fix bitmap bug
196  *
197  * Revision 1.17  2002/05/29 23:17:49  theoddone33
198  * Non working text code and fixed keys
199  *
200  * Revision 1.16  2002/05/29 19:45:13  theoddone33
201  * More changes on texture loading
202  *
203  * Revision 1.15  2002/05/29 19:06:48  theoddone33
204  * Enable string printing.  Enable texture mapping
205  *
206  * Revision 1.14  2002/05/29 08:54:40  relnev
207  * "fixed" bitmap drawing.
208  *
209  * copied more d3d code over.
210  *
211  * Revision 1.13  2002/05/29 06:25:13  theoddone33
212  * Keyboard input, mouse tracking now work
213  *
214  * Revision 1.12  2002/05/29 04:52:45  relnev
215  * bitmap
216  *
217  * Revision 1.11  2002/05/29 04:29:56  relnev
218  * removed some unncessary stubbing, implemented opengl rect
219  *
220  * Revision 1.10  2002/05/29 04:13:27  theoddone33
221  * enable opengl_line
222  *
223  * Revision 1.9  2002/05/29 03:35:51  relnev
224  * added rest of init
225  *
226  * Revision 1.8  2002/05/29 03:30:05  relnev
227  * update opengl stubs
228  *
229  * Revision 1.7  2002/05/29 02:52:32  theoddone33
230  * Enable OpenGL renderer
231  *
232  * Revision 1.6  2002/05/28 04:56:51  theoddone33
233  * runs a little bit now
234  *
235  * Revision 1.5  2002/05/28 04:07:28  theoddone33
236  * New graphics stubbing arrangement
237  *
238  * Revision 1.4  2002/05/27 23:39:34  relnev
239  * 0
240  *
241  * Revision 1.3  2002/05/27 22:35:01  theoddone33
242  * more symbols
243  *
244  * Revision 1.2  2002/05/27 22:32:02  theoddone33
245  * throw all d3d stuff at opengl
246  *
247  * Revision 1.1.1.1  2002/05/03 03:28:09  root
248  * Initial import.
249  *
250  * 
251  * 10    7/14/99 9:42a Dave
252  * Put in clear_color debug function. Put in base for 3dnow stuff / P3
253  * stuff
254  * 
255  * 9     7/09/99 9:51a Dave
256  * Added thick polyline code.
257  * 
258  * 8     6/29/99 10:35a Dave
259  * Interface polygon bitmaps! Whee!
260  * 
261  * 7     2/03/99 11:44a Dave
262  * Fixed d3d transparent textures.
263  * 
264  * 6     1/24/99 11:37p Dave
265  * First full rev of beam weapons. Very customizable. Removed some bogus
266  * Int3()'s in low level net code.
267  * 
268  * 5     12/18/98 1:13a Dave
269  * Rough 1024x768 support for Direct3D. Proper detection and usage through
270  * the launcher.
271  * 
272  * 4     12/06/98 2:36p Dave
273  * Drastically improved nebula fogging.
274  * 
275  * 3     11/11/98 5:37p Dave
276  * Checkin for multiplayer testing.
277  * 
278  * 2     10/07/98 10:53a Dave
279  * Initial checkin.
280  * 
281  * 1     10/07/98 10:49a Dave
282  * 
283  * 14    5/20/98 9:46p John
284  * added code so the places in code that change half the palette don't
285  * have to clear the screen.
286  * 
287  * 13    5/06/98 5:30p John
288  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
289  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
290  * DirectX header files and libs that fixed the Direct3D alpha blending
291  * problems.
292  * 
293  * 12    4/14/98 12:15p John
294  * Made 16-bpp movies work.
295  * 
296  * 11    3/12/98 5:36p John
297  * Took out any unused shaders.  Made shader code take rgbc instead of
298  * matrix and vector since noone used it like a matrix and it would have
299  * been impossible to do in hardware.   Made Glide implement a basic
300  * shader for online help.  
301  * 
302  * 10    3/10/98 4:18p John
303  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
304  * & Glide have popups and print screen.  Took out all >8bpp software
305  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
306  * support Fred.  Made zbuffering key off of functions rather than one
307  * global variable.
308  * 
309  * 9     12/02/97 4:00p John
310  * Added first rev of thruster glow, along with variable levels of
311  * translucency, which retquired some restructing of palman.
312  * 
313  * 8     10/03/97 9:10a John
314  * added better antialiased line drawer
315  * 
316  * 7     9/23/97 10:45a John
317  * made so you can tell bitblt code to rle a bitmap by passing flag to
318  * gr_set_bitmap
319  * 
320  * 6     9/09/97 11:01a Sandeep
321  * fixed warning level 4 bugs
322  * 
323  * 5     7/10/97 2:06p John
324  * added code to specify alphablending type for bitmaps.
325  * 
326  * 4     6/17/97 7:04p John
327  * added d3d support for gradients.
328  * fixed some color bugs by adding screen signatures instead of watching
329  * flags and palette changes.
330  * 
331  * 3     6/12/97 2:50a Lawrance
332  * bm_unlock() now passed bitmap number, not pointer
333  * 
334  * 2     6/11/97 1:12p John
335  * Started fixing all the text colors in the game.
336  * 
337  * 1     5/12/97 12:14p John
338  *
339  * $NoKeywords: $
340  */
341
342 #ifndef PLAT_UNIX
343 #include <windows.h>
344 #include <windowsx.h>
345 #endif
346
347 #ifdef __APPLE__
348 #include <OpenGL/gl.h>
349 #else
350 #include <GL/gl.h>
351 #endif
352
353 #include "pstypes.h"
354 #include "osapi.h"
355 #include "2d.h"
356 #include "3d.h"
357 #include "bmpman.h"
358 #include "floating.h"
359 #include "palman.h"
360 #include "systemvars.h"
361 #include "grinternal.h"
362 #include "gropengl.h"
363 #include "line.h"
364 #include "neb.h"
365 #include "mouse.h"
366 #include "osregistry.h"
367 #include "cmdline.h"
368
369 static int Inited = 0;
370
371 typedef enum gr_texture_source {
372         TEXTURE_SOURCE_NONE,
373         TEXTURE_SOURCE_DECAL,
374         TEXTURE_SOURCE_NO_FILTERING,
375 } gr_texture_source;
376
377 typedef enum gr_alpha_blend {
378         ALPHA_BLEND_NONE,                       // 1*SrcPixel + 0*DestPixel
379         ALPHA_BLEND_ADDITIVE,                   // 1*SrcPixel + 1*DestPixel
380         ALPHA_BLEND_ALPHA_ADDITIVE,             // Alpha*SrcPixel + 1*DestPixel
381         ALPHA_BLEND_ALPHA_BLEND_ALPHA,          // Alpha*SrcPixel + (1-Alpha)*DestPixel
382         ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR,      // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
383 } gr_alpha_blend;
384
385 typedef enum gr_zbuffer_type {
386         ZBUFFER_TYPE_NONE,
387         ZBUFFER_TYPE_READ,
388         ZBUFFER_TYPE_WRITE,
389         ZBUFFER_TYPE_FULL,
390 } gr_zbuffer_type;
391                         
392 #define NEBULA_COLORS 20
393
394 volatile int GL_activate = 0;
395 volatile int GL_deactivate = 0;
396
397 static int GL_use_luminance_alpha;
398 static int FSAA;
399
400 static char *Gr_saved_screen = NULL;
401 static int Gr_saved_screen_bitmap;
402
403 #ifdef PLAT_UNIX
404 // Throw in some dummy functions - DDOI
405
406 int D3D_32bit = 0;              // grd3d.cpp
407 int D3D_fog_mode = -1;          // grd3d.cpp
408 int D3D_inited = 0;             // grd3d.cpp
409 int D3D_zbias = 1;              // grd3d.cpp
410 int D3d_rendition_uvs = 0;      // grd3d.cpp
411
412 void d3d_flush ()
413 {
414         STUB_FUNCTION;
415 }
416
417 void d3d_zbias (int a)
418 {
419         STUB_FUNCTION;
420 }
421 #endif
422
423 static void gr_opengl_set_texture_state(gr_texture_source ts);
424
425 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
426 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
427
428 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
429 {
430         gr_opengl_set_texture_state(ts);
431         
432         if (ab != GL_current_alpha_blend) {
433                 switch (ab) {
434                         case ALPHA_BLEND_NONE:                  // 1*SrcPixel + 0*DestPixel
435                                 glBlendFunc(GL_ONE, GL_ZERO);
436                                 break;
437                         case ALPHA_BLEND_ADDITIVE:              // 1*SrcPixel + 1*DestPixel
438                                 glBlendFunc(GL_ONE, GL_ONE);
439                                 break;
440                         case ALPHA_BLEND_ALPHA_ADDITIVE:        // Alpha*SrcPixel + 1*DestPixel
441                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
442                                 break;
443                         case ALPHA_BLEND_ALPHA_BLEND_ALPHA:     // Alpha*SrcPixel + (1-Alpha)*DestPixel
444                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
445                                 break;
446                         case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
447                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
448                                 break;
449                         default:
450                                 break;
451                 }
452                 
453                 GL_current_alpha_blend = ab;
454         }
455         
456         if (zt != GL_current_zbuffer_type) {
457                 switch (zt) {
458                         case ZBUFFER_TYPE_NONE:
459                                 glDepthFunc(GL_ALWAYS);
460                                 glDepthMask(GL_FALSE);
461                                 break;
462                         case ZBUFFER_TYPE_READ:
463                                 glDepthFunc(GL_LESS);
464                                 glDepthMask(GL_FALSE);  
465                                 break;
466                         case ZBUFFER_TYPE_WRITE:
467                                 glDepthFunc(GL_ALWAYS);
468                                 glDepthMask(GL_TRUE);
469                                 break;
470                         case ZBUFFER_TYPE_FULL:
471                                 glDepthFunc(GL_LESS);
472                                 glDepthMask(GL_TRUE);
473                                 break;
474                         default:
475                                 break;
476                 }
477                 
478                 GL_current_zbuffer_type = zt;
479         }       
480 }
481
482 void gr_opengl_activate(int active)
483 {
484         if (active) {
485                 GL_activate++;
486                 
487                 // don't grab key/mouse if cmdline says so or if we're fullscreen
488                 if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
489                         SDL_WM_GrabInput(SDL_GRAB_ON);
490                 }
491         } else {
492                 GL_deactivate++;
493                 
494                 // let go of mouse/keyboard
495                 SDL_WM_GrabInput(SDL_GRAB_OFF);
496         }
497 }
498
499
500 static void opengl_tcache_flush ();
501
502 void gr_opengl_preload_init()
503 {
504         if (gr_screen.mode != GR_OPENGL) {
505                 return;
506         }
507
508         opengl_tcache_flush ();
509 }
510
511 int GL_should_preload = 0;
512 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
513 {
514         if ( gr_screen.mode != GR_OPENGL) {
515                 return 0;
516         }
517
518         if ( !GL_should_preload )      {
519                 return 0;
520         }
521
522         float u_scale, v_scale;
523
524         int retval;
525         if ( is_aabitmap )      {
526                 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1, -1, -1, 0 );
527         } else {
528                 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1, -1, -1, 0 );
529         }
530
531         if ( !retval )  {
532                 mprintf(("Texture upload failed!\n" ));
533         }
534
535         return retval;
536 }
537
538 void gr_opengl_pixel(int x, int y)
539 {
540         gr_line(x,y,x,y);
541 }
542
543 void gr_opengl_clear()
544 {
545         glClearColor(gr_screen.current_clear_color.red / 255.0, 
546                 gr_screen.current_clear_color.green / 255.0, 
547                 gr_screen.current_clear_color.blue / 255.0, 1.0);
548
549         glClear ( GL_COLOR_BUFFER_BIT );
550 }
551
552 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
553 static void opengl_tcache_frame ();
554
555 void gr_opengl_flip()
556 {
557         if (!Inited) return;
558
559         gr_reset_clip();
560
561         mouse_eval_deltas();
562         
563         extern int Gr_opengl_mouse_saved;
564         Gr_opengl_mouse_saved = 0;
565         
566         if ( mouse_is_visible() )       {
567                 int mx, my;
568                 
569                 gr_reset_clip();
570                 mouse_get_pos( &mx, &my );
571                 
572                 gr_opengl_save_mouse_area(mx,my,32,32);
573                 
574                 if ( Gr_cursor == -1 )  {
575                         // stuff
576                 } else {
577                         gr_set_bitmap(Gr_cursor, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
578                         gr_bitmap( mx, my );
579                 }
580          }
581          
582 #ifndef NDEBUG
583         GLenum error = glGetError();
584         int ic = 0;
585         do {
586                 error = glGetError();
587                 
588                 if (error != GL_NO_ERROR) {
589                         nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
590                 }
591                 ic++;
592         } while (error != GL_NO_ERROR);
593 #endif
594         
595         SDL_GL_SwapBuffers ();
596
597         opengl_tcache_frame ();
598         
599         int cnt = GL_activate;
600         if ( cnt )      {
601                 GL_activate-=cnt;
602                 opengl_tcache_flush();
603                 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
604         }
605         
606         cnt = GL_deactivate;
607         if ( cnt )      {
608                 GL_deactivate-=cnt;
609                 // gr_opengl_clip_cursor(0);  /* mouse grab, see opengl_activate */
610         }
611 }
612
613 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
614 {
615         // Not used.
616 }
617
618 void gr_opengl_set_clip(int x,int y,int w,int h)
619 {
620         // check for sanity of parameters
621         if (x < 0)
622                 x = 0;
623         if (y < 0)
624                 y = 0;
625
626         if (x >= gr_screen.max_w)
627                 x = gr_screen.max_w - 1;
628         if (y >= gr_screen.max_h)
629                 y = gr_screen.max_h - 1;
630
631         if (x + w > gr_screen.max_w)
632                 w = gr_screen.max_w - x;
633         if (y + h > gr_screen.max_h)
634                 h = gr_screen.max_h - y;
635         
636         if (w > gr_screen.max_w)
637                 w = gr_screen.max_w;
638         if (h > gr_screen.max_h)
639                 h = gr_screen.max_h;
640         
641         gr_screen.offset_x = x;
642         gr_screen.offset_y = y;
643         gr_screen.clip_left = 0;
644         gr_screen.clip_right = w-1;
645         gr_screen.clip_top = 0;
646         gr_screen.clip_bottom = h-1;
647         gr_screen.clip_width = w;
648         gr_screen.clip_height = h;
649         
650         glEnable(GL_SCISSOR_TEST);
651         glScissor(x, gr_screen.max_h-y-h, w, h);
652 }
653
654 void gr_opengl_reset_clip()
655 {
656         gr_screen.offset_x = 0;
657         gr_screen.offset_y = 0;
658         gr_screen.clip_left = 0;
659         gr_screen.clip_top = 0;
660         gr_screen.clip_right = gr_screen.max_w - 1;
661         gr_screen.clip_bottom = gr_screen.max_h - 1;
662         gr_screen.clip_width = gr_screen.max_w;
663         gr_screen.clip_height = gr_screen.max_h;
664         
665         glDisable(GL_SCISSOR_TEST);
666 //      glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
667 }
668
669 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
670 {
671         gr_screen.current_alpha = alpha;
672         gr_screen.current_alphablend_mode = alphablend_mode;
673         gr_screen.current_bitblt_mode = bitblt_mode;
674         gr_screen.current_bitmap = bitmap_num;
675
676         gr_screen.current_bitmap_sx = sx;
677         gr_screen.current_bitmap_sy = sy;
678 }
679
680 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
681 {
682         shade->screen_sig = gr_screen.signature;
683         shade->r = r;
684         shade->g = g;
685         shade->b = b;
686         shade->c = c;   
687 }
688
689 void gr_opengl_set_shader( shader * shade )
690 {       
691         if ( shade )    {
692                 if (shade->screen_sig != gr_screen.signature)   {
693                         gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
694                 }
695                 gr_screen.current_shader = *shade;
696         } else {
697                 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
698         }
699 }
700
701
702 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
703 {
704         STUB_FUNCTION; /* who called me? */
705 }
706
707 void gr_opengl_bitmap(int x, int y)
708 {
709         STUB_FUNCTION; /* who called me? */
710 }
711
712 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
713 {
714         int saved_zbuf;
715         vertex v[4];
716         vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
717
718         saved_zbuf = gr_zbuffer_get();
719         
720         // start the frame, no zbuffering, no culling
721         g3_start_frame(1);      
722         gr_zbuffer_set(GR_ZBUFF_NONE);          
723         gr_set_cull(0);         
724
725         // stuff coords         
726         v[0].sx = i2fl(x);
727         v[0].sy = i2fl(y);
728         v[0].sw = 0.0f;
729         v[0].u = 0.0f;
730         v[0].v = 0.0f;
731         v[0].flags = PF_PROJECTED;
732         v[0].codes = 0;
733         v[0].r = (ubyte)r;
734         v[0].g = (ubyte)g;
735         v[0].b = (ubyte)b;
736         v[0].a = (ubyte)a;
737
738         v[1].sx = i2fl(x + w);
739         v[1].sy = i2fl(y);      
740         v[1].sw = 0.0f;
741         v[1].u = 0.0f;
742         v[1].v = 0.0f;
743         v[1].flags = PF_PROJECTED;
744         v[1].codes = 0;
745         v[1].r = (ubyte)r;
746         v[1].g = (ubyte)g;
747         v[1].b = (ubyte)b;
748         v[1].a = (ubyte)a;
749
750         v[2].sx = i2fl(x + w);
751         v[2].sy = i2fl(y + h);
752         v[2].sw = 0.0f;
753         v[2].u = 0.0f;
754         v[2].v = 0.0f;
755         v[2].flags = PF_PROJECTED;
756         v[2].codes = 0;
757         v[2].r = (ubyte)r;
758         v[2].g = (ubyte)g;
759         v[2].b = (ubyte)b;
760         v[2].a = (ubyte)a;
761
762         v[3].sx = i2fl(x);
763         v[3].sy = i2fl(y + h);
764         v[3].sw = 0.0f;
765         v[3].u = 0.0f;
766         v[3].v = 0.0f;
767         v[3].flags = PF_PROJECTED;
768         v[3].codes = 0;                         
769         v[3].r = (ubyte)r;
770         v[3].g = (ubyte)g;
771         v[3].b = (ubyte)b;
772         v[3].a = (ubyte)a;
773
774         // draw the polys
775         g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);          
776
777         g3_end_frame();
778
779         // restore zbuffer and culling
780         gr_zbuffer_set(saved_zbuf);
781         gr_set_cull(1); 
782 }
783
784 void gr_opengl_rect(int x,int y,int w,int h)
785 {
786         gr_opengl_rect_internal(x, y, w, h, gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
787 }
788
789 void gr_opengl_shade(int x,int y,int w,int h)
790 {
791         int r,g,b,a;
792         
793         float shade1 = 1.0f;
794         float shade2 = 6.0f;
795
796         r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
797         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
798         g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
799         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
800         b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
801         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
802         a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
803         if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
804
805         gr_opengl_rect_internal(x, y, w, h, r, g, b, a);        
806 }
807
808 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
809 {
810         if ( w < 1 ) return;
811         if ( h < 1 ) return;
812
813         if ( !gr_screen.current_color.is_alphacolor )   return;
814
815         float u_scale, v_scale;
816
817         if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 0, -1, -1, 0 ) )       {
818                 // Couldn't set texture
819                 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
820                 return;
821         }
822
823         gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
824         
825         float u0, u1, v0, v1;
826         float x1, x2, y1, y2;
827         int bw, bh;
828
829         bm_get_info( gr_screen.current_bitmap, &bw, &bh );
830
831         u0 = u_scale*i2fl(sx)/i2fl(bw);
832         v0 = v_scale*i2fl(sy)/i2fl(bh);
833
834         u1 = u_scale*i2fl(sx+w)/i2fl(bw);
835         v1 = v_scale*i2fl(sy+h)/i2fl(bh);
836
837         x1 = i2fl(x+gr_screen.offset_x);
838         y1 = i2fl(y+gr_screen.offset_y);
839         x2 = i2fl(x+w+gr_screen.offset_x);
840         y2 = i2fl(y+h+gr_screen.offset_y);
841
842         if ( gr_screen.current_color.is_alphacolor )    {
843                 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
844         } else {
845                 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
846         }
847
848         glBegin (GL_QUADS);
849           glTexCoord2f (u0, v1);
850           glVertex3f (x1, y2, -0.99);
851
852           glTexCoord2f (u1, v1);
853           glVertex3f (x2, y2, -0.99);
854
855           glTexCoord2f (u1, v0);
856           glVertex3f (x2, y1, -0.99);
857
858           glTexCoord2f (u0, v0);
859           glVertex3f (x1, y1, -0.99);
860         glEnd ();
861 }
862
863 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
864 {
865         int reclip;
866         #ifndef NDEBUG
867         int count = 0;
868         #endif
869
870         int dx1=x, dx2=x+w-1;
871         int dy1=y, dy2=y+h-1;
872
873         int bw, bh;
874         bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
875
876         do {
877                 reclip = 0;
878                 #ifndef NDEBUG
879                         if ( count > 1 ) Int3();
880                         count++;
881                 #endif
882         
883                 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
884                 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
885                 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
886                 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
887                 if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
888                 if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
889
890                 if ( sx < 0 ) {
891                         dx1 -= sx;
892                         sx = 0;
893                         reclip = 1;
894                 }
895
896                 if ( sy < 0 ) {
897                         dy1 -= sy;
898                         sy = 0;
899                         reclip = 1;
900                 }
901
902                 w = dx2-dx1+1;
903                 h = dy2-dy1+1;
904
905                 if ( sx + w > bw ) {
906                         w = bw - sx;
907                         dx2 = dx1 + w - 1;
908                 }
909
910                 if ( sy + h > bh ) {
911                         h = bh - sy;
912                         dy2 = dy1 + h - 1;
913                 }
914
915                 if ( w < 1 ) return;            // clipped away!
916                 if ( h < 1 ) return;            // clipped away!
917
918         } while (reclip);
919
920         // Make sure clipping algorithm works
921         #ifndef NDEBUG
922                 Assert( w > 0 );
923                 Assert( h > 0 );
924                 Assert( w == (dx2-dx1+1) );
925                 Assert( h == (dy2-dy1+1) );
926                 Assert( sx >= 0 );
927                 Assert( sy >= 0 );
928                 Assert( sx+w <= bw );
929                 Assert( sy+h <= bh );
930                 Assert( dx2 >= dx1 );
931                 Assert( dy2 >= dy1 );
932                 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
933                 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
934                 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
935                 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
936         #endif
937
938         // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
939         gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
940 }
941
942 void gr_opengl_aabitmap(int x, int y)
943 {
944         int w, h;
945
946         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
947         int dx1=x, dx2=x+w-1;
948         int dy1=y, dy2=y+h-1;
949         int sx=0, sy=0;
950
951         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
952         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
953         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
954         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
955         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
956         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
957
958         if ( sx < 0 ) return;
959         if ( sy < 0 ) return;
960         if ( sx >= w ) return;
961         if ( sy >= h ) return;
962
963         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
964         gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
965 }
966
967 void gr_opengl_string( int sx, int sy, char *s )
968 {
969         int width, spacing, letter;
970         int x, y;
971
972         if ( !Current_font )    {
973                 return;
974         }
975
976         gr_set_bitmap(Current_font->bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
977
978         x = sx;
979         y = sy;
980
981         if (sx==0x8000) {                       //centered
982                 x = get_centered_x(s);
983         } else {
984                 x = sx;
985         }
986         
987         spacing = 0;
988
989         while (*s)      {
990                 x += spacing;
991
992                 while (*s== '\n' )      {
993                         s++;
994                         y += Current_font->h;
995                         if (sx==0x8000) {                       //centered
996                                 x = get_centered_x(s);
997                         } else {
998                                 x = sx;
999                         }
1000                 }
1001                 if (*s == 0 ) break;
1002
1003                 letter = get_char_width(s[0],s[1],&width,&spacing);
1004                 s++;
1005
1006                 //not in font, draw as space
1007                 if (letter<0)   {
1008                         continue;
1009                 }
1010
1011                 int xd, yd, xc, yc;
1012                 int wc, hc;
1013
1014                 // Check if this character is totally clipped
1015                 if ( x + width < gr_screen.clip_left ) continue;
1016                 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1017                 if ( x > gr_screen.clip_right ) continue;
1018                 if ( y > gr_screen.clip_bottom ) continue;
1019
1020                 xd = yd = 0;
1021                 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1022                 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1023                 xc = x+xd;
1024                 yc = y+yd;
1025
1026                 wc = width - xd; hc = Current_font->h - yd;
1027                 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1028                 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1029
1030                 if ( wc < 1 ) continue;
1031                 if ( hc < 1 ) continue;
1032
1033                 int u = Current_font->bm_u[letter];
1034                 int v = Current_font->bm_v[letter];
1035
1036                 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1037         }
1038 }
1039
1040 void gr_opengl_line(int x1,int y1,int x2,int y2)
1041 {
1042         int clipped = 0, swapped=0;
1043
1044         gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1045         
1046         INT_CLIPLINE(x1,y1,x2,y2,gr_screen.clip_left,gr_screen.clip_top,gr_screen.clip_right,gr_screen.clip_bottom,return,clipped=1,swapped=1);
1047         
1048         float sx1, sy1;
1049         float sx2, sy2;
1050         
1051         sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1052         sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1053         sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1054         sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1055         
1056         if ( x1 == x2 && y1 == y2 ) {
1057                 glBegin (GL_POINTS);
1058                   glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1059                   glVertex3f (sx1, sy1, -0.99f);
1060                 glEnd ();
1061                 
1062                 return;
1063         }
1064         
1065         if ( x1 == x2 ) {
1066                 if ( sy1 < sy2 )    {
1067                         sy2 += 0.5f;
1068                 } else {
1069                         sy1 += 0.5f;
1070                 }
1071         } else if ( y1 == y2 )  {
1072                 if ( sx1 < sx2 )    {
1073                         sx2 += 0.5f;
1074                 } else {
1075                         sx1 += 0.5f;
1076                 }
1077         }
1078         
1079         glBegin (GL_LINES);
1080           glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1081           glVertex3f (sx2, sy2, -0.99f);
1082           glVertex3f (sx1, sy1, -0.99f);
1083         glEnd ();
1084 }
1085
1086 void gr_opengl_aaline(vertex *v1, vertex *v2)
1087 {
1088         gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1089 }
1090
1091 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1092 {
1093         int clipped = 0, swapped=0;
1094
1095         if ( !gr_screen.current_color.is_alphacolor )   {
1096                 gr_line( x1, y1, x2, y2 );
1097                 return;
1098         }
1099
1100         INT_CLIPLINE(x1,y1,x2,y2,gr_screen.clip_left,gr_screen.clip_top,gr_screen.clip_right,gr_screen.clip_bottom,return,clipped=1,swapped=1);
1101
1102         gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1103
1104         int aa = swapped ? 0 : gr_screen.current_color.alpha;
1105         int ba = swapped ? gr_screen.current_color.alpha : 0;
1106         
1107         float sx1, sy1;
1108         float sx2, sy2;
1109         
1110         sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1111         sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1112         sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1113         sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1114
1115         if ( x1 == x2 ) {
1116                 if ( sy1 < sy2 )    {
1117                         sy2 += 0.5f;
1118                 } else {
1119                         sy1 += 0.5f;
1120                 }
1121         } else if ( y1 == y2 )  {
1122                 if ( sx1 < sx2 )    {
1123                         sx2 += 0.5f;
1124                 } else {
1125                         sx1 += 0.5f;
1126                 }
1127         }
1128         
1129         glBegin (GL_LINES);
1130           glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1131           glVertex3f (sx2, sy2, -0.99f);
1132           glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1133           glVertex3f (sx1, sy1, -0.99f);
1134         glEnd ();       
1135 }
1136
1137 void gr_opengl_circle( int xc, int yc, int d )
1138 {
1139         int p,x, y, r;
1140
1141         r = d/2;
1142         p=3-d;
1143         x=0;
1144         y=r;
1145
1146         // Big clip
1147         if ( (xc+r) < gr_screen.clip_left ) return;
1148         if ( (xc-r) > gr_screen.clip_right ) return;
1149         if ( (yc+r) < gr_screen.clip_top ) return;
1150         if ( (yc-r) > gr_screen.clip_bottom ) return;
1151
1152         while(x<y)      {
1153                 // Draw the first octant
1154                 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1155                 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1156                                 
1157                 if (p<0) 
1158                         p=p+(x<<2)+6;
1159                 else    {
1160                         // Draw the second octant
1161                         gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1162                         gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1163                                                 
1164                         p=p+((x-y)<<2)+10;
1165                         y--;
1166                 }
1167                 x++;
1168         }
1169         if(x==y) {
1170                 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1171                 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1172         }
1173         return;
1174 }
1175
1176 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1177 {
1178         float f_float;
1179         
1180         f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1181         if (f_float < 0.0f) {
1182                 f_float = 0.0f;
1183         } else {
1184                 f_float = 1.0f;
1185         }
1186         *r = 0;
1187         *g = 0;
1188         *b = 0;
1189         *a = (int)(f_float * 255.0);
1190 }
1191
1192 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1193 {
1194         int i;
1195         float u_scale = 1.0f, v_scale = 1.0f;
1196
1197         // Make nebula use the texture mapper... this blends the colors better.
1198         if ( flags & TMAP_FLAG_NEBULA ){
1199                 Int3 ();
1200         }
1201
1202         gr_texture_source texture_source = (gr_texture_source)-1;
1203         gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1204         gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1205         
1206         if ( gr_zbuffering )    {
1207                 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER)   )       {
1208                         zbuffer_type = ZBUFFER_TYPE_READ;
1209                 } else {
1210                         zbuffer_type = ZBUFFER_TYPE_FULL;
1211                 }
1212         } else {
1213                 zbuffer_type = ZBUFFER_TYPE_NONE;
1214         }
1215         
1216         int alpha;
1217
1218         int tmap_type = TCACHE_TYPE_NORMAL;
1219
1220         int r, g, b;
1221
1222         if ( flags & TMAP_FLAG_TEXTURED )       {
1223                 r = g = b = 255;
1224         } else {
1225                 r = gr_screen.current_color.red;
1226                 g = gr_screen.current_color.green;
1227                 b = gr_screen.current_color.blue;
1228         }
1229
1230         if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )        
1231         {
1232                 if (1) {
1233                         tmap_type = TCACHE_TYPE_NORMAL;
1234                         alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1235                         
1236                         // Blend with screen pixel using src*alpha+dst
1237                         float factor = gr_screen.current_alpha;
1238                         
1239                         alpha = 255;
1240                         
1241                         if ( factor <= 1.0f )   {
1242                                 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1243                                 r = (r*tmp_alpha)/255;
1244                                 g = (g*tmp_alpha)/255;
1245                                 b = (b*tmp_alpha)/255;
1246                         }
1247                 } else {
1248                         tmap_type = TCACHE_TYPE_XPARENT;
1249                         
1250                         alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1251                         
1252                         // Blend with screen pixel using src*alpha+dst
1253                         float factor = gr_screen.current_alpha;
1254                                 
1255                         if ( factor > 1.0f )    {
1256                                 alpha = 255;
1257                         } else {
1258                                 alpha = fl2i(gr_screen.current_alpha*255.0f);
1259                         }
1260                 }
1261         } else {
1262                 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1263                         alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1264                 } else {
1265                         alpha_blend = ALPHA_BLEND_NONE;
1266                 }
1267                 alpha = 255;
1268         }
1269
1270         if(flags & TMAP_FLAG_BITMAP_SECTION){
1271                 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1272         }
1273         
1274         texture_source = TEXTURE_SOURCE_NONE;
1275         
1276         if ( flags & TMAP_FLAG_TEXTURED )       {
1277                 if ( !gr_tcache_set(gr_screen.current_bitmap, tmap_type, &u_scale, &v_scale, 0, gr_screen.current_bitmap_sx, gr_screen.current_bitmap_sy, 0 ))
1278                 {
1279                         mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1280                         return;
1281                 }
1282
1283                 // use nonfiltered textures for bitmap sections
1284                 if(flags & TMAP_FLAG_BITMAP_SECTION){
1285                         texture_source = TEXTURE_SOURCE_NO_FILTERING;
1286                 } else {
1287                         texture_source = TEXTURE_SOURCE_DECAL;
1288                 }
1289         }
1290
1291
1292         gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1293         
1294         if ( flags & TMAP_FLAG_TEXTURED )
1295         {
1296                 // rendition junk
1297                 // STUB_FUNCTION;
1298         }
1299
1300         if (flags & TMAP_FLAG_PIXEL_FOG) {
1301                 int r, g, b;
1302                 int ra, ga, ba;
1303                 ra = ga = ba = 0;
1304         
1305                 /* argh */
1306                 for (i=nv-1;i>=0;i--)   // DDOI - change polygon winding
1307                 {
1308                         vertex * va = verts[i];
1309                         float sx, sy;
1310                 
1311                         int x, y;
1312                         x = fl2i(va->sx*16.0f);
1313                         y = fl2i(va->sy*16.0f);
1314                 
1315                         x += gr_screen.offset_x*16;
1316                         y += gr_screen.offset_y*16;
1317                 
1318                         sx = i2fl(x) / 16.0f;
1319                         sy = i2fl(y) / 16.0f;
1320                 
1321                         neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1322                         
1323                         ra += r;
1324                         ga += g;
1325                         ba += b;
1326                 }
1327                 
1328                 ra /= nv;
1329                 ga /= nv;
1330                 ba /= nv;
1331                 
1332                 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba, -1.0f, -1.0f);
1333         }
1334         
1335         glBegin(GL_TRIANGLE_FAN);
1336         for (i = nv-1; i >= 0; i--) {           
1337                 vertex * va = verts[i];
1338                 float sx, sy, sz;
1339                 float tu, tv;
1340                 float rhw;
1341                 int a;
1342                 
1343                 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) )      {
1344                         sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1345                         
1346                         if ( sz > 0.98f ) {
1347                                 sz = 0.98f;
1348                         }
1349                 } else {
1350                         sz = 0.99f;
1351                 }
1352
1353                 if ( flags & TMAP_FLAG_CORRECT )        {
1354                         rhw = va->sw;
1355                 } else {
1356                         rhw = 1.0f;
1357                 }
1358                 
1359                 if (flags & TMAP_FLAG_ALPHA) {
1360                         a = verts[i]->a;
1361                 } else {
1362                         a = alpha;
1363                 }
1364
1365                 if (flags & TMAP_FLAG_NEBULA ) {
1366                         int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1367                         r = gr_palette[pal*3+0];
1368                         g = gr_palette[pal*3+1];
1369                         b = gr_palette[pal*3+2];
1370                 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) )   {
1371                         r = Gr_gamma_lookup[verts[i]->b];
1372                         g = Gr_gamma_lookup[verts[i]->b];
1373                         b = Gr_gamma_lookup[verts[i]->b];
1374                 } else if ( (flags & TMAP_FLAG_RGB)  && (flags & TMAP_FLAG_GOURAUD) )   {
1375                         // Make 0.75 be 256.0f
1376                         r = Gr_gamma_lookup[verts[i]->r];
1377                         g = Gr_gamma_lookup[verts[i]->g];
1378                         b = Gr_gamma_lookup[verts[i]->b];
1379                 } else {
1380                         // use constant RGB values...
1381                 }
1382                 glColor4ub (r,g,b,a);
1383
1384                 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1385                         int sr, sg, sb, sa;
1386                         
1387                         /* this is for GL_EXT_SECONDARY_COLOR */
1388                         gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1389                         /* do separate color call here */
1390                         
1391                         STUB_FUNCTION;
1392                 }
1393                 
1394                 int x, y;
1395                 x = fl2i(va->sx*16.0f);
1396                 y = fl2i(va->sy*16.0f);
1397
1398                 x += gr_screen.offset_x*16;
1399                 y += gr_screen.offset_y*16;
1400                 
1401                 sx = i2fl(x) / 16.0f;
1402                 sy = i2fl(y) / 16.0f;
1403
1404                 if ( flags & TMAP_FLAG_TEXTURED )       {
1405                         tu = va->u*u_scale;
1406                         tv = va->v*v_scale;
1407                         glTexCoord2f(tu, tv);
1408                 }
1409                 
1410                 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1411         }
1412         glEnd();
1413 }
1414
1415 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1416 {
1417         gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1418 }
1419
1420 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1421
1422 void gr_opengl_scaler(vertex *va, vertex *vb )
1423 {
1424         float x0, y0, x1, y1;
1425         float u0, v0, u1, v1;
1426         float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1427         float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1428         float xmin, xmax, ymin, ymax;
1429         int dx0, dy0, dx1, dy1;
1430
1431         //============= CLIP IT =====================
1432
1433         x0 = va->sx; y0 = va->sy;
1434         x1 = vb->sx; y1 = vb->sy;
1435
1436         xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1437         xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1438
1439         u0 = va->u; v0 = va->v;
1440         u1 = vb->u; v1 = vb->v;
1441
1442         // Check for obviously offscreen bitmaps...
1443         if ( (y1<=y0) || (x1<=x0) ) return;
1444         if ( (x1<xmin ) || (x0>xmax) ) return;
1445         if ( (y1<ymin ) || (y0>ymax) ) return;
1446
1447         clipped_u0 = u0; clipped_v0 = v0;
1448         clipped_u1 = u1; clipped_v1 = v1;
1449
1450         clipped_x0 = x0; clipped_y0 = y0;
1451         clipped_x1 = x1; clipped_y1 = y1;
1452
1453         // Clip the left, moving u0 right as necessary
1454         if ( x0 < xmin )        {
1455                 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1456                 clipped_x0 = xmin;
1457         }
1458
1459         // Clip the right, moving u1 left as necessary
1460         if ( x1 > xmax )        {
1461                 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1462                 clipped_x1 = xmax;
1463         }
1464
1465         // Clip the top, moving v0 down as necessary
1466         if ( y0 < ymin )        {
1467                 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1468                 clipped_y0 = ymin;
1469         }
1470
1471         // Clip the bottom, moving v1 up as necessary
1472         if ( y1 > ymax )        {
1473                 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1474                 clipped_y1 = ymax;
1475         }
1476         
1477         dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1478         dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1479
1480         if (dx1<=dx0) return;
1481         if (dy1<=dy0) return;
1482
1483         //============= DRAW IT =====================
1484
1485         vertex v[4];
1486         vertex *vl[4];
1487
1488         vl[0] = &v[0];  
1489         v->sx = clipped_x0;
1490         v->sy = clipped_y0;
1491         v->sw = va->sw;
1492         v->z = va->z;
1493         v->u = clipped_u0;
1494         v->v = clipped_v0;
1495
1496         vl[1] = &v[1];  
1497         v[1].sx = clipped_x1;
1498         v[1].sy = clipped_y0;
1499         v[1].sw = va->sw;
1500         v[1].z = va->z;
1501         v[1].u = clipped_u1;
1502         v[1].v = clipped_v0;
1503
1504         vl[2] = &v[2];  
1505         v[2].sx = clipped_x1;
1506         v[2].sy = clipped_y1;
1507         v[2].sw = va->sw;
1508         v[2].z = va->z;
1509         v[2].u = clipped_u1;
1510         v[2].v = clipped_v1;
1511
1512         vl[3] = &v[3];  
1513         v[3].sx = clipped_x0;
1514         v[3].sy = clipped_y1;
1515         v[3].sw = va->sw;
1516         v[3].z = va->z;
1517         v[3].u = clipped_u0;
1518         v[3].v = clipped_v1;
1519
1520         gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1521 }
1522
1523 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1524 {
1525 }
1526
1527 void gr_opengl_get_color( int * r, int * g, int * b )
1528 {
1529         if (r) *r = gr_screen.current_color.red;
1530         if (g) *g = gr_screen.current_color.green;
1531         if (b) *b = gr_screen.current_color.blue;
1532 }
1533
1534 void gr_opengl_init_color(color *c, int r, int g, int b)
1535 {
1536         c->screen_sig = gr_screen.signature;
1537         c->red = (unsigned char)r;
1538         c->green = (unsigned char)g;
1539         c->blue = (unsigned char)b;
1540         c->alpha = 255;
1541         c->ac_type = AC_TYPE_NONE;
1542         c->alphacolor = -1;
1543         c->is_alphacolor = 0;
1544         c->magic = 0xAC01;
1545 }
1546
1547 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1548 {
1549         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1550         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1551         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1552         if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1553
1554         gr_opengl_init_color( clr, r, g, b );
1555
1556         clr->alpha = (unsigned char)alpha;
1557         clr->ac_type = (ubyte)type;
1558         clr->alphacolor = -1;
1559         clr->is_alphacolor = 1;
1560 }
1561
1562 void gr_opengl_set_color( int r, int g, int b )
1563 {
1564         Assert((r >= 0) && (r < 256));
1565         Assert((g >= 0) && (g < 256));
1566         Assert((b >= 0) && (b < 256));
1567
1568         gr_opengl_init_color( &gr_screen.current_color, r, g, b );      
1569 }
1570
1571 void gr_opengl_set_color_fast(color *dst)
1572 {
1573         if ( dst->screen_sig != gr_screen.signature )   {
1574                 if ( dst->is_alphacolor )       {
1575                         gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1576                 } else {
1577                         gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1578                 }
1579         }
1580         gr_screen.current_color = *dst;
1581 }
1582
1583 void gr_opengl_print_screen(char *filename)
1584 {
1585         STUB_FUNCTION;
1586 }
1587
1588 int gr_opengl_supports_res_ingame(int res)
1589 {
1590         STUB_FUNCTION;
1591         
1592         return 1;
1593 }
1594
1595 int gr_opengl_supports_res_interface(int res)
1596 {
1597         STUB_FUNCTION;
1598         
1599         return 1;
1600 }
1601
1602 static void opengl_tcache_cleanup ();
1603
1604 void gr_opengl_cleanup()
1605 {
1606         if ( !Inited )  return;
1607
1608
1609         gr_reset_clip();
1610         gr_clear();
1611         gr_flip();
1612
1613         opengl_tcache_cleanup ();
1614
1615         Inited = 0;
1616 }
1617
1618 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1619 {
1620         Assert((r >= 0) && (r < 256));
1621         Assert((g >= 0) && (g < 256));
1622         Assert((b >= 0) && (b < 256));
1623         
1624         if (fog_mode == GR_FOGMODE_NONE) {
1625                 if (gr_screen.current_fog_mode != fog_mode) {
1626                         glDisable(GL_FOG);
1627                 }
1628                 gr_screen.current_fog_mode = fog_mode;
1629                 
1630                 return;
1631         }
1632         
1633         if (gr_screen.current_fog_mode != fog_mode) {
1634                 glEnable(GL_FOG);
1635                 
1636                 if (D3D_fog_mode == 2) {
1637                         glFogi(GL_FOG_MODE, GL_LINEAR);
1638                 }
1639                 
1640                 gr_screen.current_fog_mode = fog_mode;
1641         }
1642         
1643         if ( (gr_screen.current_fog_color.red != r) ||
1644                         (gr_screen.current_fog_color.green != g) ||
1645                         (gr_screen.current_fog_color.blue != b) ) {
1646                 GLfloat fc[4];
1647                 
1648                 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1649         
1650                 fc[0] = (float)r/255.0;
1651                 fc[1] = (float)g/255.0;
1652                 fc[2] = (float)b/255.0;
1653                 fc[3] = 1.0;
1654                 
1655                 glFogfv(GL_FOG_COLOR, fc);
1656         }
1657         
1658         if( (fog_near >= 0.0f) && (fog_far >= 0.0f) && 
1659                         ((fog_near != gr_screen.fog_near) || 
1660                         (fog_far != gr_screen.fog_far)) ) {
1661                 gr_screen.fog_near = fog_near;
1662                 gr_screen.fog_far = fog_far;
1663                 
1664                 if (D3D_fog_mode == 2) {
1665                         glFogf(GL_FOG_START, fog_near);
1666                         glFogf(GL_FOG_END, fog_far);
1667                 }
1668         }
1669 }
1670
1671 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1672 {
1673         // Not used.
1674 }
1675
1676 void gr_opengl_set_cull(int cull)
1677 {
1678         if (cull) {
1679                 glEnable (GL_CULL_FACE);
1680                 glFrontFace (GL_CCW);
1681         } else {
1682                 glDisable (GL_CULL_FACE);
1683         }
1684 }
1685
1686 void gr_opengl_filter_set(int filter)
1687 {
1688 }
1689
1690 // cross fade
1691 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1692 {
1693         if ( pct <= 50 ) {
1694                 gr_set_bitmap(bmap1, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1695                 gr_bitmap(x1, y1);
1696         } else {
1697                 gr_set_bitmap(bmap2, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1698                 gr_bitmap(x2, y2);
1699         }               
1700 }
1701
1702
1703 typedef struct tcache_slot_opengl {
1704         GLuint  texture_handle;
1705         float   u_scale, v_scale;
1706         int     bitmap_id;
1707         int     size;
1708         int     used_this_frame;
1709         int     time_created;
1710         ushort  w,h;
1711
1712         // sections
1713         tcache_slot_opengl      *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1714         tcache_slot_opengl      *parent;
1715         
1716         gr_texture_source       texture_mode;
1717 } tcache_slot_opengl;
1718
1719 static void *Texture_sections = NULL;
1720 static tcache_slot_opengl *Textures = NULL;
1721
1722 static tcache_slot_opengl *GL_bound_texture;
1723
1724 int GL_texture_sections = 0;
1725 int GL_texture_ram = 0;
1726 int GL_frame_count = 0;
1727 int GL_min_texture_width = 0;
1728 int GL_max_texture_width = 0;
1729 int GL_min_texture_height = 0;
1730 int GL_max_texture_height = 0;
1731 int GL_square_textures = 0;
1732 int GL_textures_in = 0;
1733 int GL_textures_in_frame = 0;
1734 int GL_last_bitmap_id = -1;
1735 int GL_last_detail = -1;
1736 int GL_last_bitmap_type = -1;
1737 int GL_last_section_x = -1;
1738 int GL_last_section_y = -1;
1739
1740 int vram_full = 0;
1741
1742 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1743
1744 static void gr_opengl_set_texture_state(gr_texture_source ts)
1745 {
1746         if (ts == TEXTURE_SOURCE_NONE) {
1747                 GL_bound_texture = NULL;
1748                                 
1749                 glBindTexture(GL_TEXTURE_2D, 0);
1750                 gr_tcache_set(-1, -1, NULL, NULL, 0, -1, -1, 0 );
1751         } else if (GL_bound_texture &&
1752                 GL_bound_texture->texture_mode != ts) {
1753                 switch (ts) {
1754                         case TEXTURE_SOURCE_DECAL:
1755                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1756                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1757                                 break;
1758                         case TEXTURE_SOURCE_NO_FILTERING:
1759                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1760                                 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1761                                 break;
1762                         default:
1763                                 break;
1764                 }
1765                         
1766                 GL_bound_texture->texture_mode = ts;
1767         }
1768         
1769         GL_current_texture_source = ts;
1770 }
1771
1772 static int opengl_max_tex_size_get()
1773 {
1774         GLint max_texture_size = 0;
1775
1776         glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1777         return max_texture_size;
1778 }
1779
1780 static void opengl_tcache_init (int use_sections)
1781 {
1782         int i, idx, s_idx;
1783
1784         // DDOI - FIXME skipped a lot of stuff here
1785         GL_should_preload = 0;
1786
1787         //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1788         uint tmp_pl = 1;
1789
1790         if ( tmp_pl == 0 )      {
1791                 GL_should_preload = 0;
1792         } else if ( tmp_pl == 1 )       {
1793                 GL_should_preload = 1;
1794         } else {
1795                 GL_should_preload = 1;
1796         }
1797
1798         STUB_FUNCTION;
1799
1800         GL_min_texture_width = 16;
1801         GL_min_texture_height = 16;
1802
1803         GL_max_texture_width = opengl_max_tex_size_get();
1804         GL_max_texture_height = opengl_max_tex_size_get();
1805
1806         Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1807         if ( !Textures )        {
1808                 exit(1);
1809         }
1810
1811         if(use_sections){
1812                 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1813                 if(!Texture_sections){
1814                         exit(1);
1815                 }
1816                 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1817         }
1818
1819         // Init the texture structures
1820         int section_count = 0;
1821         for( i=0; i<MAX_BITMAPS; i++ )  {
1822                 /*
1823                 Textures[i].vram_texture = NULL;
1824                 Textures[i].vram_texture_surface = NULL;
1825                 */
1826                 Textures[i].texture_handle = 0;
1827
1828                 Textures[i].bitmap_id = -1;
1829                 Textures[i].size = 0;
1830                 Textures[i].used_this_frame = 0;
1831
1832                 Textures[i].parent = NULL;
1833
1834                 // allocate sections
1835                 if(use_sections){
1836                         for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1837                                 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1838                                         Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1839                                         Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1840                                         /*
1841                                         Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1842                                         Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1843                                         */
1844                                         Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1845                                         Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1846                                         Textures[i].data_sections[idx][s_idx]->size = 0;
1847                                         Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1848                                 }
1849                         }
1850                 } else {
1851                         for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1852                                 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1853                                         Textures[i].data_sections[idx][s_idx] = NULL;
1854                                 }
1855                         }
1856                 }
1857         }
1858
1859         GL_texture_sections = use_sections;
1860
1861         GL_last_detail = Detail.hardware_textures;
1862         GL_last_bitmap_id = -1;
1863         GL_last_bitmap_type = -1;
1864
1865         GL_last_section_x = -1;
1866         GL_last_section_y = -1;
1867
1868         GL_textures_in = 0;
1869         GL_textures_in_frame = 0;
1870 }
1871
1872 static int opengl_free_texture (tcache_slot_opengl *t);
1873
1874 void opengl_free_texture_with_handle(int handle)
1875 {
1876         for(int i=0; i<MAX_BITMAPS; i++ )  {
1877                 if (Textures[i].bitmap_id == handle) {
1878                         Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1879                         opengl_free_texture ( &Textures[i] );
1880                 }
1881         }
1882 }
1883
1884 static void opengl_tcache_flush ()
1885 {
1886         int i;
1887
1888         for( i=0; i<MAX_BITMAPS; i++ )  {
1889                 opengl_free_texture ( &Textures[i] );
1890         }
1891         if (GL_textures_in != 0) {
1892                 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1893                 GL_textures_in = 0;
1894         }
1895
1896         GL_last_bitmap_id = -1;
1897         GL_last_section_x = -1;
1898         GL_last_section_y = -1;
1899 }
1900
1901 static void opengl_tcache_cleanup ()
1902 {
1903         opengl_tcache_flush ();
1904
1905         GL_textures_in = 0;
1906         GL_textures_in_frame = 0;
1907
1908         if ( Textures ) {
1909                 free(Textures);
1910                 Textures = NULL;
1911         }
1912
1913         if( Texture_sections != NULL ){
1914                 free(Texture_sections);
1915                 Texture_sections = NULL;
1916         }
1917 }
1918
1919 static void opengl_tcache_frame ()
1920 {
1921         GL_last_bitmap_id = -1;
1922         GL_textures_in_frame = 0;
1923
1924         GL_frame_count++;
1925
1926         /*
1927         int idx, s_idx;
1928         int i;
1929         for( i=0; i<MAX_BITMAPS; i++ )  {
1930                 Textures[i].used_this_frame = 0;
1931
1932                 // data sections
1933                 if(Textures[i].data_sections[0][0] != NULL){
1934                         Assert(GL_texture_sections);
1935                         if(GL_texture_sections){
1936                                 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1937                                         for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1938                                                 if(Textures[i].data_sections[idx][s_idx] != NULL){
1939                                                         Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1940                                                 }
1941                                         }
1942                                 }
1943                         }
1944                 }
1945         }
1946         */
1947
1948         if ( vram_full )        {
1949                 opengl_tcache_flush();
1950                 vram_full = 0;
1951         }
1952 }
1953
1954 static int opengl_free_texture ( tcache_slot_opengl *t )
1955 {
1956         int idx, s_idx;
1957         
1958
1959         // Bitmap changed!!     
1960         if ( t->bitmap_id > -1 )        {
1961                 // if I, or any of my children have been used this frame, bail  
1962                 if(t->used_this_frame == GL_frame_count){
1963                         return 0;
1964                 }
1965                 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1966                         for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1967                                 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
1968                                         return 0;
1969                                 }
1970                         }
1971                 }
1972
1973                 // ok, now we know its legal to free everything safely
1974                 t->texture_mode = (gr_texture_source) -1;
1975                 glDeleteTextures (1, &t->texture_handle);
1976                 t->texture_handle = 0;
1977
1978                 if ( GL_last_bitmap_id == t->bitmap_id )       {
1979                         GL_last_bitmap_id = -1;
1980                 }
1981
1982                 // if this guy has children, free them too, since the children
1983                 // actually make up his size
1984                 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1985                         for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1986                                 if(t->data_sections[idx][s_idx] != NULL){
1987                                         opengl_free_texture(t->data_sections[idx][s_idx]);
1988                                 }
1989                         }
1990                 }
1991
1992                 t->bitmap_id = -1;
1993                 t->used_this_frame = 0;
1994                 GL_textures_in -= t->size;
1995         }
1996
1997         return 1;
1998 }
1999
2000 static void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
2001 {
2002         int tex_w, tex_h;
2003         int i;
2004         
2005         // bogus
2006         if((w_out == NULL) ||  (h_out == NULL)){
2007                 return;
2008         }
2009
2010         // starting size
2011         tex_w = w_in;
2012         tex_h = h_in;
2013
2014         // set height and width to a power of 2
2015         for (i=0; i<16; i++ )   {
2016                 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) )        {
2017                         tex_w = 1 << (i+1);
2018                         break;
2019                 }
2020         }
2021
2022         for (i=0; i<16; i++ )   {
2023                 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) )        {
2024                         tex_h = 1 << (i+1);
2025                         break;
2026                 }
2027         }
2028
2029         // try to keep an 8:1 size ratio
2030         if (tex_w/tex_h > 8)
2031                 tex_h = tex_w/8;
2032         if (tex_h/tex_w > 8)
2033                 tex_w = tex_h/8;
2034
2035         if ( tex_w < GL_min_texture_width ) {
2036                 tex_w = GL_min_texture_width;
2037         } else if ( tex_w > GL_max_texture_width )     {
2038                 tex_w = GL_max_texture_width;
2039         }
2040
2041         if ( tex_h < GL_min_texture_height ) {
2042                 tex_h = GL_min_texture_height;
2043         } else if ( tex_h > GL_max_texture_height )    {
2044                 tex_h = GL_max_texture_height;
2045         }
2046
2047         if ( GL_square_textures) {
2048                 int new_size;
2049                 // Make the both be equal to larger of the two
2050                 new_size = max(tex_w, tex_h);
2051                 tex_w = new_size;
2052                 tex_h = new_size;
2053         }
2054
2055         // store the outgoing size
2056         *w_out = tex_w;
2057         *h_out = tex_h;
2058 }
2059
2060 // data == start of bitmap data
2061 // sx == x offset into bitmap
2062 // sy == y offset into bitmap
2063 // src_w == absolute width of section on source bitmap
2064 // src_h == absolute height of section on source bitmap
2065 // bmap_w == width of source bitmap
2066 // bmap_h == height of source bitmap
2067 // tex_w == width of final texture
2068 // tex_h == height of final texture
2069 static int opengl_create_texture_sub(int bitmap_type, int texture_handle, ushort *data, int sx, int sy, int src_w, int src_h, int bmap_w, int bmap_h, int tex_w, int tex_h, tcache_slot_opengl *t, int reload, int fail_on_full)
2070 {
2071         int ret_val = 1;
2072         int size;
2073         
2074         // bogus
2075         if(t == NULL){
2076                 return 0;
2077         }
2078
2079         if ( t->used_this_frame == GL_frame_count )       {
2080                 mprintf(( "ARGHH!!! Texture already used this frame!  Cannot free it!\n" ));
2081                 return 0;
2082         }
2083         if ( !reload )  {
2084                 // gah
2085                 if(!opengl_free_texture(t)){
2086                         return 0;
2087                 }
2088         }
2089
2090         // get final texture size
2091         opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2092
2093         if ( (tex_w < 1) || (tex_h < 1) )       {
2094                 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2095                 return 0;
2096         }
2097
2098         if ( bitmap_type == TCACHE_TYPE_AABITMAP )      {
2099                 t->u_scale = (float)bmap_w / (float)tex_w;
2100                 t->v_scale = (float)bmap_h / (float)tex_h;
2101         } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2102                 t->u_scale = (float)src_w / (float)tex_w;
2103                 t->v_scale = (float)src_h / (float)tex_h;
2104         } else {
2105                 t->u_scale = 1.0f;
2106                 t->v_scale = 1.0f;
2107         }
2108
2109         if (!reload) {
2110                 glGenTextures (1, &t->texture_handle);
2111         }
2112         
2113         if (t->texture_handle == 0) {
2114                 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2115                 return 0;
2116         }
2117                 
2118         GL_bound_texture = t;
2119         
2120         GL_bound_texture->texture_mode = (gr_texture_source) -1;
2121         
2122         glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2123         
2124         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2125         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2126         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2127
2128         /* this should be set next anyway */    
2129 //      if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2130 //              gr_opengl_set_texture_state(GL_current_texture_source);
2131 //      }
2132
2133         size = 0;
2134                 
2135         switch (bitmap_type) {
2136
2137                 case TCACHE_TYPE_AABITMAP:
2138                         {
2139                         int i,j;
2140                         ubyte *bmp_data = ((ubyte*)data);
2141                         ubyte *texmem;
2142                         ubyte *texmemp;
2143                         
2144                         ubyte xlat[256];
2145                         
2146                         for (i=0; i<16; i++) {
2147                                 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2148                         }
2149                         xlat[15] = xlat[1];
2150                         for ( ; i<256; i++ )    {
2151                                 xlat[i] = xlat[0];
2152                         }
2153                         
2154                         texmem = (ubyte *) malloc (tex_w*tex_h);
2155                         texmemp = texmem;
2156                                                         
2157                         for (i=0;i<tex_h;i++)
2158                         {
2159                                 for (j=0;j<tex_w;j++)
2160                                 {
2161                                         if (i < bmap_h && j < bmap_w) {
2162                                                 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2163                                         } else {
2164                                                 *texmemp++ = 0;
2165                                         }
2166                                 }
2167                         }
2168
2169                         size = tex_w*tex_h;
2170                         
2171                         glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2172
2173                         free (texmem);
2174
2175                         }
2176                         break;
2177                 case TCACHE_TYPE_BITMAP_SECTION:
2178                         {
2179                                 int i,j;
2180                                 ubyte *bmp_data = ((ubyte*)data);
2181                                 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2182                                 ubyte *texmemp = texmem;
2183                                 
2184                                 for (i=0;i<tex_h;i++)
2185                                 {
2186                                         for (j=0;j<tex_w;j++)
2187                                         {
2188                                                 if (i < src_h && j < src_w) {
2189                                                         *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2190                                                         *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2191                                                 } else {
2192                                                         *texmemp++ = 0;
2193                                                         *texmemp++ = 0;
2194                                                 }
2195                                         }
2196                                 }
2197                                 
2198                                 size = tex_w*tex_h*2;
2199                                 
2200                                 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA,
2201                                         GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2202                                         
2203                                 free(texmem);
2204                                 break;
2205                         }
2206                 default:
2207                         {
2208                                 int i,j;
2209                                 ubyte *bmp_data = ((ubyte*)data);
2210                                 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2211                                 ubyte *texmemp = texmem;
2212                                 
2213                                 fix u, utmp, v, du, dv;
2214                                 
2215                                 u = v = 0;
2216                                 
2217                                 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2218                                 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2219                                 
2220                                 for (j=0;j<tex_h;j++)
2221                                 {
2222                                         utmp = u;
2223                                         for (i=0;i<tex_w;i++)
2224                                         {
2225                                                 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2226                                                 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2227                                                 utmp += du;
2228                                         }
2229                                         v += dv;
2230                                 }
2231
2232                                 size = tex_w*tex_h*2;
2233                                 
2234                                 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA,
2235                                         GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2236                                         
2237                                 free(texmem);
2238                                 break;
2239                         }
2240                         break;
2241         }
2242         
2243         t->bitmap_id = texture_handle;
2244         t->time_created = GL_frame_count;
2245         t->used_this_frame = 0;
2246         t->size = size;
2247         t->w = (ushort)tex_w;
2248         t->h = (ushort)tex_h;
2249         
2250         GL_textures_in_frame += t->size;
2251         if (!reload) {
2252                 GL_textures_in += t->size;
2253         }
2254
2255         return ret_val;
2256 }
2257
2258 static int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2259 {
2260         ubyte flags;
2261         bitmap *bmp;
2262         int final_w, final_h;
2263         ubyte bpp = 16;
2264         int reload = 0;
2265
2266         // setup texture/bitmap flags
2267         flags = 0;
2268         switch(bitmap_type){
2269                 case TCACHE_TYPE_AABITMAP:
2270                         flags |= BMP_AABITMAP;
2271                         bpp = 8;
2272                         break;
2273                 case TCACHE_TYPE_NORMAL:
2274                         flags |= BMP_TEX_OTHER;
2275                 case TCACHE_TYPE_XPARENT:
2276                         flags |= BMP_TEX_XPARENT;
2277                         break;
2278                 case TCACHE_TYPE_NONDARKENING:
2279                         Int3();
2280                         flags |= BMP_TEX_NONDARK;
2281                         break;
2282         }
2283
2284         // lock the bitmap into the proper format
2285         bmp = bm_lock(bitmap_handle, bpp, flags);
2286         if ( bmp == NULL ) {
2287                 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2288                 return 0;
2289         }
2290
2291         int max_w = bmp->w;
2292         int max_h = bmp->h;
2293
2294         
2295            // DDOI - TODO
2296         if ( bitmap_type != TCACHE_TYPE_AABITMAP )      {
2297                 // max_w /= D3D_texture_divider;
2298                 // max_h /= D3D_texture_divider;
2299
2300                 // Detail.debris_culling goes from 0 to 4.
2301                 max_w /= (16 >> Detail.hardware_textures);
2302                 max_h /= (16 >> Detail.hardware_textures);
2303         }
2304         
2305
2306         // get final texture size as it will be allocated as a DD surface
2307         opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h); 
2308
2309         // if this tcache slot has no bitmap
2310         if ( tslot->bitmap_id < 0) {
2311                 reload = 0;
2312         }
2313         // different bitmap altogether - determine if the new one can use the old one's slot
2314         else if (tslot->bitmap_id != bitmap_handle)     {
2315                 if((final_w == tslot->w) && (final_h == tslot->h)){
2316                         reload = 1;
2317                         //ml_printf("Reloading texture %d\n", bitmap_handle);
2318                 } else {
2319                         reload = 0;
2320                 }
2321         }
2322
2323         // call the helper
2324         int ret_val = opengl_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, 0, 0, bmp->w, bmp->h, bmp->w, bmp->h, max_w, max_h, tslot, reload, fail_on_full);
2325
2326         // unlock the bitmap
2327         bm_unlock(bitmap_handle);
2328
2329         return ret_val;
2330 }
2331
2332 static int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2333 {
2334         ubyte flags;
2335         bitmap *bmp;
2336         int final_w, final_h;
2337         int section_x, section_y;
2338         int reload = 0;
2339
2340         // setup texture/bitmap flags
2341         Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2342         if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2343                 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2344         }
2345         flags = BMP_TEX_XPARENT;
2346
2347         // lock the bitmap in the proper format
2348         bmp = bm_lock(bitmap_handle, 16, flags);
2349         if ( bmp == NULL ) {
2350                 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2351                 return 0;
2352         }
2353         // determine the width and height of this section
2354         bm_get_section_size(bitmap_handle, sx, sy, &section_x, &section_y);
2355
2356         // get final texture size as it will be allocated as an opengl texture
2357         opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2358
2359         // if this tcache slot has no bitmap
2360         if ( tslot->bitmap_id < 0) {
2361                 reload = 0;
2362         }
2363         // different bitmap altogether - determine if the new one can use the old one's slot
2364         else if (tslot->bitmap_id != bitmap_handle)     {
2365                 if((final_w == tslot->w) && (final_h == tslot->h)){
2366                         reload = 1;
2367                 } else {
2368                         reload = 0;
2369                 }
2370         }
2371
2372         // call the helper
2373         int ret_val = opengl_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, bmp->sections.sx[sx], bmp->sections.sy[sy], section_x, section_y, bmp->w, bmp->h, section_x, section_y, tslot, reload, fail_on_full);
2374
2375         // unlock the bitmap
2376         bm_unlock(bitmap_handle);
2377
2378         return ret_val;
2379 }
2380
2381                 
2382 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2383 static int gr_opengl_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int fail_on_full = 0, int sx = -1, int sy = -1, int force = 0)
2384 {
2385         bitmap *bmp = NULL;
2386
2387         int idx, s_idx;
2388         int ret_val = 1;
2389
2390         if (bitmap_id < 0)
2391         {
2392                 GL_last_bitmap_id = -1;
2393                 return 0;
2394         }
2395
2396         if ( GL_last_detail != Detail.hardware_textures )      {
2397                 GL_last_detail = Detail.hardware_textures;
2398                 opengl_tcache_flush();
2399         }
2400
2401         if (vram_full) {
2402                 return 0;
2403         }
2404
2405         int n = bm_get_cache_slot (bitmap_id, 1);
2406         tcache_slot_opengl *t = &Textures[n];
2407
2408         if ( (GL_last_bitmap_id == bitmap_id) && (GL_last_bitmap_type==bitmap_type) && (t->bitmap_id == bitmap_id) && (GL_last_section_x == sx) && (GL_last_section_y == sy))       {
2409                 t->used_this_frame = GL_frame_count;
2410
2411                 // mark all children as used
2412                 if(GL_texture_sections){
2413                         for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2414                                 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2415                                         if(t->data_sections[idx][s_idx] != NULL){
2416                                                 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2417                                         }
2418                                 }
2419                         }
2420                 }
2421
2422                 *u_scale = t->u_scale;
2423                 *v_scale = t->v_scale;
2424                 return 1;
2425         }
2426
2427         if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2428                 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2429                 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2430                         return 0;
2431                 }
2432
2433                 ret_val = 1;
2434
2435                 // if the texture sections haven't been created yet
2436                 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2437
2438                         // lock the bitmap in the proper format
2439                         bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2440                         bm_unlock(bitmap_id);
2441
2442                         // now lets do something for each texture
2443
2444                         for(idx=0; idx<bmp->sections.num_x; idx++){
2445                                 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2446                                         // hmm. i'd rather we didn't have to do it this way...
2447                                         if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2448                                                 ret_val = 0;
2449                                         }
2450
2451                                         // not used this frame
2452                                         t->data_sections[idx][s_idx]->used_this_frame = 0;
2453                                 }
2454                         }
2455
2456                         // zero out pretty much everything in the parent struct since he's just the root
2457                         t->bitmap_id = bitmap_id;
2458                         t->texture_handle = 0;
2459                         t->time_created = t->data_sections[sx][sy]->time_created;
2460                         t->used_this_frame = 0;
2461                         /*
2462                         t->vram_texture = NULL;
2463                         t->vram_texture_surface = NULL
2464                         */
2465                 }
2466
2467                 // argh. we failed to upload. free anything we can
2468                 if(!ret_val){
2469                         opengl_free_texture(t);
2470                 }
2471                 // swap in the texture we want
2472                 else {
2473                         t = t->data_sections[sx][sy];
2474                 }
2475         }
2476         // all other "normal" textures
2477         else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2478                 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2479         }
2480
2481         // everything went ok
2482         if(ret_val && (t->texture_handle) && !vram_full){
2483                 *u_scale = t->u_scale;
2484                 *v_scale = t->v_scale;
2485
2486                 GL_bound_texture = t;
2487                 
2488                 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2489
2490                 /* this should be set next anyway */
2491 //              if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2492 //                      gr_opengl_set_texture_state(GL_current_texture_source);
2493 //              }
2494                 
2495                 GL_last_bitmap_id = t->bitmap_id;
2496                 GL_last_bitmap_type = bitmap_type;
2497                 GL_last_section_x = sx;
2498                 GL_last_section_y = sy;
2499
2500                 t->used_this_frame = GL_frame_count;
2501         }
2502         // gah
2503         else {
2504                 GL_last_bitmap_id = -1;
2505                 GL_last_bitmap_type = -1;
2506
2507                 GL_last_section_x = -1;
2508                 GL_last_section_y = -1;
2509
2510                 GL_bound_texture = NULL;
2511                 
2512                 glBindTexture (GL_TEXTURE_2D, 0);       // test - DDOI
2513                 return 0;
2514         }
2515
2516         return 1;
2517 }
2518
2519 void gr_opengl_set_clear_color(int r, int g, int b)
2520 {
2521         gr_init_color (&gr_screen.current_clear_color, r, g, b);
2522 }
2523
2524 void gr_opengl_flash(int r, int g, int b)
2525 {
2526         CAP(r,0,255);
2527         CAP(g,0,255);
2528         CAP(b,0,255);
2529         
2530         if ( r || g || b ) {
2531                 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2532                 
2533                 float x1, x2, y1, y2;
2534                 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2535                 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2536                 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2537                 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2538                 
2539                 glColor4ub(r, g, b, 255);
2540                 glBegin (GL_QUADS);
2541                   glVertex3f (x1, y2, -0.99);
2542
2543                   glVertex3f (x2, y2, -0.99);
2544
2545                   glVertex3f (x2, y1, -0.99);
2546
2547                   glVertex3f (x1, y1, -0.99);
2548                 glEnd ();         
2549         }
2550 }
2551
2552 int gr_opengl_zbuffer_get()
2553 {
2554         if ( !gr_global_zbuffering )    {
2555                 return GR_ZBUFF_NONE;
2556         }
2557         return gr_zbuffering_mode;
2558 }
2559
2560 int gr_opengl_zbuffer_set(int mode)
2561 {
2562         int tmp = gr_zbuffering_mode;
2563
2564         gr_zbuffering_mode = mode;
2565
2566         if (gr_zbuffering_mode == GR_ZBUFF_NONE )      {
2567                 gr_zbuffering = 0;
2568         } else {
2569                 gr_zbuffering = 1;
2570         }
2571         return tmp;
2572 }
2573
2574 void gr_opengl_zbuffer_clear(int mode)
2575 {
2576         if (mode) {
2577                 gr_zbuffering = 1;
2578                 gr_zbuffering_mode = GR_ZBUFF_FULL;
2579                 gr_global_zbuffering = 1;
2580                 
2581                 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2582                 glClear ( GL_DEPTH_BUFFER_BIT );
2583         } else {
2584                 gr_zbuffering = 0;
2585                 gr_zbuffering_mode = GR_ZBUFF_NONE;
2586                 gr_global_zbuffering = 0;
2587         }
2588 }
2589
2590 void gr_opengl_set_gamma(float gamma)
2591 {
2592         Gr_gamma = gamma;
2593         Gr_gamma_int = int (Gr_gamma*10);
2594
2595         // Create the Gamma lookup table
2596         int i;
2597         for (i=0;i<256; i++) {
2598                 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2599                 if ( v > 255 ) {
2600                         v = 255;
2601                 } else if ( v < 0 )     {
2602                         v = 0;
2603                 }
2604                 Gr_gamma_lookup[i] = v;
2605         }
2606
2607         // Flush any existing textures
2608         opengl_tcache_flush();
2609 }
2610
2611 void gr_opengl_fade_in(int instantaneous)
2612 {
2613         // Empty - DDOI
2614 }
2615
2616 void gr_opengl_fade_out(int instantaneous)
2617 {
2618         // Empty - DDOI
2619 }
2620
2621 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2622 {
2623         if (front) {
2624                 glReadBuffer(GL_FRONT);
2625         } else {
2626                 glReadBuffer(GL_BACK);
2627         }
2628         
2629         gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2630         
2631         glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2632         
2633         if (gr_screen.bits_per_pixel == 15) {
2634                 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2635         } else if (gr_screen.bits_per_pixel == 32) {
2636                 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2637         }
2638         
2639         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2640 }
2641
2642 static int Gr_opengl_mouse_saved = 0;
2643 static int Gr_opengl_mouse_saved_x1 = 0;
2644 static int Gr_opengl_mouse_saved_y1 = 0;
2645 static int Gr_opengl_mouse_saved_x2 = 0;
2646 static int Gr_opengl_mouse_saved_y2 = 0;
2647 static int Gr_opengl_mouse_saved_w = 0;
2648 static int Gr_opengl_mouse_saved_h = 0;
2649 #define MAX_SAVE_SIZE (32*32)
2650 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2651
2652 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2653
2654 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2655 {
2656         Gr_opengl_mouse_saved_x1 = x;
2657         Gr_opengl_mouse_saved_y1 = y;
2658         Gr_opengl_mouse_saved_x2 = x+w-1;
2659         Gr_opengl_mouse_saved_y2 = y+h-1;
2660         
2661         CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2662         CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2663         CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2664         CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2665         
2666         Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2667         Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2668
2669         if ( Gr_opengl_mouse_saved_w < 1 ) return;
2670         if ( Gr_opengl_mouse_saved_h < 1 ) return;
2671         
2672         Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2673
2674         gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2675         
2676         glReadBuffer(GL_BACK);
2677         glReadPixels(x, gr_screen.max_h-y-1-h, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, Gr_opengl_mouse_saved_data);
2678         
2679         Gr_opengl_mouse_saved = 1;
2680 }
2681
2682 int gr_opengl_save_screen()
2683 {
2684         gr_reset_clip();
2685
2686         if ( Gr_saved_screen )  {
2687                 mprintf(( "Screen alread saved!\n" ));
2688                 return -1;
2689         }
2690
2691         Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2692         if (!Gr_saved_screen) {
2693                 mprintf(( "Couldn't get memory for saved screen!\n" ));
2694                 return -1;
2695         }
2696
2697         char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2698         if (!Gr_saved_screen_tmp) {
2699                 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2700                 return -1;
2701         }
2702         
2703         gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2704         
2705         glReadBuffer(GL_FRONT);
2706         glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, Gr_saved_screen_tmp);
2707         
2708         ubyte *sptr, *dptr;
2709         
2710         sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2711         dptr = (ubyte *)Gr_saved_screen;
2712         for (int j = 0; j < gr_screen.max_h; j++) {
2713                 sptr -= gr_screen.max_w*2;
2714                 memcpy(dptr, sptr, gr_screen.max_w*2);
2715                 dptr += gr_screen.max_w*2;
2716         }
2717         
2718         free(Gr_saved_screen_tmp);
2719         
2720         if (Gr_opengl_mouse_saved) {
2721                 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2722                 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2723                 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2724                         memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2725                 
2726                         sptr += 32*2;
2727                         dptr -= gr_screen.max_w*2;
2728                 }
2729         }
2730
2731         // this leaks texture handles, and the opengl doesn't currently 
2732         // perform some sort of garbage collection, so a hack was added
2733         // to bmpman to make it free textures when released
2734         Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2735         
2736         return Gr_saved_screen_bitmap;
2737 }
2738
2739 void gr_opengl_restore_screen(int id)
2740 {
2741         gr_reset_clip();
2742         
2743         if ( !Gr_saved_screen ) {
2744                 gr_clear();
2745                 return;
2746         }
2747
2748         gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2749         
2750         gr_set_bitmap(Gr_saved_screen_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
2751         gr_bitmap(0, 0);                
2752 }
2753
2754 void gr_opengl_free_screen(int id)
2755 {
2756         bm_release(Gr_saved_screen_bitmap);
2757         
2758         if ( Gr_saved_screen )  {
2759                 free( Gr_saved_screen );
2760                 Gr_saved_screen = NULL;
2761         }
2762 }
2763
2764 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2765 {
2766         STUB_FUNCTION;
2767 }
2768
2769 void gr_opengl_dump_frame_stop()
2770 {
2771         STUB_FUNCTION;
2772 }
2773
2774 void gr_opengl_dump_frame()
2775 {
2776         STUB_FUNCTION;
2777 }
2778
2779 uint gr_opengl_lock()
2780 {
2781         STUB_FUNCTION;
2782         
2783         return 1;
2784 }
2785         
2786 void gr_opengl_unlock()
2787 {
2788 }
2789
2790 void opengl_zbias(int bias)
2791 {
2792         if (bias) {
2793                 glEnable(GL_POLYGON_OFFSET_FILL);
2794                 glPolygonOffset(0.0, -bias);
2795         } else {
2796                 glDisable(GL_POLYGON_OFFSET_FILL);
2797         }
2798 }
2799                 
2800 extern char *Osreg_title;
2801 void gr_opengl_init()
2802 {
2803         if ( Inited )   {
2804                 gr_opengl_cleanup();
2805                 Inited = 0;
2806         }
2807
2808         mprintf(( "Initializing opengl graphics device...\n" ));
2809         Inited = 1;
2810
2811 #ifdef PLAT_UNIX        
2812         if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2813         {
2814                 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2815                 exit (1);
2816         }
2817
2818         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2819         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2820         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2821         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2822         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2823         
2824         int flags = SDL_OPENGL;
2825         
2826         if (!Cmdline_window && ( (os_config_read_uint( NULL, "Fullscreen", 1 ) == 1) || Cmdline_fullscreen ))
2827                 flags |= SDL_FULLSCREEN;
2828
2829         // grab mouse/key unless told otherwise, ignore when we are going fullscreen
2830         if ( !((flags & SDL_FULLSCREEN) || Cmdline_no_grab) ) {
2831                 SDL_WM_GrabInput(SDL_GRAB_ON);
2832         }
2833
2834         FSAA = os_config_read_uint( NULL, "FSAA", 1 );
2835         if ( FSAA ) {
2836             SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
2837             SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, FSAA );
2838         }
2839         
2840         if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2841         {
2842             mprintf(( "Couldn't set FSAA video mode: %s\n", SDL_GetError () ));
2843             SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0 );
2844             SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0 );
2845                 
2846             if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2847             {
2848                     fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ());
2849                     exit (1);
2850             }
2851         }
2852
2853         mprintf(( "Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel ));
2854         mprintf(( "\n" ));
2855         mprintf(( "Vendor     : %s\n", glGetString(GL_VENDOR) ));
2856         mprintf(( "Renderer   : %s\n", glGetString(GL_RENDERER) ));
2857         mprintf(( "Version    : %s\n", glGetString(GL_VERSION) ));
2858         mprintf(( "Extensions : %s\n", glGetString(GL_EXTENSIONS) ));
2859         mprintf(( "\n" ));
2860         
2861         int value;
2862         int rgb_size[3];
2863         int bpp = 15;
2864         rgb_size[0]=5;
2865         rgb_size[1]=5;
2866         rgb_size[2]=5;
2867
2868         SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
2869         mprintf(( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value ));
2870         SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
2871         mprintf(( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value ));
2872         SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
2873         mprintf(( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value ));
2874         SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
2875         mprintf(( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value ));
2876         SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
2877         mprintf(( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value ));
2878
2879         if ( FSAA ) {
2880                 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &value );
2881                 mprintf(( "SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value ));
2882                 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &value );
2883                 mprintf(( "SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", FSAA, value ));
2884         }
2885
2886         SDL_ShowCursor(0);
2887         SDL_WM_SetCaption (Osreg_title, NULL);
2888         
2889         /* might as well put this here */
2890         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2891 #endif
2892
2893         GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2894
2895         glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2896
2897         glMatrixMode(GL_PROJECTION);
2898         glLoadIdentity();
2899         glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2900         glMatrixMode(GL_MODELVIEW);
2901         glLoadIdentity();
2902         
2903         glShadeModel(GL_SMOOTH);
2904         glEnable(GL_DITHER);
2905         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2906         glHint(GL_FOG_HINT, GL_NICEST);
2907                 
2908         glEnable(GL_DEPTH_TEST);
2909         glEnable(GL_BLEND);
2910         
2911         glEnable(GL_TEXTURE_2D);
2912         
2913         glDepthRange(0.0, 1.0);
2914         
2915         glPixelStorei(GL_PACK_ALIGNMENT, 1);
2916         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2917         
2918         D3D_32bit = 1;              // grd3d.cpp
2919         extern int D3D_enabled;
2920         D3D_enabled = 1;
2921         /* 
2922           TODO: set fog_mode to 1 if EXT_secondary_color found and wanted 
2923           1 = use secondary color ext
2924           2 = use opengl linear fog
2925          */
2926         D3D_fog_mode = 2;          // grd3d.cpp
2927
2928         glFlush();
2929         
2930         Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2931         Gr_bitmap_poly = 1;
2932         
2933         switch( bpp )   {
2934         case 15:
2935                 gr_screen.bits_per_pixel = 15;
2936                 gr_screen.bytes_per_pixel = 2;
2937                 
2938                 Gr_red.bits = 5;
2939                 Gr_red.shift = 10;
2940                 Gr_red.scale = 8;
2941                 Gr_red.mask = 0x7C00;
2942
2943                 Gr_green.bits = 5;
2944                 Gr_green.shift = 5;
2945                 Gr_green.scale = 8;
2946                 Gr_green.mask = 0x3E0;
2947
2948                 Gr_blue.bits = 5;
2949                 Gr_blue.shift = 0;
2950                 Gr_blue.scale = 8;
2951                 Gr_blue.mask = 0x1F;
2952
2953                 break;
2954
2955         case 16:
2956                 gr_screen.bits_per_pixel = 16;
2957                 gr_screen.bytes_per_pixel = 2;
2958                 
2959                 Gr_red.bits = 5;
2960                 Gr_red.shift = 11;
2961                 Gr_red.scale = 8;
2962                 Gr_red.mask = 0xF800;
2963
2964                 Gr_green.bits = 6;
2965                 Gr_green.shift = 5;
2966                 Gr_green.scale = 4;
2967                 Gr_green.mask = 0x7E0;
2968
2969                 Gr_blue.bits = 5;
2970                 Gr_blue.shift = 0;
2971                 Gr_blue.scale = 8;
2972                 Gr_blue.mask = 0x1F;
2973
2974                 break;
2975
2976         case 32:
2977                 gr_screen.bits_per_pixel = 32;
2978                 gr_screen.bytes_per_pixel = 4;
2979                 
2980                 Gr_red.bits = 8;
2981                 Gr_red.shift = 16;
2982                 Gr_red.scale = 1;
2983                 Gr_red.mask = 0xff0000;
2984
2985                 Gr_green.bits = 8;
2986                 Gr_green.shift = 8;
2987                 Gr_green.scale = 1;
2988                 Gr_green.mask = 0xff00;
2989
2990                 Gr_blue.bits = 8;
2991                 Gr_blue.shift = 0;
2992                 Gr_blue.scale = 1;
2993                 Gr_blue.mask = 0xff;
2994
2995                 break;
2996
2997         default:
2998                 Int3(); // Illegal bpp
2999         }
3000
3001         // DDOI - set these so no one else does!
3002         Gr_t_red.mask = Gr_red.mask;
3003         Gr_t_red.shift = Gr_red.shift;
3004         Gr_t_red.scale = Gr_red.scale;
3005
3006         Gr_t_green.mask = Gr_green.mask;
3007         Gr_t_green.shift = Gr_green.shift;
3008         Gr_t_green.scale = Gr_green.scale;
3009         
3010         Gr_t_blue.mask = Gr_blue.mask;
3011         Gr_t_blue.shift = Gr_blue.shift;
3012         Gr_t_blue.scale = Gr_blue.scale;
3013         
3014         Gr_t_alpha.mask = 0x8000;
3015         Gr_t_alpha.scale = 255;
3016         Gr_t_alpha.shift = 15;
3017
3018         Gr_ta_red.mask = 0x0f00;
3019         Gr_ta_red.shift = 8;
3020         Gr_ta_red.scale = 16;
3021
3022         Gr_ta_green.mask = 0x00f0;
3023         Gr_ta_green.shift = 4;
3024         Gr_ta_green.scale = 16;
3025         
3026         Gr_ta_blue.mask = 0x000f;
3027         Gr_ta_blue.shift = 0;
3028         Gr_ta_blue.scale = 16;
3029
3030         Gr_ta_alpha.mask = 0xf000;
3031         Gr_ta_alpha.shift = 12;
3032         Gr_ta_alpha.scale = 16;
3033
3034
3035         opengl_tcache_init (1);
3036         gr_opengl_clear();
3037
3038         Gr_current_red = &Gr_red;
3039         Gr_current_blue = &Gr_blue;
3040         Gr_current_green = &Gr_green;
3041         Gr_current_alpha = &Gr_alpha;
3042
3043         gr_screen.gf_flip = gr_opengl_flip;
3044         gr_screen.gf_flip_window = gr_opengl_flip_window;
3045         gr_screen.gf_set_clip = gr_opengl_set_clip;
3046         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3047         gr_screen.gf_set_font = grx_set_font;
3048         
3049         gr_screen.gf_set_color = gr_opengl_set_color;
3050         gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3051         gr_screen.gf_create_shader = gr_opengl_create_shader;
3052         gr_screen.gf_set_shader = gr_opengl_set_shader;
3053         gr_screen.gf_clear = gr_opengl_clear;
3054         // gr_screen.gf_bitmap = gr_opengl_bitmap;
3055         // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3056         gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3057         gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3058         
3059         gr_screen.gf_rect = gr_opengl_rect;
3060         gr_screen.gf_shade = gr_opengl_shade;
3061         gr_screen.gf_string = gr_opengl_string;
3062         gr_screen.gf_circle = gr_opengl_circle;
3063
3064         gr_screen.gf_line = gr_opengl_line;
3065         gr_screen.gf_aaline = gr_opengl_aaline;
3066         gr_screen.gf_pixel = gr_opengl_pixel;
3067         gr_screen.gf_scaler = gr_opengl_scaler;
3068         gr_screen.gf_tmapper = gr_opengl_tmapper;
3069
3070         gr_screen.gf_gradient = gr_opengl_gradient;
3071
3072         gr_screen.gf_set_palette = gr_opengl_set_palette;
3073         gr_screen.gf_get_color = gr_opengl_get_color;
3074         gr_screen.gf_init_color = gr_opengl_init_color;
3075         gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3076         gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3077         gr_screen.gf_print_screen = gr_opengl_print_screen;
3078
3079         gr_screen.gf_fade_in = gr_opengl_fade_in;
3080         gr_screen.gf_fade_out = gr_opengl_fade_out;
3081         gr_screen.gf_flash = gr_opengl_flash;
3082         
3083         gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3084         gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3085         gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3086         
3087         gr_screen.gf_save_screen = gr_opengl_save_screen;
3088         gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3089         gr_screen.gf_free_screen = gr_opengl_free_screen;
3090         
3091         gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3092         gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3093         gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3094         
3095         gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3096         
3097         gr_screen.gf_lock = gr_opengl_lock;
3098         gr_screen.gf_unlock = gr_opengl_unlock;
3099         
3100         gr_screen.gf_fog_set = gr_opengl_fog_set;       
3101
3102         gr_screen.gf_get_region = gr_opengl_get_region;
3103
3104         gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3105
3106         gr_screen.gf_set_cull = gr_opengl_set_cull;
3107
3108         gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3109
3110         gr_screen.gf_filter_set = gr_opengl_filter_set;
3111
3112         gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3113
3114         gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;
3115         
3116         Mouse_hidden++;
3117         gr_reset_clip();
3118         gr_clear();
3119         gr_flip();
3120         Mouse_hidden--;
3121 }