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