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