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