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