]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/gropengl.cpp
"fixed" bitmap drawing.
[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.14  2002/05/29 08:54:40  relnev
11  * "fixed" bitmap drawing.
12  *
13  * copied more d3d code over.
14  *
15  * Revision 1.13  2002/05/29 06:25:13  theoddone33
16  * Keyboard input, mouse tracking now work
17  *
18  * Revision 1.12  2002/05/29 04:52:45  relnev
19  * bitmap
20  *
21  * Revision 1.11  2002/05/29 04:29:56  relnev
22  * removed some unncessary stubbing, implemented opengl rect
23  *
24  * Revision 1.10  2002/05/29 04:13:27  theoddone33
25  * enable opengl_line
26  *
27  * Revision 1.9  2002/05/29 03:35:51  relnev
28  * added rest of init
29  *
30  * Revision 1.8  2002/05/29 03:30:05  relnev
31  * update opengl stubs
32  *
33  * Revision 1.7  2002/05/29 02:52:32  theoddone33
34  * Enable OpenGL renderer
35  *
36  * Revision 1.6  2002/05/28 04:56:51  theoddone33
37  * runs a little bit now
38  *
39  * Revision 1.5  2002/05/28 04:07:28  theoddone33
40  * New graphics stubbing arrangement
41  *
42  * Revision 1.4  2002/05/27 23:39:34  relnev
43  * 0
44  *
45  * Revision 1.3  2002/05/27 22:35:01  theoddone33
46  * more symbols
47  *
48  * Revision 1.2  2002/05/27 22:32:02  theoddone33
49  * throw all d3d stuff at opengl
50  *
51  * Revision 1.1.1.1  2002/05/03 03:28:09  root
52  * Initial import.
53  *
54  * 
55  * 10    7/14/99 9:42a Dave
56  * Put in clear_color debug function. Put in base for 3dnow stuff / P3
57  * stuff
58  * 
59  * 9     7/09/99 9:51a Dave
60  * Added thick polyline code.
61  * 
62  * 8     6/29/99 10:35a Dave
63  * Interface polygon bitmaps! Whee!
64  * 
65  * 7     2/03/99 11:44a Dave
66  * Fixed d3d transparent textures.
67  * 
68  * 6     1/24/99 11:37p Dave
69  * First full rev of beam weapons. Very customizable. Removed some bogus
70  * Int3()'s in low level net code.
71  * 
72  * 5     12/18/98 1:13a Dave
73  * Rough 1024x768 support for Direct3D. Proper detection and usage through
74  * the launcher.
75  * 
76  * 4     12/06/98 2:36p Dave
77  * Drastically improved nebula fogging.
78  * 
79  * 3     11/11/98 5:37p Dave
80  * Checkin for multiplayer testing.
81  * 
82  * 2     10/07/98 10:53a Dave
83  * Initial checkin.
84  * 
85  * 1     10/07/98 10:49a Dave
86  * 
87  * 14    5/20/98 9:46p John
88  * added code so the places in code that change half the palette don't
89  * have to clear the screen.
90  * 
91  * 13    5/06/98 5:30p John
92  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
93  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
94  * DirectX header files and libs that fixed the Direct3D alpha blending
95  * problems.
96  * 
97  * 12    4/14/98 12:15p John
98  * Made 16-bpp movies work.
99  * 
100  * 11    3/12/98 5:36p John
101  * Took out any unused shaders.  Made shader code take rgbc instead of
102  * matrix and vector since noone used it like a matrix and it would have
103  * been impossible to do in hardware.   Made Glide implement a basic
104  * shader for online help.  
105  * 
106  * 10    3/10/98 4:18p John
107  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
108  * & Glide have popups and print screen.  Took out all >8bpp software
109  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
110  * support Fred.  Made zbuffering key off of functions rather than one
111  * global variable.
112  * 
113  * 9     12/02/97 4:00p John
114  * Added first rev of thruster glow, along with variable levels of
115  * translucency, which retquired some restructing of palman.
116  * 
117  * 8     10/03/97 9:10a John
118  * added better antialiased line drawer
119  * 
120  * 7     9/23/97 10:45a John
121  * made so you can tell bitblt code to rle a bitmap by passing flag to
122  * gr_set_bitmap
123  * 
124  * 6     9/09/97 11:01a Sandeep
125  * fixed warning level 4 bugs
126  * 
127  * 5     7/10/97 2:06p John
128  * added code to specify alphablending type for bitmaps.
129  * 
130  * 4     6/17/97 7:04p John
131  * added d3d support for gradients.
132  * fixed some color bugs by adding screen signatures instead of watching
133  * flags and palette changes.
134  * 
135  * 3     6/12/97 2:50a Lawrance
136  * bm_unlock() now passed bitmap number, not pointer
137  * 
138  * 2     6/11/97 1:12p John
139  * Started fixing all the text colors in the game.
140  * 
141  * 1     5/12/97 12:14p John
142  *
143  * $NoKeywords: $
144  */
145
146 #ifndef PLAT_UNIX
147 #include <windows.h>
148 #include <windowsx.h>
149 #endif
150 #include <GL/gl.h>
151
152 #include "osapi.h"
153 #include "2d.h"
154 #include "bmpman.h"
155 #include "floating.h"
156 #include "palman.h"
157 #include "grinternal.h"
158 #include "gropengl.h"
159 #include "line.h"
160
161 static int Inited = 0;
162
163 static GLuint bitmapTex;
164 static GLubyte *bitmapMem;
165
166 #ifdef PLAT_UNIX
167 // Throw in some dummy functions - DDOI
168
169 int D3D_32bit = 0;              // grd3d.cpp
170 int D3D_fog_mode = -1;          // grd3d.cpp
171 int D3D_inited = 0;             // grd3d.cpp
172 int D3D_zbias = 1;              // grd3d.cpp
173 int D3d_rendition_uvs = 0;      // grd3d.cpp
174
175 void gr_dd_activate(int active)         // grdirectdraw.cpp
176 {
177         STUB_FUNCTION;
178 }
179
180 void gr_directdraw_cleanup()            // grdirectdraw.cpp
181 {
182         STUB_FUNCTION;
183 }
184
185 void gr_directdraw_force_windowed()     // grdirectdraw.cpp
186 {
187         STUB_FUNCTION;
188 }
189
190 void gr_directdraw_init()
191 {
192         STUB_FUNCTION;
193 }
194
195
196 void gr_d3d_preload(int x, int y)
197 {
198         STUB_FUNCTION;
199 }
200
201 void d3d_start_frame()
202 {
203         STUB_FUNCTION;
204 }
205
206 void d3d_stop_frame()
207 {
208         STUB_FUNCTION;
209 }
210
211 void d3d_flush ()
212 {
213         STUB_FUNCTION;
214 }
215
216 void d3d_zbias (int a)
217 {
218         STUB_FUNCTION;
219 }
220 #endif
221
222 void gr_opengl_activate(int b)
223 {
224         STUB_FUNCTION;
225 }
226
227 void gr_opengl_preload_init()
228 {
229         STUB_FUNCTION;
230 }
231
232 void gr_opengl_pixel(int x, int y)
233 {
234         if ( x < gr_screen.clip_left ) return;
235         if ( x > gr_screen.clip_right ) return;
236         if ( y < gr_screen.clip_top ) return;
237         if ( y > gr_screen.clip_bottom ) return;
238 }
239
240 void gr_opengl_clear()
241 {
242         glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
243 }
244
245 void gr_opengl_flip()
246 {
247 #ifdef PLAT_UNIX
248         if (Inited) SDL_GL_SwapBuffers ();
249 #endif
250 }
251
252 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
253 {
254         STUB_FUNCTION;
255 }
256
257 void gr_opengl_set_clip(int x,int y,int w,int h)
258 {
259         // check for sanity of parameters
260         if (x < 0)
261                 x = 0;
262         if (y < 0)
263                 y = 0;
264
265         if (x >= gr_screen.max_w)
266                 x = gr_screen.max_w - 1;
267         if (y >= gr_screen.max_h)
268                 y = gr_screen.max_h - 1;
269
270         if (x + w > gr_screen.max_w)
271                 w = gr_screen.max_w - x;
272         if (y + h > gr_screen.max_h)
273                 h = gr_screen.max_h - y;
274         
275         if (w > gr_screen.max_w)
276                 w = gr_screen.max_w;
277         if (h > gr_screen.max_h)
278                 h = gr_screen.max_h;
279         
280         gr_screen.offset_x = x;
281         gr_screen.offset_y = y;
282         gr_screen.clip_left = 0;
283         gr_screen.clip_right = w-1;
284         gr_screen.clip_top = 0;
285         gr_screen.clip_bottom = h-1;
286         gr_screen.clip_width = w;
287         gr_screen.clip_height = h;
288         
289         STUB_FUNCTION;
290 }
291
292 void gr_opengl_reset_clip()
293 {
294         gr_screen.offset_x = 0;
295         gr_screen.offset_y = 0;
296         gr_screen.clip_left = 0;
297         gr_screen.clip_top = 0;
298         gr_screen.clip_right = gr_screen.max_w - 1;
299         gr_screen.clip_bottom = gr_screen.max_h - 1;
300         gr_screen.clip_width = gr_screen.max_w;
301         gr_screen.clip_height = gr_screen.max_h;
302         
303         STUB_FUNCTION;
304 }
305
306 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
307 {
308         gr_screen.current_alpha = alpha;
309         gr_screen.current_alphablend_mode = alphablend_mode;
310         gr_screen.current_bitblt_mode = bitblt_mode;
311         gr_screen.current_bitmap = bitmap_num;
312
313         gr_screen.current_bitmap_sx = sx;
314         gr_screen.current_bitmap_sy = sy;
315 }
316
317 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
318 {
319         shade->screen_sig = gr_screen.signature;
320         shade->r = r;
321         shade->g = g;
322         shade->b = b;
323         shade->c = c;   
324 }
325
326 void gr_opengl_set_shader( shader * shade )
327 {       
328         if ( shade )    {
329                 if (shade->screen_sig != gr_screen.signature)   {
330                         gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
331                 }
332                 gr_screen.current_shader = *shade;
333         } else {
334                 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
335         }
336 }
337
338 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
339 {
340         bitmap * bmp;
341         
342         bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
343         
344         int ix, iy, iw, ih;
345         int px, py, qx, qy;
346         GLubyte *sptr, *dptr;
347         
348         float s, t;
349         
350         int cw = min(bmp->w, w);
351         int ch = min(bmp->h, h);
352
353         glColor4f(1.0, 1.0, 1.0, 1.0);  
354         glBindTexture(GL_TEXTURE_2D, bitmapTex);
355                 
356         py = y;
357         for (iy = sy; iy < ch; iy += 256) {
358                 px = x;
359                 ih = min(256, (ch-iy));
360                 qy = py+ih;
361                 for (ix = sx; ix < cw; ix += 256) {
362                         dptr = bitmapMem;
363                         sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
364                         
365                         iw = min(256, (cw-ix));
366                                 
367                         qx = px+iw;
368                         
369                         int ihx = ih;
370                         while (ihx > 0) {
371                                 memcpy(dptr, sptr, iw*2);
372                                 
373                                 sptr += 2*bmp->w;
374                                 dptr += 2*iw;
375                                 
376                                 ihx--;
377                         }                       
378                         
379                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
380                         glBegin(GL_QUADS);
381                                 /* upper left */
382                                 s = 0.0;
383                                 t = 0.0;
384                                 glTexCoord2f(s, t);             
385                                 glVertex2f(px, py);
386                                 
387                                 /* lower left */
388                                 s = 0.0;
389                                 t = (float)ih / 256.0;
390                                 glTexCoord2f(s, t);             
391                                 glVertex2f(px, qy);
392                                 
393                                 /* lower right */
394                                 s = (float)iw / 256.0;
395                                 t = (float)ih / 256.0;
396                                 glTexCoord2f(s, t);             
397                                 glVertex2f(qx, qy);
398                                 
399                                 /* upper left */
400                                 s = (float)iw / 256.0;
401                                 t = 0.0;
402                                 glTexCoord2f(s, t);
403                                 glVertex2f(qx, py);                             
404                         glEnd();
405                         
406                         px = qx;
407                 }
408                 
409                 py = qy;
410         }       
411 }
412
413
414 void gr_d3d_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
415 {
416         int reclip;
417         #ifndef NDEBUG
418         int count = 0;
419         #endif
420
421         int dx1=x, dx2=x+w-1;
422         int dy1=y, dy2=y+h-1;
423
424         int bw, bh;
425         bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
426
427         do {
428                 reclip = 0;
429                 #ifndef NDEBUG
430                         if ( count > 1 ) Int3();
431                         count++;
432                 #endif
433         
434                 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
435                 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
436                 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
437                 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
438                 if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
439                 if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
440
441                 if ( sx < 0 ) {
442                         dx1 -= sx;
443                         sx = 0;
444                         reclip = 1;
445                 }
446
447                 if ( sy < 0 ) {
448                         dy1 -= sy;
449                         sy = 0;
450                         reclip = 1;
451                 }
452
453                 w = dx2-dx1+1;
454                 h = dy2-dy1+1;
455
456                 if ( sx + w > bw ) {
457                         w = bw - sx;
458                         dx2 = dx1 + w - 1;
459                 }
460
461                 if ( sy + h > bh ) {
462                         h = bh - sy;
463                         dy2 = dy1 + h - 1;
464                 }
465
466                 if ( w < 1 ) return;            // clipped away!
467                 if ( h < 1 ) return;            // clipped away!
468
469         } while (reclip);
470
471         // Make sure clipping algorithm works
472         #ifndef NDEBUG
473                 Assert( w > 0 );
474                 Assert( h > 0 );
475                 Assert( w == (dx2-dx1+1) );
476                 Assert( h == (dy2-dy1+1) );
477                 Assert( sx >= 0 );
478                 Assert( sy >= 0 );
479                 Assert( sx+w <= bw );
480                 Assert( sy+h <= bh );
481                 Assert( dx2 >= dx1 );
482                 Assert( dy2 >= dy1 );
483                 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
484                 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
485                 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
486                 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
487         #endif
488
489         // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
490         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
491
492         gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
493 }
494
495 void gr_opengl_bitmap(int x, int y)
496 {
497         int w, h;
498
499         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
500         int dx1=x, dx2=x+w-1;
501         int dy1=y, dy2=y+h-1;
502         int sx=0, sy=0;
503
504         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
505         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
506         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
507         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
508         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
509         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
510
511         if ( sx < 0 ) return;
512         if ( sy < 0 ) return;
513         if ( sx >= w ) return;
514         if ( sy >= h ) return;
515
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 static void opengl_scanline(int x1,int x2,int y)
522 {
523         STUB_FUNCTION;
524 }
525
526 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
527 {
528         int saved_zbuf;
529
530         saved_zbuf = gr_zbuffer_get();
531         gr_zbuffer_set(GR_ZBUFF_NONE);
532         gr_set_cull(0);
533         
534         glColor4ub(r, g, b, a);
535         glBegin(GL_QUADS);
536                 /* upper left */
537                 glVertex2f(x, y);
538                 
539                 /* lower left */
540                 glVertex2f(x, y+x);
541         
542                 /* lower right */
543                 glVertex2f(x+w, y+h);
544                 
545                 /* upper right */
546                 glVertex2f(x+w, y);
547         glEnd();
548         
549         gr_zbuffer_set(saved_zbuf);
550         gr_set_cull(1);
551 }
552
553 void gr_opengl_rect(int x,int y,int w,int h)
554 {
555         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);
556 }
557
558 void gr_opengl_shade(int x,int y,int w,int h)
559 {
560         int r,g,b,a;
561         
562         float shade1 = 1.0f;
563         float shade2 = 6.0f;
564
565         r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
566         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
567         g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
568         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
569         b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
570         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
571         a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
572         if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
573
574         gr_opengl_rect_internal(x, y, w, h, r, g, b, a);        
575 }
576
577 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
578 {
579         if ( w < 1 ) return;
580         if ( h < 1 ) return;
581
582         if ( !gr_screen.current_color.is_alphacolor )   return;
583
584         float u_scale, v_scale;
585
586 //      gr_d3d_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
587
588         if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) )     {
589                 // Couldn't set texture
590                 //mprintf(( "GLIDE: Error setting aabitmap texture!\n" ));
591                 return;
592         }
593
594 //      LPD3DTLVERTEX src_v;
595 //      D3DTLVERTEX d3d_verts[4];
596
597         float u0, u1, v0, v1;
598         float x1, x2, y1, y2;
599         int bw, bh;
600
601         bm_get_info( gr_screen.current_bitmap, &bw, &bh );
602
603         u0 = u_scale*i2fl(sx)/i2fl(bw);
604         v0 = v_scale*i2fl(sy)/i2fl(bh);
605
606         u1 = u_scale*i2fl(sx+w)/i2fl(bw);
607         v1 = v_scale*i2fl(sy+h)/i2fl(bh);
608
609         x1 = i2fl(x+gr_screen.offset_x);
610         y1 = i2fl(y+gr_screen.offset_y);
611         x2 = i2fl(x+w+gr_screen.offset_x);
612         y2 = i2fl(y+h+gr_screen.offset_y);
613
614         uint color;
615
616         if ( gr_screen.current_color.is_alphacolor )    {
617 //              color = RGBA_MAKE(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
618                         glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
619 //              } else {
620 //                      int r = (gr_screen.current_color.red*gr_screen.current_color.alpha)/255;
621 //                      int g = (gr_screen.current_color.green*gr_screen.current_color.alpha)/255;
622 //                      int b = (gr_screen.current_color.blue*gr_screen.current_color.alpha)/255;
623 //              
624 //                      color = RGBA_MAKE(r,g,b, 255 );
625 //                      glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
626 //              }
627         } else {
628 //              color = RGB_MAKE(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
629 //              glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
630         }
631
632 #if 0
633         src_v->sz = 0.99f;
634         src_v->rhw = 1.0f;
635         src_v->color = color;    
636         src_v->specular = 0;
637         src_v->sx = x1;
638         src_v->sy = y1;
639         src_v->tu = u0;
640         src_v->tv = v0;
641         src_v++;
642
643         src_v->sz = 0.99f;
644         src_v->rhw = 1.0f;
645         src_v->color = color;    
646         src_v->specular = 0;
647         src_v->sx = x2;
648         src_v->sy = y1;
649         src_v->tu = u1;
650         src_v->tv = v0;
651         src_v++;
652
653         src_v->sz = 0.99f;
654         src_v->rhw = 1.0f;
655         src_v->color = color;    
656         src_v->specular = 0;
657         src_v->sx = x2;
658         src_v->sy = y2;
659         src_v->tu = u1;
660         src_v->tv = v1;
661         src_v++;
662
663         src_v->sz = 0.99f;
664         src_v->rhw = 1.0f;
665         src_v->color = color;    
666         src_v->specular = 0;
667         src_v->sx = x1;
668         src_v->sy = y2;
669         src_v->tu = u0;
670         src_v->tv = v1;
671
672         d3d_DrawPrimitive(D3DPT_TRIANGLEFAN,D3DVT_TLVERTEX,(LPVOID)d3d_verts,4,NULL);
673 #endif  
674 }
675
676 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
677 {
678         int reclip;
679         #ifndef NDEBUG
680         int count = 0;
681         #endif
682
683         int dx1=x, dx2=x+w-1;
684         int dy1=y, dy2=y+h-1;
685
686         int bw, bh;
687         bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
688
689         do {
690                 reclip = 0;
691                 #ifndef NDEBUG
692                         if ( count > 1 ) Int3();
693                         count++;
694                 #endif
695         
696                 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
697                 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
698                 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
699                 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
700                 if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
701                 if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
702
703                 if ( sx < 0 ) {
704                         dx1 -= sx;
705                         sx = 0;
706                         reclip = 1;
707                 }
708
709                 if ( sy < 0 ) {
710                         dy1 -= sy;
711                         sy = 0;
712                         reclip = 1;
713                 }
714
715                 w = dx2-dx1+1;
716                 h = dy2-dy1+1;
717
718                 if ( sx + w > bw ) {
719                         w = bw - sx;
720                         dx2 = dx1 + w - 1;
721                 }
722
723                 if ( sy + h > bh ) {
724                         h = bh - sy;
725                         dy2 = dy1 + h - 1;
726                 }
727
728                 if ( w < 1 ) return;            // clipped away!
729                 if ( h < 1 ) return;            // clipped away!
730
731         } while (reclip);
732
733         // Make sure clipping algorithm works
734         #ifndef NDEBUG
735                 Assert( w > 0 );
736                 Assert( h > 0 );
737                 Assert( w == (dx2-dx1+1) );
738                 Assert( h == (dy2-dy1+1) );
739                 Assert( sx >= 0 );
740                 Assert( sy >= 0 );
741                 Assert( sx+w <= bw );
742                 Assert( sy+h <= bh );
743                 Assert( dx2 >= dx1 );
744                 Assert( dy2 >= dy1 );
745                 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
746                 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
747                 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
748                 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
749         #endif
750
751         // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
752         gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
753 }
754
755 void gr_opengl_aabitmap(int x, int y)
756 {
757         int w, h;
758
759         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
760         int dx1=x, dx2=x+w-1;
761         int dy1=y, dy2=y+h-1;
762         int sx=0, sy=0;
763
764         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
765         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
766         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
767         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
768         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
769         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
770
771         if ( sx < 0 ) return;
772         if ( sy < 0 ) return;
773         if ( sx >= w ) return;
774         if ( sy >= h ) return;
775
776         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
777         gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
778 }
779
780
781 void gr_opengl_string( int sx, int sy, char *s )
782 {
783         int width, spacing, letter;
784         int x, y;
785
786         if ( !Current_font )    {
787                 return;
788         }
789
790         gr_set_bitmap(Current_font->bitmap_id);
791
792         x = sx;
793         y = sy;
794
795         if (sx==0x8000) {                       //centered
796                 x = get_centered_x(s);
797         } else {
798                 x = sx;
799         }
800         
801         spacing = 0;
802
803         while (*s)      {
804                 x += spacing;
805
806                 while (*s== '\n' )      {
807                         s++;
808                         y += Current_font->h;
809                         if (sx==0x8000) {                       //centered
810                                 x = get_centered_x(s);
811                         } else {
812                                 x = sx;
813                         }
814                 }
815                 if (*s == 0 ) break;
816
817                 letter = get_char_width(s[0],s[1],&width,&spacing);
818                 s++;
819
820                 //not in font, draw as space
821                 if (letter<0)   {
822                         continue;
823                 }
824
825                 int xd, yd, xc, yc;
826                 int wc, hc;
827
828                 // Check if this character is totally clipped
829                 if ( x + width < gr_screen.clip_left ) continue;
830                 if ( y + Current_font->h < gr_screen.clip_top ) continue;
831                 if ( x > gr_screen.clip_right ) continue;
832                 if ( y > gr_screen.clip_bottom ) continue;
833
834                 xd = yd = 0;
835                 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
836                 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
837                 xc = x+xd;
838                 yc = y+yd;
839
840                 wc = width - xd; hc = Current_font->h - yd;
841                 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
842                 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
843
844                 if ( wc < 1 ) continue;
845                 if ( hc < 1 ) continue;
846
847                 font_char *ch;
848         
849                 ch = &Current_font->char_data[letter];
850
851                 int u = Current_font->bm_u[letter];
852                 int v = Current_font->bm_v[letter];
853
854                 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
855         }
856 }
857
858 void gr_opengl_circle( int xc, int yc, int d )
859 {
860         int p,x, y, r;
861
862         r = d/2;
863         p=3-d;
864         x=0;
865         y=r;
866
867         // Big clip
868         if ( (xc+r) < gr_screen.clip_left ) return;
869         if ( (xc-r) > gr_screen.clip_right ) return;
870         if ( (yc+r) < gr_screen.clip_top ) return;
871         if ( (yc-r) > gr_screen.clip_bottom ) return;
872
873         while(x<y)      {
874                 // Draw the first octant
875                 opengl_scanline( xc-y, xc+y, yc-x );
876                 opengl_scanline( xc-y, xc+y, yc+x );
877
878                 if (p<0) 
879                         p=p+(x<<2)+6;
880                 else    {
881                         // Draw the second octant
882                         opengl_scanline( xc-x, xc+x, yc-y );
883                         opengl_scanline( xc-x, xc+x, yc+y );
884                         p=p+((x-y)<<2)+10;
885                         y--;
886                 }
887                 x++;
888         }
889         if(x==y)        {
890                 opengl_scanline( xc-x, xc+x, yc-y );
891                 opengl_scanline( xc-x, xc+x, yc+y );
892         }
893         return;
894 }
895
896
897 void gr_opengl_line(int x1,int y1,int x2,int y2)
898 {
899         int clipped = 0, swapped=0;
900
901         glDisable ( GL_DEPTH_TEST );
902         glEnable ( GL_BLEND );
903         glBlendFunc ( GL_SRC_ALPHA, GL_DST_ALPHA );
904
905         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);
906                 
907         glBegin (GL_LINE);
908           glColor4f (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
909           glVertex3f (i2fl (x1+gr_screen.offset_x),i2fl (y1+gr_screen.offset_y), 0.99f);
910           glVertex3f (i2fl (x2+gr_screen.offset_x),i2fl (y2+gr_screen.offset_y), 0.99f);
911         glEnd ();
912 }
913
914 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
915
916 void gr_opengl_scaler(vertex *va, vertex *vb )
917 {
918         float x0, y0, x1, y1;
919         float u0, v0, u1, v1;
920         float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
921         float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
922         float xmin, xmax, ymin, ymax;
923         int dx0, dy0, dx1, dy1;
924
925         //============= CLIP IT =====================
926
927         x0 = va->sx; y0 = va->sy;
928         x1 = vb->sx; y1 = vb->sy;
929
930         xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
931         xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
932
933         u0 = va->u; v0 = va->v;
934         u1 = vb->u; v1 = vb->v;
935
936         // Check for obviously offscreen bitmaps...
937         if ( (y1<=y0) || (x1<=x0) ) return;
938         if ( (x1<xmin ) || (x0>xmax) ) return;
939         if ( (y1<ymin ) || (y0>ymax) ) return;
940
941         clipped_u0 = u0; clipped_v0 = v0;
942         clipped_u1 = u1; clipped_v1 = v1;
943
944         clipped_x0 = x0; clipped_y0 = y0;
945         clipped_x1 = x1; clipped_y1 = y1;
946
947         // Clip the left, moving u0 right as necessary
948         if ( x0 < xmin )        {
949                 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
950                 clipped_x0 = xmin;
951         }
952
953         // Clip the right, moving u1 left as necessary
954         if ( x1 > xmax )        {
955                 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
956                 clipped_x1 = xmax;
957         }
958
959         // Clip the top, moving v0 down as necessary
960         if ( y0 < ymin )        {
961                 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
962                 clipped_y0 = ymin;
963         }
964
965         // Clip the bottom, moving v1 up as necessary
966         if ( y1 > ymax )        {
967                 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
968                 clipped_y1 = ymax;
969         }
970         
971         dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
972         dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
973
974         if (dx1<=dx0) return;
975         if (dy1<=dy0) return;
976
977         //============= DRAW IT =====================
978         int u, v, du, dv;
979         int y, w;
980         ubyte * sbits;
981         bitmap * bp;
982         ubyte * spixels;
983         float tmpu, tmpv;
984
985         tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
986         if ( fl_abs(tmpu) < 0.001f ) {
987                 return;         // scaled up way too far!
988         }
989         tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
990         if ( fl_abs(tmpv) < 0.001f ) {
991                 return;         // scaled up way too far!
992         }
993
994         bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
995
996         du = fl2f(tmpu*(bp->w-1));
997         dv = fl2f(tmpv*(bp->h-1));
998
999         v = fl2f(clipped_v0*(bp->h-1));
1000         u = fl2f(clipped_u0*(bp->w-1)); 
1001         w = dx1 - dx0 + 1;
1002
1003         spixels = (ubyte *)bp->data;
1004
1005         for (y=dy0; y<=dy1; y++ )                       {
1006                 sbits = &spixels[bp->rowsize*(v>>16)];
1007
1008                 int x, tmp_u;
1009                 tmp_u = u;
1010                 for (x=0; x<w; x++ )                    {
1011                         ubyte c = sbits[ tmp_u >> 16 ];
1012                         if ( c != 255 ) {
1013                                 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
1014                                 gr_pixel( x+dx0, y );
1015                         }
1016                         tmp_u += du;
1017                 }
1018                 v += dv;
1019         }
1020
1021         bm_unlock(gr_screen.current_bitmap);
1022
1023         STUB_FUNCTION;
1024 }
1025
1026 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
1027 {
1028         int i;
1029         float u_scale = 1.0f, v_scale = 1.0f;
1030         int bw = 1, bh = 1;
1031
1032         // Make nebula use the texture mapper... this blends the colors better.
1033         if ( flags & TMAP_FLAG_NEBULA ){
1034                 Int3 ();
1035         }
1036
1037         /*
1038         gr_texture_source texture_source = (gr_texture_source)-1;
1039         gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1040         gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1041         
1042         if ( gr_zbuffering )    {
1043                 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER)   )       {
1044                         zbuffer_type = ZBUFFER_TYPE_READ;
1045                 } else {
1046                         zbuffer_type = ZBUFFER_TYPE_FULL;
1047                 }
1048         } else {
1049                 zbuffer_type = ZBUFFER_TYPE_NONE;
1050         }
1051         */
1052
1053         int alpha;
1054
1055         int tmap_type = TCACHE_TYPE_NORMAL;
1056
1057         int r, g, b;
1058
1059         if ( flags & TMAP_FLAG_TEXTURED )       {
1060                 r = g = b = 255;
1061         } else {
1062                 r = gr_screen.current_color.red;
1063                 g = gr_screen.current_color.green;
1064                 b = gr_screen.current_color.blue;
1065         }
1066
1067         if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )        
1068         {
1069                 // Some blend function stuff here - DDOI
1070                 STUB_FUNCTION;
1071
1072                 float factor = gr_screen.current_alpha;
1073
1074                 alpha = 255;
1075
1076                 if ( factor <= 1.0f )   {
1077                         int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1078                         r = (r*tmp_alpha)/255;
1079                         g = (g*tmp_alpha)/255;
1080                         b = (b*tmp_alpha)/255;
1081                 }
1082         } else {
1083                 STUB_FUNCTION;
1084                 alpha = 255;
1085         }
1086
1087         if ( flags & TMAP_FLAG_TEXTURED )       {
1088                 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 ))
1089                 {
1090                         mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1091                         return;
1092                 }
1093
1094                 // use nonfiltered textures for bitmap sections
1095                 /*
1096                 if(flags & TMAP_FLAG_BITMAP_SECTION){
1097                         texture_source = TEXTURE_SOURCE_NO_FILTERING;
1098                 } else {
1099                         texture_source = TEXTURE_SOURCE_DECAL;
1100                 }
1101                 */
1102         }
1103
1104         int x1, y1, x2, y2;
1105         x1 = gr_screen.clip_left*16;
1106         x2 = gr_screen.clip_right*16+15;
1107         y1 = gr_screen.clip_top*16;
1108         y2 = gr_screen.clip_bottom*16+15;
1109
1110         float uoffset = 0.0f;
1111         float voffset = 0.0f;
1112
1113         float minu=0.0f, minv=0.0f, maxu=1.0f, maxv=1.0f;
1114
1115         if ( flags & TMAP_FLAG_TEXTURED )
1116         {
1117                 STUB_FUNCTION;
1118         }
1119
1120         int a;
1121
1122         STUB_FUNCTION;  // DDOI - this still needs work
1123         glBegin (GL_TRIANGLE_FAN);
1124         for (i=0;i<nv;i++)
1125         {
1126                 if (flags & TMAP_FLAG_ALPHA) a = verts[i]->a;
1127                 else a = alpha;
1128
1129                 if (flags & TMAP_FLAG_NEBULA ) {
1130                         /*
1131                         int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1132                         r = gr_palette[pal*3+0];
1133                         g = gr_palette[pal*3+1];
1134                         b = gr_palette[pal*3+2];
1135                         */
1136                 }else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) )   {
1137                         r = Gr_gamma_lookup[verts[i]->b];
1138                         g = Gr_gamma_lookup[verts[i]->b];
1139                         b = Gr_gamma_lookup[verts[i]->b];
1140                 } else if ( (flags & TMAP_FLAG_RGB)  && (flags & TMAP_FLAG_GOURAUD) )   {
1141                         // Make 0.75 be 256.0f
1142                         r = Gr_gamma_lookup[verts[i]->r];
1143                         g = Gr_gamma_lookup[verts[i]->g];
1144                         b = Gr_gamma_lookup[verts[i]->b];
1145                 } else {
1146                         // use constant RGB values...
1147                 }
1148
1149                 glColor4i (r,g,b,a);
1150
1151                 // DDOI - FIXME fog stuff
1152                 // DDOI - FIXME TexCoord stuff
1153                 glVertex3f (verts[i]->sx+gr_screen.offset_x,
1154                                 verts[i]->sy+gr_screen.offset_y,
1155                                 0.99f);
1156         }
1157 }
1158
1159
1160 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1161 {
1162         STUB_FUNCTION;
1163 }
1164
1165 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1166 {
1167         STUB_FUNCTION;
1168 }
1169
1170 void gr_opengl_get_color( int * r, int * g, int * b )
1171 {
1172         if (r) *r = gr_screen.current_color.red;
1173         if (g) *g = gr_screen.current_color.green;
1174         if (b) *b = gr_screen.current_color.blue;
1175 }
1176
1177 void gr_opengl_init_color(color *c, int r, int g, int b)
1178 {
1179         c->screen_sig = gr_screen.signature;
1180         c->red = (unsigned char)r;
1181         c->green = (unsigned char)g;
1182         c->blue = (unsigned char)b;
1183         c->alpha = 255;
1184         c->ac_type = AC_TYPE_NONE;
1185         c->alphacolor = -1;
1186         c->is_alphacolor = 0;
1187         c->magic = 0xAC01;
1188 }
1189
1190 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1191 {
1192         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1193         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1194         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1195         if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1196
1197         gr_opengl_init_color( clr, r, g, b );
1198
1199         clr->alpha = (unsigned char)alpha;
1200         clr->ac_type = (ubyte)type;
1201         clr->alphacolor = -1;
1202         clr->is_alphacolor = 1;
1203 }
1204
1205 void gr_opengl_set_color( int r, int g, int b )
1206 {
1207         Assert((r >= 0) && (r < 256));
1208         Assert((g >= 0) && (g < 256));
1209         Assert((b >= 0) && (b < 256));
1210
1211         gr_opengl_init_color( &gr_screen.current_color, r, g, b );      
1212 }
1213
1214 void gr_opengl_set_color_fast(color *dst)
1215 {
1216         if ( dst->screen_sig != gr_screen.signature )   {
1217                 if ( dst->is_alphacolor )       {
1218                         gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1219                 } else {
1220                         gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1221                 }
1222         }
1223         gr_screen.current_color = *dst;
1224 }
1225
1226 void gr_opengl_print_screen(char *filename)
1227 {
1228         STUB_FUNCTION;
1229 }
1230
1231 int gr_opengl_supports_res_ingame(int res)
1232 {
1233         STUB_FUNCTION;
1234         
1235         return 1;
1236 }
1237
1238 int gr_opengl_supports_res_interface(int res)
1239 {
1240         STUB_FUNCTION;
1241         
1242         return 1;
1243 }
1244
1245 void gr_opengl_cleanup()
1246 {
1247         if ( !Inited )  return;
1248
1249         gr_reset_clip();
1250         gr_clear();
1251         gr_flip();
1252
1253         Inited = 0;
1254 }
1255
1256 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
1257 {
1258         STUB_FUNCTION;
1259 }
1260
1261 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1262 {
1263         STUB_FUNCTION;
1264 }
1265
1266 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
1267 {
1268         STUB_FUNCTION;
1269 }
1270
1271 void gr_opengl_set_cull(int cull)
1272 {
1273         STUB_FUNCTION;
1274 }
1275
1276 void gr_opengl_filter_set(int filter)
1277 {
1278         STUB_FUNCTION;
1279 }
1280
1281 // cross fade
1282 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1283 {
1284         STUB_FUNCTION;
1285 }
1286
1287 int gr_opengl_tcache_set(int bitmap_id, int bitmap_type, float *u_ratio, float *v_ratio, int fail_on_full = 0, int sx = -1, int sy = -1, int force = 0)
1288 {
1289         bitmap *bmp = NULL;
1290         bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
1291         bm_unlock(bitmap_id);
1292         
1293         fprintf(stderr, "DEBUG: %d %d: ", bmp->w, bmp->h);
1294         STUB_FUNCTION;
1295         
1296         return 1;
1297 }
1298
1299 void gr_opengl_set_clear_color(int r, int g, int b)
1300 {
1301         STUB_FUNCTION;
1302 }
1303
1304 void gr_opengl_aaline(vertex *v1, vertex *v2)
1305 {
1306         STUB_FUNCTION;
1307 }
1308
1309 void gr_opengl_flash(int r, int g, int b)
1310 {
1311         STUB_FUNCTION;
1312 }
1313
1314 int gr_opengl_zbuffer_get()
1315 {
1316         STUB_FUNCTION;
1317         
1318         return GR_ZBUFF_NONE;
1319 }
1320
1321 int gr_opengl_zbuffer_set(int mode)
1322 {
1323         STUB_FUNCTION;
1324         
1325         return GR_ZBUFF_NONE;
1326 }
1327
1328 void gr_opengl_zbuffer_clear(int mode)
1329 {
1330         STUB_FUNCTION;
1331 }
1332
1333 void gr_opengl_set_gamma(float gamma)
1334 {
1335         STUB_FUNCTION;
1336 }
1337
1338 void gr_opengl_fade_in(int instantaneous)
1339 {
1340         STUB_FUNCTION;
1341 }
1342
1343 void gr_opengl_fade_out(int instantaneous)
1344 {
1345         STUB_FUNCTION;
1346 }
1347
1348 int gr_opengl_save_screen()
1349 {
1350         STUB_FUNCTION;
1351         
1352         return -1;
1353 }
1354
1355 void gr_opengl_restore_screen(int id)
1356 {
1357         STUB_FUNCTION;
1358 }
1359
1360 void gr_opengl_free_screen(int id)
1361 {
1362         STUB_FUNCTION;
1363 }
1364
1365 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
1366 {
1367         STUB_FUNCTION;
1368 }
1369
1370 void gr_opengl_dump_frame_stop()
1371 {
1372         STUB_FUNCTION;
1373 }
1374
1375 void gr_opengl_dump_frame()
1376 {
1377         STUB_FUNCTION;
1378 }
1379
1380 uint gr_opengl_lock()
1381 {
1382         STUB_FUNCTION;
1383         
1384         return 1;
1385 }
1386         
1387 void gr_opengl_unlock()
1388 {
1389 }
1390         
1391 void gr_opengl_init()
1392 {
1393         if ( Inited )   {
1394                 gr_opengl_cleanup();
1395                 Inited = 0;
1396         }
1397
1398         mprintf(( "Initializing opengl graphics device...\n" ));
1399         Inited = 1;
1400
1401 #ifdef PLAT_UNIX
1402         if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
1403         {
1404                 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
1405                 exit (1);
1406         }
1407
1408         atexit (SDL_Quit);
1409
1410         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
1411         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
1412         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
1413         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
1414         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
1415                                                 
1416         if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
1417         {
1418                 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
1419                 exit (1);
1420         }               
1421 #endif
1422         glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
1423
1424         glMatrixMode(GL_PROJECTION);
1425         glLoadIdentity();
1426         glMatrixMode(GL_MODELVIEW);
1427         glLoadIdentity();
1428         
1429         glEnable(GL_TEXTURE_2D);
1430         
1431         glGenTextures(1, &bitmapTex);
1432         glBindTexture(GL_TEXTURE_2D, bitmapTex);
1433         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1434         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1435         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1436
1437         glMatrixMode(GL_PROJECTION);
1438         glPushMatrix();
1439         glLoadIdentity();
1440         
1441         glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, -1.0, 1.0);             
1442         
1443         bitmapMem = (GLubyte *)malloc(256*256*4);
1444         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, bitmapMem);
1445         //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_CHAR, bitmapMem);
1446         
1447         glFlush();
1448         
1449         int bpp = 15;
1450         
1451         switch( bpp )   {
1452         case 8:
1453                 Gr_red.bits = 8;
1454                 Gr_red.shift = 16;
1455                 Gr_red.scale = 1;
1456                 Gr_red.mask = 0xff0000;
1457
1458                 Gr_green.bits = 8;
1459                 Gr_green.shift = 8;
1460                 Gr_green.scale = 1;
1461                 Gr_green.mask = 0xff00;
1462
1463                 Gr_blue.bits = 8;
1464                 Gr_blue.shift = 0;
1465                 Gr_blue.scale = 1;
1466                 Gr_blue.mask = 0xff;
1467                 
1468                 break;
1469                 
1470         case 15:
1471                 Gr_red.bits = 5;
1472                 Gr_red.shift = 10;
1473                 Gr_red.scale = 8;
1474                 Gr_red.mask = 0x7C00;
1475
1476                 Gr_green.bits = 5;
1477                 Gr_green.shift = 5;
1478                 Gr_green.scale = 8;
1479                 Gr_green.mask = 0x3E0;
1480
1481                 Gr_blue.bits = 5;
1482                 Gr_blue.shift = 0;
1483                 Gr_blue.scale = 8;
1484                 Gr_blue.mask = 0x1F;
1485
1486                 break;
1487
1488         case 16:
1489                 Gr_red.bits = 5;
1490                 Gr_red.shift = 11;
1491                 Gr_red.scale = 8;
1492                 Gr_red.mask = 0xF800;
1493
1494                 Gr_green.bits = 6;
1495                 Gr_green.shift = 5;
1496                 Gr_green.scale = 4;
1497                 Gr_green.mask = 0x7E0;
1498
1499                 Gr_blue.bits = 5;
1500                 Gr_blue.shift = 0;
1501                 Gr_blue.scale = 8;
1502                 Gr_blue.mask = 0x1F;
1503
1504                 break;
1505
1506         case 24:
1507         case 32:
1508                 Gr_red.bits = 8;
1509                 Gr_red.shift = 16;
1510                 Gr_red.scale = 1;
1511                 Gr_red.mask = 0xff0000;
1512
1513                 Gr_green.bits = 8;
1514                 Gr_green.shift = 8;
1515                 Gr_green.scale = 1;
1516                 Gr_green.mask = 0xff00;
1517
1518                 Gr_blue.bits = 8;
1519                 Gr_blue.shift = 0;
1520                 Gr_blue.scale = 1;
1521                 Gr_blue.mask = 0xff;
1522
1523                 break;
1524
1525         default:
1526                 Int3(); // Illegal bpp
1527         }
1528
1529
1530         gr_opengl_clear();
1531
1532         Gr_current_red = &Gr_red;
1533         Gr_current_blue = &Gr_blue;
1534         Gr_current_green = &Gr_green;
1535         Gr_current_alpha = &Gr_alpha;
1536                                 
1537         gr_screen.gf_flip = gr_opengl_flip;
1538         gr_screen.gf_flip_window = gr_opengl_flip_window;
1539         gr_screen.gf_set_clip = gr_opengl_set_clip;
1540         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
1541         gr_screen.gf_set_font = grx_set_font;
1542         
1543         gr_screen.gf_set_color = gr_opengl_set_color;
1544         gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
1545         gr_screen.gf_create_shader = gr_opengl_create_shader;
1546         gr_screen.gf_set_shader = gr_opengl_set_shader;
1547         gr_screen.gf_clear = gr_opengl_clear;
1548         // gr_screen.gf_bitmap = gr_opengl_bitmap;
1549         // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
1550         gr_screen.gf_aabitmap = gr_opengl_aabitmap;
1551         gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
1552         
1553         gr_screen.gf_rect = gr_opengl_rect;
1554         gr_screen.gf_shade = gr_opengl_shade;
1555         gr_screen.gf_string = gr_opengl_string;
1556         gr_screen.gf_circle = gr_opengl_circle;
1557
1558         gr_screen.gf_line = gr_opengl_line;
1559         gr_screen.gf_aaline = gr_opengl_aaline;
1560         gr_screen.gf_pixel = gr_opengl_pixel;
1561         gr_screen.gf_scaler = gr_opengl_scaler;
1562         gr_screen.gf_tmapper = gr_opengl_tmapper;
1563
1564         gr_screen.gf_gradient = gr_opengl_gradient;
1565
1566         gr_screen.gf_set_palette = gr_opengl_set_palette;
1567         gr_screen.gf_get_color = gr_opengl_get_color;
1568         gr_screen.gf_init_color = gr_opengl_init_color;
1569         gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
1570         gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
1571         gr_screen.gf_print_screen = gr_opengl_print_screen;
1572
1573         gr_screen.gf_fade_in = gr_opengl_fade_in;
1574         gr_screen.gf_fade_out = gr_opengl_fade_out;
1575         gr_screen.gf_flash = gr_opengl_flash;
1576         
1577         gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
1578         gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
1579         gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
1580         
1581         gr_screen.gf_save_screen = gr_opengl_save_screen;
1582         gr_screen.gf_restore_screen = gr_opengl_restore_screen;
1583         gr_screen.gf_free_screen = gr_opengl_free_screen;
1584         
1585         gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
1586         gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
1587         gr_screen.gf_dump_frame = gr_opengl_dump_frame;
1588         
1589         gr_screen.gf_set_gamma = gr_opengl_set_gamma;
1590         
1591         gr_screen.gf_lock = gr_opengl_lock;
1592         gr_screen.gf_unlock = gr_opengl_unlock;
1593         
1594         gr_screen.gf_fog_set = gr_opengl_fog_set;       
1595
1596         gr_screen.gf_get_region = gr_opengl_get_region;
1597
1598         gr_screen.gf_get_pixel = gr_opengl_get_pixel;
1599
1600         gr_screen.gf_set_cull = gr_opengl_set_cull;
1601
1602         gr_screen.gf_cross_fade = gr_opengl_cross_fade;
1603
1604         gr_screen.gf_filter_set = gr_opengl_filter_set;
1605
1606         gr_screen.gf_tcache_set = gr_opengl_tcache_set;
1607
1608         gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;
1609 }
1610