2 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
7 * Code that uses the OpenGL graphics library
10 * Revision 1.45 2002/06/03 09:25:37 relnev
11 * implement mouse cursor and screen save/restore
13 * Revision 1.44 2002/06/02 18:46:59 relnev
16 * Revision 1.43 2002/06/02 11:34:00 relnev
19 * Revision 1.42 2002/06/02 10:28:17 relnev
20 * fix texture handle leak
22 * Revision 1.41 2002/06/01 09:00:34 relnev
23 * silly debug memmanager
25 * Revision 1.40 2002/06/01 07:12:33 relnev
26 * a few NDEBUG updates.
28 * removed a few warnings.
30 * Revision 1.39 2002/06/01 05:33:15 relnev
31 * copied more code over.
33 * added scissor clipping.
35 * Revision 1.38 2002/06/01 03:35:27 relnev
38 * Revision 1.37 2002/06/01 03:32:00 relnev
39 * fix texture loading mistake.
41 * enable some d3d stuff for opengl also
43 * Revision 1.36 2002/05/31 23:25:03 relnev
46 * Revision 1.34 2002/05/31 22:15:22 relnev
49 * Revision 1.33 2002/05/31 22:04:55 relnev
50 * use d3d rect_internal
52 * Revision 1.32 2002/05/31 06:28:23 relnev
55 * Revision 1.31 2002/05/31 06:04:39 relnev
58 * Revision 1.30 2002/05/31 03:56:11 theoddone33
59 * Change tmapper polygon winding and enable culling
61 * Revision 1.29 2002/05/31 03:34:02 theoddone33
65 * Revision 1.28 2002/05/31 00:06:59 relnev
68 * Revision 1.27 2002/05/30 23:46:29 theoddone33
69 * some minor key changes (not necessarily fixes)
71 * Revision 1.26 2002/05/30 23:33:12 relnev
72 * implemented a few more functions.
74 * Revision 1.25 2002/05/30 23:01:16 relnev
75 * implement gr_opengl_set_state.
77 * Revision 1.24 2002/05/30 22:12:57 relnev
78 * finish default texture case
80 * Revision 1.23 2002/05/30 22:02:30 theoddone33
83 * Revision 1.22 2002/05/30 21:44:48 relnev
84 * implemented some missing texture stuff.
86 * enable bitmap polys for opengl.
88 * work around greenness in bitmaps.
90 * Revision 1.21 2002/05/30 17:29:30 theoddone33
91 * Fix some more stubs, change at least one polygon winding since culling is now
94 * Revision 1.20 2002/05/30 16:50:24 theoddone33
95 * Keyboard partially fixed
97 * Revision 1.19 2002/05/30 08:13:14 relnev
100 * Revision 1.18 2002/05/29 23:37:36 relnev
103 * Revision 1.17 2002/05/29 23:17:49 theoddone33
104 * Non working text code and fixed keys
106 * Revision 1.16 2002/05/29 19:45:13 theoddone33
107 * More changes on texture loading
109 * Revision 1.15 2002/05/29 19:06:48 theoddone33
110 * Enable string printing. Enable texture mapping
112 * Revision 1.14 2002/05/29 08:54:40 relnev
113 * "fixed" bitmap drawing.
115 * copied more d3d code over.
117 * Revision 1.13 2002/05/29 06:25:13 theoddone33
118 * Keyboard input, mouse tracking now work
120 * Revision 1.12 2002/05/29 04:52:45 relnev
123 * Revision 1.11 2002/05/29 04:29:56 relnev
124 * removed some unncessary stubbing, implemented opengl rect
126 * Revision 1.10 2002/05/29 04:13:27 theoddone33
129 * Revision 1.9 2002/05/29 03:35:51 relnev
132 * Revision 1.8 2002/05/29 03:30:05 relnev
133 * update opengl stubs
135 * Revision 1.7 2002/05/29 02:52:32 theoddone33
136 * Enable OpenGL renderer
138 * Revision 1.6 2002/05/28 04:56:51 theoddone33
139 * runs a little bit now
141 * Revision 1.5 2002/05/28 04:07:28 theoddone33
142 * New graphics stubbing arrangement
144 * Revision 1.4 2002/05/27 23:39:34 relnev
147 * Revision 1.3 2002/05/27 22:35:01 theoddone33
150 * Revision 1.2 2002/05/27 22:32:02 theoddone33
151 * throw all d3d stuff at opengl
153 * Revision 1.1.1.1 2002/05/03 03:28:09 root
157 * 10 7/14/99 9:42a Dave
158 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
161 * 9 7/09/99 9:51a Dave
162 * Added thick polyline code.
164 * 8 6/29/99 10:35a Dave
165 * Interface polygon bitmaps! Whee!
167 * 7 2/03/99 11:44a Dave
168 * Fixed d3d transparent textures.
170 * 6 1/24/99 11:37p Dave
171 * First full rev of beam weapons. Very customizable. Removed some bogus
172 * Int3()'s in low level net code.
174 * 5 12/18/98 1:13a Dave
175 * Rough 1024x768 support for Direct3D. Proper detection and usage through
178 * 4 12/06/98 2:36p Dave
179 * Drastically improved nebula fogging.
181 * 3 11/11/98 5:37p Dave
182 * Checkin for multiplayer testing.
184 * 2 10/07/98 10:53a Dave
187 * 1 10/07/98 10:49a Dave
189 * 14 5/20/98 9:46p John
190 * added code so the places in code that change half the palette don't
191 * have to clear the screen.
193 * 13 5/06/98 5:30p John
194 * Removed unused cfilearchiver. Removed/replaced some unused/little used
195 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
196 * DirectX header files and libs that fixed the Direct3D alpha blending
199 * 12 4/14/98 12:15p John
200 * Made 16-bpp movies work.
202 * 11 3/12/98 5:36p John
203 * Took out any unused shaders. Made shader code take rgbc instead of
204 * matrix and vector since noone used it like a matrix and it would have
205 * been impossible to do in hardware. Made Glide implement a basic
206 * shader for online help.
208 * 10 3/10/98 4:18p John
209 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
210 * & Glide have popups and print screen. Took out all >8bpp software
211 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
212 * support Fred. Made zbuffering key off of functions rather than one
215 * 9 12/02/97 4:00p John
216 * Added first rev of thruster glow, along with variable levels of
217 * translucency, which retquired some restructing of palman.
219 * 8 10/03/97 9:10a John
220 * added better antialiased line drawer
222 * 7 9/23/97 10:45a John
223 * made so you can tell bitblt code to rle a bitmap by passing flag to
226 * 6 9/09/97 11:01a Sandeep
227 * fixed warning level 4 bugs
229 * 5 7/10/97 2:06p John
230 * added code to specify alphablending type for bitmaps.
232 * 4 6/17/97 7:04p John
233 * added d3d support for gradients.
234 * fixed some color bugs by adding screen signatures instead of watching
235 * flags and palette changes.
237 * 3 6/12/97 2:50a Lawrance
238 * bm_unlock() now passed bitmap number, not pointer
240 * 2 6/11/97 1:12p John
241 * Started fixing all the text colors in the game.
243 * 1 5/12/97 12:14p John
250 #include <windowsx.h>
259 #include "floating.h"
261 #include "systemvars.h"
262 #include "grinternal.h"
263 #include "gropengl.h"
267 #include "osregistry.h"
269 static int Inited = 0;
271 typedef enum gr_texture_source {
273 TEXTURE_SOURCE_DECAL,
274 TEXTURE_SOURCE_NO_FILTERING,
277 typedef enum gr_alpha_blend {
278 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
279 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
280 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
281 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
284 typedef enum gr_zbuffer_type {
291 #define NEBULA_COLORS 20
293 volatile int GL_activate = 0;
294 volatile int GL_deactivate = 0;
296 static char *Gr_saved_screen = NULL;
297 static int Gr_saved_screen_bitmap;
300 // Throw in some dummy functions - DDOI
302 int D3D_32bit = 0; // grd3d.cpp
303 int D3D_fog_mode = -1; // grd3d.cpp
304 int D3D_inited = 0; // grd3d.cpp
305 int D3D_zbias = 1; // grd3d.cpp
306 int D3d_rendition_uvs = 0; // grd3d.cpp
308 void gr_dd_activate(int active) // grdirectdraw.cpp
313 void gr_directdraw_cleanup() // grdirectdraw.cpp
318 void gr_directdraw_force_windowed() // grdirectdraw.cpp
323 void gr_directdraw_init()
328 extern int gr_opengl_preload (int x, int y);
329 int gr_d3d_preload (int x, int y)
331 return gr_opengl_preload(x, y);
334 void d3d_start_frame()
339 void d3d_stop_frame()
349 void d3d_zbias (int a)
355 void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
358 case TEXTURE_SOURCE_NONE:
359 glBindTexture(GL_TEXTURE_2D, 0);
360 gr_tcache_set(-1, -1, NULL, NULL );
362 case TEXTURE_SOURCE_DECAL:
363 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
364 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
365 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
367 case TEXTURE_SOURCE_NO_FILTERING:
368 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
369 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
370 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
377 case ALPHA_BLEND_NONE:
378 glBlendFunc(GL_ONE, GL_ZERO);
380 case ALPHA_BLEND_ALPHA_ADDITIVE:
381 glBlendFunc(GL_ONE, GL_ONE);
383 case ALPHA_BLEND_ALPHA_BLEND_ALPHA:
384 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
386 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR:
387 glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
394 case ZBUFFER_TYPE_NONE:
395 glDepthFunc(GL_ALWAYS);
396 glDepthMask(GL_FALSE);
398 case ZBUFFER_TYPE_READ:
399 glDepthFunc(GL_LESS);
400 glDepthMask(GL_FALSE);
402 case ZBUFFER_TYPE_WRITE:
403 glDepthFunc(GL_ALWAYS);
404 glDepthMask(GL_TRUE);
406 case ZBUFFER_TYPE_FULL:
407 glDepthFunc(GL_LESS);
408 glDepthMask(GL_TRUE);
415 void gr_opengl_activate(int active)
421 make sure window is active and mouse grabbed
427 make sure mouse is not grabbed and window minimized
435 void opengl_tcache_flush ();
437 void gr_opengl_preload_init()
439 if (gr_screen.mode != GR_OPENGL) {
443 opengl_tcache_flush ();
446 int GL_should_preload = 0;
447 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
449 if ( gr_screen.mode != GR_OPENGL) {
453 if ( !GL_should_preload ) {
457 float u_scale, v_scale;
461 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
463 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
467 mprintf(("Texture upload failed!\n" ));
473 void gr_opengl_pixel(int x, int y)
478 void gr_opengl_clear()
480 glClearColor(gr_screen.current_clear_color.red / 255.0,
481 gr_screen.current_clear_color.green / 255.0,
482 gr_screen.current_clear_color.blue / 255.0, 1.0);
484 glClear ( GL_COLOR_BUFFER_BIT );
487 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
488 void opengl_tcache_frame ();
489 void gr_opengl_flip()
497 extern int Gr_opengl_mouse_saved;
498 Gr_opengl_mouse_saved = 0;
500 if ( mouse_is_visible() ) {
504 mouse_get_pos( &mx, &my );
506 gr_opengl_save_mouse_area(mx,my,32,32);
508 if ( Gr_cursor == -1 ) {
511 gr_set_bitmap(Gr_cursor);
517 GLenum error = glGetError();
520 error = glGetError();
522 if (error != GL_NO_ERROR) {
523 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
526 } while (error != GL_NO_ERROR);
529 SDL_GL_SwapBuffers ();
531 opengl_tcache_frame ();
533 int cnt = GL_activate;
536 opengl_tcache_flush();
537 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
543 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
547 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
552 void gr_opengl_set_clip(int x,int y,int w,int h)
554 // check for sanity of parameters
560 if (x >= gr_screen.max_w)
561 x = gr_screen.max_w - 1;
562 if (y >= gr_screen.max_h)
563 y = gr_screen.max_h - 1;
565 if (x + w > gr_screen.max_w)
566 w = gr_screen.max_w - x;
567 if (y + h > gr_screen.max_h)
568 h = gr_screen.max_h - y;
570 if (w > gr_screen.max_w)
572 if (h > gr_screen.max_h)
575 gr_screen.offset_x = x;
576 gr_screen.offset_y = y;
577 gr_screen.clip_left = 0;
578 gr_screen.clip_right = w-1;
579 gr_screen.clip_top = 0;
580 gr_screen.clip_bottom = h-1;
581 gr_screen.clip_width = w;
582 gr_screen.clip_height = h;
584 glEnable(GL_SCISSOR_TEST);
585 glScissor(x, gr_screen.max_h-y-h, w, h);
588 void gr_opengl_reset_clip()
590 gr_screen.offset_x = 0;
591 gr_screen.offset_y = 0;
592 gr_screen.clip_left = 0;
593 gr_screen.clip_top = 0;
594 gr_screen.clip_right = gr_screen.max_w - 1;
595 gr_screen.clip_bottom = gr_screen.max_h - 1;
596 gr_screen.clip_width = gr_screen.max_w;
597 gr_screen.clip_height = gr_screen.max_h;
599 glDisable(GL_SCISSOR_TEST);
600 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
603 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
605 gr_screen.current_alpha = alpha;
606 gr_screen.current_alphablend_mode = alphablend_mode;
607 gr_screen.current_bitblt_mode = bitblt_mode;
608 gr_screen.current_bitmap = bitmap_num;
610 gr_screen.current_bitmap_sx = sx;
611 gr_screen.current_bitmap_sy = sy;
614 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
616 shade->screen_sig = gr_screen.signature;
623 void gr_opengl_set_shader( shader * shade )
626 if (shade->screen_sig != gr_screen.signature) {
627 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
629 gr_screen.current_shader = *shade;
631 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
636 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
638 STUB_FUNCTION; /* who called me? */
641 extern int GL_last_bitmap_id;
642 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
646 GLubyte *sptr, *dptr;
650 int cw = min(bmp->w, w);
651 int ch = min(bmp->h, h);
653 GL_last_bitmap_id = -1; /* HACK! */
655 glColor4f(1.0, 1.0, 1.0, 1.0);
656 glBindTexture(GL_TEXTURE_2D, bitmapTex);
659 for (iy = sy; iy < ch; iy += 256) {
661 ih = min(256, (ch-iy));
663 for (ix = sx; ix < cw; ix += 256) {
665 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
667 iw = min(256, (cw-ix));
673 memcpy(dptr, sptr, iw*2);
681 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
691 t = (float)ih / 256.0;
696 s = (float)iw / 256.0;
697 t = (float)ih / 256.0;
702 s = (float)iw / 256.0;
714 bm_unlock(gr_screen.current_bitmap);
719 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
721 STUB_FUNCTION; /* who called me? */
728 int dx1=x, dx2=x+w-1;
729 int dy1=y, dy2=y+h-1;
732 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
737 if ( count > 1 ) Int3();
741 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
742 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
743 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
744 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
745 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
746 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
773 if ( w < 1 ) return; // clipped away!
774 if ( h < 1 ) return; // clipped away!
778 // Make sure clipping algorithm works
782 Assert( w == (dx2-dx1+1) );
783 Assert( h == (dy2-dy1+1) );
786 Assert( sx+w <= bw );
787 Assert( sy+h <= bh );
788 Assert( dx2 >= dx1 );
789 Assert( dy2 >= dy1 );
790 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
791 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
792 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
793 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
796 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
797 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
799 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
803 void gr_opengl_bitmap(int x, int y)
805 STUB_FUNCTION; /* who called me? */
809 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
810 int dx1=x, dx2=x+w-1;
811 int dy1=y, dy2=y+h-1;
814 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
815 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
816 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
817 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
818 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
819 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
821 if ( sx < 0 ) return;
822 if ( sy < 0 ) return;
823 if ( sx >= w ) return;
824 if ( sy >= h ) return;
826 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
828 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
832 void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
836 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
838 saved_zbuf = gr_zbuffer_get();
840 // start the frame, no zbuffering, no culling
842 gr_zbuffer_set(GR_ZBUFF_NONE);
851 v[0].flags = PF_PROJECTED;
858 v[1].sx = i2fl(x + w);
863 v[1].flags = PF_PROJECTED;
870 v[2].sx = i2fl(x + w);
871 v[2].sy = i2fl(y + h);
875 v[2].flags = PF_PROJECTED;
883 v[3].sy = i2fl(y + h);
887 v[3].flags = PF_PROJECTED;
895 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
899 // restore zbuffer and culling
900 gr_zbuffer_set(saved_zbuf);
904 void gr_opengl_rect(int x,int y,int w,int h)
906 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);
909 void gr_opengl_shade(int x,int y,int w,int h)
916 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
917 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
918 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
919 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
920 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
921 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
922 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
923 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
925 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
928 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
933 if ( !gr_screen.current_color.is_alphacolor ) return;
935 float u_scale, v_scale;
937 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
939 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
940 // Couldn't set texture
941 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
945 float u0, u1, v0, v1;
946 float x1, x2, y1, y2;
949 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
951 u0 = u_scale*i2fl(sx)/i2fl(bw);
952 v0 = v_scale*i2fl(sy)/i2fl(bh);
954 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
955 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
957 x1 = i2fl(x+gr_screen.offset_x);
958 y1 = i2fl(y+gr_screen.offset_y);
959 x2 = i2fl(x+w+gr_screen.offset_x);
960 y2 = i2fl(y+h+gr_screen.offset_y);
962 if ( gr_screen.current_color.is_alphacolor ) {
963 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
965 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
969 glTexCoord2f (u0, v1);
970 glVertex3f (x1, y2, -0.99);
972 glTexCoord2f (u1, v1);
973 glVertex3f (x2, y2, -0.99);
975 glTexCoord2f (u1, v0);
976 glVertex3f (x2, y1, -0.99);
978 glTexCoord2f (u0, v0);
979 glVertex3f (x1, y1, -0.99);
983 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
990 int dx1=x, dx2=x+w-1;
991 int dy1=y, dy2=y+h-1;
994 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
999 if ( count > 1 ) Int3();
1003 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1004 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1005 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1006 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1007 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1008 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1025 if ( sx + w > bw ) {
1030 if ( sy + h > bh ) {
1035 if ( w < 1 ) return; // clipped away!
1036 if ( h < 1 ) return; // clipped away!
1040 // Make sure clipping algorithm works
1044 Assert( w == (dx2-dx1+1) );
1045 Assert( h == (dy2-dy1+1) );
1048 Assert( sx+w <= bw );
1049 Assert( sy+h <= bh );
1050 Assert( dx2 >= dx1 );
1051 Assert( dy2 >= dy1 );
1052 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
1053 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
1054 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
1055 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
1058 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
1059 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1062 void gr_opengl_aabitmap(int x, int y)
1066 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
1067 int dx1=x, dx2=x+w-1;
1068 int dy1=y, dy2=y+h-1;
1071 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1072 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1073 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1074 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1075 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1076 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1078 if ( sx < 0 ) return;
1079 if ( sy < 0 ) return;
1080 if ( sx >= w ) return;
1081 if ( sy >= h ) return;
1083 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
1084 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1088 void gr_opengl_string( int sx, int sy, char *s )
1090 int width, spacing, letter;
1093 if ( !Current_font ) {
1097 gr_set_bitmap(Current_font->bitmap_id);
1102 if (sx==0x8000) { //centered
1103 x = get_centered_x(s);
1113 while (*s== '\n' ) {
1115 y += Current_font->h;
1116 if (sx==0x8000) { //centered
1117 x = get_centered_x(s);
1122 if (*s == 0 ) break;
1124 letter = get_char_width(s[0],s[1],&width,&spacing);
1127 //not in font, draw as space
1135 // Check if this character is totally clipped
1136 if ( x + width < gr_screen.clip_left ) continue;
1137 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1138 if ( x > gr_screen.clip_right ) continue;
1139 if ( y > gr_screen.clip_bottom ) continue;
1142 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1143 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1147 wc = width - xd; hc = Current_font->h - yd;
1148 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1149 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1151 if ( wc < 1 ) continue;
1152 if ( hc < 1 ) continue;
1154 int u = Current_font->bm_u[letter];
1155 int v = Current_font->bm_v[letter];
1157 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1161 void gr_opengl_line(int x1,int y1,int x2,int y2)
1163 int clipped = 0, swapped=0;
1165 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1167 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);
1172 sx1 = i2fl(x1 + gr_screen.offset_x);
1173 sy1 = i2fl(y1 + gr_screen.offset_y);
1174 sx2 = i2fl(x2 + gr_screen.offset_x);
1175 sy2 = i2fl(y2 + gr_screen.offset_y);
1177 if ( x1 == x2 && y1 == y2 ) {
1178 glBegin (GL_POINTS);
1179 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1180 glVertex3f (sx1, sy1, -0.99f);
1192 } else if ( y1 == y2 ) {
1201 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1202 glVertex3f (sx2, sy2, -0.99f);
1203 glVertex3f (sx1, sy1, -0.99f);
1207 void gr_opengl_aaline(vertex *v1, vertex *v2)
1209 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1212 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1214 int clipped = 0, swapped=0;
1216 if ( !gr_screen.current_color.is_alphacolor ) {
1217 gr_line( x1, y1, x2, y2 );
1221 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);
1223 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1225 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1226 int ba = swapped ? gr_screen.current_color.alpha : 0;
1231 sx1 = i2fl(x1 + gr_screen.offset_x);
1232 sy1 = i2fl(y1 + gr_screen.offset_y);
1233 sx2 = i2fl(x2 + gr_screen.offset_x);
1234 sy2 = i2fl(y2 + gr_screen.offset_y);
1242 } else if ( y1 == y2 ) {
1251 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1252 glVertex3f (sx2, sy2, -0.99f);
1253 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1254 glVertex3f (sx1, sy1, -0.99f);
1258 void gr_opengl_circle( int xc, int yc, int d )
1268 if ( (xc+r) < gr_screen.clip_left ) return;
1269 if ( (xc-r) > gr_screen.clip_right ) return;
1270 if ( (yc+r) < gr_screen.clip_top ) return;
1271 if ( (yc-r) > gr_screen.clip_bottom ) return;
1274 // Draw the first octant
1275 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1276 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1281 // Draw the second octant
1282 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1283 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1291 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1292 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1297 void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1301 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1302 if (f_float < 0.0f) {
1310 *a = (int)(f_float * 255.0);
1313 void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1316 float u_scale = 1.0f, v_scale = 1.0f;
1318 // Make nebula use the texture mapper... this blends the colors better.
1319 if ( flags & TMAP_FLAG_NEBULA ){
1323 gr_texture_source texture_source = (gr_texture_source)-1;
1324 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1325 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1327 if ( gr_zbuffering ) {
1328 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1329 zbuffer_type = ZBUFFER_TYPE_READ;
1331 zbuffer_type = ZBUFFER_TYPE_FULL;
1334 zbuffer_type = ZBUFFER_TYPE_NONE;
1339 int tmap_type = TCACHE_TYPE_NORMAL;
1343 if ( flags & TMAP_FLAG_TEXTURED ) {
1346 r = gr_screen.current_color.red;
1347 g = gr_screen.current_color.green;
1348 b = gr_screen.current_color.blue;
1351 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1354 tmap_type = TCACHE_TYPE_NORMAL;
1355 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1357 // Blend with screen pixel using src*alpha+dst
1358 float factor = gr_screen.current_alpha;
1362 if ( factor <= 1.0f ) {
1363 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1364 r = (r*tmp_alpha)/255;
1365 g = (g*tmp_alpha)/255;
1366 b = (b*tmp_alpha)/255;
1369 tmap_type = TCACHE_TYPE_XPARENT;
1371 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1373 // Blend with screen pixel using src*alpha+dst
1374 float factor = gr_screen.current_alpha;
1376 if ( factor > 1.0f ) {
1379 alpha = fl2i(gr_screen.current_alpha*255.0f);
1383 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1384 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1386 alpha_blend = ALPHA_BLEND_NONE;
1391 if(flags & TMAP_FLAG_BITMAP_SECTION){
1392 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1395 texture_source = TEXTURE_SOURCE_NONE;
1397 if ( flags & TMAP_FLAG_TEXTURED ) {
1398 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 ))
1400 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1404 // use nonfiltered textures for bitmap sections
1405 if(flags & TMAP_FLAG_BITMAP_SECTION){
1406 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1408 texture_source = TEXTURE_SOURCE_DECAL;
1413 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1415 if ( flags & TMAP_FLAG_TEXTURED )
1421 if (flags & TMAP_FLAG_PIXEL_FOG) {
1427 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1429 vertex * va = verts[i];
1433 x = fl2i(va->sx*16.0f);
1434 y = fl2i(va->sy*16.0f);
1436 x += gr_screen.offset_x*16;
1437 y += gr_screen.offset_y*16;
1439 sx = i2fl(x) / 16.0f;
1440 sy = i2fl(y) / 16.0f;
1442 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1453 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1456 glBegin(GL_TRIANGLE_FAN);
1457 for (i = nv-1; i >= 0; i--) {
1458 vertex * va = verts[i];
1464 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1465 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1474 if ( flags & TMAP_FLAG_CORRECT ) {
1480 if (flags & TMAP_FLAG_ALPHA) {
1486 if (flags & TMAP_FLAG_NEBULA ) {
1487 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1488 r = gr_palette[pal*3+0];
1489 g = gr_palette[pal*3+1];
1490 b = gr_palette[pal*3+2];
1491 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1492 r = Gr_gamma_lookup[verts[i]->b];
1493 g = Gr_gamma_lookup[verts[i]->b];
1494 b = Gr_gamma_lookup[verts[i]->b];
1495 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1496 // Make 0.75 be 256.0f
1497 r = Gr_gamma_lookup[verts[i]->r];
1498 g = Gr_gamma_lookup[verts[i]->g];
1499 b = Gr_gamma_lookup[verts[i]->b];
1501 // use constant RGB values...
1503 glColor4ub (r,g,b,a);
1505 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1508 /* this is for GL_EXT_SECONDARY_COLOR */
1509 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1510 /* do separate color call here */
1516 x = fl2i(va->sx*16.0f);
1517 y = fl2i(va->sy*16.0f);
1519 x += gr_screen.offset_x*16;
1520 y += gr_screen.offset_y*16;
1522 sx = i2fl(x) / 16.0f;
1523 sy = i2fl(y) / 16.0f;
1525 if ( flags & TMAP_FLAG_TEXTURED ) {
1528 glTexCoord2f(tu, tv);
1531 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1536 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1538 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1541 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1543 void gr_opengl_scaler(vertex *va, vertex *vb )
1545 float x0, y0, x1, y1;
1546 float u0, v0, u1, v1;
1547 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1548 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1549 float xmin, xmax, ymin, ymax;
1550 int dx0, dy0, dx1, dy1;
1552 //============= CLIP IT =====================
1554 x0 = va->sx; y0 = va->sy;
1555 x1 = vb->sx; y1 = vb->sy;
1557 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1558 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1560 u0 = va->u; v0 = va->v;
1561 u1 = vb->u; v1 = vb->v;
1563 // Check for obviously offscreen bitmaps...
1564 if ( (y1<=y0) || (x1<=x0) ) return;
1565 if ( (x1<xmin ) || (x0>xmax) ) return;
1566 if ( (y1<ymin ) || (y0>ymax) ) return;
1568 clipped_u0 = u0; clipped_v0 = v0;
1569 clipped_u1 = u1; clipped_v1 = v1;
1571 clipped_x0 = x0; clipped_y0 = y0;
1572 clipped_x1 = x1; clipped_y1 = y1;
1574 // Clip the left, moving u0 right as necessary
1576 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1580 // Clip the right, moving u1 left as necessary
1582 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1586 // Clip the top, moving v0 down as necessary
1588 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1592 // Clip the bottom, moving v1 up as necessary
1594 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1598 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1599 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1601 if (dx1<=dx0) return;
1602 if (dy1<=dy0) return;
1604 //============= DRAW IT =====================
1618 v[1].sx = clipped_x1;
1619 v[1].sy = clipped_y0;
1622 v[1].u = clipped_u1;
1623 v[1].v = clipped_v0;
1626 v[2].sx = clipped_x1;
1627 v[2].sy = clipped_y1;
1630 v[2].u = clipped_u1;
1631 v[2].v = clipped_v1;
1634 v[3].sx = clipped_x0;
1635 v[3].sy = clipped_y1;
1638 v[3].u = clipped_u0;
1639 v[3].v = clipped_v1;
1641 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1644 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1648 void gr_opengl_get_color( int * r, int * g, int * b )
1650 if (r) *r = gr_screen.current_color.red;
1651 if (g) *g = gr_screen.current_color.green;
1652 if (b) *b = gr_screen.current_color.blue;
1655 void gr_opengl_init_color(color *c, int r, int g, int b)
1657 c->screen_sig = gr_screen.signature;
1658 c->red = (unsigned char)r;
1659 c->green = (unsigned char)g;
1660 c->blue = (unsigned char)b;
1662 c->ac_type = AC_TYPE_NONE;
1664 c->is_alphacolor = 0;
1668 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1670 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1671 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1672 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1673 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1675 gr_opengl_init_color( clr, r, g, b );
1677 clr->alpha = (unsigned char)alpha;
1678 clr->ac_type = (ubyte)type;
1679 clr->alphacolor = -1;
1680 clr->is_alphacolor = 1;
1683 void gr_opengl_set_color( int r, int g, int b )
1685 Assert((r >= 0) && (r < 256));
1686 Assert((g >= 0) && (g < 256));
1687 Assert((b >= 0) && (b < 256));
1689 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1692 void gr_opengl_set_color_fast(color *dst)
1694 if ( dst->screen_sig != gr_screen.signature ) {
1695 if ( dst->is_alphacolor ) {
1696 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1698 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1701 gr_screen.current_color = *dst;
1704 void gr_opengl_print_screen(char *filename)
1709 int gr_opengl_supports_res_ingame(int res)
1716 int gr_opengl_supports_res_interface(int res)
1723 void opengl_tcache_cleanup ();
1724 void gr_opengl_cleanup()
1726 if ( !Inited ) return;
1733 opengl_tcache_cleanup ();
1738 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1740 Assert((r >= 0) && (r < 256));
1741 Assert((g >= 0) && (g < 256));
1742 Assert((b >= 0) && (b < 256));
1744 if (fog_mode == GR_FOGMODE_NONE) {
1745 if (gr_screen.current_fog_mode != fog_mode) {
1748 gr_screen.current_fog_mode = fog_mode;
1753 if (gr_screen.current_fog_mode != fog_mode) {
1756 if (D3D_fog_mode == 2) {
1757 glFogi(GL_FOG_MODE, GL_LINEAR);
1760 gr_screen.current_fog_mode = fog_mode;
1763 if ( (gr_screen.current_fog_color.red != r) ||
1764 (gr_screen.current_fog_color.green != g) ||
1765 (gr_screen.current_fog_color.blue != b) ) {
1768 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1770 fc[0] = (float)r/255.0;
1771 fc[1] = (float)g/255.0;
1772 fc[2] = (float)b/255.0;
1775 glFogfv(GL_FOG_COLOR, fc);
1778 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1779 ((fog_near != gr_screen.fog_near) ||
1780 (fog_far != gr_screen.fog_far)) ) {
1781 gr_screen.fog_near = fog_near;
1782 gr_screen.fog_far = fog_far;
1784 if (D3D_fog_mode == 2) {
1785 glFogf(GL_FOG_START, fog_near);
1786 glFogf(GL_FOG_END, fog_far);
1791 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1796 void gr_opengl_set_cull(int cull)
1799 glEnable (GL_CULL_FACE);
1800 glFrontFace (GL_CCW);
1802 glDisable (GL_CULL_FACE);
1806 void gr_opengl_filter_set(int filter)
1811 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1814 gr_set_bitmap(bmap1);
1817 gr_set_bitmap(bmap2);
1823 typedef struct tcache_slot_opengl {
1824 GLuint texture_handle;
1825 float u_scale, v_scale;
1828 char used_this_frame;
1833 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1834 tcache_slot_opengl *parent;
1835 } tcache_slot_opengl;
1837 static void *Texture_sections = NULL;
1838 tcache_slot_opengl *Textures = NULL;
1840 int GL_texture_sections = 0;
1841 int GL_texture_ram = 0;
1842 int GL_frame_count = 0;
1843 int GL_min_texture_width = 0;
1844 int GL_max_texture_width = 0;
1845 int GL_min_texture_height = 0;
1846 int GL_max_texture_height = 0;
1847 int GL_square_textures = 0;
1848 int GL_textures_in = 0;
1849 int GL_textures_in_frame = 0;
1850 int GL_last_bitmap_id = -1;
1851 int GL_last_detail = -1;
1852 int GL_last_bitmap_type = -1;
1853 int GL_last_section_x = -1;
1854 int GL_last_section_y = -1;
1858 void opengl_tcache_init (int use_sections)
1862 // DDOI - FIXME skipped a lot of stuff here
1863 GL_should_preload = 0;
1865 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1868 if ( tmp_pl == 0 ) {
1869 GL_should_preload = 0;
1870 } else if ( tmp_pl == 1 ) {
1871 GL_should_preload = 1;
1873 GL_should_preload = 1;
1878 GL_min_texture_width = 16;
1879 GL_min_texture_height = 16;
1880 GL_max_texture_width = 256;
1881 GL_max_texture_height = 256;
1883 GL_square_textures = 1;
1885 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1891 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1892 if(!Texture_sections){
1895 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1898 // Init the texture structures
1899 int section_count = 0;
1900 for( i=0; i<MAX_BITMAPS; i++ ) {
1902 Textures[i].vram_texture = NULL;
1903 Textures[i].vram_texture_surface = NULL;
1905 Textures[i].texture_handle = 0;
1907 Textures[i].bitmap_id = -1;
1908 Textures[i].size = 0;
1909 Textures[i].used_this_frame = 0;
1911 Textures[i].parent = NULL;
1913 // allocate sections
1915 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1916 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1917 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1918 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1920 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1921 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1923 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1924 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1925 Textures[i].data_sections[idx][s_idx]->size = 0;
1926 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1930 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1931 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1932 Textures[i].data_sections[idx][s_idx] = NULL;
1938 GL_texture_sections = use_sections;
1940 //GL_last_detail = Detail.hardware_textures;
1941 GL_last_bitmap_id = -1;
1942 GL_last_bitmap_type = -1;
1944 GL_last_section_x = -1;
1945 GL_last_section_y = -1;
1948 GL_textures_in_frame = 0;
1951 int opengl_free_texture (tcache_slot_opengl *t);
1953 void opengl_free_texture_with_handle(int handle)
1955 for(int i=0; i<MAX_BITMAPS; i++ ) {
1956 if (Textures[i].bitmap_id == handle) {
1957 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1958 opengl_free_texture ( &Textures[i] );
1963 void opengl_tcache_flush ()
1967 for( i=0; i<MAX_BITMAPS; i++ ) {
1968 opengl_free_texture ( &Textures[i] );
1970 if (GL_textures_in != 0) {
1971 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1975 GL_last_bitmap_id = -1;
1976 GL_last_section_x = -1;
1977 GL_last_section_y = -1;
1980 void opengl_tcache_cleanup ()
1982 opengl_tcache_flush ();
1985 GL_textures_in_frame = 0;
1992 if( Texture_sections != NULL ){
1993 free(Texture_sections);
1994 Texture_sections = NULL;
1998 void opengl_tcache_frame ()
2002 GL_last_bitmap_id = -1;
2003 GL_textures_in_frame = 0;
2008 for( i=0; i<MAX_BITMAPS; i++ ) {
2009 Textures[i].used_this_frame = 0;
2012 if(Textures[i].data_sections[0][0] != NULL){
2013 Assert(GL_texture_sections);
2014 if(GL_texture_sections){
2015 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2016 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2017 if(Textures[i].data_sections[idx][s_idx] != NULL){
2018 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
2027 opengl_tcache_flush();
2032 int opengl_free_texture ( tcache_slot_opengl *t )
2038 if ( t->bitmap_id > -1 ) {
2039 // if I, or any of my children have been used this frame, bail
2040 if(t->used_this_frame){
2043 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2044 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2045 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
2051 // ok, now we know its legal to free everything safely
2052 glDeleteTextures (1, &t->texture_handle);
2053 t->texture_handle = 0;
2055 if ( GL_last_bitmap_id == t->bitmap_id ) {
2056 GL_last_bitmap_id = -1;
2059 // if this guy has children, free them too, since the children
2060 // actually make up his size
2061 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2062 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2063 if(t->data_sections[idx][s_idx] != NULL){
2064 opengl_free_texture(t->data_sections[idx][s_idx]);
2070 t->used_this_frame = 0;
2071 GL_textures_in -= t->size;
2077 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
2082 if((w_out == NULL) || (h_out == NULL)){
2092 for (i=0; i<16; i++ ) {
2093 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2099 for (i=0; i<16; i++ ) {
2100 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2107 if ( tex_w < GL_min_texture_width ) {
2108 tex_w = GL_min_texture_width;
2109 } else if ( tex_w > GL_max_texture_width ) {
2110 tex_w = GL_max_texture_width;
2113 if ( tex_h < GL_min_texture_height ) {
2114 tex_h = GL_min_texture_height;
2115 } else if ( tex_h > GL_max_texture_height ) {
2116 tex_h = GL_max_texture_height;
2119 if ( GL_square_textures ) {
2121 // Make the both be equal to larger of the two
2122 new_size = max(tex_w, tex_h);
2127 // store the outgoing size
2132 // data == start of bitmap data
2133 // sx == x offset into bitmap
2134 // sy == y offset into bitmap
2135 // src_w == absolute width of section on source bitmap
2136 // src_h == absolute height of section on source bitmap
2137 // bmap_w == width of source bitmap
2138 // bmap_h == height of source bitmap
2139 // tex_w == width of final texture
2140 // tex_h == height of final texture
2141 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)
2150 if ( t->used_this_frame ) {
2151 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2156 if(!opengl_free_texture(t)){
2161 // get final texture size
2162 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2164 if ( (tex_w < 1) || (tex_h < 1) ) {
2165 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2169 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2170 t->u_scale = (float)bmap_w / (float)tex_w;
2171 t->v_scale = (float)bmap_h / (float)tex_h;
2172 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2173 t->u_scale = (float)src_w / (float)tex_w;
2174 t->v_scale = (float)src_h / (float)tex_h;
2181 glGenTextures (1, &t->texture_handle);
2184 if (t->texture_handle == 0) {
2185 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2189 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2191 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2192 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2193 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2194 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2196 switch (bitmap_type) {
2198 case TCACHE_TYPE_AABITMAP:
2201 ubyte *bmp_data = ((ubyte*)data);
2202 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2203 ubyte *texmemp = texmem;
2206 for (i=0; i<16; i++) {
2207 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2210 for ( ; i<256; i++ ) {
2214 for (i=0;i<tex_h;i++)
2216 for (j=0;j<tex_w;j++)
2218 if (i < bmap_h && j < bmap_w) {
2220 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2228 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2233 case TCACHE_TYPE_BITMAP_SECTION:
2236 ubyte *bmp_data = ((ubyte*)data);
2237 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2238 ubyte *texmemp = texmem;
2240 for (i=0;i<tex_h;i++)
2242 for (j=0;j<tex_w;j++)
2244 if (i < src_h && j < src_w) {
2245 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2246 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2253 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2254 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2262 ubyte *bmp_data = ((ubyte*)data);
2263 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2264 ubyte *texmemp = texmem;
2266 fix u, utmp, v, du, dv;
2270 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2271 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2273 for (j=0;j<tex_h;j++)
2276 for (i=0;i<tex_w;i++)
2278 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2279 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2285 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2286 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2294 t->bitmap_id = texture_handle;
2295 t->time_created = GL_frame_count;
2296 t->used_this_frame = 0;
2297 t->size = tex_w * tex_h * 2;
2298 t->w = (ushort)tex_w;
2299 t->h = (ushort)tex_h;
2300 GL_textures_in_frame += t->size;
2302 GL_textures_in += t->size;
2308 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2312 int final_w, final_h;
2316 // setup texture/bitmap flags
2318 switch(bitmap_type){
2319 case TCACHE_TYPE_AABITMAP:
2320 flags |= BMP_AABITMAP;
2323 case TCACHE_TYPE_NORMAL:
2324 flags |= BMP_TEX_OTHER;
2325 case TCACHE_TYPE_XPARENT:
2326 flags |= BMP_TEX_XPARENT;
2328 case TCACHE_TYPE_NONDARKENING:
2330 flags |= BMP_TEX_NONDARK;
2334 // lock the bitmap into the proper format
2335 bmp = bm_lock(bitmap_handle, bpp, flags);
2336 if ( bmp == NULL ) {
2337 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2346 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2347 // max_w /= D3D_texture_divider;
2348 // max_h /= D3D_texture_divider;
2350 // Detail.debris_culling goes from 0 to 4.
2351 max_w /= (16 >> Detail.hardware_textures);
2352 max_h /= (16 >> Detail.hardware_textures);
2356 // get final texture size as it will be allocated as a DD surface
2357 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2359 // if this tcache slot has no bitmap
2360 if ( tslot->bitmap_id < 0) {
2363 // different bitmap altogether - determine if the new one can use the old one's slot
2364 else if (tslot->bitmap_id != bitmap_handle) {
2365 if((final_w == tslot->w) && (final_h == tslot->h)){
2367 //ml_printf("Reloading texture %d\n", bitmap_handle);
2374 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);
2376 // unlock the bitmap
2377 bm_unlock(bitmap_handle);
2382 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2386 int final_w, final_h;
2387 int section_x, section_y;
2390 // setup texture/bitmap flags
2391 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2392 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2393 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2395 flags = BMP_TEX_XPARENT;
2397 // lock the bitmap in the proper format
2398 bmp = bm_lock(bitmap_handle, 16, flags);
2399 if ( bmp == NULL ) {
2400 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2403 // determine the width and height of this section
2404 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2406 // get final texture size as it will be allocated as an opengl texture
2407 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2409 // if this tcache slot has no bitmap
2410 if ( tslot->bitmap_id < 0) {
2413 // different bitmap altogether - determine if the new one can use the old one's slot
2414 else if (tslot->bitmap_id != bitmap_handle) {
2415 if((final_w == tslot->w) && (final_h == tslot->h)){
2423 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);
2425 // unlock the bitmap
2426 bm_unlock(bitmap_handle);
2432 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2433 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)
2442 GL_last_bitmap_id = -1;
2446 if ( GL_last_detail != Detail.hardware_textures ) {
2447 GL_last_detail = Detail.hardware_textures;
2448 opengl_tcache_flush();
2455 int n = bm_get_cache_slot (bitmap_id, 1);
2456 tcache_slot_opengl *t = &Textures[n];
2458 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)) {
2459 t->used_this_frame++;
2461 // mark all children as used
2462 if(GL_texture_sections){
2463 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2464 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2465 if(t->data_sections[idx][s_idx] != NULL){
2466 t->data_sections[idx][s_idx]->used_this_frame++;
2472 *u_scale = t->u_scale;
2473 *v_scale = t->v_scale;
2477 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2478 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2479 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2485 // if the texture sections haven't been created yet
2486 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2488 // lock the bitmap in the proper format
2489 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2490 bm_unlock(bitmap_id);
2492 // now lets do something for each texture
2494 for(idx=0; idx<bmp->sections.num_x; idx++){
2495 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2496 // hmm. i'd rather we didn't have to do it this way...
2497 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2501 // not used this frame
2502 t->data_sections[idx][s_idx]->used_this_frame = 0;
2506 // zero out pretty much everything in the parent struct since he's just the root
2507 t->bitmap_id = bitmap_id;
2508 t->texture_handle = 0;
2509 t->time_created = t->data_sections[sx][sy]->time_created;
2510 t->used_this_frame = 0;
2512 t->vram_texture = NULL;
2513 t->vram_texture_surface = NULL
2517 // argh. we failed to upload. free anything we can
2519 opengl_free_texture(t);
2521 // swap in the texture we want
2523 t = t->data_sections[sx][sy];
2526 // all other "normal" textures
2527 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2528 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2531 // everything went ok
2532 if(ret_val && (t->texture_handle) && !vram_full){
2533 *u_scale = t->u_scale;
2534 *v_scale = t->v_scale;
2536 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2538 GL_last_bitmap_id = t->bitmap_id;
2539 GL_last_bitmap_type = bitmap_type;
2540 GL_last_section_x = sx;
2541 GL_last_section_y = sy;
2543 t->used_this_frame++;
2547 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2554 void gr_opengl_set_clear_color(int r, int g, int b)
2556 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2559 void gr_opengl_flash(int r, int g, int b)
2565 if ( r || g || b ) {
2566 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2568 float x1, x2, y1, y2;
2569 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2570 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2571 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2572 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2574 glColor4ub(r, g, b, 255);
2576 glVertex3f (x1, y2, -0.99);
2578 glVertex3f (x2, y2, -0.99);
2580 glVertex3f (x2, y1, -0.99);
2582 glVertex3f (x1, y1, -0.99);
2587 int gr_opengl_zbuffer_get()
2589 if ( !gr_global_zbuffering ) {
2590 return GR_ZBUFF_NONE;
2592 return gr_zbuffering_mode;
2595 int gr_opengl_zbuffer_set(int mode)
2597 int tmp = gr_zbuffering_mode;
2599 gr_zbuffering_mode = mode;
2601 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2609 void gr_opengl_zbuffer_clear(int mode)
2613 gr_zbuffering_mode = GR_ZBUFF_FULL;
2614 gr_global_zbuffering = 1;
2616 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2617 glClear ( GL_DEPTH_BUFFER_BIT );
2620 gr_zbuffering_mode = GR_ZBUFF_NONE;
2621 gr_global_zbuffering = 0;
2625 void gr_opengl_set_gamma(float gamma)
2628 Gr_gamma_int = int (Gr_gamma*10);
2630 // Create the Gamma lookup table
2632 for (i=0;i<256; i++) {
2633 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2636 } else if ( v < 0 ) {
2639 Gr_gamma_lookup[i] = v;
2642 // Flush any existing textures
2643 opengl_tcache_flush();
2646 void gr_opengl_fade_in(int instantaneous)
2651 void gr_opengl_fade_out(int instantaneous)
2656 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2659 glReadBuffer(GL_FRONT);
2661 glReadBuffer(GL_BACK);
2664 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2666 glPixelStorei(GL_PACK_ROW_LENGTH, gr_screen.max_w);
2668 if (gr_screen.bits_per_pixel == 15) {
2669 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2670 } else if (gr_screen.bits_per_pixel == 32) {
2671 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2674 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
2677 static int Gr_opengl_mouse_saved = 0;
2678 static int Gr_opengl_mouse_saved_x1 = 0;
2679 static int Gr_opengl_mouse_saved_y1 = 0;
2680 static int Gr_opengl_mouse_saved_x2 = 0;
2681 static int Gr_opengl_mouse_saved_y2 = 0;
2682 static int Gr_opengl_mouse_saved_w = 0;
2683 static int Gr_opengl_mouse_saved_h = 0;
2684 #define MAX_SAVE_SIZE (32*32)
2685 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2687 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2689 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2691 Gr_opengl_mouse_saved_x1 = x;
2692 Gr_opengl_mouse_saved_y1 = y;
2693 Gr_opengl_mouse_saved_x2 = x+w-1;
2694 Gr_opengl_mouse_saved_y2 = y+h-1;
2696 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2697 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2698 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2699 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2701 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2702 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2704 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2705 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2707 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2709 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2711 glReadBuffer(GL_BACK);
2712 glReadPixels(x, gr_screen.max_h-y-1-h, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, Gr_opengl_mouse_saved_data);
2714 Gr_opengl_mouse_saved = 1;
2717 int gr_opengl_save_screen()
2721 if ( Gr_saved_screen ) {
2722 mprintf(( "Screen alread saved!\n" ));
2726 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2727 if (!Gr_saved_screen) {
2728 mprintf(( "Couldn't get memory for saved screen!\n" ));
2732 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2733 if (!Gr_saved_screen_tmp) {
2734 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2738 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2740 glReadBuffer(GL_FRONT);
2741 glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, Gr_saved_screen_tmp);
2745 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2746 dptr = (ubyte *)Gr_saved_screen;
2747 for (int j = 0; j < gr_screen.max_h; j++) {
2748 sptr -= gr_screen.max_w*2;
2749 memcpy(dptr, sptr, gr_screen.max_w*2);
2750 dptr += gr_screen.max_w*2;
2753 free(Gr_saved_screen_tmp);
2755 if (Gr_opengl_mouse_saved) {
2756 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2757 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2+1)*gr_screen.max_w)];
2758 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2759 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2762 dptr -= gr_screen.max_w*2;
2766 // this leaks texture handles, and the opengl doesn't currently
2767 // perform some sort of garbage collection, so a hack was added
2768 // to bmpman to make it free textures
2769 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2771 return Gr_saved_screen_bitmap;
2774 void gr_opengl_restore_screen(int id)
2778 if ( !Gr_saved_screen ) {
2783 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2785 gr_set_bitmap(Gr_saved_screen_bitmap);
2789 void gr_opengl_free_screen(int id)
2791 bm_release(Gr_saved_screen_bitmap);
2793 if ( Gr_saved_screen ) {
2794 free( Gr_saved_screen );
2795 Gr_saved_screen = NULL;
2799 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2804 void gr_opengl_dump_frame_stop()
2809 void gr_opengl_dump_frame()
2814 uint gr_opengl_lock()
2821 void gr_opengl_unlock()
2825 void opengl_zbias(int bias)
2828 glEnable(GL_POLYGON_OFFSET_FILL);
2829 glPolygonOffset(0.0, -bias);
2831 glDisable(GL_POLYGON_OFFSET_FILL);
2835 extern char *Osreg_title;
2836 void gr_opengl_init()
2839 gr_opengl_cleanup();
2843 mprintf(( "Initializing opengl graphics device...\n" ));
2847 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2849 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2855 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2856 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2857 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2858 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2859 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2861 int flags = SDL_OPENGL;
2863 if (os_config_read_uint( NULL, "Fullscreen", 0 ) == 1)
2864 flags |= SDL_FULLSCREEN;
2866 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2868 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2873 SDL_WM_SetCaption (Osreg_title, "FS2");
2875 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2877 glMatrixMode(GL_PROJECTION);
2879 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2880 glMatrixMode(GL_MODELVIEW);
2883 glShadeModel(GL_SMOOTH);
2884 glEnable(GL_DITHER);
2885 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2886 glHint(GL_FOG_HINT, GL_NICEST);
2888 glEnable(GL_DEPTH_TEST);
2891 glEnable(GL_TEXTURE_2D);
2893 glDepthRange(0.0, 1.0);
2895 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2896 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2898 D3D_32bit = 1; // grd3d.cpp
2899 extern int D3D_enabled;
2902 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2903 1 = use secondary color ext
2904 2 = use opengl linear fog
2906 D3D_fog_mode = 2; // grd3d.cpp
2910 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2917 gr_screen.bits_per_pixel = 15;
2918 gr_screen.bytes_per_pixel = 2;
2923 Gr_red.mask = 0x7C00;
2928 Gr_green.mask = 0x3E0;
2933 Gr_blue.mask = 0x1F;
2938 gr_screen.bits_per_pixel = 16;
2939 gr_screen.bytes_per_pixel = 2;
2944 Gr_red.mask = 0xF800;
2949 Gr_green.mask = 0x7E0;
2954 Gr_blue.mask = 0x1F;
2959 gr_screen.bits_per_pixel = 32;
2960 gr_screen.bytes_per_pixel = 4;
2965 Gr_red.mask = 0xff0000;
2970 Gr_green.mask = 0xff00;
2975 Gr_blue.mask = 0xff;
2980 Int3(); // Illegal bpp
2983 // DDOI - set these so no one else does!
2984 Gr_ta_red.mask = 0x0f00;
2985 Gr_ta_red.shift = 8;
2986 Gr_ta_red.scale = 16;
2988 Gr_ta_green.mask = 0x00f0;
2989 Gr_ta_green.shift = 4;
2990 Gr_ta_green.scale = 16;
2992 Gr_ta_blue.mask = 0x000f;
2993 Gr_ta_blue.shift = 0;
2994 Gr_ta_blue.scale = 16;
2996 Gr_ta_alpha.mask = 0xf000;
2997 Gr_ta_alpha.shift = 12;
2998 Gr_ta_alpha.scale = 16;
3001 opengl_tcache_init (1);
3004 Gr_current_red = &Gr_red;
3005 Gr_current_blue = &Gr_blue;
3006 Gr_current_green = &Gr_green;
3007 Gr_current_alpha = &Gr_alpha;
3009 gr_screen.gf_flip = gr_opengl_flip;
3010 gr_screen.gf_flip_window = gr_opengl_flip_window;
3011 gr_screen.gf_set_clip = gr_opengl_set_clip;
3012 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3013 gr_screen.gf_set_font = grx_set_font;
3015 gr_screen.gf_set_color = gr_opengl_set_color;
3016 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3017 gr_screen.gf_create_shader = gr_opengl_create_shader;
3018 gr_screen.gf_set_shader = gr_opengl_set_shader;
3019 gr_screen.gf_clear = gr_opengl_clear;
3020 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3021 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3022 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3023 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3025 gr_screen.gf_rect = gr_opengl_rect;
3026 gr_screen.gf_shade = gr_opengl_shade;
3027 gr_screen.gf_string = gr_opengl_string;
3028 gr_screen.gf_circle = gr_opengl_circle;
3030 gr_screen.gf_line = gr_opengl_line;
3031 gr_screen.gf_aaline = gr_opengl_aaline;
3032 gr_screen.gf_pixel = gr_opengl_pixel;
3033 gr_screen.gf_scaler = gr_opengl_scaler;
3034 gr_screen.gf_tmapper = gr_opengl_tmapper;
3036 gr_screen.gf_gradient = gr_opengl_gradient;
3038 gr_screen.gf_set_palette = gr_opengl_set_palette;
3039 gr_screen.gf_get_color = gr_opengl_get_color;
3040 gr_screen.gf_init_color = gr_opengl_init_color;
3041 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3042 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3043 gr_screen.gf_print_screen = gr_opengl_print_screen;
3045 gr_screen.gf_fade_in = gr_opengl_fade_in;
3046 gr_screen.gf_fade_out = gr_opengl_fade_out;
3047 gr_screen.gf_flash = gr_opengl_flash;
3049 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3050 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3051 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3053 gr_screen.gf_save_screen = gr_opengl_save_screen;
3054 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3055 gr_screen.gf_free_screen = gr_opengl_free_screen;
3057 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3058 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3059 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3061 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3063 gr_screen.gf_lock = gr_opengl_lock;
3064 gr_screen.gf_unlock = gr_opengl_unlock;
3066 gr_screen.gf_fog_set = gr_opengl_fog_set;
3068 gr_screen.gf_get_region = gr_opengl_get_region;
3070 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3072 gr_screen.gf_set_cull = gr_opengl_set_cull;
3074 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3076 gr_screen.gf_filter_set = gr_opengl_filter_set;
3078 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3080 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;