2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.6 2002/05/28 04:56:51 theoddone33
11 * runs a little bit now
13 * Revision 1.5 2002/05/28 04:07:28 theoddone33
14 * New graphics stubbing arrangement
16 * Revision 1.4 2002/05/27 23:39:34 relnev
19 * Revision 1.3 2002/05/27 22:35:01 theoddone33
22 * Revision 1.2 2002/05/27 22:32:02 theoddone33
23 * throw all d3d stuff at opengl
25 * Revision 1.1.1.1 2002/05/03 03:28:09 root
29 * 10 7/14/99 9:42a Dave
30 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
33 * 9 7/09/99 9:51a Dave
34 * Added thick polyline code.
36 * 8 6/29/99 10:35a Dave
37 * Interface polygon bitmaps! Whee!
39 * 7 2/03/99 11:44a Dave
40 * Fixed d3d transparent textures.
42 * 6 1/24/99 11:37p Dave
43 * First full rev of beam weapons. Very customizable. Removed some bogus
44 * Int3()'s in low level net code.
46 * 5 12/18/98 1:13a Dave
47 * Rough 1024x768 support for Direct3D. Proper detection and usage through
50 * 4 12/06/98 2:36p Dave
51 * Drastically improved nebula fogging.
53 * 3 11/11/98 5:37p Dave
54 * Checkin for multiplayer testing.
56 * 2 10/07/98 10:53a Dave
59 * 1 10/07/98 10:49a Dave
61 * 14 5/20/98 9:46p John
62 * added code so the places in code that change half the palette don't
63 * have to clear the screen.
65 * 13 5/06/98 5:30p John
66 * Removed unused cfilearchiver. Removed/replaced some unused/little used
67 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
68 * DirectX header files and libs that fixed the Direct3D alpha blending
71 * 12 4/14/98 12:15p John
72 * Made 16-bpp movies work.
74 * 11 3/12/98 5:36p John
75 * Took out any unused shaders. Made shader code take rgbc instead of
76 * matrix and vector since noone used it like a matrix and it would have
77 * been impossible to do in hardware. Made Glide implement a basic
78 * shader for online help.
80 * 10 3/10/98 4:18p John
81 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
82 * & Glide have popups and print screen. Took out all >8bpp software
83 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
84 * support Fred. Made zbuffering key off of functions rather than one
87 * 9 12/02/97 4:00p John
88 * Added first rev of thruster glow, along with variable levels of
89 * translucency, which retquired some restructing of palman.
91 * 8 10/03/97 9:10a John
92 * added better antialiased line drawer
94 * 7 9/23/97 10:45a John
95 * made so you can tell bitblt code to rle a bitmap by passing flag to
98 * 6 9/09/97 11:01a Sandeep
99 * fixed warning level 4 bugs
101 * 5 7/10/97 2:06p John
102 * added code to specify alphablending type for bitmaps.
104 * 4 6/17/97 7:04p John
105 * added d3d support for gradients.
106 * fixed some color bugs by adding screen signatures instead of watching
107 * flags and palette changes.
109 * 3 6/12/97 2:50a Lawrance
110 * bm_unlock() now passed bitmap number, not pointer
112 * 2 6/11/97 1:12p John
113 * Started fixing all the text colors in the game.
115 * 1 5/12/97 12:14p John
122 #include <windowsx.h>
128 #include "floating.h"
130 #include "grinternal.h"
131 #include "gropengl.h"
134 static int Inited = 0;
136 // Throw in some dummy functions - DDOI
138 int D3D_32bit = 0; // grd3d.cpp
139 int D3D_fog_mode = -1; // grd3d.cpp
140 int D3D_inited = 0; // grd3d.cpp
141 int D3D_zbias = 1; // grd3d.cpp
142 int D3d_rendition_uvs = 0; // grd3d.cpp
144 void gr_dd_activate(int active) // grdirectdraw.cpp
149 void gr_directdraw_cleanup() // grdirectdraw.cpp
154 void gr_directdraw_force_windowed() // grdirectdraw.cpp
159 void gr_directdraw_init()
164 void gr_opengl_preload_init()
169 void gr_d3d_preload(int x, int y)
174 void gr_opengl_activate(int b)
179 void d3d_start_frame()
184 void d3d_stop_frame()
194 void d3d_zbias (int a)
200 void gr_opengl_pixel(int x, int y)
202 if ( x < gr_screen.clip_left ) return;
203 if ( x > gr_screen.clip_right ) return;
204 if ( y < gr_screen.clip_top ) return;
205 if ( y > gr_screen.clip_bottom ) return;
208 void gr_opengl_clear()
213 void gr_opengl_flip()
217 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
221 void gr_opengl_set_clip(int x,int y,int w,int h)
223 // check for sanity of parameters
229 if (x >= gr_screen.max_w)
230 x = gr_screen.max_w - 1;
231 if (y >= gr_screen.max_h)
232 y = gr_screen.max_h - 1;
234 if (x + w > gr_screen.max_w)
235 w = gr_screen.max_w - x;
236 if (y + h > gr_screen.max_h)
237 h = gr_screen.max_h - y;
239 if (w > gr_screen.max_w)
241 if (h > gr_screen.max_h)
244 gr_screen.offset_x = x;
245 gr_screen.offset_y = y;
246 gr_screen.clip_left = 0;
247 gr_screen.clip_right = w-1;
248 gr_screen.clip_top = 0;
249 gr_screen.clip_bottom = h-1;
250 gr_screen.clip_width = w;
251 gr_screen.clip_height = h;
254 void gr_opengl_reset_clip()
256 gr_screen.offset_x = 0;
257 gr_screen.offset_y = 0;
258 gr_screen.clip_left = 0;
259 gr_screen.clip_top = 0;
260 gr_screen.clip_right = gr_screen.max_w - 1;
261 gr_screen.clip_bottom = gr_screen.max_h - 1;
262 gr_screen.clip_width = gr_screen.max_w;
263 gr_screen.clip_height = gr_screen.max_h;
266 void gr_opengl_set_font(int fontnum)
270 void gr_opengl_set_color( int r, int g, int b )
272 Assert((r >= 0) && (r < 256));
273 Assert((g >= 0) && (g < 256));
274 Assert((b >= 0) && (b < 256));
276 gr_screen.current_color.red = (unsigned char)r;
277 gr_screen.current_color.green = (unsigned char)g;
278 gr_screen.current_color.blue = (unsigned char)b;
281 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
283 gr_screen.current_alpha = alpha;
284 gr_screen.current_alphablend_mode = alphablend_mode;
285 gr_screen.current_bitblt_mode = bitblt_mode;
286 gr_screen.current_bitmap = bitmap_num;
288 gr_screen.current_bitmap_sx = sx;
289 gr_screen.current_bitmap_sy = sy;
292 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
294 shade->screen_sig = gr_screen.signature;
301 void gr_opengl_set_shader( shader * shade )
304 if (shade->screen_sig != gr_screen.signature) {
305 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
307 gr_screen.current_shader = *shade;
309 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
314 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
320 bmp = bm_lock( gr_screen.current_bitmap, 8, 0 );
321 sptr = (ubyte *)( bmp->data + (sy*bmp->w+sx) );
323 // mprintf(( "x=%d, y=%d, w=%d, h=%d\n", x, y, w, h ));
324 // mprintf(( "sx=%d, sy=%d, bw=%d, bh=%d\n", sx, sy, bmp->w, bmp->h ));
326 for (i=0; i<h; i++ ) {
327 for ( j=0; j<w; j++ ) {
328 gr_set_color( gr_palette[sptr[j]*3+0], gr_palette[sptr[j]*3+1], gr_palette[sptr[j]*3+2] );
329 gr_pixel( x+j, i+y );
333 bm_unlock(gr_screen.current_bitmap);
336 void gr_opengl_bitmap(int x, int y)
340 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
341 int dx1=x, dx2=x+w-1;
342 int dy1=y, dy2=y+h-1;
345 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
346 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
347 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
348 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
349 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
350 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
352 if ( sx < 0 ) return;
353 if ( sy < 0 ) return;
354 if ( sx >= w ) return;
355 if ( sy >= h ) return;
357 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
359 gr_bitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
362 static void opengl_scanline(int x1,int x2,int y)
366 void gr_opengl_rect(int x,int y,int w,int h)
401 opengl_scanline( x1, x2, y1+i );
405 void gr_opengl_shade(int x,int y,int w,int h)
409 void opengl_mtext(int x, int y, char *s, int len )
413 void gr_opengl_string(int x,int y,char * text)
420 p = strchr( p1, '\n' );
425 gr_get_string_size( &w, &h, p1 );
428 opengl_mtext(gr_screen.offset_x+(gr_screen.clip_width-w)/2,y+gr_screen.offset_y,p1,strlen(p1));
430 opengl_mtext(gr_screen.offset_x+x,y+gr_screen.offset_y,p1,strlen(p1));
433 if ( p1 && (strlen(p1) < 1) ) p1 = NULL;
441 void gr_opengl_circle( int xc, int yc, int d )
451 if ( (xc+r) < gr_screen.clip_left ) return;
452 if ( (xc-r) > gr_screen.clip_right ) return;
453 if ( (yc+r) < gr_screen.clip_top ) return;
454 if ( (yc-r) > gr_screen.clip_bottom ) return;
457 // Draw the first octant
458 opengl_scanline( xc-y, xc+y, yc-x );
459 opengl_scanline( xc-y, xc+y, yc+x );
464 // Draw the second octant
465 opengl_scanline( xc-x, xc+x, yc-y );
466 opengl_scanline( xc-x, xc+x, yc+y );
473 opengl_scanline( xc-x, xc+x, yc-y );
474 opengl_scanline( xc-x, xc+x, yc+y );
480 void gr_opengl_line(int x1,int y1,int x2,int y2)
487 int clipped = 0, swapped=0;
489 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);
507 for(i=dx+1;i>0;i--) {
519 for(i=dy+1;i>0;i--) {
533 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
535 void gr_opengl_scaler(vertex *va, vertex *vb )
537 float x0, y0, x1, y1;
538 float u0, v0, u1, v1;
539 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
540 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
541 float xmin, xmax, ymin, ymax;
542 int dx0, dy0, dx1, dy1;
544 //============= CLIP IT =====================
546 x0 = va->sx; y0 = va->sy;
547 x1 = vb->sx; y1 = vb->sy;
549 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
550 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
552 u0 = va->u; v0 = va->v;
553 u1 = vb->u; v1 = vb->v;
555 // Check for obviously offscreen bitmaps...
556 if ( (y1<=y0) || (x1<=x0) ) return;
557 if ( (x1<xmin ) || (x0>xmax) ) return;
558 if ( (y1<ymin ) || (y0>ymax) ) return;
560 clipped_u0 = u0; clipped_v0 = v0;
561 clipped_u1 = u1; clipped_v1 = v1;
563 clipped_x0 = x0; clipped_y0 = y0;
564 clipped_x1 = x1; clipped_y1 = y1;
566 // Clip the left, moving u0 right as necessary
568 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
572 // Clip the right, moving u1 left as necessary
574 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
578 // Clip the top, moving v0 down as necessary
580 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
584 // Clip the bottom, moving v1 up as necessary
586 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
590 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
591 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
593 if (dx1<=dx0) return;
594 if (dy1<=dy0) return;
596 //============= DRAW IT =====================
604 tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
605 if ( fl_abs(tmpu) < 0.001f ) {
606 return; // scaled up way too far!
608 tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
609 if ( fl_abs(tmpv) < 0.001f ) {
610 return; // scaled up way too far!
613 bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
615 du = fl2f(tmpu*(bp->w-1));
616 dv = fl2f(tmpv*(bp->h-1));
618 v = fl2f(clipped_v0*(bp->h-1));
619 u = fl2f(clipped_u0*(bp->w-1));
622 spixels = (ubyte *)bp->data;
624 for (y=dy0; y<=dy1; y++ ) {
625 sbits = &spixels[bp->rowsize*(v>>16)];
629 for (x=0; x<w; x++ ) {
630 ubyte c = sbits[ tmp_u >> 16 ];
632 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
633 gr_pixel( x+dx0, y );
640 bm_unlock(gr_screen.current_bitmap);
646 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
651 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
655 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
659 void gr_opengl_get_color( int * r, int * g, int * b )
661 if (r) *r = gr_screen.current_color.red;
662 if (g) *g = gr_screen.current_color.green;
663 if (b) *b = gr_screen.current_color.blue;
666 void gr_opengl_init_color(color *c, int r, int g, int b)
668 gr_screen.current_color.screen_sig = gr_screen.signature;
669 gr_screen.current_color.red = (unsigned char)r;
670 gr_screen.current_color.green = (unsigned char)g;
671 gr_screen.current_color.blue = (unsigned char)b;
674 void gr_opengl_set_color_fast(color *dst)
676 if ( dst->screen_sig != gr_screen.signature ) {
677 gr_init_color( dst, dst->red, dst->green, dst->blue );
680 gr_screen.current_color = *dst;
685 void gr_opengl_print_screen(char *filename)
690 int gr_opengl_supports_res_ingame(int res)
695 int gr_opengl_supports_res_interface(int res)
700 void gr_opengl_cleanup()
702 if ( !Inited ) return;
711 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
715 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
719 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
723 void gr_opengl_set_cull(int cull)
727 void gr_opengl_filter_set(int filter)
732 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
736 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)
741 void gr_opengl_set_clear_color(int r, int g, int b)
745 void gr_opengl_init()
752 mprintf(( "Initializing opengl graphics device...\n" ));
757 gr_screen.gf_flip = gr_opengl_flip;
758 gr_screen.gf_flip_window = gr_opengl_flip_window;
759 gr_screen.gf_set_clip = gr_opengl_set_clip;
760 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
761 gr_screen.gf_set_font = gr_opengl_set_font;
762 gr_screen.gf_set_color = gr_opengl_set_color;
763 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
764 gr_screen.gf_create_shader = gr_opengl_create_shader;
765 gr_screen.gf_set_shader = gr_opengl_set_shader;
766 gr_screen.gf_clear = gr_opengl_clear;
767 // gr_screen.gf_bitmap = gr_opengl_bitmap;
768 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
769 gr_screen.gf_rect = gr_opengl_rect;
770 gr_screen.gf_shade = gr_opengl_shade;
771 gr_screen.gf_string = gr_opengl_string;
772 gr_screen.gf_circle = gr_opengl_circle;
774 gr_screen.gf_line = gr_opengl_line;
775 gr_screen.gf_pixel = gr_opengl_pixel;
776 gr_screen.gf_scaler = gr_opengl_scaler;
777 gr_screen.gf_tmapper = gr_opengl_tmapper;
779 gr_screen.gf_gradient = gr_opengl_gradient;
781 gr_screen.gf_set_palette = gr_opengl_set_palette;
782 gr_screen.gf_get_color = gr_opengl_get_color;
783 gr_screen.gf_init_color = gr_opengl_init_color;
784 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
785 gr_screen.gf_print_screen = gr_opengl_print_screen;
787 gr_screen.gf_fog_set = gr_opengl_fog_set;
789 gr_screen.gf_get_region = gr_opengl_get_region;
791 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
793 gr_screen.gf_set_cull = gr_opengl_set_cull;
795 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
797 gr_screen.gf_filter_set = gr_opengl_filter_set;
799 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
801 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;