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