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.66 2003/06/22 12:52:34 taylor
19 * more texture size fixin
21 * Revision 1.65 2003/06/19 11:52:47 taylor
22 * fix texture size issue with lower detail settings
24 * Revision 1.64 2003/05/28 06:02:04 taylor
25 * fix transparency in green weapon blobs
27 * Revision 1.63 2003/05/09 05:05:52 taylor
28 * improve gr_(de)activate, mouse grab, default fullscreen
30 * Revision 1.62 2003/05/04 04:56:53 taylor
31 * move SDL_Quit to os_deinit to fix fonttool segfault
33 * Revision 1.61 2003/02/02 21:13:27 relnev
34 * minor updates (made functions static, tried to improve texture sizes)
36 * Revision 1.60 2003/02/02 20:20:03 relnev
37 * add large texture support (Taylor Richards)
39 * Revision 1.59 2002/09/04 01:38:09 relnev
42 * Revision 1.57 2002/08/31 01:39:13 theoddone33
43 * Speed up the renderer a tad
45 * Revision 1.56 2002/08/01 05:03:11 relnev
48 * Revision 1.55 2002/08/01 04:55:45 relnev
49 * experimenting with texture state
51 * Revision 1.54 2002/07/30 15:00:15 relnev
52 * not use luminance alpha by default
54 * Revision 1.53 2002/06/22 17:08:16 relnev
55 * more fixes to unused alpha code
57 * Revision 1.52 2002/06/21 23:59:14 relnev
58 * moved a gr_opengl_set_state to after the gr_tcache_set
60 * Revision 1.51 2002/06/19 04:52:45 relnev
61 * MacOS X updates (Ryan)
63 * Revision 1.50 2002/06/18 23:20:44 relnev
66 * adjusted line offsets.
68 * Revision 1.49 2002/06/17 23:11:39 relnev
69 * enable sdl key repeating.
73 * Revision 1.48 2002/06/09 04:41:17 relnev
74 * added copyright header
76 * Revision 1.47 2002/06/09 03:16:04 relnev
79 * removed unneeded asm, old sdl 2d setup.
81 * fixed crash caused by opengl_get_region.
83 * Revision 1.46 2002/06/05 04:03:32 relnev
84 * finished cfilesystem.
86 * removed some old code.
88 * fixed mouse save off-by-one.
92 * Revision 1.45 2002/06/03 09:25:37 relnev
93 * implement mouse cursor and screen save/restore
95 * Revision 1.44 2002/06/02 18:46:59 relnev
98 * Revision 1.43 2002/06/02 11:34:00 relnev
101 * Revision 1.42 2002/06/02 10:28:17 relnev
102 * fix texture handle leak
104 * Revision 1.41 2002/06/01 09:00:34 relnev
105 * silly debug memmanager
107 * Revision 1.40 2002/06/01 07:12:33 relnev
108 * a few NDEBUG updates.
110 * removed a few warnings.
112 * Revision 1.39 2002/06/01 05:33:15 relnev
113 * copied more code over.
115 * added scissor clipping.
117 * Revision 1.38 2002/06/01 03:35:27 relnev
120 * Revision 1.37 2002/06/01 03:32:00 relnev
121 * fix texture loading mistake.
123 * enable some d3d stuff for opengl also
125 * Revision 1.36 2002/05/31 23:25:03 relnev
128 * Revision 1.34 2002/05/31 22:15:22 relnev
131 * Revision 1.33 2002/05/31 22:04:55 relnev
132 * use d3d rect_internal
134 * Revision 1.32 2002/05/31 06:28:23 relnev
137 * Revision 1.31 2002/05/31 06:04:39 relnev
140 * Revision 1.30 2002/05/31 03:56:11 theoddone33
141 * Change tmapper polygon winding and enable culling
143 * Revision 1.29 2002/05/31 03:34:02 theoddone33
147 * Revision 1.28 2002/05/31 00:06:59 relnev
150 * Revision 1.27 2002/05/30 23:46:29 theoddone33
151 * some minor key changes (not necessarily fixes)
153 * Revision 1.26 2002/05/30 23:33:12 relnev
154 * implemented a few more functions.
156 * Revision 1.25 2002/05/30 23:01:16 relnev
157 * implement gr_opengl_set_state.
159 * Revision 1.24 2002/05/30 22:12:57 relnev
160 * finish default texture case
162 * Revision 1.23 2002/05/30 22:02:30 theoddone33
165 * Revision 1.22 2002/05/30 21:44:48 relnev
166 * implemented some missing texture stuff.
168 * enable bitmap polys for opengl.
170 * work around greenness in bitmaps.
172 * Revision 1.21 2002/05/30 17:29:30 theoddone33
173 * Fix some more stubs, change at least one polygon winding since culling is now
176 * Revision 1.20 2002/05/30 16:50:24 theoddone33
177 * Keyboard partially fixed
179 * Revision 1.19 2002/05/30 08:13:14 relnev
182 * Revision 1.18 2002/05/29 23:37:36 relnev
185 * Revision 1.17 2002/05/29 23:17:49 theoddone33
186 * Non working text code and fixed keys
188 * Revision 1.16 2002/05/29 19:45:13 theoddone33
189 * More changes on texture loading
191 * Revision 1.15 2002/05/29 19:06:48 theoddone33
192 * Enable string printing. Enable texture mapping
194 * Revision 1.14 2002/05/29 08:54:40 relnev
195 * "fixed" bitmap drawing.
197 * copied more d3d code over.
199 * Revision 1.13 2002/05/29 06:25:13 theoddone33
200 * Keyboard input, mouse tracking now work
202 * Revision 1.12 2002/05/29 04:52:45 relnev
205 * Revision 1.11 2002/05/29 04:29:56 relnev
206 * removed some unncessary stubbing, implemented opengl rect
208 * Revision 1.10 2002/05/29 04:13:27 theoddone33
211 * Revision 1.9 2002/05/29 03:35:51 relnev
214 * Revision 1.8 2002/05/29 03:30:05 relnev
215 * update opengl stubs
217 * Revision 1.7 2002/05/29 02:52:32 theoddone33
218 * Enable OpenGL renderer
220 * Revision 1.6 2002/05/28 04:56:51 theoddone33
221 * runs a little bit now
223 * Revision 1.5 2002/05/28 04:07:28 theoddone33
224 * New graphics stubbing arrangement
226 * Revision 1.4 2002/05/27 23:39:34 relnev
229 * Revision 1.3 2002/05/27 22:35:01 theoddone33
232 * Revision 1.2 2002/05/27 22:32:02 theoddone33
233 * throw all d3d stuff at opengl
235 * Revision 1.1.1.1 2002/05/03 03:28:09 root
239 * 10 7/14/99 9:42a Dave
240 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
243 * 9 7/09/99 9:51a Dave
244 * Added thick polyline code.
246 * 8 6/29/99 10:35a Dave
247 * Interface polygon bitmaps! Whee!
249 * 7 2/03/99 11:44a Dave
250 * Fixed d3d transparent textures.
252 * 6 1/24/99 11:37p Dave
253 * First full rev of beam weapons. Very customizable. Removed some bogus
254 * Int3()'s in low level net code.
256 * 5 12/18/98 1:13a Dave
257 * Rough 1024x768 support for Direct3D. Proper detection and usage through
260 * 4 12/06/98 2:36p Dave
261 * Drastically improved nebula fogging.
263 * 3 11/11/98 5:37p Dave
264 * Checkin for multiplayer testing.
266 * 2 10/07/98 10:53a Dave
269 * 1 10/07/98 10:49a Dave
271 * 14 5/20/98 9:46p John
272 * added code so the places in code that change half the palette don't
273 * have to clear the screen.
275 * 13 5/06/98 5:30p John
276 * Removed unused cfilearchiver. Removed/replaced some unused/little used
277 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
278 * DirectX header files and libs that fixed the Direct3D alpha blending
281 * 12 4/14/98 12:15p John
282 * Made 16-bpp movies work.
284 * 11 3/12/98 5:36p John
285 * Took out any unused shaders. Made shader code take rgbc instead of
286 * matrix and vector since noone used it like a matrix and it would have
287 * been impossible to do in hardware. Made Glide implement a basic
288 * shader for online help.
290 * 10 3/10/98 4:18p John
291 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
292 * & Glide have popups and print screen. Took out all >8bpp software
293 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
294 * support Fred. Made zbuffering key off of functions rather than one
297 * 9 12/02/97 4:00p John
298 * Added first rev of thruster glow, along with variable levels of
299 * translucency, which retquired some restructing of palman.
301 * 8 10/03/97 9:10a John
302 * added better antialiased line drawer
304 * 7 9/23/97 10:45a John
305 * made so you can tell bitblt code to rle a bitmap by passing flag to
308 * 6 9/09/97 11:01a Sandeep
309 * fixed warning level 4 bugs
311 * 5 7/10/97 2:06p John
312 * added code to specify alphablending type for bitmaps.
314 * 4 6/17/97 7:04p John
315 * added d3d support for gradients.
316 * fixed some color bugs by adding screen signatures instead of watching
317 * flags and palette changes.
319 * 3 6/12/97 2:50a Lawrance
320 * bm_unlock() now passed bitmap number, not pointer
322 * 2 6/11/97 1:12p John
323 * Started fixing all the text colors in the game.
325 * 1 5/12/97 12:14p John
332 #include <windowsx.h>
346 #include "floating.h"
348 #include "systemvars.h"
349 #include "grinternal.h"
350 #include "gropengl.h"
354 #include "osregistry.h"
357 static int Inited = 0;
359 typedef enum gr_texture_source {
361 TEXTURE_SOURCE_DECAL,
362 TEXTURE_SOURCE_NO_FILTERING,
365 typedef enum gr_alpha_blend {
366 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
367 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
368 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
369 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
370 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
373 typedef enum gr_zbuffer_type {
380 #define NEBULA_COLORS 20
382 volatile int GL_activate = 0;
383 volatile int GL_deactivate = 0;
385 static int GL_use_luminance_alpha;
387 static char *Gr_saved_screen = NULL;
388 static int Gr_saved_screen_bitmap;
391 // Throw in some dummy functions - DDOI
393 int D3D_32bit = 0; // grd3d.cpp
394 int D3D_fog_mode = -1; // grd3d.cpp
395 int D3D_inited = 0; // grd3d.cpp
396 int D3D_zbias = 1; // grd3d.cpp
397 int D3d_rendition_uvs = 0; // grd3d.cpp
404 void d3d_zbias (int a)
410 static void gr_opengl_set_texture_state(gr_texture_source ts);
412 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
413 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
415 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
417 gr_opengl_set_texture_state(ts);
419 if (ab != GL_current_alpha_blend) {
421 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
422 glBlendFunc(GL_ONE, GL_ZERO);
424 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
425 glBlendFunc(GL_ONE, GL_ONE);
427 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
428 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
430 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
431 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
433 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
434 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
440 GL_current_alpha_blend = ab;
443 if (zt != GL_current_zbuffer_type) {
445 case ZBUFFER_TYPE_NONE:
446 glDepthFunc(GL_ALWAYS);
447 glDepthMask(GL_FALSE);
449 case ZBUFFER_TYPE_READ:
450 glDepthFunc(GL_LESS);
451 glDepthMask(GL_FALSE);
453 case ZBUFFER_TYPE_WRITE:
454 glDepthFunc(GL_ALWAYS);
455 glDepthMask(GL_TRUE);
457 case ZBUFFER_TYPE_FULL:
458 glDepthFunc(GL_LESS);
459 glDepthMask(GL_TRUE);
465 GL_current_zbuffer_type = zt;
469 void gr_opengl_activate(int active)
474 // don't grab key/mouse if cmdline says so or if we're fullscreen
475 if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
476 SDL_WM_GrabInput(SDL_GRAB_ON);
481 // let go of mouse/keyboard
482 SDL_WM_GrabInput(SDL_GRAB_OFF);
487 static void opengl_tcache_flush ();
489 void gr_opengl_preload_init()
491 if (gr_screen.mode != GR_OPENGL) {
495 opengl_tcache_flush ();
498 int GL_should_preload = 0;
499 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
501 if ( gr_screen.mode != GR_OPENGL) {
505 if ( !GL_should_preload ) {
509 float u_scale, v_scale;
513 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
515 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
519 mprintf(("Texture upload failed!\n" ));
525 void gr_opengl_pixel(int x, int y)
530 void gr_opengl_clear()
532 glClearColor(gr_screen.current_clear_color.red / 255.0,
533 gr_screen.current_clear_color.green / 255.0,
534 gr_screen.current_clear_color.blue / 255.0, 1.0);
536 glClear ( GL_COLOR_BUFFER_BIT );
539 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
540 static void opengl_tcache_frame ();
542 void gr_opengl_flip()
550 extern int Gr_opengl_mouse_saved;
551 Gr_opengl_mouse_saved = 0;
553 if ( mouse_is_visible() ) {
557 mouse_get_pos( &mx, &my );
559 gr_opengl_save_mouse_area(mx,my,32,32);
561 if ( Gr_cursor == -1 ) {
564 gr_set_bitmap(Gr_cursor);
570 GLenum error = glGetError();
573 error = glGetError();
575 if (error != GL_NO_ERROR) {
576 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
579 } while (error != GL_NO_ERROR);
582 SDL_GL_SwapBuffers ();
584 opengl_tcache_frame ();
586 int cnt = GL_activate;
589 opengl_tcache_flush();
590 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
596 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
600 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
605 void gr_opengl_set_clip(int x,int y,int w,int h)
607 // check for sanity of parameters
613 if (x >= gr_screen.max_w)
614 x = gr_screen.max_w - 1;
615 if (y >= gr_screen.max_h)
616 y = gr_screen.max_h - 1;
618 if (x + w > gr_screen.max_w)
619 w = gr_screen.max_w - x;
620 if (y + h > gr_screen.max_h)
621 h = gr_screen.max_h - y;
623 if (w > gr_screen.max_w)
625 if (h > gr_screen.max_h)
628 gr_screen.offset_x = x;
629 gr_screen.offset_y = y;
630 gr_screen.clip_left = 0;
631 gr_screen.clip_right = w-1;
632 gr_screen.clip_top = 0;
633 gr_screen.clip_bottom = h-1;
634 gr_screen.clip_width = w;
635 gr_screen.clip_height = h;
637 glEnable(GL_SCISSOR_TEST);
638 glScissor(x, gr_screen.max_h-y-h, w, h);
641 void gr_opengl_reset_clip()
643 gr_screen.offset_x = 0;
644 gr_screen.offset_y = 0;
645 gr_screen.clip_left = 0;
646 gr_screen.clip_top = 0;
647 gr_screen.clip_right = gr_screen.max_w - 1;
648 gr_screen.clip_bottom = gr_screen.max_h - 1;
649 gr_screen.clip_width = gr_screen.max_w;
650 gr_screen.clip_height = gr_screen.max_h;
652 glDisable(GL_SCISSOR_TEST);
653 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
656 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
658 gr_screen.current_alpha = alpha;
659 gr_screen.current_alphablend_mode = alphablend_mode;
660 gr_screen.current_bitblt_mode = bitblt_mode;
661 gr_screen.current_bitmap = bitmap_num;
663 gr_screen.current_bitmap_sx = sx;
664 gr_screen.current_bitmap_sy = sy;
667 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
669 shade->screen_sig = gr_screen.signature;
676 void gr_opengl_set_shader( shader * shade )
679 if (shade->screen_sig != gr_screen.signature) {
680 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
682 gr_screen.current_shader = *shade;
684 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
689 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
691 STUB_FUNCTION; /* who called me? */
694 void gr_opengl_bitmap(int x, int y)
696 STUB_FUNCTION; /* who called me? */
699 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
703 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
705 saved_zbuf = gr_zbuffer_get();
707 // start the frame, no zbuffering, no culling
709 gr_zbuffer_set(GR_ZBUFF_NONE);
718 v[0].flags = PF_PROJECTED;
725 v[1].sx = i2fl(x + w);
730 v[1].flags = PF_PROJECTED;
737 v[2].sx = i2fl(x + w);
738 v[2].sy = i2fl(y + h);
742 v[2].flags = PF_PROJECTED;
750 v[3].sy = i2fl(y + h);
754 v[3].flags = PF_PROJECTED;
762 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
766 // restore zbuffer and culling
767 gr_zbuffer_set(saved_zbuf);
771 void gr_opengl_rect(int x,int y,int w,int h)
773 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);
776 void gr_opengl_shade(int x,int y,int w,int h)
783 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
784 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
785 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
786 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
787 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
788 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
789 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
790 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
792 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
795 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
800 if ( !gr_screen.current_color.is_alphacolor ) return;
802 float u_scale, v_scale;
804 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
805 // Couldn't set texture
806 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
810 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
812 float u0, u1, v0, v1;
813 float x1, x2, y1, y2;
816 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
818 u0 = u_scale*i2fl(sx)/i2fl(bw);
819 v0 = v_scale*i2fl(sy)/i2fl(bh);
821 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
822 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
824 x1 = i2fl(x+gr_screen.offset_x);
825 y1 = i2fl(y+gr_screen.offset_y);
826 x2 = i2fl(x+w+gr_screen.offset_x);
827 y2 = i2fl(y+h+gr_screen.offset_y);
829 if ( gr_screen.current_color.is_alphacolor ) {
830 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
832 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
836 glTexCoord2f (u0, v1);
837 glVertex3f (x1, y2, -0.99);
839 glTexCoord2f (u1, v1);
840 glVertex3f (x2, y2, -0.99);
842 glTexCoord2f (u1, v0);
843 glVertex3f (x2, y1, -0.99);
845 glTexCoord2f (u0, v0);
846 glVertex3f (x1, y1, -0.99);
850 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
857 int dx1=x, dx2=x+w-1;
858 int dy1=y, dy2=y+h-1;
861 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
866 if ( count > 1 ) Int3();
870 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
871 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
872 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
873 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
874 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
875 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
902 if ( w < 1 ) return; // clipped away!
903 if ( h < 1 ) return; // clipped away!
907 // Make sure clipping algorithm works
911 Assert( w == (dx2-dx1+1) );
912 Assert( h == (dy2-dy1+1) );
915 Assert( sx+w <= bw );
916 Assert( sy+h <= bh );
917 Assert( dx2 >= dx1 );
918 Assert( dy2 >= dy1 );
919 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
920 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
921 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
922 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
925 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
926 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
929 void gr_opengl_aabitmap(int x, int y)
933 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
934 int dx1=x, dx2=x+w-1;
935 int dy1=y, dy2=y+h-1;
938 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
939 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
940 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
941 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
942 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
943 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
945 if ( sx < 0 ) return;
946 if ( sy < 0 ) return;
947 if ( sx >= w ) return;
948 if ( sy >= h ) return;
950 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
951 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
954 void gr_opengl_string( int sx, int sy, char *s )
956 int width, spacing, letter;
959 if ( !Current_font ) {
963 gr_set_bitmap(Current_font->bitmap_id);
968 if (sx==0x8000) { //centered
969 x = get_centered_x(s);
981 y += Current_font->h;
982 if (sx==0x8000) { //centered
983 x = get_centered_x(s);
990 letter = get_char_width(s[0],s[1],&width,&spacing);
993 //not in font, draw as space
1001 // Check if this character is totally clipped
1002 if ( x + width < gr_screen.clip_left ) continue;
1003 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1004 if ( x > gr_screen.clip_right ) continue;
1005 if ( y > gr_screen.clip_bottom ) continue;
1008 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1009 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1013 wc = width - xd; hc = Current_font->h - yd;
1014 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1015 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1017 if ( wc < 1 ) continue;
1018 if ( hc < 1 ) continue;
1020 int u = Current_font->bm_u[letter];
1021 int v = Current_font->bm_v[letter];
1023 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1027 void gr_opengl_line(int x1,int y1,int x2,int y2)
1029 int clipped = 0, swapped=0;
1031 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1033 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);
1038 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1039 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1040 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1041 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1043 if ( x1 == x2 && y1 == y2 ) {
1044 glBegin (GL_POINTS);
1045 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1046 glVertex3f (sx1, sy1, -0.99f);
1058 } else if ( y1 == y2 ) {
1067 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1068 glVertex3f (sx2, sy2, -0.99f);
1069 glVertex3f (sx1, sy1, -0.99f);
1073 void gr_opengl_aaline(vertex *v1, vertex *v2)
1075 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1078 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1080 int clipped = 0, swapped=0;
1082 if ( !gr_screen.current_color.is_alphacolor ) {
1083 gr_line( x1, y1, x2, y2 );
1087 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);
1089 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1091 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1092 int ba = swapped ? gr_screen.current_color.alpha : 0;
1097 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1098 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1099 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1100 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1108 } else if ( y1 == y2 ) {
1117 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1118 glVertex3f (sx2, sy2, -0.99f);
1119 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1120 glVertex3f (sx1, sy1, -0.99f);
1124 void gr_opengl_circle( int xc, int yc, int d )
1134 if ( (xc+r) < gr_screen.clip_left ) return;
1135 if ( (xc-r) > gr_screen.clip_right ) return;
1136 if ( (yc+r) < gr_screen.clip_top ) return;
1137 if ( (yc-r) > gr_screen.clip_bottom ) return;
1140 // Draw the first octant
1141 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1142 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1147 // Draw the second octant
1148 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1149 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1157 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1158 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1163 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1167 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1168 if (f_float < 0.0f) {
1176 *a = (int)(f_float * 255.0);
1179 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1182 float u_scale = 1.0f, v_scale = 1.0f;
1184 // Make nebula use the texture mapper... this blends the colors better.
1185 if ( flags & TMAP_FLAG_NEBULA ){
1189 gr_texture_source texture_source = (gr_texture_source)-1;
1190 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1191 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1193 if ( gr_zbuffering ) {
1194 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1195 zbuffer_type = ZBUFFER_TYPE_READ;
1197 zbuffer_type = ZBUFFER_TYPE_FULL;
1200 zbuffer_type = ZBUFFER_TYPE_NONE;
1205 int tmap_type = TCACHE_TYPE_NORMAL;
1209 if ( flags & TMAP_FLAG_TEXTURED ) {
1212 r = gr_screen.current_color.red;
1213 g = gr_screen.current_color.green;
1214 b = gr_screen.current_color.blue;
1217 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1220 tmap_type = TCACHE_TYPE_NORMAL;
1221 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1223 // Blend with screen pixel using src*alpha+dst
1224 float factor = gr_screen.current_alpha;
1228 if ( factor <= 1.0f ) {
1229 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1230 r = (r*tmp_alpha)/255;
1231 g = (g*tmp_alpha)/255;
1232 b = (b*tmp_alpha)/255;
1235 tmap_type = TCACHE_TYPE_XPARENT;
1237 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1239 // Blend with screen pixel using src*alpha+dst
1240 float factor = gr_screen.current_alpha;
1242 if ( factor > 1.0f ) {
1245 alpha = fl2i(gr_screen.current_alpha*255.0f);
1249 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1250 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1252 alpha_blend = ALPHA_BLEND_NONE;
1257 if(flags & TMAP_FLAG_BITMAP_SECTION){
1258 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1261 texture_source = TEXTURE_SOURCE_NONE;
1263 if ( flags & TMAP_FLAG_TEXTURED ) {
1264 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 ))
1266 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1270 // use nonfiltered textures for bitmap sections
1271 if(flags & TMAP_FLAG_BITMAP_SECTION){
1272 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1274 texture_source = TEXTURE_SOURCE_DECAL;
1279 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1281 if ( flags & TMAP_FLAG_TEXTURED )
1287 if (flags & TMAP_FLAG_PIXEL_FOG) {
1293 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1295 vertex * va = verts[i];
1299 x = fl2i(va->sx*16.0f);
1300 y = fl2i(va->sy*16.0f);
1302 x += gr_screen.offset_x*16;
1303 y += gr_screen.offset_y*16;
1305 sx = i2fl(x) / 16.0f;
1306 sy = i2fl(y) / 16.0f;
1308 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1319 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1322 glBegin(GL_TRIANGLE_FAN);
1323 for (i = nv-1; i >= 0; i--) {
1324 vertex * va = verts[i];
1330 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1331 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1340 if ( flags & TMAP_FLAG_CORRECT ) {
1346 if (flags & TMAP_FLAG_ALPHA) {
1352 if (flags & TMAP_FLAG_NEBULA ) {
1353 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1354 r = gr_palette[pal*3+0];
1355 g = gr_palette[pal*3+1];
1356 b = gr_palette[pal*3+2];
1357 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1358 r = Gr_gamma_lookup[verts[i]->b];
1359 g = Gr_gamma_lookup[verts[i]->b];
1360 b = Gr_gamma_lookup[verts[i]->b];
1361 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1362 // Make 0.75 be 256.0f
1363 r = Gr_gamma_lookup[verts[i]->r];
1364 g = Gr_gamma_lookup[verts[i]->g];
1365 b = Gr_gamma_lookup[verts[i]->b];
1367 // use constant RGB values...
1369 glColor4ub (r,g,b,a);
1371 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1374 /* this is for GL_EXT_SECONDARY_COLOR */
1375 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1376 /* do separate color call here */
1382 x = fl2i(va->sx*16.0f);
1383 y = fl2i(va->sy*16.0f);
1385 x += gr_screen.offset_x*16;
1386 y += gr_screen.offset_y*16;
1388 sx = i2fl(x) / 16.0f;
1389 sy = i2fl(y) / 16.0f;
1391 if ( flags & TMAP_FLAG_TEXTURED ) {
1394 glTexCoord2f(tu, tv);
1397 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1402 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1404 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1407 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1409 void gr_opengl_scaler(vertex *va, vertex *vb )
1411 float x0, y0, x1, y1;
1412 float u0, v0, u1, v1;
1413 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1414 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1415 float xmin, xmax, ymin, ymax;
1416 int dx0, dy0, dx1, dy1;
1418 //============= CLIP IT =====================
1420 x0 = va->sx; y0 = va->sy;
1421 x1 = vb->sx; y1 = vb->sy;
1423 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1424 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1426 u0 = va->u; v0 = va->v;
1427 u1 = vb->u; v1 = vb->v;
1429 // Check for obviously offscreen bitmaps...
1430 if ( (y1<=y0) || (x1<=x0) ) return;
1431 if ( (x1<xmin ) || (x0>xmax) ) return;
1432 if ( (y1<ymin ) || (y0>ymax) ) return;
1434 clipped_u0 = u0; clipped_v0 = v0;
1435 clipped_u1 = u1; clipped_v1 = v1;
1437 clipped_x0 = x0; clipped_y0 = y0;
1438 clipped_x1 = x1; clipped_y1 = y1;
1440 // Clip the left, moving u0 right as necessary
1442 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1446 // Clip the right, moving u1 left as necessary
1448 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1452 // Clip the top, moving v0 down as necessary
1454 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1458 // Clip the bottom, moving v1 up as necessary
1460 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1464 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1465 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1467 if (dx1<=dx0) return;
1468 if (dy1<=dy0) return;
1470 //============= DRAW IT =====================
1484 v[1].sx = clipped_x1;
1485 v[1].sy = clipped_y0;
1488 v[1].u = clipped_u1;
1489 v[1].v = clipped_v0;
1492 v[2].sx = clipped_x1;
1493 v[2].sy = clipped_y1;
1496 v[2].u = clipped_u1;
1497 v[2].v = clipped_v1;
1500 v[3].sx = clipped_x0;
1501 v[3].sy = clipped_y1;
1504 v[3].u = clipped_u0;
1505 v[3].v = clipped_v1;
1507 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1510 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1514 void gr_opengl_get_color( int * r, int * g, int * b )
1516 if (r) *r = gr_screen.current_color.red;
1517 if (g) *g = gr_screen.current_color.green;
1518 if (b) *b = gr_screen.current_color.blue;
1521 void gr_opengl_init_color(color *c, int r, int g, int b)
1523 c->screen_sig = gr_screen.signature;
1524 c->red = (unsigned char)r;
1525 c->green = (unsigned char)g;
1526 c->blue = (unsigned char)b;
1528 c->ac_type = AC_TYPE_NONE;
1530 c->is_alphacolor = 0;
1534 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1536 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1537 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1538 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1539 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1541 gr_opengl_init_color( clr, r, g, b );
1543 clr->alpha = (unsigned char)alpha;
1544 clr->ac_type = (ubyte)type;
1545 clr->alphacolor = -1;
1546 clr->is_alphacolor = 1;
1549 void gr_opengl_set_color( int r, int g, int b )
1551 Assert((r >= 0) && (r < 256));
1552 Assert((g >= 0) && (g < 256));
1553 Assert((b >= 0) && (b < 256));
1555 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1558 void gr_opengl_set_color_fast(color *dst)
1560 if ( dst->screen_sig != gr_screen.signature ) {
1561 if ( dst->is_alphacolor ) {
1562 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1564 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1567 gr_screen.current_color = *dst;
1570 void gr_opengl_print_screen(char *filename)
1575 int gr_opengl_supports_res_ingame(int res)
1582 int gr_opengl_supports_res_interface(int res)
1589 static void opengl_tcache_cleanup ();
1591 void gr_opengl_cleanup()
1593 if ( !Inited ) return;
1600 opengl_tcache_cleanup ();
1605 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1607 Assert((r >= 0) && (r < 256));
1608 Assert((g >= 0) && (g < 256));
1609 Assert((b >= 0) && (b < 256));
1611 if (fog_mode == GR_FOGMODE_NONE) {
1612 if (gr_screen.current_fog_mode != fog_mode) {
1615 gr_screen.current_fog_mode = fog_mode;
1620 if (gr_screen.current_fog_mode != fog_mode) {
1623 if (D3D_fog_mode == 2) {
1624 glFogi(GL_FOG_MODE, GL_LINEAR);
1627 gr_screen.current_fog_mode = fog_mode;
1630 if ( (gr_screen.current_fog_color.red != r) ||
1631 (gr_screen.current_fog_color.green != g) ||
1632 (gr_screen.current_fog_color.blue != b) ) {
1635 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1637 fc[0] = (float)r/255.0;
1638 fc[1] = (float)g/255.0;
1639 fc[2] = (float)b/255.0;
1642 glFogfv(GL_FOG_COLOR, fc);
1645 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1646 ((fog_near != gr_screen.fog_near) ||
1647 (fog_far != gr_screen.fog_far)) ) {
1648 gr_screen.fog_near = fog_near;
1649 gr_screen.fog_far = fog_far;
1651 if (D3D_fog_mode == 2) {
1652 glFogf(GL_FOG_START, fog_near);
1653 glFogf(GL_FOG_END, fog_far);
1658 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1663 void gr_opengl_set_cull(int cull)
1666 glEnable (GL_CULL_FACE);
1667 glFrontFace (GL_CCW);
1669 glDisable (GL_CULL_FACE);
1673 void gr_opengl_filter_set(int filter)
1678 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1681 gr_set_bitmap(bmap1);
1684 gr_set_bitmap(bmap2);
1690 typedef struct tcache_slot_opengl {
1691 GLuint texture_handle;
1692 float u_scale, v_scale;
1695 int used_this_frame;
1700 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1701 tcache_slot_opengl *parent;
1703 gr_texture_source texture_mode;
1704 } tcache_slot_opengl;
1706 static void *Texture_sections = NULL;
1707 static tcache_slot_opengl *Textures = NULL;
1709 static tcache_slot_opengl *GL_bound_texture;
1711 int GL_texture_sections = 0;
1712 int GL_texture_ram = 0;
1713 int GL_frame_count = 0;
1714 int GL_min_texture_width = 0;
1715 int GL_max_texture_width = 0;
1716 int GL_min_texture_height = 0;
1717 int GL_max_texture_height = 0;
1718 int GL_square_textures = 0;
1719 int GL_textures_in = 0;
1720 int GL_textures_in_frame = 0;
1721 int GL_last_bitmap_id = -1;
1722 int GL_last_detail = -1;
1723 int GL_last_bitmap_type = -1;
1724 int GL_last_section_x = -1;
1725 int GL_last_section_y = -1;
1729 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1731 static void gr_opengl_set_texture_state(gr_texture_source ts)
1733 if (ts == TEXTURE_SOURCE_NONE) {
1734 GL_bound_texture = NULL;
1736 glBindTexture(GL_TEXTURE_2D, 0);
1737 gr_tcache_set(-1, -1, NULL, NULL );
1738 } else if (GL_bound_texture &&
1739 GL_bound_texture->texture_mode != ts) {
1741 case TEXTURE_SOURCE_DECAL:
1742 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1743 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1745 case TEXTURE_SOURCE_NO_FILTERING:
1746 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1747 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1753 GL_bound_texture->texture_mode = ts;
1756 GL_current_texture_source = ts;
1759 static int opengl_max_tex_size_get()
1761 GLint max_texture_size = 0;
1763 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1764 return max_texture_size;
1767 static void opengl_tcache_init (int use_sections)
1771 // DDOI - FIXME skipped a lot of stuff here
1772 GL_should_preload = 0;
1774 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1777 if ( tmp_pl == 0 ) {
1778 GL_should_preload = 0;
1779 } else if ( tmp_pl == 1 ) {
1780 GL_should_preload = 1;
1782 GL_should_preload = 1;
1787 GL_min_texture_width = 16;
1788 GL_min_texture_height = 16;
1790 GL_max_texture_width = opengl_max_tex_size_get();
1791 GL_max_texture_height = opengl_max_tex_size_get();
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 // set height and width to a power of 2
2003 for (i=0; i<16; i++ ) {
2004 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2010 for (i=0; i<16; i++ ) {
2011 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2017 // try to keep an 8:1 size ratio
2018 if (tex_w/tex_h > 8)
2020 if (tex_h/tex_w > 8)
2023 if ( tex_w < GL_min_texture_width ) {
2024 tex_w = GL_min_texture_width;
2025 } else if ( tex_w > GL_max_texture_width ) {
2026 tex_w = GL_max_texture_width;
2029 if ( tex_h < GL_min_texture_height ) {
2030 tex_h = GL_min_texture_height;
2031 } else if ( tex_h > GL_max_texture_height ) {
2032 tex_h = GL_max_texture_height;
2035 if ( GL_square_textures) {
2037 // Make the both be equal to larger of the two
2038 new_size = max(tex_w, tex_h);
2043 // store the outgoing size
2048 // data == start of bitmap data
2049 // sx == x offset into bitmap
2050 // sy == y offset into bitmap
2051 // src_w == absolute width of section on source bitmap
2052 // src_h == absolute height of section on source bitmap
2053 // bmap_w == width of source bitmap
2054 // bmap_h == height of source bitmap
2055 // tex_w == width of final texture
2056 // tex_h == height of final texture
2057 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)
2067 if ( t->used_this_frame == GL_frame_count ) {
2068 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2073 if(!opengl_free_texture(t)){
2078 // get final texture size
2079 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2081 if ( (tex_w < 1) || (tex_h < 1) ) {
2082 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2086 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2087 t->u_scale = (float)bmap_w / (float)tex_w;
2088 t->v_scale = (float)bmap_h / (float)tex_h;
2089 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2090 t->u_scale = (float)src_w / (float)tex_w;
2091 t->v_scale = (float)src_h / (float)tex_h;
2098 glGenTextures (1, &t->texture_handle);
2101 if (t->texture_handle == 0) {
2102 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2106 GL_bound_texture = t;
2108 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2110 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2112 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2113 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2114 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2116 /* this should be set next anyway */
2117 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2118 // gr_opengl_set_texture_state(GL_current_texture_source);
2123 switch (bitmap_type) {
2125 case TCACHE_TYPE_AABITMAP:
2128 ubyte *bmp_data = ((ubyte*)data);
2134 for (i=0; i<16; i++) {
2135 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2138 for ( ; i<256; i++ ) {
2142 texmem = (ubyte *) malloc (tex_w*tex_h);
2145 for (i=0;i<tex_h;i++)
2147 for (j=0;j<tex_w;j++)
2149 if (i < bmap_h && j < bmap_w) {
2150 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2159 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2165 case TCACHE_TYPE_BITMAP_SECTION:
2168 ubyte *bmp_data = ((ubyte*)data);
2169 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2170 ubyte *texmemp = texmem;
2172 for (i=0;i<tex_h;i++)
2174 for (j=0;j<tex_w;j++)
2176 if (i < src_h && j < src_w) {
2177 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2178 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2186 size = tex_w*tex_h*2;
2188 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2189 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2197 ubyte *bmp_data = ((ubyte*)data);
2198 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2199 ubyte *texmemp = texmem;
2201 fix u, utmp, v, du, dv;
2205 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2206 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2208 for (j=0;j<tex_h;j++)
2211 for (i=0;i<tex_w;i++)
2213 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2214 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2220 size = tex_w*tex_h*2;
2222 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2223 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2231 t->bitmap_id = texture_handle;
2232 t->time_created = GL_frame_count;
2233 t->used_this_frame = 0;
2235 t->w = (ushort)tex_w;
2236 t->h = (ushort)tex_h;
2238 GL_textures_in_frame += t->size;
2240 GL_textures_in += t->size;
2246 static int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2250 int final_w, final_h;
2254 // setup texture/bitmap flags
2256 switch(bitmap_type){
2257 case TCACHE_TYPE_AABITMAP:
2258 flags |= BMP_AABITMAP;
2261 case TCACHE_TYPE_NORMAL:
2262 flags |= BMP_TEX_OTHER;
2263 case TCACHE_TYPE_XPARENT:
2264 flags |= BMP_TEX_XPARENT;
2266 case TCACHE_TYPE_NONDARKENING:
2268 flags |= BMP_TEX_NONDARK;
2272 // lock the bitmap into the proper format
2273 bmp = bm_lock(bitmap_handle, bpp, flags);
2274 if ( bmp == NULL ) {
2275 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2284 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2285 // max_w /= D3D_texture_divider;
2286 // max_h /= D3D_texture_divider;
2288 // Detail.debris_culling goes from 0 to 4.
2289 max_w /= (16 >> Detail.hardware_textures);
2290 max_h /= (16 >> Detail.hardware_textures);
2294 // get final texture size as it will be allocated as a DD surface
2295 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2297 // if this tcache slot has no bitmap
2298 if ( tslot->bitmap_id < 0) {
2301 // different bitmap altogether - determine if the new one can use the old one's slot
2302 else if (tslot->bitmap_id != bitmap_handle) {
2303 if((final_w == tslot->w) && (final_h == tslot->h)){
2305 //ml_printf("Reloading texture %d\n", bitmap_handle);
2312 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);
2314 // unlock the bitmap
2315 bm_unlock(bitmap_handle);
2320 static int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2324 int final_w, final_h;
2325 int section_x, section_y;
2328 // setup texture/bitmap flags
2329 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2330 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2331 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2333 flags = BMP_TEX_XPARENT;
2335 // lock the bitmap in the proper format
2336 bmp = bm_lock(bitmap_handle, 16, flags);
2337 if ( bmp == NULL ) {
2338 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2341 // determine the width and height of this section
2342 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2344 // get final texture size as it will be allocated as an opengl texture
2345 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2347 // if this tcache slot has no bitmap
2348 if ( tslot->bitmap_id < 0) {
2351 // different bitmap altogether - determine if the new one can use the old one's slot
2352 else if (tslot->bitmap_id != bitmap_handle) {
2353 if((final_w == tslot->w) && (final_h == tslot->h)){
2361 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);
2363 // unlock the bitmap
2364 bm_unlock(bitmap_handle);
2370 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2371 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)
2380 GL_last_bitmap_id = -1;
2384 if ( GL_last_detail != Detail.hardware_textures ) {
2385 GL_last_detail = Detail.hardware_textures;
2386 opengl_tcache_flush();
2393 int n = bm_get_cache_slot (bitmap_id, 1);
2394 tcache_slot_opengl *t = &Textures[n];
2396 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)) {
2397 t->used_this_frame = GL_frame_count;
2399 // mark all children as used
2400 if(GL_texture_sections){
2401 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2402 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2403 if(t->data_sections[idx][s_idx] != NULL){
2404 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2410 *u_scale = t->u_scale;
2411 *v_scale = t->v_scale;
2415 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2416 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2417 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2423 // if the texture sections haven't been created yet
2424 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2426 // lock the bitmap in the proper format
2427 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2428 bm_unlock(bitmap_id);
2430 // now lets do something for each texture
2432 for(idx=0; idx<bmp->sections.num_x; idx++){
2433 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2434 // hmm. i'd rather we didn't have to do it this way...
2435 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2439 // not used this frame
2440 t->data_sections[idx][s_idx]->used_this_frame = 0;
2444 // zero out pretty much everything in the parent struct since he's just the root
2445 t->bitmap_id = bitmap_id;
2446 t->texture_handle = 0;
2447 t->time_created = t->data_sections[sx][sy]->time_created;
2448 t->used_this_frame = 0;
2450 t->vram_texture = NULL;
2451 t->vram_texture_surface = NULL
2455 // argh. we failed to upload. free anything we can
2457 opengl_free_texture(t);
2459 // swap in the texture we want
2461 t = t->data_sections[sx][sy];
2464 // all other "normal" textures
2465 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2466 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2469 // everything went ok
2470 if(ret_val && (t->texture_handle) && !vram_full){
2471 *u_scale = t->u_scale;
2472 *v_scale = t->v_scale;
2474 GL_bound_texture = t;
2476 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2478 /* this should be set next anyway */
2479 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2480 // gr_opengl_set_texture_state(GL_current_texture_source);
2483 GL_last_bitmap_id = t->bitmap_id;
2484 GL_last_bitmap_type = bitmap_type;
2485 GL_last_section_x = sx;
2486 GL_last_section_y = sy;
2488 t->used_this_frame = GL_frame_count;
2492 GL_last_bitmap_id = -1;
2493 GL_last_bitmap_type = -1;
2495 GL_last_section_x = -1;
2496 GL_last_section_y = -1;
2498 GL_bound_texture = NULL;
2500 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2507 void gr_opengl_set_clear_color(int r, int g, int b)
2509 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2512 void gr_opengl_flash(int r, int g, int b)
2518 if ( r || g || b ) {
2519 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2521 float x1, x2, y1, y2;
2522 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2523 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2524 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2525 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2527 glColor4ub(r, g, b, 255);
2529 glVertex3f (x1, y2, -0.99);
2531 glVertex3f (x2, y2, -0.99);
2533 glVertex3f (x2, y1, -0.99);
2535 glVertex3f (x1, y1, -0.99);
2540 int gr_opengl_zbuffer_get()
2542 if ( !gr_global_zbuffering ) {
2543 return GR_ZBUFF_NONE;
2545 return gr_zbuffering_mode;
2548 int gr_opengl_zbuffer_set(int mode)
2550 int tmp = gr_zbuffering_mode;
2552 gr_zbuffering_mode = mode;
2554 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2562 void gr_opengl_zbuffer_clear(int mode)
2566 gr_zbuffering_mode = GR_ZBUFF_FULL;
2567 gr_global_zbuffering = 1;
2569 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2570 glClear ( GL_DEPTH_BUFFER_BIT );
2573 gr_zbuffering_mode = GR_ZBUFF_NONE;
2574 gr_global_zbuffering = 0;
2578 void gr_opengl_set_gamma(float gamma)
2581 Gr_gamma_int = int (Gr_gamma*10);
2583 // Create the Gamma lookup table
2585 for (i=0;i<256; i++) {
2586 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2589 } else if ( v < 0 ) {
2592 Gr_gamma_lookup[i] = v;
2595 // Flush any existing textures
2596 opengl_tcache_flush();
2599 void gr_opengl_fade_in(int instantaneous)
2604 void gr_opengl_fade_out(int instantaneous)
2609 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2612 glReadBuffer(GL_FRONT);
2614 glReadBuffer(GL_BACK);
2617 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2619 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2621 if (gr_screen.bits_per_pixel == 15) {
2622 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2623 } else if (gr_screen.bits_per_pixel == 32) {
2624 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2627 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2630 static int Gr_opengl_mouse_saved = 0;
2631 static int Gr_opengl_mouse_saved_x1 = 0;
2632 static int Gr_opengl_mouse_saved_y1 = 0;
2633 static int Gr_opengl_mouse_saved_x2 = 0;
2634 static int Gr_opengl_mouse_saved_y2 = 0;
2635 static int Gr_opengl_mouse_saved_w = 0;
2636 static int Gr_opengl_mouse_saved_h = 0;
2637 #define MAX_SAVE_SIZE (32*32)
2638 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2640 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2642 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2644 Gr_opengl_mouse_saved_x1 = x;
2645 Gr_opengl_mouse_saved_y1 = y;
2646 Gr_opengl_mouse_saved_x2 = x+w-1;
2647 Gr_opengl_mouse_saved_y2 = y+h-1;
2649 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2650 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2651 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2652 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2654 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2655 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2657 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2658 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2660 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2662 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2664 glReadBuffer(GL_BACK);
2665 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);
2667 Gr_opengl_mouse_saved = 1;
2670 int gr_opengl_save_screen()
2674 if ( Gr_saved_screen ) {
2675 mprintf(( "Screen alread saved!\n" ));
2679 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2680 if (!Gr_saved_screen) {
2681 mprintf(( "Couldn't get memory for saved screen!\n" ));
2685 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2686 if (!Gr_saved_screen_tmp) {
2687 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2691 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2693 glReadBuffer(GL_FRONT);
2694 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);
2698 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2699 dptr = (ubyte *)Gr_saved_screen;
2700 for (int j = 0; j < gr_screen.max_h; j++) {
2701 sptr -= gr_screen.max_w*2;
2702 memcpy(dptr, sptr, gr_screen.max_w*2);
2703 dptr += gr_screen.max_w*2;
2706 free(Gr_saved_screen_tmp);
2708 if (Gr_opengl_mouse_saved) {
2709 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2710 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2711 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2712 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2715 dptr -= gr_screen.max_w*2;
2719 // this leaks texture handles, and the opengl doesn't currently
2720 // perform some sort of garbage collection, so a hack was added
2721 // to bmpman to make it free textures when released
2722 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2724 return Gr_saved_screen_bitmap;
2727 void gr_opengl_restore_screen(int id)
2731 if ( !Gr_saved_screen ) {
2736 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2738 gr_set_bitmap(Gr_saved_screen_bitmap);
2742 void gr_opengl_free_screen(int id)
2744 bm_release(Gr_saved_screen_bitmap);
2746 if ( Gr_saved_screen ) {
2747 free( Gr_saved_screen );
2748 Gr_saved_screen = NULL;
2752 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2757 void gr_opengl_dump_frame_stop()
2762 void gr_opengl_dump_frame()
2767 uint gr_opengl_lock()
2774 void gr_opengl_unlock()
2778 void opengl_zbias(int bias)
2781 glEnable(GL_POLYGON_OFFSET_FILL);
2782 glPolygonOffset(0.0, -bias);
2784 glDisable(GL_POLYGON_OFFSET_FILL);
2788 extern char *Osreg_title;
2789 void gr_opengl_init()
2792 gr_opengl_cleanup();
2796 mprintf(( "Initializing opengl graphics device...\n" ));
2800 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2802 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2806 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2807 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2808 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2809 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2810 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2812 int flags = SDL_OPENGL;
2814 if (!Cmdline_window && ( (os_config_read_uint( NULL, "Fullscreen", 1 ) == 1) || Cmdline_fullscreen ))
2815 flags |= SDL_FULLSCREEN;
2817 // don't automatically grab key/mouse if cmdline says so, else do
2818 if(Cmdline_no_grab) {
2819 SDL_WM_GrabInput(SDL_GRAB_OFF);
2821 SDL_WM_GrabInput(SDL_GRAB_ON);
2824 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2826 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2831 SDL_WM_SetCaption (Osreg_title, "FS2");
2833 /* might as well put this here */
2834 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2836 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2838 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2840 glMatrixMode(GL_PROJECTION);
2842 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2843 glMatrixMode(GL_MODELVIEW);
2846 glShadeModel(GL_SMOOTH);
2847 glEnable(GL_DITHER);
2848 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2849 glHint(GL_FOG_HINT, GL_NICEST);
2851 glEnable(GL_DEPTH_TEST);
2854 glEnable(GL_TEXTURE_2D);
2856 glDepthRange(0.0, 1.0);
2858 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2859 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2861 D3D_32bit = 1; // grd3d.cpp
2862 extern int D3D_enabled;
2865 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2866 1 = use secondary color ext
2867 2 = use opengl linear fog
2869 D3D_fog_mode = 2; // grd3d.cpp
2873 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2880 gr_screen.bits_per_pixel = 15;
2881 gr_screen.bytes_per_pixel = 2;
2886 Gr_red.mask = 0x7C00;
2891 Gr_green.mask = 0x3E0;
2896 Gr_blue.mask = 0x1F;
2901 gr_screen.bits_per_pixel = 16;
2902 gr_screen.bytes_per_pixel = 2;
2907 Gr_red.mask = 0xF800;
2912 Gr_green.mask = 0x7E0;
2917 Gr_blue.mask = 0x1F;
2922 gr_screen.bits_per_pixel = 32;
2923 gr_screen.bytes_per_pixel = 4;
2928 Gr_red.mask = 0xff0000;
2933 Gr_green.mask = 0xff00;
2938 Gr_blue.mask = 0xff;
2943 Int3(); // Illegal bpp
2946 // DDOI - set these so no one else does!
2947 Gr_t_red.mask = Gr_red.mask;
2948 Gr_t_red.shift = Gr_red.shift;
2949 Gr_t_red.scale = Gr_red.scale;
2951 Gr_t_green.mask = Gr_green.mask;
2952 Gr_t_green.shift = Gr_green.shift;
2953 Gr_t_green.scale = Gr_green.scale;
2955 Gr_t_blue.mask = Gr_blue.mask;
2956 Gr_t_blue.shift = Gr_blue.shift;
2957 Gr_t_blue.scale = Gr_blue.scale;
2959 Gr_t_alpha.mask = 0x8000;
2960 Gr_t_alpha.scale = 255;
2961 Gr_t_alpha.shift = 15;
2963 Gr_ta_red.mask = 0x0f00;
2964 Gr_ta_red.shift = 8;
2965 Gr_ta_red.scale = 16;
2967 Gr_ta_green.mask = 0x00f0;
2968 Gr_ta_green.shift = 4;
2969 Gr_ta_green.scale = 16;
2971 Gr_ta_blue.mask = 0x000f;
2972 Gr_ta_blue.shift = 0;
2973 Gr_ta_blue.scale = 16;
2975 Gr_ta_alpha.mask = 0xf000;
2976 Gr_ta_alpha.shift = 12;
2977 Gr_ta_alpha.scale = 16;
2980 opengl_tcache_init (1);
2983 Gr_current_red = &Gr_red;
2984 Gr_current_blue = &Gr_blue;
2985 Gr_current_green = &Gr_green;
2986 Gr_current_alpha = &Gr_alpha;
2988 gr_screen.gf_flip = gr_opengl_flip;
2989 gr_screen.gf_flip_window = gr_opengl_flip_window;
2990 gr_screen.gf_set_clip = gr_opengl_set_clip;
2991 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2992 gr_screen.gf_set_font = grx_set_font;
2994 gr_screen.gf_set_color = gr_opengl_set_color;
2995 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2996 gr_screen.gf_create_shader = gr_opengl_create_shader;
2997 gr_screen.gf_set_shader = gr_opengl_set_shader;
2998 gr_screen.gf_clear = gr_opengl_clear;
2999 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3000 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3001 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3002 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3004 gr_screen.gf_rect = gr_opengl_rect;
3005 gr_screen.gf_shade = gr_opengl_shade;
3006 gr_screen.gf_string = gr_opengl_string;
3007 gr_screen.gf_circle = gr_opengl_circle;
3009 gr_screen.gf_line = gr_opengl_line;
3010 gr_screen.gf_aaline = gr_opengl_aaline;
3011 gr_screen.gf_pixel = gr_opengl_pixel;
3012 gr_screen.gf_scaler = gr_opengl_scaler;
3013 gr_screen.gf_tmapper = gr_opengl_tmapper;
3015 gr_screen.gf_gradient = gr_opengl_gradient;
3017 gr_screen.gf_set_palette = gr_opengl_set_palette;
3018 gr_screen.gf_get_color = gr_opengl_get_color;
3019 gr_screen.gf_init_color = gr_opengl_init_color;
3020 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3021 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3022 gr_screen.gf_print_screen = gr_opengl_print_screen;
3024 gr_screen.gf_fade_in = gr_opengl_fade_in;
3025 gr_screen.gf_fade_out = gr_opengl_fade_out;
3026 gr_screen.gf_flash = gr_opengl_flash;
3028 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3029 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3030 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3032 gr_screen.gf_save_screen = gr_opengl_save_screen;
3033 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3034 gr_screen.gf_free_screen = gr_opengl_free_screen;
3036 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3037 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3038 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3040 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3042 gr_screen.gf_lock = gr_opengl_lock;
3043 gr_screen.gf_unlock = gr_opengl_unlock;
3045 gr_screen.gf_fog_set = gr_opengl_fog_set;
3047 gr_screen.gf_get_region = gr_opengl_get_region;
3049 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3051 gr_screen.gf_set_cull = gr_opengl_set_cull;
3053 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3055 gr_screen.gf_filter_set = gr_opengl_filter_set;
3057 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3059 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;