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