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