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