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