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