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