2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.18 2002/05/29 23:37:36 relnev
13 * Revision 1.17 2002/05/29 23:17:49 theoddone33
14 * Non working text code and fixed keys
16 * Revision 1.16 2002/05/29 19:45:13 theoddone33
17 * More changes on texture loading
19 * Revision 1.15 2002/05/29 19:06:48 theoddone33
20 * Enable string printing. Enable texture mapping
22 * Revision 1.14 2002/05/29 08:54:40 relnev
23 * "fixed" bitmap drawing.
25 * copied more d3d code over.
27 * Revision 1.13 2002/05/29 06:25:13 theoddone33
28 * Keyboard input, mouse tracking now work
30 * Revision 1.12 2002/05/29 04:52:45 relnev
33 * Revision 1.11 2002/05/29 04:29:56 relnev
34 * removed some unncessary stubbing, implemented opengl rect
36 * Revision 1.10 2002/05/29 04:13:27 theoddone33
39 * Revision 1.9 2002/05/29 03:35:51 relnev
42 * Revision 1.8 2002/05/29 03:30:05 relnev
45 * Revision 1.7 2002/05/29 02:52:32 theoddone33
46 * Enable OpenGL renderer
48 * Revision 1.6 2002/05/28 04:56:51 theoddone33
49 * runs a little bit now
51 * Revision 1.5 2002/05/28 04:07:28 theoddone33
52 * New graphics stubbing arrangement
54 * Revision 1.4 2002/05/27 23:39:34 relnev
57 * Revision 1.3 2002/05/27 22:35:01 theoddone33
60 * Revision 1.2 2002/05/27 22:32:02 theoddone33
61 * throw all d3d stuff at opengl
63 * Revision 1.1.1.1 2002/05/03 03:28:09 root
67 * 10 7/14/99 9:42a Dave
68 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
71 * 9 7/09/99 9:51a Dave
72 * Added thick polyline code.
74 * 8 6/29/99 10:35a Dave
75 * Interface polygon bitmaps! Whee!
77 * 7 2/03/99 11:44a Dave
78 * Fixed d3d transparent textures.
80 * 6 1/24/99 11:37p Dave
81 * First full rev of beam weapons. Very customizable. Removed some bogus
82 * Int3()'s in low level net code.
84 * 5 12/18/98 1:13a Dave
85 * Rough 1024x768 support for Direct3D. Proper detection and usage through
88 * 4 12/06/98 2:36p Dave
89 * Drastically improved nebula fogging.
91 * 3 11/11/98 5:37p Dave
92 * Checkin for multiplayer testing.
94 * 2 10/07/98 10:53a Dave
97 * 1 10/07/98 10:49a Dave
99 * 14 5/20/98 9:46p John
100 * added code so the places in code that change half the palette don't
101 * have to clear the screen.
103 * 13 5/06/98 5:30p John
104 * Removed unused cfilearchiver. Removed/replaced some unused/little used
105 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
106 * DirectX header files and libs that fixed the Direct3D alpha blending
109 * 12 4/14/98 12:15p John
110 * Made 16-bpp movies work.
112 * 11 3/12/98 5:36p John
113 * Took out any unused shaders. Made shader code take rgbc instead of
114 * matrix and vector since noone used it like a matrix and it would have
115 * been impossible to do in hardware. Made Glide implement a basic
116 * shader for online help.
118 * 10 3/10/98 4:18p John
119 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
120 * & Glide have popups and print screen. Took out all >8bpp software
121 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
122 * support Fred. Made zbuffering key off of functions rather than one
125 * 9 12/02/97 4:00p John
126 * Added first rev of thruster glow, along with variable levels of
127 * translucency, which retquired some restructing of palman.
129 * 8 10/03/97 9:10a John
130 * added better antialiased line drawer
132 * 7 9/23/97 10:45a John
133 * made so you can tell bitblt code to rle a bitmap by passing flag to
136 * 6 9/09/97 11:01a Sandeep
137 * fixed warning level 4 bugs
139 * 5 7/10/97 2:06p John
140 * added code to specify alphablending type for bitmaps.
142 * 4 6/17/97 7:04p John
143 * added d3d support for gradients.
144 * fixed some color bugs by adding screen signatures instead of watching
145 * flags and palette changes.
147 * 3 6/12/97 2:50a Lawrance
148 * bm_unlock() now passed bitmap number, not pointer
150 * 2 6/11/97 1:12p John
151 * Started fixing all the text colors in the game.
153 * 1 5/12/97 12:14p John
160 #include <windowsx.h>
167 #include "floating.h"
169 #include "grinternal.h"
170 #include "gropengl.h"
173 static int Inited = 0;
175 static GLuint bitmapTex;
176 static GLubyte *bitmapMem;
179 // Throw in some dummy functions - DDOI
181 int D3D_32bit = 0; // grd3d.cpp
182 int D3D_fog_mode = -1; // grd3d.cpp
183 int D3D_inited = 0; // grd3d.cpp
184 int D3D_zbias = 1; // grd3d.cpp
185 int D3d_rendition_uvs = 0; // grd3d.cpp
187 void gr_dd_activate(int active) // grdirectdraw.cpp
192 void gr_directdraw_cleanup() // grdirectdraw.cpp
197 void gr_directdraw_force_windowed() // grdirectdraw.cpp
202 void gr_directdraw_init()
208 void gr_d3d_preload(int x, int y)
213 void d3d_start_frame()
218 void d3d_stop_frame()
228 void d3d_zbias (int a)
234 void gr_opengl_activate(int b)
239 void gr_opengl_preload_init()
244 void gr_opengl_pixel(int x, int y)
246 if ( x < gr_screen.clip_left ) return;
247 if ( x > gr_screen.clip_right ) return;
248 if ( y < gr_screen.clip_top ) return;
249 if ( y > gr_screen.clip_bottom ) return;
252 void gr_opengl_clear()
254 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
257 void opengl_tcache_frame ();
258 void gr_opengl_flip()
262 SDL_GL_SwapBuffers ();
264 opengl_tcache_frame ();
267 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
272 void gr_opengl_set_clip(int x,int y,int w,int h)
274 // check for sanity of parameters
280 if (x >= gr_screen.max_w)
281 x = gr_screen.max_w - 1;
282 if (y >= gr_screen.max_h)
283 y = gr_screen.max_h - 1;
285 if (x + w > gr_screen.max_w)
286 w = gr_screen.max_w - x;
287 if (y + h > gr_screen.max_h)
288 h = gr_screen.max_h - y;
290 if (w > gr_screen.max_w)
292 if (h > gr_screen.max_h)
295 gr_screen.offset_x = x;
296 gr_screen.offset_y = y;
297 gr_screen.clip_left = 0;
298 gr_screen.clip_right = w-1;
299 gr_screen.clip_top = 0;
300 gr_screen.clip_bottom = h-1;
301 gr_screen.clip_width = w;
302 gr_screen.clip_height = h;
307 void gr_opengl_reset_clip()
309 gr_screen.offset_x = 0;
310 gr_screen.offset_y = 0;
311 gr_screen.clip_left = 0;
312 gr_screen.clip_top = 0;
313 gr_screen.clip_right = gr_screen.max_w - 1;
314 gr_screen.clip_bottom = gr_screen.max_h - 1;
315 gr_screen.clip_width = gr_screen.max_w;
316 gr_screen.clip_height = gr_screen.max_h;
319 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
321 gr_screen.current_alpha = alpha;
322 gr_screen.current_alphablend_mode = alphablend_mode;
323 gr_screen.current_bitblt_mode = bitblt_mode;
324 gr_screen.current_bitmap = bitmap_num;
326 gr_screen.current_bitmap_sx = sx;
327 gr_screen.current_bitmap_sy = sy;
330 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
332 shade->screen_sig = gr_screen.signature;
339 void gr_opengl_set_shader( shader * shade )
342 if (shade->screen_sig != gr_screen.signature) {
343 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
345 gr_screen.current_shader = *shade;
347 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
351 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
355 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
359 GLubyte *sptr, *dptr;
363 int cw = min(bmp->w, w);
364 int ch = min(bmp->h, h);
366 glColor4f(1.0, 1.0, 1.0, 1.0);
367 glBindTexture(GL_TEXTURE_2D, bitmapTex);
370 for (iy = sy; iy < ch; iy += 256) {
372 ih = min(256, (ch-iy));
374 for (ix = sx; ix < cw; ix += 256) {
376 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
378 iw = min(256, (cw-ix));
384 memcpy(dptr, sptr, iw*2);
392 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
402 t = (float)ih / 256.0;
407 s = (float)iw / 256.0;
408 t = (float)ih / 256.0;
413 s = (float)iw / 256.0;
425 bm_unlock(gr_screen.current_bitmap);
429 void gr_d3d_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
436 int dx1=x, dx2=x+w-1;
437 int dy1=y, dy2=y+h-1;
440 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
445 if ( count > 1 ) Int3();
449 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
450 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
451 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
452 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
453 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
454 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
481 if ( w < 1 ) return; // clipped away!
482 if ( h < 1 ) return; // clipped away!
486 // Make sure clipping algorithm works
490 Assert( w == (dx2-dx1+1) );
491 Assert( h == (dy2-dy1+1) );
494 Assert( sx+w <= bw );
495 Assert( sy+h <= bh );
496 Assert( dx2 >= dx1 );
497 Assert( dy2 >= dy1 );
498 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
499 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
500 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
501 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
504 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
505 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
507 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
510 void gr_opengl_bitmap(int x, int y)
514 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
515 int dx1=x, dx2=x+w-1;
516 int dy1=y, dy2=y+h-1;
519 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
520 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
521 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
522 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
523 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
524 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
526 if ( sx < 0 ) return;
527 if ( sy < 0 ) return;
528 if ( sx >= w ) return;
529 if ( sy >= h ) return;
531 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
533 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
536 static void opengl_scanline(int x1,int x2,int y)
541 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
545 saved_zbuf = gr_zbuffer_get();
546 gr_zbuffer_set(GR_ZBUFF_NONE);
549 glColor4ub(r, g, b, a);
558 glVertex2f(x+w, y+h);
564 gr_zbuffer_set(saved_zbuf);
568 void gr_opengl_rect(int x,int y,int w,int h)
570 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);
573 void gr_opengl_shade(int x,int y,int w,int h)
580 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
581 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
582 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
583 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
584 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
585 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
586 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
587 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
589 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
592 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
597 if ( !gr_screen.current_color.is_alphacolor ) return;
599 float u_scale, v_scale;
601 // gr_d3d_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
603 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
604 // Couldn't set texture
605 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
609 // LPD3DTLVERTEX src_v;
610 // D3DTLVERTEX d3d_verts[4];
612 float u0, u1, v0, v1;
613 float x1, x2, y1, y2;
616 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
619 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
621 u0 = u_scale*i2fl(sx)/i2fl(bw);
622 v0 = v_scale*i2fl(sy)/i2fl(bh);
624 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
625 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
627 x1 = i2fl(x+gr_screen.offset_x);
628 y1 = i2fl(y+gr_screen.offset_y);
629 x2 = i2fl(x+w+gr_screen.offset_x);
630 y2 = i2fl(y+h+gr_screen.offset_y);
632 if ( gr_screen.current_color.is_alphacolor ) {
633 //glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
635 //glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
639 glTexCoord2f (u0, v0);
640 glVertex3f (x1, y1, 0.99);
642 glTexCoord2f (u1, v0);
643 glVertex3f (x2, y1, 0.99);
645 glTexCoord2f (u1, v1);
646 glVertex3f (x2, y2, 0.99);
648 glTexCoord2f (u0, v1);
649 glVertex3f (x1, y2, 0.99);
653 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
660 int dx1=x, dx2=x+w-1;
661 int dy1=y, dy2=y+h-1;
664 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
669 if ( count > 1 ) Int3();
673 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
674 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
675 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
676 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
677 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
678 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
705 if ( w < 1 ) return; // clipped away!
706 if ( h < 1 ) return; // clipped away!
710 // Make sure clipping algorithm works
714 Assert( w == (dx2-dx1+1) );
715 Assert( h == (dy2-dy1+1) );
718 Assert( sx+w <= bw );
719 Assert( sy+h <= bh );
720 Assert( dx2 >= dx1 );
721 Assert( dy2 >= dy1 );
722 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
723 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
724 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
725 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
728 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
729 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
732 void gr_opengl_aabitmap(int x, int y)
736 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
737 int dx1=x, dx2=x+w-1;
738 int dy1=y, dy2=y+h-1;
741 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
742 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
743 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
744 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
745 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
746 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
748 if ( sx < 0 ) return;
749 if ( sy < 0 ) return;
750 if ( sx >= w ) return;
751 if ( sy >= h ) return;
753 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
754 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
758 void gr_opengl_string( int sx, int sy, char *s )
760 int width, spacing, letter;
763 if ( !Current_font ) {
767 gr_set_bitmap(Current_font->bitmap_id);
772 if (sx==0x8000) { //centered
773 x = get_centered_x(s);
785 y += Current_font->h;
786 if (sx==0x8000) { //centered
787 x = get_centered_x(s);
794 letter = get_char_width(s[0],s[1],&width,&spacing);
797 //not in font, draw as space
805 // Check if this character is totally clipped
806 if ( x + width < gr_screen.clip_left ) continue;
807 if ( y + Current_font->h < gr_screen.clip_top ) continue;
808 if ( x > gr_screen.clip_right ) continue;
809 if ( y > gr_screen.clip_bottom ) continue;
812 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
813 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
817 wc = width - xd; hc = Current_font->h - yd;
818 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
819 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
821 if ( wc < 1 ) continue;
822 if ( hc < 1 ) continue;
826 ch = &Current_font->char_data[letter];
828 int u = Current_font->bm_u[letter];
829 int v = Current_font->bm_v[letter];
831 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
835 void gr_opengl_circle( int xc, int yc, int d )
845 if ( (xc+r) < gr_screen.clip_left ) return;
846 if ( (xc-r) > gr_screen.clip_right ) return;
847 if ( (yc+r) < gr_screen.clip_top ) return;
848 if ( (yc-r) > gr_screen.clip_bottom ) return;
851 // Draw the first octant
852 opengl_scanline( xc-y, xc+y, yc-x );
853 opengl_scanline( xc-y, xc+y, yc+x );
858 // Draw the second octant
859 opengl_scanline( xc-x, xc+x, yc-y );
860 opengl_scanline( xc-x, xc+x, yc+y );
867 opengl_scanline( xc-x, xc+x, yc-y );
868 opengl_scanline( xc-x, xc+x, yc+y );
874 void gr_opengl_line(int x1,int y1,int x2,int y2)
876 int clipped = 0, swapped=0;
878 glDisable ( GL_DEPTH_TEST );
879 glEnable ( GL_BLEND );
880 glBlendFunc ( GL_SRC_ALPHA, GL_DST_ALPHA );
882 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);
885 glColor4f (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
886 glVertex3f (i2fl (x1+gr_screen.offset_x),i2fl (y1+gr_screen.offset_y), 0.99f);
887 glVertex3f (i2fl (x2+gr_screen.offset_x),i2fl (y2+gr_screen.offset_y), 0.99f);
891 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
893 void gr_opengl_scaler(vertex *va, vertex *vb )
895 float x0, y0, x1, y1;
896 float u0, v0, u1, v1;
897 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
898 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
899 float xmin, xmax, ymin, ymax;
900 int dx0, dy0, dx1, dy1;
902 //============= CLIP IT =====================
904 x0 = va->sx; y0 = va->sy;
905 x1 = vb->sx; y1 = vb->sy;
907 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
908 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
910 u0 = va->u; v0 = va->v;
911 u1 = vb->u; v1 = vb->v;
913 // Check for obviously offscreen bitmaps...
914 if ( (y1<=y0) || (x1<=x0) ) return;
915 if ( (x1<xmin ) || (x0>xmax) ) return;
916 if ( (y1<ymin ) || (y0>ymax) ) return;
918 clipped_u0 = u0; clipped_v0 = v0;
919 clipped_u1 = u1; clipped_v1 = v1;
921 clipped_x0 = x0; clipped_y0 = y0;
922 clipped_x1 = x1; clipped_y1 = y1;
924 // Clip the left, moving u0 right as necessary
926 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
930 // Clip the right, moving u1 left as necessary
932 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
936 // Clip the top, moving v0 down as necessary
938 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
942 // Clip the bottom, moving v1 up as necessary
944 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
948 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
949 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
951 if (dx1<=dx0) return;
952 if (dy1<=dy0) return;
954 //============= DRAW IT =====================
962 tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
963 if ( fl_abs(tmpu) < 0.001f ) {
964 return; // scaled up way too far!
966 tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
967 if ( fl_abs(tmpv) < 0.001f ) {
968 return; // scaled up way too far!
971 bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
973 du = fl2f(tmpu*(bp->w-1));
974 dv = fl2f(tmpv*(bp->h-1));
976 v = fl2f(clipped_v0*(bp->h-1));
977 u = fl2f(clipped_u0*(bp->w-1));
980 spixels = (ubyte *)bp->data;
982 for (y=dy0; y<=dy1; y++ ) {
983 sbits = &spixels[bp->rowsize*(v>>16)];
987 for (x=0; x<w; x++ ) {
988 ubyte c = sbits[ tmp_u >> 16 ];
990 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
991 gr_pixel( x+dx0, y );
998 bm_unlock(gr_screen.current_bitmap);
1003 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
1006 float u_scale = 1.0f, v_scale = 1.0f;
1009 // Make nebula use the texture mapper... this blends the colors better.
1010 if ( flags & TMAP_FLAG_NEBULA ){
1015 gr_texture_source texture_source = (gr_texture_source)-1;
1016 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1017 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1019 if ( gr_zbuffering ) {
1020 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1021 zbuffer_type = ZBUFFER_TYPE_READ;
1023 zbuffer_type = ZBUFFER_TYPE_FULL;
1026 zbuffer_type = ZBUFFER_TYPE_NONE;
1032 int tmap_type = TCACHE_TYPE_NORMAL;
1036 if ( flags & TMAP_FLAG_TEXTURED ) {
1039 r = gr_screen.current_color.red;
1040 g = gr_screen.current_color.green;
1041 b = gr_screen.current_color.blue;
1044 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1046 // Some blend function stuff here - DDOI
1049 float factor = gr_screen.current_alpha;
1053 if ( factor <= 1.0f ) {
1054 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1055 r = (r*tmp_alpha)/255;
1056 g = (g*tmp_alpha)/255;
1057 b = (b*tmp_alpha)/255;
1064 if ( flags & TMAP_FLAG_TEXTURED ) {
1065 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 ))
1067 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1071 // use nonfiltered textures for bitmap sections
1073 if(flags & TMAP_FLAG_BITMAP_SECTION){
1074 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1076 texture_source = TEXTURE_SOURCE_DECAL;
1082 x1 = gr_screen.clip_left*16;
1083 x2 = gr_screen.clip_right*16+15;
1084 y1 = gr_screen.clip_top*16;
1085 y2 = gr_screen.clip_bottom*16+15;
1087 float uoffset = 0.0f;
1088 float voffset = 0.0f;
1090 float minu=0.0f, minv=0.0f, maxu=1.0f, maxv=1.0f;
1092 if ( flags & TMAP_FLAG_TEXTURED )
1099 STUB_FUNCTION; // DDOI - this still needs work
1100 glBegin (GL_TRIANGLE_FAN);
1103 if (flags & TMAP_FLAG_ALPHA) a = verts[i]->a;
1106 if (flags & TMAP_FLAG_NEBULA ) {
1108 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1109 r = gr_palette[pal*3+0];
1110 g = gr_palette[pal*3+1];
1111 b = gr_palette[pal*3+2];
1113 }else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1114 r = Gr_gamma_lookup[verts[i]->b];
1115 g = Gr_gamma_lookup[verts[i]->b];
1116 b = Gr_gamma_lookup[verts[i]->b];
1117 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1118 // Make 0.75 be 256.0f
1119 r = Gr_gamma_lookup[verts[i]->r];
1120 g = Gr_gamma_lookup[verts[i]->g];
1121 b = Gr_gamma_lookup[verts[i]->b];
1123 // use constant RGB values...
1126 glColor4i (r,g,b,a);
1128 // DDOI - FIXME fog stuff
1129 // DDOI - FIXME TexCoord stuff
1130 glVertex3f (verts[i]->sx+gr_screen.offset_x,
1131 verts[i]->sy+gr_screen.offset_y,
1137 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1142 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1147 void gr_opengl_get_color( int * r, int * g, int * b )
1149 if (r) *r = gr_screen.current_color.red;
1150 if (g) *g = gr_screen.current_color.green;
1151 if (b) *b = gr_screen.current_color.blue;
1154 void gr_opengl_init_color(color *c, int r, int g, int b)
1156 c->screen_sig = gr_screen.signature;
1157 c->red = (unsigned char)r;
1158 c->green = (unsigned char)g;
1159 c->blue = (unsigned char)b;
1161 c->ac_type = AC_TYPE_NONE;
1163 c->is_alphacolor = 0;
1167 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1169 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1170 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1171 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1172 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1174 gr_opengl_init_color( clr, r, g, b );
1176 clr->alpha = (unsigned char)alpha;
1177 clr->ac_type = (ubyte)type;
1178 clr->alphacolor = -1;
1179 clr->is_alphacolor = 1;
1182 void gr_opengl_set_color( int r, int g, int b )
1184 Assert((r >= 0) && (r < 256));
1185 Assert((g >= 0) && (g < 256));
1186 Assert((b >= 0) && (b < 256));
1188 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1191 void gr_opengl_set_color_fast(color *dst)
1193 if ( dst->screen_sig != gr_screen.signature ) {
1194 if ( dst->is_alphacolor ) {
1195 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1197 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1200 gr_screen.current_color = *dst;
1203 void gr_opengl_print_screen(char *filename)
1208 int gr_opengl_supports_res_ingame(int res)
1215 int gr_opengl_supports_res_interface(int res)
1222 void opengl_tcache_cleanup ();
1223 void gr_opengl_cleanup()
1225 if ( !Inited ) return;
1232 opengl_tcache_cleanup ();
1237 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
1242 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1247 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
1252 void gr_opengl_set_cull(int cull)
1257 void gr_opengl_filter_set(int filter)
1263 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1269 typedef struct tcache_slot_opengl {
1270 GLuint texture_handle;
1271 float u_scale, v_scale;
1274 char used_this_frame;
1279 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1280 tcache_slot_opengl *parent;
1281 } tcache_slot_opengl;
1283 static void *Texture_sections = NULL;
1284 tcache_slot_opengl *Textures = NULL;
1286 int GL_texture_sections = 0;
1287 int GL_texture_ram = 0;
1288 int GL_frame_count = 0;
1289 int GL_min_texture_width = 0;
1290 int GL_max_texture_width = 0;
1291 int GL_min_texture_height = 0;
1292 int GL_max_texture_height = 0;
1293 int GL_square_textures = 0;
1294 int GL_pow2_textures = 0;
1295 int GL_textures_in = 0;
1296 int GL_textures_in_frame = 0;
1297 int GL_last_bitmap_id = -1;
1298 int GL_last_detail = -1;
1299 int GL_last_bitmap_type = -1;
1300 int GL_last_section_x = -1;
1301 int GL_last_section_y = -1;
1305 void opengl_tcache_init (int use_sections)
1309 // DDOI - FIXME skipped a lot of stuff here
1312 GL_min_texture_width = 16;
1313 GL_min_texture_height = 16;
1314 GL_max_texture_width = 256;
1315 GL_max_texture_height = 256;
1317 GL_square_textures = 1;
1319 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1325 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1326 if(!Texture_sections){
1329 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1332 // Init the texture structures
1333 int section_count = 0;
1334 for( i=0; i<MAX_BITMAPS; i++ ) {
1336 Textures[i].vram_texture = NULL;
1337 Textures[i].vram_texture_surface = NULL;
1339 Textures[i].texture_handle = 0;
1341 Textures[i].bitmap_id = -1;
1342 Textures[i].size = 0;
1343 Textures[i].used_this_frame = 0;
1345 Textures[i].parent = NULL;
1347 // allocate sections
1349 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1350 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1351 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1352 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1354 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1355 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1357 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1358 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1359 Textures[i].data_sections[idx][s_idx]->size = 0;
1360 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1364 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1365 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1366 Textures[i].data_sections[idx][s_idx] = NULL;
1372 GL_texture_sections = use_sections;
1374 //GL_last_detail = Detail.hardware_textures;
1375 GL_last_bitmap_id = -1;
1376 GL_last_bitmap_type = -1;
1378 GL_last_section_x = -1;
1379 GL_last_section_y = -1;
1382 GL_textures_in_frame = 0;
1385 int opengl_free_texture (tcache_slot_opengl *t);
1387 void opengl_tcache_flush ()
1391 for( i=0; i<MAX_BITMAPS; i++ ) {
1392 opengl_free_texture ( &Textures[i] );
1394 if (GL_textures_in != 0) {
1395 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1399 GL_last_bitmap_id = -1;
1400 GL_last_section_x = -1;
1401 GL_last_section_y = -1;
1404 void opengl_tcache_cleanup ()
1406 opengl_tcache_flush ();
1409 GL_textures_in_frame = 0;
1416 if( Texture_sections != NULL ){
1417 free(Texture_sections);
1418 Texture_sections = NULL;
1422 void opengl_tcache_frame ()
1426 GL_last_bitmap_id = -1;
1427 GL_textures_in_frame = 0;
1432 for( i=0; i<MAX_BITMAPS; i++ ) {
1433 Textures[i].used_this_frame = 0;
1436 if(Textures[i].data_sections[0][0] != NULL){
1437 Assert(GL_texture_sections);
1438 if(GL_texture_sections){
1439 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1440 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1441 if(Textures[i].data_sections[idx][s_idx] != NULL){
1442 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1451 opengl_tcache_flush();
1456 int opengl_free_texture ( tcache_slot_opengl *t )
1462 if ( t->bitmap_id > -1 ) {
1463 // if I, or any of my children have been used this frame, bail
1464 if(t->used_this_frame){
1467 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1468 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1469 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
1475 // ok, now we know its legal to free everything safely
1476 glDeleteTextures (1, &t->texture_handle);
1477 t->texture_handle = 0;
1479 if ( GL_last_bitmap_id == t->bitmap_id ) {
1480 GL_last_bitmap_id = -1;
1483 // if this guy has children, free them too, since the children
1484 // actually make up his size
1485 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1486 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1487 if(t->data_sections[idx][s_idx] != NULL){
1488 opengl_free_texture(t->data_sections[idx][s_idx]);
1494 t->used_this_frame = 0;
1495 GL_textures_in -= t->size;
1501 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1506 if((w_out == NULL) || (h_out == NULL)){
1516 if ( D3D_pow2_textures ) {
1518 for (i=0; i<16; i++ ) {
1519 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
1525 for (i=0; i<16; i++ ) {
1526 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
1534 if ( tex_w < GL_min_texture_width ) {
1535 tex_w = GL_min_texture_width;
1536 } else if ( tex_w > GL_max_texture_width ) {
1537 tex_w = GL_max_texture_width;
1540 if ( tex_h < GL_min_texture_height ) {
1541 tex_h = GL_min_texture_height;
1542 } else if ( tex_h > GL_max_texture_height ) {
1543 tex_h = GL_max_texture_height;
1546 if ( GL_square_textures ) {
1548 // Make the both be equal to larger of the two
1549 new_size = max(tex_w, tex_h);
1554 // store the outgoing size
1559 // data == start of bitmap data
1560 // sx == x offset into bitmap
1561 // sy == y offset into bitmap
1562 // src_w == absolute width of section on source bitmap
1563 // src_h == absolute height of section on source bitmap
1564 // bmap_w == width of source bitmap
1565 // bmap_h == height of source bitmap
1566 // tex_w == width of final texture
1567 // tex_h == height of final texture
1568 int opengl_create_texture_sub(int bitmap_type, int texture_handle, ushort *data, int sx, int sy, int src_w, int src_h, int bmap_w, int bmap_h, int tex_w, int tex_h, tcache_slot_opengl *t, int reload, int fail_on_full)
1577 if ( t->used_this_frame ) {
1578 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
1583 if(!opengl_free_texture(t)){
1588 // get final texture size
1589 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
1591 if ( (tex_w < 1) || (tex_h < 1) ) {
1592 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
1596 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
1597 t->u_scale = (float)bmap_w / (float)tex_w;
1598 t->v_scale = (float)bmap_h / (float)tex_h;
1599 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1600 t->u_scale = (float)src_w / (float)tex_w;
1601 t->v_scale = (float)src_h / (float)tex_h;
1607 glGenTextures (1, &t->texture_handle);
1608 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
1610 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1611 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1612 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1613 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1615 switch (bitmap_type) {
1617 case TCACHE_TYPE_AABITMAP:
1620 ubyte *bmp_data = ((ubyte*)data);
1621 ushort *texmem = (ushort *) malloc (tex_w*tex_h*2);
1623 for (i=0;i<tex_h;i++)
1625 for (j=0;j<tex_w;j++)
1627 if (i < bmap_h && j < bmap_w)
1628 *texmem++ = ((bmp_data[i*bmap_w+j]<<8)|0xff);
1634 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
1640 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGBA,
1641 GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
1645 t->bitmap_id = texture_handle;
1646 t->time_created = GL_frame_count;
1647 t->used_this_frame = 0;
1648 t->size = tex_w * tex_h * 2;
1649 t->w = (ushort)tex_w;
1650 t->h = (ushort)tex_h;
1651 GL_textures_in_frame += t->size;
1653 GL_textures_in += t->size;
1661 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
1665 int final_w, final_h;
1669 // setup texture/bitmap flags
1671 switch(bitmap_type){
1672 case TCACHE_TYPE_AABITMAP:
1673 flags |= BMP_AABITMAP;
1676 case TCACHE_TYPE_NORMAL:
1677 flags |= BMP_TEX_OTHER;
1678 case TCACHE_TYPE_XPARENT:
1679 flags |= BMP_TEX_XPARENT;
1681 case TCACHE_TYPE_NONDARKENING:
1683 flags |= BMP_TEX_NONDARK;
1687 // lock the bitmap into the proper format
1688 bmp = bm_lock(bitmap_handle, bpp, flags);
1689 if ( bmp == NULL ) {
1690 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1699 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
1700 max_w /= D3D_texture_divider;
1701 max_h /= D3D_texture_divider;
1703 // Detail.debris_culling goes from 0 to 4.
1704 max_w /= 16 >> Detail.hardware_textures;
1705 max_h /= 16 >> Detail.hardware_textures;
1709 // get final texture size as it will be allocated as a DD surface
1710 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
1712 // if this tcache slot has no bitmap
1713 if ( tslot->bitmap_id < 0) {
1716 // different bitmap altogether - determine if the new one can use the old one's slot
1717 else if (tslot->bitmap_id != bitmap_handle) {
1718 if((final_w == tslot->w) && (final_h == tslot->h)){
1720 //ml_printf("Reloading texture %d\n", bitmap_handle);
1727 int ret_val = opengl_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, 0, 0, bmp->w, bmp->h, bmp->w, bmp->h, max_w, max_h, tslot, reload, fail_on_full);
1729 // unlock the bitmap
1730 bm_unlock(bitmap_handle);
1735 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
1739 int final_w, final_h;
1740 int section_x, section_y;
1743 // setup texture/bitmap flags
1744 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
1745 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
1746 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
1748 flags = BMP_TEX_XPARENT;
1750 // lock the bitmap in the proper format
1751 bmp = bm_lock(bitmap_handle, 16, flags);
1752 if ( bmp == NULL ) {
1753 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1756 // determine the width and height of this section
1757 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
1759 // get final texture size as it will be allocated as a DD surface
1760 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
1762 // if this tcache slot has no bitmap
1763 if ( tslot->bitmap_id < 0) {
1766 // different bitmap altogether - determine if the new one can use the old one's slot
1767 else if (tslot->bitmap_id != bitmap_handle) {
1768 if((final_w == tslot->w) && (final_h == tslot->h)){
1776 int ret_val = opengl_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, bmp->sections.sx[sx], bmp->sections.sy[sy], section_x, section_y, bmp->w, bmp->h, section_x, section_y, tslot, reload, fail_on_full);
1778 // unlock the bitmap
1779 bm_unlock(bitmap_handle);
1785 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
1786 int gr_opengl_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int fail_on_full = 0, int sx = -1, int sy = -1, int force = 0)
1795 GL_last_bitmap_id = -1;
1800 if ( GL_last_detail != Detail.hardware_textures ) {
1801 GL_last_detail = Detail.hardware_textures;
1802 opengl_tcache_flush();
1810 int n = bm_get_cache_slot (bitmap_id, 1);
1811 tcache_slot_opengl *t = &Textures[n];
1813 if ( (GL_last_bitmap_id == bitmap_id) && (GL_last_bitmap_type==bitmap_type) && (t->bitmap_id == bitmap_id) && (GL_last_section_x == sx) && (GL_last_section_y == sy)) {
1814 t->used_this_frame++;
1816 // mark all children as used
1817 if(GL_texture_sections){
1818 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1819 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1820 if(t->data_sections[idx][s_idx] != NULL){
1821 t->data_sections[idx][s_idx]->used_this_frame++;
1827 *u_scale = t->u_scale;
1828 *v_scale = t->v_scale;
1832 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1833 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
1834 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
1840 // if the texture sections haven't been created yet
1841 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
1843 // lock the bitmap in the proper format
1844 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
1845 bm_unlock(bitmap_id);
1847 // now lets do something for each texture
1849 for(idx=0; idx<bmp->sections.num_x; idx++){
1850 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
1851 // hmm. i'd rather we didn't have to do it this way...
1852 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
1856 // not used this frame
1857 t->data_sections[idx][s_idx]->used_this_frame = 0;
1861 // zero out pretty much everything in the parent struct since he's just the root
1862 t->bitmap_id = bitmap_id;
1863 t->texture_handle = 0;
1864 t->time_created = t->data_sections[sx][sy]->time_created;
1865 t->used_this_frame = 0;
1867 t->vram_texture = NULL;
1868 t->vram_texture_surface = NULL
1872 // argh. we failed to upload. free anything we can
1874 opengl_free_texture(t);
1876 // swap in the texture we want
1878 t = t->data_sections[sx][sy];
1881 // all other "normal" textures
1882 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
1883 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
1886 // everything went ok
1887 if(ret_val && (t->texture_handle) && !vram_full){
1888 *u_scale = t->u_scale;
1889 *v_scale = t->v_scale;
1891 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
1893 GL_last_bitmap_id = t->bitmap_id;
1894 GL_last_bitmap_type = bitmap_type;
1895 GL_last_section_x = sx;
1896 GL_last_section_y = sy;
1898 t->used_this_frame++;
1902 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
1909 void gr_opengl_set_clear_color(int r, int g, int b)
1914 void gr_opengl_aaline(vertex *v1, vertex *v2)
1919 void gr_opengl_flash(int r, int g, int b)
1924 int gr_opengl_zbuffer_get()
1928 return GR_ZBUFF_NONE;
1931 int gr_opengl_zbuffer_set(int mode)
1935 return GR_ZBUFF_NONE;
1938 void gr_opengl_zbuffer_clear(int mode)
1943 void gr_opengl_set_gamma(float gamma)
1948 void gr_opengl_fade_in(int instantaneous)
1953 void gr_opengl_fade_out(int instantaneous)
1958 int gr_opengl_save_screen()
1965 void gr_opengl_restore_screen(int id)
1970 void gr_opengl_free_screen(int id)
1975 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
1980 void gr_opengl_dump_frame_stop()
1985 void gr_opengl_dump_frame()
1990 uint gr_opengl_lock()
1997 void gr_opengl_unlock()
2001 void gr_opengl_init()
2004 gr_opengl_cleanup();
2008 mprintf(( "Initializing opengl graphics device...\n" ));
2012 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2014 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2020 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2021 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2022 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2023 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2024 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2026 if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
2028 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2032 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2034 glMatrixMode(GL_PROJECTION);
2036 glMatrixMode(GL_MODELVIEW);
2039 glEnable(GL_TEXTURE_2D);
2041 glGenTextures(1, &bitmapTex);
2042 glBindTexture(GL_TEXTURE_2D, bitmapTex);
2043 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2044 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2045 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2047 glMatrixMode(GL_PROJECTION);
2051 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, -1.0, 1.0);
2053 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2055 bitmapMem = (GLubyte *)malloc(256*256*4);
2056 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, bitmapMem);
2057 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_CHAR, bitmapMem);
2068 Gr_red.mask = 0xff0000;
2073 Gr_green.mask = 0xff00;
2078 Gr_blue.mask = 0xff;
2086 Gr_red.mask = 0x7C00;
2091 Gr_green.mask = 0x3E0;
2096 Gr_blue.mask = 0x1F;
2104 Gr_red.mask = 0xF800;
2109 Gr_green.mask = 0x7E0;
2114 Gr_blue.mask = 0x1F;
2123 Gr_red.mask = 0xff0000;
2128 Gr_green.mask = 0xff00;
2133 Gr_blue.mask = 0xff;
2138 Int3(); // Illegal bpp
2141 // DDOI - set these so no one else does!
2142 Gr_ta_red.mask = 0x0f00;
2143 Gr_ta_red.shift = 8;
2144 Gr_ta_red.scale = 16;
2146 Gr_ta_green.mask = 0x00f0;
2147 Gr_ta_green.shift = 4;
2148 Gr_ta_green.scale = 16;
2150 Gr_ta_blue.mask = 0x000f;
2151 Gr_ta_blue.shift = 0;
2152 Gr_ta_blue.scale = 16;
2154 Gr_ta_alpha.mask = 0xf000;
2155 Gr_ta_alpha.shift = 12;
2156 Gr_ta_alpha.scale = 16;
2159 opengl_tcache_init (0);
2162 Gr_current_red = &Gr_red;
2163 Gr_current_blue = &Gr_blue;
2164 Gr_current_green = &Gr_green;
2165 Gr_current_alpha = &Gr_alpha;
2167 gr_screen.gf_flip = gr_opengl_flip;
2168 gr_screen.gf_flip_window = gr_opengl_flip_window;
2169 gr_screen.gf_set_clip = gr_opengl_set_clip;
2170 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2171 gr_screen.gf_set_font = grx_set_font;
2173 gr_screen.gf_set_color = gr_opengl_set_color;
2174 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2175 gr_screen.gf_create_shader = gr_opengl_create_shader;
2176 gr_screen.gf_set_shader = gr_opengl_set_shader;
2177 gr_screen.gf_clear = gr_opengl_clear;
2178 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2179 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2180 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2181 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2183 gr_screen.gf_rect = gr_opengl_rect;
2184 gr_screen.gf_shade = gr_opengl_shade;
2185 gr_screen.gf_string = gr_opengl_string;
2186 gr_screen.gf_circle = gr_opengl_circle;
2188 gr_screen.gf_line = gr_opengl_line;
2189 gr_screen.gf_aaline = gr_opengl_aaline;
2190 gr_screen.gf_pixel = gr_opengl_pixel;
2191 gr_screen.gf_scaler = gr_opengl_scaler;
2192 gr_screen.gf_tmapper = gr_opengl_tmapper;
2194 gr_screen.gf_gradient = gr_opengl_gradient;
2196 gr_screen.gf_set_palette = gr_opengl_set_palette;
2197 gr_screen.gf_get_color = gr_opengl_get_color;
2198 gr_screen.gf_init_color = gr_opengl_init_color;
2199 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
2200 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
2201 gr_screen.gf_print_screen = gr_opengl_print_screen;
2203 gr_screen.gf_fade_in = gr_opengl_fade_in;
2204 gr_screen.gf_fade_out = gr_opengl_fade_out;
2205 gr_screen.gf_flash = gr_opengl_flash;
2207 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
2208 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
2209 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
2211 gr_screen.gf_save_screen = gr_opengl_save_screen;
2212 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
2213 gr_screen.gf_free_screen = gr_opengl_free_screen;
2215 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
2216 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
2217 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
2219 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
2221 gr_screen.gf_lock = gr_opengl_lock;
2222 gr_screen.gf_unlock = gr_opengl_unlock;
2224 gr_screen.gf_fog_set = gr_opengl_fog_set;
2226 gr_screen.gf_get_region = gr_opengl_get_region;
2228 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
2230 gr_screen.gf_set_cull = gr_opengl_set_cull;
2232 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
2234 gr_screen.gf_filter_set = gr_opengl_filter_set;
2236 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
2238 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;