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