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