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