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.65 2003/06/19 11:52:47 taylor
19 * fix texture size issue with lower detail settings
21 * Revision 1.64 2003/05/28 06:02:04 taylor
22 * fix transparency in green weapon blobs
24 * Revision 1.63 2003/05/09 05:05:52 taylor
25 * improve gr_(de)activate, mouse grab, default fullscreen
27 * Revision 1.62 2003/05/04 04:56:53 taylor
28 * move SDL_Quit to os_deinit to fix fonttool segfault
30 * Revision 1.61 2003/02/02 21:13:27 relnev
31 * minor updates (made functions static, tried to improve texture sizes)
33 * Revision 1.60 2003/02/02 20:20:03 relnev
34 * add large texture support (Taylor Richards)
36 * Revision 1.59 2002/09/04 01:38:09 relnev
39 * Revision 1.57 2002/08/31 01:39:13 theoddone33
40 * Speed up the renderer a tad
42 * Revision 1.56 2002/08/01 05:03:11 relnev
45 * Revision 1.55 2002/08/01 04:55:45 relnev
46 * experimenting with texture state
48 * Revision 1.54 2002/07/30 15:00:15 relnev
49 * not use luminance alpha by default
51 * Revision 1.53 2002/06/22 17:08:16 relnev
52 * more fixes to unused alpha code
54 * Revision 1.52 2002/06/21 23:59:14 relnev
55 * moved a gr_opengl_set_state to after the gr_tcache_set
57 * Revision 1.51 2002/06/19 04:52:45 relnev
58 * MacOS X updates (Ryan)
60 * Revision 1.50 2002/06/18 23:20:44 relnev
63 * adjusted line offsets.
65 * Revision 1.49 2002/06/17 23:11:39 relnev
66 * enable sdl key repeating.
70 * Revision 1.48 2002/06/09 04:41:17 relnev
71 * added copyright header
73 * Revision 1.47 2002/06/09 03:16:04 relnev
76 * removed unneeded asm, old sdl 2d setup.
78 * fixed crash caused by opengl_get_region.
80 * Revision 1.46 2002/06/05 04:03:32 relnev
81 * finished cfilesystem.
83 * removed some old code.
85 * fixed mouse save off-by-one.
89 * Revision 1.45 2002/06/03 09:25:37 relnev
90 * implement mouse cursor and screen save/restore
92 * Revision 1.44 2002/06/02 18:46:59 relnev
95 * Revision 1.43 2002/06/02 11:34:00 relnev
98 * Revision 1.42 2002/06/02 10:28:17 relnev
99 * fix texture handle leak
101 * Revision 1.41 2002/06/01 09:00:34 relnev
102 * silly debug memmanager
104 * Revision 1.40 2002/06/01 07:12:33 relnev
105 * a few NDEBUG updates.
107 * removed a few warnings.
109 * Revision 1.39 2002/06/01 05:33:15 relnev
110 * copied more code over.
112 * added scissor clipping.
114 * Revision 1.38 2002/06/01 03:35:27 relnev
117 * Revision 1.37 2002/06/01 03:32:00 relnev
118 * fix texture loading mistake.
120 * enable some d3d stuff for opengl also
122 * Revision 1.36 2002/05/31 23:25:03 relnev
125 * Revision 1.34 2002/05/31 22:15:22 relnev
128 * Revision 1.33 2002/05/31 22:04:55 relnev
129 * use d3d rect_internal
131 * Revision 1.32 2002/05/31 06:28:23 relnev
134 * Revision 1.31 2002/05/31 06:04:39 relnev
137 * Revision 1.30 2002/05/31 03:56:11 theoddone33
138 * Change tmapper polygon winding and enable culling
140 * Revision 1.29 2002/05/31 03:34:02 theoddone33
144 * Revision 1.28 2002/05/31 00:06:59 relnev
147 * Revision 1.27 2002/05/30 23:46:29 theoddone33
148 * some minor key changes (not necessarily fixes)
150 * Revision 1.26 2002/05/30 23:33:12 relnev
151 * implemented a few more functions.
153 * Revision 1.25 2002/05/30 23:01:16 relnev
154 * implement gr_opengl_set_state.
156 * Revision 1.24 2002/05/30 22:12:57 relnev
157 * finish default texture case
159 * Revision 1.23 2002/05/30 22:02:30 theoddone33
162 * Revision 1.22 2002/05/30 21:44:48 relnev
163 * implemented some missing texture stuff.
165 * enable bitmap polys for opengl.
167 * work around greenness in bitmaps.
169 * Revision 1.21 2002/05/30 17:29:30 theoddone33
170 * Fix some more stubs, change at least one polygon winding since culling is now
173 * Revision 1.20 2002/05/30 16:50:24 theoddone33
174 * Keyboard partially fixed
176 * Revision 1.19 2002/05/30 08:13:14 relnev
179 * Revision 1.18 2002/05/29 23:37:36 relnev
182 * Revision 1.17 2002/05/29 23:17:49 theoddone33
183 * Non working text code and fixed keys
185 * Revision 1.16 2002/05/29 19:45:13 theoddone33
186 * More changes on texture loading
188 * Revision 1.15 2002/05/29 19:06:48 theoddone33
189 * Enable string printing. Enable texture mapping
191 * Revision 1.14 2002/05/29 08:54:40 relnev
192 * "fixed" bitmap drawing.
194 * copied more d3d code over.
196 * Revision 1.13 2002/05/29 06:25:13 theoddone33
197 * Keyboard input, mouse tracking now work
199 * Revision 1.12 2002/05/29 04:52:45 relnev
202 * Revision 1.11 2002/05/29 04:29:56 relnev
203 * removed some unncessary stubbing, implemented opengl rect
205 * Revision 1.10 2002/05/29 04:13:27 theoddone33
208 * Revision 1.9 2002/05/29 03:35:51 relnev
211 * Revision 1.8 2002/05/29 03:30:05 relnev
212 * update opengl stubs
214 * Revision 1.7 2002/05/29 02:52:32 theoddone33
215 * Enable OpenGL renderer
217 * Revision 1.6 2002/05/28 04:56:51 theoddone33
218 * runs a little bit now
220 * Revision 1.5 2002/05/28 04:07:28 theoddone33
221 * New graphics stubbing arrangement
223 * Revision 1.4 2002/05/27 23:39:34 relnev
226 * Revision 1.3 2002/05/27 22:35:01 theoddone33
229 * Revision 1.2 2002/05/27 22:32:02 theoddone33
230 * throw all d3d stuff at opengl
232 * Revision 1.1.1.1 2002/05/03 03:28:09 root
236 * 10 7/14/99 9:42a Dave
237 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
240 * 9 7/09/99 9:51a Dave
241 * Added thick polyline code.
243 * 8 6/29/99 10:35a Dave
244 * Interface polygon bitmaps! Whee!
246 * 7 2/03/99 11:44a Dave
247 * Fixed d3d transparent textures.
249 * 6 1/24/99 11:37p Dave
250 * First full rev of beam weapons. Very customizable. Removed some bogus
251 * Int3()'s in low level net code.
253 * 5 12/18/98 1:13a Dave
254 * Rough 1024x768 support for Direct3D. Proper detection and usage through
257 * 4 12/06/98 2:36p Dave
258 * Drastically improved nebula fogging.
260 * 3 11/11/98 5:37p Dave
261 * Checkin for multiplayer testing.
263 * 2 10/07/98 10:53a Dave
266 * 1 10/07/98 10:49a Dave
268 * 14 5/20/98 9:46p John
269 * added code so the places in code that change half the palette don't
270 * have to clear the screen.
272 * 13 5/06/98 5:30p John
273 * Removed unused cfilearchiver. Removed/replaced some unused/little used
274 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
275 * DirectX header files and libs that fixed the Direct3D alpha blending
278 * 12 4/14/98 12:15p John
279 * Made 16-bpp movies work.
281 * 11 3/12/98 5:36p John
282 * Took out any unused shaders. Made shader code take rgbc instead of
283 * matrix and vector since noone used it like a matrix and it would have
284 * been impossible to do in hardware. Made Glide implement a basic
285 * shader for online help.
287 * 10 3/10/98 4:18p John
288 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
289 * & Glide have popups and print screen. Took out all >8bpp software
290 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
291 * support Fred. Made zbuffering key off of functions rather than one
294 * 9 12/02/97 4:00p John
295 * Added first rev of thruster glow, along with variable levels of
296 * translucency, which retquired some restructing of palman.
298 * 8 10/03/97 9:10a John
299 * added better antialiased line drawer
301 * 7 9/23/97 10:45a John
302 * made so you can tell bitblt code to rle a bitmap by passing flag to
305 * 6 9/09/97 11:01a Sandeep
306 * fixed warning level 4 bugs
308 * 5 7/10/97 2:06p John
309 * added code to specify alphablending type for bitmaps.
311 * 4 6/17/97 7:04p John
312 * added d3d support for gradients.
313 * fixed some color bugs by adding screen signatures instead of watching
314 * flags and palette changes.
316 * 3 6/12/97 2:50a Lawrance
317 * bm_unlock() now passed bitmap number, not pointer
319 * 2 6/11/97 1:12p John
320 * Started fixing all the text colors in the game.
322 * 1 5/12/97 12:14p John
329 #include <windowsx.h>
343 #include "floating.h"
345 #include "systemvars.h"
346 #include "grinternal.h"
347 #include "gropengl.h"
351 #include "osregistry.h"
354 static int Inited = 0;
356 typedef enum gr_texture_source {
358 TEXTURE_SOURCE_DECAL,
359 TEXTURE_SOURCE_NO_FILTERING,
362 typedef enum gr_alpha_blend {
363 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
364 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
365 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
366 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
367 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
370 typedef enum gr_zbuffer_type {
377 #define NEBULA_COLORS 20
379 volatile int GL_activate = 0;
380 volatile int GL_deactivate = 0;
382 static int GL_use_luminance_alpha;
384 static char *Gr_saved_screen = NULL;
385 static int Gr_saved_screen_bitmap;
388 // Throw in some dummy functions - DDOI
390 int D3D_32bit = 0; // grd3d.cpp
391 int D3D_fog_mode = -1; // grd3d.cpp
392 int D3D_inited = 0; // grd3d.cpp
393 int D3D_zbias = 1; // grd3d.cpp
394 int D3d_rendition_uvs = 0; // grd3d.cpp
401 void d3d_zbias (int a)
407 static void gr_opengl_set_texture_state(gr_texture_source ts);
409 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
410 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
412 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
414 gr_opengl_set_texture_state(ts);
416 if (ab != GL_current_alpha_blend) {
418 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
419 glBlendFunc(GL_ONE, GL_ZERO);
421 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
422 glBlendFunc(GL_ONE, GL_ONE);
424 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
425 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
427 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
428 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
430 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
431 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
437 GL_current_alpha_blend = ab;
440 if (zt != GL_current_zbuffer_type) {
442 case ZBUFFER_TYPE_NONE:
443 glDepthFunc(GL_ALWAYS);
444 glDepthMask(GL_FALSE);
446 case ZBUFFER_TYPE_READ:
447 glDepthFunc(GL_LESS);
448 glDepthMask(GL_FALSE);
450 case ZBUFFER_TYPE_WRITE:
451 glDepthFunc(GL_ALWAYS);
452 glDepthMask(GL_TRUE);
454 case ZBUFFER_TYPE_FULL:
455 glDepthFunc(GL_LESS);
456 glDepthMask(GL_TRUE);
462 GL_current_zbuffer_type = zt;
466 void gr_opengl_activate(int active)
471 // don't grab key/mouse if cmdline says so or if we're fullscreen
472 if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
473 SDL_WM_GrabInput(SDL_GRAB_ON);
478 // let go of mouse/keyboard
479 SDL_WM_GrabInput(SDL_GRAB_OFF);
484 static void opengl_tcache_flush ();
486 void gr_opengl_preload_init()
488 if (gr_screen.mode != GR_OPENGL) {
492 opengl_tcache_flush ();
495 int GL_should_preload = 0;
496 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
498 if ( gr_screen.mode != GR_OPENGL) {
502 if ( !GL_should_preload ) {
506 float u_scale, v_scale;
510 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
512 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
516 mprintf(("Texture upload failed!\n" ));
522 void gr_opengl_pixel(int x, int y)
527 void gr_opengl_clear()
529 glClearColor(gr_screen.current_clear_color.red / 255.0,
530 gr_screen.current_clear_color.green / 255.0,
531 gr_screen.current_clear_color.blue / 255.0, 1.0);
533 glClear ( GL_COLOR_BUFFER_BIT );
536 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
537 static void opengl_tcache_frame ();
539 void gr_opengl_flip()
547 extern int Gr_opengl_mouse_saved;
548 Gr_opengl_mouse_saved = 0;
550 if ( mouse_is_visible() ) {
554 mouse_get_pos( &mx, &my );
556 gr_opengl_save_mouse_area(mx,my,32,32);
558 if ( Gr_cursor == -1 ) {
561 gr_set_bitmap(Gr_cursor);
567 GLenum error = glGetError();
570 error = glGetError();
572 if (error != GL_NO_ERROR) {
573 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
576 } while (error != GL_NO_ERROR);
579 SDL_GL_SwapBuffers ();
581 opengl_tcache_frame ();
583 int cnt = GL_activate;
586 opengl_tcache_flush();
587 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
593 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
597 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
602 void gr_opengl_set_clip(int x,int y,int w,int h)
604 // check for sanity of parameters
610 if (x >= gr_screen.max_w)
611 x = gr_screen.max_w - 1;
612 if (y >= gr_screen.max_h)
613 y = gr_screen.max_h - 1;
615 if (x + w > gr_screen.max_w)
616 w = gr_screen.max_w - x;
617 if (y + h > gr_screen.max_h)
618 h = gr_screen.max_h - y;
620 if (w > gr_screen.max_w)
622 if (h > gr_screen.max_h)
625 gr_screen.offset_x = x;
626 gr_screen.offset_y = y;
627 gr_screen.clip_left = 0;
628 gr_screen.clip_right = w-1;
629 gr_screen.clip_top = 0;
630 gr_screen.clip_bottom = h-1;
631 gr_screen.clip_width = w;
632 gr_screen.clip_height = h;
634 glEnable(GL_SCISSOR_TEST);
635 glScissor(x, gr_screen.max_h-y-h, w, h);
638 void gr_opengl_reset_clip()
640 gr_screen.offset_x = 0;
641 gr_screen.offset_y = 0;
642 gr_screen.clip_left = 0;
643 gr_screen.clip_top = 0;
644 gr_screen.clip_right = gr_screen.max_w - 1;
645 gr_screen.clip_bottom = gr_screen.max_h - 1;
646 gr_screen.clip_width = gr_screen.max_w;
647 gr_screen.clip_height = gr_screen.max_h;
649 glDisable(GL_SCISSOR_TEST);
650 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
653 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
655 gr_screen.current_alpha = alpha;
656 gr_screen.current_alphablend_mode = alphablend_mode;
657 gr_screen.current_bitblt_mode = bitblt_mode;
658 gr_screen.current_bitmap = bitmap_num;
660 gr_screen.current_bitmap_sx = sx;
661 gr_screen.current_bitmap_sy = sy;
664 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
666 shade->screen_sig = gr_screen.signature;
673 void gr_opengl_set_shader( shader * shade )
676 if (shade->screen_sig != gr_screen.signature) {
677 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
679 gr_screen.current_shader = *shade;
681 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
686 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
688 STUB_FUNCTION; /* who called me? */
691 void gr_opengl_bitmap(int x, int y)
693 STUB_FUNCTION; /* who called me? */
696 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
700 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
702 saved_zbuf = gr_zbuffer_get();
704 // start the frame, no zbuffering, no culling
706 gr_zbuffer_set(GR_ZBUFF_NONE);
715 v[0].flags = PF_PROJECTED;
722 v[1].sx = i2fl(x + w);
727 v[1].flags = PF_PROJECTED;
734 v[2].sx = i2fl(x + w);
735 v[2].sy = i2fl(y + h);
739 v[2].flags = PF_PROJECTED;
747 v[3].sy = i2fl(y + h);
751 v[3].flags = PF_PROJECTED;
759 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
763 // restore zbuffer and culling
764 gr_zbuffer_set(saved_zbuf);
768 void gr_opengl_rect(int x,int y,int w,int h)
770 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);
773 void gr_opengl_shade(int x,int y,int w,int h)
780 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
781 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
782 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
783 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
784 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
785 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
786 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
787 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
789 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
792 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
797 if ( !gr_screen.current_color.is_alphacolor ) return;
799 float u_scale, v_scale;
801 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
802 // Couldn't set texture
803 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
807 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
809 float u0, u1, v0, v1;
810 float x1, x2, y1, y2;
813 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
815 u0 = u_scale*i2fl(sx)/i2fl(bw);
816 v0 = v_scale*i2fl(sy)/i2fl(bh);
818 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
819 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
821 x1 = i2fl(x+gr_screen.offset_x);
822 y1 = i2fl(y+gr_screen.offset_y);
823 x2 = i2fl(x+w+gr_screen.offset_x);
824 y2 = i2fl(y+h+gr_screen.offset_y);
826 if ( gr_screen.current_color.is_alphacolor ) {
827 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
829 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
833 glTexCoord2f (u0, v1);
834 glVertex3f (x1, y2, -0.99);
836 glTexCoord2f (u1, v1);
837 glVertex3f (x2, y2, -0.99);
839 glTexCoord2f (u1, v0);
840 glVertex3f (x2, y1, -0.99);
842 glTexCoord2f (u0, v0);
843 glVertex3f (x1, y1, -0.99);
847 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
854 int dx1=x, dx2=x+w-1;
855 int dy1=y, dy2=y+h-1;
858 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
863 if ( count > 1 ) Int3();
867 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
868 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
869 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
870 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
871 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
872 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
899 if ( w < 1 ) return; // clipped away!
900 if ( h < 1 ) return; // clipped away!
904 // Make sure clipping algorithm works
908 Assert( w == (dx2-dx1+1) );
909 Assert( h == (dy2-dy1+1) );
912 Assert( sx+w <= bw );
913 Assert( sy+h <= bh );
914 Assert( dx2 >= dx1 );
915 Assert( dy2 >= dy1 );
916 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
917 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
918 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
919 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
922 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
923 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
926 void gr_opengl_aabitmap(int x, int y)
930 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
931 int dx1=x, dx2=x+w-1;
932 int dy1=y, dy2=y+h-1;
935 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
936 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
937 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
938 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
939 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
940 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
942 if ( sx < 0 ) return;
943 if ( sy < 0 ) return;
944 if ( sx >= w ) return;
945 if ( sy >= h ) return;
947 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
948 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
951 void gr_opengl_string( int sx, int sy, char *s )
953 int width, spacing, letter;
956 if ( !Current_font ) {
960 gr_set_bitmap(Current_font->bitmap_id);
965 if (sx==0x8000) { //centered
966 x = get_centered_x(s);
978 y += Current_font->h;
979 if (sx==0x8000) { //centered
980 x = get_centered_x(s);
987 letter = get_char_width(s[0],s[1],&width,&spacing);
990 //not in font, draw as space
998 // Check if this character is totally clipped
999 if ( x + width < gr_screen.clip_left ) continue;
1000 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1001 if ( x > gr_screen.clip_right ) continue;
1002 if ( y > gr_screen.clip_bottom ) continue;
1005 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1006 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1010 wc = width - xd; hc = Current_font->h - yd;
1011 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1012 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1014 if ( wc < 1 ) continue;
1015 if ( hc < 1 ) continue;
1017 int u = Current_font->bm_u[letter];
1018 int v = Current_font->bm_v[letter];
1020 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1024 void gr_opengl_line(int x1,int y1,int x2,int y2)
1026 int clipped = 0, swapped=0;
1028 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1030 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);
1035 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1036 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1037 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1038 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1040 if ( x1 == x2 && y1 == y2 ) {
1041 glBegin (GL_POINTS);
1042 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1043 glVertex3f (sx1, sy1, -0.99f);
1055 } else if ( y1 == y2 ) {
1064 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1065 glVertex3f (sx2, sy2, -0.99f);
1066 glVertex3f (sx1, sy1, -0.99f);
1070 void gr_opengl_aaline(vertex *v1, vertex *v2)
1072 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1075 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1077 int clipped = 0, swapped=0;
1079 if ( !gr_screen.current_color.is_alphacolor ) {
1080 gr_line( x1, y1, x2, y2 );
1084 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);
1086 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1088 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1089 int ba = swapped ? gr_screen.current_color.alpha : 0;
1094 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1095 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1096 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1097 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1105 } else if ( y1 == y2 ) {
1114 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1115 glVertex3f (sx2, sy2, -0.99f);
1116 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1117 glVertex3f (sx1, sy1, -0.99f);
1121 void gr_opengl_circle( int xc, int yc, int d )
1131 if ( (xc+r) < gr_screen.clip_left ) return;
1132 if ( (xc-r) > gr_screen.clip_right ) return;
1133 if ( (yc+r) < gr_screen.clip_top ) return;
1134 if ( (yc-r) > gr_screen.clip_bottom ) return;
1137 // Draw the first octant
1138 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1139 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1144 // Draw the second octant
1145 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1146 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1154 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1155 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1160 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1164 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1165 if (f_float < 0.0f) {
1173 *a = (int)(f_float * 255.0);
1176 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1179 float u_scale = 1.0f, v_scale = 1.0f;
1181 // Make nebula use the texture mapper... this blends the colors better.
1182 if ( flags & TMAP_FLAG_NEBULA ){
1186 gr_texture_source texture_source = (gr_texture_source)-1;
1187 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1188 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1190 if ( gr_zbuffering ) {
1191 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1192 zbuffer_type = ZBUFFER_TYPE_READ;
1194 zbuffer_type = ZBUFFER_TYPE_FULL;
1197 zbuffer_type = ZBUFFER_TYPE_NONE;
1202 int tmap_type = TCACHE_TYPE_NORMAL;
1206 if ( flags & TMAP_FLAG_TEXTURED ) {
1209 r = gr_screen.current_color.red;
1210 g = gr_screen.current_color.green;
1211 b = gr_screen.current_color.blue;
1214 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1217 tmap_type = TCACHE_TYPE_NORMAL;
1218 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1220 // Blend with screen pixel using src*alpha+dst
1221 float factor = gr_screen.current_alpha;
1225 if ( factor <= 1.0f ) {
1226 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1227 r = (r*tmp_alpha)/255;
1228 g = (g*tmp_alpha)/255;
1229 b = (b*tmp_alpha)/255;
1232 tmap_type = TCACHE_TYPE_XPARENT;
1234 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1236 // Blend with screen pixel using src*alpha+dst
1237 float factor = gr_screen.current_alpha;
1239 if ( factor > 1.0f ) {
1242 alpha = fl2i(gr_screen.current_alpha*255.0f);
1246 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1247 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1249 alpha_blend = ALPHA_BLEND_NONE;
1254 if(flags & TMAP_FLAG_BITMAP_SECTION){
1255 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1258 texture_source = TEXTURE_SOURCE_NONE;
1260 if ( flags & TMAP_FLAG_TEXTURED ) {
1261 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 ))
1263 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1267 // use nonfiltered textures for bitmap sections
1268 if(flags & TMAP_FLAG_BITMAP_SECTION){
1269 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1271 texture_source = TEXTURE_SOURCE_DECAL;
1276 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1278 if ( flags & TMAP_FLAG_TEXTURED )
1284 if (flags & TMAP_FLAG_PIXEL_FOG) {
1290 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1292 vertex * va = verts[i];
1296 x = fl2i(va->sx*16.0f);
1297 y = fl2i(va->sy*16.0f);
1299 x += gr_screen.offset_x*16;
1300 y += gr_screen.offset_y*16;
1302 sx = i2fl(x) / 16.0f;
1303 sy = i2fl(y) / 16.0f;
1305 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1316 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1319 glBegin(GL_TRIANGLE_FAN);
1320 for (i = nv-1; i >= 0; i--) {
1321 vertex * va = verts[i];
1327 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1328 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1337 if ( flags & TMAP_FLAG_CORRECT ) {
1343 if (flags & TMAP_FLAG_ALPHA) {
1349 if (flags & TMAP_FLAG_NEBULA ) {
1350 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1351 r = gr_palette[pal*3+0];
1352 g = gr_palette[pal*3+1];
1353 b = gr_palette[pal*3+2];
1354 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1355 r = Gr_gamma_lookup[verts[i]->b];
1356 g = Gr_gamma_lookup[verts[i]->b];
1357 b = Gr_gamma_lookup[verts[i]->b];
1358 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1359 // Make 0.75 be 256.0f
1360 r = Gr_gamma_lookup[verts[i]->r];
1361 g = Gr_gamma_lookup[verts[i]->g];
1362 b = Gr_gamma_lookup[verts[i]->b];
1364 // use constant RGB values...
1366 glColor4ub (r,g,b,a);
1368 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1371 /* this is for GL_EXT_SECONDARY_COLOR */
1372 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1373 /* do separate color call here */
1379 x = fl2i(va->sx*16.0f);
1380 y = fl2i(va->sy*16.0f);
1382 x += gr_screen.offset_x*16;
1383 y += gr_screen.offset_y*16;
1385 sx = i2fl(x) / 16.0f;
1386 sy = i2fl(y) / 16.0f;
1388 if ( flags & TMAP_FLAG_TEXTURED ) {
1391 glTexCoord2f(tu, tv);
1394 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1399 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1401 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1404 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1406 void gr_opengl_scaler(vertex *va, vertex *vb )
1408 float x0, y0, x1, y1;
1409 float u0, v0, u1, v1;
1410 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1411 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1412 float xmin, xmax, ymin, ymax;
1413 int dx0, dy0, dx1, dy1;
1415 //============= CLIP IT =====================
1417 x0 = va->sx; y0 = va->sy;
1418 x1 = vb->sx; y1 = vb->sy;
1420 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1421 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1423 u0 = va->u; v0 = va->v;
1424 u1 = vb->u; v1 = vb->v;
1426 // Check for obviously offscreen bitmaps...
1427 if ( (y1<=y0) || (x1<=x0) ) return;
1428 if ( (x1<xmin ) || (x0>xmax) ) return;
1429 if ( (y1<ymin ) || (y0>ymax) ) return;
1431 clipped_u0 = u0; clipped_v0 = v0;
1432 clipped_u1 = u1; clipped_v1 = v1;
1434 clipped_x0 = x0; clipped_y0 = y0;
1435 clipped_x1 = x1; clipped_y1 = y1;
1437 // Clip the left, moving u0 right as necessary
1439 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1443 // Clip the right, moving u1 left as necessary
1445 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1449 // Clip the top, moving v0 down as necessary
1451 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1455 // Clip the bottom, moving v1 up as necessary
1457 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1461 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1462 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1464 if (dx1<=dx0) return;
1465 if (dy1<=dy0) return;
1467 //============= DRAW IT =====================
1481 v[1].sx = clipped_x1;
1482 v[1].sy = clipped_y0;
1485 v[1].u = clipped_u1;
1486 v[1].v = clipped_v0;
1489 v[2].sx = clipped_x1;
1490 v[2].sy = clipped_y1;
1493 v[2].u = clipped_u1;
1494 v[2].v = clipped_v1;
1497 v[3].sx = clipped_x0;
1498 v[3].sy = clipped_y1;
1501 v[3].u = clipped_u0;
1502 v[3].v = clipped_v1;
1504 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1507 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1511 void gr_opengl_get_color( int * r, int * g, int * b )
1513 if (r) *r = gr_screen.current_color.red;
1514 if (g) *g = gr_screen.current_color.green;
1515 if (b) *b = gr_screen.current_color.blue;
1518 void gr_opengl_init_color(color *c, int r, int g, int b)
1520 c->screen_sig = gr_screen.signature;
1521 c->red = (unsigned char)r;
1522 c->green = (unsigned char)g;
1523 c->blue = (unsigned char)b;
1525 c->ac_type = AC_TYPE_NONE;
1527 c->is_alphacolor = 0;
1531 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1533 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1534 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1535 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1536 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1538 gr_opengl_init_color( clr, r, g, b );
1540 clr->alpha = (unsigned char)alpha;
1541 clr->ac_type = (ubyte)type;
1542 clr->alphacolor = -1;
1543 clr->is_alphacolor = 1;
1546 void gr_opengl_set_color( int r, int g, int b )
1548 Assert((r >= 0) && (r < 256));
1549 Assert((g >= 0) && (g < 256));
1550 Assert((b >= 0) && (b < 256));
1552 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1555 void gr_opengl_set_color_fast(color *dst)
1557 if ( dst->screen_sig != gr_screen.signature ) {
1558 if ( dst->is_alphacolor ) {
1559 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1561 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1564 gr_screen.current_color = *dst;
1567 void gr_opengl_print_screen(char *filename)
1572 int gr_opengl_supports_res_ingame(int res)
1579 int gr_opengl_supports_res_interface(int res)
1586 static void opengl_tcache_cleanup ();
1588 void gr_opengl_cleanup()
1590 if ( !Inited ) return;
1597 opengl_tcache_cleanup ();
1602 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1604 Assert((r >= 0) && (r < 256));
1605 Assert((g >= 0) && (g < 256));
1606 Assert((b >= 0) && (b < 256));
1608 if (fog_mode == GR_FOGMODE_NONE) {
1609 if (gr_screen.current_fog_mode != fog_mode) {
1612 gr_screen.current_fog_mode = fog_mode;
1617 if (gr_screen.current_fog_mode != fog_mode) {
1620 if (D3D_fog_mode == 2) {
1621 glFogi(GL_FOG_MODE, GL_LINEAR);
1624 gr_screen.current_fog_mode = fog_mode;
1627 if ( (gr_screen.current_fog_color.red != r) ||
1628 (gr_screen.current_fog_color.green != g) ||
1629 (gr_screen.current_fog_color.blue != b) ) {
1632 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1634 fc[0] = (float)r/255.0;
1635 fc[1] = (float)g/255.0;
1636 fc[2] = (float)b/255.0;
1639 glFogfv(GL_FOG_COLOR, fc);
1642 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1643 ((fog_near != gr_screen.fog_near) ||
1644 (fog_far != gr_screen.fog_far)) ) {
1645 gr_screen.fog_near = fog_near;
1646 gr_screen.fog_far = fog_far;
1648 if (D3D_fog_mode == 2) {
1649 glFogf(GL_FOG_START, fog_near);
1650 glFogf(GL_FOG_END, fog_far);
1655 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1660 void gr_opengl_set_cull(int cull)
1663 glEnable (GL_CULL_FACE);
1664 glFrontFace (GL_CCW);
1666 glDisable (GL_CULL_FACE);
1670 void gr_opengl_filter_set(int filter)
1675 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1678 gr_set_bitmap(bmap1);
1681 gr_set_bitmap(bmap2);
1687 typedef struct tcache_slot_opengl {
1688 GLuint texture_handle;
1689 float u_scale, v_scale;
1692 int used_this_frame;
1697 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1698 tcache_slot_opengl *parent;
1700 gr_texture_source texture_mode;
1701 } tcache_slot_opengl;
1703 static void *Texture_sections = NULL;
1704 static tcache_slot_opengl *Textures = NULL;
1706 static tcache_slot_opengl *GL_bound_texture;
1708 int GL_texture_sections = 0;
1709 int GL_texture_ram = 0;
1710 int GL_frame_count = 0;
1711 int GL_min_texture_width = 0;
1712 int GL_max_texture_width = 0;
1713 int GL_min_texture_height = 0;
1714 int GL_max_texture_height = 0;
1715 int GL_square_textures = 0;
1716 int GL_textures_in = 0;
1717 int GL_textures_in_frame = 0;
1718 int GL_last_bitmap_id = -1;
1719 int GL_last_detail = -1;
1720 int GL_last_bitmap_type = -1;
1721 int GL_last_section_x = -1;
1722 int GL_last_section_y = -1;
1726 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1728 static void gr_opengl_set_texture_state(gr_texture_source ts)
1730 if (ts == TEXTURE_SOURCE_NONE) {
1731 GL_bound_texture = NULL;
1733 glBindTexture(GL_TEXTURE_2D, 0);
1734 gr_tcache_set(-1, -1, NULL, NULL );
1735 } else if (GL_bound_texture &&
1736 GL_bound_texture->texture_mode != ts) {
1738 case TEXTURE_SOURCE_DECAL:
1739 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1740 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1742 case TEXTURE_SOURCE_NO_FILTERING:
1743 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1744 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1750 GL_bound_texture->texture_mode = ts;
1753 GL_current_texture_source = ts;
1756 static int opengl_max_tex_size_get()
1758 GLint max_texture_size = 0;
1760 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1761 return max_texture_size;
1764 static void opengl_tcache_init (int use_sections)
1768 // DDOI - FIXME skipped a lot of stuff here
1769 GL_should_preload = 0;
1771 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1774 if ( tmp_pl == 0 ) {
1775 GL_should_preload = 0;
1776 } else if ( tmp_pl == 1 ) {
1777 GL_should_preload = 1;
1779 GL_should_preload = 1;
1784 GL_min_texture_width = 16;
1785 GL_min_texture_height = 16;
1787 GL_max_texture_width = opengl_max_tex_size_get();
1788 GL_max_texture_height = opengl_max_tex_size_get();
1790 /* TODO: why don't textures work correctly when this is disabled? */
1791 GL_square_textures = 1;
1793 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1799 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1800 if(!Texture_sections){
1803 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1806 // Init the texture structures
1807 int section_count = 0;
1808 for( i=0; i<MAX_BITMAPS; i++ ) {
1810 Textures[i].vram_texture = NULL;
1811 Textures[i].vram_texture_surface = NULL;
1813 Textures[i].texture_handle = 0;
1815 Textures[i].bitmap_id = -1;
1816 Textures[i].size = 0;
1817 Textures[i].used_this_frame = 0;
1819 Textures[i].parent = NULL;
1821 // allocate sections
1823 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1824 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1825 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1826 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1828 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1829 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1831 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1832 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1833 Textures[i].data_sections[idx][s_idx]->size = 0;
1834 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1838 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1839 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1840 Textures[i].data_sections[idx][s_idx] = NULL;
1846 GL_texture_sections = use_sections;
1848 //GL_last_detail = Detail.hardware_textures;
1849 GL_last_bitmap_id = -1;
1850 GL_last_bitmap_type = -1;
1852 GL_last_section_x = -1;
1853 GL_last_section_y = -1;
1856 GL_textures_in_frame = 0;
1859 static int opengl_free_texture (tcache_slot_opengl *t);
1861 void opengl_free_texture_with_handle(int handle)
1863 for(int i=0; i<MAX_BITMAPS; i++ ) {
1864 if (Textures[i].bitmap_id == handle) {
1865 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1866 opengl_free_texture ( &Textures[i] );
1871 static void opengl_tcache_flush ()
1875 for( i=0; i<MAX_BITMAPS; i++ ) {
1876 opengl_free_texture ( &Textures[i] );
1878 if (GL_textures_in != 0) {
1879 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1883 GL_last_bitmap_id = -1;
1884 GL_last_section_x = -1;
1885 GL_last_section_y = -1;
1888 static void opengl_tcache_cleanup ()
1890 opengl_tcache_flush ();
1893 GL_textures_in_frame = 0;
1900 if( Texture_sections != NULL ){
1901 free(Texture_sections);
1902 Texture_sections = NULL;
1906 static void opengl_tcache_frame ()
1910 GL_last_bitmap_id = -1;
1911 GL_textures_in_frame = 0;
1917 for( i=0; i<MAX_BITMAPS; i++ ) {
1918 Textures[i].used_this_frame = 0;
1921 if(Textures[i].data_sections[0][0] != NULL){
1922 Assert(GL_texture_sections);
1923 if(GL_texture_sections){
1924 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1925 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1926 if(Textures[i].data_sections[idx][s_idx] != NULL){
1927 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1937 opengl_tcache_flush();
1942 static int opengl_free_texture ( tcache_slot_opengl *t )
1948 if ( t->bitmap_id > -1 ) {
1949 // if I, or any of my children have been used this frame, bail
1950 if(t->used_this_frame == GL_frame_count){
1953 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1954 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1955 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
1961 // ok, now we know its legal to free everything safely
1962 t->texture_mode = (gr_texture_source) -1;
1963 glDeleteTextures (1, &t->texture_handle);
1964 t->texture_handle = 0;
1966 if ( GL_last_bitmap_id == t->bitmap_id ) {
1967 GL_last_bitmap_id = -1;
1970 // if this guy has children, free them too, since the children
1971 // actually make up his size
1972 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1973 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1974 if(t->data_sections[idx][s_idx] != NULL){
1975 opengl_free_texture(t->data_sections[idx][s_idx]);
1981 t->used_this_frame = 0;
1982 GL_textures_in -= t->size;
1988 static void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1994 if((w_out == NULL) || (h_out == NULL)){
2002 for (i=0; i<16; i++ ) {
2003 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2008 for (j=0; j<16; j++ ) {
2009 if ( (tex_h > (1<<j)) && (tex_h <= (1<<(j+1))) ) {
2014 /* make i the smaller one */
2022 /* try to keep a 8:1 ratio */
2024 /* increase i, or decrease j? */
2029 for (i=0; i<16; i++ ) {
2030 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2036 for (i=0; i<16; i++ ) {
2037 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2043 if ( tex_w < GL_min_texture_width ) {
2044 tex_w = GL_min_texture_width;
2045 } else if ( tex_w > GL_max_texture_width ) {
2046 tex_w = GL_max_texture_width;
2049 if ( tex_h < GL_min_texture_height ) {
2050 tex_h = GL_min_texture_height;
2051 } else if ( tex_h > GL_max_texture_height ) {
2052 tex_h = GL_max_texture_height;
2055 if ( GL_square_textures) {
2057 // Make the both be equal to larger of the two
2058 new_size = max(tex_w, tex_h);
2063 // store the outgoing size
2068 // data == start of bitmap data
2069 // sx == x offset into bitmap
2070 // sy == y offset into bitmap
2071 // src_w == absolute width of section on source bitmap
2072 // src_h == absolute height of section on source bitmap
2073 // bmap_w == width of source bitmap
2074 // bmap_h == height of source bitmap
2075 // tex_w == width of final texture
2076 // tex_h == height of final texture
2077 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)
2087 if ( t->used_this_frame == GL_frame_count ) {
2088 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2093 if(!opengl_free_texture(t)){
2098 // get final texture size
2099 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2101 if ( (tex_w < 1) || (tex_h < 1) ) {
2102 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2106 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2107 t->u_scale = (float)bmap_w / (float)tex_w;
2108 t->v_scale = (float)bmap_h / (float)tex_h;
2109 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2110 t->u_scale = (float)src_w / (float)tex_w;
2111 t->v_scale = (float)src_h / (float)tex_h;
2118 glGenTextures (1, &t->texture_handle);
2121 if (t->texture_handle == 0) {
2122 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2126 GL_bound_texture = t;
2128 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2130 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2132 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2133 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2134 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2136 /* this should be set next anyway */
2137 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2138 // gr_opengl_set_texture_state(GL_current_texture_source);
2143 switch (bitmap_type) {
2145 case TCACHE_TYPE_AABITMAP:
2148 ubyte *bmp_data = ((ubyte*)data);
2154 for (i=0; i<16; i++) {
2155 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2158 for ( ; i<256; i++ ) {
2162 texmem = (ubyte *) malloc (tex_w*tex_h);
2165 for (i=0;i<tex_h;i++)
2167 for (j=0;j<tex_w;j++)
2169 if (i < bmap_h && j < bmap_w) {
2170 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2179 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2185 case TCACHE_TYPE_BITMAP_SECTION:
2188 ubyte *bmp_data = ((ubyte*)data);
2189 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2190 ubyte *texmemp = texmem;
2192 for (i=0;i<tex_h;i++)
2194 for (j=0;j<tex_w;j++)
2196 if (i < src_h && j < src_w) {
2197 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2198 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2206 size = tex_w*tex_h*2;
2208 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2209 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2217 ubyte *bmp_data = ((ubyte*)data);
2218 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2219 ubyte *texmemp = texmem;
2221 fix u, utmp, v, du, dv;
2225 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2226 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2228 for (j=0;j<tex_h;j++)
2231 for (i=0;i<tex_w;i++)
2233 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2234 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2240 size = tex_w*tex_h*2;
2242 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2243 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2251 t->bitmap_id = texture_handle;
2252 t->time_created = GL_frame_count;
2253 t->used_this_frame = 0;
2255 t->w = (ushort)tex_w;
2256 t->h = (ushort)tex_h;
2258 GL_textures_in_frame += t->size;
2260 GL_textures_in += t->size;
2266 static int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2270 int final_w, final_h;
2274 // setup texture/bitmap flags
2276 switch(bitmap_type){
2277 case TCACHE_TYPE_AABITMAP:
2278 flags |= BMP_AABITMAP;
2281 case TCACHE_TYPE_NORMAL:
2282 flags |= BMP_TEX_OTHER;
2283 case TCACHE_TYPE_XPARENT:
2284 flags |= BMP_TEX_XPARENT;
2286 case TCACHE_TYPE_NONDARKENING:
2288 flags |= BMP_TEX_NONDARK;
2292 // lock the bitmap into the proper format
2293 bmp = bm_lock(bitmap_handle, bpp, flags);
2294 if ( bmp == NULL ) {
2295 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2304 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2305 // max_w /= D3D_texture_divider;
2306 // max_h /= D3D_texture_divider;
2308 // Detail.debris_culling goes from 0 to 4.
2309 max_w /= (16 >> Detail.hardware_textures);
2310 max_h /= (16 >> Detail.hardware_textures);
2314 // get final texture size as it will be allocated as a DD surface
2315 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2317 // if this tcache slot has no bitmap
2318 if ( tslot->bitmap_id < 0) {
2321 // different bitmap altogether - determine if the new one can use the old one's slot
2322 else if (tslot->bitmap_id != bitmap_handle) {
2323 if((final_w == tslot->w) && (final_h == tslot->h)){
2325 //ml_printf("Reloading texture %d\n", bitmap_handle);
2332 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);
2334 // unlock the bitmap
2335 bm_unlock(bitmap_handle);
2340 static int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2344 int final_w, final_h;
2345 int section_x, section_y;
2348 // setup texture/bitmap flags
2349 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2350 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2351 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2353 flags = BMP_TEX_XPARENT;
2355 // lock the bitmap in the proper format
2356 bmp = bm_lock(bitmap_handle, 16, flags);
2357 if ( bmp == NULL ) {
2358 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2361 // determine the width and height of this section
2362 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2364 // get final texture size as it will be allocated as an opengl texture
2365 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2367 // if this tcache slot has no bitmap
2368 if ( tslot->bitmap_id < 0) {
2371 // different bitmap altogether - determine if the new one can use the old one's slot
2372 else if (tslot->bitmap_id != bitmap_handle) {
2373 if((final_w == tslot->w) && (final_h == tslot->h)){
2381 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);
2383 // unlock the bitmap
2384 bm_unlock(bitmap_handle);
2390 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2391 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)
2400 GL_last_bitmap_id = -1;
2404 if ( GL_last_detail != Detail.hardware_textures ) {
2405 GL_last_detail = Detail.hardware_textures;
2406 opengl_tcache_flush();
2413 int n = bm_get_cache_slot (bitmap_id, 1);
2414 tcache_slot_opengl *t = &Textures[n];
2416 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)) {
2417 t->used_this_frame = GL_frame_count;
2419 // mark all children as used
2420 if(GL_texture_sections){
2421 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2422 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2423 if(t->data_sections[idx][s_idx] != NULL){
2424 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2430 *u_scale = t->u_scale;
2431 *v_scale = t->v_scale;
2435 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2436 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2437 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2443 // if the texture sections haven't been created yet
2444 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2446 // lock the bitmap in the proper format
2447 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2448 bm_unlock(bitmap_id);
2450 // now lets do something for each texture
2452 for(idx=0; idx<bmp->sections.num_x; idx++){
2453 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2454 // hmm. i'd rather we didn't have to do it this way...
2455 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2459 // not used this frame
2460 t->data_sections[idx][s_idx]->used_this_frame = 0;
2464 // zero out pretty much everything in the parent struct since he's just the root
2465 t->bitmap_id = bitmap_id;
2466 t->texture_handle = 0;
2467 t->time_created = t->data_sections[sx][sy]->time_created;
2468 t->used_this_frame = 0;
2470 t->vram_texture = NULL;
2471 t->vram_texture_surface = NULL
2475 // argh. we failed to upload. free anything we can
2477 opengl_free_texture(t);
2479 // swap in the texture we want
2481 t = t->data_sections[sx][sy];
2484 // all other "normal" textures
2485 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2486 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2489 // everything went ok
2490 if(ret_val && (t->texture_handle) && !vram_full){
2491 *u_scale = t->u_scale;
2492 *v_scale = t->v_scale;
2494 GL_bound_texture = t;
2496 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2498 /* this should be set next anyway */
2499 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2500 // gr_opengl_set_texture_state(GL_current_texture_source);
2503 GL_last_bitmap_id = t->bitmap_id;
2504 GL_last_bitmap_type = bitmap_type;
2505 GL_last_section_x = sx;
2506 GL_last_section_y = sy;
2508 t->used_this_frame = GL_frame_count;
2512 GL_last_bitmap_id = -1;
2513 GL_last_bitmap_type = -1;
2515 GL_last_section_x = -1;
2516 GL_last_section_y = -1;
2518 GL_bound_texture = NULL;
2520 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2527 void gr_opengl_set_clear_color(int r, int g, int b)
2529 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2532 void gr_opengl_flash(int r, int g, int b)
2538 if ( r || g || b ) {
2539 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2541 float x1, x2, y1, y2;
2542 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2543 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2544 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2545 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2547 glColor4ub(r, g, b, 255);
2549 glVertex3f (x1, y2, -0.99);
2551 glVertex3f (x2, y2, -0.99);
2553 glVertex3f (x2, y1, -0.99);
2555 glVertex3f (x1, y1, -0.99);
2560 int gr_opengl_zbuffer_get()
2562 if ( !gr_global_zbuffering ) {
2563 return GR_ZBUFF_NONE;
2565 return gr_zbuffering_mode;
2568 int gr_opengl_zbuffer_set(int mode)
2570 int tmp = gr_zbuffering_mode;
2572 gr_zbuffering_mode = mode;
2574 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2582 void gr_opengl_zbuffer_clear(int mode)
2586 gr_zbuffering_mode = GR_ZBUFF_FULL;
2587 gr_global_zbuffering = 1;
2589 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2590 glClear ( GL_DEPTH_BUFFER_BIT );
2593 gr_zbuffering_mode = GR_ZBUFF_NONE;
2594 gr_global_zbuffering = 0;
2598 void gr_opengl_set_gamma(float gamma)
2601 Gr_gamma_int = int (Gr_gamma*10);
2603 // Create the Gamma lookup table
2605 for (i=0;i<256; i++) {
2606 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2609 } else if ( v < 0 ) {
2612 Gr_gamma_lookup[i] = v;
2615 // Flush any existing textures
2616 opengl_tcache_flush();
2619 void gr_opengl_fade_in(int instantaneous)
2624 void gr_opengl_fade_out(int instantaneous)
2629 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2632 glReadBuffer(GL_FRONT);
2634 glReadBuffer(GL_BACK);
2637 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2639 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2641 if (gr_screen.bits_per_pixel == 15) {
2642 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2643 } else if (gr_screen.bits_per_pixel == 32) {
2644 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2647 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2650 static int Gr_opengl_mouse_saved = 0;
2651 static int Gr_opengl_mouse_saved_x1 = 0;
2652 static int Gr_opengl_mouse_saved_y1 = 0;
2653 static int Gr_opengl_mouse_saved_x2 = 0;
2654 static int Gr_opengl_mouse_saved_y2 = 0;
2655 static int Gr_opengl_mouse_saved_w = 0;
2656 static int Gr_opengl_mouse_saved_h = 0;
2657 #define MAX_SAVE_SIZE (32*32)
2658 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2660 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2662 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2664 Gr_opengl_mouse_saved_x1 = x;
2665 Gr_opengl_mouse_saved_y1 = y;
2666 Gr_opengl_mouse_saved_x2 = x+w-1;
2667 Gr_opengl_mouse_saved_y2 = y+h-1;
2669 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2670 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2671 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2672 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2674 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2675 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2677 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2678 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2680 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2682 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2684 glReadBuffer(GL_BACK);
2685 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);
2687 Gr_opengl_mouse_saved = 1;
2690 int gr_opengl_save_screen()
2694 if ( Gr_saved_screen ) {
2695 mprintf(( "Screen alread saved!\n" ));
2699 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2700 if (!Gr_saved_screen) {
2701 mprintf(( "Couldn't get memory for saved screen!\n" ));
2705 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2706 if (!Gr_saved_screen_tmp) {
2707 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2711 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2713 glReadBuffer(GL_FRONT);
2714 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);
2718 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2719 dptr = (ubyte *)Gr_saved_screen;
2720 for (int j = 0; j < gr_screen.max_h; j++) {
2721 sptr -= gr_screen.max_w*2;
2722 memcpy(dptr, sptr, gr_screen.max_w*2);
2723 dptr += gr_screen.max_w*2;
2726 free(Gr_saved_screen_tmp);
2728 if (Gr_opengl_mouse_saved) {
2729 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2730 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2731 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2732 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2735 dptr -= gr_screen.max_w*2;
2739 // this leaks texture handles, and the opengl doesn't currently
2740 // perform some sort of garbage collection, so a hack was added
2741 // to bmpman to make it free textures when released
2742 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2744 return Gr_saved_screen_bitmap;
2747 void gr_opengl_restore_screen(int id)
2751 if ( !Gr_saved_screen ) {
2756 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2758 gr_set_bitmap(Gr_saved_screen_bitmap);
2762 void gr_opengl_free_screen(int id)
2764 bm_release(Gr_saved_screen_bitmap);
2766 if ( Gr_saved_screen ) {
2767 free( Gr_saved_screen );
2768 Gr_saved_screen = NULL;
2772 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2777 void gr_opengl_dump_frame_stop()
2782 void gr_opengl_dump_frame()
2787 uint gr_opengl_lock()
2794 void gr_opengl_unlock()
2798 void opengl_zbias(int bias)
2801 glEnable(GL_POLYGON_OFFSET_FILL);
2802 glPolygonOffset(0.0, -bias);
2804 glDisable(GL_POLYGON_OFFSET_FILL);
2808 extern char *Osreg_title;
2809 void gr_opengl_init()
2812 gr_opengl_cleanup();
2816 mprintf(( "Initializing opengl graphics device...\n" ));
2820 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2822 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2826 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2827 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2828 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2829 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2830 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2832 int flags = SDL_OPENGL;
2834 if (!Cmdline_window && ( (os_config_read_uint( NULL, "Fullscreen", 1 ) == 1) || Cmdline_fullscreen ))
2835 flags |= SDL_FULLSCREEN;
2837 // don't automatically grab key/mouse if cmdline says so, else do
2838 if(Cmdline_no_grab) {
2839 SDL_WM_GrabInput(SDL_GRAB_OFF);
2841 SDL_WM_GrabInput(SDL_GRAB_ON);
2844 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2846 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2851 SDL_WM_SetCaption (Osreg_title, "FS2");
2853 /* might as well put this here */
2854 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2856 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2858 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2860 glMatrixMode(GL_PROJECTION);
2862 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2863 glMatrixMode(GL_MODELVIEW);
2866 glShadeModel(GL_SMOOTH);
2867 glEnable(GL_DITHER);
2868 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2869 glHint(GL_FOG_HINT, GL_NICEST);
2871 glEnable(GL_DEPTH_TEST);
2874 glEnable(GL_TEXTURE_2D);
2876 glDepthRange(0.0, 1.0);
2878 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2879 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2881 D3D_32bit = 1; // grd3d.cpp
2882 extern int D3D_enabled;
2885 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2886 1 = use secondary color ext
2887 2 = use opengl linear fog
2889 D3D_fog_mode = 2; // grd3d.cpp
2893 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2900 gr_screen.bits_per_pixel = 15;
2901 gr_screen.bytes_per_pixel = 2;
2906 Gr_red.mask = 0x7C00;
2911 Gr_green.mask = 0x3E0;
2916 Gr_blue.mask = 0x1F;
2921 gr_screen.bits_per_pixel = 16;
2922 gr_screen.bytes_per_pixel = 2;
2927 Gr_red.mask = 0xF800;
2932 Gr_green.mask = 0x7E0;
2937 Gr_blue.mask = 0x1F;
2942 gr_screen.bits_per_pixel = 32;
2943 gr_screen.bytes_per_pixel = 4;
2948 Gr_red.mask = 0xff0000;
2953 Gr_green.mask = 0xff00;
2958 Gr_blue.mask = 0xff;
2963 Int3(); // Illegal bpp
2966 // DDOI - set these so no one else does!
2967 Gr_t_red.mask = Gr_red.mask;
2968 Gr_t_red.shift = Gr_red.shift;
2969 Gr_t_red.scale = Gr_red.scale;
2971 Gr_t_green.mask = Gr_green.mask;
2972 Gr_t_green.shift = Gr_green.shift;
2973 Gr_t_green.scale = Gr_green.scale;
2975 Gr_t_blue.mask = Gr_blue.mask;
2976 Gr_t_blue.shift = Gr_blue.shift;
2977 Gr_t_blue.scale = Gr_blue.scale;
2979 Gr_t_alpha.mask = 0x8000;
2980 Gr_t_alpha.scale = 255;
2981 Gr_t_alpha.shift = 15;
2983 Gr_ta_red.mask = 0x0f00;
2984 Gr_ta_red.shift = 8;
2985 Gr_ta_red.scale = 16;
2987 Gr_ta_green.mask = 0x00f0;
2988 Gr_ta_green.shift = 4;
2989 Gr_ta_green.scale = 16;
2991 Gr_ta_blue.mask = 0x000f;
2992 Gr_ta_blue.shift = 0;
2993 Gr_ta_blue.scale = 16;
2995 Gr_ta_alpha.mask = 0xf000;
2996 Gr_ta_alpha.shift = 12;
2997 Gr_ta_alpha.scale = 16;
3000 opengl_tcache_init (1);
3003 Gr_current_red = &Gr_red;
3004 Gr_current_blue = &Gr_blue;
3005 Gr_current_green = &Gr_green;
3006 Gr_current_alpha = &Gr_alpha;
3008 gr_screen.gf_flip = gr_opengl_flip;
3009 gr_screen.gf_flip_window = gr_opengl_flip_window;
3010 gr_screen.gf_set_clip = gr_opengl_set_clip;
3011 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3012 gr_screen.gf_set_font = grx_set_font;
3014 gr_screen.gf_set_color = gr_opengl_set_color;
3015 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3016 gr_screen.gf_create_shader = gr_opengl_create_shader;
3017 gr_screen.gf_set_shader = gr_opengl_set_shader;
3018 gr_screen.gf_clear = gr_opengl_clear;
3019 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3020 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3021 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3022 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3024 gr_screen.gf_rect = gr_opengl_rect;
3025 gr_screen.gf_shade = gr_opengl_shade;
3026 gr_screen.gf_string = gr_opengl_string;
3027 gr_screen.gf_circle = gr_opengl_circle;
3029 gr_screen.gf_line = gr_opengl_line;
3030 gr_screen.gf_aaline = gr_opengl_aaline;
3031 gr_screen.gf_pixel = gr_opengl_pixel;
3032 gr_screen.gf_scaler = gr_opengl_scaler;
3033 gr_screen.gf_tmapper = gr_opengl_tmapper;
3035 gr_screen.gf_gradient = gr_opengl_gradient;
3037 gr_screen.gf_set_palette = gr_opengl_set_palette;
3038 gr_screen.gf_get_color = gr_opengl_get_color;
3039 gr_screen.gf_init_color = gr_opengl_init_color;
3040 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3041 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3042 gr_screen.gf_print_screen = gr_opengl_print_screen;
3044 gr_screen.gf_fade_in = gr_opengl_fade_in;
3045 gr_screen.gf_fade_out = gr_opengl_fade_out;
3046 gr_screen.gf_flash = gr_opengl_flash;
3048 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3049 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3050 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3052 gr_screen.gf_save_screen = gr_opengl_save_screen;
3053 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3054 gr_screen.gf_free_screen = gr_opengl_free_screen;
3056 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3057 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3058 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3060 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3062 gr_screen.gf_lock = gr_opengl_lock;
3063 gr_screen.gf_unlock = gr_opengl_unlock;
3065 gr_screen.gf_fog_set = gr_opengl_fog_set;
3067 gr_screen.gf_get_region = gr_opengl_get_region;
3069 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3071 gr_screen.gf_set_cull = gr_opengl_set_cull;
3073 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3075 gr_screen.gf_filter_set = gr_opengl_filter_set;
3077 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3079 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;