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