2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.14 2002/05/29 08:54:40 relnev
11 * "fixed" bitmap drawing.
13 * copied more d3d code over.
15 * Revision 1.13 2002/05/29 06:25:13 theoddone33
16 * Keyboard input, mouse tracking now work
18 * Revision 1.12 2002/05/29 04:52:45 relnev
21 * Revision 1.11 2002/05/29 04:29:56 relnev
22 * removed some unncessary stubbing, implemented opengl rect
24 * Revision 1.10 2002/05/29 04:13:27 theoddone33
27 * Revision 1.9 2002/05/29 03:35:51 relnev
30 * Revision 1.8 2002/05/29 03:30:05 relnev
33 * Revision 1.7 2002/05/29 02:52:32 theoddone33
34 * Enable OpenGL renderer
36 * Revision 1.6 2002/05/28 04:56:51 theoddone33
37 * runs a little bit now
39 * Revision 1.5 2002/05/28 04:07:28 theoddone33
40 * New graphics stubbing arrangement
42 * Revision 1.4 2002/05/27 23:39:34 relnev
45 * Revision 1.3 2002/05/27 22:35:01 theoddone33
48 * Revision 1.2 2002/05/27 22:32:02 theoddone33
49 * throw all d3d stuff at opengl
51 * Revision 1.1.1.1 2002/05/03 03:28:09 root
55 * 10 7/14/99 9:42a Dave
56 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
59 * 9 7/09/99 9:51a Dave
60 * Added thick polyline code.
62 * 8 6/29/99 10:35a Dave
63 * Interface polygon bitmaps! Whee!
65 * 7 2/03/99 11:44a Dave
66 * Fixed d3d transparent textures.
68 * 6 1/24/99 11:37p Dave
69 * First full rev of beam weapons. Very customizable. Removed some bogus
70 * Int3()'s in low level net code.
72 * 5 12/18/98 1:13a Dave
73 * Rough 1024x768 support for Direct3D. Proper detection and usage through
76 * 4 12/06/98 2:36p Dave
77 * Drastically improved nebula fogging.
79 * 3 11/11/98 5:37p Dave
80 * Checkin for multiplayer testing.
82 * 2 10/07/98 10:53a Dave
85 * 1 10/07/98 10:49a Dave
87 * 14 5/20/98 9:46p John
88 * added code so the places in code that change half the palette don't
89 * have to clear the screen.
91 * 13 5/06/98 5:30p John
92 * Removed unused cfilearchiver. Removed/replaced some unused/little used
93 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
94 * DirectX header files and libs that fixed the Direct3D alpha blending
97 * 12 4/14/98 12:15p John
98 * Made 16-bpp movies work.
100 * 11 3/12/98 5:36p John
101 * Took out any unused shaders. Made shader code take rgbc instead of
102 * matrix and vector since noone used it like a matrix and it would have
103 * been impossible to do in hardware. Made Glide implement a basic
104 * shader for online help.
106 * 10 3/10/98 4:18p John
107 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
108 * & Glide have popups and print screen. Took out all >8bpp software
109 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
110 * support Fred. Made zbuffering key off of functions rather than one
113 * 9 12/02/97 4:00p John
114 * Added first rev of thruster glow, along with variable levels of
115 * translucency, which retquired some restructing of palman.
117 * 8 10/03/97 9:10a John
118 * added better antialiased line drawer
120 * 7 9/23/97 10:45a John
121 * made so you can tell bitblt code to rle a bitmap by passing flag to
124 * 6 9/09/97 11:01a Sandeep
125 * fixed warning level 4 bugs
127 * 5 7/10/97 2:06p John
128 * added code to specify alphablending type for bitmaps.
130 * 4 6/17/97 7:04p John
131 * added d3d support for gradients.
132 * fixed some color bugs by adding screen signatures instead of watching
133 * flags and palette changes.
135 * 3 6/12/97 2:50a Lawrance
136 * bm_unlock() now passed bitmap number, not pointer
138 * 2 6/11/97 1:12p John
139 * Started fixing all the text colors in the game.
141 * 1 5/12/97 12:14p John
148 #include <windowsx.h>
155 #include "floating.h"
157 #include "grinternal.h"
158 #include "gropengl.h"
161 static int Inited = 0;
163 static GLuint bitmapTex;
164 static GLubyte *bitmapMem;
167 // Throw in some dummy functions - DDOI
169 int D3D_32bit = 0; // grd3d.cpp
170 int D3D_fog_mode = -1; // grd3d.cpp
171 int D3D_inited = 0; // grd3d.cpp
172 int D3D_zbias = 1; // grd3d.cpp
173 int D3d_rendition_uvs = 0; // grd3d.cpp
175 void gr_dd_activate(int active) // grdirectdraw.cpp
180 void gr_directdraw_cleanup() // grdirectdraw.cpp
185 void gr_directdraw_force_windowed() // grdirectdraw.cpp
190 void gr_directdraw_init()
196 void gr_d3d_preload(int x, int y)
201 void d3d_start_frame()
206 void d3d_stop_frame()
216 void d3d_zbias (int a)
222 void gr_opengl_activate(int b)
227 void gr_opengl_preload_init()
232 void gr_opengl_pixel(int x, int y)
234 if ( x < gr_screen.clip_left ) return;
235 if ( x > gr_screen.clip_right ) return;
236 if ( y < gr_screen.clip_top ) return;
237 if ( y > gr_screen.clip_bottom ) return;
240 void gr_opengl_clear()
242 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
245 void gr_opengl_flip()
248 if (Inited) SDL_GL_SwapBuffers ();
252 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
257 void gr_opengl_set_clip(int x,int y,int w,int h)
259 // check for sanity of parameters
265 if (x >= gr_screen.max_w)
266 x = gr_screen.max_w - 1;
267 if (y >= gr_screen.max_h)
268 y = gr_screen.max_h - 1;
270 if (x + w > gr_screen.max_w)
271 w = gr_screen.max_w - x;
272 if (y + h > gr_screen.max_h)
273 h = gr_screen.max_h - y;
275 if (w > gr_screen.max_w)
277 if (h > gr_screen.max_h)
280 gr_screen.offset_x = x;
281 gr_screen.offset_y = y;
282 gr_screen.clip_left = 0;
283 gr_screen.clip_right = w-1;
284 gr_screen.clip_top = 0;
285 gr_screen.clip_bottom = h-1;
286 gr_screen.clip_width = w;
287 gr_screen.clip_height = h;
292 void gr_opengl_reset_clip()
294 gr_screen.offset_x = 0;
295 gr_screen.offset_y = 0;
296 gr_screen.clip_left = 0;
297 gr_screen.clip_top = 0;
298 gr_screen.clip_right = gr_screen.max_w - 1;
299 gr_screen.clip_bottom = gr_screen.max_h - 1;
300 gr_screen.clip_width = gr_screen.max_w;
301 gr_screen.clip_height = gr_screen.max_h;
306 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
308 gr_screen.current_alpha = alpha;
309 gr_screen.current_alphablend_mode = alphablend_mode;
310 gr_screen.current_bitblt_mode = bitblt_mode;
311 gr_screen.current_bitmap = bitmap_num;
313 gr_screen.current_bitmap_sx = sx;
314 gr_screen.current_bitmap_sy = sy;
317 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
319 shade->screen_sig = gr_screen.signature;
326 void gr_opengl_set_shader( shader * shade )
329 if (shade->screen_sig != gr_screen.signature) {
330 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
332 gr_screen.current_shader = *shade;
334 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
338 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
342 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
346 GLubyte *sptr, *dptr;
350 int cw = min(bmp->w, w);
351 int ch = min(bmp->h, h);
353 glColor4f(1.0, 1.0, 1.0, 1.0);
354 glBindTexture(GL_TEXTURE_2D, bitmapTex);
357 for (iy = sy; iy < ch; iy += 256) {
359 ih = min(256, (ch-iy));
361 for (ix = sx; ix < cw; ix += 256) {
363 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
365 iw = min(256, (cw-ix));
371 memcpy(dptr, sptr, iw*2);
379 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
389 t = (float)ih / 256.0;
394 s = (float)iw / 256.0;
395 t = (float)ih / 256.0;
400 s = (float)iw / 256.0;
414 void gr_d3d_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
421 int dx1=x, dx2=x+w-1;
422 int dy1=y, dy2=y+h-1;
425 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
430 if ( count > 1 ) Int3();
434 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
435 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
436 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
437 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
438 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
439 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
466 if ( w < 1 ) return; // clipped away!
467 if ( h < 1 ) return; // clipped away!
471 // Make sure clipping algorithm works
475 Assert( w == (dx2-dx1+1) );
476 Assert( h == (dy2-dy1+1) );
479 Assert( sx+w <= bw );
480 Assert( sy+h <= bh );
481 Assert( dx2 >= dx1 );
482 Assert( dy2 >= dy1 );
483 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
484 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
485 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
486 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
489 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
490 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
492 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
495 void gr_opengl_bitmap(int x, int y)
499 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
500 int dx1=x, dx2=x+w-1;
501 int dy1=y, dy2=y+h-1;
504 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
505 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
506 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
507 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
508 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
509 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
511 if ( sx < 0 ) return;
512 if ( sy < 0 ) return;
513 if ( sx >= w ) return;
514 if ( sy >= h ) return;
516 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
518 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
521 static void opengl_scanline(int x1,int x2,int y)
526 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
530 saved_zbuf = gr_zbuffer_get();
531 gr_zbuffer_set(GR_ZBUFF_NONE);
534 glColor4ub(r, g, b, a);
543 glVertex2f(x+w, y+h);
549 gr_zbuffer_set(saved_zbuf);
553 void gr_opengl_rect(int x,int y,int w,int h)
555 gr_opengl_rect_internal(x, y, w, h, gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
558 void gr_opengl_shade(int x,int y,int w,int h)
565 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
566 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
567 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
568 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
569 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
570 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
571 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
572 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
574 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
577 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
582 if ( !gr_screen.current_color.is_alphacolor ) return;
584 float u_scale, v_scale;
586 // gr_d3d_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
588 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
589 // Couldn't set texture
590 //mprintf(( "GLIDE: Error setting aabitmap texture!\n" ));
594 // LPD3DTLVERTEX src_v;
595 // D3DTLVERTEX d3d_verts[4];
597 float u0, u1, v0, v1;
598 float x1, x2, y1, y2;
601 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
603 u0 = u_scale*i2fl(sx)/i2fl(bw);
604 v0 = v_scale*i2fl(sy)/i2fl(bh);
606 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
607 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
609 x1 = i2fl(x+gr_screen.offset_x);
610 y1 = i2fl(y+gr_screen.offset_y);
611 x2 = i2fl(x+w+gr_screen.offset_x);
612 y2 = i2fl(y+h+gr_screen.offset_y);
616 if ( gr_screen.current_color.is_alphacolor ) {
617 // color = RGBA_MAKE(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
618 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
620 // int r = (gr_screen.current_color.red*gr_screen.current_color.alpha)/255;
621 // int g = (gr_screen.current_color.green*gr_screen.current_color.alpha)/255;
622 // int b = (gr_screen.current_color.blue*gr_screen.current_color.alpha)/255;
624 // color = RGBA_MAKE(r,g,b, 255 );
625 // glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
628 // color = RGB_MAKE(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
629 // glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
635 src_v->color = color;
645 src_v->color = color;
655 src_v->color = color;
665 src_v->color = color;
672 d3d_DrawPrimitive(D3DPT_TRIANGLEFAN,D3DVT_TLVERTEX,(LPVOID)d3d_verts,4,NULL);
676 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
683 int dx1=x, dx2=x+w-1;
684 int dy1=y, dy2=y+h-1;
687 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
692 if ( count > 1 ) Int3();
696 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
697 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
698 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
699 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
700 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
701 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
728 if ( w < 1 ) return; // clipped away!
729 if ( h < 1 ) return; // clipped away!
733 // Make sure clipping algorithm works
737 Assert( w == (dx2-dx1+1) );
738 Assert( h == (dy2-dy1+1) );
741 Assert( sx+w <= bw );
742 Assert( sy+h <= bh );
743 Assert( dx2 >= dx1 );
744 Assert( dy2 >= dy1 );
745 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
746 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
747 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
748 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
751 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
752 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
755 void gr_opengl_aabitmap(int x, int y)
759 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
760 int dx1=x, dx2=x+w-1;
761 int dy1=y, dy2=y+h-1;
764 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
765 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
766 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
767 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
768 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
769 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
771 if ( sx < 0 ) return;
772 if ( sy < 0 ) return;
773 if ( sx >= w ) return;
774 if ( sy >= h ) return;
776 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
777 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
781 void gr_opengl_string( int sx, int sy, char *s )
783 int width, spacing, letter;
786 if ( !Current_font ) {
790 gr_set_bitmap(Current_font->bitmap_id);
795 if (sx==0x8000) { //centered
796 x = get_centered_x(s);
808 y += Current_font->h;
809 if (sx==0x8000) { //centered
810 x = get_centered_x(s);
817 letter = get_char_width(s[0],s[1],&width,&spacing);
820 //not in font, draw as space
828 // Check if this character is totally clipped
829 if ( x + width < gr_screen.clip_left ) continue;
830 if ( y + Current_font->h < gr_screen.clip_top ) continue;
831 if ( x > gr_screen.clip_right ) continue;
832 if ( y > gr_screen.clip_bottom ) continue;
835 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
836 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
840 wc = width - xd; hc = Current_font->h - yd;
841 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
842 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
844 if ( wc < 1 ) continue;
845 if ( hc < 1 ) continue;
849 ch = &Current_font->char_data[letter];
851 int u = Current_font->bm_u[letter];
852 int v = Current_font->bm_v[letter];
854 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
858 void gr_opengl_circle( int xc, int yc, int d )
868 if ( (xc+r) < gr_screen.clip_left ) return;
869 if ( (xc-r) > gr_screen.clip_right ) return;
870 if ( (yc+r) < gr_screen.clip_top ) return;
871 if ( (yc-r) > gr_screen.clip_bottom ) return;
874 // Draw the first octant
875 opengl_scanline( xc-y, xc+y, yc-x );
876 opengl_scanline( xc-y, xc+y, yc+x );
881 // Draw the second octant
882 opengl_scanline( xc-x, xc+x, yc-y );
883 opengl_scanline( xc-x, xc+x, yc+y );
890 opengl_scanline( xc-x, xc+x, yc-y );
891 opengl_scanline( xc-x, xc+x, yc+y );
897 void gr_opengl_line(int x1,int y1,int x2,int y2)
899 int clipped = 0, swapped=0;
901 glDisable ( GL_DEPTH_TEST );
902 glEnable ( GL_BLEND );
903 glBlendFunc ( GL_SRC_ALPHA, GL_DST_ALPHA );
905 INT_CLIPLINE(x1,y1,x2,y2,gr_screen.clip_left,gr_screen.clip_top,gr_screen.clip_right,gr_screen.clip_bottom,return,clipped=1,swapped=1);
908 glColor4f (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
909 glVertex3f (i2fl (x1+gr_screen.offset_x),i2fl (y1+gr_screen.offset_y), 0.99f);
910 glVertex3f (i2fl (x2+gr_screen.offset_x),i2fl (y2+gr_screen.offset_y), 0.99f);
914 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
916 void gr_opengl_scaler(vertex *va, vertex *vb )
918 float x0, y0, x1, y1;
919 float u0, v0, u1, v1;
920 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
921 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
922 float xmin, xmax, ymin, ymax;
923 int dx0, dy0, dx1, dy1;
925 //============= CLIP IT =====================
927 x0 = va->sx; y0 = va->sy;
928 x1 = vb->sx; y1 = vb->sy;
930 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
931 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
933 u0 = va->u; v0 = va->v;
934 u1 = vb->u; v1 = vb->v;
936 // Check for obviously offscreen bitmaps...
937 if ( (y1<=y0) || (x1<=x0) ) return;
938 if ( (x1<xmin ) || (x0>xmax) ) return;
939 if ( (y1<ymin ) || (y0>ymax) ) return;
941 clipped_u0 = u0; clipped_v0 = v0;
942 clipped_u1 = u1; clipped_v1 = v1;
944 clipped_x0 = x0; clipped_y0 = y0;
945 clipped_x1 = x1; clipped_y1 = y1;
947 // Clip the left, moving u0 right as necessary
949 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
953 // Clip the right, moving u1 left as necessary
955 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
959 // Clip the top, moving v0 down as necessary
961 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
965 // Clip the bottom, moving v1 up as necessary
967 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
971 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
972 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
974 if (dx1<=dx0) return;
975 if (dy1<=dy0) return;
977 //============= DRAW IT =====================
985 tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
986 if ( fl_abs(tmpu) < 0.001f ) {
987 return; // scaled up way too far!
989 tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
990 if ( fl_abs(tmpv) < 0.001f ) {
991 return; // scaled up way too far!
994 bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
996 du = fl2f(tmpu*(bp->w-1));
997 dv = fl2f(tmpv*(bp->h-1));
999 v = fl2f(clipped_v0*(bp->h-1));
1000 u = fl2f(clipped_u0*(bp->w-1));
1003 spixels = (ubyte *)bp->data;
1005 for (y=dy0; y<=dy1; y++ ) {
1006 sbits = &spixels[bp->rowsize*(v>>16)];
1010 for (x=0; x<w; x++ ) {
1011 ubyte c = sbits[ tmp_u >> 16 ];
1013 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
1014 gr_pixel( x+dx0, y );
1021 bm_unlock(gr_screen.current_bitmap);
1026 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
1029 float u_scale = 1.0f, v_scale = 1.0f;
1032 // Make nebula use the texture mapper... this blends the colors better.
1033 if ( flags & TMAP_FLAG_NEBULA ){
1038 gr_texture_source texture_source = (gr_texture_source)-1;
1039 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1040 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1042 if ( gr_zbuffering ) {
1043 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1044 zbuffer_type = ZBUFFER_TYPE_READ;
1046 zbuffer_type = ZBUFFER_TYPE_FULL;
1049 zbuffer_type = ZBUFFER_TYPE_NONE;
1055 int tmap_type = TCACHE_TYPE_NORMAL;
1059 if ( flags & TMAP_FLAG_TEXTURED ) {
1062 r = gr_screen.current_color.red;
1063 g = gr_screen.current_color.green;
1064 b = gr_screen.current_color.blue;
1067 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1069 // Some blend function stuff here - DDOI
1072 float factor = gr_screen.current_alpha;
1076 if ( factor <= 1.0f ) {
1077 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1078 r = (r*tmp_alpha)/255;
1079 g = (g*tmp_alpha)/255;
1080 b = (b*tmp_alpha)/255;
1087 if ( flags & TMAP_FLAG_TEXTURED ) {
1088 if ( !gr_tcache_set(gr_screen.current_bitmap, tmap_type, &u_scale, &v_scale, 0, gr_screen.current_bitmap_sx, gr_screen.current_bitmap_sy ))
1090 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1094 // use nonfiltered textures for bitmap sections
1096 if(flags & TMAP_FLAG_BITMAP_SECTION){
1097 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1099 texture_source = TEXTURE_SOURCE_DECAL;
1105 x1 = gr_screen.clip_left*16;
1106 x2 = gr_screen.clip_right*16+15;
1107 y1 = gr_screen.clip_top*16;
1108 y2 = gr_screen.clip_bottom*16+15;
1110 float uoffset = 0.0f;
1111 float voffset = 0.0f;
1113 float minu=0.0f, minv=0.0f, maxu=1.0f, maxv=1.0f;
1115 if ( flags & TMAP_FLAG_TEXTURED )
1122 STUB_FUNCTION; // DDOI - this still needs work
1123 glBegin (GL_TRIANGLE_FAN);
1126 if (flags & TMAP_FLAG_ALPHA) a = verts[i]->a;
1129 if (flags & TMAP_FLAG_NEBULA ) {
1131 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1132 r = gr_palette[pal*3+0];
1133 g = gr_palette[pal*3+1];
1134 b = gr_palette[pal*3+2];
1136 }else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1137 r = Gr_gamma_lookup[verts[i]->b];
1138 g = Gr_gamma_lookup[verts[i]->b];
1139 b = Gr_gamma_lookup[verts[i]->b];
1140 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1141 // Make 0.75 be 256.0f
1142 r = Gr_gamma_lookup[verts[i]->r];
1143 g = Gr_gamma_lookup[verts[i]->g];
1144 b = Gr_gamma_lookup[verts[i]->b];
1146 // use constant RGB values...
1149 glColor4i (r,g,b,a);
1151 // DDOI - FIXME fog stuff
1152 // DDOI - FIXME TexCoord stuff
1153 glVertex3f (verts[i]->sx+gr_screen.offset_x,
1154 verts[i]->sy+gr_screen.offset_y,
1160 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1165 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1170 void gr_opengl_get_color( int * r, int * g, int * b )
1172 if (r) *r = gr_screen.current_color.red;
1173 if (g) *g = gr_screen.current_color.green;
1174 if (b) *b = gr_screen.current_color.blue;
1177 void gr_opengl_init_color(color *c, int r, int g, int b)
1179 c->screen_sig = gr_screen.signature;
1180 c->red = (unsigned char)r;
1181 c->green = (unsigned char)g;
1182 c->blue = (unsigned char)b;
1184 c->ac_type = AC_TYPE_NONE;
1186 c->is_alphacolor = 0;
1190 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1192 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1193 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1194 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1195 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1197 gr_opengl_init_color( clr, r, g, b );
1199 clr->alpha = (unsigned char)alpha;
1200 clr->ac_type = (ubyte)type;
1201 clr->alphacolor = -1;
1202 clr->is_alphacolor = 1;
1205 void gr_opengl_set_color( int r, int g, int b )
1207 Assert((r >= 0) && (r < 256));
1208 Assert((g >= 0) && (g < 256));
1209 Assert((b >= 0) && (b < 256));
1211 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1214 void gr_opengl_set_color_fast(color *dst)
1216 if ( dst->screen_sig != gr_screen.signature ) {
1217 if ( dst->is_alphacolor ) {
1218 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1220 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1223 gr_screen.current_color = *dst;
1226 void gr_opengl_print_screen(char *filename)
1231 int gr_opengl_supports_res_ingame(int res)
1238 int gr_opengl_supports_res_interface(int res)
1245 void gr_opengl_cleanup()
1247 if ( !Inited ) return;
1256 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
1261 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1266 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
1271 void gr_opengl_set_cull(int cull)
1276 void gr_opengl_filter_set(int filter)
1282 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1287 int gr_opengl_tcache_set(int bitmap_id, int bitmap_type, float *u_ratio, float *v_ratio, int fail_on_full = 0, int sx = -1, int sy = -1, int force = 0)
1290 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
1291 bm_unlock(bitmap_id);
1293 fprintf(stderr, "DEBUG: %d %d: ", bmp->w, bmp->h);
1299 void gr_opengl_set_clear_color(int r, int g, int b)
1304 void gr_opengl_aaline(vertex *v1, vertex *v2)
1309 void gr_opengl_flash(int r, int g, int b)
1314 int gr_opengl_zbuffer_get()
1318 return GR_ZBUFF_NONE;
1321 int gr_opengl_zbuffer_set(int mode)
1325 return GR_ZBUFF_NONE;
1328 void gr_opengl_zbuffer_clear(int mode)
1333 void gr_opengl_set_gamma(float gamma)
1338 void gr_opengl_fade_in(int instantaneous)
1343 void gr_opengl_fade_out(int instantaneous)
1348 int gr_opengl_save_screen()
1355 void gr_opengl_restore_screen(int id)
1360 void gr_opengl_free_screen(int id)
1365 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
1370 void gr_opengl_dump_frame_stop()
1375 void gr_opengl_dump_frame()
1380 uint gr_opengl_lock()
1387 void gr_opengl_unlock()
1391 void gr_opengl_init()
1394 gr_opengl_cleanup();
1398 mprintf(( "Initializing opengl graphics device...\n" ));
1402 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
1404 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
1410 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
1411 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
1412 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
1413 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
1414 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
1416 if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
1418 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
1422 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
1424 glMatrixMode(GL_PROJECTION);
1426 glMatrixMode(GL_MODELVIEW);
1429 glEnable(GL_TEXTURE_2D);
1431 glGenTextures(1, &bitmapTex);
1432 glBindTexture(GL_TEXTURE_2D, bitmapTex);
1433 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1434 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1435 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1437 glMatrixMode(GL_PROJECTION);
1441 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, -1.0, 1.0);
1443 bitmapMem = (GLubyte *)malloc(256*256*4);
1444 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, bitmapMem);
1445 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_CHAR, bitmapMem);
1456 Gr_red.mask = 0xff0000;
1461 Gr_green.mask = 0xff00;
1466 Gr_blue.mask = 0xff;
1474 Gr_red.mask = 0x7C00;
1479 Gr_green.mask = 0x3E0;
1484 Gr_blue.mask = 0x1F;
1492 Gr_red.mask = 0xF800;
1497 Gr_green.mask = 0x7E0;
1502 Gr_blue.mask = 0x1F;
1511 Gr_red.mask = 0xff0000;
1516 Gr_green.mask = 0xff00;
1521 Gr_blue.mask = 0xff;
1526 Int3(); // Illegal bpp
1532 Gr_current_red = &Gr_red;
1533 Gr_current_blue = &Gr_blue;
1534 Gr_current_green = &Gr_green;
1535 Gr_current_alpha = &Gr_alpha;
1537 gr_screen.gf_flip = gr_opengl_flip;
1538 gr_screen.gf_flip_window = gr_opengl_flip_window;
1539 gr_screen.gf_set_clip = gr_opengl_set_clip;
1540 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
1541 gr_screen.gf_set_font = grx_set_font;
1543 gr_screen.gf_set_color = gr_opengl_set_color;
1544 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
1545 gr_screen.gf_create_shader = gr_opengl_create_shader;
1546 gr_screen.gf_set_shader = gr_opengl_set_shader;
1547 gr_screen.gf_clear = gr_opengl_clear;
1548 // gr_screen.gf_bitmap = gr_opengl_bitmap;
1549 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
1550 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
1551 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
1553 gr_screen.gf_rect = gr_opengl_rect;
1554 gr_screen.gf_shade = gr_opengl_shade;
1555 gr_screen.gf_string = gr_opengl_string;
1556 gr_screen.gf_circle = gr_opengl_circle;
1558 gr_screen.gf_line = gr_opengl_line;
1559 gr_screen.gf_aaline = gr_opengl_aaline;
1560 gr_screen.gf_pixel = gr_opengl_pixel;
1561 gr_screen.gf_scaler = gr_opengl_scaler;
1562 gr_screen.gf_tmapper = gr_opengl_tmapper;
1564 gr_screen.gf_gradient = gr_opengl_gradient;
1566 gr_screen.gf_set_palette = gr_opengl_set_palette;
1567 gr_screen.gf_get_color = gr_opengl_get_color;
1568 gr_screen.gf_init_color = gr_opengl_init_color;
1569 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
1570 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
1571 gr_screen.gf_print_screen = gr_opengl_print_screen;
1573 gr_screen.gf_fade_in = gr_opengl_fade_in;
1574 gr_screen.gf_fade_out = gr_opengl_fade_out;
1575 gr_screen.gf_flash = gr_opengl_flash;
1577 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
1578 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
1579 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
1581 gr_screen.gf_save_screen = gr_opengl_save_screen;
1582 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
1583 gr_screen.gf_free_screen = gr_opengl_free_screen;
1585 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
1586 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
1587 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
1589 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
1591 gr_screen.gf_lock = gr_opengl_lock;
1592 gr_screen.gf_unlock = gr_opengl_unlock;
1594 gr_screen.gf_fog_set = gr_opengl_fog_set;
1596 gr_screen.gf_get_region = gr_opengl_get_region;
1598 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
1600 gr_screen.gf_set_cull = gr_opengl_set_cull;
1602 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
1604 gr_screen.gf_filter_set = gr_opengl_filter_set;
1606 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
1608 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;