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