2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.1 2002/05/03 03:28:09 root
14 * 10 7/14/99 9:42a Dave
15 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
18 * 9 7/09/99 9:51a Dave
19 * Added thick polyline code.
21 * 8 6/29/99 10:35a Dave
22 * Interface polygon bitmaps! Whee!
24 * 7 2/03/99 11:44a Dave
25 * Fixed d3d transparent textures.
27 * 6 1/24/99 11:37p Dave
28 * First full rev of beam weapons. Very customizable. Removed some bogus
29 * Int3()'s in low level net code.
31 * 5 12/18/98 1:13a Dave
32 * Rough 1024x768 support for Direct3D. Proper detection and usage through
35 * 4 12/06/98 2:36p Dave
36 * Drastically improved nebula fogging.
38 * 3 11/11/98 5:37p Dave
39 * Checkin for multiplayer testing.
41 * 2 10/07/98 10:53a Dave
44 * 1 10/07/98 10:49a Dave
46 * 14 5/20/98 9:46p John
47 * added code so the places in code that change half the palette don't
48 * have to clear the screen.
50 * 13 5/06/98 5:30p John
51 * Removed unused cfilearchiver. Removed/replaced some unused/little used
52 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
53 * DirectX header files and libs that fixed the Direct3D alpha blending
56 * 12 4/14/98 12:15p John
57 * Made 16-bpp movies work.
59 * 11 3/12/98 5:36p John
60 * Took out any unused shaders. Made shader code take rgbc instead of
61 * matrix and vector since noone used it like a matrix and it would have
62 * been impossible to do in hardware. Made Glide implement a basic
63 * shader for online help.
65 * 10 3/10/98 4:18p John
66 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
67 * & Glide have popups and print screen. Took out all >8bpp software
68 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
69 * support Fred. Made zbuffering key off of functions rather than one
72 * 9 12/02/97 4:00p John
73 * Added first rev of thruster glow, along with variable levels of
74 * translucency, which retquired some restructing of palman.
76 * 8 10/03/97 9:10a John
77 * added better antialiased line drawer
79 * 7 9/23/97 10:45a John
80 * made so you can tell bitblt code to rle a bitmap by passing flag to
83 * 6 9/09/97 11:01a Sandeep
84 * fixed warning level 4 bugs
86 * 5 7/10/97 2:06p John
87 * added code to specify alphablending type for bitmaps.
89 * 4 6/17/97 7:04p John
90 * added d3d support for gradients.
91 * fixed some color bugs by adding screen signatures instead of watching
92 * flags and palette changes.
94 * 3 6/12/97 2:50a Lawrance
95 * bm_unlock() now passed bitmap number, not pointer
97 * 2 6/11/97 1:12p John
98 * Started fixing all the text colors in the game.
100 * 1 5/12/97 12:14p John
107 #include <windowsx.h>
113 #include "floating.h"
115 #include "grinternal.h"
116 #include "gropengl.h"
119 static int Inited = 0;
121 void gr_opengl_pixel(int x, int y)
123 if ( x < gr_screen.clip_left ) return;
124 if ( x > gr_screen.clip_right ) return;
125 if ( y < gr_screen.clip_top ) return;
126 if ( y > gr_screen.clip_bottom ) return;
129 void gr_opengl_clear()
134 void gr_opengl_flip()
138 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
142 void gr_opengl_set_clip(int x,int y,int w,int h)
144 // check for sanity of parameters
150 if (x >= gr_screen.max_w)
151 x = gr_screen.max_w - 1;
152 if (y >= gr_screen.max_h)
153 y = gr_screen.max_h - 1;
155 if (x + w > gr_screen.max_w)
156 w = gr_screen.max_w - x;
157 if (y + h > gr_screen.max_h)
158 h = gr_screen.max_h - y;
160 if (w > gr_screen.max_w)
162 if (h > gr_screen.max_h)
165 gr_screen.offset_x = x;
166 gr_screen.offset_y = y;
167 gr_screen.clip_left = 0;
168 gr_screen.clip_right = w-1;
169 gr_screen.clip_top = 0;
170 gr_screen.clip_bottom = h-1;
171 gr_screen.clip_width = w;
172 gr_screen.clip_height = h;
175 void gr_opengl_reset_clip()
177 gr_screen.offset_x = 0;
178 gr_screen.offset_y = 0;
179 gr_screen.clip_left = 0;
180 gr_screen.clip_top = 0;
181 gr_screen.clip_right = gr_screen.max_w - 1;
182 gr_screen.clip_bottom = gr_screen.max_h - 1;
183 gr_screen.clip_width = gr_screen.max_w;
184 gr_screen.clip_height = gr_screen.max_h;
187 void gr_opengl_set_font(int fontnum)
191 void gr_opengl_set_color( int r, int g, int b )
193 Assert((r >= 0) && (r < 256));
194 Assert((g >= 0) && (g < 256));
195 Assert((b >= 0) && (b < 256));
197 gr_screen.current_color.red = (unsigned char)r;
198 gr_screen.current_color.green = (unsigned char)g;
199 gr_screen.current_color.blue = (unsigned char)b;
202 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
204 gr_screen.current_alpha = alpha;
205 gr_screen.current_alphablend_mode = alphablend_mode;
206 gr_screen.current_bitblt_mode = bitblt_mode;
207 gr_screen.current_bitmap = bitmap_num;
209 gr_screen.current_bitmap_sx = sx;
210 gr_screen.current_bitmap_sy = sy;
213 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
215 shade->screen_sig = gr_screen.signature;
222 void gr_opengl_set_shader( shader * shade )
225 if (shade->screen_sig != gr_screen.signature) {
226 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
228 gr_screen.current_shader = *shade;
230 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
235 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
241 bmp = bm_lock( gr_screen.current_bitmap, 8, 0 );
242 sptr = (ubyte *)( bmp->data + (sy*bmp->w+sx) );
244 // mprintf(( "x=%d, y=%d, w=%d, h=%d\n", x, y, w, h ));
245 // mprintf(( "sx=%d, sy=%d, bw=%d, bh=%d\n", sx, sy, bmp->w, bmp->h ));
247 for (i=0; i<h; i++ ) {
248 for ( j=0; j<w; j++ ) {
249 gr_set_color( gr_palette[sptr[j]*3+0], gr_palette[sptr[j]*3+1], gr_palette[sptr[j]*3+2] );
250 gr_pixel( x+j, i+y );
254 bm_unlock(gr_screen.current_bitmap);
257 void gr_opengl_bitmap(int x, int y)
261 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
262 int dx1=x, dx2=x+w-1;
263 int dy1=y, dy2=y+h-1;
266 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
267 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
268 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
269 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
270 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
271 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
273 if ( sx < 0 ) return;
274 if ( sy < 0 ) return;
275 if ( sx >= w ) return;
276 if ( sy >= h ) return;
278 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
280 gr_bitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
283 static void opengl_scanline(int x1,int x2,int y)
287 void gr_opengl_rect(int x,int y,int w,int h)
322 opengl_scanline( x1, x2, y1+i );
326 void gr_opengl_shade(int x,int y,int w,int h)
330 void opengl_mtext(int x, int y, char *s, int len )
334 void gr_opengl_string(int x,int y,char * text)
341 p = strchr( p1, '\n' );
346 gr_get_string_size( &w, &h, p1 );
349 opengl_mtext(gr_screen.offset_x+(gr_screen.clip_width-w)/2,y+gr_screen.offset_y,p1,strlen(p1));
351 opengl_mtext(gr_screen.offset_x+x,y+gr_screen.offset_y,p1,strlen(p1));
354 if ( p1 && (strlen(p1) < 1) ) p1 = NULL;
362 void gr_opengl_circle( int xc, int yc, int d )
372 if ( (xc+r) < gr_screen.clip_left ) return;
373 if ( (xc-r) > gr_screen.clip_right ) return;
374 if ( (yc+r) < gr_screen.clip_top ) return;
375 if ( (yc-r) > gr_screen.clip_bottom ) return;
378 // Draw the first octant
379 opengl_scanline( xc-y, xc+y, yc-x );
380 opengl_scanline( xc-y, xc+y, yc+x );
385 // Draw the second octant
386 opengl_scanline( xc-x, xc+x, yc-y );
387 opengl_scanline( xc-x, xc+x, yc+y );
394 opengl_scanline( xc-x, xc+x, yc-y );
395 opengl_scanline( xc-x, xc+x, yc+y );
401 void gr_opengl_line(int x1,int y1,int x2,int y2)
408 int clipped = 0, swapped=0;
410 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);
428 for(i=dx+1;i>0;i--) {
440 for(i=dy+1;i>0;i--) {
454 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
456 void gr_opengl_scaler(vertex *va, vertex *vb )
458 float x0, y0, x1, y1;
459 float u0, v0, u1, v1;
460 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
461 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
462 float xmin, xmax, ymin, ymax;
463 int dx0, dy0, dx1, dy1;
465 //============= CLIP IT =====================
467 x0 = va->sx; y0 = va->sy;
468 x1 = vb->sx; y1 = vb->sy;
470 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
471 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
473 u0 = va->u; v0 = va->v;
474 u1 = vb->u; v1 = vb->v;
476 // Check for obviously offscreen bitmaps...
477 if ( (y1<=y0) || (x1<=x0) ) return;
478 if ( (x1<xmin ) || (x0>xmax) ) return;
479 if ( (y1<ymin ) || (y0>ymax) ) return;
481 clipped_u0 = u0; clipped_v0 = v0;
482 clipped_u1 = u1; clipped_v1 = v1;
484 clipped_x0 = x0; clipped_y0 = y0;
485 clipped_x1 = x1; clipped_y1 = y1;
487 // Clip the left, moving u0 right as necessary
489 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
493 // Clip the right, moving u1 left as necessary
495 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
499 // Clip the top, moving v0 down as necessary
501 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
505 // Clip the bottom, moving v1 up as necessary
507 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
511 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
512 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
514 if (dx1<=dx0) return;
515 if (dy1<=dy0) return;
517 //============= DRAW IT =====================
525 tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
526 if ( fl_abs(tmpu) < 0.001f ) {
527 return; // scaled up way too far!
529 tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
530 if ( fl_abs(tmpv) < 0.001f ) {
531 return; // scaled up way too far!
534 bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
536 du = fl2f(tmpu*(bp->w-1));
537 dv = fl2f(tmpv*(bp->h-1));
539 v = fl2f(clipped_v0*(bp->h-1));
540 u = fl2f(clipped_u0*(bp->w-1));
543 spixels = (ubyte *)bp->data;
545 for (y=dy0; y<=dy1; y++ ) {
546 sbits = &spixels[bp->rowsize*(v>>16)];
550 for (x=0; x<w; x++ ) {
551 ubyte c = sbits[ tmp_u >> 16 ];
553 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
554 gr_pixel( x+dx0, y );
561 bm_unlock(gr_screen.current_bitmap);
567 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
572 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
576 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
580 void gr_opengl_get_color( int * r, int * g, int * b )
582 if (r) *r = gr_screen.current_color.red;
583 if (g) *g = gr_screen.current_color.green;
584 if (b) *b = gr_screen.current_color.blue;
587 void gr_opengl_init_color(color *c, int r, int g, int b)
589 gr_screen.current_color.screen_sig = gr_screen.signature;
590 gr_screen.current_color.red = (unsigned char)r;
591 gr_screen.current_color.green = (unsigned char)g;
592 gr_screen.current_color.blue = (unsigned char)b;
595 void gr_opengl_set_color_fast(color *dst)
597 if ( dst->screen_sig != gr_screen.signature ) {
598 gr_init_color( dst, dst->red, dst->green, dst->blue );
601 gr_screen.current_color = *dst;
606 void gr_opengl_print_screen(char *filename)
611 int gr_opengl_supports_res_ingame(int res)
616 int gr_opengl_supports_res_interface(int res)
621 void gr_opengl_cleanup()
623 if ( !Inited ) return;
632 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
636 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
640 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
644 void gr_opengl_set_cull(int cull)
648 void gr_opengl_filter_set(int filter)
653 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
657 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)
662 void gr_opengl_set_clear_color(int r, int g, int b)
666 void gr_opengl_init()
673 mprintf(( "Initializing opengl graphics device...\n" ));
678 gr_screen.gf_flip = gr_opengl_flip;
679 gr_screen.gf_flip_window = gr_opengl_flip_window;
680 gr_screen.gf_set_clip = gr_opengl_set_clip;
681 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
682 gr_screen.gf_set_font = gr_opengl_set_font;
683 gr_screen.gf_set_color = gr_opengl_set_color;
684 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
685 gr_screen.gf_create_shader = gr_opengl_create_shader;
686 gr_screen.gf_set_shader = gr_opengl_set_shader;
687 gr_screen.gf_clear = gr_opengl_clear;
688 // gr_screen.gf_bitmap = gr_opengl_bitmap;
689 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
690 gr_screen.gf_rect = gr_opengl_rect;
691 gr_screen.gf_shade = gr_opengl_shade;
692 gr_screen.gf_string = gr_opengl_string;
693 gr_screen.gf_circle = gr_opengl_circle;
695 gr_screen.gf_line = gr_opengl_line;
696 gr_screen.gf_pixel = gr_opengl_pixel;
697 gr_screen.gf_scaler = gr_opengl_scaler;
698 gr_screen.gf_tmapper = gr_opengl_tmapper;
700 gr_screen.gf_gradient = gr_opengl_gradient;
702 gr_screen.gf_set_palette = gr_opengl_set_palette;
703 gr_screen.gf_get_color = gr_opengl_get_color;
704 gr_screen.gf_init_color = gr_opengl_init_color;
705 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
706 gr_screen.gf_print_screen = gr_opengl_print_screen;
708 gr_screen.gf_fog_set = gr_opengl_fog_set;
710 gr_screen.gf_get_region = gr_opengl_get_region;
712 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
714 gr_screen.gf_set_cull = gr_opengl_set_cull;
716 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
718 gr_screen.gf_filter_set = gr_opengl_filter_set;
720 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
722 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;