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