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