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