2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.36 2002/05/31 23:25:03 relnev
13 * Revision 1.34 2002/05/31 22:15:22 relnev
16 * Revision 1.33 2002/05/31 22:04:55 relnev
17 * use d3d rect_internal
19 * Revision 1.32 2002/05/31 06:28:23 relnev
22 * Revision 1.31 2002/05/31 06:04:39 relnev
25 * Revision 1.30 2002/05/31 03:56:11 theoddone33
26 * Change tmapper polygon winding and enable culling
28 * Revision 1.29 2002/05/31 03:34:02 theoddone33
32 * Revision 1.28 2002/05/31 00:06:59 relnev
35 * Revision 1.27 2002/05/30 23:46:29 theoddone33
36 * some minor key changes (not necessarily fixes)
38 * Revision 1.26 2002/05/30 23:33:12 relnev
39 * implemented a few more functions.
41 * Revision 1.25 2002/05/30 23:01:16 relnev
42 * implement gr_opengl_set_state.
44 * Revision 1.24 2002/05/30 22:12:57 relnev
45 * finish default texture case
47 * Revision 1.23 2002/05/30 22:02:30 theoddone33
50 * Revision 1.22 2002/05/30 21:44:48 relnev
51 * implemented some missing texture stuff.
53 * enable bitmap polys for opengl.
55 * work around greenness in bitmaps.
57 * Revision 1.21 2002/05/30 17:29:30 theoddone33
58 * Fix some more stubs, change at least one polygon winding since culling is now
61 * Revision 1.20 2002/05/30 16:50:24 theoddone33
62 * Keyboard partially fixed
64 * Revision 1.19 2002/05/30 08:13:14 relnev
67 * Revision 1.18 2002/05/29 23:37:36 relnev
70 * Revision 1.17 2002/05/29 23:17:49 theoddone33
71 * Non working text code and fixed keys
73 * Revision 1.16 2002/05/29 19:45:13 theoddone33
74 * More changes on texture loading
76 * Revision 1.15 2002/05/29 19:06:48 theoddone33
77 * Enable string printing. Enable texture mapping
79 * Revision 1.14 2002/05/29 08:54:40 relnev
80 * "fixed" bitmap drawing.
82 * copied more d3d code over.
84 * Revision 1.13 2002/05/29 06:25:13 theoddone33
85 * Keyboard input, mouse tracking now work
87 * Revision 1.12 2002/05/29 04:52:45 relnev
90 * Revision 1.11 2002/05/29 04:29:56 relnev
91 * removed some unncessary stubbing, implemented opengl rect
93 * Revision 1.10 2002/05/29 04:13:27 theoddone33
96 * Revision 1.9 2002/05/29 03:35:51 relnev
99 * Revision 1.8 2002/05/29 03:30:05 relnev
100 * update opengl stubs
102 * Revision 1.7 2002/05/29 02:52:32 theoddone33
103 * Enable OpenGL renderer
105 * Revision 1.6 2002/05/28 04:56:51 theoddone33
106 * runs a little bit now
108 * Revision 1.5 2002/05/28 04:07:28 theoddone33
109 * New graphics stubbing arrangement
111 * Revision 1.4 2002/05/27 23:39:34 relnev
114 * Revision 1.3 2002/05/27 22:35:01 theoddone33
117 * Revision 1.2 2002/05/27 22:32:02 theoddone33
118 * throw all d3d stuff at opengl
120 * Revision 1.1.1.1 2002/05/03 03:28:09 root
124 * 10 7/14/99 9:42a Dave
125 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
128 * 9 7/09/99 9:51a Dave
129 * Added thick polyline code.
131 * 8 6/29/99 10:35a Dave
132 * Interface polygon bitmaps! Whee!
134 * 7 2/03/99 11:44a Dave
135 * Fixed d3d transparent textures.
137 * 6 1/24/99 11:37p Dave
138 * First full rev of beam weapons. Very customizable. Removed some bogus
139 * Int3()'s in low level net code.
141 * 5 12/18/98 1:13a Dave
142 * Rough 1024x768 support for Direct3D. Proper detection and usage through
145 * 4 12/06/98 2:36p Dave
146 * Drastically improved nebula fogging.
148 * 3 11/11/98 5:37p Dave
149 * Checkin for multiplayer testing.
151 * 2 10/07/98 10:53a Dave
154 * 1 10/07/98 10:49a Dave
156 * 14 5/20/98 9:46p John
157 * added code so the places in code that change half the palette don't
158 * have to clear the screen.
160 * 13 5/06/98 5:30p John
161 * Removed unused cfilearchiver. Removed/replaced some unused/little used
162 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
163 * DirectX header files and libs that fixed the Direct3D alpha blending
166 * 12 4/14/98 12:15p John
167 * Made 16-bpp movies work.
169 * 11 3/12/98 5:36p John
170 * Took out any unused shaders. Made shader code take rgbc instead of
171 * matrix and vector since noone used it like a matrix and it would have
172 * been impossible to do in hardware. Made Glide implement a basic
173 * shader for online help.
175 * 10 3/10/98 4:18p John
176 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
177 * & Glide have popups and print screen. Took out all >8bpp software
178 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
179 * support Fred. Made zbuffering key off of functions rather than one
182 * 9 12/02/97 4:00p John
183 * Added first rev of thruster glow, along with variable levels of
184 * translucency, which retquired some restructing of palman.
186 * 8 10/03/97 9:10a John
187 * added better antialiased line drawer
189 * 7 9/23/97 10:45a John
190 * made so you can tell bitblt code to rle a bitmap by passing flag to
193 * 6 9/09/97 11:01a Sandeep
194 * fixed warning level 4 bugs
196 * 5 7/10/97 2:06p John
197 * added code to specify alphablending type for bitmaps.
199 * 4 6/17/97 7:04p John
200 * added d3d support for gradients.
201 * fixed some color bugs by adding screen signatures instead of watching
202 * flags and palette changes.
204 * 3 6/12/97 2:50a Lawrance
205 * bm_unlock() now passed bitmap number, not pointer
207 * 2 6/11/97 1:12p John
208 * Started fixing all the text colors in the game.
210 * 1 5/12/97 12:14p John
217 #include <windowsx.h>
226 #include "floating.h"
228 #include "grinternal.h"
229 #include "gropengl.h"
233 static int Inited = 0;
235 //static GLuint bitmapTex;
236 //static GLubyte *bitmapMem;
238 typedef enum gr_texture_source {
240 TEXTURE_SOURCE_DECAL,
241 TEXTURE_SOURCE_NO_FILTERING,
244 typedef enum gr_alpha_blend {
245 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
246 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
247 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
248 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
251 typedef enum gr_zbuffer_type {
258 float z_mult = 30000.0f;
259 #define NEBULA_COLORS 20
261 static char *Gr_saved_screen = NULL;
264 // Throw in some dummy functions - DDOI
266 int D3D_32bit = 0; // grd3d.cpp
267 int D3D_fog_mode = -1; // grd3d.cpp
268 int D3D_inited = 0; // grd3d.cpp
269 int D3D_zbias = 1; // grd3d.cpp
270 int D3d_rendition_uvs = 0; // grd3d.cpp
272 void gr_dd_activate(int active) // grdirectdraw.cpp
277 void gr_directdraw_cleanup() // grdirectdraw.cpp
282 void gr_directdraw_force_windowed() // grdirectdraw.cpp
287 void gr_directdraw_init()
292 extern int gr_opengl_preload (int x, int y);
293 int gr_d3d_preload (int x, int y)
295 return gr_opengl_preload(x, y);
298 void d3d_start_frame()
303 void d3d_stop_frame()
313 void d3d_zbias (int a)
319 void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
322 case TEXTURE_SOURCE_NONE:
323 glBindTexture(GL_TEXTURE_2D, 0);
324 gr_tcache_set(-1, -1, NULL, NULL );
326 case TEXTURE_SOURCE_DECAL:
327 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
328 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
329 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
331 case TEXTURE_SOURCE_NO_FILTERING:
332 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
333 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
334 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
341 case ALPHA_BLEND_NONE:
342 glBlendFunc(GL_ONE, GL_ZERO);
344 case ALPHA_BLEND_ALPHA_ADDITIVE:
345 glBlendFunc(GL_ONE, GL_ONE);
347 case ALPHA_BLEND_ALPHA_BLEND_ALPHA:
348 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
350 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR:
351 glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
358 case ZBUFFER_TYPE_NONE:
359 glDepthFunc(GL_ALWAYS);
360 glDepthMask(GL_FALSE);
362 case ZBUFFER_TYPE_READ:
363 glDepthFunc(GL_LESS);
364 glDepthMask(GL_FALSE);
366 case ZBUFFER_TYPE_WRITE:
367 glDepthFunc(GL_ALWAYS);
368 glDepthMask(GL_TRUE);
370 case ZBUFFER_TYPE_FULL:
371 glDepthFunc(GL_LESS);
372 glDepthMask(GL_TRUE);
379 void gr_opengl_activate(int b)
385 void opengl_tcache_flush ();
387 void gr_opengl_preload_init()
389 if (gr_screen.mode != GR_OPENGL) {
393 opengl_tcache_flush ();
396 int GL_should_preload = 0;
397 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
399 if ( gr_screen.mode != GR_OPENGL) {
403 if ( !GL_should_preload ) {
407 float u_scale, v_scale;
411 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
413 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
417 mprintf(("Texture upload failed!\n" ));
423 void gr_opengl_pixel(int x, int y)
428 void gr_opengl_clear()
430 glClearColor(gr_screen.current_clear_color.red / 255.0,
431 gr_screen.current_clear_color.red / 255.0,
432 gr_screen.current_clear_color.red / 255.0, 1.0);
434 glClear ( GL_COLOR_BUFFER_BIT );
437 void opengl_tcache_frame ();
438 void gr_opengl_flip()
443 GLenum error = glGetError();
446 error = glGetError();
448 if (error != GL_NO_ERROR) {
449 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
452 } while (error != GL_NO_ERROR);
455 SDL_GL_SwapBuffers ();
457 opengl_tcache_frame ();
460 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
465 void gr_opengl_set_clip(int x,int y,int w,int h)
467 // check for sanity of parameters
473 if (x >= gr_screen.max_w)
474 x = gr_screen.max_w - 1;
475 if (y >= gr_screen.max_h)
476 y = gr_screen.max_h - 1;
478 if (x + w > gr_screen.max_w)
479 w = gr_screen.max_w - x;
480 if (y + h > gr_screen.max_h)
481 h = gr_screen.max_h - y;
483 if (w > gr_screen.max_w)
485 if (h > gr_screen.max_h)
488 gr_screen.offset_x = x;
489 gr_screen.offset_y = y;
490 gr_screen.clip_left = 0;
491 gr_screen.clip_right = w-1;
492 gr_screen.clip_top = 0;
493 gr_screen.clip_bottom = h-1;
494 gr_screen.clip_width = w;
495 gr_screen.clip_height = h;
497 // glViewport(x, y, w, h);
500 void gr_opengl_reset_clip()
502 gr_screen.offset_x = 0;
503 gr_screen.offset_y = 0;
504 gr_screen.clip_left = 0;
505 gr_screen.clip_top = 0;
506 gr_screen.clip_right = gr_screen.max_w - 1;
507 gr_screen.clip_bottom = gr_screen.max_h - 1;
508 gr_screen.clip_width = gr_screen.max_w;
509 gr_screen.clip_height = gr_screen.max_h;
511 // glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
514 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
516 gr_screen.current_alpha = alpha;
517 gr_screen.current_alphablend_mode = alphablend_mode;
518 gr_screen.current_bitblt_mode = bitblt_mode;
519 gr_screen.current_bitmap = bitmap_num;
521 gr_screen.current_bitmap_sx = sx;
522 gr_screen.current_bitmap_sy = sy;
525 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
527 shade->screen_sig = gr_screen.signature;
534 void gr_opengl_set_shader( shader * shade )
537 if (shade->screen_sig != gr_screen.signature) {
538 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
540 gr_screen.current_shader = *shade;
542 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
547 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
549 STUB_FUNCTION; /* who called me? */
552 extern int GL_last_bitmap_id;
553 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
557 GLubyte *sptr, *dptr;
561 int cw = min(bmp->w, w);
562 int ch = min(bmp->h, h);
564 GL_last_bitmap_id = -1; /* HACK! */
566 glColor4f(1.0, 1.0, 1.0, 1.0);
567 glBindTexture(GL_TEXTURE_2D, bitmapTex);
570 for (iy = sy; iy < ch; iy += 256) {
572 ih = min(256, (ch-iy));
574 for (ix = sx; ix < cw; ix += 256) {
576 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
578 iw = min(256, (cw-ix));
584 memcpy(dptr, sptr, iw*2);
592 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
602 t = (float)ih / 256.0;
607 s = (float)iw / 256.0;
608 t = (float)ih / 256.0;
613 s = (float)iw / 256.0;
625 bm_unlock(gr_screen.current_bitmap);
630 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
637 int dx1=x, dx2=x+w-1;
638 int dy1=y, dy2=y+h-1;
641 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
646 if ( count > 1 ) Int3();
650 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
651 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
652 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
653 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
654 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
655 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
682 if ( w < 1 ) return; // clipped away!
683 if ( h < 1 ) return; // clipped away!
687 // Make sure clipping algorithm works
691 Assert( w == (dx2-dx1+1) );
692 Assert( h == (dy2-dy1+1) );
695 Assert( sx+w <= bw );
696 Assert( sy+h <= bh );
697 Assert( dx2 >= dx1 );
698 Assert( dy2 >= dy1 );
699 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
700 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
701 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
702 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
705 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
706 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
708 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
711 void gr_opengl_bitmap(int x, int y)
715 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
716 int dx1=x, dx2=x+w-1;
717 int dy1=y, dy2=y+h-1;
720 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
721 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
722 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
723 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
724 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
725 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
727 if ( sx < 0 ) return;
728 if ( sy < 0 ) return;
729 if ( sx >= w ) return;
730 if ( sy >= h ) return;
732 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
734 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
737 void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
741 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
743 saved_zbuf = gr_zbuffer_get();
745 // start the frame, no zbuffering, no culling
747 gr_zbuffer_set(GR_ZBUFF_NONE);
756 v[0].flags = PF_PROJECTED;
763 v[1].sx = i2fl(x + w);
768 v[1].flags = PF_PROJECTED;
775 v[2].sx = i2fl(x + w);
776 v[2].sy = i2fl(y + h);
780 v[2].flags = PF_PROJECTED;
788 v[3].sy = i2fl(y + h);
792 v[3].flags = PF_PROJECTED;
800 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
804 // restore zbuffer and culling
805 gr_zbuffer_set(saved_zbuf);
809 void gr_opengl_rect(int x,int y,int w,int h)
811 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);
814 void gr_opengl_shade(int x,int y,int w,int h)
821 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
822 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
823 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
824 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
825 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
826 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
827 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
828 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
830 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
833 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
838 if ( !gr_screen.current_color.is_alphacolor ) return;
840 float u_scale, v_scale;
842 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
844 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
845 // Couldn't set texture
846 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
850 float u0, u1, v0, v1;
851 float x1, x2, y1, y2;
854 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
856 u0 = u_scale*i2fl(sx)/i2fl(bw);
857 v0 = v_scale*i2fl(sy)/i2fl(bh);
859 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
860 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
862 x1 = i2fl(x+gr_screen.offset_x);
863 y1 = i2fl(y+gr_screen.offset_y);
864 x2 = i2fl(x+w+gr_screen.offset_x);
865 y2 = i2fl(y+h+gr_screen.offset_y);
867 if ( gr_screen.current_color.is_alphacolor ) {
868 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
870 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
874 glTexCoord2f (u0, v1);
875 glVertex3f (x1, y2, -0.99);
877 glTexCoord2f (u1, v1);
878 glVertex3f (x2, y2, -0.99);
880 glTexCoord2f (u1, v0);
881 glVertex3f (x2, y1, -0.99);
883 glTexCoord2f (u0, v0);
884 glVertex3f (x1, y1, -0.99);
888 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
895 int dx1=x, dx2=x+w-1;
896 int dy1=y, dy2=y+h-1;
899 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
904 if ( count > 1 ) Int3();
908 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
909 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
910 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
911 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
912 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
913 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
940 if ( w < 1 ) return; // clipped away!
941 if ( h < 1 ) return; // clipped away!
945 // Make sure clipping algorithm works
949 Assert( w == (dx2-dx1+1) );
950 Assert( h == (dy2-dy1+1) );
953 Assert( sx+w <= bw );
954 Assert( sy+h <= bh );
955 Assert( dx2 >= dx1 );
956 Assert( dy2 >= dy1 );
957 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
958 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
959 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
960 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
963 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
964 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
967 void gr_opengl_aabitmap(int x, int y)
971 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
972 int dx1=x, dx2=x+w-1;
973 int dy1=y, dy2=y+h-1;
976 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
977 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
978 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
979 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
980 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
981 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
983 if ( sx < 0 ) return;
984 if ( sy < 0 ) return;
985 if ( sx >= w ) return;
986 if ( sy >= h ) return;
988 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
989 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
993 void gr_opengl_string( int sx, int sy, char *s )
995 int width, spacing, letter;
998 if ( !Current_font ) {
1002 gr_set_bitmap(Current_font->bitmap_id);
1007 if (sx==0x8000) { //centered
1008 x = get_centered_x(s);
1018 while (*s== '\n' ) {
1020 y += Current_font->h;
1021 if (sx==0x8000) { //centered
1022 x = get_centered_x(s);
1027 if (*s == 0 ) break;
1029 letter = get_char_width(s[0],s[1],&width,&spacing);
1032 //not in font, draw as space
1040 // Check if this character is totally clipped
1041 if ( x + width < gr_screen.clip_left ) continue;
1042 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1043 if ( x > gr_screen.clip_right ) continue;
1044 if ( y > gr_screen.clip_bottom ) continue;
1047 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1048 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1052 wc = width - xd; hc = Current_font->h - yd;
1053 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1054 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1056 if ( wc < 1 ) continue;
1057 if ( hc < 1 ) continue;
1059 int u = Current_font->bm_u[letter];
1060 int v = Current_font->bm_v[letter];
1062 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1066 void gr_opengl_line(int x1,int y1,int x2,int y2)
1068 int clipped = 0, swapped=0;
1070 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1072 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);
1077 sx1 = i2fl(x1 + gr_screen.offset_x);
1078 sy1 = i2fl(y1 + gr_screen.offset_y);
1079 sx2 = i2fl(x2 + gr_screen.offset_x);
1080 sy2 = i2fl(y2 + gr_screen.offset_y);
1082 if ( x1 == x2 && y1 == y2 ) {
1083 glBegin (GL_POINTS);
1084 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1085 glVertex3f (sx1, sy1, -0.99f);
1097 } else if ( y1 == y2 ) {
1106 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1107 glVertex3f (sx2, sy2, -0.99f);
1108 glVertex3f (sx1, sy1, -0.99f);
1112 void gr_opengl_aaline(vertex *v1, vertex *v2)
1114 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1117 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1119 int clipped = 0, swapped=0;
1121 if ( !gr_screen.current_color.is_alphacolor ) {
1122 gr_line( x1, y1, x2, y2 );
1126 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);
1128 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1130 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1131 int ba = swapped ? gr_screen.current_color.alpha : 0;
1136 sx1 = i2fl(x1 + gr_screen.offset_x);
1137 sy1 = i2fl(y1 + gr_screen.offset_y);
1138 sx2 = i2fl(x2 + gr_screen.offset_x);
1139 sy2 = i2fl(y2 + gr_screen.offset_y);
1147 } else if ( y1 == y2 ) {
1156 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1157 glVertex3f (sx2, sy2, -0.99f);
1158 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1159 glVertex3f (sx1, sy1, -0.99f);
1163 void gr_opengl_circle( int xc, int yc, int d )
1173 if ( (xc+r) < gr_screen.clip_left ) return;
1174 if ( (xc-r) > gr_screen.clip_right ) return;
1175 if ( (yc+r) < gr_screen.clip_top ) return;
1176 if ( (yc-r) > gr_screen.clip_bottom ) return;
1179 // Draw the first octant
1180 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1181 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1186 // Draw the second octant
1187 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1188 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1196 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1197 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1202 void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1206 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1207 if (f_float < 0.0f) {
1215 *a = (int)(f_float * 255.0);
1218 void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1221 float u_scale = 1.0f, v_scale = 1.0f;
1223 // Make nebula use the texture mapper... this blends the colors better.
1224 if ( flags & TMAP_FLAG_NEBULA ){
1228 gr_texture_source texture_source = (gr_texture_source)-1;
1229 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1230 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1232 if ( gr_zbuffering ) {
1233 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1234 zbuffer_type = ZBUFFER_TYPE_READ;
1236 zbuffer_type = ZBUFFER_TYPE_FULL;
1239 zbuffer_type = ZBUFFER_TYPE_NONE;
1244 int tmap_type = TCACHE_TYPE_NORMAL;
1248 if ( flags & TMAP_FLAG_TEXTURED ) {
1251 r = gr_screen.current_color.red;
1252 g = gr_screen.current_color.green;
1253 b = gr_screen.current_color.blue;
1256 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1259 tmap_type = TCACHE_TYPE_NORMAL;
1260 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1262 // Blend with screen pixel using src*alpha+dst
1263 float factor = gr_screen.current_alpha;
1267 if ( factor <= 1.0f ) {
1268 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1269 r = (r*tmp_alpha)/255;
1270 g = (g*tmp_alpha)/255;
1271 b = (b*tmp_alpha)/255;
1274 tmap_type = TCACHE_TYPE_XPARENT;
1276 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1278 // Blend with screen pixel using src*alpha+dst
1279 float factor = gr_screen.current_alpha;
1281 if ( factor > 1.0f ) {
1284 alpha = fl2i(gr_screen.current_alpha*255.0f);
1288 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1289 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1291 alpha_blend = ALPHA_BLEND_NONE;
1296 if(flags & TMAP_FLAG_BITMAP_SECTION){
1297 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1300 texture_source = TEXTURE_SOURCE_NONE;
1302 if ( flags & TMAP_FLAG_TEXTURED ) {
1303 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 ))
1305 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1309 // use nonfiltered textures for bitmap sections
1310 if(flags & TMAP_FLAG_BITMAP_SECTION){
1311 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1313 texture_source = TEXTURE_SOURCE_DECAL;
1318 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1320 if ( flags & TMAP_FLAG_TEXTURED )
1326 if (flags & TMAP_FLAG_PIXEL_FOG) {
1332 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1334 vertex * va = verts[i];
1338 x = fl2i(va->sx*16.0f);
1339 y = fl2i(va->sy*16.0f);
1341 x += gr_screen.offset_x*16;
1342 y += gr_screen.offset_y*16;
1344 sx = i2fl(x) / 16.0f;
1345 sy = i2fl(y) / 16.0f;
1347 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1358 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1361 glBegin(GL_TRIANGLE_FAN);
1362 for (i = nv-1; i >= 0; i--) {
1363 vertex * va = verts[i];
1364 float sx, sy, sz, sw;
1369 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1370 sz = 1.0 - 1.0 / va->z;
1378 if ( flags & TMAP_FLAG_CORRECT ) {
1384 if (flags & TMAP_FLAG_ALPHA) {
1390 if (flags & TMAP_FLAG_NEBULA ) {
1391 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1392 r = gr_palette[pal*3+0];
1393 g = gr_palette[pal*3+1];
1394 b = gr_palette[pal*3+2];
1395 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1396 r = Gr_gamma_lookup[verts[i]->b];
1397 g = Gr_gamma_lookup[verts[i]->b];
1398 b = Gr_gamma_lookup[verts[i]->b];
1399 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1400 // Make 0.75 be 256.0f
1401 r = Gr_gamma_lookup[verts[i]->r];
1402 g = Gr_gamma_lookup[verts[i]->g];
1403 b = Gr_gamma_lookup[verts[i]->b];
1405 // use constant RGB values...
1407 glColor4ub (r,g,b,a);
1409 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1412 /* this is for GL_EXT_SECONDARY_COLOR */
1413 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1414 /* do separate color call here */
1420 x = fl2i(va->sx*16.0f);
1421 y = fl2i(va->sy*16.0f);
1423 x += gr_screen.offset_x*16;
1424 y += gr_screen.offset_y*16;
1426 sx = i2fl(x) / 16.0f;
1427 sy = i2fl(y) / 16.0f;
1429 if ( flags & TMAP_FLAG_TEXTURED ) {
1432 glTexCoord2d(tu, tv);
1437 glVertex4d(sx/rhw, sy/rhw, -sz/rhw, sw/rhw);
1442 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1444 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1447 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1449 void gr_opengl_scaler(vertex *va, vertex *vb )
1451 float x0, y0, x1, y1;
1452 float u0, v0, u1, v1;
1453 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1454 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1455 float xmin, xmax, ymin, ymax;
1456 int dx0, dy0, dx1, dy1;
1458 //============= CLIP IT =====================
1460 x0 = va->sx; y0 = va->sy;
1461 x1 = vb->sx; y1 = vb->sy;
1463 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1464 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1466 u0 = va->u; v0 = va->v;
1467 u1 = vb->u; v1 = vb->v;
1469 // Check for obviously offscreen bitmaps...
1470 if ( (y1<=y0) || (x1<=x0) ) return;
1471 if ( (x1<xmin ) || (x0>xmax) ) return;
1472 if ( (y1<ymin ) || (y0>ymax) ) return;
1474 clipped_u0 = u0; clipped_v0 = v0;
1475 clipped_u1 = u1; clipped_v1 = v1;
1477 clipped_x0 = x0; clipped_y0 = y0;
1478 clipped_x1 = x1; clipped_y1 = y1;
1480 // Clip the left, moving u0 right as necessary
1482 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1486 // Clip the right, moving u1 left as necessary
1488 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1492 // Clip the top, moving v0 down as necessary
1494 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1498 // Clip the bottom, moving v1 up as necessary
1500 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1504 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1505 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1507 if (dx1<=dx0) return;
1508 if (dy1<=dy0) return;
1510 //============= DRAW IT =====================
1524 v[1].sx = clipped_x1;
1525 v[1].sy = clipped_y0;
1528 v[1].u = clipped_u1;
1529 v[1].v = clipped_v0;
1532 v[2].sx = clipped_x1;
1533 v[2].sy = clipped_y1;
1536 v[2].u = clipped_u1;
1537 v[2].v = clipped_v1;
1540 v[3].sx = clipped_x0;
1541 v[3].sy = clipped_y1;
1544 v[3].u = clipped_u0;
1545 v[3].v = clipped_v1;
1547 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1550 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1554 void gr_opengl_get_color( int * r, int * g, int * b )
1556 if (r) *r = gr_screen.current_color.red;
1557 if (g) *g = gr_screen.current_color.green;
1558 if (b) *b = gr_screen.current_color.blue;
1561 void gr_opengl_init_color(color *c, int r, int g, int b)
1563 c->screen_sig = gr_screen.signature;
1564 c->red = (unsigned char)r;
1565 c->green = (unsigned char)g;
1566 c->blue = (unsigned char)b;
1568 c->ac_type = AC_TYPE_NONE;
1570 c->is_alphacolor = 0;
1574 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1576 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1577 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1578 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1579 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1581 gr_opengl_init_color( clr, r, g, b );
1583 clr->alpha = (unsigned char)alpha;
1584 clr->ac_type = (ubyte)type;
1585 clr->alphacolor = -1;
1586 clr->is_alphacolor = 1;
1589 void gr_opengl_set_color( int r, int g, int b )
1591 Assert((r >= 0) && (r < 256));
1592 Assert((g >= 0) && (g < 256));
1593 Assert((b >= 0) && (b < 256));
1595 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1598 void gr_opengl_set_color_fast(color *dst)
1600 if ( dst->screen_sig != gr_screen.signature ) {
1601 if ( dst->is_alphacolor ) {
1602 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1604 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1607 gr_screen.current_color = *dst;
1610 void gr_opengl_print_screen(char *filename)
1615 int gr_opengl_supports_res_ingame(int res)
1622 int gr_opengl_supports_res_interface(int res)
1629 void opengl_tcache_cleanup ();
1630 void gr_opengl_cleanup()
1632 if ( !Inited ) return;
1639 opengl_tcache_cleanup ();
1644 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1646 Assert((r >= 0) && (r < 256));
1647 Assert((g >= 0) && (g < 256));
1648 Assert((b >= 0) && (b < 256));
1650 if (fog_mode == GR_FOGMODE_NONE) {
1651 if (gr_screen.current_fog_mode != fog_mode) {
1654 gr_screen.current_fog_mode = fog_mode;
1659 if (gr_screen.current_fog_mode != fog_mode) {
1662 if (D3D_fog_mode == 2) {
1663 glFogi(GL_FOG_MODE, GL_LINEAR);
1666 gr_screen.current_fog_mode = fog_mode;
1669 if ( (gr_screen.current_fog_color.red != r) ||
1670 (gr_screen.current_fog_color.green != g) ||
1671 (gr_screen.current_fog_color.blue != b) ) {
1674 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1676 fc[0] = (float)r/255.0;
1677 fc[1] = (float)g/255.0;
1678 fc[2] = (float)b/255.0;
1681 glFogfv(GL_FOG_COLOR, fc);
1684 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1685 ((fog_near != gr_screen.fog_near) ||
1686 (fog_far != gr_screen.fog_far)) ) {
1687 gr_screen.fog_near = fog_near;
1688 gr_screen.fog_far = fog_far;
1690 if (D3D_fog_mode == 2) {
1691 glFogf(GL_FOG_START, fog_near);
1692 glFogf(GL_FOG_END, fog_far);
1697 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1702 void gr_opengl_set_cull(int cull)
1705 // DDOI - disabled for debugging purposes
1706 glEnable (GL_CULL_FACE);
1707 glFrontFace (GL_CCW);
1709 glDisable (GL_CULL_FACE);
1713 void gr_opengl_filter_set(int filter)
1718 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1724 typedef struct tcache_slot_opengl {
1725 GLuint texture_handle;
1726 float u_scale, v_scale;
1729 char used_this_frame;
1734 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1735 tcache_slot_opengl *parent;
1736 } tcache_slot_opengl;
1738 static void *Texture_sections = NULL;
1739 tcache_slot_opengl *Textures = NULL;
1741 int GL_texture_sections = 0;
1742 int GL_texture_ram = 0;
1743 int GL_frame_count = 0;
1744 int GL_min_texture_width = 0;
1745 int GL_max_texture_width = 0;
1746 int GL_min_texture_height = 0;
1747 int GL_max_texture_height = 0;
1748 int GL_square_textures = 0;
1749 int GL_textures_in = 0;
1750 int GL_textures_in_frame = 0;
1751 int GL_last_bitmap_id = -1;
1752 int GL_last_detail = -1;
1753 int GL_last_bitmap_type = -1;
1754 int GL_last_section_x = -1;
1755 int GL_last_section_y = -1;
1759 void opengl_tcache_init (int use_sections)
1763 // DDOI - FIXME skipped a lot of stuff here
1764 GL_should_preload = 0;
1766 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1769 if ( tmp_pl == 0 ) {
1770 GL_should_preload = 0;
1771 } else if ( tmp_pl == 1 ) {
1772 GL_should_preload = 1;
1774 GL_should_preload = 1;
1779 GL_min_texture_width = 16;
1780 GL_min_texture_height = 16;
1781 GL_max_texture_width = 256;
1782 GL_max_texture_height = 256;
1784 GL_square_textures = 1;
1786 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1792 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1793 if(!Texture_sections){
1796 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1799 // Init the texture structures
1800 int section_count = 0;
1801 for( i=0; i<MAX_BITMAPS; i++ ) {
1803 Textures[i].vram_texture = NULL;
1804 Textures[i].vram_texture_surface = NULL;
1806 Textures[i].texture_handle = 0;
1808 Textures[i].bitmap_id = -1;
1809 Textures[i].size = 0;
1810 Textures[i].used_this_frame = 0;
1812 Textures[i].parent = NULL;
1814 // allocate sections
1816 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1817 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1818 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1819 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1821 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1822 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1824 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1825 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1826 Textures[i].data_sections[idx][s_idx]->size = 0;
1827 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1831 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1832 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1833 Textures[i].data_sections[idx][s_idx] = NULL;
1839 GL_texture_sections = use_sections;
1841 //GL_last_detail = Detail.hardware_textures;
1842 GL_last_bitmap_id = -1;
1843 GL_last_bitmap_type = -1;
1845 GL_last_section_x = -1;
1846 GL_last_section_y = -1;
1849 GL_textures_in_frame = 0;
1852 int opengl_free_texture (tcache_slot_opengl *t);
1854 void opengl_tcache_flush ()
1858 for( i=0; i<MAX_BITMAPS; i++ ) {
1859 opengl_free_texture ( &Textures[i] );
1861 if (GL_textures_in != 0) {
1862 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1866 GL_last_bitmap_id = -1;
1867 GL_last_section_x = -1;
1868 GL_last_section_y = -1;
1871 void opengl_tcache_cleanup ()
1873 opengl_tcache_flush ();
1876 GL_textures_in_frame = 0;
1883 if( Texture_sections != NULL ){
1884 free(Texture_sections);
1885 Texture_sections = NULL;
1889 void opengl_tcache_frame ()
1893 GL_last_bitmap_id = -1;
1894 GL_textures_in_frame = 0;
1899 for( i=0; i<MAX_BITMAPS; i++ ) {
1900 Textures[i].used_this_frame = 0;
1903 if(Textures[i].data_sections[0][0] != NULL){
1904 Assert(GL_texture_sections);
1905 if(GL_texture_sections){
1906 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1907 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1908 if(Textures[i].data_sections[idx][s_idx] != NULL){
1909 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1918 opengl_tcache_flush();
1923 int opengl_free_texture ( tcache_slot_opengl *t )
1929 if ( t->bitmap_id > -1 ) {
1930 // if I, or any of my children have been used this frame, bail
1931 if(t->used_this_frame){
1934 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1935 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1936 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
1942 // ok, now we know its legal to free everything safely
1943 glDeleteTextures (1, &t->texture_handle);
1944 t->texture_handle = 0;
1946 if ( GL_last_bitmap_id == t->bitmap_id ) {
1947 GL_last_bitmap_id = -1;
1950 // if this guy has children, free them too, since the children
1951 // actually make up his size
1952 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1953 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1954 if(t->data_sections[idx][s_idx] != NULL){
1955 opengl_free_texture(t->data_sections[idx][s_idx]);
1961 t->used_this_frame = 0;
1962 GL_textures_in -= t->size;
1968 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1973 if((w_out == NULL) || (h_out == NULL)){
1983 for (i=0; i<16; i++ ) {
1984 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
1990 for (i=0; i<16; i++ ) {
1991 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
1998 if ( tex_w < GL_min_texture_width ) {
1999 tex_w = GL_min_texture_width;
2000 } else if ( tex_w > GL_max_texture_width ) {
2001 tex_w = GL_max_texture_width;
2004 if ( tex_h < GL_min_texture_height ) {
2005 tex_h = GL_min_texture_height;
2006 } else if ( tex_h > GL_max_texture_height ) {
2007 tex_h = GL_max_texture_height;
2010 if ( GL_square_textures ) {
2012 // Make the both be equal to larger of the two
2013 new_size = max(tex_w, tex_h);
2018 // store the outgoing size
2023 // data == start of bitmap data
2024 // sx == x offset into bitmap
2025 // sy == y offset into bitmap
2026 // src_w == absolute width of section on source bitmap
2027 // src_h == absolute height of section on source bitmap
2028 // bmap_w == width of source bitmap
2029 // bmap_h == height of source bitmap
2030 // tex_w == width of final texture
2031 // tex_h == height of final texture
2032 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)
2041 if ( t->used_this_frame ) {
2042 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2047 if(!opengl_free_texture(t)){
2052 // get final texture size
2053 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2055 if ( (tex_w < 1) || (tex_h < 1) ) {
2056 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2060 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2061 t->u_scale = (float)bmap_w / (float)tex_w;
2062 t->v_scale = (float)bmap_h / (float)tex_h;
2063 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2064 t->u_scale = (float)src_w / (float)tex_w;
2065 t->v_scale = (float)src_h / (float)tex_h;
2071 glGenTextures (1, &t->texture_handle);
2072 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2074 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2075 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2076 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2077 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2079 switch (bitmap_type) {
2081 case TCACHE_TYPE_AABITMAP:
2084 ubyte *bmp_data = ((ubyte*)data);
2085 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2086 ubyte *texmemp = texmem;
2089 for (i=0; i<16; i++) {
2090 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2093 for ( ; i<256; i++ ) {
2097 for (i=0;i<tex_h;i++)
2099 for (j=0;j<tex_w;j++)
2101 if (i < bmap_h && j < bmap_w) {
2103 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2111 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2116 case TCACHE_TYPE_BITMAP_SECTION:
2119 ubyte *bmp_data = ((ubyte*)data);
2120 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2121 ubyte *texmemp = texmem;
2123 for (i=0;i<tex_h;i++)
2125 for (j=0;j<tex_w;j++)
2127 if (i < src_h && j < src_w) {
2128 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2129 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2136 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2137 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2145 ubyte *bmp_data = ((ubyte*)data);
2146 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2147 ubyte *texmemp = texmem;
2149 fix u, utmp, v, du, dv;
2153 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2154 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2156 for (j=0;j<tex_h;j++)
2159 for (i=0;i<tex_w;i++)
2161 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2162 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2167 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2168 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2176 t->bitmap_id = texture_handle;
2177 t->time_created = GL_frame_count;
2178 t->used_this_frame = 0;
2179 t->size = tex_w * tex_h * 2;
2180 t->w = (ushort)tex_w;
2181 t->h = (ushort)tex_h;
2182 GL_textures_in_frame += t->size;
2184 GL_textures_in += t->size;
2190 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2194 int final_w, final_h;
2198 // setup texture/bitmap flags
2200 switch(bitmap_type){
2201 case TCACHE_TYPE_AABITMAP:
2202 flags |= BMP_AABITMAP;
2205 case TCACHE_TYPE_NORMAL:
2206 flags |= BMP_TEX_OTHER;
2207 case TCACHE_TYPE_XPARENT:
2208 flags |= BMP_TEX_XPARENT;
2210 case TCACHE_TYPE_NONDARKENING:
2212 flags |= BMP_TEX_NONDARK;
2216 // lock the bitmap into the proper format
2217 bmp = bm_lock(bitmap_handle, bpp, flags);
2218 if ( bmp == NULL ) {
2219 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2228 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2229 max_w /= D3D_texture_divider;
2230 max_h /= D3D_texture_divider;
2232 // Detail.debris_culling goes from 0 to 4.
2233 max_w /= 16 >> Detail.hardware_textures;
2234 max_h /= 16 >> Detail.hardware_textures;
2238 // get final texture size as it will be allocated as a DD surface
2239 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2241 // if this tcache slot has no bitmap
2242 if ( tslot->bitmap_id < 0) {
2245 // different bitmap altogether - determine if the new one can use the old one's slot
2246 else if (tslot->bitmap_id != bitmap_handle) {
2247 if((final_w == tslot->w) && (final_h == tslot->h)){
2249 //ml_printf("Reloading texture %d\n", bitmap_handle);
2256 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);
2258 // unlock the bitmap
2259 bm_unlock(bitmap_handle);
2264 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2268 int final_w, final_h;
2269 int section_x, section_y;
2272 // setup texture/bitmap flags
2273 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2274 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2275 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2277 flags = BMP_TEX_XPARENT;
2279 // lock the bitmap in the proper format
2280 bmp = bm_lock(bitmap_handle, 16, flags);
2281 if ( bmp == NULL ) {
2282 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2285 // determine the width and height of this section
2286 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2288 // get final texture size as it will be allocated as an opengl texture
2289 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2291 // if this tcache slot has no bitmap
2292 if ( tslot->bitmap_id < 0) {
2295 // different bitmap altogether - determine if the new one can use the old one's slot
2296 else if (tslot->bitmap_id != bitmap_handle) {
2297 if((final_w == tslot->w) && (final_h == tslot->h)){
2305 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);
2307 // unlock the bitmap
2308 bm_unlock(bitmap_handle);
2314 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2315 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)
2324 GL_last_bitmap_id = -1;
2329 if ( GL_last_detail != Detail.hardware_textures ) {
2330 GL_last_detail = Detail.hardware_textures;
2331 opengl_tcache_flush();
2339 int n = bm_get_cache_slot (bitmap_id, 1);
2340 tcache_slot_opengl *t = &Textures[n];
2342 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)) {
2343 t->used_this_frame++;
2345 // mark all children as used
2346 if(GL_texture_sections){
2347 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2348 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2349 if(t->data_sections[idx][s_idx] != NULL){
2350 t->data_sections[idx][s_idx]->used_this_frame++;
2356 *u_scale = t->u_scale;
2357 *v_scale = t->v_scale;
2361 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2362 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2363 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2369 // if the texture sections haven't been created yet
2370 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2372 // lock the bitmap in the proper format
2373 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2374 bm_unlock(bitmap_id);
2376 // now lets do something for each texture
2378 for(idx=0; idx<bmp->sections.num_x; idx++){
2379 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2380 // hmm. i'd rather we didn't have to do it this way...
2381 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2385 // not used this frame
2386 t->data_sections[idx][s_idx]->used_this_frame = 0;
2390 // zero out pretty much everything in the parent struct since he's just the root
2391 t->bitmap_id = bitmap_id;
2392 t->texture_handle = 0;
2393 t->time_created = t->data_sections[sx][sy]->time_created;
2394 t->used_this_frame = 0;
2396 t->vram_texture = NULL;
2397 t->vram_texture_surface = NULL
2401 // argh. we failed to upload. free anything we can
2403 opengl_free_texture(t);
2405 // swap in the texture we want
2407 t = t->data_sections[sx][sy];
2410 // all other "normal" textures
2411 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2412 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2415 // everything went ok
2416 if(ret_val && (t->texture_handle) && !vram_full){
2417 *u_scale = t->u_scale;
2418 *v_scale = t->v_scale;
2420 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2422 GL_last_bitmap_id = t->bitmap_id;
2423 GL_last_bitmap_type = bitmap_type;
2424 GL_last_section_x = sx;
2425 GL_last_section_y = sy;
2427 t->used_this_frame++;
2431 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2438 void gr_opengl_set_clear_color(int r, int g, int b)
2440 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2443 void gr_opengl_flash(int r, int g, int b)
2449 if ( r || g || b ) {
2450 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2452 float x1, x2, y1, y2;
2453 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2454 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2455 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2456 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2458 glColor4ub(r, g, b, 255);
2460 glVertex3f (x1, y2, -0.99);
2462 glVertex3f (x2, y2, -0.99);
2464 glVertex3f (x2, y1, -0.99);
2466 glVertex3f (x1, y1, -0.99);
2471 int gr_opengl_zbuffer_get()
2473 if ( !gr_global_zbuffering ) {
2474 return GR_ZBUFF_NONE;
2476 return gr_zbuffering_mode;
2479 int gr_opengl_zbuffer_set(int mode)
2481 int tmp = gr_zbuffering_mode;
2483 gr_zbuffering_mode = mode;
2485 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2493 void gr_opengl_zbuffer_clear(int mode)
2497 gr_zbuffering_mode = GR_ZBUFF_FULL;
2498 gr_global_zbuffering = 1;
2500 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2501 glClear(GL_DEPTH_BUFFER_BIT);
2504 gr_zbuffering_mode = GR_ZBUFF_NONE;
2505 gr_global_zbuffering = 0;
2509 void gr_opengl_set_gamma(float gamma)
2512 Gr_gamma_int = int (Gr_gamma*10);
2514 // Create the Gamma lookup table
2516 for (i=0;i<256; i++) {
2517 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2520 } else if ( v < 0 ) {
2523 Gr_gamma_lookup[i] = v;
2526 // Flush any existing textures
2527 opengl_tcache_flush();
2530 void gr_opengl_fade_in(int instantaneous)
2535 void gr_opengl_fade_out(int instantaneous)
2540 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2543 glReadBuffer(GL_FRONT);
2545 glReadBuffer(GL_BACK);
2548 // glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2551 int gr_opengl_save_screen()
2555 if ( Gr_saved_screen ) {
2556 mprintf(( "Screen alread saved!\n" ));
2560 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2561 if (!Gr_saved_screen) {
2562 mprintf(( "Couldn't get memory for saved screen!\n" ));
2566 gr_opengl_get_region(1, gr_screen.max_w, gr_screen.max_h, (ubyte *)Gr_saved_screen);
2571 void gr_opengl_restore_screen(int id)
2575 if ( !Gr_saved_screen ) {
2581 // glDrawPixels(gr_screen.max_w, gr_screen.max_h, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, Gr_saved_screen);
2584 void gr_opengl_free_screen(int id)
2586 if ( Gr_saved_screen ) {
2587 free( Gr_saved_screen );
2588 Gr_saved_screen = NULL;
2592 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2597 void gr_opengl_dump_frame_stop()
2602 void gr_opengl_dump_frame()
2607 uint gr_opengl_lock()
2614 void gr_opengl_unlock()
2618 extern char *Osreg_title;
2619 void gr_opengl_init()
2622 gr_opengl_cleanup();
2626 mprintf(( "Initializing opengl graphics device...\n" ));
2630 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2632 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2638 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2639 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2640 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2641 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2642 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2644 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,SDL_OPENGL) == NULL)
2646 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2650 SDL_WM_SetCaption (Osreg_title, "FS2");
2652 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2654 glMatrixMode(GL_PROJECTION);
2656 glMatrixMode(GL_MODELVIEW);
2659 glShadeModel(GL_SMOOTH);
2660 glEnable(GL_DITHER);
2661 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2662 glHint(GL_FOG_HINT, GL_NICEST);
2664 glEnable(GL_DEPTH_TEST);
2667 glEnable(GL_TEXTURE_2D);
2669 glMatrixMode(GL_PROJECTION);
2673 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2674 glDepthRange(0.0, 1.0);
2676 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2678 //glGenTextures(1, &bitmapTex);
2679 //glBindTexture(GL_TEXTURE_2D, bitmapTex);
2680 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2681 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2682 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2683 //bitmapMem = (GLubyte *)malloc(256*256*4);
2684 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, bitmapMem);
2686 D3D_32bit = 1; // grd3d.cpp
2689 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2690 1 = use secondary color ext
2691 2 = use opengl linear fog
2693 D3D_fog_mode = 2; // grd3d.cpp
2697 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2707 Gr_red.mask = 0xff0000;
2712 Gr_green.mask = 0xff00;
2717 Gr_blue.mask = 0xff;
2725 Gr_red.mask = 0x7C00;
2730 Gr_green.mask = 0x3E0;
2735 Gr_blue.mask = 0x1F;
2743 Gr_red.mask = 0xF800;
2748 Gr_green.mask = 0x7E0;
2753 Gr_blue.mask = 0x1F;
2762 Gr_red.mask = 0xff0000;
2767 Gr_green.mask = 0xff00;
2772 Gr_blue.mask = 0xff;
2777 Int3(); // Illegal bpp
2780 // DDOI - set these so no one else does!
2781 Gr_ta_red.mask = 0x0f00;
2782 Gr_ta_red.shift = 8;
2783 Gr_ta_red.scale = 16;
2785 Gr_ta_green.mask = 0x00f0;
2786 Gr_ta_green.shift = 4;
2787 Gr_ta_green.scale = 16;
2789 Gr_ta_blue.mask = 0x000f;
2790 Gr_ta_blue.shift = 0;
2791 Gr_ta_blue.scale = 16;
2793 Gr_ta_alpha.mask = 0xf000;
2794 Gr_ta_alpha.shift = 12;
2795 Gr_ta_alpha.scale = 16;
2798 opengl_tcache_init (1);
2801 Gr_current_red = &Gr_red;
2802 Gr_current_blue = &Gr_blue;
2803 Gr_current_green = &Gr_green;
2804 Gr_current_alpha = &Gr_alpha;
2806 gr_screen.gf_flip = gr_opengl_flip;
2807 gr_screen.gf_flip_window = gr_opengl_flip_window;
2808 gr_screen.gf_set_clip = gr_opengl_set_clip;
2809 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2810 gr_screen.gf_set_font = grx_set_font;
2812 gr_screen.gf_set_color = gr_opengl_set_color;
2813 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2814 gr_screen.gf_create_shader = gr_opengl_create_shader;
2815 gr_screen.gf_set_shader = gr_opengl_set_shader;
2816 gr_screen.gf_clear = gr_opengl_clear;
2817 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2818 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2819 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2820 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2822 gr_screen.gf_rect = gr_opengl_rect;
2823 gr_screen.gf_shade = gr_opengl_shade;
2824 gr_screen.gf_string = gr_opengl_string;
2825 gr_screen.gf_circle = gr_opengl_circle;
2827 gr_screen.gf_line = gr_opengl_line;
2828 gr_screen.gf_aaline = gr_opengl_aaline;
2829 gr_screen.gf_pixel = gr_opengl_pixel;
2830 gr_screen.gf_scaler = gr_opengl_scaler;
2831 gr_screen.gf_tmapper = gr_opengl_tmapper;
2833 gr_screen.gf_gradient = gr_opengl_gradient;
2835 gr_screen.gf_set_palette = gr_opengl_set_palette;
2836 gr_screen.gf_get_color = gr_opengl_get_color;
2837 gr_screen.gf_init_color = gr_opengl_init_color;
2838 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
2839 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
2840 gr_screen.gf_print_screen = gr_opengl_print_screen;
2842 gr_screen.gf_fade_in = gr_opengl_fade_in;
2843 gr_screen.gf_fade_out = gr_opengl_fade_out;
2844 gr_screen.gf_flash = gr_opengl_flash;
2846 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
2847 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
2848 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
2850 gr_screen.gf_save_screen = gr_opengl_save_screen;
2851 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
2852 gr_screen.gf_free_screen = gr_opengl_free_screen;
2854 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
2855 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
2856 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
2858 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
2860 gr_screen.gf_lock = gr_opengl_lock;
2861 gr_screen.gf_unlock = gr_opengl_unlock;
2863 gr_screen.gf_fog_set = gr_opengl_fog_set;
2865 gr_screen.gf_get_region = gr_opengl_get_region;
2867 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
2869 gr_screen.gf_set_cull = gr_opengl_set_cull;
2871 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
2873 gr_screen.gf_filter_set = gr_opengl_filter_set;
2875 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
2877 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;