2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
15 * Code that uses the OpenGL graphics library
18 * Revision 1.63 2003/05/09 05:05:52 taylor
19 * improve gr_(de)activate, mouse grab, default fullscreen
21 * Revision 1.62 2003/05/04 04:56:53 taylor
22 * move SDL_Quit to os_deinit to fix fonttool segfault
24 * Revision 1.61 2003/02/02 21:13:27 relnev
25 * minor updates (made functions static, tried to improve texture sizes)
27 * Revision 1.60 2003/02/02 20:20:03 relnev
28 * add large texture support (Taylor Richards)
30 * Revision 1.59 2002/09/04 01:38:09 relnev
33 * Revision 1.57 2002/08/31 01:39:13 theoddone33
34 * Speed up the renderer a tad
36 * Revision 1.56 2002/08/01 05:03:11 relnev
39 * Revision 1.55 2002/08/01 04:55:45 relnev
40 * experimenting with texture state
42 * Revision 1.54 2002/07/30 15:00:15 relnev
43 * not use luminance alpha by default
45 * Revision 1.53 2002/06/22 17:08:16 relnev
46 * more fixes to unused alpha code
48 * Revision 1.52 2002/06/21 23:59:14 relnev
49 * moved a gr_opengl_set_state to after the gr_tcache_set
51 * Revision 1.51 2002/06/19 04:52:45 relnev
52 * MacOS X updates (Ryan)
54 * Revision 1.50 2002/06/18 23:20:44 relnev
57 * adjusted line offsets.
59 * Revision 1.49 2002/06/17 23:11:39 relnev
60 * enable sdl key repeating.
64 * Revision 1.48 2002/06/09 04:41:17 relnev
65 * added copyright header
67 * Revision 1.47 2002/06/09 03:16:04 relnev
70 * removed unneeded asm, old sdl 2d setup.
72 * fixed crash caused by opengl_get_region.
74 * Revision 1.46 2002/06/05 04:03:32 relnev
75 * finished cfilesystem.
77 * removed some old code.
79 * fixed mouse save off-by-one.
83 * Revision 1.45 2002/06/03 09:25:37 relnev
84 * implement mouse cursor and screen save/restore
86 * Revision 1.44 2002/06/02 18:46:59 relnev
89 * Revision 1.43 2002/06/02 11:34:00 relnev
92 * Revision 1.42 2002/06/02 10:28:17 relnev
93 * fix texture handle leak
95 * Revision 1.41 2002/06/01 09:00:34 relnev
96 * silly debug memmanager
98 * Revision 1.40 2002/06/01 07:12:33 relnev
99 * a few NDEBUG updates.
101 * removed a few warnings.
103 * Revision 1.39 2002/06/01 05:33:15 relnev
104 * copied more code over.
106 * added scissor clipping.
108 * Revision 1.38 2002/06/01 03:35:27 relnev
111 * Revision 1.37 2002/06/01 03:32:00 relnev
112 * fix texture loading mistake.
114 * enable some d3d stuff for opengl also
116 * Revision 1.36 2002/05/31 23:25:03 relnev
119 * Revision 1.34 2002/05/31 22:15:22 relnev
122 * Revision 1.33 2002/05/31 22:04:55 relnev
123 * use d3d rect_internal
125 * Revision 1.32 2002/05/31 06:28:23 relnev
128 * Revision 1.31 2002/05/31 06:04:39 relnev
131 * Revision 1.30 2002/05/31 03:56:11 theoddone33
132 * Change tmapper polygon winding and enable culling
134 * Revision 1.29 2002/05/31 03:34:02 theoddone33
138 * Revision 1.28 2002/05/31 00:06:59 relnev
141 * Revision 1.27 2002/05/30 23:46:29 theoddone33
142 * some minor key changes (not necessarily fixes)
144 * Revision 1.26 2002/05/30 23:33:12 relnev
145 * implemented a few more functions.
147 * Revision 1.25 2002/05/30 23:01:16 relnev
148 * implement gr_opengl_set_state.
150 * Revision 1.24 2002/05/30 22:12:57 relnev
151 * finish default texture case
153 * Revision 1.23 2002/05/30 22:02:30 theoddone33
156 * Revision 1.22 2002/05/30 21:44:48 relnev
157 * implemented some missing texture stuff.
159 * enable bitmap polys for opengl.
161 * work around greenness in bitmaps.
163 * Revision 1.21 2002/05/30 17:29:30 theoddone33
164 * Fix some more stubs, change at least one polygon winding since culling is now
167 * Revision 1.20 2002/05/30 16:50:24 theoddone33
168 * Keyboard partially fixed
170 * Revision 1.19 2002/05/30 08:13:14 relnev
173 * Revision 1.18 2002/05/29 23:37:36 relnev
176 * Revision 1.17 2002/05/29 23:17:49 theoddone33
177 * Non working text code and fixed keys
179 * Revision 1.16 2002/05/29 19:45:13 theoddone33
180 * More changes on texture loading
182 * Revision 1.15 2002/05/29 19:06:48 theoddone33
183 * Enable string printing. Enable texture mapping
185 * Revision 1.14 2002/05/29 08:54:40 relnev
186 * "fixed" bitmap drawing.
188 * copied more d3d code over.
190 * Revision 1.13 2002/05/29 06:25:13 theoddone33
191 * Keyboard input, mouse tracking now work
193 * Revision 1.12 2002/05/29 04:52:45 relnev
196 * Revision 1.11 2002/05/29 04:29:56 relnev
197 * removed some unncessary stubbing, implemented opengl rect
199 * Revision 1.10 2002/05/29 04:13:27 theoddone33
202 * Revision 1.9 2002/05/29 03:35:51 relnev
205 * Revision 1.8 2002/05/29 03:30:05 relnev
206 * update opengl stubs
208 * Revision 1.7 2002/05/29 02:52:32 theoddone33
209 * Enable OpenGL renderer
211 * Revision 1.6 2002/05/28 04:56:51 theoddone33
212 * runs a little bit now
214 * Revision 1.5 2002/05/28 04:07:28 theoddone33
215 * New graphics stubbing arrangement
217 * Revision 1.4 2002/05/27 23:39:34 relnev
220 * Revision 1.3 2002/05/27 22:35:01 theoddone33
223 * Revision 1.2 2002/05/27 22:32:02 theoddone33
224 * throw all d3d stuff at opengl
226 * Revision 1.1.1.1 2002/05/03 03:28:09 root
230 * 10 7/14/99 9:42a Dave
231 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
234 * 9 7/09/99 9:51a Dave
235 * Added thick polyline code.
237 * 8 6/29/99 10:35a Dave
238 * Interface polygon bitmaps! Whee!
240 * 7 2/03/99 11:44a Dave
241 * Fixed d3d transparent textures.
243 * 6 1/24/99 11:37p Dave
244 * First full rev of beam weapons. Very customizable. Removed some bogus
245 * Int3()'s in low level net code.
247 * 5 12/18/98 1:13a Dave
248 * Rough 1024x768 support for Direct3D. Proper detection and usage through
251 * 4 12/06/98 2:36p Dave
252 * Drastically improved nebula fogging.
254 * 3 11/11/98 5:37p Dave
255 * Checkin for multiplayer testing.
257 * 2 10/07/98 10:53a Dave
260 * 1 10/07/98 10:49a Dave
262 * 14 5/20/98 9:46p John
263 * added code so the places in code that change half the palette don't
264 * have to clear the screen.
266 * 13 5/06/98 5:30p John
267 * Removed unused cfilearchiver. Removed/replaced some unused/little used
268 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
269 * DirectX header files and libs that fixed the Direct3D alpha blending
272 * 12 4/14/98 12:15p John
273 * Made 16-bpp movies work.
275 * 11 3/12/98 5:36p John
276 * Took out any unused shaders. Made shader code take rgbc instead of
277 * matrix and vector since noone used it like a matrix and it would have
278 * been impossible to do in hardware. Made Glide implement a basic
279 * shader for online help.
281 * 10 3/10/98 4:18p John
282 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
283 * & Glide have popups and print screen. Took out all >8bpp software
284 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
285 * support Fred. Made zbuffering key off of functions rather than one
288 * 9 12/02/97 4:00p John
289 * Added first rev of thruster glow, along with variable levels of
290 * translucency, which retquired some restructing of palman.
292 * 8 10/03/97 9:10a John
293 * added better antialiased line drawer
295 * 7 9/23/97 10:45a John
296 * made so you can tell bitblt code to rle a bitmap by passing flag to
299 * 6 9/09/97 11:01a Sandeep
300 * fixed warning level 4 bugs
302 * 5 7/10/97 2:06p John
303 * added code to specify alphablending type for bitmaps.
305 * 4 6/17/97 7:04p John
306 * added d3d support for gradients.
307 * fixed some color bugs by adding screen signatures instead of watching
308 * flags and palette changes.
310 * 3 6/12/97 2:50a Lawrance
311 * bm_unlock() now passed bitmap number, not pointer
313 * 2 6/11/97 1:12p John
314 * Started fixing all the text colors in the game.
316 * 1 5/12/97 12:14p John
323 #include <windowsx.h>
337 #include "floating.h"
339 #include "systemvars.h"
340 #include "grinternal.h"
341 #include "gropengl.h"
345 #include "osregistry.h"
348 static int Inited = 0;
350 typedef enum gr_texture_source {
352 TEXTURE_SOURCE_DECAL,
353 TEXTURE_SOURCE_NO_FILTERING,
356 typedef enum gr_alpha_blend {
357 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
358 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
359 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
360 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
361 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
364 typedef enum gr_zbuffer_type {
371 #define NEBULA_COLORS 20
373 volatile int GL_activate = 0;
374 volatile int GL_deactivate = 0;
376 static int GL_use_luminance_alpha;
378 static char *Gr_saved_screen = NULL;
379 static int Gr_saved_screen_bitmap;
382 // Throw in some dummy functions - DDOI
384 int D3D_32bit = 0; // grd3d.cpp
385 int D3D_fog_mode = -1; // grd3d.cpp
386 int D3D_inited = 0; // grd3d.cpp
387 int D3D_zbias = 1; // grd3d.cpp
388 int D3d_rendition_uvs = 0; // grd3d.cpp
395 void d3d_zbias (int a)
401 static void gr_opengl_set_texture_state(gr_texture_source ts);
403 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
404 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
406 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
408 gr_opengl_set_texture_state(ts);
410 if (ab != GL_current_alpha_blend) {
412 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
413 glBlendFunc(GL_ONE, GL_ZERO);
415 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
416 glBlendFunc(GL_ONE, GL_ONE);
418 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
419 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
421 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
422 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
424 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
425 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
431 GL_current_alpha_blend = ab;
434 if (zt != GL_current_zbuffer_type) {
436 case ZBUFFER_TYPE_NONE:
437 glDepthFunc(GL_ALWAYS);
438 glDepthMask(GL_FALSE);
440 case ZBUFFER_TYPE_READ:
441 glDepthFunc(GL_LESS);
442 glDepthMask(GL_FALSE);
444 case ZBUFFER_TYPE_WRITE:
445 glDepthFunc(GL_ALWAYS);
446 glDepthMask(GL_TRUE);
448 case ZBUFFER_TYPE_FULL:
449 glDepthFunc(GL_LESS);
450 glDepthMask(GL_TRUE);
456 GL_current_zbuffer_type = zt;
460 void gr_opengl_activate(int active)
465 // don't grab key/mouse if cmdline says so or if we're fullscreen
466 if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
467 SDL_WM_GrabInput(SDL_GRAB_ON);
472 // let go of mouse/keyboard
473 SDL_WM_GrabInput(SDL_GRAB_OFF);
478 static void opengl_tcache_flush ();
480 void gr_opengl_preload_init()
482 if (gr_screen.mode != GR_OPENGL) {
486 opengl_tcache_flush ();
489 int GL_should_preload = 0;
490 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
492 if ( gr_screen.mode != GR_OPENGL) {
496 if ( !GL_should_preload ) {
500 float u_scale, v_scale;
504 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
506 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
510 mprintf(("Texture upload failed!\n" ));
516 void gr_opengl_pixel(int x, int y)
521 void gr_opengl_clear()
523 glClearColor(gr_screen.current_clear_color.red / 255.0,
524 gr_screen.current_clear_color.green / 255.0,
525 gr_screen.current_clear_color.blue / 255.0, 1.0);
527 glClear ( GL_COLOR_BUFFER_BIT );
530 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
531 static void opengl_tcache_frame ();
533 void gr_opengl_flip()
541 extern int Gr_opengl_mouse_saved;
542 Gr_opengl_mouse_saved = 0;
544 if ( mouse_is_visible() ) {
548 mouse_get_pos( &mx, &my );
550 gr_opengl_save_mouse_area(mx,my,32,32);
552 if ( Gr_cursor == -1 ) {
555 gr_set_bitmap(Gr_cursor);
561 GLenum error = glGetError();
564 error = glGetError();
566 if (error != GL_NO_ERROR) {
567 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
570 } while (error != GL_NO_ERROR);
573 SDL_GL_SwapBuffers ();
575 opengl_tcache_frame ();
577 int cnt = GL_activate;
580 opengl_tcache_flush();
581 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
587 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
591 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
596 void gr_opengl_set_clip(int x,int y,int w,int h)
598 // check for sanity of parameters
604 if (x >= gr_screen.max_w)
605 x = gr_screen.max_w - 1;
606 if (y >= gr_screen.max_h)
607 y = gr_screen.max_h - 1;
609 if (x + w > gr_screen.max_w)
610 w = gr_screen.max_w - x;
611 if (y + h > gr_screen.max_h)
612 h = gr_screen.max_h - y;
614 if (w > gr_screen.max_w)
616 if (h > gr_screen.max_h)
619 gr_screen.offset_x = x;
620 gr_screen.offset_y = y;
621 gr_screen.clip_left = 0;
622 gr_screen.clip_right = w-1;
623 gr_screen.clip_top = 0;
624 gr_screen.clip_bottom = h-1;
625 gr_screen.clip_width = w;
626 gr_screen.clip_height = h;
628 glEnable(GL_SCISSOR_TEST);
629 glScissor(x, gr_screen.max_h-y-h, w, h);
632 void gr_opengl_reset_clip()
634 gr_screen.offset_x = 0;
635 gr_screen.offset_y = 0;
636 gr_screen.clip_left = 0;
637 gr_screen.clip_top = 0;
638 gr_screen.clip_right = gr_screen.max_w - 1;
639 gr_screen.clip_bottom = gr_screen.max_h - 1;
640 gr_screen.clip_width = gr_screen.max_w;
641 gr_screen.clip_height = gr_screen.max_h;
643 glDisable(GL_SCISSOR_TEST);
644 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
647 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
649 gr_screen.current_alpha = alpha;
650 gr_screen.current_alphablend_mode = alphablend_mode;
651 gr_screen.current_bitblt_mode = bitblt_mode;
652 gr_screen.current_bitmap = bitmap_num;
654 gr_screen.current_bitmap_sx = sx;
655 gr_screen.current_bitmap_sy = sy;
658 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
660 shade->screen_sig = gr_screen.signature;
667 void gr_opengl_set_shader( shader * shade )
670 if (shade->screen_sig != gr_screen.signature) {
671 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
673 gr_screen.current_shader = *shade;
675 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
680 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
682 STUB_FUNCTION; /* who called me? */
685 void gr_opengl_bitmap(int x, int y)
687 STUB_FUNCTION; /* who called me? */
690 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
694 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
696 saved_zbuf = gr_zbuffer_get();
698 // start the frame, no zbuffering, no culling
700 gr_zbuffer_set(GR_ZBUFF_NONE);
709 v[0].flags = PF_PROJECTED;
716 v[1].sx = i2fl(x + w);
721 v[1].flags = PF_PROJECTED;
728 v[2].sx = i2fl(x + w);
729 v[2].sy = i2fl(y + h);
733 v[2].flags = PF_PROJECTED;
741 v[3].sy = i2fl(y + h);
745 v[3].flags = PF_PROJECTED;
753 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
757 // restore zbuffer and culling
758 gr_zbuffer_set(saved_zbuf);
762 void gr_opengl_rect(int x,int y,int w,int h)
764 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);
767 void gr_opengl_shade(int x,int y,int w,int h)
774 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
775 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
776 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
777 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
778 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
779 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
780 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
781 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
783 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
786 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
791 if ( !gr_screen.current_color.is_alphacolor ) return;
793 float u_scale, v_scale;
795 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
796 // Couldn't set texture
797 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
801 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
803 float u0, u1, v0, v1;
804 float x1, x2, y1, y2;
807 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
809 u0 = u_scale*i2fl(sx)/i2fl(bw);
810 v0 = v_scale*i2fl(sy)/i2fl(bh);
812 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
813 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
815 x1 = i2fl(x+gr_screen.offset_x);
816 y1 = i2fl(y+gr_screen.offset_y);
817 x2 = i2fl(x+w+gr_screen.offset_x);
818 y2 = i2fl(y+h+gr_screen.offset_y);
820 if ( gr_screen.current_color.is_alphacolor ) {
821 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
823 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
827 glTexCoord2f (u0, v1);
828 glVertex3f (x1, y2, -0.99);
830 glTexCoord2f (u1, v1);
831 glVertex3f (x2, y2, -0.99);
833 glTexCoord2f (u1, v0);
834 glVertex3f (x2, y1, -0.99);
836 glTexCoord2f (u0, v0);
837 glVertex3f (x1, y1, -0.99);
841 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
848 int dx1=x, dx2=x+w-1;
849 int dy1=y, dy2=y+h-1;
852 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
857 if ( count > 1 ) Int3();
861 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
862 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
863 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
864 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
865 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
866 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
893 if ( w < 1 ) return; // clipped away!
894 if ( h < 1 ) return; // clipped away!
898 // Make sure clipping algorithm works
902 Assert( w == (dx2-dx1+1) );
903 Assert( h == (dy2-dy1+1) );
906 Assert( sx+w <= bw );
907 Assert( sy+h <= bh );
908 Assert( dx2 >= dx1 );
909 Assert( dy2 >= dy1 );
910 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
911 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
912 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
913 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
916 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
917 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
920 void gr_opengl_aabitmap(int x, int y)
924 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
925 int dx1=x, dx2=x+w-1;
926 int dy1=y, dy2=y+h-1;
929 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
930 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
931 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
932 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
933 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
934 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
936 if ( sx < 0 ) return;
937 if ( sy < 0 ) return;
938 if ( sx >= w ) return;
939 if ( sy >= h ) return;
941 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
942 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
945 void gr_opengl_string( int sx, int sy, char *s )
947 int width, spacing, letter;
950 if ( !Current_font ) {
954 gr_set_bitmap(Current_font->bitmap_id);
959 if (sx==0x8000) { //centered
960 x = get_centered_x(s);
972 y += Current_font->h;
973 if (sx==0x8000) { //centered
974 x = get_centered_x(s);
981 letter = get_char_width(s[0],s[1],&width,&spacing);
984 //not in font, draw as space
992 // Check if this character is totally clipped
993 if ( x + width < gr_screen.clip_left ) continue;
994 if ( y + Current_font->h < gr_screen.clip_top ) continue;
995 if ( x > gr_screen.clip_right ) continue;
996 if ( y > gr_screen.clip_bottom ) continue;
999 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1000 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1004 wc = width - xd; hc = Current_font->h - yd;
1005 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1006 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1008 if ( wc < 1 ) continue;
1009 if ( hc < 1 ) continue;
1011 int u = Current_font->bm_u[letter];
1012 int v = Current_font->bm_v[letter];
1014 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1018 void gr_opengl_line(int x1,int y1,int x2,int y2)
1020 int clipped = 0, swapped=0;
1022 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1024 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);
1029 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1030 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1031 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1032 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1034 if ( x1 == x2 && y1 == y2 ) {
1035 glBegin (GL_POINTS);
1036 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1037 glVertex3f (sx1, sy1, -0.99f);
1049 } else if ( y1 == y2 ) {
1058 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1059 glVertex3f (sx2, sy2, -0.99f);
1060 glVertex3f (sx1, sy1, -0.99f);
1064 void gr_opengl_aaline(vertex *v1, vertex *v2)
1066 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1069 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1071 int clipped = 0, swapped=0;
1073 if ( !gr_screen.current_color.is_alphacolor ) {
1074 gr_line( x1, y1, x2, y2 );
1078 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);
1080 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1082 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1083 int ba = swapped ? gr_screen.current_color.alpha : 0;
1088 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1089 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1090 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1091 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1099 } else if ( y1 == y2 ) {
1108 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1109 glVertex3f (sx2, sy2, -0.99f);
1110 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1111 glVertex3f (sx1, sy1, -0.99f);
1115 void gr_opengl_circle( int xc, int yc, int d )
1125 if ( (xc+r) < gr_screen.clip_left ) return;
1126 if ( (xc-r) > gr_screen.clip_right ) return;
1127 if ( (yc+r) < gr_screen.clip_top ) return;
1128 if ( (yc-r) > gr_screen.clip_bottom ) return;
1131 // Draw the first octant
1132 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1133 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1138 // Draw the second octant
1139 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1140 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1148 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1149 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1154 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1158 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1159 if (f_float < 0.0f) {
1167 *a = (int)(f_float * 255.0);
1170 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1173 float u_scale = 1.0f, v_scale = 1.0f;
1175 // Make nebula use the texture mapper... this blends the colors better.
1176 if ( flags & TMAP_FLAG_NEBULA ){
1180 gr_texture_source texture_source = (gr_texture_source)-1;
1181 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1182 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1184 if ( gr_zbuffering ) {
1185 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1186 zbuffer_type = ZBUFFER_TYPE_READ;
1188 zbuffer_type = ZBUFFER_TYPE_FULL;
1191 zbuffer_type = ZBUFFER_TYPE_NONE;
1196 int tmap_type = TCACHE_TYPE_NORMAL;
1200 if ( flags & TMAP_FLAG_TEXTURED ) {
1203 r = gr_screen.current_color.red;
1204 g = gr_screen.current_color.green;
1205 b = gr_screen.current_color.blue;
1208 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1211 tmap_type = TCACHE_TYPE_NORMAL;
1212 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1214 // Blend with screen pixel using src*alpha+dst
1215 float factor = gr_screen.current_alpha;
1219 if ( factor <= 1.0f ) {
1220 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1221 r = (r*tmp_alpha)/255;
1222 g = (g*tmp_alpha)/255;
1223 b = (b*tmp_alpha)/255;
1226 tmap_type = TCACHE_TYPE_XPARENT;
1228 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1230 // Blend with screen pixel using src*alpha+dst
1231 float factor = gr_screen.current_alpha;
1233 if ( factor > 1.0f ) {
1236 alpha = fl2i(gr_screen.current_alpha*255.0f);
1240 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1241 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1243 alpha_blend = ALPHA_BLEND_NONE;
1248 if(flags & TMAP_FLAG_BITMAP_SECTION){
1249 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1252 texture_source = TEXTURE_SOURCE_NONE;
1254 if ( flags & TMAP_FLAG_TEXTURED ) {
1255 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 ))
1257 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1261 // use nonfiltered textures for bitmap sections
1262 if(flags & TMAP_FLAG_BITMAP_SECTION){
1263 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1265 texture_source = TEXTURE_SOURCE_DECAL;
1270 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1272 if ( flags & TMAP_FLAG_TEXTURED )
1278 if (flags & TMAP_FLAG_PIXEL_FOG) {
1284 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1286 vertex * va = verts[i];
1290 x = fl2i(va->sx*16.0f);
1291 y = fl2i(va->sy*16.0f);
1293 x += gr_screen.offset_x*16;
1294 y += gr_screen.offset_y*16;
1296 sx = i2fl(x) / 16.0f;
1297 sy = i2fl(y) / 16.0f;
1299 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1310 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1313 glBegin(GL_TRIANGLE_FAN);
1314 for (i = nv-1; i >= 0; i--) {
1315 vertex * va = verts[i];
1321 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1322 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1331 if ( flags & TMAP_FLAG_CORRECT ) {
1337 if (flags & TMAP_FLAG_ALPHA) {
1343 if (flags & TMAP_FLAG_NEBULA ) {
1344 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1345 r = gr_palette[pal*3+0];
1346 g = gr_palette[pal*3+1];
1347 b = gr_palette[pal*3+2];
1348 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1349 r = Gr_gamma_lookup[verts[i]->b];
1350 g = Gr_gamma_lookup[verts[i]->b];
1351 b = Gr_gamma_lookup[verts[i]->b];
1352 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1353 // Make 0.75 be 256.0f
1354 r = Gr_gamma_lookup[verts[i]->r];
1355 g = Gr_gamma_lookup[verts[i]->g];
1356 b = Gr_gamma_lookup[verts[i]->b];
1358 // use constant RGB values...
1360 glColor4ub (r,g,b,a);
1362 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1365 /* this is for GL_EXT_SECONDARY_COLOR */
1366 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1367 /* do separate color call here */
1373 x = fl2i(va->sx*16.0f);
1374 y = fl2i(va->sy*16.0f);
1376 x += gr_screen.offset_x*16;
1377 y += gr_screen.offset_y*16;
1379 sx = i2fl(x) / 16.0f;
1380 sy = i2fl(y) / 16.0f;
1382 if ( flags & TMAP_FLAG_TEXTURED ) {
1385 glTexCoord2f(tu, tv);
1388 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1393 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1395 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1398 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1400 void gr_opengl_scaler(vertex *va, vertex *vb )
1402 float x0, y0, x1, y1;
1403 float u0, v0, u1, v1;
1404 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1405 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1406 float xmin, xmax, ymin, ymax;
1407 int dx0, dy0, dx1, dy1;
1409 //============= CLIP IT =====================
1411 x0 = va->sx; y0 = va->sy;
1412 x1 = vb->sx; y1 = vb->sy;
1414 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1415 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1417 u0 = va->u; v0 = va->v;
1418 u1 = vb->u; v1 = vb->v;
1420 // Check for obviously offscreen bitmaps...
1421 if ( (y1<=y0) || (x1<=x0) ) return;
1422 if ( (x1<xmin ) || (x0>xmax) ) return;
1423 if ( (y1<ymin ) || (y0>ymax) ) return;
1425 clipped_u0 = u0; clipped_v0 = v0;
1426 clipped_u1 = u1; clipped_v1 = v1;
1428 clipped_x0 = x0; clipped_y0 = y0;
1429 clipped_x1 = x1; clipped_y1 = y1;
1431 // Clip the left, moving u0 right as necessary
1433 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1437 // Clip the right, moving u1 left as necessary
1439 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1443 // Clip the top, moving v0 down as necessary
1445 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1449 // Clip the bottom, moving v1 up as necessary
1451 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1455 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1456 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1458 if (dx1<=dx0) return;
1459 if (dy1<=dy0) return;
1461 //============= DRAW IT =====================
1475 v[1].sx = clipped_x1;
1476 v[1].sy = clipped_y0;
1479 v[1].u = clipped_u1;
1480 v[1].v = clipped_v0;
1483 v[2].sx = clipped_x1;
1484 v[2].sy = clipped_y1;
1487 v[2].u = clipped_u1;
1488 v[2].v = clipped_v1;
1491 v[3].sx = clipped_x0;
1492 v[3].sy = clipped_y1;
1495 v[3].u = clipped_u0;
1496 v[3].v = clipped_v1;
1498 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1501 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1505 void gr_opengl_get_color( int * r, int * g, int * b )
1507 if (r) *r = gr_screen.current_color.red;
1508 if (g) *g = gr_screen.current_color.green;
1509 if (b) *b = gr_screen.current_color.blue;
1512 void gr_opengl_init_color(color *c, int r, int g, int b)
1514 c->screen_sig = gr_screen.signature;
1515 c->red = (unsigned char)r;
1516 c->green = (unsigned char)g;
1517 c->blue = (unsigned char)b;
1519 c->ac_type = AC_TYPE_NONE;
1521 c->is_alphacolor = 0;
1525 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1527 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1528 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1529 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1530 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1532 gr_opengl_init_color( clr, r, g, b );
1534 clr->alpha = (unsigned char)alpha;
1535 clr->ac_type = (ubyte)type;
1536 clr->alphacolor = -1;
1537 clr->is_alphacolor = 1;
1540 void gr_opengl_set_color( int r, int g, int b )
1542 Assert((r >= 0) && (r < 256));
1543 Assert((g >= 0) && (g < 256));
1544 Assert((b >= 0) && (b < 256));
1546 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1549 void gr_opengl_set_color_fast(color *dst)
1551 if ( dst->screen_sig != gr_screen.signature ) {
1552 if ( dst->is_alphacolor ) {
1553 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1555 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1558 gr_screen.current_color = *dst;
1561 void gr_opengl_print_screen(char *filename)
1566 int gr_opengl_supports_res_ingame(int res)
1573 int gr_opengl_supports_res_interface(int res)
1580 static void opengl_tcache_cleanup ();
1582 void gr_opengl_cleanup()
1584 if ( !Inited ) return;
1591 opengl_tcache_cleanup ();
1596 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1598 Assert((r >= 0) && (r < 256));
1599 Assert((g >= 0) && (g < 256));
1600 Assert((b >= 0) && (b < 256));
1602 if (fog_mode == GR_FOGMODE_NONE) {
1603 if (gr_screen.current_fog_mode != fog_mode) {
1606 gr_screen.current_fog_mode = fog_mode;
1611 if (gr_screen.current_fog_mode != fog_mode) {
1614 if (D3D_fog_mode == 2) {
1615 glFogi(GL_FOG_MODE, GL_LINEAR);
1618 gr_screen.current_fog_mode = fog_mode;
1621 if ( (gr_screen.current_fog_color.red != r) ||
1622 (gr_screen.current_fog_color.green != g) ||
1623 (gr_screen.current_fog_color.blue != b) ) {
1626 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1628 fc[0] = (float)r/255.0;
1629 fc[1] = (float)g/255.0;
1630 fc[2] = (float)b/255.0;
1633 glFogfv(GL_FOG_COLOR, fc);
1636 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1637 ((fog_near != gr_screen.fog_near) ||
1638 (fog_far != gr_screen.fog_far)) ) {
1639 gr_screen.fog_near = fog_near;
1640 gr_screen.fog_far = fog_far;
1642 if (D3D_fog_mode == 2) {
1643 glFogf(GL_FOG_START, fog_near);
1644 glFogf(GL_FOG_END, fog_far);
1649 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1654 void gr_opengl_set_cull(int cull)
1657 glEnable (GL_CULL_FACE);
1658 glFrontFace (GL_CCW);
1660 glDisable (GL_CULL_FACE);
1664 void gr_opengl_filter_set(int filter)
1669 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1672 gr_set_bitmap(bmap1);
1675 gr_set_bitmap(bmap2);
1681 typedef struct tcache_slot_opengl {
1682 GLuint texture_handle;
1683 float u_scale, v_scale;
1686 int used_this_frame;
1691 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1692 tcache_slot_opengl *parent;
1694 gr_texture_source texture_mode;
1695 } tcache_slot_opengl;
1697 static void *Texture_sections = NULL;
1698 static tcache_slot_opengl *Textures = NULL;
1700 static tcache_slot_opengl *GL_bound_texture;
1702 int GL_texture_sections = 0;
1703 int GL_texture_ram = 0;
1704 int GL_frame_count = 0;
1705 int GL_min_texture_width = 0;
1706 int GL_max_texture_width = 0;
1707 int GL_min_texture_height = 0;
1708 int GL_max_texture_height = 0;
1709 int GL_square_textures = 0;
1710 int GL_textures_in = 0;
1711 int GL_textures_in_frame = 0;
1712 int GL_last_bitmap_id = -1;
1713 int GL_last_detail = -1;
1714 int GL_last_bitmap_type = -1;
1715 int GL_last_section_x = -1;
1716 int GL_last_section_y = -1;
1720 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1722 static void gr_opengl_set_texture_state(gr_texture_source ts)
1724 if (ts == TEXTURE_SOURCE_NONE) {
1725 GL_bound_texture = NULL;
1727 glBindTexture(GL_TEXTURE_2D, 0);
1728 gr_tcache_set(-1, -1, NULL, NULL );
1729 } else if (GL_bound_texture &&
1730 GL_bound_texture->texture_mode != ts) {
1732 case TEXTURE_SOURCE_DECAL:
1733 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1734 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1736 case TEXTURE_SOURCE_NO_FILTERING:
1737 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1738 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1744 GL_bound_texture->texture_mode = ts;
1747 GL_current_texture_source = ts;
1750 static int opengl_max_tex_size_get()
1752 GLint max_texture_size = 0;
1754 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1755 return max_texture_size;
1758 static void opengl_tcache_init (int use_sections)
1762 // DDOI - FIXME skipped a lot of stuff here
1763 GL_should_preload = 0;
1765 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1768 if ( tmp_pl == 0 ) {
1769 GL_should_preload = 0;
1770 } else if ( tmp_pl == 1 ) {
1771 GL_should_preload = 1;
1773 GL_should_preload = 1;
1778 GL_min_texture_width = 16;
1779 GL_min_texture_height = 16;
1781 GL_max_texture_width = opengl_max_tex_size_get();
1782 GL_max_texture_height = opengl_max_tex_size_get();
1784 /* TODO: why don't textures work correctly when this is disabled? */
1785 GL_square_textures = 1;
1787 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1793 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1794 if(!Texture_sections){
1797 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1800 // Init the texture structures
1801 int section_count = 0;
1802 for( i=0; i<MAX_BITMAPS; i++ ) {
1804 Textures[i].vram_texture = NULL;
1805 Textures[i].vram_texture_surface = NULL;
1807 Textures[i].texture_handle = 0;
1809 Textures[i].bitmap_id = -1;
1810 Textures[i].size = 0;
1811 Textures[i].used_this_frame = 0;
1813 Textures[i].parent = NULL;
1815 // allocate sections
1817 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1818 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1819 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1820 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1822 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1823 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1825 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1826 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1827 Textures[i].data_sections[idx][s_idx]->size = 0;
1828 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1832 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1833 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1834 Textures[i].data_sections[idx][s_idx] = NULL;
1840 GL_texture_sections = use_sections;
1842 //GL_last_detail = Detail.hardware_textures;
1843 GL_last_bitmap_id = -1;
1844 GL_last_bitmap_type = -1;
1846 GL_last_section_x = -1;
1847 GL_last_section_y = -1;
1850 GL_textures_in_frame = 0;
1853 static int opengl_free_texture (tcache_slot_opengl *t);
1855 void opengl_free_texture_with_handle(int handle)
1857 for(int i=0; i<MAX_BITMAPS; i++ ) {
1858 if (Textures[i].bitmap_id == handle) {
1859 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1860 opengl_free_texture ( &Textures[i] );
1865 static void opengl_tcache_flush ()
1869 for( i=0; i<MAX_BITMAPS; i++ ) {
1870 opengl_free_texture ( &Textures[i] );
1872 if (GL_textures_in != 0) {
1873 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1877 GL_last_bitmap_id = -1;
1878 GL_last_section_x = -1;
1879 GL_last_section_y = -1;
1882 static void opengl_tcache_cleanup ()
1884 opengl_tcache_flush ();
1887 GL_textures_in_frame = 0;
1894 if( Texture_sections != NULL ){
1895 free(Texture_sections);
1896 Texture_sections = NULL;
1900 static void opengl_tcache_frame ()
1904 GL_last_bitmap_id = -1;
1905 GL_textures_in_frame = 0;
1911 for( i=0; i<MAX_BITMAPS; i++ ) {
1912 Textures[i].used_this_frame = 0;
1915 if(Textures[i].data_sections[0][0] != NULL){
1916 Assert(GL_texture_sections);
1917 if(GL_texture_sections){
1918 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1919 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1920 if(Textures[i].data_sections[idx][s_idx] != NULL){
1921 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1931 opengl_tcache_flush();
1936 static int opengl_free_texture ( tcache_slot_opengl *t )
1942 if ( t->bitmap_id > -1 ) {
1943 // if I, or any of my children have been used this frame, bail
1944 if(t->used_this_frame == GL_frame_count){
1947 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1948 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1949 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
1955 // ok, now we know its legal to free everything safely
1956 t->texture_mode = (gr_texture_source) -1;
1957 glDeleteTextures (1, &t->texture_handle);
1958 t->texture_handle = 0;
1960 if ( GL_last_bitmap_id == t->bitmap_id ) {
1961 GL_last_bitmap_id = -1;
1964 // if this guy has children, free them too, since the children
1965 // actually make up his size
1966 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1967 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1968 if(t->data_sections[idx][s_idx] != NULL){
1969 opengl_free_texture(t->data_sections[idx][s_idx]);
1975 t->used_this_frame = 0;
1976 GL_textures_in -= t->size;
1982 static void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1988 if((w_out == NULL) || (h_out == NULL)){
1996 for (i=0; i<16; i++ ) {
1997 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2002 for (j=0; j<16; j++ ) {
2003 if ( (tex_h > (1<<j)) && (tex_h <= (1<<(j+1))) ) {
2008 /* make i the smaller one */
2016 /* try to keep a 8:1 ratio */
2018 /* increase i, or decrease j? */
2026 if ( tex_w < GL_min_texture_width ) {
2027 tex_w = GL_min_texture_width;
2028 } else if ( tex_w > GL_max_texture_width ) {
2029 tex_w = GL_max_texture_width;
2032 if ( tex_h < GL_min_texture_height ) {
2033 tex_h = GL_min_texture_height;
2034 } else if ( tex_h > GL_max_texture_height ) {
2035 tex_h = GL_max_texture_height;
2038 if ( GL_square_textures) {
2040 // Make the both be equal to larger of the two
2041 new_size = max(tex_w, tex_h);
2046 // store the outgoing size
2051 // data == start of bitmap data
2052 // sx == x offset into bitmap
2053 // sy == y offset into bitmap
2054 // src_w == absolute width of section on source bitmap
2055 // src_h == absolute height of section on source bitmap
2056 // bmap_w == width of source bitmap
2057 // bmap_h == height of source bitmap
2058 // tex_w == width of final texture
2059 // tex_h == height of final texture
2060 static 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)
2070 if ( t->used_this_frame == GL_frame_count ) {
2071 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2076 if(!opengl_free_texture(t)){
2081 // get final texture size
2082 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2084 if ( (tex_w < 1) || (tex_h < 1) ) {
2085 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2089 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2090 t->u_scale = (float)bmap_w / (float)tex_w;
2091 t->v_scale = (float)bmap_h / (float)tex_h;
2092 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2093 t->u_scale = (float)src_w / (float)tex_w;
2094 t->v_scale = (float)src_h / (float)tex_h;
2101 glGenTextures (1, &t->texture_handle);
2104 if (t->texture_handle == 0) {
2105 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2109 GL_bound_texture = t;
2111 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2113 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2115 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2116 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2117 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2119 /* this should be set next anyway */
2120 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2121 // gr_opengl_set_texture_state(GL_current_texture_source);
2126 switch (bitmap_type) {
2128 case TCACHE_TYPE_AABITMAP:
2131 ubyte *bmp_data = ((ubyte*)data);
2137 for (i=0; i<16; i++) {
2138 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2141 for ( ; i<256; i++ ) {
2145 texmem = (ubyte *) malloc (tex_w*tex_h);
2148 for (i=0;i<tex_h;i++)
2150 for (j=0;j<tex_w;j++)
2152 if (i < bmap_h && j < bmap_w) {
2153 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2162 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2168 case TCACHE_TYPE_BITMAP_SECTION:
2171 ubyte *bmp_data = ((ubyte*)data);
2172 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2173 ubyte *texmemp = texmem;
2175 for (i=0;i<tex_h;i++)
2177 for (j=0;j<tex_w;j++)
2179 if (i < src_h && j < src_w) {
2180 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2181 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2189 size = tex_w*tex_h*2;
2191 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2192 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2200 ubyte *bmp_data = ((ubyte*)data);
2201 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2202 ubyte *texmemp = texmem;
2204 fix u, utmp, v, du, dv;
2208 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2209 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2211 for (j=0;j<tex_h;j++)
2214 for (i=0;i<tex_w;i++)
2216 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2217 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2223 size = tex_w*tex_h*2;
2225 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2226 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2234 t->bitmap_id = texture_handle;
2235 t->time_created = GL_frame_count;
2236 t->used_this_frame = 0;
2238 t->w = (ushort)tex_w;
2239 t->h = (ushort)tex_h;
2241 GL_textures_in_frame += t->size;
2243 GL_textures_in += t->size;
2249 static int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2253 int final_w, final_h;
2257 // setup texture/bitmap flags
2259 switch(bitmap_type){
2260 case TCACHE_TYPE_AABITMAP:
2261 flags |= BMP_AABITMAP;
2264 case TCACHE_TYPE_NORMAL:
2265 flags |= BMP_TEX_OTHER;
2266 case TCACHE_TYPE_XPARENT:
2267 flags |= BMP_TEX_XPARENT;
2269 case TCACHE_TYPE_NONDARKENING:
2271 flags |= BMP_TEX_NONDARK;
2275 // lock the bitmap into the proper format
2276 bmp = bm_lock(bitmap_handle, bpp, flags);
2277 if ( bmp == NULL ) {
2278 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2287 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2288 // max_w /= D3D_texture_divider;
2289 // max_h /= D3D_texture_divider;
2291 // Detail.debris_culling goes from 0 to 4.
2292 max_w /= (16 >> Detail.hardware_textures);
2293 max_h /= (16 >> Detail.hardware_textures);
2297 // get final texture size as it will be allocated as a DD surface
2298 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2300 // if this tcache slot has no bitmap
2301 if ( tslot->bitmap_id < 0) {
2304 // different bitmap altogether - determine if the new one can use the old one's slot
2305 else if (tslot->bitmap_id != bitmap_handle) {
2306 if((final_w == tslot->w) && (final_h == tslot->h)){
2308 //ml_printf("Reloading texture %d\n", bitmap_handle);
2315 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);
2317 // unlock the bitmap
2318 bm_unlock(bitmap_handle);
2323 static int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2327 int final_w, final_h;
2328 int section_x, section_y;
2331 // setup texture/bitmap flags
2332 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2333 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2334 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2336 flags = BMP_TEX_XPARENT;
2338 // lock the bitmap in the proper format
2339 bmp = bm_lock(bitmap_handle, 16, flags);
2340 if ( bmp == NULL ) {
2341 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2344 // determine the width and height of this section
2345 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2347 // get final texture size as it will be allocated as an opengl texture
2348 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2350 // if this tcache slot has no bitmap
2351 if ( tslot->bitmap_id < 0) {
2354 // different bitmap altogether - determine if the new one can use the old one's slot
2355 else if (tslot->bitmap_id != bitmap_handle) {
2356 if((final_w == tslot->w) && (final_h == tslot->h)){
2364 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);
2366 // unlock the bitmap
2367 bm_unlock(bitmap_handle);
2373 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2374 static 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)
2383 GL_last_bitmap_id = -1;
2387 if ( GL_last_detail != Detail.hardware_textures ) {
2388 GL_last_detail = Detail.hardware_textures;
2389 opengl_tcache_flush();
2396 int n = bm_get_cache_slot (bitmap_id, 1);
2397 tcache_slot_opengl *t = &Textures[n];
2399 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)) {
2400 t->used_this_frame = GL_frame_count;
2402 // mark all children as used
2403 if(GL_texture_sections){
2404 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2405 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2406 if(t->data_sections[idx][s_idx] != NULL){
2407 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2413 *u_scale = t->u_scale;
2414 *v_scale = t->v_scale;
2418 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2419 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2420 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2426 // if the texture sections haven't been created yet
2427 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2429 // lock the bitmap in the proper format
2430 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2431 bm_unlock(bitmap_id);
2433 // now lets do something for each texture
2435 for(idx=0; idx<bmp->sections.num_x; idx++){
2436 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2437 // hmm. i'd rather we didn't have to do it this way...
2438 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2442 // not used this frame
2443 t->data_sections[idx][s_idx]->used_this_frame = 0;
2447 // zero out pretty much everything in the parent struct since he's just the root
2448 t->bitmap_id = bitmap_id;
2449 t->texture_handle = 0;
2450 t->time_created = t->data_sections[sx][sy]->time_created;
2451 t->used_this_frame = 0;
2453 t->vram_texture = NULL;
2454 t->vram_texture_surface = NULL
2458 // argh. we failed to upload. free anything we can
2460 opengl_free_texture(t);
2462 // swap in the texture we want
2464 t = t->data_sections[sx][sy];
2467 // all other "normal" textures
2468 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2469 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2472 // everything went ok
2473 if(ret_val && (t->texture_handle) && !vram_full){
2474 *u_scale = t->u_scale;
2475 *v_scale = t->v_scale;
2477 GL_bound_texture = t;
2479 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2481 /* this should be set next anyway */
2482 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2483 // gr_opengl_set_texture_state(GL_current_texture_source);
2486 GL_last_bitmap_id = t->bitmap_id;
2487 GL_last_bitmap_type = bitmap_type;
2488 GL_last_section_x = sx;
2489 GL_last_section_y = sy;
2491 t->used_this_frame = GL_frame_count;
2495 GL_last_bitmap_id = -1;
2496 GL_last_bitmap_type = -1;
2498 GL_last_section_x = -1;
2499 GL_last_section_y = -1;
2501 GL_bound_texture = NULL;
2503 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2510 void gr_opengl_set_clear_color(int r, int g, int b)
2512 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2515 void gr_opengl_flash(int r, int g, int b)
2521 if ( r || g || b ) {
2522 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2524 float x1, x2, y1, y2;
2525 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2526 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2527 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2528 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2530 glColor4ub(r, g, b, 255);
2532 glVertex3f (x1, y2, -0.99);
2534 glVertex3f (x2, y2, -0.99);
2536 glVertex3f (x2, y1, -0.99);
2538 glVertex3f (x1, y1, -0.99);
2543 int gr_opengl_zbuffer_get()
2545 if ( !gr_global_zbuffering ) {
2546 return GR_ZBUFF_NONE;
2548 return gr_zbuffering_mode;
2551 int gr_opengl_zbuffer_set(int mode)
2553 int tmp = gr_zbuffering_mode;
2555 gr_zbuffering_mode = mode;
2557 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2565 void gr_opengl_zbuffer_clear(int mode)
2569 gr_zbuffering_mode = GR_ZBUFF_FULL;
2570 gr_global_zbuffering = 1;
2572 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2573 glClear ( GL_DEPTH_BUFFER_BIT );
2576 gr_zbuffering_mode = GR_ZBUFF_NONE;
2577 gr_global_zbuffering = 0;
2581 void gr_opengl_set_gamma(float gamma)
2584 Gr_gamma_int = int (Gr_gamma*10);
2586 // Create the Gamma lookup table
2588 for (i=0;i<256; i++) {
2589 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2592 } else if ( v < 0 ) {
2595 Gr_gamma_lookup[i] = v;
2598 // Flush any existing textures
2599 opengl_tcache_flush();
2602 void gr_opengl_fade_in(int instantaneous)
2607 void gr_opengl_fade_out(int instantaneous)
2612 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2615 glReadBuffer(GL_FRONT);
2617 glReadBuffer(GL_BACK);
2620 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2622 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2624 if (gr_screen.bits_per_pixel == 15) {
2625 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2626 } else if (gr_screen.bits_per_pixel == 32) {
2627 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2630 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2633 static int Gr_opengl_mouse_saved = 0;
2634 static int Gr_opengl_mouse_saved_x1 = 0;
2635 static int Gr_opengl_mouse_saved_y1 = 0;
2636 static int Gr_opengl_mouse_saved_x2 = 0;
2637 static int Gr_opengl_mouse_saved_y2 = 0;
2638 static int Gr_opengl_mouse_saved_w = 0;
2639 static int Gr_opengl_mouse_saved_h = 0;
2640 #define MAX_SAVE_SIZE (32*32)
2641 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2643 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2645 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2647 Gr_opengl_mouse_saved_x1 = x;
2648 Gr_opengl_mouse_saved_y1 = y;
2649 Gr_opengl_mouse_saved_x2 = x+w-1;
2650 Gr_opengl_mouse_saved_y2 = y+h-1;
2652 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2653 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2654 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2655 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2657 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2658 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2660 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2661 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2663 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2665 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2667 glReadBuffer(GL_BACK);
2668 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);
2670 Gr_opengl_mouse_saved = 1;
2673 int gr_opengl_save_screen()
2677 if ( Gr_saved_screen ) {
2678 mprintf(( "Screen alread saved!\n" ));
2682 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2683 if (!Gr_saved_screen) {
2684 mprintf(( "Couldn't get memory for saved screen!\n" ));
2688 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2689 if (!Gr_saved_screen_tmp) {
2690 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2694 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2696 glReadBuffer(GL_FRONT);
2697 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);
2701 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2702 dptr = (ubyte *)Gr_saved_screen;
2703 for (int j = 0; j < gr_screen.max_h; j++) {
2704 sptr -= gr_screen.max_w*2;
2705 memcpy(dptr, sptr, gr_screen.max_w*2);
2706 dptr += gr_screen.max_w*2;
2709 free(Gr_saved_screen_tmp);
2711 if (Gr_opengl_mouse_saved) {
2712 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2713 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2714 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2715 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2718 dptr -= gr_screen.max_w*2;
2722 // this leaks texture handles, and the opengl doesn't currently
2723 // perform some sort of garbage collection, so a hack was added
2724 // to bmpman to make it free textures when released
2725 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2727 return Gr_saved_screen_bitmap;
2730 void gr_opengl_restore_screen(int id)
2734 if ( !Gr_saved_screen ) {
2739 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2741 gr_set_bitmap(Gr_saved_screen_bitmap);
2745 void gr_opengl_free_screen(int id)
2747 bm_release(Gr_saved_screen_bitmap);
2749 if ( Gr_saved_screen ) {
2750 free( Gr_saved_screen );
2751 Gr_saved_screen = NULL;
2755 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2760 void gr_opengl_dump_frame_stop()
2765 void gr_opengl_dump_frame()
2770 uint gr_opengl_lock()
2777 void gr_opengl_unlock()
2781 void opengl_zbias(int bias)
2784 glEnable(GL_POLYGON_OFFSET_FILL);
2785 glPolygonOffset(0.0, -bias);
2787 glDisable(GL_POLYGON_OFFSET_FILL);
2791 extern char *Osreg_title;
2792 void gr_opengl_init()
2795 gr_opengl_cleanup();
2799 mprintf(( "Initializing opengl graphics device...\n" ));
2803 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2805 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2809 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2810 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2811 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2812 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2813 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2815 int flags = SDL_OPENGL;
2817 if (!Cmdline_window && ( (os_config_read_uint( NULL, "Fullscreen", 1 ) == 1) || Cmdline_fullscreen ))
2818 flags |= SDL_FULLSCREEN;
2820 // don't automatically grab key/mouse if cmdline says so, else do
2821 if(Cmdline_no_grab) {
2822 SDL_WM_GrabInput(SDL_GRAB_OFF);
2824 SDL_WM_GrabInput(SDL_GRAB_ON);
2827 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2829 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2834 SDL_WM_SetCaption (Osreg_title, "FS2");
2836 /* might as well put this here */
2837 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2839 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2841 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2843 glMatrixMode(GL_PROJECTION);
2845 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2846 glMatrixMode(GL_MODELVIEW);
2849 glShadeModel(GL_SMOOTH);
2850 glEnable(GL_DITHER);
2851 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2852 glHint(GL_FOG_HINT, GL_NICEST);
2854 glEnable(GL_DEPTH_TEST);
2857 glEnable(GL_TEXTURE_2D);
2859 glDepthRange(0.0, 1.0);
2861 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2862 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2864 D3D_32bit = 1; // grd3d.cpp
2865 extern int D3D_enabled;
2868 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2869 1 = use secondary color ext
2870 2 = use opengl linear fog
2872 D3D_fog_mode = 2; // grd3d.cpp
2876 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2883 gr_screen.bits_per_pixel = 15;
2884 gr_screen.bytes_per_pixel = 2;
2889 Gr_red.mask = 0x7C00;
2894 Gr_green.mask = 0x3E0;
2899 Gr_blue.mask = 0x1F;
2904 gr_screen.bits_per_pixel = 16;
2905 gr_screen.bytes_per_pixel = 2;
2910 Gr_red.mask = 0xF800;
2915 Gr_green.mask = 0x7E0;
2920 Gr_blue.mask = 0x1F;
2925 gr_screen.bits_per_pixel = 32;
2926 gr_screen.bytes_per_pixel = 4;
2931 Gr_red.mask = 0xff0000;
2936 Gr_green.mask = 0xff00;
2941 Gr_blue.mask = 0xff;
2946 Int3(); // Illegal bpp
2949 // DDOI - set these so no one else does!
2950 Gr_ta_red.mask = 0x0f00;
2951 Gr_ta_red.shift = 8;
2952 Gr_ta_red.scale = 16;
2954 Gr_ta_green.mask = 0x00f0;
2955 Gr_ta_green.shift = 4;
2956 Gr_ta_green.scale = 16;
2958 Gr_ta_blue.mask = 0x000f;
2959 Gr_ta_blue.shift = 0;
2960 Gr_ta_blue.scale = 16;
2962 Gr_ta_alpha.mask = 0xf000;
2963 Gr_ta_alpha.shift = 12;
2964 Gr_ta_alpha.scale = 16;
2967 opengl_tcache_init (1);
2970 Gr_current_red = &Gr_red;
2971 Gr_current_blue = &Gr_blue;
2972 Gr_current_green = &Gr_green;
2973 Gr_current_alpha = &Gr_alpha;
2975 gr_screen.gf_flip = gr_opengl_flip;
2976 gr_screen.gf_flip_window = gr_opengl_flip_window;
2977 gr_screen.gf_set_clip = gr_opengl_set_clip;
2978 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2979 gr_screen.gf_set_font = grx_set_font;
2981 gr_screen.gf_set_color = gr_opengl_set_color;
2982 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2983 gr_screen.gf_create_shader = gr_opengl_create_shader;
2984 gr_screen.gf_set_shader = gr_opengl_set_shader;
2985 gr_screen.gf_clear = gr_opengl_clear;
2986 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2987 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2988 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2989 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2991 gr_screen.gf_rect = gr_opengl_rect;
2992 gr_screen.gf_shade = gr_opengl_shade;
2993 gr_screen.gf_string = gr_opengl_string;
2994 gr_screen.gf_circle = gr_opengl_circle;
2996 gr_screen.gf_line = gr_opengl_line;
2997 gr_screen.gf_aaline = gr_opengl_aaline;
2998 gr_screen.gf_pixel = gr_opengl_pixel;
2999 gr_screen.gf_scaler = gr_opengl_scaler;
3000 gr_screen.gf_tmapper = gr_opengl_tmapper;
3002 gr_screen.gf_gradient = gr_opengl_gradient;
3004 gr_screen.gf_set_palette = gr_opengl_set_palette;
3005 gr_screen.gf_get_color = gr_opengl_get_color;
3006 gr_screen.gf_init_color = gr_opengl_init_color;
3007 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3008 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3009 gr_screen.gf_print_screen = gr_opengl_print_screen;
3011 gr_screen.gf_fade_in = gr_opengl_fade_in;
3012 gr_screen.gf_fade_out = gr_opengl_fade_out;
3013 gr_screen.gf_flash = gr_opengl_flash;
3015 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3016 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3017 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3019 gr_screen.gf_save_screen = gr_opengl_save_screen;
3020 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3021 gr_screen.gf_free_screen = gr_opengl_free_screen;
3023 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3024 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3025 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3027 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3029 gr_screen.gf_lock = gr_opengl_lock;
3030 gr_screen.gf_unlock = gr_opengl_unlock;
3032 gr_screen.gf_fog_set = gr_opengl_fog_set;
3034 gr_screen.gf_get_region = gr_opengl_get_region;
3036 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3038 gr_screen.gf_set_cull = gr_opengl_set_cull;
3040 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3042 gr_screen.gf_filter_set = gr_opengl_filter_set;
3044 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3046 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;