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