2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.20 2002/05/30 16:50:24 theoddone33
11 * Keyboard partially fixed
13 * Revision 1.19 2002/05/30 08:13:14 relnev
16 * Revision 1.18 2002/05/29 23:37:36 relnev
19 * Revision 1.17 2002/05/29 23:17:49 theoddone33
20 * Non working text code and fixed keys
22 * Revision 1.16 2002/05/29 19:45:13 theoddone33
23 * More changes on texture loading
25 * Revision 1.15 2002/05/29 19:06:48 theoddone33
26 * Enable string printing. Enable texture mapping
28 * Revision 1.14 2002/05/29 08:54:40 relnev
29 * "fixed" bitmap drawing.
31 * copied more d3d code over.
33 * Revision 1.13 2002/05/29 06:25:13 theoddone33
34 * Keyboard input, mouse tracking now work
36 * Revision 1.12 2002/05/29 04:52:45 relnev
39 * Revision 1.11 2002/05/29 04:29:56 relnev
40 * removed some unncessary stubbing, implemented opengl rect
42 * Revision 1.10 2002/05/29 04:13:27 theoddone33
45 * Revision 1.9 2002/05/29 03:35:51 relnev
48 * Revision 1.8 2002/05/29 03:30:05 relnev
51 * Revision 1.7 2002/05/29 02:52:32 theoddone33
52 * Enable OpenGL renderer
54 * Revision 1.6 2002/05/28 04:56:51 theoddone33
55 * runs a little bit now
57 * Revision 1.5 2002/05/28 04:07:28 theoddone33
58 * New graphics stubbing arrangement
60 * Revision 1.4 2002/05/27 23:39:34 relnev
63 * Revision 1.3 2002/05/27 22:35:01 theoddone33
66 * Revision 1.2 2002/05/27 22:32:02 theoddone33
67 * throw all d3d stuff at opengl
69 * Revision 1.1.1.1 2002/05/03 03:28:09 root
73 * 10 7/14/99 9:42a Dave
74 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
77 * 9 7/09/99 9:51a Dave
78 * Added thick polyline code.
80 * 8 6/29/99 10:35a Dave
81 * Interface polygon bitmaps! Whee!
83 * 7 2/03/99 11:44a Dave
84 * Fixed d3d transparent textures.
86 * 6 1/24/99 11:37p Dave
87 * First full rev of beam weapons. Very customizable. Removed some bogus
88 * Int3()'s in low level net code.
90 * 5 12/18/98 1:13a Dave
91 * Rough 1024x768 support for Direct3D. Proper detection and usage through
94 * 4 12/06/98 2:36p Dave
95 * Drastically improved nebula fogging.
97 * 3 11/11/98 5:37p Dave
98 * Checkin for multiplayer testing.
100 * 2 10/07/98 10:53a Dave
103 * 1 10/07/98 10:49a Dave
105 * 14 5/20/98 9:46p John
106 * added code so the places in code that change half the palette don't
107 * have to clear the screen.
109 * 13 5/06/98 5:30p John
110 * Removed unused cfilearchiver. Removed/replaced some unused/little used
111 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
112 * DirectX header files and libs that fixed the Direct3D alpha blending
115 * 12 4/14/98 12:15p John
116 * Made 16-bpp movies work.
118 * 11 3/12/98 5:36p John
119 * Took out any unused shaders. Made shader code take rgbc instead of
120 * matrix and vector since noone used it like a matrix and it would have
121 * been impossible to do in hardware. Made Glide implement a basic
122 * shader for online help.
124 * 10 3/10/98 4:18p John
125 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
126 * & Glide have popups and print screen. Took out all >8bpp software
127 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
128 * support Fred. Made zbuffering key off of functions rather than one
131 * 9 12/02/97 4:00p John
132 * Added first rev of thruster glow, along with variable levels of
133 * translucency, which retquired some restructing of palman.
135 * 8 10/03/97 9:10a John
136 * added better antialiased line drawer
138 * 7 9/23/97 10:45a John
139 * made so you can tell bitblt code to rle a bitmap by passing flag to
142 * 6 9/09/97 11:01a Sandeep
143 * fixed warning level 4 bugs
145 * 5 7/10/97 2:06p John
146 * added code to specify alphablending type for bitmaps.
148 * 4 6/17/97 7:04p John
149 * added d3d support for gradients.
150 * fixed some color bugs by adding screen signatures instead of watching
151 * flags and palette changes.
153 * 3 6/12/97 2:50a Lawrance
154 * bm_unlock() now passed bitmap number, not pointer
156 * 2 6/11/97 1:12p John
157 * Started fixing all the text colors in the game.
159 * 1 5/12/97 12:14p John
166 #include <windowsx.h>
173 #include "floating.h"
175 #include "grinternal.h"
176 #include "gropengl.h"
179 static int Inited = 0;
181 static GLuint bitmapTex;
182 static GLubyte *bitmapMem;
185 // Throw in some dummy functions - DDOI
187 int D3D_32bit = 0; // grd3d.cpp
188 int D3D_fog_mode = -1; // grd3d.cpp
189 int D3D_inited = 0; // grd3d.cpp
190 int D3D_zbias = 1; // grd3d.cpp
191 int D3d_rendition_uvs = 0; // grd3d.cpp
193 void gr_dd_activate(int active) // grdirectdraw.cpp
198 void gr_directdraw_cleanup() // grdirectdraw.cpp
203 void gr_directdraw_force_windowed() // grdirectdraw.cpp
208 void gr_directdraw_init()
214 void gr_d3d_preload(int x, int y)
219 void d3d_start_frame()
224 void d3d_stop_frame()
234 void d3d_zbias (int a)
240 void gr_opengl_activate(int b)
245 void gr_opengl_preload_init()
250 void gr_opengl_pixel(int x, int y)
252 if ( x < gr_screen.clip_left ) return;
253 if ( x > gr_screen.clip_right ) return;
254 if ( y < gr_screen.clip_top ) return;
255 if ( y > gr_screen.clip_bottom ) return;
258 void gr_opengl_clear()
260 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
263 void opengl_tcache_frame ();
264 void gr_opengl_flip()
268 SDL_GL_SwapBuffers ();
270 opengl_tcache_frame ();
273 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
278 void gr_opengl_set_clip(int x,int y,int w,int h)
280 // check for sanity of parameters
286 if (x >= gr_screen.max_w)
287 x = gr_screen.max_w - 1;
288 if (y >= gr_screen.max_h)
289 y = gr_screen.max_h - 1;
291 if (x + w > gr_screen.max_w)
292 w = gr_screen.max_w - x;
293 if (y + h > gr_screen.max_h)
294 h = gr_screen.max_h - y;
296 if (w > gr_screen.max_w)
298 if (h > gr_screen.max_h)
301 gr_screen.offset_x = x;
302 gr_screen.offset_y = y;
303 gr_screen.clip_left = 0;
304 gr_screen.clip_right = w-1;
305 gr_screen.clip_top = 0;
306 gr_screen.clip_bottom = h-1;
307 gr_screen.clip_width = w;
308 gr_screen.clip_height = h;
313 void gr_opengl_reset_clip()
315 gr_screen.offset_x = 0;
316 gr_screen.offset_y = 0;
317 gr_screen.clip_left = 0;
318 gr_screen.clip_top = 0;
319 gr_screen.clip_right = gr_screen.max_w - 1;
320 gr_screen.clip_bottom = gr_screen.max_h - 1;
321 gr_screen.clip_width = gr_screen.max_w;
322 gr_screen.clip_height = gr_screen.max_h;
325 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
327 gr_screen.current_alpha = alpha;
328 gr_screen.current_alphablend_mode = alphablend_mode;
329 gr_screen.current_bitblt_mode = bitblt_mode;
330 gr_screen.current_bitmap = bitmap_num;
332 gr_screen.current_bitmap_sx = sx;
333 gr_screen.current_bitmap_sy = sy;
336 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
338 shade->screen_sig = gr_screen.signature;
345 void gr_opengl_set_shader( shader * shade )
348 if (shade->screen_sig != gr_screen.signature) {
349 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
351 gr_screen.current_shader = *shade;
353 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
358 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
361 extern int GL_last_bitmap_id;
362 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
366 GLubyte *sptr, *dptr;
370 int cw = min(bmp->w, w);
371 int ch = min(bmp->h, h);
373 GL_last_bitmap_id = -1; /* HACK! */
375 glColor4f(1.0, 1.0, 1.0, 1.0);
376 glBindTexture(GL_TEXTURE_2D, bitmapTex);
379 for (iy = sy; iy < ch; iy += 256) {
381 ih = min(256, (ch-iy));
383 for (ix = sx; ix < cw; ix += 256) {
385 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
387 iw = min(256, (cw-ix));
393 memcpy(dptr, sptr, iw*2);
401 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
411 t = (float)ih / 256.0;
416 s = (float)iw / 256.0;
417 t = (float)ih / 256.0;
422 s = (float)iw / 256.0;
434 bm_unlock(gr_screen.current_bitmap);
438 void gr_d3d_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
445 int dx1=x, dx2=x+w-1;
446 int dy1=y, dy2=y+h-1;
449 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
454 if ( count > 1 ) Int3();
458 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
459 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
460 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
461 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
462 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
463 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
490 if ( w < 1 ) return; // clipped away!
491 if ( h < 1 ) return; // clipped away!
495 // Make sure clipping algorithm works
499 Assert( w == (dx2-dx1+1) );
500 Assert( h == (dy2-dy1+1) );
503 Assert( sx+w <= bw );
504 Assert( sy+h <= bh );
505 Assert( dx2 >= dx1 );
506 Assert( dy2 >= dy1 );
507 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
508 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
509 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
510 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
513 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
514 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
516 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
519 void gr_opengl_bitmap(int x, int y)
523 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
524 int dx1=x, dx2=x+w-1;
525 int dy1=y, dy2=y+h-1;
528 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
529 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
530 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
531 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
532 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
533 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
535 if ( sx < 0 ) return;
536 if ( sy < 0 ) return;
537 if ( sx >= w ) return;
538 if ( sy >= h ) return;
540 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
542 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
545 static void opengl_scanline(int x1,int x2,int y)
550 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
554 saved_zbuf = gr_zbuffer_get();
555 gr_zbuffer_set(GR_ZBUFF_NONE);
558 glColor4ub(r, g, b, a);
567 glVertex2f(x+w, y+h);
573 gr_zbuffer_set(saved_zbuf);
577 void gr_opengl_rect(int x,int y,int w,int h)
579 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);
582 void gr_opengl_shade(int x,int y,int w,int h)
589 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
590 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
591 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
592 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
593 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
594 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
595 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
596 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
598 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
601 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
606 if ( !gr_screen.current_color.is_alphacolor ) return;
608 float u_scale, v_scale;
610 // gr_d3d_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
612 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
613 // Couldn't set texture
614 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
618 // LPD3DTLVERTEX src_v;
619 // D3DTLVERTEX d3d_verts[4];
621 float u0, u1, v0, v1;
622 float x1, x2, y1, y2;
625 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
628 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
630 u0 = u_scale*i2fl(sx)/i2fl(bw);
631 v0 = v_scale*i2fl(sy)/i2fl(bh);
633 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
634 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
636 x1 = i2fl(x+gr_screen.offset_x);
637 y1 = i2fl(y+gr_screen.offset_y);
638 x2 = i2fl(x+w+gr_screen.offset_x);
639 y2 = i2fl(y+h+gr_screen.offset_y);
641 if ( gr_screen.current_color.is_alphacolor ) {
642 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
644 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
648 glTexCoord2f (u0, v0);
649 glVertex3f (x1, y1, 0.99);
651 glTexCoord2f (u1, v0);
652 glVertex3f (x2, y1, 0.99);
654 glTexCoord2f (u1, v1);
655 glVertex3f (x2, y2, 0.99);
657 glTexCoord2f (u0, v1);
658 glVertex3f (x1, y2, 0.99);
662 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
669 int dx1=x, dx2=x+w-1;
670 int dy1=y, dy2=y+h-1;
673 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
678 if ( count > 1 ) Int3();
682 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
683 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
684 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
685 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
686 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
687 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
714 if ( w < 1 ) return; // clipped away!
715 if ( h < 1 ) return; // clipped away!
719 // Make sure clipping algorithm works
723 Assert( w == (dx2-dx1+1) );
724 Assert( h == (dy2-dy1+1) );
727 Assert( sx+w <= bw );
728 Assert( sy+h <= bh );
729 Assert( dx2 >= dx1 );
730 Assert( dy2 >= dy1 );
731 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
732 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
733 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
734 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
737 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
738 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
741 void gr_opengl_aabitmap(int x, int y)
745 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
746 int dx1=x, dx2=x+w-1;
747 int dy1=y, dy2=y+h-1;
750 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
751 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
752 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
753 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
754 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
755 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
757 if ( sx < 0 ) return;
758 if ( sy < 0 ) return;
759 if ( sx >= w ) return;
760 if ( sy >= h ) return;
762 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
763 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
767 void gr_opengl_string( int sx, int sy, char *s )
769 int width, spacing, letter;
772 if ( !Current_font ) {
776 gr_set_bitmap(Current_font->bitmap_id);
781 if (sx==0x8000) { //centered
782 x = get_centered_x(s);
794 y += Current_font->h;
795 if (sx==0x8000) { //centered
796 x = get_centered_x(s);
803 letter = get_char_width(s[0],s[1],&width,&spacing);
806 //not in font, draw as space
814 // Check if this character is totally clipped
815 if ( x + width < gr_screen.clip_left ) continue;
816 if ( y + Current_font->h < gr_screen.clip_top ) continue;
817 if ( x > gr_screen.clip_right ) continue;
818 if ( y > gr_screen.clip_bottom ) continue;
821 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
822 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
826 wc = width - xd; hc = Current_font->h - yd;
827 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
828 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
830 if ( wc < 1 ) continue;
831 if ( hc < 1 ) continue;
833 int u = Current_font->bm_u[letter];
834 int v = Current_font->bm_v[letter];
836 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
840 void gr_opengl_circle( int xc, int yc, int d )
850 if ( (xc+r) < gr_screen.clip_left ) return;
851 if ( (xc-r) > gr_screen.clip_right ) return;
852 if ( (yc+r) < gr_screen.clip_top ) return;
853 if ( (yc-r) > gr_screen.clip_bottom ) return;
856 // Draw the first octant
857 opengl_scanline( xc-y, xc+y, yc-x );
858 opengl_scanline( xc-y, xc+y, yc+x );
863 // Draw the second octant
864 opengl_scanline( xc-x, xc+x, yc-y );
865 opengl_scanline( xc-x, xc+x, yc+y );
872 opengl_scanline( xc-x, xc+x, yc-y );
873 opengl_scanline( xc-x, xc+x, yc+y );
879 void gr_opengl_line(int x1,int y1,int x2,int y2)
881 int clipped = 0, swapped=0;
883 glDisable ( GL_DEPTH_TEST );
884 glEnable ( GL_BLEND );
885 glBlendFunc ( GL_SRC_ALPHA, GL_DST_ALPHA );
887 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);
890 glColor4f (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
891 glVertex3f (i2fl (x1+gr_screen.offset_x),i2fl (y1+gr_screen.offset_y), 0.99f);
892 glVertex3f (i2fl (x2+gr_screen.offset_x),i2fl (y2+gr_screen.offset_y), 0.99f);
896 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
898 void gr_opengl_scaler(vertex *va, vertex *vb )
900 float x0, y0, x1, y1;
901 float u0, v0, u1, v1;
902 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
903 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
904 float xmin, xmax, ymin, ymax;
905 int dx0, dy0, dx1, dy1;
907 //============= CLIP IT =====================
909 x0 = va->sx; y0 = va->sy;
910 x1 = vb->sx; y1 = vb->sy;
912 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
913 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
915 u0 = va->u; v0 = va->v;
916 u1 = vb->u; v1 = vb->v;
918 // Check for obviously offscreen bitmaps...
919 if ( (y1<=y0) || (x1<=x0) ) return;
920 if ( (x1<xmin ) || (x0>xmax) ) return;
921 if ( (y1<ymin ) || (y0>ymax) ) return;
923 clipped_u0 = u0; clipped_v0 = v0;
924 clipped_u1 = u1; clipped_v1 = v1;
926 clipped_x0 = x0; clipped_y0 = y0;
927 clipped_x1 = x1; clipped_y1 = y1;
929 // Clip the left, moving u0 right as necessary
931 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
935 // Clip the right, moving u1 left as necessary
937 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
941 // Clip the top, moving v0 down as necessary
943 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
947 // Clip the bottom, moving v1 up as necessary
949 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
953 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
954 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
956 if (dx1<=dx0) return;
957 if (dy1<=dy0) return;
959 //============= DRAW IT =====================
967 tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
968 if ( fl_abs(tmpu) < 0.001f ) {
969 return; // scaled up way too far!
971 tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
972 if ( fl_abs(tmpv) < 0.001f ) {
973 return; // scaled up way too far!
976 bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
978 du = fl2f(tmpu*(bp->w-1));
979 dv = fl2f(tmpv*(bp->h-1));
981 v = fl2f(clipped_v0*(bp->h-1));
982 u = fl2f(clipped_u0*(bp->w-1));
985 spixels = (ubyte *)bp->data;
987 for (y=dy0; y<=dy1; y++ ) {
988 sbits = &spixels[bp->rowsize*(v>>16)];
992 for (x=0; x<w; x++ ) {
993 ubyte c = sbits[ tmp_u >> 16 ];
995 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
996 gr_pixel( x+dx0, y );
1003 bm_unlock(gr_screen.current_bitmap);
1008 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
1011 float u_scale = 1.0f, v_scale = 1.0f;
1014 // Make nebula use the texture mapper... this blends the colors better.
1015 if ( flags & TMAP_FLAG_NEBULA ){
1020 gr_texture_source texture_source = (gr_texture_source)-1;
1021 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1022 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1024 if ( gr_zbuffering ) {
1025 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1026 zbuffer_type = ZBUFFER_TYPE_READ;
1028 zbuffer_type = ZBUFFER_TYPE_FULL;
1031 zbuffer_type = ZBUFFER_TYPE_NONE;
1037 int tmap_type = TCACHE_TYPE_NORMAL;
1041 if ( flags & TMAP_FLAG_TEXTURED ) {
1044 r = gr_screen.current_color.red;
1045 g = gr_screen.current_color.green;
1046 b = gr_screen.current_color.blue;
1049 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1051 // Some blend function stuff here - DDOI
1054 float factor = gr_screen.current_alpha;
1058 if ( factor <= 1.0f ) {
1059 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1060 r = (r*tmp_alpha)/255;
1061 g = (g*tmp_alpha)/255;
1062 b = (b*tmp_alpha)/255;
1069 if ( flags & TMAP_FLAG_TEXTURED ) {
1070 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 ))
1072 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1076 // use nonfiltered textures for bitmap sections
1078 if(flags & TMAP_FLAG_BITMAP_SECTION){
1079 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1081 texture_source = TEXTURE_SOURCE_DECAL;
1087 x1 = gr_screen.clip_left*16;
1088 x2 = gr_screen.clip_right*16+15;
1089 y1 = gr_screen.clip_top*16;
1090 y2 = gr_screen.clip_bottom*16+15;
1092 float uoffset = 0.0f;
1093 float voffset = 0.0f;
1095 float minu=0.0f, minv=0.0f, maxu=1.0f, maxv=1.0f;
1097 if ( flags & TMAP_FLAG_TEXTURED )
1104 STUB_FUNCTION; // DDOI - this still needs work
1105 glBegin (GL_TRIANGLE_FAN);
1108 if (flags & TMAP_FLAG_ALPHA) a = verts[i]->a;
1111 if (flags & TMAP_FLAG_NEBULA ) {
1113 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1114 r = gr_palette[pal*3+0];
1115 g = gr_palette[pal*3+1];
1116 b = gr_palette[pal*3+2];
1118 }else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1119 r = Gr_gamma_lookup[verts[i]->b];
1120 g = Gr_gamma_lookup[verts[i]->b];
1121 b = Gr_gamma_lookup[verts[i]->b];
1122 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1123 // Make 0.75 be 256.0f
1124 r = Gr_gamma_lookup[verts[i]->r];
1125 g = Gr_gamma_lookup[verts[i]->g];
1126 b = Gr_gamma_lookup[verts[i]->b];
1128 // use constant RGB values...
1131 glColor4i (r,g,b,a);
1133 // DDOI - FIXME fog stuff
1134 // DDOI - FIXME TexCoord stuff
1135 glVertex3f (verts[i]->sx+gr_screen.offset_x,
1136 verts[i]->sy+gr_screen.offset_y,
1143 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1148 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1153 void gr_opengl_get_color( int * r, int * g, int * b )
1155 if (r) *r = gr_screen.current_color.red;
1156 if (g) *g = gr_screen.current_color.green;
1157 if (b) *b = gr_screen.current_color.blue;
1160 void gr_opengl_init_color(color *c, int r, int g, int b)
1162 c->screen_sig = gr_screen.signature;
1163 c->red = (unsigned char)r;
1164 c->green = (unsigned char)g;
1165 c->blue = (unsigned char)b;
1167 c->ac_type = AC_TYPE_NONE;
1169 c->is_alphacolor = 0;
1173 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1175 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1176 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1177 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1178 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1180 gr_opengl_init_color( clr, r, g, b );
1182 clr->alpha = (unsigned char)alpha;
1183 clr->ac_type = (ubyte)type;
1184 clr->alphacolor = -1;
1185 clr->is_alphacolor = 1;
1188 void gr_opengl_set_color( int r, int g, int b )
1190 Assert((r >= 0) && (r < 256));
1191 Assert((g >= 0) && (g < 256));
1192 Assert((b >= 0) && (b < 256));
1194 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1197 void gr_opengl_set_color_fast(color *dst)
1199 if ( dst->screen_sig != gr_screen.signature ) {
1200 if ( dst->is_alphacolor ) {
1201 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1203 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1206 gr_screen.current_color = *dst;
1209 void gr_opengl_print_screen(char *filename)
1214 int gr_opengl_supports_res_ingame(int res)
1221 int gr_opengl_supports_res_interface(int res)
1228 void opengl_tcache_cleanup ();
1229 void gr_opengl_cleanup()
1231 if ( !Inited ) return;
1238 opengl_tcache_cleanup ();
1243 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
1248 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1253 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
1258 void gr_opengl_set_cull(int cull)
1263 void gr_opengl_filter_set(int filter)
1269 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1275 typedef struct tcache_slot_opengl {
1276 GLuint texture_handle;
1277 float u_scale, v_scale;
1280 char used_this_frame;
1285 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1286 tcache_slot_opengl *parent;
1287 } tcache_slot_opengl;
1289 static void *Texture_sections = NULL;
1290 tcache_slot_opengl *Textures = NULL;
1292 int GL_texture_sections = 0;
1293 int GL_texture_ram = 0;
1294 int GL_frame_count = 0;
1295 int GL_min_texture_width = 0;
1296 int GL_max_texture_width = 0;
1297 int GL_min_texture_height = 0;
1298 int GL_max_texture_height = 0;
1299 int GL_square_textures = 0;
1300 int GL_textures_in = 0;
1301 int GL_textures_in_frame = 0;
1302 int GL_last_bitmap_id = -1;
1303 int GL_last_detail = -1;
1304 int GL_last_bitmap_type = -1;
1305 int GL_last_section_x = -1;
1306 int GL_last_section_y = -1;
1310 void opengl_tcache_init (int use_sections)
1314 // DDOI - FIXME skipped a lot of stuff here
1317 GL_min_texture_width = 16;
1318 GL_min_texture_height = 16;
1319 GL_max_texture_width = 256;
1320 GL_max_texture_height = 256;
1322 GL_square_textures = 1;
1324 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1330 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1331 if(!Texture_sections){
1334 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1337 // Init the texture structures
1338 int section_count = 0;
1339 for( i=0; i<MAX_BITMAPS; i++ ) {
1341 Textures[i].vram_texture = NULL;
1342 Textures[i].vram_texture_surface = NULL;
1344 Textures[i].texture_handle = 0;
1346 Textures[i].bitmap_id = -1;
1347 Textures[i].size = 0;
1348 Textures[i].used_this_frame = 0;
1350 Textures[i].parent = NULL;
1352 // allocate sections
1354 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1355 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1356 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1357 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1359 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1360 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1362 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1363 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1364 Textures[i].data_sections[idx][s_idx]->size = 0;
1365 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1369 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1370 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1371 Textures[i].data_sections[idx][s_idx] = NULL;
1377 GL_texture_sections = use_sections;
1379 //GL_last_detail = Detail.hardware_textures;
1380 GL_last_bitmap_id = -1;
1381 GL_last_bitmap_type = -1;
1383 GL_last_section_x = -1;
1384 GL_last_section_y = -1;
1387 GL_textures_in_frame = 0;
1390 int opengl_free_texture (tcache_slot_opengl *t);
1392 void opengl_tcache_flush ()
1396 for( i=0; i<MAX_BITMAPS; i++ ) {
1397 opengl_free_texture ( &Textures[i] );
1399 if (GL_textures_in != 0) {
1400 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1404 GL_last_bitmap_id = -1;
1405 GL_last_section_x = -1;
1406 GL_last_section_y = -1;
1409 void opengl_tcache_cleanup ()
1411 opengl_tcache_flush ();
1414 GL_textures_in_frame = 0;
1421 if( Texture_sections != NULL ){
1422 free(Texture_sections);
1423 Texture_sections = NULL;
1427 void opengl_tcache_frame ()
1431 GL_last_bitmap_id = -1;
1432 GL_textures_in_frame = 0;
1437 for( i=0; i<MAX_BITMAPS; i++ ) {
1438 Textures[i].used_this_frame = 0;
1441 if(Textures[i].data_sections[0][0] != NULL){
1442 Assert(GL_texture_sections);
1443 if(GL_texture_sections){
1444 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1445 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1446 if(Textures[i].data_sections[idx][s_idx] != NULL){
1447 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1456 opengl_tcache_flush();
1461 int opengl_free_texture ( tcache_slot_opengl *t )
1467 if ( t->bitmap_id > -1 ) {
1468 // if I, or any of my children have been used this frame, bail
1469 if(t->used_this_frame){
1472 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1473 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1474 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
1480 // ok, now we know its legal to free everything safely
1481 glDeleteTextures (1, &t->texture_handle);
1482 t->texture_handle = 0;
1484 if ( GL_last_bitmap_id == t->bitmap_id ) {
1485 GL_last_bitmap_id = -1;
1488 // if this guy has children, free them too, since the children
1489 // actually make up his size
1490 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1491 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1492 if(t->data_sections[idx][s_idx] != NULL){
1493 opengl_free_texture(t->data_sections[idx][s_idx]);
1499 t->used_this_frame = 0;
1500 GL_textures_in -= t->size;
1506 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1511 if((w_out == NULL) || (h_out == NULL)){
1521 for (i=0; i<16; i++ ) {
1522 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
1528 for (i=0; i<16; i++ ) {
1529 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
1536 if ( tex_w < GL_min_texture_width ) {
1537 tex_w = GL_min_texture_width;
1538 } else if ( tex_w > GL_max_texture_width ) {
1539 tex_w = GL_max_texture_width;
1542 if ( tex_h < GL_min_texture_height ) {
1543 tex_h = GL_min_texture_height;
1544 } else if ( tex_h > GL_max_texture_height ) {
1545 tex_h = GL_max_texture_height;
1548 if ( GL_square_textures ) {
1550 // Make the both be equal to larger of the two
1551 new_size = max(tex_w, tex_h);
1556 // store the outgoing size
1561 // data == start of bitmap data
1562 // sx == x offset into bitmap
1563 // sy == y offset into bitmap
1564 // src_w == absolute width of section on source bitmap
1565 // src_h == absolute height of section on source bitmap
1566 // bmap_w == width of source bitmap
1567 // bmap_h == height of source bitmap
1568 // tex_w == width of final texture
1569 // tex_h == height of final texture
1570 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)
1579 if ( t->used_this_frame ) {
1580 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
1585 if(!opengl_free_texture(t)){
1590 // get final texture size
1591 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
1593 if ( (tex_w < 1) || (tex_h < 1) ) {
1594 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
1598 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
1599 t->u_scale = (float)bmap_w / (float)tex_w;
1600 t->v_scale = (float)bmap_h / (float)tex_h;
1601 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1602 t->u_scale = (float)src_w / (float)tex_w;
1603 t->v_scale = (float)src_h / (float)tex_h;
1609 glGenTextures (1, &t->texture_handle);
1610 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
1612 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1613 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1614 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1615 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1617 switch (bitmap_type) {
1619 case TCACHE_TYPE_AABITMAP:
1622 ubyte *bmp_data = ((ubyte*)data);
1623 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
1624 ubyte *texmemp = texmem;
1626 for (i=0;i<tex_h;i++)
1628 for (j=0;j<tex_w;j++)
1630 if (i < bmap_h && j < bmap_w) {
1632 *texmemp++ = bmp_data[i*bmap_w+j]<<4;
1640 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
1646 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGBA,
1647 GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
1651 t->bitmap_id = texture_handle;
1652 t->time_created = GL_frame_count;
1653 t->used_this_frame = 0;
1654 t->size = tex_w * tex_h * 2;
1655 t->w = (ushort)tex_w;
1656 t->h = (ushort)tex_h;
1657 GL_textures_in_frame += t->size;
1659 GL_textures_in += t->size;
1667 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
1671 int final_w, final_h;
1675 // setup texture/bitmap flags
1677 switch(bitmap_type){
1678 case TCACHE_TYPE_AABITMAP:
1679 flags |= BMP_AABITMAP;
1682 case TCACHE_TYPE_NORMAL:
1683 flags |= BMP_TEX_OTHER;
1684 case TCACHE_TYPE_XPARENT:
1685 flags |= BMP_TEX_XPARENT;
1687 case TCACHE_TYPE_NONDARKENING:
1689 flags |= BMP_TEX_NONDARK;
1693 // lock the bitmap into the proper format
1694 bmp = bm_lock(bitmap_handle, bpp, flags);
1695 if ( bmp == NULL ) {
1696 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1705 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
1706 max_w /= D3D_texture_divider;
1707 max_h /= D3D_texture_divider;
1709 // Detail.debris_culling goes from 0 to 4.
1710 max_w /= 16 >> Detail.hardware_textures;
1711 max_h /= 16 >> Detail.hardware_textures;
1715 // get final texture size as it will be allocated as a DD surface
1716 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
1718 // if this tcache slot has no bitmap
1719 if ( tslot->bitmap_id < 0) {
1722 // different bitmap altogether - determine if the new one can use the old one's slot
1723 else if (tslot->bitmap_id != bitmap_handle) {
1724 if((final_w == tslot->w) && (final_h == tslot->h)){
1726 //ml_printf("Reloading texture %d\n", bitmap_handle);
1733 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);
1735 // unlock the bitmap
1736 bm_unlock(bitmap_handle);
1741 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
1745 int final_w, final_h;
1746 int section_x, section_y;
1749 // setup texture/bitmap flags
1750 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
1751 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
1752 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
1754 flags = BMP_TEX_XPARENT;
1756 // lock the bitmap in the proper format
1757 bmp = bm_lock(bitmap_handle, 16, flags);
1758 if ( bmp == NULL ) {
1759 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1762 // determine the width and height of this section
1763 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
1765 // get final texture size as it will be allocated as a DD surface
1766 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
1768 // if this tcache slot has no bitmap
1769 if ( tslot->bitmap_id < 0) {
1772 // different bitmap altogether - determine if the new one can use the old one's slot
1773 else if (tslot->bitmap_id != bitmap_handle) {
1774 if((final_w == tslot->w) && (final_h == tslot->h)){
1782 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);
1784 // unlock the bitmap
1785 bm_unlock(bitmap_handle);
1791 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
1792 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)
1801 GL_last_bitmap_id = -1;
1806 if ( GL_last_detail != Detail.hardware_textures ) {
1807 GL_last_detail = Detail.hardware_textures;
1808 opengl_tcache_flush();
1816 int n = bm_get_cache_slot (bitmap_id, 1);
1817 tcache_slot_opengl *t = &Textures[n];
1819 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)) {
1820 t->used_this_frame++;
1822 // mark all children as used
1823 if(GL_texture_sections){
1824 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1825 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1826 if(t->data_sections[idx][s_idx] != NULL){
1827 t->data_sections[idx][s_idx]->used_this_frame++;
1833 *u_scale = t->u_scale;
1834 *v_scale = t->v_scale;
1838 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1839 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
1840 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
1846 // if the texture sections haven't been created yet
1847 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
1849 // lock the bitmap in the proper format
1850 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
1851 bm_unlock(bitmap_id);
1853 // now lets do something for each texture
1855 for(idx=0; idx<bmp->sections.num_x; idx++){
1856 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
1857 // hmm. i'd rather we didn't have to do it this way...
1858 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
1862 // not used this frame
1863 t->data_sections[idx][s_idx]->used_this_frame = 0;
1867 // zero out pretty much everything in the parent struct since he's just the root
1868 t->bitmap_id = bitmap_id;
1869 t->texture_handle = 0;
1870 t->time_created = t->data_sections[sx][sy]->time_created;
1871 t->used_this_frame = 0;
1873 t->vram_texture = NULL;
1874 t->vram_texture_surface = NULL
1878 // argh. we failed to upload. free anything we can
1880 opengl_free_texture(t);
1882 // swap in the texture we want
1884 t = t->data_sections[sx][sy];
1887 // all other "normal" textures
1888 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
1889 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
1892 // everything went ok
1893 if(ret_val && (t->texture_handle) && !vram_full){
1894 *u_scale = t->u_scale;
1895 *v_scale = t->v_scale;
1897 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
1899 GL_last_bitmap_id = t->bitmap_id;
1900 GL_last_bitmap_type = bitmap_type;
1901 GL_last_section_x = sx;
1902 GL_last_section_y = sy;
1904 t->used_this_frame++;
1908 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
1915 void gr_opengl_set_clear_color(int r, int g, int b)
1920 void gr_opengl_aaline(vertex *v1, vertex *v2)
1925 void gr_opengl_flash(int r, int g, int b)
1930 int gr_opengl_zbuffer_get()
1934 return GR_ZBUFF_NONE;
1937 int gr_opengl_zbuffer_set(int mode)
1941 return GR_ZBUFF_NONE;
1944 void gr_opengl_zbuffer_clear(int mode)
1949 void gr_opengl_set_gamma(float gamma)
1954 void gr_opengl_fade_in(int instantaneous)
1959 void gr_opengl_fade_out(int instantaneous)
1964 int gr_opengl_save_screen()
1971 void gr_opengl_restore_screen(int id)
1976 void gr_opengl_free_screen(int id)
1981 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
1986 void gr_opengl_dump_frame_stop()
1991 void gr_opengl_dump_frame()
1996 uint gr_opengl_lock()
2003 void gr_opengl_unlock()
2007 void gr_opengl_init()
2010 gr_opengl_cleanup();
2014 mprintf(( "Initializing opengl graphics device...\n" ));
2018 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2020 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2026 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2027 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2028 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2029 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2030 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2032 if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
2034 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2038 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2040 glMatrixMode(GL_PROJECTION);
2042 glMatrixMode(GL_MODELVIEW);
2045 glEnable(GL_TEXTURE_2D);
2047 glGenTextures(1, &bitmapTex);
2048 glBindTexture(GL_TEXTURE_2D, bitmapTex);
2049 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2050 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2051 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2053 glMatrixMode(GL_PROJECTION);
2057 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, -1.0, 1.0);
2059 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2061 bitmapMem = (GLubyte *)malloc(256*256*4);
2062 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, bitmapMem);
2063 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_CHAR, bitmapMem);
2074 Gr_red.mask = 0xff0000;
2079 Gr_green.mask = 0xff00;
2084 Gr_blue.mask = 0xff;
2092 Gr_red.mask = 0x7C00;
2097 Gr_green.mask = 0x3E0;
2102 Gr_blue.mask = 0x1F;
2110 Gr_red.mask = 0xF800;
2115 Gr_green.mask = 0x7E0;
2120 Gr_blue.mask = 0x1F;
2129 Gr_red.mask = 0xff0000;
2134 Gr_green.mask = 0xff00;
2139 Gr_blue.mask = 0xff;
2144 Int3(); // Illegal bpp
2147 // DDOI - set these so no one else does!
2148 Gr_ta_red.mask = 0x0f00;
2149 Gr_ta_red.shift = 8;
2150 Gr_ta_red.scale = 16;
2152 Gr_ta_green.mask = 0x00f0;
2153 Gr_ta_green.shift = 4;
2154 Gr_ta_green.scale = 16;
2156 Gr_ta_blue.mask = 0x000f;
2157 Gr_ta_blue.shift = 0;
2158 Gr_ta_blue.scale = 16;
2160 Gr_ta_alpha.mask = 0xf000;
2161 Gr_ta_alpha.shift = 12;
2162 Gr_ta_alpha.scale = 16;
2165 opengl_tcache_init (0);
2168 Gr_current_red = &Gr_red;
2169 Gr_current_blue = &Gr_blue;
2170 Gr_current_green = &Gr_green;
2171 Gr_current_alpha = &Gr_alpha;
2173 gr_screen.gf_flip = gr_opengl_flip;
2174 gr_screen.gf_flip_window = gr_opengl_flip_window;
2175 gr_screen.gf_set_clip = gr_opengl_set_clip;
2176 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2177 gr_screen.gf_set_font = grx_set_font;
2179 gr_screen.gf_set_color = gr_opengl_set_color;
2180 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2181 gr_screen.gf_create_shader = gr_opengl_create_shader;
2182 gr_screen.gf_set_shader = gr_opengl_set_shader;
2183 gr_screen.gf_clear = gr_opengl_clear;
2184 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2185 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2186 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2187 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2189 gr_screen.gf_rect = gr_opengl_rect;
2190 gr_screen.gf_shade = gr_opengl_shade;
2191 gr_screen.gf_string = gr_opengl_string;
2192 gr_screen.gf_circle = gr_opengl_circle;
2194 gr_screen.gf_line = gr_opengl_line;
2195 gr_screen.gf_aaline = gr_opengl_aaline;
2196 gr_screen.gf_pixel = gr_opengl_pixel;
2197 gr_screen.gf_scaler = gr_opengl_scaler;
2198 gr_screen.gf_tmapper = gr_opengl_tmapper;
2200 gr_screen.gf_gradient = gr_opengl_gradient;
2202 gr_screen.gf_set_palette = gr_opengl_set_palette;
2203 gr_screen.gf_get_color = gr_opengl_get_color;
2204 gr_screen.gf_init_color = gr_opengl_init_color;
2205 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
2206 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
2207 gr_screen.gf_print_screen = gr_opengl_print_screen;
2209 gr_screen.gf_fade_in = gr_opengl_fade_in;
2210 gr_screen.gf_fade_out = gr_opengl_fade_out;
2211 gr_screen.gf_flash = gr_opengl_flash;
2213 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
2214 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
2215 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
2217 gr_screen.gf_save_screen = gr_opengl_save_screen;
2218 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
2219 gr_screen.gf_free_screen = gr_opengl_free_screen;
2221 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
2222 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
2223 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
2225 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
2227 gr_screen.gf_lock = gr_opengl_lock;
2228 gr_screen.gf_unlock = gr_opengl_unlock;
2230 gr_screen.gf_fog_set = gr_opengl_fog_set;
2232 gr_screen.gf_get_region = gr_opengl_get_region;
2234 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
2236 gr_screen.gf_set_cull = gr_opengl_set_cull;
2238 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
2240 gr_screen.gf_filter_set = gr_opengl_filter_set;
2242 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
2244 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;