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.55 2002/08/01 04:55:45 relnev
19 * experimenting with texture state
21 * Revision 1.54 2002/07/30 15:00:15 relnev
22 * not use luminance alpha by default
24 * Revision 1.53 2002/06/22 17:08:16 relnev
25 * more fixes to unused alpha code
27 * Revision 1.52 2002/06/21 23:59:14 relnev
28 * moved a gr_opengl_set_state to after the gr_tcache_set
30 * Revision 1.51 2002/06/19 04:52:45 relnev
31 * MacOS X updates (Ryan)
33 * Revision 1.50 2002/06/18 23:20:44 relnev
36 * adjusted line offsets.
38 * Revision 1.49 2002/06/17 23:11:39 relnev
39 * enable sdl key repeating.
43 * Revision 1.48 2002/06/09 04:41:17 relnev
44 * added copyright header
46 * Revision 1.47 2002/06/09 03:16:04 relnev
49 * removed unneeded asm, old sdl 2d setup.
51 * fixed crash caused by opengl_get_region.
53 * Revision 1.46 2002/06/05 04:03:32 relnev
54 * finished cfilesystem.
56 * removed some old code.
58 * fixed mouse save off-by-one.
62 * Revision 1.45 2002/06/03 09:25:37 relnev
63 * implement mouse cursor and screen save/restore
65 * Revision 1.44 2002/06/02 18:46:59 relnev
68 * Revision 1.43 2002/06/02 11:34:00 relnev
71 * Revision 1.42 2002/06/02 10:28:17 relnev
72 * fix texture handle leak
74 * Revision 1.41 2002/06/01 09:00:34 relnev
75 * silly debug memmanager
77 * Revision 1.40 2002/06/01 07:12:33 relnev
78 * a few NDEBUG updates.
80 * removed a few warnings.
82 * Revision 1.39 2002/06/01 05:33:15 relnev
83 * copied more code over.
85 * added scissor clipping.
87 * Revision 1.38 2002/06/01 03:35:27 relnev
90 * Revision 1.37 2002/06/01 03:32:00 relnev
91 * fix texture loading mistake.
93 * enable some d3d stuff for opengl also
95 * Revision 1.36 2002/05/31 23:25:03 relnev
98 * Revision 1.34 2002/05/31 22:15:22 relnev
101 * Revision 1.33 2002/05/31 22:04:55 relnev
102 * use d3d rect_internal
104 * Revision 1.32 2002/05/31 06:28:23 relnev
107 * Revision 1.31 2002/05/31 06:04:39 relnev
110 * Revision 1.30 2002/05/31 03:56:11 theoddone33
111 * Change tmapper polygon winding and enable culling
113 * Revision 1.29 2002/05/31 03:34:02 theoddone33
117 * Revision 1.28 2002/05/31 00:06:59 relnev
120 * Revision 1.27 2002/05/30 23:46:29 theoddone33
121 * some minor key changes (not necessarily fixes)
123 * Revision 1.26 2002/05/30 23:33:12 relnev
124 * implemented a few more functions.
126 * Revision 1.25 2002/05/30 23:01:16 relnev
127 * implement gr_opengl_set_state.
129 * Revision 1.24 2002/05/30 22:12:57 relnev
130 * finish default texture case
132 * Revision 1.23 2002/05/30 22:02:30 theoddone33
135 * Revision 1.22 2002/05/30 21:44:48 relnev
136 * implemented some missing texture stuff.
138 * enable bitmap polys for opengl.
140 * work around greenness in bitmaps.
142 * Revision 1.21 2002/05/30 17:29:30 theoddone33
143 * Fix some more stubs, change at least one polygon winding since culling is now
146 * Revision 1.20 2002/05/30 16:50:24 theoddone33
147 * Keyboard partially fixed
149 * Revision 1.19 2002/05/30 08:13:14 relnev
152 * Revision 1.18 2002/05/29 23:37:36 relnev
155 * Revision 1.17 2002/05/29 23:17:49 theoddone33
156 * Non working text code and fixed keys
158 * Revision 1.16 2002/05/29 19:45:13 theoddone33
159 * More changes on texture loading
161 * Revision 1.15 2002/05/29 19:06:48 theoddone33
162 * Enable string printing. Enable texture mapping
164 * Revision 1.14 2002/05/29 08:54:40 relnev
165 * "fixed" bitmap drawing.
167 * copied more d3d code over.
169 * Revision 1.13 2002/05/29 06:25:13 theoddone33
170 * Keyboard input, mouse tracking now work
172 * Revision 1.12 2002/05/29 04:52:45 relnev
175 * Revision 1.11 2002/05/29 04:29:56 relnev
176 * removed some unncessary stubbing, implemented opengl rect
178 * Revision 1.10 2002/05/29 04:13:27 theoddone33
181 * Revision 1.9 2002/05/29 03:35:51 relnev
184 * Revision 1.8 2002/05/29 03:30:05 relnev
185 * update opengl stubs
187 * Revision 1.7 2002/05/29 02:52:32 theoddone33
188 * Enable OpenGL renderer
190 * Revision 1.6 2002/05/28 04:56:51 theoddone33
191 * runs a little bit now
193 * Revision 1.5 2002/05/28 04:07:28 theoddone33
194 * New graphics stubbing arrangement
196 * Revision 1.4 2002/05/27 23:39:34 relnev
199 * Revision 1.3 2002/05/27 22:35:01 theoddone33
202 * Revision 1.2 2002/05/27 22:32:02 theoddone33
203 * throw all d3d stuff at opengl
205 * Revision 1.1.1.1 2002/05/03 03:28:09 root
209 * 10 7/14/99 9:42a Dave
210 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
213 * 9 7/09/99 9:51a Dave
214 * Added thick polyline code.
216 * 8 6/29/99 10:35a Dave
217 * Interface polygon bitmaps! Whee!
219 * 7 2/03/99 11:44a Dave
220 * Fixed d3d transparent textures.
222 * 6 1/24/99 11:37p Dave
223 * First full rev of beam weapons. Very customizable. Removed some bogus
224 * Int3()'s in low level net code.
226 * 5 12/18/98 1:13a Dave
227 * Rough 1024x768 support for Direct3D. Proper detection and usage through
230 * 4 12/06/98 2:36p Dave
231 * Drastically improved nebula fogging.
233 * 3 11/11/98 5:37p Dave
234 * Checkin for multiplayer testing.
236 * 2 10/07/98 10:53a Dave
239 * 1 10/07/98 10:49a Dave
241 * 14 5/20/98 9:46p John
242 * added code so the places in code that change half the palette don't
243 * have to clear the screen.
245 * 13 5/06/98 5:30p John
246 * Removed unused cfilearchiver. Removed/replaced some unused/little used
247 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
248 * DirectX header files and libs that fixed the Direct3D alpha blending
251 * 12 4/14/98 12:15p John
252 * Made 16-bpp movies work.
254 * 11 3/12/98 5:36p John
255 * Took out any unused shaders. Made shader code take rgbc instead of
256 * matrix and vector since noone used it like a matrix and it would have
257 * been impossible to do in hardware. Made Glide implement a basic
258 * shader for online help.
260 * 10 3/10/98 4:18p John
261 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
262 * & Glide have popups and print screen. Took out all >8bpp software
263 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
264 * support Fred. Made zbuffering key off of functions rather than one
267 * 9 12/02/97 4:00p John
268 * Added first rev of thruster glow, along with variable levels of
269 * translucency, which retquired some restructing of palman.
271 * 8 10/03/97 9:10a John
272 * added better antialiased line drawer
274 * 7 9/23/97 10:45a John
275 * made so you can tell bitblt code to rle a bitmap by passing flag to
278 * 6 9/09/97 11:01a Sandeep
279 * fixed warning level 4 bugs
281 * 5 7/10/97 2:06p John
282 * added code to specify alphablending type for bitmaps.
284 * 4 6/17/97 7:04p John
285 * added d3d support for gradients.
286 * fixed some color bugs by adding screen signatures instead of watching
287 * flags and palette changes.
289 * 3 6/12/97 2:50a Lawrance
290 * bm_unlock() now passed bitmap number, not pointer
292 * 2 6/11/97 1:12p John
293 * Started fixing all the text colors in the game.
295 * 1 5/12/97 12:14p John
302 #include <windowsx.h>
316 #include "floating.h"
318 #include "systemvars.h"
319 #include "grinternal.h"
320 #include "gropengl.h"
324 #include "osregistry.h"
326 static int Inited = 0;
328 typedef enum gr_texture_source {
330 TEXTURE_SOURCE_DECAL,
331 TEXTURE_SOURCE_NO_FILTERING,
334 typedef enum gr_alpha_blend {
335 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
336 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
337 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
338 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
339 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
342 typedef enum gr_zbuffer_type {
349 #define NEBULA_COLORS 20
351 volatile int GL_activate = 0;
352 volatile int GL_deactivate = 0;
354 static int GL_use_luminance_alpha;
356 static char *Gr_saved_screen = NULL;
357 static int Gr_saved_screen_bitmap;
360 // Throw in some dummy functions - DDOI
362 int D3D_32bit = 0; // grd3d.cpp
363 int D3D_fog_mode = -1; // grd3d.cpp
364 int D3D_inited = 0; // grd3d.cpp
365 int D3D_zbias = 1; // grd3d.cpp
366 int D3d_rendition_uvs = 0; // grd3d.cpp
373 void d3d_zbias (int a)
379 static void gr_opengl_set_texture_state(gr_texture_source ts);
381 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
382 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
384 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
386 gr_opengl_set_texture_state(ts);
388 if (ab != GL_current_alpha_blend) {
390 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
391 glBlendFunc(GL_ONE, GL_ZERO);
393 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
394 glBlendFunc(GL_ONE, GL_ONE);
396 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
397 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
399 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
400 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
402 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
403 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
409 GL_current_alpha_blend = ab;
412 if (zt != GL_current_zbuffer_type) {
414 case ZBUFFER_TYPE_NONE:
415 glDepthFunc(GL_ALWAYS);
416 glDepthMask(GL_FALSE);
418 case ZBUFFER_TYPE_READ:
419 glDepthFunc(GL_LESS);
420 glDepthMask(GL_FALSE);
422 case ZBUFFER_TYPE_WRITE:
423 glDepthFunc(GL_ALWAYS);
424 glDepthMask(GL_TRUE);
426 case ZBUFFER_TYPE_FULL:
427 glDepthFunc(GL_LESS);
428 glDepthMask(GL_TRUE);
434 GL_current_zbuffer_type = zt;
438 void gr_opengl_activate(int active)
444 make sure window is active and mouse grabbed
450 make sure mouse is not grabbed and window minimized
458 void opengl_tcache_flush ();
460 void gr_opengl_preload_init()
462 if (gr_screen.mode != GR_OPENGL) {
466 opengl_tcache_flush ();
469 int GL_should_preload = 0;
470 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
472 if ( gr_screen.mode != GR_OPENGL) {
476 if ( !GL_should_preload ) {
480 float u_scale, v_scale;
484 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
486 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
490 mprintf(("Texture upload failed!\n" ));
496 void gr_opengl_pixel(int x, int y)
501 void gr_opengl_clear()
503 glClearColor(gr_screen.current_clear_color.red / 255.0,
504 gr_screen.current_clear_color.green / 255.0,
505 gr_screen.current_clear_color.blue / 255.0, 1.0);
507 glClear ( GL_COLOR_BUFFER_BIT );
510 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
511 void opengl_tcache_frame ();
512 void gr_opengl_flip()
520 extern int Gr_opengl_mouse_saved;
521 Gr_opengl_mouse_saved = 0;
523 if ( mouse_is_visible() ) {
527 mouse_get_pos( &mx, &my );
529 gr_opengl_save_mouse_area(mx,my,32,32);
531 if ( Gr_cursor == -1 ) {
534 gr_set_bitmap(Gr_cursor);
540 GLenum error = glGetError();
543 error = glGetError();
545 if (error != GL_NO_ERROR) {
546 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
549 } while (error != GL_NO_ERROR);
552 SDL_GL_SwapBuffers ();
554 opengl_tcache_frame ();
556 int cnt = GL_activate;
559 opengl_tcache_flush();
560 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
566 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
570 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
575 void gr_opengl_set_clip(int x,int y,int w,int h)
577 // check for sanity of parameters
583 if (x >= gr_screen.max_w)
584 x = gr_screen.max_w - 1;
585 if (y >= gr_screen.max_h)
586 y = gr_screen.max_h - 1;
588 if (x + w > gr_screen.max_w)
589 w = gr_screen.max_w - x;
590 if (y + h > gr_screen.max_h)
591 h = gr_screen.max_h - y;
593 if (w > gr_screen.max_w)
595 if (h > gr_screen.max_h)
598 gr_screen.offset_x = x;
599 gr_screen.offset_y = y;
600 gr_screen.clip_left = 0;
601 gr_screen.clip_right = w-1;
602 gr_screen.clip_top = 0;
603 gr_screen.clip_bottom = h-1;
604 gr_screen.clip_width = w;
605 gr_screen.clip_height = h;
607 glEnable(GL_SCISSOR_TEST);
608 glScissor(x, gr_screen.max_h-y-h, w, h);
611 void gr_opengl_reset_clip()
613 gr_screen.offset_x = 0;
614 gr_screen.offset_y = 0;
615 gr_screen.clip_left = 0;
616 gr_screen.clip_top = 0;
617 gr_screen.clip_right = gr_screen.max_w - 1;
618 gr_screen.clip_bottom = gr_screen.max_h - 1;
619 gr_screen.clip_width = gr_screen.max_w;
620 gr_screen.clip_height = gr_screen.max_h;
622 glDisable(GL_SCISSOR_TEST);
623 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
626 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
628 gr_screen.current_alpha = alpha;
629 gr_screen.current_alphablend_mode = alphablend_mode;
630 gr_screen.current_bitblt_mode = bitblt_mode;
631 gr_screen.current_bitmap = bitmap_num;
633 gr_screen.current_bitmap_sx = sx;
634 gr_screen.current_bitmap_sy = sy;
637 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
639 shade->screen_sig = gr_screen.signature;
646 void gr_opengl_set_shader( shader * shade )
649 if (shade->screen_sig != gr_screen.signature) {
650 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
652 gr_screen.current_shader = *shade;
654 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
659 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
661 STUB_FUNCTION; /* who called me? */
665 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
667 STUB_FUNCTION; /* who called me? */
670 void gr_opengl_bitmap(int x, int y)
672 STUB_FUNCTION; /* who called me? */
675 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
679 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
681 saved_zbuf = gr_zbuffer_get();
683 // start the frame, no zbuffering, no culling
685 gr_zbuffer_set(GR_ZBUFF_NONE);
694 v[0].flags = PF_PROJECTED;
701 v[1].sx = i2fl(x + w);
706 v[1].flags = PF_PROJECTED;
713 v[2].sx = i2fl(x + w);
714 v[2].sy = i2fl(y + h);
718 v[2].flags = PF_PROJECTED;
726 v[3].sy = i2fl(y + h);
730 v[3].flags = PF_PROJECTED;
738 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
742 // restore zbuffer and culling
743 gr_zbuffer_set(saved_zbuf);
747 void gr_opengl_rect(int x,int y,int w,int h)
749 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);
752 void gr_opengl_shade(int x,int y,int w,int h)
759 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
760 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
761 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
762 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
763 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
764 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
765 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
766 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
768 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
771 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
776 if ( !gr_screen.current_color.is_alphacolor ) return;
778 float u_scale, v_scale;
780 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
781 // Couldn't set texture
782 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
786 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
788 float u0, u1, v0, v1;
789 float x1, x2, y1, y2;
792 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
794 u0 = u_scale*i2fl(sx)/i2fl(bw);
795 v0 = v_scale*i2fl(sy)/i2fl(bh);
797 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
798 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
800 x1 = i2fl(x+gr_screen.offset_x);
801 y1 = i2fl(y+gr_screen.offset_y);
802 x2 = i2fl(x+w+gr_screen.offset_x);
803 y2 = i2fl(y+h+gr_screen.offset_y);
805 if ( gr_screen.current_color.is_alphacolor ) {
806 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
808 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
812 glTexCoord2f (u0, v1);
813 glVertex3f (x1, y2, -0.99);
815 glTexCoord2f (u1, v1);
816 glVertex3f (x2, y2, -0.99);
818 glTexCoord2f (u1, v0);
819 glVertex3f (x2, y1, -0.99);
821 glTexCoord2f (u0, v0);
822 glVertex3f (x1, y1, -0.99);
826 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
833 int dx1=x, dx2=x+w-1;
834 int dy1=y, dy2=y+h-1;
837 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
842 if ( count > 1 ) Int3();
846 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
847 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
848 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
849 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
850 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
851 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
878 if ( w < 1 ) return; // clipped away!
879 if ( h < 1 ) return; // clipped away!
883 // Make sure clipping algorithm works
887 Assert( w == (dx2-dx1+1) );
888 Assert( h == (dy2-dy1+1) );
891 Assert( sx+w <= bw );
892 Assert( sy+h <= bh );
893 Assert( dx2 >= dx1 );
894 Assert( dy2 >= dy1 );
895 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
896 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
897 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
898 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
901 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
902 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
905 void gr_opengl_aabitmap(int x, int y)
909 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
910 int dx1=x, dx2=x+w-1;
911 int dy1=y, dy2=y+h-1;
914 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
915 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
916 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
917 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
918 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
919 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
921 if ( sx < 0 ) return;
922 if ( sy < 0 ) return;
923 if ( sx >= w ) return;
924 if ( sy >= h ) return;
926 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
927 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
930 void gr_opengl_string( int sx, int sy, char *s )
932 int width, spacing, letter;
935 if ( !Current_font ) {
939 gr_set_bitmap(Current_font->bitmap_id);
944 if (sx==0x8000) { //centered
945 x = get_centered_x(s);
957 y += Current_font->h;
958 if (sx==0x8000) { //centered
959 x = get_centered_x(s);
966 letter = get_char_width(s[0],s[1],&width,&spacing);
969 //not in font, draw as space
977 // Check if this character is totally clipped
978 if ( x + width < gr_screen.clip_left ) continue;
979 if ( y + Current_font->h < gr_screen.clip_top ) continue;
980 if ( x > gr_screen.clip_right ) continue;
981 if ( y > gr_screen.clip_bottom ) continue;
984 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
985 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
989 wc = width - xd; hc = Current_font->h - yd;
990 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
991 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
993 if ( wc < 1 ) continue;
994 if ( hc < 1 ) continue;
996 int u = Current_font->bm_u[letter];
997 int v = Current_font->bm_v[letter];
999 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1003 void gr_opengl_line(int x1,int y1,int x2,int y2)
1005 int clipped = 0, swapped=0;
1007 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1009 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);
1014 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1015 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1016 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1017 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1019 if ( x1 == x2 && y1 == y2 ) {
1020 glBegin (GL_POINTS);
1021 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1022 glVertex3f (sx1, sy1, -0.99f);
1034 } else if ( y1 == y2 ) {
1043 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1044 glVertex3f (sx2, sy2, -0.99f);
1045 glVertex3f (sx1, sy1, -0.99f);
1049 void gr_opengl_aaline(vertex *v1, vertex *v2)
1051 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1054 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1056 int clipped = 0, swapped=0;
1058 if ( !gr_screen.current_color.is_alphacolor ) {
1059 gr_line( x1, y1, x2, y2 );
1063 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);
1065 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1067 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1068 int ba = swapped ? gr_screen.current_color.alpha : 0;
1073 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1074 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1075 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1076 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1084 } else if ( y1 == y2 ) {
1093 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1094 glVertex3f (sx2, sy2, -0.99f);
1095 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1096 glVertex3f (sx1, sy1, -0.99f);
1100 void gr_opengl_circle( int xc, int yc, int d )
1110 if ( (xc+r) < gr_screen.clip_left ) return;
1111 if ( (xc-r) > gr_screen.clip_right ) return;
1112 if ( (yc+r) < gr_screen.clip_top ) return;
1113 if ( (yc-r) > gr_screen.clip_bottom ) return;
1116 // Draw the first octant
1117 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1118 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1123 // Draw the second octant
1124 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1125 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1133 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1134 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1139 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1143 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1144 if (f_float < 0.0f) {
1152 *a = (int)(f_float * 255.0);
1155 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1158 float u_scale = 1.0f, v_scale = 1.0f;
1160 // Make nebula use the texture mapper... this blends the colors better.
1161 if ( flags & TMAP_FLAG_NEBULA ){
1165 gr_texture_source texture_source = (gr_texture_source)-1;
1166 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1167 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1169 if ( gr_zbuffering ) {
1170 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1171 zbuffer_type = ZBUFFER_TYPE_READ;
1173 zbuffer_type = ZBUFFER_TYPE_FULL;
1176 zbuffer_type = ZBUFFER_TYPE_NONE;
1181 int tmap_type = TCACHE_TYPE_NORMAL;
1185 if ( flags & TMAP_FLAG_TEXTURED ) {
1188 r = gr_screen.current_color.red;
1189 g = gr_screen.current_color.green;
1190 b = gr_screen.current_color.blue;
1193 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1196 tmap_type = TCACHE_TYPE_NORMAL;
1197 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1199 // Blend with screen pixel using src*alpha+dst
1200 float factor = gr_screen.current_alpha;
1204 if ( factor <= 1.0f ) {
1205 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1206 r = (r*tmp_alpha)/255;
1207 g = (g*tmp_alpha)/255;
1208 b = (b*tmp_alpha)/255;
1211 tmap_type = TCACHE_TYPE_XPARENT;
1213 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1215 // Blend with screen pixel using src*alpha+dst
1216 float factor = gr_screen.current_alpha;
1218 if ( factor > 1.0f ) {
1221 alpha = fl2i(gr_screen.current_alpha*255.0f);
1225 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1226 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1228 alpha_blend = ALPHA_BLEND_NONE;
1233 if(flags & TMAP_FLAG_BITMAP_SECTION){
1234 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1237 texture_source = TEXTURE_SOURCE_NONE;
1239 if ( flags & TMAP_FLAG_TEXTURED ) {
1240 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 ))
1242 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1246 // use nonfiltered textures for bitmap sections
1247 if(flags & TMAP_FLAG_BITMAP_SECTION){
1248 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1250 texture_source = TEXTURE_SOURCE_DECAL;
1255 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1257 if ( flags & TMAP_FLAG_TEXTURED )
1263 if (flags & TMAP_FLAG_PIXEL_FOG) {
1269 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1271 vertex * va = verts[i];
1275 x = fl2i(va->sx*16.0f);
1276 y = fl2i(va->sy*16.0f);
1278 x += gr_screen.offset_x*16;
1279 y += gr_screen.offset_y*16;
1281 sx = i2fl(x) / 16.0f;
1282 sy = i2fl(y) / 16.0f;
1284 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1295 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1298 glBegin(GL_TRIANGLE_FAN);
1299 for (i = nv-1; i >= 0; i--) {
1300 vertex * va = verts[i];
1306 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1307 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1316 if ( flags & TMAP_FLAG_CORRECT ) {
1322 if (flags & TMAP_FLAG_ALPHA) {
1328 if (flags & TMAP_FLAG_NEBULA ) {
1329 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1330 r = gr_palette[pal*3+0];
1331 g = gr_palette[pal*3+1];
1332 b = gr_palette[pal*3+2];
1333 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1334 r = Gr_gamma_lookup[verts[i]->b];
1335 g = Gr_gamma_lookup[verts[i]->b];
1336 b = Gr_gamma_lookup[verts[i]->b];
1337 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1338 // Make 0.75 be 256.0f
1339 r = Gr_gamma_lookup[verts[i]->r];
1340 g = Gr_gamma_lookup[verts[i]->g];
1341 b = Gr_gamma_lookup[verts[i]->b];
1343 // use constant RGB values...
1345 glColor4ub (r,g,b,a);
1347 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1350 /* this is for GL_EXT_SECONDARY_COLOR */
1351 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1352 /* do separate color call here */
1358 x = fl2i(va->sx*16.0f);
1359 y = fl2i(va->sy*16.0f);
1361 x += gr_screen.offset_x*16;
1362 y += gr_screen.offset_y*16;
1364 sx = i2fl(x) / 16.0f;
1365 sy = i2fl(y) / 16.0f;
1367 if ( flags & TMAP_FLAG_TEXTURED ) {
1370 glTexCoord2f(tu, tv);
1373 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1378 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1380 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1383 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1385 void gr_opengl_scaler(vertex *va, vertex *vb )
1387 float x0, y0, x1, y1;
1388 float u0, v0, u1, v1;
1389 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1390 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1391 float xmin, xmax, ymin, ymax;
1392 int dx0, dy0, dx1, dy1;
1394 //============= CLIP IT =====================
1396 x0 = va->sx; y0 = va->sy;
1397 x1 = vb->sx; y1 = vb->sy;
1399 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1400 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1402 u0 = va->u; v0 = va->v;
1403 u1 = vb->u; v1 = vb->v;
1405 // Check for obviously offscreen bitmaps...
1406 if ( (y1<=y0) || (x1<=x0) ) return;
1407 if ( (x1<xmin ) || (x0>xmax) ) return;
1408 if ( (y1<ymin ) || (y0>ymax) ) return;
1410 clipped_u0 = u0; clipped_v0 = v0;
1411 clipped_u1 = u1; clipped_v1 = v1;
1413 clipped_x0 = x0; clipped_y0 = y0;
1414 clipped_x1 = x1; clipped_y1 = y1;
1416 // Clip the left, moving u0 right as necessary
1418 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1422 // Clip the right, moving u1 left as necessary
1424 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1428 // Clip the top, moving v0 down as necessary
1430 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1434 // Clip the bottom, moving v1 up as necessary
1436 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1440 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1441 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1443 if (dx1<=dx0) return;
1444 if (dy1<=dy0) return;
1446 //============= DRAW IT =====================
1460 v[1].sx = clipped_x1;
1461 v[1].sy = clipped_y0;
1464 v[1].u = clipped_u1;
1465 v[1].v = clipped_v0;
1468 v[2].sx = clipped_x1;
1469 v[2].sy = clipped_y1;
1472 v[2].u = clipped_u1;
1473 v[2].v = clipped_v1;
1476 v[3].sx = clipped_x0;
1477 v[3].sy = clipped_y1;
1480 v[3].u = clipped_u0;
1481 v[3].v = clipped_v1;
1483 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1486 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1490 void gr_opengl_get_color( int * r, int * g, int * b )
1492 if (r) *r = gr_screen.current_color.red;
1493 if (g) *g = gr_screen.current_color.green;
1494 if (b) *b = gr_screen.current_color.blue;
1497 void gr_opengl_init_color(color *c, int r, int g, int b)
1499 c->screen_sig = gr_screen.signature;
1500 c->red = (unsigned char)r;
1501 c->green = (unsigned char)g;
1502 c->blue = (unsigned char)b;
1504 c->ac_type = AC_TYPE_NONE;
1506 c->is_alphacolor = 0;
1510 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1512 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1513 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1514 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1515 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1517 gr_opengl_init_color( clr, r, g, b );
1519 clr->alpha = (unsigned char)alpha;
1520 clr->ac_type = (ubyte)type;
1521 clr->alphacolor = -1;
1522 clr->is_alphacolor = 1;
1525 void gr_opengl_set_color( int r, int g, int b )
1527 Assert((r >= 0) && (r < 256));
1528 Assert((g >= 0) && (g < 256));
1529 Assert((b >= 0) && (b < 256));
1531 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1534 void gr_opengl_set_color_fast(color *dst)
1536 if ( dst->screen_sig != gr_screen.signature ) {
1537 if ( dst->is_alphacolor ) {
1538 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1540 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1543 gr_screen.current_color = *dst;
1546 void gr_opengl_print_screen(char *filename)
1551 int gr_opengl_supports_res_ingame(int res)
1558 int gr_opengl_supports_res_interface(int res)
1565 void opengl_tcache_cleanup ();
1566 void gr_opengl_cleanup()
1568 if ( !Inited ) return;
1575 opengl_tcache_cleanup ();
1580 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1582 Assert((r >= 0) && (r < 256));
1583 Assert((g >= 0) && (g < 256));
1584 Assert((b >= 0) && (b < 256));
1586 if (fog_mode == GR_FOGMODE_NONE) {
1587 if (gr_screen.current_fog_mode != fog_mode) {
1590 gr_screen.current_fog_mode = fog_mode;
1595 if (gr_screen.current_fog_mode != fog_mode) {
1598 if (D3D_fog_mode == 2) {
1599 glFogi(GL_FOG_MODE, GL_LINEAR);
1602 gr_screen.current_fog_mode = fog_mode;
1605 if ( (gr_screen.current_fog_color.red != r) ||
1606 (gr_screen.current_fog_color.green != g) ||
1607 (gr_screen.current_fog_color.blue != b) ) {
1610 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1612 fc[0] = (float)r/255.0;
1613 fc[1] = (float)g/255.0;
1614 fc[2] = (float)b/255.0;
1617 glFogfv(GL_FOG_COLOR, fc);
1620 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1621 ((fog_near != gr_screen.fog_near) ||
1622 (fog_far != gr_screen.fog_far)) ) {
1623 gr_screen.fog_near = fog_near;
1624 gr_screen.fog_far = fog_far;
1626 if (D3D_fog_mode == 2) {
1627 glFogf(GL_FOG_START, fog_near);
1628 glFogf(GL_FOG_END, fog_far);
1633 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1638 void gr_opengl_set_cull(int cull)
1641 glEnable (GL_CULL_FACE);
1642 glFrontFace (GL_CCW);
1644 glDisable (GL_CULL_FACE);
1648 void gr_opengl_filter_set(int filter)
1653 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1656 gr_set_bitmap(bmap1);
1659 gr_set_bitmap(bmap2);
1665 typedef struct tcache_slot_opengl {
1666 GLuint texture_handle;
1667 float u_scale, v_scale;
1670 char used_this_frame;
1675 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1676 tcache_slot_opengl *parent;
1678 gr_texture_source texture_mode;
1679 } tcache_slot_opengl;
1681 static void *Texture_sections = NULL;
1682 static tcache_slot_opengl *Textures = NULL;
1684 static tcache_slot_opengl *GL_bound_texture;
1686 int GL_texture_sections = 0;
1687 int GL_texture_ram = 0;
1688 int GL_frame_count = 0;
1689 int GL_min_texture_width = 0;
1690 int GL_max_texture_width = 0;
1691 int GL_min_texture_height = 0;
1692 int GL_max_texture_height = 0;
1693 int GL_square_textures = 0;
1694 int GL_textures_in = 0;
1695 int GL_textures_in_frame = 0;
1696 int GL_last_bitmap_id = -1;
1697 int GL_last_detail = -1;
1698 int GL_last_bitmap_type = -1;
1699 int GL_last_section_x = -1;
1700 int GL_last_section_y = -1;
1704 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1706 static void gr_opengl_set_texture_state(gr_texture_source ts)
1708 if (ts == TEXTURE_SOURCE_NONE) {
1709 GL_bound_texture = NULL;
1711 glBindTexture(GL_TEXTURE_2D, 0);
1712 gr_tcache_set(-1, -1, NULL, NULL );
1713 } else if (GL_bound_texture &&
1714 GL_bound_texture->texture_mode != ts) {
1716 case TEXTURE_SOURCE_DECAL:
1717 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1718 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1720 case TEXTURE_SOURCE_NO_FILTERING:
1721 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1722 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1728 GL_bound_texture->texture_mode = ts;
1731 GL_current_texture_source = ts;
1734 void opengl_tcache_init (int use_sections)
1738 // DDOI - FIXME skipped a lot of stuff here
1739 GL_should_preload = 0;
1741 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1744 if ( tmp_pl == 0 ) {
1745 GL_should_preload = 0;
1746 } else if ( tmp_pl == 1 ) {
1747 GL_should_preload = 1;
1749 GL_should_preload = 1;
1754 GL_min_texture_width = 16;
1755 GL_min_texture_height = 16;
1756 GL_max_texture_width = 256;
1757 GL_max_texture_height = 256;
1759 GL_square_textures = 1;
1761 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1767 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1768 if(!Texture_sections){
1771 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1774 // Init the texture structures
1775 int section_count = 0;
1776 for( i=0; i<MAX_BITMAPS; i++ ) {
1778 Textures[i].vram_texture = NULL;
1779 Textures[i].vram_texture_surface = NULL;
1781 Textures[i].texture_handle = 0;
1783 Textures[i].bitmap_id = -1;
1784 Textures[i].size = 0;
1785 Textures[i].used_this_frame = 0;
1787 Textures[i].parent = NULL;
1789 // allocate sections
1791 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1792 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1793 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1794 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1796 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1797 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1799 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1800 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1801 Textures[i].data_sections[idx][s_idx]->size = 0;
1802 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1806 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1807 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1808 Textures[i].data_sections[idx][s_idx] = NULL;
1814 GL_texture_sections = use_sections;
1816 //GL_last_detail = Detail.hardware_textures;
1817 GL_last_bitmap_id = -1;
1818 GL_last_bitmap_type = -1;
1820 GL_last_section_x = -1;
1821 GL_last_section_y = -1;
1824 GL_textures_in_frame = 0;
1827 int opengl_free_texture (tcache_slot_opengl *t);
1829 void opengl_free_texture_with_handle(int handle)
1831 for(int i=0; i<MAX_BITMAPS; i++ ) {
1832 if (Textures[i].bitmap_id == handle) {
1833 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1834 opengl_free_texture ( &Textures[i] );
1839 void opengl_tcache_flush ()
1843 for( i=0; i<MAX_BITMAPS; i++ ) {
1844 opengl_free_texture ( &Textures[i] );
1846 if (GL_textures_in != 0) {
1847 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1851 GL_last_bitmap_id = -1;
1852 GL_last_section_x = -1;
1853 GL_last_section_y = -1;
1856 void opengl_tcache_cleanup ()
1858 opengl_tcache_flush ();
1861 GL_textures_in_frame = 0;
1868 if( Texture_sections != NULL ){
1869 free(Texture_sections);
1870 Texture_sections = NULL;
1874 void opengl_tcache_frame ()
1878 GL_last_bitmap_id = -1;
1879 GL_textures_in_frame = 0;
1884 for( i=0; i<MAX_BITMAPS; i++ ) {
1885 Textures[i].used_this_frame = 0;
1888 if(Textures[i].data_sections[0][0] != NULL){
1889 Assert(GL_texture_sections);
1890 if(GL_texture_sections){
1891 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1892 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1893 if(Textures[i].data_sections[idx][s_idx] != NULL){
1894 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1903 opengl_tcache_flush();
1908 int opengl_free_texture ( tcache_slot_opengl *t )
1914 if ( t->bitmap_id > -1 ) {
1915 // if I, or any of my children have been used this frame, bail
1916 if(t->used_this_frame){
1919 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1920 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1921 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
1927 // ok, now we know its legal to free everything safely
1928 t->texture_mode = (gr_texture_source) -1;
1929 glDeleteTextures (1, &t->texture_handle);
1930 t->texture_handle = 0;
1932 if ( GL_last_bitmap_id == t->bitmap_id ) {
1933 GL_last_bitmap_id = -1;
1936 // if this guy has children, free them too, since the children
1937 // actually make up his size
1938 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1939 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1940 if(t->data_sections[idx][s_idx] != NULL){
1941 opengl_free_texture(t->data_sections[idx][s_idx]);
1947 t->used_this_frame = 0;
1948 GL_textures_in -= t->size;
1954 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1959 if((w_out == NULL) || (h_out == NULL)){
1969 for (i=0; i<16; i++ ) {
1970 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
1976 for (i=0; i<16; i++ ) {
1977 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
1984 if ( tex_w < GL_min_texture_width ) {
1985 tex_w = GL_min_texture_width;
1986 } else if ( tex_w > GL_max_texture_width ) {
1987 tex_w = GL_max_texture_width;
1990 if ( tex_h < GL_min_texture_height ) {
1991 tex_h = GL_min_texture_height;
1992 } else if ( tex_h > GL_max_texture_height ) {
1993 tex_h = GL_max_texture_height;
1996 if ( GL_square_textures ) {
1998 // Make the both be equal to larger of the two
1999 new_size = max(tex_w, tex_h);
2004 // store the outgoing size
2009 // data == start of bitmap data
2010 // sx == x offset into bitmap
2011 // sy == y offset into bitmap
2012 // src_w == absolute width of section on source bitmap
2013 // src_h == absolute height of section on source bitmap
2014 // bmap_w == width of source bitmap
2015 // bmap_h == height of source bitmap
2016 // tex_w == width of final texture
2017 // tex_h == height of final texture
2018 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)
2027 if ( t->used_this_frame ) {
2028 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2033 if(!opengl_free_texture(t)){
2038 // get final texture size
2039 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2041 if ( (tex_w < 1) || (tex_h < 1) ) {
2042 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2046 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2047 t->u_scale = (float)bmap_w / (float)tex_w;
2048 t->v_scale = (float)bmap_h / (float)tex_h;
2049 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2050 t->u_scale = (float)src_w / (float)tex_w;
2051 t->v_scale = (float)src_h / (float)tex_h;
2058 glGenTextures (1, &t->texture_handle);
2061 if (t->texture_handle == 0) {
2062 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2066 GL_bound_texture = t;
2068 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2070 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2072 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2073 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2074 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2076 if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2077 gr_opengl_set_texture_state(GL_current_texture_source);
2080 switch (bitmap_type) {
2082 case TCACHE_TYPE_AABITMAP:
2085 ubyte *bmp_data = ((ubyte*)data);
2091 for (i=0; i<16; i++) {
2092 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2095 for ( ; i<256; i++ ) {
2099 if (GL_use_luminance_alpha) {
2100 texmem = (ubyte *) malloc (tex_w*tex_h*2);
2103 for (i=0;i<tex_h;i++)
2105 for (j=0;j<tex_w;j++)
2107 if (i < bmap_h && j < bmap_w) {
2109 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2117 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2121 texmem = (ubyte *) malloc (tex_w*tex_h*4);
2124 for (i=0;i<tex_h;i++)
2126 for (j=0;j<tex_w;j++)
2128 if (i < bmap_h && j < bmap_w) {
2129 *texmemp++ = 0xff; /* R */
2130 *texmemp++ = 0xff; /* G */
2131 *texmemp++ = 0xff; /* B */
2132 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2142 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texmem);
2148 case TCACHE_TYPE_BITMAP_SECTION:
2151 ubyte *bmp_data = ((ubyte*)data);
2152 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2153 ubyte *texmemp = texmem;
2155 for (i=0;i<tex_h;i++)
2157 for (j=0;j<tex_w;j++)
2159 if (i < src_h && j < src_w) {
2160 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2161 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2168 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2169 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2177 ubyte *bmp_data = ((ubyte*)data);
2178 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2179 ubyte *texmemp = texmem;
2181 fix u, utmp, v, du, dv;
2185 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2186 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2188 for (j=0;j<tex_h;j++)
2191 for (i=0;i<tex_w;i++)
2193 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2194 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2200 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2201 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2209 t->bitmap_id = texture_handle;
2210 t->time_created = GL_frame_count;
2211 t->used_this_frame = 0;
2212 t->size = tex_w * tex_h * 2;
2213 t->w = (ushort)tex_w;
2214 t->h = (ushort)tex_h;
2215 GL_textures_in_frame += t->size;
2217 GL_textures_in += t->size;
2223 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2227 int final_w, final_h;
2231 // setup texture/bitmap flags
2233 switch(bitmap_type){
2234 case TCACHE_TYPE_AABITMAP:
2235 flags |= BMP_AABITMAP;
2238 case TCACHE_TYPE_NORMAL:
2239 flags |= BMP_TEX_OTHER;
2240 case TCACHE_TYPE_XPARENT:
2241 flags |= BMP_TEX_XPARENT;
2243 case TCACHE_TYPE_NONDARKENING:
2245 flags |= BMP_TEX_NONDARK;
2249 // lock the bitmap into the proper format
2250 bmp = bm_lock(bitmap_handle, bpp, flags);
2251 if ( bmp == NULL ) {
2252 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2261 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2262 // max_w /= D3D_texture_divider;
2263 // max_h /= D3D_texture_divider;
2265 // Detail.debris_culling goes from 0 to 4.
2266 max_w /= (16 >> Detail.hardware_textures);
2267 max_h /= (16 >> Detail.hardware_textures);
2271 // get final texture size as it will be allocated as a DD surface
2272 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2274 // if this tcache slot has no bitmap
2275 if ( tslot->bitmap_id < 0) {
2278 // different bitmap altogether - determine if the new one can use the old one's slot
2279 else if (tslot->bitmap_id != bitmap_handle) {
2280 if((final_w == tslot->w) && (final_h == tslot->h)){
2282 //ml_printf("Reloading texture %d\n", bitmap_handle);
2289 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);
2291 // unlock the bitmap
2292 bm_unlock(bitmap_handle);
2297 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2301 int final_w, final_h;
2302 int section_x, section_y;
2305 // setup texture/bitmap flags
2306 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2307 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2308 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2310 flags = BMP_TEX_XPARENT;
2312 // lock the bitmap in the proper format
2313 bmp = bm_lock(bitmap_handle, 16, flags);
2314 if ( bmp == NULL ) {
2315 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2318 // determine the width and height of this section
2319 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2321 // get final texture size as it will be allocated as an opengl texture
2322 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2324 // if this tcache slot has no bitmap
2325 if ( tslot->bitmap_id < 0) {
2328 // different bitmap altogether - determine if the new one can use the old one's slot
2329 else if (tslot->bitmap_id != bitmap_handle) {
2330 if((final_w == tslot->w) && (final_h == tslot->h)){
2338 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);
2340 // unlock the bitmap
2341 bm_unlock(bitmap_handle);
2347 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2348 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)
2357 GL_last_bitmap_id = -1;
2361 if ( GL_last_detail != Detail.hardware_textures ) {
2362 GL_last_detail = Detail.hardware_textures;
2363 opengl_tcache_flush();
2370 int n = bm_get_cache_slot (bitmap_id, 1);
2371 tcache_slot_opengl *t = &Textures[n];
2373 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)) {
2374 t->used_this_frame++;
2376 // mark all children as used
2377 if(GL_texture_sections){
2378 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2379 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2380 if(t->data_sections[idx][s_idx] != NULL){
2381 t->data_sections[idx][s_idx]->used_this_frame++;
2387 *u_scale = t->u_scale;
2388 *v_scale = t->v_scale;
2392 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2393 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2394 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2400 // if the texture sections haven't been created yet
2401 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2403 // lock the bitmap in the proper format
2404 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2405 bm_unlock(bitmap_id);
2407 // now lets do something for each texture
2409 for(idx=0; idx<bmp->sections.num_x; idx++){
2410 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2411 // hmm. i'd rather we didn't have to do it this way...
2412 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2416 // not used this frame
2417 t->data_sections[idx][s_idx]->used_this_frame = 0;
2421 // zero out pretty much everything in the parent struct since he's just the root
2422 t->bitmap_id = bitmap_id;
2423 t->texture_handle = 0;
2424 t->time_created = t->data_sections[sx][sy]->time_created;
2425 t->used_this_frame = 0;
2427 t->vram_texture = NULL;
2428 t->vram_texture_surface = NULL
2432 // argh. we failed to upload. free anything we can
2434 opengl_free_texture(t);
2436 // swap in the texture we want
2438 t = t->data_sections[sx][sy];
2441 // all other "normal" textures
2442 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2443 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2446 // everything went ok
2447 if(ret_val && (t->texture_handle) && !vram_full){
2448 *u_scale = t->u_scale;
2449 *v_scale = t->v_scale;
2451 GL_bound_texture = t;
2453 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2455 if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2456 gr_opengl_set_texture_state(GL_current_texture_source);
2459 GL_last_bitmap_id = t->bitmap_id;
2460 GL_last_bitmap_type = bitmap_type;
2461 GL_last_section_x = sx;
2462 GL_last_section_y = sy;
2464 t->used_this_frame++;
2468 GL_last_bitmap_id = -1;
2469 GL_last_bitmap_type = -1;
2471 GL_last_section_x = -1;
2472 GL_last_section_y = -1;
2474 GL_bound_texture = NULL;
2476 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2483 void gr_opengl_set_clear_color(int r, int g, int b)
2485 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2488 void gr_opengl_flash(int r, int g, int b)
2494 if ( r || g || b ) {
2495 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2497 float x1, x2, y1, y2;
2498 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2499 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2500 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2501 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2503 glColor4ub(r, g, b, 255);
2505 glVertex3f (x1, y2, -0.99);
2507 glVertex3f (x2, y2, -0.99);
2509 glVertex3f (x2, y1, -0.99);
2511 glVertex3f (x1, y1, -0.99);
2516 int gr_opengl_zbuffer_get()
2518 if ( !gr_global_zbuffering ) {
2519 return GR_ZBUFF_NONE;
2521 return gr_zbuffering_mode;
2524 int gr_opengl_zbuffer_set(int mode)
2526 int tmp = gr_zbuffering_mode;
2528 gr_zbuffering_mode = mode;
2530 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2538 void gr_opengl_zbuffer_clear(int mode)
2542 gr_zbuffering_mode = GR_ZBUFF_FULL;
2543 gr_global_zbuffering = 1;
2545 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2546 glClear ( GL_DEPTH_BUFFER_BIT );
2549 gr_zbuffering_mode = GR_ZBUFF_NONE;
2550 gr_global_zbuffering = 0;
2554 void gr_opengl_set_gamma(float gamma)
2557 Gr_gamma_int = int (Gr_gamma*10);
2559 // Create the Gamma lookup table
2561 for (i=0;i<256; i++) {
2562 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2565 } else if ( v < 0 ) {
2568 Gr_gamma_lookup[i] = v;
2571 // Flush any existing textures
2572 opengl_tcache_flush();
2575 void gr_opengl_fade_in(int instantaneous)
2580 void gr_opengl_fade_out(int instantaneous)
2585 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2588 glReadBuffer(GL_FRONT);
2590 glReadBuffer(GL_BACK);
2593 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2595 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2597 if (gr_screen.bits_per_pixel == 15) {
2598 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2599 } else if (gr_screen.bits_per_pixel == 32) {
2600 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2603 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2606 static int Gr_opengl_mouse_saved = 0;
2607 static int Gr_opengl_mouse_saved_x1 = 0;
2608 static int Gr_opengl_mouse_saved_y1 = 0;
2609 static int Gr_opengl_mouse_saved_x2 = 0;
2610 static int Gr_opengl_mouse_saved_y2 = 0;
2611 static int Gr_opengl_mouse_saved_w = 0;
2612 static int Gr_opengl_mouse_saved_h = 0;
2613 #define MAX_SAVE_SIZE (32*32)
2614 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2616 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2618 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2620 Gr_opengl_mouse_saved_x1 = x;
2621 Gr_opengl_mouse_saved_y1 = y;
2622 Gr_opengl_mouse_saved_x2 = x+w-1;
2623 Gr_opengl_mouse_saved_y2 = y+h-1;
2625 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2626 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2627 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2628 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2630 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2631 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2633 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2634 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2636 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2638 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2640 glReadBuffer(GL_BACK);
2641 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);
2643 Gr_opengl_mouse_saved = 1;
2646 int gr_opengl_save_screen()
2650 if ( Gr_saved_screen ) {
2651 mprintf(( "Screen alread saved!\n" ));
2655 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2656 if (!Gr_saved_screen) {
2657 mprintf(( "Couldn't get memory for saved screen!\n" ));
2661 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2662 if (!Gr_saved_screen_tmp) {
2663 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2667 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2669 glReadBuffer(GL_FRONT);
2670 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);
2674 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2675 dptr = (ubyte *)Gr_saved_screen;
2676 for (int j = 0; j < gr_screen.max_h; j++) {
2677 sptr -= gr_screen.max_w*2;
2678 memcpy(dptr, sptr, gr_screen.max_w*2);
2679 dptr += gr_screen.max_w*2;
2682 free(Gr_saved_screen_tmp);
2684 if (Gr_opengl_mouse_saved) {
2685 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2686 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2687 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2688 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2691 dptr -= gr_screen.max_w*2;
2695 // this leaks texture handles, and the opengl doesn't currently
2696 // perform some sort of garbage collection, so a hack was added
2697 // to bmpman to make it free textures when released
2698 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2700 return Gr_saved_screen_bitmap;
2703 void gr_opengl_restore_screen(int id)
2707 if ( !Gr_saved_screen ) {
2712 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2714 gr_set_bitmap(Gr_saved_screen_bitmap);
2718 void gr_opengl_free_screen(int id)
2720 bm_release(Gr_saved_screen_bitmap);
2722 if ( Gr_saved_screen ) {
2723 free( Gr_saved_screen );
2724 Gr_saved_screen = NULL;
2728 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2733 void gr_opengl_dump_frame_stop()
2738 void gr_opengl_dump_frame()
2743 uint gr_opengl_lock()
2750 void gr_opengl_unlock()
2754 void opengl_zbias(int bias)
2757 glEnable(GL_POLYGON_OFFSET_FILL);
2758 glPolygonOffset(0.0, -bias);
2760 glDisable(GL_POLYGON_OFFSET_FILL);
2764 extern char *Osreg_title;
2765 void gr_opengl_init()
2768 gr_opengl_cleanup();
2772 mprintf(( "Initializing opengl graphics device...\n" ));
2776 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2778 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2784 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2785 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2786 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2787 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2788 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2790 int flags = SDL_OPENGL;
2792 if (os_config_read_uint( NULL, "Fullscreen", 0 ) == 1)
2793 flags |= SDL_FULLSCREEN;
2795 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2797 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2802 SDL_WM_SetCaption (Osreg_title, "FS2");
2804 /* might as well put this here */
2805 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2807 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2809 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2811 glMatrixMode(GL_PROJECTION);
2813 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2814 glMatrixMode(GL_MODELVIEW);
2817 glShadeModel(GL_SMOOTH);
2818 glEnable(GL_DITHER);
2819 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2820 glHint(GL_FOG_HINT, GL_NICEST);
2822 glEnable(GL_DEPTH_TEST);
2825 glEnable(GL_TEXTURE_2D);
2827 glDepthRange(0.0, 1.0);
2829 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2830 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2832 D3D_32bit = 1; // grd3d.cpp
2833 extern int D3D_enabled;
2836 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2837 1 = use secondary color ext
2838 2 = use opengl linear fog
2840 D3D_fog_mode = 2; // grd3d.cpp
2844 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2851 gr_screen.bits_per_pixel = 15;
2852 gr_screen.bytes_per_pixel = 2;
2857 Gr_red.mask = 0x7C00;
2862 Gr_green.mask = 0x3E0;
2867 Gr_blue.mask = 0x1F;
2872 gr_screen.bits_per_pixel = 16;
2873 gr_screen.bytes_per_pixel = 2;
2878 Gr_red.mask = 0xF800;
2883 Gr_green.mask = 0x7E0;
2888 Gr_blue.mask = 0x1F;
2893 gr_screen.bits_per_pixel = 32;
2894 gr_screen.bytes_per_pixel = 4;
2899 Gr_red.mask = 0xff0000;
2904 Gr_green.mask = 0xff00;
2909 Gr_blue.mask = 0xff;
2914 Int3(); // Illegal bpp
2917 // DDOI - set these so no one else does!
2918 Gr_ta_red.mask = 0x0f00;
2919 Gr_ta_red.shift = 8;
2920 Gr_ta_red.scale = 16;
2922 Gr_ta_green.mask = 0x00f0;
2923 Gr_ta_green.shift = 4;
2924 Gr_ta_green.scale = 16;
2926 Gr_ta_blue.mask = 0x000f;
2927 Gr_ta_blue.shift = 0;
2928 Gr_ta_blue.scale = 16;
2930 Gr_ta_alpha.mask = 0xf000;
2931 Gr_ta_alpha.shift = 12;
2932 Gr_ta_alpha.scale = 16;
2935 opengl_tcache_init (1);
2938 Gr_current_red = &Gr_red;
2939 Gr_current_blue = &Gr_blue;
2940 Gr_current_green = &Gr_green;
2941 Gr_current_alpha = &Gr_alpha;
2943 gr_screen.gf_flip = gr_opengl_flip;
2944 gr_screen.gf_flip_window = gr_opengl_flip_window;
2945 gr_screen.gf_set_clip = gr_opengl_set_clip;
2946 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2947 gr_screen.gf_set_font = grx_set_font;
2949 gr_screen.gf_set_color = gr_opengl_set_color;
2950 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2951 gr_screen.gf_create_shader = gr_opengl_create_shader;
2952 gr_screen.gf_set_shader = gr_opengl_set_shader;
2953 gr_screen.gf_clear = gr_opengl_clear;
2954 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2955 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2956 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2957 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2959 gr_screen.gf_rect = gr_opengl_rect;
2960 gr_screen.gf_shade = gr_opengl_shade;
2961 gr_screen.gf_string = gr_opengl_string;
2962 gr_screen.gf_circle = gr_opengl_circle;
2964 gr_screen.gf_line = gr_opengl_line;
2965 gr_screen.gf_aaline = gr_opengl_aaline;
2966 gr_screen.gf_pixel = gr_opengl_pixel;
2967 gr_screen.gf_scaler = gr_opengl_scaler;
2968 gr_screen.gf_tmapper = gr_opengl_tmapper;
2970 gr_screen.gf_gradient = gr_opengl_gradient;
2972 gr_screen.gf_set_palette = gr_opengl_set_palette;
2973 gr_screen.gf_get_color = gr_opengl_get_color;
2974 gr_screen.gf_init_color = gr_opengl_init_color;
2975 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
2976 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
2977 gr_screen.gf_print_screen = gr_opengl_print_screen;
2979 gr_screen.gf_fade_in = gr_opengl_fade_in;
2980 gr_screen.gf_fade_out = gr_opengl_fade_out;
2981 gr_screen.gf_flash = gr_opengl_flash;
2983 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
2984 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
2985 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
2987 gr_screen.gf_save_screen = gr_opengl_save_screen;
2988 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
2989 gr_screen.gf_free_screen = gr_opengl_free_screen;
2991 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
2992 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
2993 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
2995 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
2997 gr_screen.gf_lock = gr_opengl_lock;
2998 gr_screen.gf_unlock = gr_opengl_unlock;
3000 gr_screen.gf_fog_set = gr_opengl_fog_set;
3002 gr_screen.gf_get_region = gr_opengl_get_region;
3004 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3006 gr_screen.gf_set_cull = gr_opengl_set_cull;
3008 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3010 gr_screen.gf_filter_set = gr_opengl_filter_set;
3012 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3014 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;