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