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