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