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