]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/gropengl.cpp
removed some unncessary stubbing, implemented opengl rect
[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.11  2002/05/29 04:29:56  relnev
11  * removed some unncessary stubbing, implemented opengl rect
12  *
13  * Revision 1.10  2002/05/29 04:13:27  theoddone33
14  * enable opengl_line
15  *
16  * Revision 1.9  2002/05/29 03:35:51  relnev
17  * added rest of init
18  *
19  * Revision 1.8  2002/05/29 03:30:05  relnev
20  * update opengl stubs
21  *
22  * Revision 1.7  2002/05/29 02:52:32  theoddone33
23  * Enable OpenGL renderer
24  *
25  * Revision 1.6  2002/05/28 04:56:51  theoddone33
26  * runs a little bit now
27  *
28  * Revision 1.5  2002/05/28 04:07:28  theoddone33
29  * New graphics stubbing arrangement
30  *
31  * Revision 1.4  2002/05/27 23:39:34  relnev
32  * 0
33  *
34  * Revision 1.3  2002/05/27 22:35:01  theoddone33
35  * more symbols
36  *
37  * Revision 1.2  2002/05/27 22:32:02  theoddone33
38  * throw all d3d stuff at opengl
39  *
40  * Revision 1.1.1.1  2002/05/03 03:28:09  root
41  * Initial import.
42  *
43  * 
44  * 10    7/14/99 9:42a Dave
45  * Put in clear_color debug function. Put in base for 3dnow stuff / P3
46  * stuff
47  * 
48  * 9     7/09/99 9:51a Dave
49  * Added thick polyline code.
50  * 
51  * 8     6/29/99 10:35a Dave
52  * Interface polygon bitmaps! Whee!
53  * 
54  * 7     2/03/99 11:44a Dave
55  * Fixed d3d transparent textures.
56  * 
57  * 6     1/24/99 11:37p Dave
58  * First full rev of beam weapons. Very customizable. Removed some bogus
59  * Int3()'s in low level net code.
60  * 
61  * 5     12/18/98 1:13a Dave
62  * Rough 1024x768 support for Direct3D. Proper detection and usage through
63  * the launcher.
64  * 
65  * 4     12/06/98 2:36p Dave
66  * Drastically improved nebula fogging.
67  * 
68  * 3     11/11/98 5:37p Dave
69  * Checkin for multiplayer testing.
70  * 
71  * 2     10/07/98 10:53a Dave
72  * Initial checkin.
73  * 
74  * 1     10/07/98 10:49a Dave
75  * 
76  * 14    5/20/98 9:46p John
77  * added code so the places in code that change half the palette don't
78  * have to clear the screen.
79  * 
80  * 13    5/06/98 5:30p John
81  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
82  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
83  * DirectX header files and libs that fixed the Direct3D alpha blending
84  * problems.
85  * 
86  * 12    4/14/98 12:15p John
87  * Made 16-bpp movies work.
88  * 
89  * 11    3/12/98 5:36p John
90  * Took out any unused shaders.  Made shader code take rgbc instead of
91  * matrix and vector since noone used it like a matrix and it would have
92  * been impossible to do in hardware.   Made Glide implement a basic
93  * shader for online help.  
94  * 
95  * 10    3/10/98 4:18p John
96  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
97  * & Glide have popups and print screen.  Took out all >8bpp software
98  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
99  * support Fred.  Made zbuffering key off of functions rather than one
100  * global variable.
101  * 
102  * 9     12/02/97 4:00p John
103  * Added first rev of thruster glow, along with variable levels of
104  * translucency, which retquired some restructing of palman.
105  * 
106  * 8     10/03/97 9:10a John
107  * added better antialiased line drawer
108  * 
109  * 7     9/23/97 10:45a John
110  * made so you can tell bitblt code to rle a bitmap by passing flag to
111  * gr_set_bitmap
112  * 
113  * 6     9/09/97 11:01a Sandeep
114  * fixed warning level 4 bugs
115  * 
116  * 5     7/10/97 2:06p John
117  * added code to specify alphablending type for bitmaps.
118  * 
119  * 4     6/17/97 7:04p John
120  * added d3d support for gradients.
121  * fixed some color bugs by adding screen signatures instead of watching
122  * flags and palette changes.
123  * 
124  * 3     6/12/97 2:50a Lawrance
125  * bm_unlock() now passed bitmap number, not pointer
126  * 
127  * 2     6/11/97 1:12p John
128  * Started fixing all the text colors in the game.
129  * 
130  * 1     5/12/97 12:14p John
131  *
132  * $NoKeywords: $
133  */
134
135 #ifndef PLAT_UNIX
136 #include <windows.h>
137 #include <windowsx.h>
138 #endif
139 #include <GL/gl.h>
140
141 #include "osapi.h"
142 #include "2d.h"
143 #include "bmpman.h"
144 #include "floating.h"
145 #include "palman.h"
146 #include "grinternal.h"
147 #include "gropengl.h"
148 #include "line.h"
149
150 static int Inited = 0;
151 #ifdef PLAT_UNIX
152 // Throw in some dummy functions - DDOI
153
154 int D3D_32bit = 0;              // grd3d.cpp
155 int D3D_fog_mode = -1;          // grd3d.cpp
156 int D3D_inited = 0;             // grd3d.cpp
157 int D3D_zbias = 1;              // grd3d.cpp
158 int D3d_rendition_uvs = 0;      // grd3d.cpp
159
160 void gr_dd_activate(int active)         // grdirectdraw.cpp
161 {
162         STUB_FUNCTION;
163 }
164
165 void gr_directdraw_cleanup()            // grdirectdraw.cpp
166 {
167         STUB_FUNCTION;
168 }
169
170 void gr_directdraw_force_windowed()     // grdirectdraw.cpp
171 {
172         STUB_FUNCTION;
173 }
174
175 void gr_directdraw_init()
176 {
177         STUB_FUNCTION;
178 }
179
180
181 void gr_d3d_preload(int x, int y)
182 {
183         STUB_FUNCTION;
184 }
185
186 void d3d_start_frame()
187 {
188         STUB_FUNCTION;
189 }
190
191 void d3d_stop_frame()
192 {
193         STUB_FUNCTION;
194 }
195
196 void d3d_flush ()
197 {
198         STUB_FUNCTION;
199 }
200
201 void d3d_zbias (int a)
202 {
203         STUB_FUNCTION;
204 }
205 #endif
206
207 void gr_opengl_activate(int b)
208 {
209         STUB_FUNCTION;
210 }
211
212 void gr_opengl_preload_init()
213 {
214         STUB_FUNCTION;
215 }
216
217 void gr_opengl_pixel(int x, int y)
218 {
219         if ( x < gr_screen.clip_left ) return;
220         if ( x > gr_screen.clip_right ) return;
221         if ( y < gr_screen.clip_top ) return;
222         if ( y > gr_screen.clip_bottom ) return;
223 }
224
225 void gr_opengl_clear()
226 {
227         glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228 }
229
230 void gr_opengl_flip()
231 {
232 #ifdef PLAT_UNIX
233         if (Inited) SDL_GL_SwapBuffers ();
234 #endif
235 }
236
237 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
238 {
239         STUB_FUNCTION;
240 }
241
242 void gr_opengl_set_clip(int x,int y,int w,int h)
243 {
244         // check for sanity of parameters
245         if (x < 0)
246                 x = 0;
247         if (y < 0)
248                 y = 0;
249
250         if (x >= gr_screen.max_w)
251                 x = gr_screen.max_w - 1;
252         if (y >= gr_screen.max_h)
253                 y = gr_screen.max_h - 1;
254
255         if (x + w > gr_screen.max_w)
256                 w = gr_screen.max_w - x;
257         if (y + h > gr_screen.max_h)
258                 h = gr_screen.max_h - y;
259         
260         if (w > gr_screen.max_w)
261                 w = gr_screen.max_w;
262         if (h > gr_screen.max_h)
263                 h = gr_screen.max_h;
264         
265         gr_screen.offset_x = x;
266         gr_screen.offset_y = y;
267         gr_screen.clip_left = 0;
268         gr_screen.clip_right = w-1;
269         gr_screen.clip_top = 0;
270         gr_screen.clip_bottom = h-1;
271         gr_screen.clip_width = w;
272         gr_screen.clip_height = h;
273         
274         STUB_FUNCTION;
275 }
276
277 void gr_opengl_reset_clip()
278 {
279         gr_screen.offset_x = 0;
280         gr_screen.offset_y = 0;
281         gr_screen.clip_left = 0;
282         gr_screen.clip_top = 0;
283         gr_screen.clip_right = gr_screen.max_w - 1;
284         gr_screen.clip_bottom = gr_screen.max_h - 1;
285         gr_screen.clip_width = gr_screen.max_w;
286         gr_screen.clip_height = gr_screen.max_h;
287         
288         STUB_FUNCTION;
289 }
290
291 void gr_opengl_set_font(int fontnum)
292 {
293         STUB_FUNCTION;
294 }
295
296 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
297 {
298         gr_screen.current_alpha = alpha;
299         gr_screen.current_alphablend_mode = alphablend_mode;
300         gr_screen.current_bitblt_mode = bitblt_mode;
301         gr_screen.current_bitmap = bitmap_num;
302
303         gr_screen.current_bitmap_sx = sx;
304         gr_screen.current_bitmap_sy = sy;
305 }
306
307 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
308 {
309         shade->screen_sig = gr_screen.signature;
310         shade->r = r;
311         shade->g = g;
312         shade->b = b;
313         shade->c = c;   
314 }
315
316 void gr_opengl_set_shader( shader * shade )
317 {       
318         if ( shade )    {
319                 if (shade->screen_sig != gr_screen.signature)   {
320                         gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
321                 }
322                 gr_screen.current_shader = *shade;
323         } else {
324                 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
325         }
326 }
327
328
329 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
330 {
331         int i,j;
332         bitmap * bmp;
333         ubyte * sptr;
334
335         bmp = bm_lock( gr_screen.current_bitmap, 8, 0 );
336         sptr = (ubyte *)( bmp->data + (sy*bmp->w+sx) );
337
338 //      mprintf(( "x=%d, y=%d, w=%d, h=%d\n", x, y, w, h ));
339 //      mprintf(( "sx=%d, sy=%d, bw=%d, bh=%d\n", sx, sy, bmp->w, bmp->h ));
340
341         for (i=0; i<h; i++ )    {
342                 for ( j=0; j<w; j++ )   {
343                         gr_set_color( gr_palette[sptr[j]*3+0], gr_palette[sptr[j]*3+1], gr_palette[sptr[j]*3+2] );
344                         gr_pixel( x+j, i+y );
345                 }
346                 sptr += bmp->w;
347         }
348         bm_unlock(gr_screen.current_bitmap);
349         
350         STUB_FUNCTION;
351 }
352
353 void gr_opengl_bitmap(int x, int y)
354 {
355         int w, h;
356
357         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
358         int dx1=x, dx2=x+w-1;
359         int dy1=y, dy2=y+h-1;
360         int sx=0, sy=0;
361
362         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
363         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
364         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
365         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
366         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
367         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
368
369         if ( sx < 0 ) return;
370         if ( sy < 0 ) return;
371         if ( sx >= w ) return;
372         if ( sy >= h ) return;
373
374         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
375
376         gr_bitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);        
377 }
378
379 static void opengl_scanline(int x1,int x2,int y)
380 {
381         STUB_FUNCTION;
382 }
383
384 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
385 {
386         int saved_zbuf;
387         
388         saved_zbuf = gr_zbuffer_get();
389         gr_zbuffer_set(GR_ZBUFF_NONE);
390         gr_set_cull(0);
391         
392         glColor4ub(r, g, b, a);
393         glBegin(GL_QUADS);
394                 float m, n;
395                 float wx, wy;
396                 
397                 wx = gr_screen.max_w / 2.0;
398                 wy = gr_screen.max_h / 2.0;
399                 
400                 /* upper left */
401                 m = -(wx - (float)x) / wx;
402                 n = -(wy - (float)y) / wy;
403                 glVertex2f(m, n);
404                 
405                 /* lower left */
406                 m = -(wx - (float)x) / wx;
407                 n = -(wy - (float)(y+h)) / wy;
408                 glVertex2f(m, n);
409         
410                 /* lower right */
411                 m = -(wx - (float)(x+w)) / wx;
412                 n = -(wy - (float)(y+h)) / wy;
413                 glVertex2f(m, n);
414                 
415                 /* upper right */
416                 m = -(wx - (float)(x+w)) / wx;
417                 n = -(wy - (float)y) / wy;
418                 glVertex2f(m, n);
419         glEnd();
420         
421         gr_zbuffer_set(saved_zbuf);
422         gr_set_cull(1);
423 }
424
425 void gr_opengl_rect(int x,int y,int w,int h)
426 {
427         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);
428 }
429
430 void gr_opengl_shade(int x,int y,int w,int h)
431 {
432         int r,g,b,a;
433         
434         float shade1 = 1.0f;
435         float shade2 = 6.0f;
436
437         r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
438         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
439         g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
440         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
441         b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
442         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
443         a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
444         if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
445
446         gr_opengl_rect_internal(x, y, w, h, r, g, b, a);        
447 }
448
449 void opengl_mtext(int x, int y, char *s, int len )
450 {
451         STUB_FUNCTION;
452 }
453
454 void gr_opengl_string(int x,int y,char * text)
455 {
456         char *p, *p1;
457         int w, h;
458
459         p1 = text;
460         do {
461                 p = strchr( p1, '\n' );
462                 if ( p ) { 
463                         *p = 0;
464                         p++;
465                 }
466                 gr_get_string_size( &w, &h, p1 );
467
468                 if ( x == 0x8000 )
469                         opengl_mtext(gr_screen.offset_x+(gr_screen.clip_width-w)/2,y+gr_screen.offset_y,p1,strlen(p1));
470                 else
471                         opengl_mtext(gr_screen.offset_x+x,y+gr_screen.offset_y,p1,strlen(p1));
472
473                 p1 = p;
474                 if ( p1 && (strlen(p1) < 1) ) p1 = NULL;
475                 y += h;
476         } while(p1!=NULL);
477 }
478
479
480
481
482 void gr_opengl_circle( int xc, int yc, int d )
483 {
484         int p,x, y, r;
485
486         r = d/2;
487         p=3-d;
488         x=0;
489         y=r;
490
491         // Big clip
492         if ( (xc+r) < gr_screen.clip_left ) return;
493         if ( (xc-r) > gr_screen.clip_right ) return;
494         if ( (yc+r) < gr_screen.clip_top ) return;
495         if ( (yc-r) > gr_screen.clip_bottom ) return;
496
497         while(x<y)      {
498                 // Draw the first octant
499                 opengl_scanline( xc-y, xc+y, yc-x );
500                 opengl_scanline( xc-y, xc+y, yc+x );
501
502                 if (p<0) 
503                         p=p+(x<<2)+6;
504                 else    {
505                         // Draw the second octant
506                         opengl_scanline( xc-x, xc+x, yc-y );
507                         opengl_scanline( xc-x, xc+x, yc+y );
508                         p=p+((x-y)<<2)+10;
509                         y--;
510                 }
511                 x++;
512         }
513         if(x==y)        {
514                 opengl_scanline( xc-x, xc+x, yc-y );
515                 opengl_scanline( xc-x, xc+x, yc+y );
516         }
517         return;
518 }
519
520
521 void gr_opengl_line(int x1,int y1,int x2,int y2)
522 {
523         int clipped = 0, swapped=0;
524
525         glDisable ( GL_DEPTH_TEST );
526         glEnable ( GL_BLEND );
527         glBlendFunc ( GL_SRC_ALPHA, GL_DST_ALPHA );
528
529         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);
530                 
531         glBegin (GL_LINE);
532           glColor4f (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
533           glVertex3f (i2fl (x1+gr_screen.offset_x),i2fl (y1+gr_screen.offset_y), 0.99f);
534           glVertex3f (i2fl (x2+gr_screen.offset_x),i2fl (y2+gr_screen.offset_y), 0.99f);
535         glEnd ();
536 }
537
538 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
539
540 void gr_opengl_scaler(vertex *va, vertex *vb )
541 {
542         float x0, y0, x1, y1;
543         float u0, v0, u1, v1;
544         float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
545         float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
546         float xmin, xmax, ymin, ymax;
547         int dx0, dy0, dx1, dy1;
548
549         //============= CLIP IT =====================
550
551         x0 = va->sx; y0 = va->sy;
552         x1 = vb->sx; y1 = vb->sy;
553
554         xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
555         xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
556
557         u0 = va->u; v0 = va->v;
558         u1 = vb->u; v1 = vb->v;
559
560         // Check for obviously offscreen bitmaps...
561         if ( (y1<=y0) || (x1<=x0) ) return;
562         if ( (x1<xmin ) || (x0>xmax) ) return;
563         if ( (y1<ymin ) || (y0>ymax) ) return;
564
565         clipped_u0 = u0; clipped_v0 = v0;
566         clipped_u1 = u1; clipped_v1 = v1;
567
568         clipped_x0 = x0; clipped_y0 = y0;
569         clipped_x1 = x1; clipped_y1 = y1;
570
571         // Clip the left, moving u0 right as necessary
572         if ( x0 < xmin )        {
573                 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
574                 clipped_x0 = xmin;
575         }
576
577         // Clip the right, moving u1 left as necessary
578         if ( x1 > xmax )        {
579                 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
580                 clipped_x1 = xmax;
581         }
582
583         // Clip the top, moving v0 down as necessary
584         if ( y0 < ymin )        {
585                 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
586                 clipped_y0 = ymin;
587         }
588
589         // Clip the bottom, moving v1 up as necessary
590         if ( y1 > ymax )        {
591                 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
592                 clipped_y1 = ymax;
593         }
594         
595         dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
596         dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
597
598         if (dx1<=dx0) return;
599         if (dy1<=dy0) return;
600
601         //============= DRAW IT =====================
602         int u, v, du, dv;
603         int y, w;
604         ubyte * sbits;
605         bitmap * bp;
606         ubyte * spixels;
607         float tmpu, tmpv;
608
609         tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
610         if ( fl_abs(tmpu) < 0.001f ) {
611                 return;         // scaled up way too far!
612         }
613         tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
614         if ( fl_abs(tmpv) < 0.001f ) {
615                 return;         // scaled up way too far!
616         }
617
618         bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
619
620         du = fl2f(tmpu*(bp->w-1));
621         dv = fl2f(tmpv*(bp->h-1));
622
623         v = fl2f(clipped_v0*(bp->h-1));
624         u = fl2f(clipped_u0*(bp->w-1)); 
625         w = dx1 - dx0 + 1;
626
627         spixels = (ubyte *)bp->data;
628
629         for (y=dy0; y<=dy1; y++ )                       {
630                 sbits = &spixels[bp->rowsize*(v>>16)];
631
632                 int x, tmp_u;
633                 tmp_u = u;
634                 for (x=0; x<w; x++ )                    {
635                         ubyte c = sbits[ tmp_u >> 16 ];
636                         if ( c != 255 ) {
637                                 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
638                                 gr_pixel( x+dx0, y );
639                         }
640                         tmp_u += du;
641                 }
642                 v += dv;
643         }
644
645         bm_unlock(gr_screen.current_bitmap);
646
647         STUB_FUNCTION;
648 }
649
650 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
651 {
652         STUB_FUNCTION;
653 }
654
655
656 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
657 {
658         STUB_FUNCTION;
659 }
660
661 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
662 {
663         STUB_FUNCTION;
664 }
665
666 void gr_opengl_get_color( int * r, int * g, int * b )
667 {
668         if (r) *r = gr_screen.current_color.red;
669         if (g) *g = gr_screen.current_color.green;
670         if (b) *b = gr_screen.current_color.blue;
671 }
672
673 void gr_opengl_init_color(color *c, int r, int g, int b)
674 {
675         c->screen_sig = gr_screen.signature;
676         c->red = (unsigned char)r;
677         c->green = (unsigned char)g;
678         c->blue = (unsigned char)b;
679         c->alpha = 255;
680         c->ac_type = AC_TYPE_NONE;
681         c->alphacolor = -1;
682         c->is_alphacolor = 0;
683         c->magic = 0xAC01;
684 }
685
686 void gr_opengl_set_color( int r, int g, int b )
687 {
688         Assert((r >= 0) && (r < 256));
689         Assert((g >= 0) && (g < 256));
690         Assert((b >= 0) && (b < 256));
691
692         gr_opengl_init_color( &gr_screen.current_color, r, g, b );      
693 }
694
695 void gr_opengl_set_color_fast(color *dst)
696 {
697         if ( dst->screen_sig != gr_screen.signature )   {
698                 gr_init_color( dst, dst->red, dst->green, dst->blue );
699                 return;
700         }
701         gr_screen.current_color = *dst;
702 }
703
704 void gr_opengl_print_screen(char *filename)
705 {
706         STUB_FUNCTION;
707 }
708
709 int gr_opengl_supports_res_ingame(int res)
710 {
711         STUB_FUNCTION;
712         
713         return 1;
714 }
715
716 int gr_opengl_supports_res_interface(int res)
717 {
718         STUB_FUNCTION;
719         
720         return 1;
721 }
722
723 void gr_opengl_cleanup()
724 {
725         if ( !Inited )  return;
726
727         gr_reset_clip();
728         gr_clear();
729         gr_flip();
730
731         Inited = 0;
732 }
733
734 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
735 {
736         STUB_FUNCTION;
737 }
738
739 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
740 {
741         STUB_FUNCTION;
742 }
743
744 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
745 {
746         STUB_FUNCTION;
747 }
748
749 void gr_opengl_set_cull(int cull)
750 {
751         STUB_FUNCTION;
752 }
753
754 void gr_opengl_filter_set(int filter)
755 {
756         STUB_FUNCTION;
757 }
758
759 // cross fade
760 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
761 {
762         STUB_FUNCTION;
763 }
764
765 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)
766 {
767         STUB_FUNCTION;
768         
769         return 1;
770 }
771
772 void gr_opengl_set_clear_color(int r, int g, int b)
773 {
774         STUB_FUNCTION;
775 }
776
777 void gr_opengl_aabitmap(int x, int y)
778 {
779         STUB_FUNCTION;
780 }
781
782 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
783 {
784         STUB_FUNCTION;
785 }
786
787 void gr_opengl_aaline(vertex *v1, vertex *v2)
788 {
789         STUB_FUNCTION;
790 }
791
792 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
793 {
794         STUB_FUNCTION;
795 }
796
797 void gr_opengl_flash(int r, int g, int b)
798 {
799         STUB_FUNCTION;
800 }
801
802 int gr_opengl_zbuffer_get()
803 {
804         STUB_FUNCTION;
805         
806         return GR_ZBUFF_NONE;
807 }
808
809 int gr_opengl_zbuffer_set(int mode)
810 {
811         STUB_FUNCTION;
812         
813         return GR_ZBUFF_NONE;
814 }
815
816 void gr_opengl_zbuffer_clear(int mode)
817 {
818         STUB_FUNCTION;
819 }
820
821 void gr_opengl_set_gamma(float gamma)
822 {
823         STUB_FUNCTION;
824 }
825
826 void gr_opengl_fade_in(int instantaneous)
827 {
828         STUB_FUNCTION;
829 }
830
831 void gr_opengl_fade_out(int instantaneous)
832 {
833         STUB_FUNCTION;
834 }
835
836 int gr_opengl_save_screen()
837 {
838         STUB_FUNCTION;
839         
840         return -1;
841 }
842
843 void gr_opengl_restore_screen(int id)
844 {
845         STUB_FUNCTION;
846 }
847
848 void gr_opengl_free_screen(int id)
849 {
850         STUB_FUNCTION;
851 }
852
853 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
854 {
855         STUB_FUNCTION;
856 }
857
858 void gr_opengl_dump_frame_stop()
859 {
860         STUB_FUNCTION;
861 }
862
863 void gr_opengl_dump_frame()
864 {
865         STUB_FUNCTION;
866 }
867
868 uint gr_opengl_lock()
869 {
870         STUB_FUNCTION;
871         
872         return 1;
873 }
874         
875 void gr_opengl_unlock()
876 {
877 }
878         
879 void gr_opengl_init()
880 {
881         if ( Inited )   {
882                 gr_opengl_cleanup();
883                 Inited = 0;
884         }
885
886         mprintf(( "Initializing opengl graphics device...\n" ));
887         Inited = 1;
888
889 #ifdef PLAT_UNIX
890         if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
891         {
892                 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
893                 exit (1);
894         }
895
896         atexit (SDL_Quit);
897
898         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
899         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
900         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
901         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
902         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
903                                                 
904         if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
905         {
906                 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
907                 exit (1);
908         }
909 #endif
910         int bpp = 16;
911         
912         switch( bpp )   {
913         case 8:
914                 Gr_red.bits = 8;
915                 Gr_red.shift = 16;
916                 Gr_red.scale = 1;
917                 Gr_red.mask = 0xff0000;
918
919                 Gr_green.bits = 8;
920                 Gr_green.shift = 8;
921                 Gr_green.scale = 1;
922                 Gr_green.mask = 0xff00;
923
924                 Gr_blue.bits = 8;
925                 Gr_blue.shift = 0;
926                 Gr_blue.scale = 1;
927                 Gr_blue.mask = 0xff;
928
929         case 15:
930                 Gr_red.bits = 5;
931                 Gr_red.shift = 10;
932                 Gr_red.scale = 8;
933                 Gr_red.mask = 0x7C00;
934
935                 Gr_green.bits = 5;
936                 Gr_green.shift = 5;
937                 Gr_green.scale = 8;
938                 Gr_green.mask = 0x3E0;
939
940                 Gr_blue.bits = 5;
941                 Gr_blue.shift = 0;
942                 Gr_blue.scale = 8;
943                 Gr_blue.mask = 0x1F;
944
945                 break;
946
947         case 16:
948                 Gr_red.bits = 5;
949                 Gr_red.shift = 11;
950                 Gr_red.scale = 8;
951                 Gr_red.mask = 0xF800;
952
953                 Gr_green.bits = 6;
954                 Gr_green.shift = 5;
955                 Gr_green.scale = 4;
956                 Gr_green.mask = 0x7E0;
957
958                 Gr_blue.bits = 5;
959                 Gr_blue.shift = 0;
960                 Gr_blue.scale = 8;
961                 Gr_blue.mask = 0x1F;
962
963                 break;
964
965         case 24:
966         case 32:
967                 Gr_red.bits = 8;
968                 Gr_red.shift = 16;
969                 Gr_red.scale = 1;
970                 Gr_red.mask = 0xff0000;
971
972                 Gr_green.bits = 8;
973                 Gr_green.shift = 8;
974                 Gr_green.scale = 1;
975                 Gr_green.mask = 0xff00;
976
977                 Gr_blue.bits = 8;
978                 Gr_blue.shift = 0;
979                 Gr_blue.scale = 1;
980                 Gr_blue.mask = 0xff;
981
982                 break;
983
984         default:
985                 Int3(); // Illegal bpp
986         }
987
988
989         gr_opengl_clear();
990
991         Gr_current_red = &Gr_red;
992         Gr_current_blue = &Gr_blue;
993         Gr_current_green = &Gr_green;
994         Gr_current_alpha = &Gr_alpha;
995                                 
996         gr_screen.gf_flip = gr_opengl_flip;
997         gr_screen.gf_flip_window = gr_opengl_flip_window;
998         gr_screen.gf_set_clip = gr_opengl_set_clip;
999         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
1000         gr_screen.gf_set_font = gr_opengl_set_font;
1001         
1002         gr_screen.gf_set_color = gr_opengl_set_color;
1003         gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
1004         gr_screen.gf_create_shader = gr_opengl_create_shader;
1005         gr_screen.gf_set_shader = gr_opengl_set_shader;
1006         gr_screen.gf_clear = gr_opengl_clear;
1007         // gr_screen.gf_bitmap = gr_opengl_bitmap;
1008         // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
1009         gr_screen.gf_aabitmap = gr_opengl_aabitmap;
1010         gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
1011         
1012         gr_screen.gf_rect = gr_opengl_rect;
1013         gr_screen.gf_shade = gr_opengl_shade;
1014         gr_screen.gf_string = gr_opengl_string;
1015         gr_screen.gf_circle = gr_opengl_circle;
1016
1017         gr_screen.gf_line = gr_opengl_line;
1018         gr_screen.gf_aaline = gr_opengl_aaline;
1019         gr_screen.gf_pixel = gr_opengl_pixel;
1020         gr_screen.gf_scaler = gr_opengl_scaler;
1021         gr_screen.gf_tmapper = gr_opengl_tmapper;
1022
1023         gr_screen.gf_gradient = gr_opengl_gradient;
1024
1025         gr_screen.gf_set_palette = gr_opengl_set_palette;
1026         gr_screen.gf_get_color = gr_opengl_get_color;
1027         gr_screen.gf_init_color = gr_opengl_init_color;
1028         gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
1029         gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
1030         gr_screen.gf_print_screen = gr_opengl_print_screen;
1031
1032         gr_screen.gf_fade_in = gr_opengl_fade_in;
1033         gr_screen.gf_fade_out = gr_opengl_fade_out;
1034         gr_screen.gf_flash = gr_opengl_flash;
1035         
1036         gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
1037         gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
1038         gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
1039         
1040         gr_screen.gf_save_screen = gr_opengl_save_screen;
1041         gr_screen.gf_restore_screen = gr_opengl_restore_screen;
1042         gr_screen.gf_free_screen = gr_opengl_free_screen;
1043         
1044         gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
1045         gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
1046         gr_screen.gf_dump_frame = gr_opengl_dump_frame;
1047         
1048         gr_screen.gf_set_gamma = gr_opengl_set_gamma;
1049         
1050         gr_screen.gf_lock = gr_opengl_lock;
1051         gr_screen.gf_unlock = gr_opengl_unlock;
1052         
1053         gr_screen.gf_fog_set = gr_opengl_fog_set;       
1054
1055         gr_screen.gf_get_region = gr_opengl_get_region;
1056
1057         gr_screen.gf_get_pixel = gr_opengl_get_pixel;
1058
1059         gr_screen.gf_set_cull = gr_opengl_set_cull;
1060
1061         gr_screen.gf_cross_fade = gr_opengl_cross_fade;
1062
1063         gr_screen.gf_filter_set = gr_opengl_filter_set;
1064
1065         gr_screen.gf_tcache_set = gr_opengl_tcache_set;
1066
1067         gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;
1068 }
1069