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