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