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