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