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.67 2003/08/03 15:59:40 taylor
19 * GL_RGB5_A1 as TexImage internal format; cleaner input grab; min window title; cleanup
21 * Revision 1.66 2003/06/22 12:52:34 taylor
22 * more texture size fixin
24 * Revision 1.65 2003/06/19 11:52:47 taylor
25 * fix texture size issue with lower detail settings
27 * Revision 1.64 2003/05/28 06:02:04 taylor
28 * fix transparency in green weapon blobs
30 * Revision 1.63 2003/05/09 05:05:52 taylor
31 * improve gr_(de)activate, mouse grab, default fullscreen
33 * Revision 1.62 2003/05/04 04:56:53 taylor
34 * move SDL_Quit to os_deinit to fix fonttool segfault
36 * Revision 1.61 2003/02/02 21:13:27 relnev
37 * minor updates (made functions static, tried to improve texture sizes)
39 * Revision 1.60 2003/02/02 20:20:03 relnev
40 * add large texture support (Taylor Richards)
42 * Revision 1.59 2002/09/04 01:38:09 relnev
45 * Revision 1.57 2002/08/31 01:39:13 theoddone33
46 * Speed up the renderer a tad
48 * Revision 1.56 2002/08/01 05:03:11 relnev
51 * Revision 1.55 2002/08/01 04:55:45 relnev
52 * experimenting with texture state
54 * Revision 1.54 2002/07/30 15:00:15 relnev
55 * not use luminance alpha by default
57 * Revision 1.53 2002/06/22 17:08:16 relnev
58 * more fixes to unused alpha code
60 * Revision 1.52 2002/06/21 23:59:14 relnev
61 * moved a gr_opengl_set_state to after the gr_tcache_set
63 * Revision 1.51 2002/06/19 04:52:45 relnev
64 * MacOS X updates (Ryan)
66 * Revision 1.50 2002/06/18 23:20:44 relnev
69 * adjusted line offsets.
71 * Revision 1.49 2002/06/17 23:11:39 relnev
72 * enable sdl key repeating.
76 * Revision 1.48 2002/06/09 04:41:17 relnev
77 * added copyright header
79 * Revision 1.47 2002/06/09 03:16:04 relnev
82 * removed unneeded asm, old sdl 2d setup.
84 * fixed crash caused by opengl_get_region.
86 * Revision 1.46 2002/06/05 04:03:32 relnev
87 * finished cfilesystem.
89 * removed some old code.
91 * fixed mouse save off-by-one.
95 * Revision 1.45 2002/06/03 09:25:37 relnev
96 * implement mouse cursor and screen save/restore
98 * Revision 1.44 2002/06/02 18:46:59 relnev
101 * Revision 1.43 2002/06/02 11:34:00 relnev
104 * Revision 1.42 2002/06/02 10:28:17 relnev
105 * fix texture handle leak
107 * Revision 1.41 2002/06/01 09:00:34 relnev
108 * silly debug memmanager
110 * Revision 1.40 2002/06/01 07:12:33 relnev
111 * a few NDEBUG updates.
113 * removed a few warnings.
115 * Revision 1.39 2002/06/01 05:33:15 relnev
116 * copied more code over.
118 * added scissor clipping.
120 * Revision 1.38 2002/06/01 03:35:27 relnev
123 * Revision 1.37 2002/06/01 03:32:00 relnev
124 * fix texture loading mistake.
126 * enable some d3d stuff for opengl also
128 * Revision 1.36 2002/05/31 23:25:03 relnev
131 * Revision 1.34 2002/05/31 22:15:22 relnev
134 * Revision 1.33 2002/05/31 22:04:55 relnev
135 * use d3d rect_internal
137 * Revision 1.32 2002/05/31 06:28:23 relnev
140 * Revision 1.31 2002/05/31 06:04:39 relnev
143 * Revision 1.30 2002/05/31 03:56:11 theoddone33
144 * Change tmapper polygon winding and enable culling
146 * Revision 1.29 2002/05/31 03:34:02 theoddone33
150 * Revision 1.28 2002/05/31 00:06:59 relnev
153 * Revision 1.27 2002/05/30 23:46:29 theoddone33
154 * some minor key changes (not necessarily fixes)
156 * Revision 1.26 2002/05/30 23:33:12 relnev
157 * implemented a few more functions.
159 * Revision 1.25 2002/05/30 23:01:16 relnev
160 * implement gr_opengl_set_state.
162 * Revision 1.24 2002/05/30 22:12:57 relnev
163 * finish default texture case
165 * Revision 1.23 2002/05/30 22:02:30 theoddone33
168 * Revision 1.22 2002/05/30 21:44:48 relnev
169 * implemented some missing texture stuff.
171 * enable bitmap polys for opengl.
173 * work around greenness in bitmaps.
175 * Revision 1.21 2002/05/30 17:29:30 theoddone33
176 * Fix some more stubs, change at least one polygon winding since culling is now
179 * Revision 1.20 2002/05/30 16:50:24 theoddone33
180 * Keyboard partially fixed
182 * Revision 1.19 2002/05/30 08:13:14 relnev
185 * Revision 1.18 2002/05/29 23:37:36 relnev
188 * Revision 1.17 2002/05/29 23:17:49 theoddone33
189 * Non working text code and fixed keys
191 * Revision 1.16 2002/05/29 19:45:13 theoddone33
192 * More changes on texture loading
194 * Revision 1.15 2002/05/29 19:06:48 theoddone33
195 * Enable string printing. Enable texture mapping
197 * Revision 1.14 2002/05/29 08:54:40 relnev
198 * "fixed" bitmap drawing.
200 * copied more d3d code over.
202 * Revision 1.13 2002/05/29 06:25:13 theoddone33
203 * Keyboard input, mouse tracking now work
205 * Revision 1.12 2002/05/29 04:52:45 relnev
208 * Revision 1.11 2002/05/29 04:29:56 relnev
209 * removed some unncessary stubbing, implemented opengl rect
211 * Revision 1.10 2002/05/29 04:13:27 theoddone33
214 * Revision 1.9 2002/05/29 03:35:51 relnev
217 * Revision 1.8 2002/05/29 03:30:05 relnev
218 * update opengl stubs
220 * Revision 1.7 2002/05/29 02:52:32 theoddone33
221 * Enable OpenGL renderer
223 * Revision 1.6 2002/05/28 04:56:51 theoddone33
224 * runs a little bit now
226 * Revision 1.5 2002/05/28 04:07:28 theoddone33
227 * New graphics stubbing arrangement
229 * Revision 1.4 2002/05/27 23:39:34 relnev
232 * Revision 1.3 2002/05/27 22:35:01 theoddone33
235 * Revision 1.2 2002/05/27 22:32:02 theoddone33
236 * throw all d3d stuff at opengl
238 * Revision 1.1.1.1 2002/05/03 03:28:09 root
242 * 10 7/14/99 9:42a Dave
243 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
246 * 9 7/09/99 9:51a Dave
247 * Added thick polyline code.
249 * 8 6/29/99 10:35a Dave
250 * Interface polygon bitmaps! Whee!
252 * 7 2/03/99 11:44a Dave
253 * Fixed d3d transparent textures.
255 * 6 1/24/99 11:37p Dave
256 * First full rev of beam weapons. Very customizable. Removed some bogus
257 * Int3()'s in low level net code.
259 * 5 12/18/98 1:13a Dave
260 * Rough 1024x768 support for Direct3D. Proper detection and usage through
263 * 4 12/06/98 2:36p Dave
264 * Drastically improved nebula fogging.
266 * 3 11/11/98 5:37p Dave
267 * Checkin for multiplayer testing.
269 * 2 10/07/98 10:53a Dave
272 * 1 10/07/98 10:49a Dave
274 * 14 5/20/98 9:46p John
275 * added code so the places in code that change half the palette don't
276 * have to clear the screen.
278 * 13 5/06/98 5:30p John
279 * Removed unused cfilearchiver. Removed/replaced some unused/little used
280 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
281 * DirectX header files and libs that fixed the Direct3D alpha blending
284 * 12 4/14/98 12:15p John
285 * Made 16-bpp movies work.
287 * 11 3/12/98 5:36p John
288 * Took out any unused shaders. Made shader code take rgbc instead of
289 * matrix and vector since noone used it like a matrix and it would have
290 * been impossible to do in hardware. Made Glide implement a basic
291 * shader for online help.
293 * 10 3/10/98 4:18p John
294 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
295 * & Glide have popups and print screen. Took out all >8bpp software
296 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
297 * support Fred. Made zbuffering key off of functions rather than one
300 * 9 12/02/97 4:00p John
301 * Added first rev of thruster glow, along with variable levels of
302 * translucency, which retquired some restructing of palman.
304 * 8 10/03/97 9:10a John
305 * added better antialiased line drawer
307 * 7 9/23/97 10:45a John
308 * made so you can tell bitblt code to rle a bitmap by passing flag to
311 * 6 9/09/97 11:01a Sandeep
312 * fixed warning level 4 bugs
314 * 5 7/10/97 2:06p John
315 * added code to specify alphablending type for bitmaps.
317 * 4 6/17/97 7:04p John
318 * added d3d support for gradients.
319 * fixed some color bugs by adding screen signatures instead of watching
320 * flags and palette changes.
322 * 3 6/12/97 2:50a Lawrance
323 * bm_unlock() now passed bitmap number, not pointer
325 * 2 6/11/97 1:12p John
326 * Started fixing all the text colors in the game.
328 * 1 5/12/97 12:14p John
335 #include <windowsx.h>
349 #include "floating.h"
351 #include "systemvars.h"
352 #include "grinternal.h"
353 #include "gropengl.h"
357 #include "osregistry.h"
360 static int Inited = 0;
362 typedef enum gr_texture_source {
364 TEXTURE_SOURCE_DECAL,
365 TEXTURE_SOURCE_NO_FILTERING,
368 typedef enum gr_alpha_blend {
369 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
370 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
371 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
372 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
373 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
376 typedef enum gr_zbuffer_type {
383 #define NEBULA_COLORS 20
385 volatile int GL_activate = 0;
386 volatile int GL_deactivate = 0;
388 static int GL_use_luminance_alpha;
390 static char *Gr_saved_screen = NULL;
391 static int Gr_saved_screen_bitmap;
394 // Throw in some dummy functions - DDOI
396 int D3D_32bit = 0; // grd3d.cpp
397 int D3D_fog_mode = -1; // grd3d.cpp
398 int D3D_inited = 0; // grd3d.cpp
399 int D3D_zbias = 1; // grd3d.cpp
400 int D3d_rendition_uvs = 0; // grd3d.cpp
407 void d3d_zbias (int a)
413 static void gr_opengl_set_texture_state(gr_texture_source ts);
415 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
416 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
418 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
420 gr_opengl_set_texture_state(ts);
422 if (ab != GL_current_alpha_blend) {
424 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
425 glBlendFunc(GL_ONE, GL_ZERO);
427 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
428 glBlendFunc(GL_ONE, GL_ONE);
430 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
431 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
433 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
434 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
436 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
437 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
443 GL_current_alpha_blend = ab;
446 if (zt != GL_current_zbuffer_type) {
448 case ZBUFFER_TYPE_NONE:
449 glDepthFunc(GL_ALWAYS);
450 glDepthMask(GL_FALSE);
452 case ZBUFFER_TYPE_READ:
453 glDepthFunc(GL_LESS);
454 glDepthMask(GL_FALSE);
456 case ZBUFFER_TYPE_WRITE:
457 glDepthFunc(GL_ALWAYS);
458 glDepthMask(GL_TRUE);
460 case ZBUFFER_TYPE_FULL:
461 glDepthFunc(GL_LESS);
462 glDepthMask(GL_TRUE);
468 GL_current_zbuffer_type = zt;
472 void gr_opengl_activate(int active)
477 // don't grab key/mouse if cmdline says so or if we're fullscreen
478 if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
479 SDL_WM_GrabInput(SDL_GRAB_ON);
484 // let go of mouse/keyboard
485 SDL_WM_GrabInput(SDL_GRAB_OFF);
490 static void opengl_tcache_flush ();
492 void gr_opengl_preload_init()
494 if (gr_screen.mode != GR_OPENGL) {
498 opengl_tcache_flush ();
501 int GL_should_preload = 0;
502 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
504 if ( gr_screen.mode != GR_OPENGL) {
508 if ( !GL_should_preload ) {
512 float u_scale, v_scale;
516 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
518 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
522 mprintf(("Texture upload failed!\n" ));
528 void gr_opengl_pixel(int x, int y)
533 void gr_opengl_clear()
535 glClearColor(gr_screen.current_clear_color.red / 255.0,
536 gr_screen.current_clear_color.green / 255.0,
537 gr_screen.current_clear_color.blue / 255.0, 1.0);
539 glClear ( GL_COLOR_BUFFER_BIT );
542 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
543 static void opengl_tcache_frame ();
545 void gr_opengl_flip()
553 extern int Gr_opengl_mouse_saved;
554 Gr_opengl_mouse_saved = 0;
556 if ( mouse_is_visible() ) {
560 mouse_get_pos( &mx, &my );
562 gr_opengl_save_mouse_area(mx,my,32,32);
564 if ( Gr_cursor == -1 ) {
567 gr_set_bitmap(Gr_cursor);
573 GLenum error = glGetError();
576 error = glGetError();
578 if (error != GL_NO_ERROR) {
579 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
582 } while (error != GL_NO_ERROR);
585 SDL_GL_SwapBuffers ();
587 opengl_tcache_frame ();
589 int cnt = GL_activate;
592 opengl_tcache_flush();
593 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
599 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
603 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
608 void gr_opengl_set_clip(int x,int y,int w,int h)
610 // check for sanity of parameters
616 if (x >= gr_screen.max_w)
617 x = gr_screen.max_w - 1;
618 if (y >= gr_screen.max_h)
619 y = gr_screen.max_h - 1;
621 if (x + w > gr_screen.max_w)
622 w = gr_screen.max_w - x;
623 if (y + h > gr_screen.max_h)
624 h = gr_screen.max_h - y;
626 if (w > gr_screen.max_w)
628 if (h > gr_screen.max_h)
631 gr_screen.offset_x = x;
632 gr_screen.offset_y = y;
633 gr_screen.clip_left = 0;
634 gr_screen.clip_right = w-1;
635 gr_screen.clip_top = 0;
636 gr_screen.clip_bottom = h-1;
637 gr_screen.clip_width = w;
638 gr_screen.clip_height = h;
640 glEnable(GL_SCISSOR_TEST);
641 glScissor(x, gr_screen.max_h-y-h, w, h);
644 void gr_opengl_reset_clip()
646 gr_screen.offset_x = 0;
647 gr_screen.offset_y = 0;
648 gr_screen.clip_left = 0;
649 gr_screen.clip_top = 0;
650 gr_screen.clip_right = gr_screen.max_w - 1;
651 gr_screen.clip_bottom = gr_screen.max_h - 1;
652 gr_screen.clip_width = gr_screen.max_w;
653 gr_screen.clip_height = gr_screen.max_h;
655 glDisable(GL_SCISSOR_TEST);
656 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
659 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
661 gr_screen.current_alpha = alpha;
662 gr_screen.current_alphablend_mode = alphablend_mode;
663 gr_screen.current_bitblt_mode = bitblt_mode;
664 gr_screen.current_bitmap = bitmap_num;
666 gr_screen.current_bitmap_sx = sx;
667 gr_screen.current_bitmap_sy = sy;
670 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
672 shade->screen_sig = gr_screen.signature;
679 void gr_opengl_set_shader( shader * shade )
682 if (shade->screen_sig != gr_screen.signature) {
683 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
685 gr_screen.current_shader = *shade;
687 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
692 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
694 STUB_FUNCTION; /* who called me? */
697 void gr_opengl_bitmap(int x, int y)
699 STUB_FUNCTION; /* who called me? */
702 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
706 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
708 saved_zbuf = gr_zbuffer_get();
710 // start the frame, no zbuffering, no culling
712 gr_zbuffer_set(GR_ZBUFF_NONE);
721 v[0].flags = PF_PROJECTED;
728 v[1].sx = i2fl(x + w);
733 v[1].flags = PF_PROJECTED;
740 v[2].sx = i2fl(x + w);
741 v[2].sy = i2fl(y + h);
745 v[2].flags = PF_PROJECTED;
753 v[3].sy = i2fl(y + h);
757 v[3].flags = PF_PROJECTED;
765 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
769 // restore zbuffer and culling
770 gr_zbuffer_set(saved_zbuf);
774 void gr_opengl_rect(int x,int y,int w,int h)
776 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);
779 void gr_opengl_shade(int x,int y,int w,int h)
786 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
787 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
788 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
789 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
790 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
791 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
792 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
793 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
795 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
798 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
803 if ( !gr_screen.current_color.is_alphacolor ) return;
805 float u_scale, v_scale;
807 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
808 // Couldn't set texture
809 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
813 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
815 float u0, u1, v0, v1;
816 float x1, x2, y1, y2;
819 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
821 u0 = u_scale*i2fl(sx)/i2fl(bw);
822 v0 = v_scale*i2fl(sy)/i2fl(bh);
824 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
825 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
827 x1 = i2fl(x+gr_screen.offset_x);
828 y1 = i2fl(y+gr_screen.offset_y);
829 x2 = i2fl(x+w+gr_screen.offset_x);
830 y2 = i2fl(y+h+gr_screen.offset_y);
832 if ( gr_screen.current_color.is_alphacolor ) {
833 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
835 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
839 glTexCoord2f (u0, v1);
840 glVertex3f (x1, y2, -0.99);
842 glTexCoord2f (u1, v1);
843 glVertex3f (x2, y2, -0.99);
845 glTexCoord2f (u1, v0);
846 glVertex3f (x2, y1, -0.99);
848 glTexCoord2f (u0, v0);
849 glVertex3f (x1, y1, -0.99);
853 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
860 int dx1=x, dx2=x+w-1;
861 int dy1=y, dy2=y+h-1;
864 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
869 if ( count > 1 ) Int3();
873 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
874 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
875 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
876 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
877 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
878 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
905 if ( w < 1 ) return; // clipped away!
906 if ( h < 1 ) return; // clipped away!
910 // Make sure clipping algorithm works
914 Assert( w == (dx2-dx1+1) );
915 Assert( h == (dy2-dy1+1) );
918 Assert( sx+w <= bw );
919 Assert( sy+h <= bh );
920 Assert( dx2 >= dx1 );
921 Assert( dy2 >= dy1 );
922 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
923 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
924 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
925 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
928 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
929 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
932 void gr_opengl_aabitmap(int x, int y)
936 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
937 int dx1=x, dx2=x+w-1;
938 int dy1=y, dy2=y+h-1;
941 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
942 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
943 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
944 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
945 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
946 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
948 if ( sx < 0 ) return;
949 if ( sy < 0 ) return;
950 if ( sx >= w ) return;
951 if ( sy >= h ) return;
953 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
954 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
957 void gr_opengl_string( int sx, int sy, char *s )
959 int width, spacing, letter;
962 if ( !Current_font ) {
966 gr_set_bitmap(Current_font->bitmap_id);
971 if (sx==0x8000) { //centered
972 x = get_centered_x(s);
984 y += Current_font->h;
985 if (sx==0x8000) { //centered
986 x = get_centered_x(s);
993 letter = get_char_width(s[0],s[1],&width,&spacing);
996 //not in font, draw as space
1004 // Check if this character is totally clipped
1005 if ( x + width < gr_screen.clip_left ) continue;
1006 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1007 if ( x > gr_screen.clip_right ) continue;
1008 if ( y > gr_screen.clip_bottom ) continue;
1011 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1012 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1016 wc = width - xd; hc = Current_font->h - yd;
1017 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1018 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1020 if ( wc < 1 ) continue;
1021 if ( hc < 1 ) continue;
1023 int u = Current_font->bm_u[letter];
1024 int v = Current_font->bm_v[letter];
1026 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1030 void gr_opengl_line(int x1,int y1,int x2,int y2)
1032 int clipped = 0, swapped=0;
1034 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1036 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);
1041 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1042 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1043 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1044 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1046 if ( x1 == x2 && y1 == y2 ) {
1047 glBegin (GL_POINTS);
1048 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1049 glVertex3f (sx1, sy1, -0.99f);
1061 } else if ( y1 == y2 ) {
1070 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1071 glVertex3f (sx2, sy2, -0.99f);
1072 glVertex3f (sx1, sy1, -0.99f);
1076 void gr_opengl_aaline(vertex *v1, vertex *v2)
1078 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1081 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1083 int clipped = 0, swapped=0;
1085 if ( !gr_screen.current_color.is_alphacolor ) {
1086 gr_line( x1, y1, x2, y2 );
1090 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);
1092 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1094 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1095 int ba = swapped ? gr_screen.current_color.alpha : 0;
1100 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1101 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1102 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1103 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1111 } else if ( y1 == y2 ) {
1120 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1121 glVertex3f (sx2, sy2, -0.99f);
1122 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1123 glVertex3f (sx1, sy1, -0.99f);
1127 void gr_opengl_circle( int xc, int yc, int d )
1137 if ( (xc+r) < gr_screen.clip_left ) return;
1138 if ( (xc-r) > gr_screen.clip_right ) return;
1139 if ( (yc+r) < gr_screen.clip_top ) return;
1140 if ( (yc-r) > gr_screen.clip_bottom ) return;
1143 // Draw the first octant
1144 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1145 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1150 // Draw the second octant
1151 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1152 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1160 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1161 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1166 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1170 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1171 if (f_float < 0.0f) {
1179 *a = (int)(f_float * 255.0);
1182 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1185 float u_scale = 1.0f, v_scale = 1.0f;
1187 // Make nebula use the texture mapper... this blends the colors better.
1188 if ( flags & TMAP_FLAG_NEBULA ){
1192 gr_texture_source texture_source = (gr_texture_source)-1;
1193 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1194 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1196 if ( gr_zbuffering ) {
1197 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1198 zbuffer_type = ZBUFFER_TYPE_READ;
1200 zbuffer_type = ZBUFFER_TYPE_FULL;
1203 zbuffer_type = ZBUFFER_TYPE_NONE;
1208 int tmap_type = TCACHE_TYPE_NORMAL;
1212 if ( flags & TMAP_FLAG_TEXTURED ) {
1215 r = gr_screen.current_color.red;
1216 g = gr_screen.current_color.green;
1217 b = gr_screen.current_color.blue;
1220 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1223 tmap_type = TCACHE_TYPE_NORMAL;
1224 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1226 // Blend with screen pixel using src*alpha+dst
1227 float factor = gr_screen.current_alpha;
1231 if ( factor <= 1.0f ) {
1232 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1233 r = (r*tmp_alpha)/255;
1234 g = (g*tmp_alpha)/255;
1235 b = (b*tmp_alpha)/255;
1238 tmap_type = TCACHE_TYPE_XPARENT;
1240 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1242 // Blend with screen pixel using src*alpha+dst
1243 float factor = gr_screen.current_alpha;
1245 if ( factor > 1.0f ) {
1248 alpha = fl2i(gr_screen.current_alpha*255.0f);
1252 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1253 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1255 alpha_blend = ALPHA_BLEND_NONE;
1260 if(flags & TMAP_FLAG_BITMAP_SECTION){
1261 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1264 texture_source = TEXTURE_SOURCE_NONE;
1266 if ( flags & TMAP_FLAG_TEXTURED ) {
1267 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 ))
1269 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1273 // use nonfiltered textures for bitmap sections
1274 if(flags & TMAP_FLAG_BITMAP_SECTION){
1275 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1277 texture_source = TEXTURE_SOURCE_DECAL;
1282 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1284 if ( flags & TMAP_FLAG_TEXTURED )
1290 if (flags & TMAP_FLAG_PIXEL_FOG) {
1296 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1298 vertex * va = verts[i];
1302 x = fl2i(va->sx*16.0f);
1303 y = fl2i(va->sy*16.0f);
1305 x += gr_screen.offset_x*16;
1306 y += gr_screen.offset_y*16;
1308 sx = i2fl(x) / 16.0f;
1309 sy = i2fl(y) / 16.0f;
1311 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1322 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1325 glBegin(GL_TRIANGLE_FAN);
1326 for (i = nv-1; i >= 0; i--) {
1327 vertex * va = verts[i];
1333 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1334 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1343 if ( flags & TMAP_FLAG_CORRECT ) {
1349 if (flags & TMAP_FLAG_ALPHA) {
1355 if (flags & TMAP_FLAG_NEBULA ) {
1356 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1357 r = gr_palette[pal*3+0];
1358 g = gr_palette[pal*3+1];
1359 b = gr_palette[pal*3+2];
1360 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1361 r = Gr_gamma_lookup[verts[i]->b];
1362 g = Gr_gamma_lookup[verts[i]->b];
1363 b = Gr_gamma_lookup[verts[i]->b];
1364 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1365 // Make 0.75 be 256.0f
1366 r = Gr_gamma_lookup[verts[i]->r];
1367 g = Gr_gamma_lookup[verts[i]->g];
1368 b = Gr_gamma_lookup[verts[i]->b];
1370 // use constant RGB values...
1372 glColor4ub (r,g,b,a);
1374 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1377 /* this is for GL_EXT_SECONDARY_COLOR */
1378 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1379 /* do separate color call here */
1385 x = fl2i(va->sx*16.0f);
1386 y = fl2i(va->sy*16.0f);
1388 x += gr_screen.offset_x*16;
1389 y += gr_screen.offset_y*16;
1391 sx = i2fl(x) / 16.0f;
1392 sy = i2fl(y) / 16.0f;
1394 if ( flags & TMAP_FLAG_TEXTURED ) {
1397 glTexCoord2f(tu, tv);
1400 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1405 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1407 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1410 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1412 void gr_opengl_scaler(vertex *va, vertex *vb )
1414 float x0, y0, x1, y1;
1415 float u0, v0, u1, v1;
1416 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1417 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1418 float xmin, xmax, ymin, ymax;
1419 int dx0, dy0, dx1, dy1;
1421 //============= CLIP IT =====================
1423 x0 = va->sx; y0 = va->sy;
1424 x1 = vb->sx; y1 = vb->sy;
1426 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1427 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1429 u0 = va->u; v0 = va->v;
1430 u1 = vb->u; v1 = vb->v;
1432 // Check for obviously offscreen bitmaps...
1433 if ( (y1<=y0) || (x1<=x0) ) return;
1434 if ( (x1<xmin ) || (x0>xmax) ) return;
1435 if ( (y1<ymin ) || (y0>ymax) ) return;
1437 clipped_u0 = u0; clipped_v0 = v0;
1438 clipped_u1 = u1; clipped_v1 = v1;
1440 clipped_x0 = x0; clipped_y0 = y0;
1441 clipped_x1 = x1; clipped_y1 = y1;
1443 // Clip the left, moving u0 right as necessary
1445 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1449 // Clip the right, moving u1 left as necessary
1451 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1455 // Clip the top, moving v0 down as necessary
1457 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1461 // Clip the bottom, moving v1 up as necessary
1463 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1467 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1468 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1470 if (dx1<=dx0) return;
1471 if (dy1<=dy0) return;
1473 //============= DRAW IT =====================
1487 v[1].sx = clipped_x1;
1488 v[1].sy = clipped_y0;
1491 v[1].u = clipped_u1;
1492 v[1].v = clipped_v0;
1495 v[2].sx = clipped_x1;
1496 v[2].sy = clipped_y1;
1499 v[2].u = clipped_u1;
1500 v[2].v = clipped_v1;
1503 v[3].sx = clipped_x0;
1504 v[3].sy = clipped_y1;
1507 v[3].u = clipped_u0;
1508 v[3].v = clipped_v1;
1510 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1513 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1517 void gr_opengl_get_color( int * r, int * g, int * b )
1519 if (r) *r = gr_screen.current_color.red;
1520 if (g) *g = gr_screen.current_color.green;
1521 if (b) *b = gr_screen.current_color.blue;
1524 void gr_opengl_init_color(color *c, int r, int g, int b)
1526 c->screen_sig = gr_screen.signature;
1527 c->red = (unsigned char)r;
1528 c->green = (unsigned char)g;
1529 c->blue = (unsigned char)b;
1531 c->ac_type = AC_TYPE_NONE;
1533 c->is_alphacolor = 0;
1537 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1539 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1540 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1541 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1542 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1544 gr_opengl_init_color( clr, r, g, b );
1546 clr->alpha = (unsigned char)alpha;
1547 clr->ac_type = (ubyte)type;
1548 clr->alphacolor = -1;
1549 clr->is_alphacolor = 1;
1552 void gr_opengl_set_color( int r, int g, int b )
1554 Assert((r >= 0) && (r < 256));
1555 Assert((g >= 0) && (g < 256));
1556 Assert((b >= 0) && (b < 256));
1558 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1561 void gr_opengl_set_color_fast(color *dst)
1563 if ( dst->screen_sig != gr_screen.signature ) {
1564 if ( dst->is_alphacolor ) {
1565 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1567 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1570 gr_screen.current_color = *dst;
1573 void gr_opengl_print_screen(char *filename)
1578 int gr_opengl_supports_res_ingame(int res)
1585 int gr_opengl_supports_res_interface(int res)
1592 static void opengl_tcache_cleanup ();
1594 void gr_opengl_cleanup()
1596 if ( !Inited ) return;
1603 opengl_tcache_cleanup ();
1608 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1610 Assert((r >= 0) && (r < 256));
1611 Assert((g >= 0) && (g < 256));
1612 Assert((b >= 0) && (b < 256));
1614 if (fog_mode == GR_FOGMODE_NONE) {
1615 if (gr_screen.current_fog_mode != fog_mode) {
1618 gr_screen.current_fog_mode = fog_mode;
1623 if (gr_screen.current_fog_mode != fog_mode) {
1626 if (D3D_fog_mode == 2) {
1627 glFogi(GL_FOG_MODE, GL_LINEAR);
1630 gr_screen.current_fog_mode = fog_mode;
1633 if ( (gr_screen.current_fog_color.red != r) ||
1634 (gr_screen.current_fog_color.green != g) ||
1635 (gr_screen.current_fog_color.blue != b) ) {
1638 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1640 fc[0] = (float)r/255.0;
1641 fc[1] = (float)g/255.0;
1642 fc[2] = (float)b/255.0;
1645 glFogfv(GL_FOG_COLOR, fc);
1648 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1649 ((fog_near != gr_screen.fog_near) ||
1650 (fog_far != gr_screen.fog_far)) ) {
1651 gr_screen.fog_near = fog_near;
1652 gr_screen.fog_far = fog_far;
1654 if (D3D_fog_mode == 2) {
1655 glFogf(GL_FOG_START, fog_near);
1656 glFogf(GL_FOG_END, fog_far);
1661 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1666 void gr_opengl_set_cull(int cull)
1669 glEnable (GL_CULL_FACE);
1670 glFrontFace (GL_CCW);
1672 glDisable (GL_CULL_FACE);
1676 void gr_opengl_filter_set(int filter)
1681 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1684 gr_set_bitmap(bmap1);
1687 gr_set_bitmap(bmap2);
1693 typedef struct tcache_slot_opengl {
1694 GLuint texture_handle;
1695 float u_scale, v_scale;
1698 int used_this_frame;
1703 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1704 tcache_slot_opengl *parent;
1706 gr_texture_source texture_mode;
1707 } tcache_slot_opengl;
1709 static void *Texture_sections = NULL;
1710 static tcache_slot_opengl *Textures = NULL;
1712 static tcache_slot_opengl *GL_bound_texture;
1714 int GL_texture_sections = 0;
1715 int GL_texture_ram = 0;
1716 int GL_frame_count = 0;
1717 int GL_min_texture_width = 0;
1718 int GL_max_texture_width = 0;
1719 int GL_min_texture_height = 0;
1720 int GL_max_texture_height = 0;
1721 int GL_square_textures = 0;
1722 int GL_textures_in = 0;
1723 int GL_textures_in_frame = 0;
1724 int GL_last_bitmap_id = -1;
1725 int GL_last_detail = -1;
1726 int GL_last_bitmap_type = -1;
1727 int GL_last_section_x = -1;
1728 int GL_last_section_y = -1;
1732 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1734 static void gr_opengl_set_texture_state(gr_texture_source ts)
1736 if (ts == TEXTURE_SOURCE_NONE) {
1737 GL_bound_texture = NULL;
1739 glBindTexture(GL_TEXTURE_2D, 0);
1740 gr_tcache_set(-1, -1, NULL, NULL );
1741 } else if (GL_bound_texture &&
1742 GL_bound_texture->texture_mode != ts) {
1744 case TEXTURE_SOURCE_DECAL:
1745 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1746 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1748 case TEXTURE_SOURCE_NO_FILTERING:
1749 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1750 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1756 GL_bound_texture->texture_mode = ts;
1759 GL_current_texture_source = ts;
1762 static int opengl_max_tex_size_get()
1764 GLint max_texture_size = 0;
1766 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1767 return max_texture_size;
1770 static void opengl_tcache_init (int use_sections)
1774 // DDOI - FIXME skipped a lot of stuff here
1775 GL_should_preload = 0;
1777 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1780 if ( tmp_pl == 0 ) {
1781 GL_should_preload = 0;
1782 } else if ( tmp_pl == 1 ) {
1783 GL_should_preload = 1;
1785 GL_should_preload = 1;
1790 GL_min_texture_width = 16;
1791 GL_min_texture_height = 16;
1793 GL_max_texture_width = opengl_max_tex_size_get();
1794 GL_max_texture_height = opengl_max_tex_size_get();
1796 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1802 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1803 if(!Texture_sections){
1806 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1809 // Init the texture structures
1810 int section_count = 0;
1811 for( i=0; i<MAX_BITMAPS; i++ ) {
1813 Textures[i].vram_texture = NULL;
1814 Textures[i].vram_texture_surface = NULL;
1816 Textures[i].texture_handle = 0;
1818 Textures[i].bitmap_id = -1;
1819 Textures[i].size = 0;
1820 Textures[i].used_this_frame = 0;
1822 Textures[i].parent = NULL;
1824 // allocate sections
1826 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1827 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1828 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1829 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1831 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1832 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1834 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1835 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1836 Textures[i].data_sections[idx][s_idx]->size = 0;
1837 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1841 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1842 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1843 Textures[i].data_sections[idx][s_idx] = NULL;
1849 GL_texture_sections = use_sections;
1851 GL_last_detail = Detail.hardware_textures;
1852 GL_last_bitmap_id = -1;
1853 GL_last_bitmap_type = -1;
1855 GL_last_section_x = -1;
1856 GL_last_section_y = -1;
1859 GL_textures_in_frame = 0;
1862 static int opengl_free_texture (tcache_slot_opengl *t);
1864 void opengl_free_texture_with_handle(int handle)
1866 for(int i=0; i<MAX_BITMAPS; i++ ) {
1867 if (Textures[i].bitmap_id == handle) {
1868 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1869 opengl_free_texture ( &Textures[i] );
1874 static void opengl_tcache_flush ()
1878 for( i=0; i<MAX_BITMAPS; i++ ) {
1879 opengl_free_texture ( &Textures[i] );
1881 if (GL_textures_in != 0) {
1882 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1886 GL_last_bitmap_id = -1;
1887 GL_last_section_x = -1;
1888 GL_last_section_y = -1;
1891 static void opengl_tcache_cleanup ()
1893 opengl_tcache_flush ();
1896 GL_textures_in_frame = 0;
1903 if( Texture_sections != NULL ){
1904 free(Texture_sections);
1905 Texture_sections = NULL;
1909 static void opengl_tcache_frame ()
1911 GL_last_bitmap_id = -1;
1912 GL_textures_in_frame = 0;
1919 for( i=0; i<MAX_BITMAPS; i++ ) {
1920 Textures[i].used_this_frame = 0;
1923 if(Textures[i].data_sections[0][0] != NULL){
1924 Assert(GL_texture_sections);
1925 if(GL_texture_sections){
1926 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1927 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1928 if(Textures[i].data_sections[idx][s_idx] != NULL){
1929 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1939 opengl_tcache_flush();
1944 static int opengl_free_texture ( tcache_slot_opengl *t )
1950 if ( t->bitmap_id > -1 ) {
1951 // if I, or any of my children have been used this frame, bail
1952 if(t->used_this_frame == GL_frame_count){
1955 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1956 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1957 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
1963 // ok, now we know its legal to free everything safely
1964 t->texture_mode = (gr_texture_source) -1;
1965 glDeleteTextures (1, &t->texture_handle);
1966 t->texture_handle = 0;
1968 if ( GL_last_bitmap_id == t->bitmap_id ) {
1969 GL_last_bitmap_id = -1;
1972 // if this guy has children, free them too, since the children
1973 // actually make up his size
1974 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1975 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1976 if(t->data_sections[idx][s_idx] != NULL){
1977 opengl_free_texture(t->data_sections[idx][s_idx]);
1983 t->used_this_frame = 0;
1984 GL_textures_in -= t->size;
1990 static void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1996 if((w_out == NULL) || (h_out == NULL)){
2004 // set height and width to a power of 2
2005 for (i=0; i<16; i++ ) {
2006 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2012 for (i=0; i<16; i++ ) {
2013 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2019 // try to keep an 8:1 size ratio
2020 if (tex_w/tex_h > 8)
2022 if (tex_h/tex_w > 8)
2025 if ( tex_w < GL_min_texture_width ) {
2026 tex_w = GL_min_texture_width;
2027 } else if ( tex_w > GL_max_texture_width ) {
2028 tex_w = GL_max_texture_width;
2031 if ( tex_h < GL_min_texture_height ) {
2032 tex_h = GL_min_texture_height;
2033 } else if ( tex_h > GL_max_texture_height ) {
2034 tex_h = GL_max_texture_height;
2037 if ( GL_square_textures) {
2039 // Make the both be equal to larger of the two
2040 new_size = max(tex_w, tex_h);
2045 // store the outgoing size
2050 // data == start of bitmap data
2051 // sx == x offset into bitmap
2052 // sy == y offset into bitmap
2053 // src_w == absolute width of section on source bitmap
2054 // src_h == absolute height of section on source bitmap
2055 // bmap_w == width of source bitmap
2056 // bmap_h == height of source bitmap
2057 // tex_w == width of final texture
2058 // tex_h == height of final texture
2059 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)
2069 if ( t->used_this_frame == GL_frame_count ) {
2070 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2075 if(!opengl_free_texture(t)){
2080 // get final texture size
2081 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2083 if ( (tex_w < 1) || (tex_h < 1) ) {
2084 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2088 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2089 t->u_scale = (float)bmap_w / (float)tex_w;
2090 t->v_scale = (float)bmap_h / (float)tex_h;
2091 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2092 t->u_scale = (float)src_w / (float)tex_w;
2093 t->v_scale = (float)src_h / (float)tex_h;
2100 glGenTextures (1, &t->texture_handle);
2103 if (t->texture_handle == 0) {
2104 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2108 GL_bound_texture = t;
2110 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2112 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2114 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2115 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2116 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2118 /* this should be set next anyway */
2119 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2120 // gr_opengl_set_texture_state(GL_current_texture_source);
2125 switch (bitmap_type) {
2127 case TCACHE_TYPE_AABITMAP:
2130 ubyte *bmp_data = ((ubyte*)data);
2136 for (i=0; i<16; i++) {
2137 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2140 for ( ; i<256; i++ ) {
2144 texmem = (ubyte *) malloc (tex_w*tex_h);
2147 for (i=0;i<tex_h;i++)
2149 for (j=0;j<tex_w;j++)
2151 if (i < bmap_h && j < bmap_w) {
2152 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2161 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2167 case TCACHE_TYPE_BITMAP_SECTION:
2170 ubyte *bmp_data = ((ubyte*)data);
2171 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2172 ubyte *texmemp = texmem;
2174 for (i=0;i<tex_h;i++)
2176 for (j=0;j<tex_w;j++)
2178 if (i < src_h && j < src_w) {
2179 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2180 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2188 size = tex_w*tex_h*2;
2190 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA,
2191 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2199 ubyte *bmp_data = ((ubyte*)data);
2200 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2201 ubyte *texmemp = texmem;
2203 fix u, utmp, v, du, dv;
2207 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2208 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2210 for (j=0;j<tex_h;j++)
2213 for (i=0;i<tex_w;i++)
2215 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2216 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2222 size = tex_w*tex_h*2;
2224 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA,
2225 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2233 t->bitmap_id = texture_handle;
2234 t->time_created = GL_frame_count;
2235 t->used_this_frame = 0;
2237 t->w = (ushort)tex_w;
2238 t->h = (ushort)tex_h;
2240 GL_textures_in_frame += t->size;
2242 GL_textures_in += t->size;
2248 static int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2252 int final_w, final_h;
2256 // setup texture/bitmap flags
2258 switch(bitmap_type){
2259 case TCACHE_TYPE_AABITMAP:
2260 flags |= BMP_AABITMAP;
2263 case TCACHE_TYPE_NORMAL:
2264 flags |= BMP_TEX_OTHER;
2265 case TCACHE_TYPE_XPARENT:
2266 flags |= BMP_TEX_XPARENT;
2268 case TCACHE_TYPE_NONDARKENING:
2270 flags |= BMP_TEX_NONDARK;
2274 // lock the bitmap into the proper format
2275 bmp = bm_lock(bitmap_handle, bpp, flags);
2276 if ( bmp == NULL ) {
2277 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2286 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2287 // max_w /= D3D_texture_divider;
2288 // max_h /= D3D_texture_divider;
2290 // Detail.debris_culling goes from 0 to 4.
2291 max_w /= (16 >> Detail.hardware_textures);
2292 max_h /= (16 >> Detail.hardware_textures);
2296 // get final texture size as it will be allocated as a DD surface
2297 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2299 // if this tcache slot has no bitmap
2300 if ( tslot->bitmap_id < 0) {
2303 // different bitmap altogether - determine if the new one can use the old one's slot
2304 else if (tslot->bitmap_id != bitmap_handle) {
2305 if((final_w == tslot->w) && (final_h == tslot->h)){
2307 //ml_printf("Reloading texture %d\n", bitmap_handle);
2314 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);
2316 // unlock the bitmap
2317 bm_unlock(bitmap_handle);
2322 static int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2326 int final_w, final_h;
2327 int section_x, section_y;
2330 // setup texture/bitmap flags
2331 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2332 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2333 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2335 flags = BMP_TEX_XPARENT;
2337 // lock the bitmap in the proper format
2338 bmp = bm_lock(bitmap_handle, 16, flags);
2339 if ( bmp == NULL ) {
2340 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2343 // determine the width and height of this section
2344 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2346 // get final texture size as it will be allocated as an opengl texture
2347 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2349 // if this tcache slot has no bitmap
2350 if ( tslot->bitmap_id < 0) {
2353 // different bitmap altogether - determine if the new one can use the old one's slot
2354 else if (tslot->bitmap_id != bitmap_handle) {
2355 if((final_w == tslot->w) && (final_h == tslot->h)){
2363 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);
2365 // unlock the bitmap
2366 bm_unlock(bitmap_handle);
2372 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2373 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)
2382 GL_last_bitmap_id = -1;
2386 if ( GL_last_detail != Detail.hardware_textures ) {
2387 GL_last_detail = Detail.hardware_textures;
2388 opengl_tcache_flush();
2395 int n = bm_get_cache_slot (bitmap_id, 1);
2396 tcache_slot_opengl *t = &Textures[n];
2398 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)) {
2399 t->used_this_frame = GL_frame_count;
2401 // mark all children as used
2402 if(GL_texture_sections){
2403 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2404 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2405 if(t->data_sections[idx][s_idx] != NULL){
2406 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2412 *u_scale = t->u_scale;
2413 *v_scale = t->v_scale;
2417 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2418 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2419 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2425 // if the texture sections haven't been created yet
2426 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2428 // lock the bitmap in the proper format
2429 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2430 bm_unlock(bitmap_id);
2432 // now lets do something for each texture
2434 for(idx=0; idx<bmp->sections.num_x; idx++){
2435 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2436 // hmm. i'd rather we didn't have to do it this way...
2437 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2441 // not used this frame
2442 t->data_sections[idx][s_idx]->used_this_frame = 0;
2446 // zero out pretty much everything in the parent struct since he's just the root
2447 t->bitmap_id = bitmap_id;
2448 t->texture_handle = 0;
2449 t->time_created = t->data_sections[sx][sy]->time_created;
2450 t->used_this_frame = 0;
2452 t->vram_texture = NULL;
2453 t->vram_texture_surface = NULL
2457 // argh. we failed to upload. free anything we can
2459 opengl_free_texture(t);
2461 // swap in the texture we want
2463 t = t->data_sections[sx][sy];
2466 // all other "normal" textures
2467 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2468 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2471 // everything went ok
2472 if(ret_val && (t->texture_handle) && !vram_full){
2473 *u_scale = t->u_scale;
2474 *v_scale = t->v_scale;
2476 GL_bound_texture = t;
2478 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2480 /* this should be set next anyway */
2481 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2482 // gr_opengl_set_texture_state(GL_current_texture_source);
2485 GL_last_bitmap_id = t->bitmap_id;
2486 GL_last_bitmap_type = bitmap_type;
2487 GL_last_section_x = sx;
2488 GL_last_section_y = sy;
2490 t->used_this_frame = GL_frame_count;
2494 GL_last_bitmap_id = -1;
2495 GL_last_bitmap_type = -1;
2497 GL_last_section_x = -1;
2498 GL_last_section_y = -1;
2500 GL_bound_texture = NULL;
2502 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2509 void gr_opengl_set_clear_color(int r, int g, int b)
2511 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2514 void gr_opengl_flash(int r, int g, int b)
2520 if ( r || g || b ) {
2521 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2523 float x1, x2, y1, y2;
2524 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2525 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2526 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2527 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2529 glColor4ub(r, g, b, 255);
2531 glVertex3f (x1, y2, -0.99);
2533 glVertex3f (x2, y2, -0.99);
2535 glVertex3f (x2, y1, -0.99);
2537 glVertex3f (x1, y1, -0.99);
2542 int gr_opengl_zbuffer_get()
2544 if ( !gr_global_zbuffering ) {
2545 return GR_ZBUFF_NONE;
2547 return gr_zbuffering_mode;
2550 int gr_opengl_zbuffer_set(int mode)
2552 int tmp = gr_zbuffering_mode;
2554 gr_zbuffering_mode = mode;
2556 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2564 void gr_opengl_zbuffer_clear(int mode)
2568 gr_zbuffering_mode = GR_ZBUFF_FULL;
2569 gr_global_zbuffering = 1;
2571 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2572 glClear ( GL_DEPTH_BUFFER_BIT );
2575 gr_zbuffering_mode = GR_ZBUFF_NONE;
2576 gr_global_zbuffering = 0;
2580 void gr_opengl_set_gamma(float gamma)
2583 Gr_gamma_int = int (Gr_gamma*10);
2585 // Create the Gamma lookup table
2587 for (i=0;i<256; i++) {
2588 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2591 } else if ( v < 0 ) {
2594 Gr_gamma_lookup[i] = v;
2597 // Flush any existing textures
2598 opengl_tcache_flush();
2601 void gr_opengl_fade_in(int instantaneous)
2606 void gr_opengl_fade_out(int instantaneous)
2611 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2614 glReadBuffer(GL_FRONT);
2616 glReadBuffer(GL_BACK);
2619 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2621 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2623 if (gr_screen.bits_per_pixel == 15) {
2624 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2625 } else if (gr_screen.bits_per_pixel == 32) {
2626 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2629 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2632 static int Gr_opengl_mouse_saved = 0;
2633 static int Gr_opengl_mouse_saved_x1 = 0;
2634 static int Gr_opengl_mouse_saved_y1 = 0;
2635 static int Gr_opengl_mouse_saved_x2 = 0;
2636 static int Gr_opengl_mouse_saved_y2 = 0;
2637 static int Gr_opengl_mouse_saved_w = 0;
2638 static int Gr_opengl_mouse_saved_h = 0;
2639 #define MAX_SAVE_SIZE (32*32)
2640 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2642 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2644 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2646 Gr_opengl_mouse_saved_x1 = x;
2647 Gr_opengl_mouse_saved_y1 = y;
2648 Gr_opengl_mouse_saved_x2 = x+w-1;
2649 Gr_opengl_mouse_saved_y2 = y+h-1;
2651 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2652 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2653 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2654 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2656 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2657 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2659 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2660 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2662 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2664 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2666 glReadBuffer(GL_BACK);
2667 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);
2669 Gr_opengl_mouse_saved = 1;
2672 int gr_opengl_save_screen()
2676 if ( Gr_saved_screen ) {
2677 mprintf(( "Screen alread saved!\n" ));
2681 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2682 if (!Gr_saved_screen) {
2683 mprintf(( "Couldn't get memory for saved screen!\n" ));
2687 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2688 if (!Gr_saved_screen_tmp) {
2689 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2693 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2695 glReadBuffer(GL_FRONT);
2696 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);
2700 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2701 dptr = (ubyte *)Gr_saved_screen;
2702 for (int j = 0; j < gr_screen.max_h; j++) {
2703 sptr -= gr_screen.max_w*2;
2704 memcpy(dptr, sptr, gr_screen.max_w*2);
2705 dptr += gr_screen.max_w*2;
2708 free(Gr_saved_screen_tmp);
2710 if (Gr_opengl_mouse_saved) {
2711 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2712 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2713 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2714 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2717 dptr -= gr_screen.max_w*2;
2721 // this leaks texture handles, and the opengl doesn't currently
2722 // perform some sort of garbage collection, so a hack was added
2723 // to bmpman to make it free textures when released
2724 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2726 return Gr_saved_screen_bitmap;
2729 void gr_opengl_restore_screen(int id)
2733 if ( !Gr_saved_screen ) {
2738 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2740 gr_set_bitmap(Gr_saved_screen_bitmap);
2744 void gr_opengl_free_screen(int id)
2746 bm_release(Gr_saved_screen_bitmap);
2748 if ( Gr_saved_screen ) {
2749 free( Gr_saved_screen );
2750 Gr_saved_screen = NULL;
2754 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2759 void gr_opengl_dump_frame_stop()
2764 void gr_opengl_dump_frame()
2769 uint gr_opengl_lock()
2776 void gr_opengl_unlock()
2780 void opengl_zbias(int bias)
2783 glEnable(GL_POLYGON_OFFSET_FILL);
2784 glPolygonOffset(0.0, -bias);
2786 glDisable(GL_POLYGON_OFFSET_FILL);
2790 extern char *Osreg_title;
2791 void gr_opengl_init()
2794 gr_opengl_cleanup();
2798 mprintf(( "Initializing opengl graphics device...\n" ));
2802 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2804 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2808 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2809 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2810 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2811 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2812 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2814 int flags = SDL_OPENGL;
2816 if (!Cmdline_window && ( (os_config_read_uint( NULL, "Fullscreen", 1 ) == 1) || Cmdline_fullscreen ))
2817 flags |= SDL_FULLSCREEN;
2819 // grab mouse/key unless told otherwise, ignore when we are going fullscreen
2820 if ( !((flags & SDL_FULLSCREEN) || Cmdline_no_grab) ) {
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, NULL);
2833 /* might as well put this here */
2834 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2837 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2839 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2841 glMatrixMode(GL_PROJECTION);
2843 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2844 glMatrixMode(GL_MODELVIEW);
2847 glShadeModel(GL_SMOOTH);
2848 glEnable(GL_DITHER);
2849 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2850 glHint(GL_FOG_HINT, GL_NICEST);
2852 glEnable(GL_DEPTH_TEST);
2855 glEnable(GL_TEXTURE_2D);
2857 glDepthRange(0.0, 1.0);
2859 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2860 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2862 D3D_32bit = 1; // grd3d.cpp
2863 extern int D3D_enabled;
2866 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2867 1 = use secondary color ext
2868 2 = use opengl linear fog
2870 D3D_fog_mode = 2; // grd3d.cpp
2874 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2881 gr_screen.bits_per_pixel = 15;
2882 gr_screen.bytes_per_pixel = 2;
2887 Gr_red.mask = 0x7C00;
2892 Gr_green.mask = 0x3E0;
2897 Gr_blue.mask = 0x1F;
2902 gr_screen.bits_per_pixel = 16;
2903 gr_screen.bytes_per_pixel = 2;
2908 Gr_red.mask = 0xF800;
2913 Gr_green.mask = 0x7E0;
2918 Gr_blue.mask = 0x1F;
2923 gr_screen.bits_per_pixel = 32;
2924 gr_screen.bytes_per_pixel = 4;
2929 Gr_red.mask = 0xff0000;
2934 Gr_green.mask = 0xff00;
2939 Gr_blue.mask = 0xff;
2944 Int3(); // Illegal bpp
2947 // DDOI - set these so no one else does!
2948 Gr_t_red.mask = Gr_red.mask;
2949 Gr_t_red.shift = Gr_red.shift;
2950 Gr_t_red.scale = Gr_red.scale;
2952 Gr_t_green.mask = Gr_green.mask;
2953 Gr_t_green.shift = Gr_green.shift;
2954 Gr_t_green.scale = Gr_green.scale;
2956 Gr_t_blue.mask = Gr_blue.mask;
2957 Gr_t_blue.shift = Gr_blue.shift;
2958 Gr_t_blue.scale = Gr_blue.scale;
2960 Gr_t_alpha.mask = 0x8000;
2961 Gr_t_alpha.scale = 255;
2962 Gr_t_alpha.shift = 15;
2964 Gr_ta_red.mask = 0x0f00;
2965 Gr_ta_red.shift = 8;
2966 Gr_ta_red.scale = 16;
2968 Gr_ta_green.mask = 0x00f0;
2969 Gr_ta_green.shift = 4;
2970 Gr_ta_green.scale = 16;
2972 Gr_ta_blue.mask = 0x000f;
2973 Gr_ta_blue.shift = 0;
2974 Gr_ta_blue.scale = 16;
2976 Gr_ta_alpha.mask = 0xf000;
2977 Gr_ta_alpha.shift = 12;
2978 Gr_ta_alpha.scale = 16;
2981 opengl_tcache_init (1);
2984 Gr_current_red = &Gr_red;
2985 Gr_current_blue = &Gr_blue;
2986 Gr_current_green = &Gr_green;
2987 Gr_current_alpha = &Gr_alpha;
2989 gr_screen.gf_flip = gr_opengl_flip;
2990 gr_screen.gf_flip_window = gr_opengl_flip_window;
2991 gr_screen.gf_set_clip = gr_opengl_set_clip;
2992 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2993 gr_screen.gf_set_font = grx_set_font;
2995 gr_screen.gf_set_color = gr_opengl_set_color;
2996 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2997 gr_screen.gf_create_shader = gr_opengl_create_shader;
2998 gr_screen.gf_set_shader = gr_opengl_set_shader;
2999 gr_screen.gf_clear = gr_opengl_clear;
3000 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3001 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3002 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3003 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3005 gr_screen.gf_rect = gr_opengl_rect;
3006 gr_screen.gf_shade = gr_opengl_shade;
3007 gr_screen.gf_string = gr_opengl_string;
3008 gr_screen.gf_circle = gr_opengl_circle;
3010 gr_screen.gf_line = gr_opengl_line;
3011 gr_screen.gf_aaline = gr_opengl_aaline;
3012 gr_screen.gf_pixel = gr_opengl_pixel;
3013 gr_screen.gf_scaler = gr_opengl_scaler;
3014 gr_screen.gf_tmapper = gr_opengl_tmapper;
3016 gr_screen.gf_gradient = gr_opengl_gradient;
3018 gr_screen.gf_set_palette = gr_opengl_set_palette;
3019 gr_screen.gf_get_color = gr_opengl_get_color;
3020 gr_screen.gf_init_color = gr_opengl_init_color;
3021 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3022 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3023 gr_screen.gf_print_screen = gr_opengl_print_screen;
3025 gr_screen.gf_fade_in = gr_opengl_fade_in;
3026 gr_screen.gf_fade_out = gr_opengl_fade_out;
3027 gr_screen.gf_flash = gr_opengl_flash;
3029 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3030 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3031 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3033 gr_screen.gf_save_screen = gr_opengl_save_screen;
3034 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3035 gr_screen.gf_free_screen = gr_opengl_free_screen;
3037 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3038 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3039 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3041 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3043 gr_screen.gf_lock = gr_opengl_lock;
3044 gr_screen.gf_unlock = gr_opengl_unlock;
3046 gr_screen.gf_fog_set = gr_opengl_fog_set;
3048 gr_screen.gf_get_region = gr_opengl_get_region;
3050 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3052 gr_screen.gf_set_cull = gr_opengl_set_cull;
3054 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3056 gr_screen.gf_filter_set = gr_opengl_filter_set;
3058 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3060 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;