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