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