2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.16 2002/05/29 19:45:13 theoddone33
11 * More changes on texture loading
13 * Revision 1.15 2002/05/29 19:06:48 theoddone33
14 * Enable string printing. Enable texture mapping
16 * Revision 1.14 2002/05/29 08:54:40 relnev
17 * "fixed" bitmap drawing.
19 * copied more d3d code over.
21 * Revision 1.13 2002/05/29 06:25:13 theoddone33
22 * Keyboard input, mouse tracking now work
24 * Revision 1.12 2002/05/29 04:52:45 relnev
27 * Revision 1.11 2002/05/29 04:29:56 relnev
28 * removed some unncessary stubbing, implemented opengl rect
30 * Revision 1.10 2002/05/29 04:13:27 theoddone33
33 * Revision 1.9 2002/05/29 03:35:51 relnev
36 * Revision 1.8 2002/05/29 03:30:05 relnev
39 * Revision 1.7 2002/05/29 02:52:32 theoddone33
40 * Enable OpenGL renderer
42 * Revision 1.6 2002/05/28 04:56:51 theoddone33
43 * runs a little bit now
45 * Revision 1.5 2002/05/28 04:07:28 theoddone33
46 * New graphics stubbing arrangement
48 * Revision 1.4 2002/05/27 23:39:34 relnev
51 * Revision 1.3 2002/05/27 22:35:01 theoddone33
54 * Revision 1.2 2002/05/27 22:32:02 theoddone33
55 * throw all d3d stuff at opengl
57 * Revision 1.1.1.1 2002/05/03 03:28:09 root
61 * 10 7/14/99 9:42a Dave
62 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
65 * 9 7/09/99 9:51a Dave
66 * Added thick polyline code.
68 * 8 6/29/99 10:35a Dave
69 * Interface polygon bitmaps! Whee!
71 * 7 2/03/99 11:44a Dave
72 * Fixed d3d transparent textures.
74 * 6 1/24/99 11:37p Dave
75 * First full rev of beam weapons. Very customizable. Removed some bogus
76 * Int3()'s in low level net code.
78 * 5 12/18/98 1:13a Dave
79 * Rough 1024x768 support for Direct3D. Proper detection and usage through
82 * 4 12/06/98 2:36p Dave
83 * Drastically improved nebula fogging.
85 * 3 11/11/98 5:37p Dave
86 * Checkin for multiplayer testing.
88 * 2 10/07/98 10:53a Dave
91 * 1 10/07/98 10:49a Dave
93 * 14 5/20/98 9:46p John
94 * added code so the places in code that change half the palette don't
95 * have to clear the screen.
97 * 13 5/06/98 5:30p John
98 * Removed unused cfilearchiver. Removed/replaced some unused/little used
99 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
100 * DirectX header files and libs that fixed the Direct3D alpha blending
103 * 12 4/14/98 12:15p John
104 * Made 16-bpp movies work.
106 * 11 3/12/98 5:36p John
107 * Took out any unused shaders. Made shader code take rgbc instead of
108 * matrix and vector since noone used it like a matrix and it would have
109 * been impossible to do in hardware. Made Glide implement a basic
110 * shader for online help.
112 * 10 3/10/98 4:18p John
113 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
114 * & Glide have popups and print screen. Took out all >8bpp software
115 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
116 * support Fred. Made zbuffering key off of functions rather than one
119 * 9 12/02/97 4:00p John
120 * Added first rev of thruster glow, along with variable levels of
121 * translucency, which retquired some restructing of palman.
123 * 8 10/03/97 9:10a John
124 * added better antialiased line drawer
126 * 7 9/23/97 10:45a John
127 * made so you can tell bitblt code to rle a bitmap by passing flag to
130 * 6 9/09/97 11:01a Sandeep
131 * fixed warning level 4 bugs
133 * 5 7/10/97 2:06p John
134 * added code to specify alphablending type for bitmaps.
136 * 4 6/17/97 7:04p John
137 * added d3d support for gradients.
138 * fixed some color bugs by adding screen signatures instead of watching
139 * flags and palette changes.
141 * 3 6/12/97 2:50a Lawrance
142 * bm_unlock() now passed bitmap number, not pointer
144 * 2 6/11/97 1:12p John
145 * Started fixing all the text colors in the game.
147 * 1 5/12/97 12:14p John
154 #include <windowsx.h>
161 #include "floating.h"
163 #include "grinternal.h"
164 #include "gropengl.h"
167 static int Inited = 0;
169 static GLuint bitmapTex;
170 static GLubyte *bitmapMem;
173 // Throw in some dummy functions - DDOI
175 int D3D_32bit = 0; // grd3d.cpp
176 int D3D_fog_mode = -1; // grd3d.cpp
177 int D3D_inited = 0; // grd3d.cpp
178 int D3D_zbias = 1; // grd3d.cpp
179 int D3d_rendition_uvs = 0; // grd3d.cpp
181 void gr_dd_activate(int active) // grdirectdraw.cpp
186 void gr_directdraw_cleanup() // grdirectdraw.cpp
191 void gr_directdraw_force_windowed() // grdirectdraw.cpp
196 void gr_directdraw_init()
202 void gr_d3d_preload(int x, int y)
207 void d3d_start_frame()
212 void d3d_stop_frame()
222 void d3d_zbias (int a)
228 void gr_opengl_activate(int b)
233 void gr_opengl_preload_init()
238 void gr_opengl_pixel(int x, int y)
240 if ( x < gr_screen.clip_left ) return;
241 if ( x > gr_screen.clip_right ) return;
242 if ( y < gr_screen.clip_top ) return;
243 if ( y > gr_screen.clip_bottom ) return;
246 void gr_opengl_clear()
248 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
251 void opengl_tcache_frame ();
252 void gr_opengl_flip()
256 SDL_GL_SwapBuffers ();
258 opengl_tcache_frame ();
261 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
266 void gr_opengl_set_clip(int x,int y,int w,int h)
268 // check for sanity of parameters
274 if (x >= gr_screen.max_w)
275 x = gr_screen.max_w - 1;
276 if (y >= gr_screen.max_h)
277 y = gr_screen.max_h - 1;
279 if (x + w > gr_screen.max_w)
280 w = gr_screen.max_w - x;
281 if (y + h > gr_screen.max_h)
282 h = gr_screen.max_h - y;
284 if (w > gr_screen.max_w)
286 if (h > gr_screen.max_h)
289 gr_screen.offset_x = x;
290 gr_screen.offset_y = y;
291 gr_screen.clip_left = 0;
292 gr_screen.clip_right = w-1;
293 gr_screen.clip_top = 0;
294 gr_screen.clip_bottom = h-1;
295 gr_screen.clip_width = w;
296 gr_screen.clip_height = h;
301 void gr_opengl_reset_clip()
303 gr_screen.offset_x = 0;
304 gr_screen.offset_y = 0;
305 gr_screen.clip_left = 0;
306 gr_screen.clip_top = 0;
307 gr_screen.clip_right = gr_screen.max_w - 1;
308 gr_screen.clip_bottom = gr_screen.max_h - 1;
309 gr_screen.clip_width = gr_screen.max_w;
310 gr_screen.clip_height = gr_screen.max_h;
313 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
315 gr_screen.current_alpha = alpha;
316 gr_screen.current_alphablend_mode = alphablend_mode;
317 gr_screen.current_bitblt_mode = bitblt_mode;
318 gr_screen.current_bitmap = bitmap_num;
320 gr_screen.current_bitmap_sx = sx;
321 gr_screen.current_bitmap_sy = sy;
324 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
326 shade->screen_sig = gr_screen.signature;
333 void gr_opengl_set_shader( shader * shade )
336 if (shade->screen_sig != gr_screen.signature) {
337 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
339 gr_screen.current_shader = *shade;
341 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
345 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
349 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
353 GLubyte *sptr, *dptr;
357 int cw = min(bmp->w, w);
358 int ch = min(bmp->h, h);
360 glColor4f(1.0, 1.0, 1.0, 1.0);
361 glBindTexture(GL_TEXTURE_2D, bitmapTex);
364 for (iy = sy; iy < ch; iy += 256) {
366 ih = min(256, (ch-iy));
368 for (ix = sx; ix < cw; ix += 256) {
370 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
372 iw = min(256, (cw-ix));
378 memcpy(dptr, sptr, iw*2);
386 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
396 t = (float)ih / 256.0;
401 s = (float)iw / 256.0;
402 t = (float)ih / 256.0;
407 s = (float)iw / 256.0;
421 void gr_d3d_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
428 int dx1=x, dx2=x+w-1;
429 int dy1=y, dy2=y+h-1;
432 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
437 if ( count > 1 ) Int3();
441 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
442 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
443 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
444 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
445 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
446 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
473 if ( w < 1 ) return; // clipped away!
474 if ( h < 1 ) return; // clipped away!
478 // Make sure clipping algorithm works
482 Assert( w == (dx2-dx1+1) );
483 Assert( h == (dy2-dy1+1) );
486 Assert( sx+w <= bw );
487 Assert( sy+h <= bh );
488 Assert( dx2 >= dx1 );
489 Assert( dy2 >= dy1 );
490 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
491 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
492 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
493 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
496 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
497 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
499 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
502 void gr_opengl_bitmap(int x, int y)
506 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
507 int dx1=x, dx2=x+w-1;
508 int dy1=y, dy2=y+h-1;
511 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
512 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
513 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
514 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
515 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
516 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
518 if ( sx < 0 ) return;
519 if ( sy < 0 ) return;
520 if ( sx >= w ) return;
521 if ( sy >= h ) return;
523 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
525 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
528 static void opengl_scanline(int x1,int x2,int y)
533 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
537 saved_zbuf = gr_zbuffer_get();
538 gr_zbuffer_set(GR_ZBUFF_NONE);
541 glColor4ub(r, g, b, a);
550 glVertex2f(x+w, y+h);
556 gr_zbuffer_set(saved_zbuf);
560 void gr_opengl_rect(int x,int y,int w,int h)
562 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);
565 void gr_opengl_shade(int x,int y,int w,int h)
572 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
573 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
574 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
575 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
576 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
577 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
578 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
579 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
581 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
584 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
589 if ( !gr_screen.current_color.is_alphacolor ) return;
591 float u_scale, v_scale;
593 // gr_d3d_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
595 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
596 // Couldn't set texture
597 //mprintf(( "GLIDE: Error setting aabitmap texture!\n" ));
601 // LPD3DTLVERTEX src_v;
602 // D3DTLVERTEX d3d_verts[4];
604 float u0, u1, v0, v1;
605 float x1, x2, y1, y2;
608 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
610 u0 = u_scale*i2fl(sx)/i2fl(bw);
611 v0 = v_scale*i2fl(sy)/i2fl(bh);
613 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
614 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
616 x1 = i2fl(x+gr_screen.offset_x);
617 y1 = i2fl(y+gr_screen.offset_y);
618 x2 = i2fl(x+w+gr_screen.offset_x);
619 y2 = i2fl(y+h+gr_screen.offset_y);
621 if ( gr_screen.current_color.is_alphacolor ) {
622 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
624 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
627 glBegin (GL_TRIANGLE_FAN);
628 glTexCoord2f (u0, v0);
629 glVertex3f (x1, y1, 0.99);
631 glTexCoord2f (u1, v0);
632 glVertex3f (x2, y1, 0.99);
634 glTexCoord2f (u1, v1);
635 glVertex3f (x2, y2, 0.99);
637 glTexCoord2f (u0, v1);
638 glVertex3f (x1, y2, 0.99);
642 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
649 int dx1=x, dx2=x+w-1;
650 int dy1=y, dy2=y+h-1;
653 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
658 if ( count > 1 ) Int3();
662 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
663 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
664 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
665 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
666 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
667 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
694 if ( w < 1 ) return; // clipped away!
695 if ( h < 1 ) return; // clipped away!
699 // Make sure clipping algorithm works
703 Assert( w == (dx2-dx1+1) );
704 Assert( h == (dy2-dy1+1) );
707 Assert( sx+w <= bw );
708 Assert( sy+h <= bh );
709 Assert( dx2 >= dx1 );
710 Assert( dy2 >= dy1 );
711 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
712 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
713 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
714 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
717 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
718 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
721 void gr_opengl_aabitmap(int x, int y)
725 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
726 int dx1=x, dx2=x+w-1;
727 int dy1=y, dy2=y+h-1;
730 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
731 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
732 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
733 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
734 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
735 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
737 if ( sx < 0 ) return;
738 if ( sy < 0 ) return;
739 if ( sx >= w ) return;
740 if ( sy >= h ) return;
742 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
743 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
747 void gr_opengl_string( int sx, int sy, char *s )
749 int width, spacing, letter;
752 if ( !Current_font ) {
756 gr_set_bitmap(Current_font->bitmap_id);
761 if (sx==0x8000) { //centered
762 x = get_centered_x(s);
774 y += Current_font->h;
775 if (sx==0x8000) { //centered
776 x = get_centered_x(s);
783 letter = get_char_width(s[0],s[1],&width,&spacing);
786 //not in font, draw as space
794 // Check if this character is totally clipped
795 if ( x + width < gr_screen.clip_left ) continue;
796 if ( y + Current_font->h < gr_screen.clip_top ) continue;
797 if ( x > gr_screen.clip_right ) continue;
798 if ( y > gr_screen.clip_bottom ) continue;
801 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
802 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
806 wc = width - xd; hc = Current_font->h - yd;
807 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
808 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
810 if ( wc < 1 ) continue;
811 if ( hc < 1 ) continue;
815 ch = &Current_font->char_data[letter];
817 int u = Current_font->bm_u[letter];
818 int v = Current_font->bm_v[letter];
820 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
824 void gr_opengl_circle( int xc, int yc, int d )
834 if ( (xc+r) < gr_screen.clip_left ) return;
835 if ( (xc-r) > gr_screen.clip_right ) return;
836 if ( (yc+r) < gr_screen.clip_top ) return;
837 if ( (yc-r) > gr_screen.clip_bottom ) return;
840 // Draw the first octant
841 opengl_scanline( xc-y, xc+y, yc-x );
842 opengl_scanline( xc-y, xc+y, yc+x );
847 // Draw the second octant
848 opengl_scanline( xc-x, xc+x, yc-y );
849 opengl_scanline( xc-x, xc+x, yc+y );
856 opengl_scanline( xc-x, xc+x, yc-y );
857 opengl_scanline( xc-x, xc+x, yc+y );
863 void gr_opengl_line(int x1,int y1,int x2,int y2)
865 int clipped = 0, swapped=0;
867 glDisable ( GL_DEPTH_TEST );
868 glEnable ( GL_BLEND );
869 glBlendFunc ( GL_SRC_ALPHA, GL_DST_ALPHA );
871 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);
874 glColor4f (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
875 glVertex3f (i2fl (x1+gr_screen.offset_x),i2fl (y1+gr_screen.offset_y), 0.99f);
876 glVertex3f (i2fl (x2+gr_screen.offset_x),i2fl (y2+gr_screen.offset_y), 0.99f);
880 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
882 void gr_opengl_scaler(vertex *va, vertex *vb )
884 float x0, y0, x1, y1;
885 float u0, v0, u1, v1;
886 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
887 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
888 float xmin, xmax, ymin, ymax;
889 int dx0, dy0, dx1, dy1;
891 //============= CLIP IT =====================
893 x0 = va->sx; y0 = va->sy;
894 x1 = vb->sx; y1 = vb->sy;
896 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
897 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
899 u0 = va->u; v0 = va->v;
900 u1 = vb->u; v1 = vb->v;
902 // Check for obviously offscreen bitmaps...
903 if ( (y1<=y0) || (x1<=x0) ) return;
904 if ( (x1<xmin ) || (x0>xmax) ) return;
905 if ( (y1<ymin ) || (y0>ymax) ) return;
907 clipped_u0 = u0; clipped_v0 = v0;
908 clipped_u1 = u1; clipped_v1 = v1;
910 clipped_x0 = x0; clipped_y0 = y0;
911 clipped_x1 = x1; clipped_y1 = y1;
913 // Clip the left, moving u0 right as necessary
915 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
919 // Clip the right, moving u1 left as necessary
921 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
925 // Clip the top, moving v0 down as necessary
927 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
931 // Clip the bottom, moving v1 up as necessary
933 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
937 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
938 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
940 if (dx1<=dx0) return;
941 if (dy1<=dy0) return;
943 //============= DRAW IT =====================
951 tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
952 if ( fl_abs(tmpu) < 0.001f ) {
953 return; // scaled up way too far!
955 tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
956 if ( fl_abs(tmpv) < 0.001f ) {
957 return; // scaled up way too far!
960 bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
962 du = fl2f(tmpu*(bp->w-1));
963 dv = fl2f(tmpv*(bp->h-1));
965 v = fl2f(clipped_v0*(bp->h-1));
966 u = fl2f(clipped_u0*(bp->w-1));
969 spixels = (ubyte *)bp->data;
971 for (y=dy0; y<=dy1; y++ ) {
972 sbits = &spixels[bp->rowsize*(v>>16)];
976 for (x=0; x<w; x++ ) {
977 ubyte c = sbits[ tmp_u >> 16 ];
979 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
980 gr_pixel( x+dx0, y );
987 bm_unlock(gr_screen.current_bitmap);
992 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
995 float u_scale = 1.0f, v_scale = 1.0f;
998 // Make nebula use the texture mapper... this blends the colors better.
999 if ( flags & TMAP_FLAG_NEBULA ){
1004 gr_texture_source texture_source = (gr_texture_source)-1;
1005 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1006 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1008 if ( gr_zbuffering ) {
1009 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1010 zbuffer_type = ZBUFFER_TYPE_READ;
1012 zbuffer_type = ZBUFFER_TYPE_FULL;
1015 zbuffer_type = ZBUFFER_TYPE_NONE;
1021 int tmap_type = TCACHE_TYPE_NORMAL;
1025 if ( flags & TMAP_FLAG_TEXTURED ) {
1028 r = gr_screen.current_color.red;
1029 g = gr_screen.current_color.green;
1030 b = gr_screen.current_color.blue;
1033 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1035 // Some blend function stuff here - DDOI
1038 float factor = gr_screen.current_alpha;
1042 if ( factor <= 1.0f ) {
1043 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1044 r = (r*tmp_alpha)/255;
1045 g = (g*tmp_alpha)/255;
1046 b = (b*tmp_alpha)/255;
1053 if ( flags & TMAP_FLAG_TEXTURED ) {
1054 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 ))
1056 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1060 // use nonfiltered textures for bitmap sections
1062 if(flags & TMAP_FLAG_BITMAP_SECTION){
1063 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1065 texture_source = TEXTURE_SOURCE_DECAL;
1071 x1 = gr_screen.clip_left*16;
1072 x2 = gr_screen.clip_right*16+15;
1073 y1 = gr_screen.clip_top*16;
1074 y2 = gr_screen.clip_bottom*16+15;
1076 float uoffset = 0.0f;
1077 float voffset = 0.0f;
1079 float minu=0.0f, minv=0.0f, maxu=1.0f, maxv=1.0f;
1081 if ( flags & TMAP_FLAG_TEXTURED )
1088 STUB_FUNCTION; // DDOI - this still needs work
1089 glBegin (GL_TRIANGLE_FAN);
1092 if (flags & TMAP_FLAG_ALPHA) a = verts[i]->a;
1095 if (flags & TMAP_FLAG_NEBULA ) {
1097 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1098 r = gr_palette[pal*3+0];
1099 g = gr_palette[pal*3+1];
1100 b = gr_palette[pal*3+2];
1102 }else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1103 r = Gr_gamma_lookup[verts[i]->b];
1104 g = Gr_gamma_lookup[verts[i]->b];
1105 b = Gr_gamma_lookup[verts[i]->b];
1106 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1107 // Make 0.75 be 256.0f
1108 r = Gr_gamma_lookup[verts[i]->r];
1109 g = Gr_gamma_lookup[verts[i]->g];
1110 b = Gr_gamma_lookup[verts[i]->b];
1112 // use constant RGB values...
1115 glColor4i (r,g,b,a);
1117 // DDOI - FIXME fog stuff
1118 // DDOI - FIXME TexCoord stuff
1119 glVertex3f (verts[i]->sx+gr_screen.offset_x,
1120 verts[i]->sy+gr_screen.offset_y,
1126 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1131 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1136 void gr_opengl_get_color( int * r, int * g, int * b )
1138 if (r) *r = gr_screen.current_color.red;
1139 if (g) *g = gr_screen.current_color.green;
1140 if (b) *b = gr_screen.current_color.blue;
1143 void gr_opengl_init_color(color *c, int r, int g, int b)
1145 c->screen_sig = gr_screen.signature;
1146 c->red = (unsigned char)r;
1147 c->green = (unsigned char)g;
1148 c->blue = (unsigned char)b;
1150 c->ac_type = AC_TYPE_NONE;
1152 c->is_alphacolor = 0;
1156 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1158 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1159 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1160 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1161 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1163 gr_opengl_init_color( clr, r, g, b );
1165 clr->alpha = (unsigned char)alpha;
1166 clr->ac_type = (ubyte)type;
1167 clr->alphacolor = -1;
1168 clr->is_alphacolor = 1;
1171 void gr_opengl_set_color( int r, int g, int b )
1173 Assert((r >= 0) && (r < 256));
1174 Assert((g >= 0) && (g < 256));
1175 Assert((b >= 0) && (b < 256));
1177 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1180 void gr_opengl_set_color_fast(color *dst)
1182 if ( dst->screen_sig != gr_screen.signature ) {
1183 if ( dst->is_alphacolor ) {
1184 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1186 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1189 gr_screen.current_color = *dst;
1192 void gr_opengl_print_screen(char *filename)
1197 int gr_opengl_supports_res_ingame(int res)
1204 int gr_opengl_supports_res_interface(int res)
1211 void opengl_tcache_cleanup ();
1212 void gr_opengl_cleanup()
1214 if ( !Inited ) return;
1221 opengl_tcache_cleanup ();
1226 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
1231 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1236 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
1241 void gr_opengl_set_cull(int cull)
1246 void gr_opengl_filter_set(int filter)
1252 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1258 typedef struct tcache_slot_opengl {
1259 GLuint texture_handle;
1260 float u_scale, v_scale;
1263 char used_this_frame;
1268 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1269 tcache_slot_opengl *parent;
1270 } tcache_slot_opengl;
1272 static void *Texture_sections = NULL;
1273 tcache_slot_opengl *Textures = NULL;
1275 int GL_texture_sections = 0;
1276 int GL_texture_ram = 0;
1277 int GL_frame_count = 0;
1278 int GL_min_texture_width = 0;
1279 int GL_max_texture_width = 0;
1280 int GL_min_texture_height = 0;
1281 int GL_max_texture_height = 0;
1282 int GL_square_textures = 0;
1283 int GL_pow2_textures = 0;
1284 int GL_textures_in = 0;
1285 int GL_textures_in_frame = 0;
1286 int GL_last_bitmap_id = -1;
1287 int GL_last_detail = -1;
1288 int GL_last_bitmap_type = -1;
1289 int GL_last_section_x = -1;
1290 int GL_last_section_y = -1;
1294 void opengl_tcache_init (int use_sections)
1298 // DDOI - FIXME skipped a lot of stuff here
1301 GL_min_texture_width = 16;
1302 GL_min_texture_height = 16;
1303 GL_max_texture_width = 256;
1304 GL_max_texture_height = 256;
1306 GL_square_textures = 1;
1308 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1314 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1315 if(!Texture_sections){
1318 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1321 // Init the texture structures
1322 int section_count = 0;
1323 for( i=0; i<MAX_BITMAPS; i++ ) {
1325 Textures[i].vram_texture = NULL;
1326 Textures[i].vram_texture_surface = NULL;
1328 Textures[i].texture_handle = 0;
1330 Textures[i].bitmap_id = -1;
1331 Textures[i].size = 0;
1332 Textures[i].used_this_frame = 0;
1334 Textures[i].parent = NULL;
1336 // allocate sections
1338 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1339 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1340 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1341 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1343 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1344 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1346 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1347 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1348 Textures[i].data_sections[idx][s_idx]->size = 0;
1349 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1353 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1354 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1355 Textures[i].data_sections[idx][s_idx] = NULL;
1361 GL_texture_sections = use_sections;
1363 //GL_last_detail = Detail.hardware_textures;
1364 GL_last_bitmap_id = -1;
1365 GL_last_bitmap_type = -1;
1367 GL_last_section_x = -1;
1368 GL_last_section_y = -1;
1371 GL_textures_in_frame = 0;
1374 int opengl_free_texture (tcache_slot_opengl *t);
1376 void opengl_tcache_flush ()
1380 for( i=0; i<MAX_BITMAPS; i++ ) {
1381 opengl_free_texture ( &Textures[i] );
1383 if (GL_textures_in != 0) {
1384 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1388 GL_last_bitmap_id = -1;
1389 GL_last_section_x = -1;
1390 GL_last_section_y = -1;
1393 void opengl_tcache_cleanup ()
1395 opengl_tcache_flush ();
1398 GL_textures_in_frame = 0;
1405 if( Texture_sections != NULL ){
1406 free(Texture_sections);
1407 Texture_sections = NULL;
1411 void opengl_tcache_frame ()
1415 GL_last_bitmap_id = -1;
1416 GL_textures_in_frame = 0;
1421 for( i=0; i<MAX_BITMAPS; i++ ) {
1422 Textures[i].used_this_frame = 0;
1425 if(Textures[i].data_sections[0][0] != NULL){
1426 Assert(GL_texture_sections);
1427 if(GL_texture_sections){
1428 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1429 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1430 if(Textures[i].data_sections[idx][s_idx] != NULL){
1431 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1440 opengl_tcache_flush();
1445 int opengl_free_texture ( tcache_slot_opengl *t )
1451 if ( t->bitmap_id > -1 ) {
1452 // if I, or any of my children have been used this frame, bail
1453 if(t->used_this_frame){
1456 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1457 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1458 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
1464 // ok, now we know its legal to free everything safely
1465 glDeleteTextures (1, &t->texture_handle);
1466 t->texture_handle = 0;
1468 if ( GL_last_bitmap_id == t->bitmap_id ) {
1469 GL_last_bitmap_id = -1;
1472 // if this guy has children, free them too, since the children
1473 // actually make up his size
1474 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1475 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1476 if(t->data_sections[idx][s_idx] != NULL){
1477 opengl_free_texture(t->data_sections[idx][s_idx]);
1483 t->used_this_frame = 0;
1484 GL_textures_in -= t->size;
1490 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1495 if((w_out == NULL) || (h_out == NULL)){
1505 if ( D3D_pow2_textures ) {
1507 for (i=0; i<16; i++ ) {
1508 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
1514 for (i=0; i<16; i++ ) {
1515 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
1523 if ( tex_w < GL_min_texture_width ) {
1524 tex_w = GL_min_texture_width;
1525 } else if ( tex_w > GL_max_texture_width ) {
1526 tex_w = GL_max_texture_width;
1529 if ( tex_h < GL_min_texture_height ) {
1530 tex_h = GL_min_texture_height;
1531 } else if ( tex_h > GL_max_texture_height ) {
1532 tex_h = GL_max_texture_height;
1535 if ( GL_square_textures ) {
1537 // Make the both be equal to larger of the two
1538 new_size = max(tex_w, tex_h);
1543 // store the outgoing size
1548 // data == start of bitmap data
1549 // sx == x offset into bitmap
1550 // sy == y offset into bitmap
1551 // src_w == absolute width of section on source bitmap
1552 // src_h == absolute height of section on source bitmap
1553 // bmap_w == width of source bitmap
1554 // bmap_h == height of source bitmap
1555 // tex_w == width of final texture
1556 // tex_h == height of final texture
1557 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)
1566 if ( t->used_this_frame ) {
1567 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
1572 if(!opengl_free_texture(t)){
1577 // get final texture size
1578 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
1580 if ( (tex_w < 1) || (tex_h < 1) ) {
1581 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
1585 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
1586 t->u_scale = (float)bmap_w / (float)tex_w;
1587 t->v_scale = (float)bmap_h / (float)tex_h;
1588 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1589 t->u_scale = (float)src_w / (float)tex_w;
1590 t->v_scale = (float)src_h / (float)tex_h;
1596 ushort *bmp_data = (ushort *)data;
1597 ubyte *bmp_data_byte = (ubyte*)data;
1599 glGenTextures (1, &t->texture_handle);
1600 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
1602 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1603 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1604 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1605 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1610 ushort *texmem = (ushort *)malloc (tex_w*tex_h*2);
1611 switch (bitmap_type) {
1613 case TCACHE_TYPE_AABITMAP:
1614 for (i=0; i<16;i++) {
1618 a = Gr_gamma_lookup[(i*255)/15];
1620 r /= Gr_ta_red.scale;
1621 g /= Gr_ta_green.scale;
1622 b /= Gr_ta_blue.scale;
1623 a /= Gr_ta_alpha.scale;
1625 xlat[i] = (unsigned short)(((a<<Gr_ta_alpha.shift) | (r << Gr_ta_red.shift) | (g << Gr_ta_green.shift) | (b << Gr_ta_blue.shift)));
1629 for ( ; i<256; i++ ) {
1633 for (j = 0; j < tex_h; j++) {
1634 for (i = 0; i < tex_w; i++) {
1635 if ( (i < bmap_w) && (j<bmap_h) ) {
1636 *texmem++ = xlat[(ubyte)bmp_data_byte[j*bmap_w+i]];
1643 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGBA,
1644 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
1648 fix u, utmp, v, du, dv;
1652 du = ( (bmap_w-1)*F1_0 ) / tex_w;
1653 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
1655 for (j = 0; j < tex_h; j++) {
1658 for (i = 0; i < tex_w; i++) {
1659 *texmem++ = bmp_data[f2i(v)*bmap_w+f2i(utmp)];
1665 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGBA,
1666 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
1671 t->bitmap_id = texture_handle;
1672 t->time_created = GL_frame_count;
1673 t->used_this_frame = 0;
1674 t->size = tex_w * tex_h * 2;
1675 t->w = (ushort)tex_w;
1676 t->h = (ushort)tex_h;
1677 GL_textures_in_frame += t->size;
1679 GL_textures_in += t->size;
1687 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
1691 int final_w, final_h;
1695 // setup texture/bitmap flags
1697 switch(bitmap_type){
1698 case TCACHE_TYPE_AABITMAP:
1699 flags |= BMP_AABITMAP;
1702 case TCACHE_TYPE_NORMAL:
1703 flags |= BMP_TEX_OTHER;
1704 case TCACHE_TYPE_XPARENT:
1705 flags |= BMP_TEX_XPARENT;
1707 case TCACHE_TYPE_NONDARKENING:
1709 flags |= BMP_TEX_NONDARK;
1713 // lock the bitmap into the proper format
1714 bmp = bm_lock(bitmap_handle, bpp, flags);
1715 if ( bmp == NULL ) {
1716 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1725 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
1726 max_w /= D3D_texture_divider;
1727 max_h /= D3D_texture_divider;
1729 // Detail.debris_culling goes from 0 to 4.
1730 max_w /= 16 >> Detail.hardware_textures;
1731 max_h /= 16 >> Detail.hardware_textures;
1735 // get final texture size as it will be allocated as a DD surface
1736 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
1738 // if this tcache slot has no bitmap
1739 if ( tslot->bitmap_id < 0) {
1742 // different bitmap altogether - determine if the new one can use the old one's slot
1743 else if (tslot->bitmap_id != bitmap_handle) {
1744 if((final_w == tslot->w) && (final_h == tslot->h)){
1746 //ml_printf("Reloading texture %d\n", bitmap_handle);
1753 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);
1755 // unlock the bitmap
1756 bm_unlock(bitmap_handle);
1761 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
1765 int final_w, final_h;
1766 int section_x, section_y;
1769 // setup texture/bitmap flags
1770 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
1771 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
1772 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
1774 flags = BMP_TEX_XPARENT;
1776 // lock the bitmap in the proper format
1777 bmp = bm_lock(bitmap_handle, 16, flags);
1778 if ( bmp == NULL ) {
1779 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1782 // determine the width and height of this section
1783 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
1785 // get final texture size as it will be allocated as a DD surface
1786 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
1788 // if this tcache slot has no bitmap
1789 if ( tslot->bitmap_id < 0) {
1792 // different bitmap altogether - determine if the new one can use the old one's slot
1793 else if (tslot->bitmap_id != bitmap_handle) {
1794 if((final_w == tslot->w) && (final_h == tslot->h)){
1802 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);
1804 // unlock the bitmap
1805 bm_unlock(bitmap_handle);
1811 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
1812 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)
1821 GL_last_bitmap_id = -1;
1826 if ( GL_last_detail != Detail.hardware_textures ) {
1827 GL_last_detail = Detail.hardware_textures;
1828 opengl_tcache_flush();
1836 int n = bm_get_cache_slot (bitmap_id, 1);
1837 tcache_slot_opengl *t = &Textures[n];
1839 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)) {
1840 t->used_this_frame++;
1842 // mark all children as used
1843 if(GL_texture_sections){
1844 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1845 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1846 if(t->data_sections[idx][s_idx] != NULL){
1847 t->data_sections[idx][s_idx]->used_this_frame++;
1853 *u_scale = t->u_scale;
1854 *v_scale = t->v_scale;
1858 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1859 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
1860 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
1866 // if the texture sections haven't been created yet
1867 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
1869 // lock the bitmap in the proper format
1870 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
1871 bm_unlock(bitmap_id);
1873 // now lets do something for each texture
1875 for(idx=0; idx<bmp->sections.num_x; idx++){
1876 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
1877 // hmm. i'd rather we didn't have to do it this way...
1878 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
1882 // not used this frame
1883 t->data_sections[idx][s_idx]->used_this_frame = 0;
1887 // zero out pretty much everything in the parent struct since he's just the root
1888 t->bitmap_id = bitmap_id;
1889 t->texture_handle = 0;
1890 t->time_created = t->data_sections[sx][sy]->time_created;
1891 t->used_this_frame = 0;
1893 t->vram_texture = NULL;
1894 t->vram_texture_surface = NULL
1898 // argh. we failed to upload. free anything we can
1900 opengl_free_texture(t);
1902 // swap in the texture we want
1904 t = t->data_sections[sx][sy];
1907 // all other "normal" textures
1908 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
1909 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
1912 // everything went ok
1913 if(ret_val && (t->texture_handle) && !vram_full){
1914 *u_scale = t->u_scale;
1915 *v_scale = t->v_scale;
1917 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
1919 GL_last_bitmap_id = t->bitmap_id;
1920 GL_last_bitmap_type = bitmap_type;
1921 GL_last_section_x = sx;
1922 GL_last_section_y = sy;
1924 t->used_this_frame++;
1928 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
1935 void gr_opengl_set_clear_color(int r, int g, int b)
1940 void gr_opengl_aaline(vertex *v1, vertex *v2)
1945 void gr_opengl_flash(int r, int g, int b)
1950 int gr_opengl_zbuffer_get()
1954 return GR_ZBUFF_NONE;
1957 int gr_opengl_zbuffer_set(int mode)
1961 return GR_ZBUFF_NONE;
1964 void gr_opengl_zbuffer_clear(int mode)
1969 void gr_opengl_set_gamma(float gamma)
1974 void gr_opengl_fade_in(int instantaneous)
1979 void gr_opengl_fade_out(int instantaneous)
1984 int gr_opengl_save_screen()
1991 void gr_opengl_restore_screen(int id)
1996 void gr_opengl_free_screen(int id)
2001 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2006 void gr_opengl_dump_frame_stop()
2011 void gr_opengl_dump_frame()
2016 uint gr_opengl_lock()
2023 void gr_opengl_unlock()
2027 void gr_opengl_init()
2030 gr_opengl_cleanup();
2034 mprintf(( "Initializing opengl graphics device...\n" ));
2038 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2040 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2046 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2047 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2048 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2049 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2050 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2052 if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
2054 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2058 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2060 glMatrixMode(GL_PROJECTION);
2062 glMatrixMode(GL_MODELVIEW);
2065 glEnable(GL_TEXTURE_2D);
2067 glGenTextures(1, &bitmapTex);
2068 glBindTexture(GL_TEXTURE_2D, bitmapTex);
2069 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2070 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2071 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2073 glMatrixMode(GL_PROJECTION);
2077 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, -1.0, 1.0);
2079 bitmapMem = (GLubyte *)malloc(256*256*4);
2080 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, bitmapMem);
2081 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_CHAR, bitmapMem);
2092 Gr_red.mask = 0xff0000;
2097 Gr_green.mask = 0xff00;
2102 Gr_blue.mask = 0xff;
2110 Gr_red.mask = 0x7C00;
2115 Gr_green.mask = 0x3E0;
2120 Gr_blue.mask = 0x1F;
2128 Gr_red.mask = 0xF800;
2133 Gr_green.mask = 0x7E0;
2138 Gr_blue.mask = 0x1F;
2147 Gr_red.mask = 0xff0000;
2152 Gr_green.mask = 0xff00;
2157 Gr_blue.mask = 0xff;
2162 Int3(); // Illegal bpp
2166 opengl_tcache_init (0);
2169 Gr_current_red = &Gr_red;
2170 Gr_current_blue = &Gr_blue;
2171 Gr_current_green = &Gr_green;
2172 Gr_current_alpha = &Gr_alpha;
2174 gr_screen.gf_flip = gr_opengl_flip;
2175 gr_screen.gf_flip_window = gr_opengl_flip_window;
2176 gr_screen.gf_set_clip = gr_opengl_set_clip;
2177 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2178 gr_screen.gf_set_font = grx_set_font;
2180 gr_screen.gf_set_color = gr_opengl_set_color;
2181 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2182 gr_screen.gf_create_shader = gr_opengl_create_shader;
2183 gr_screen.gf_set_shader = gr_opengl_set_shader;
2184 gr_screen.gf_clear = gr_opengl_clear;
2185 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2186 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2187 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2188 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2190 gr_screen.gf_rect = gr_opengl_rect;
2191 gr_screen.gf_shade = gr_opengl_shade;
2192 gr_screen.gf_string = gr_opengl_string;
2193 gr_screen.gf_circle = gr_opengl_circle;
2195 gr_screen.gf_line = gr_opengl_line;
2196 gr_screen.gf_aaline = gr_opengl_aaline;
2197 gr_screen.gf_pixel = gr_opengl_pixel;
2198 gr_screen.gf_scaler = gr_opengl_scaler;
2199 gr_screen.gf_tmapper = gr_opengl_tmapper;
2201 gr_screen.gf_gradient = gr_opengl_gradient;
2203 gr_screen.gf_set_palette = gr_opengl_set_palette;
2204 gr_screen.gf_get_color = gr_opengl_get_color;
2205 gr_screen.gf_init_color = gr_opengl_init_color;
2206 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
2207 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
2208 gr_screen.gf_print_screen = gr_opengl_print_screen;
2210 gr_screen.gf_fade_in = gr_opengl_fade_in;
2211 gr_screen.gf_fade_out = gr_opengl_fade_out;
2212 gr_screen.gf_flash = gr_opengl_flash;
2214 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
2215 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
2216 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
2218 gr_screen.gf_save_screen = gr_opengl_save_screen;
2219 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
2220 gr_screen.gf_free_screen = gr_opengl_free_screen;
2222 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
2223 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
2224 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
2226 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
2228 gr_screen.gf_lock = gr_opengl_lock;
2229 gr_screen.gf_unlock = gr_opengl_unlock;
2231 gr_screen.gf_fog_set = gr_opengl_fog_set;
2233 gr_screen.gf_get_region = gr_opengl_get_region;
2235 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
2237 gr_screen.gf_set_cull = gr_opengl_set_cull;
2239 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
2241 gr_screen.gf_filter_set = gr_opengl_filter_set;
2243 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
2245 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;