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.72 2005/03/30 01:20:12 taylor
19 * Screenshot function filled, will output into ~/.freespace(2)/Data
20 * Use glTexSubImage2D on reloaded texture slots, this is only used by ANIs
22 * Revision 1.71 2004/12/15 04:10:45 taylor
23 * outwnd_unix.cpp from fs2_open for logging to file in debug mode
24 * fixes for default function values
25 * always use vm_* functions for sanity sake
26 * make cfilearchiver 64-bit compatible
27 * fix crash on exit from double free()
28 * fix crash on startup from extra long GL extension string in debug
30 * Revision 1.70 2004/09/20 01:31:44 theoddone33
33 * Revision 1.69 2004/07/04 11:31:43 taylor
34 * amd64 support, compiler warning fixes, don't use software rendering
36 * Revision 1.68 2004/06/11 01:01:07 tigital
37 * added FSAA support: switched on in .ini
39 * Revision 1.67 2003/08/03 15:59:40 taylor
40 * GL_RGB5_A1 as TexImage internal format; cleaner input grab; min window title; cleanup
42 * Revision 1.66 2003/06/22 12:52:34 taylor
43 * more texture size fixin
45 * Revision 1.65 2003/06/19 11:52:47 taylor
46 * fix texture size issue with lower detail settings
48 * Revision 1.64 2003/05/28 06:02:04 taylor
49 * fix transparency in green weapon blobs
51 * Revision 1.63 2003/05/09 05:05:52 taylor
52 * improve gr_(de)activate, mouse grab, default fullscreen
54 * Revision 1.62 2003/05/04 04:56:53 taylor
55 * move SDL_Quit to os_deinit to fix fonttool segfault
57 * Revision 1.61 2003/02/02 21:13:27 relnev
58 * minor updates (made functions static, tried to improve texture sizes)
60 * Revision 1.60 2003/02/02 20:20:03 relnev
61 * add large texture support (Taylor Richards)
63 * Revision 1.59 2002/09/04 01:38:09 relnev
66 * Revision 1.57 2002/08/31 01:39:13 theoddone33
67 * Speed up the renderer a tad
69 * Revision 1.56 2002/08/01 05:03:11 relnev
72 * Revision 1.55 2002/08/01 04:55:45 relnev
73 * experimenting with texture state
75 * Revision 1.54 2002/07/30 15:00:15 relnev
76 * not use luminance alpha by default
78 * Revision 1.53 2002/06/22 17:08:16 relnev
79 * more fixes to unused alpha code
81 * Revision 1.52 2002/06/21 23:59:14 relnev
82 * moved a gr_opengl_set_state to after the gr_tcache_set
84 * Revision 1.51 2002/06/19 04:52:45 relnev
85 * MacOS X updates (Ryan)
87 * Revision 1.50 2002/06/18 23:20:44 relnev
90 * adjusted line offsets.
92 * Revision 1.49 2002/06/17 23:11:39 relnev
93 * enable sdl key repeating.
97 * Revision 1.48 2002/06/09 04:41:17 relnev
98 * added copyright header
100 * Revision 1.47 2002/06/09 03:16:04 relnev
103 * removed unneeded asm, old sdl 2d setup.
105 * fixed crash caused by opengl_get_region.
107 * Revision 1.46 2002/06/05 04:03:32 relnev
108 * finished cfilesystem.
110 * removed some old code.
112 * fixed mouse save off-by-one.
116 * Revision 1.45 2002/06/03 09:25:37 relnev
117 * implement mouse cursor and screen save/restore
119 * Revision 1.44 2002/06/02 18:46:59 relnev
122 * Revision 1.43 2002/06/02 11:34:00 relnev
125 * Revision 1.42 2002/06/02 10:28:17 relnev
126 * fix texture handle leak
128 * Revision 1.41 2002/06/01 09:00:34 relnev
129 * silly debug memmanager
131 * Revision 1.40 2002/06/01 07:12:33 relnev
132 * a few NDEBUG updates.
134 * removed a few warnings.
136 * Revision 1.39 2002/06/01 05:33:15 relnev
137 * copied more code over.
139 * added scissor clipping.
141 * Revision 1.38 2002/06/01 03:35:27 relnev
144 * Revision 1.37 2002/06/01 03:32:00 relnev
145 * fix texture loading mistake.
147 * enable some d3d stuff for opengl also
149 * Revision 1.36 2002/05/31 23:25:03 relnev
152 * Revision 1.34 2002/05/31 22:15:22 relnev
155 * Revision 1.33 2002/05/31 22:04:55 relnev
156 * use d3d rect_internal
158 * Revision 1.32 2002/05/31 06:28:23 relnev
161 * Revision 1.31 2002/05/31 06:04:39 relnev
164 * Revision 1.30 2002/05/31 03:56:11 theoddone33
165 * Change tmapper polygon winding and enable culling
167 * Revision 1.29 2002/05/31 03:34:02 theoddone33
171 * Revision 1.28 2002/05/31 00:06:59 relnev
174 * Revision 1.27 2002/05/30 23:46:29 theoddone33
175 * some minor key changes (not necessarily fixes)
177 * Revision 1.26 2002/05/30 23:33:12 relnev
178 * implemented a few more functions.
180 * Revision 1.25 2002/05/30 23:01:16 relnev
181 * implement gr_opengl_set_state.
183 * Revision 1.24 2002/05/30 22:12:57 relnev
184 * finish default texture case
186 * Revision 1.23 2002/05/30 22:02:30 theoddone33
189 * Revision 1.22 2002/05/30 21:44:48 relnev
190 * implemented some missing texture stuff.
192 * enable bitmap polys for opengl.
194 * work around greenness in bitmaps.
196 * Revision 1.21 2002/05/30 17:29:30 theoddone33
197 * Fix some more stubs, change at least one polygon winding since culling is now
200 * Revision 1.20 2002/05/30 16:50:24 theoddone33
201 * Keyboard partially fixed
203 * Revision 1.19 2002/05/30 08:13:14 relnev
206 * Revision 1.18 2002/05/29 23:37:36 relnev
209 * Revision 1.17 2002/05/29 23:17:49 theoddone33
210 * Non working text code and fixed keys
212 * Revision 1.16 2002/05/29 19:45:13 theoddone33
213 * More changes on texture loading
215 * Revision 1.15 2002/05/29 19:06:48 theoddone33
216 * Enable string printing. Enable texture mapping
218 * Revision 1.14 2002/05/29 08:54:40 relnev
219 * "fixed" bitmap drawing.
221 * copied more d3d code over.
223 * Revision 1.13 2002/05/29 06:25:13 theoddone33
224 * Keyboard input, mouse tracking now work
226 * Revision 1.12 2002/05/29 04:52:45 relnev
229 * Revision 1.11 2002/05/29 04:29:56 relnev
230 * removed some unncessary stubbing, implemented opengl rect
232 * Revision 1.10 2002/05/29 04:13:27 theoddone33
235 * Revision 1.9 2002/05/29 03:35:51 relnev
238 * Revision 1.8 2002/05/29 03:30:05 relnev
239 * update opengl stubs
241 * Revision 1.7 2002/05/29 02:52:32 theoddone33
242 * Enable OpenGL renderer
244 * Revision 1.6 2002/05/28 04:56:51 theoddone33
245 * runs a little bit now
247 * Revision 1.5 2002/05/28 04:07:28 theoddone33
248 * New graphics stubbing arrangement
250 * Revision 1.4 2002/05/27 23:39:34 relnev
253 * Revision 1.3 2002/05/27 22:35:01 theoddone33
256 * Revision 1.2 2002/05/27 22:32:02 theoddone33
257 * throw all d3d stuff at opengl
259 * Revision 1.1.1.1 2002/05/03 03:28:09 root
263 * 10 7/14/99 9:42a Dave
264 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
267 * 9 7/09/99 9:51a Dave
268 * Added thick polyline code.
270 * 8 6/29/99 10:35a Dave
271 * Interface polygon bitmaps! Whee!
273 * 7 2/03/99 11:44a Dave
274 * Fixed d3d transparent textures.
276 * 6 1/24/99 11:37p Dave
277 * First full rev of beam weapons. Very customizable. Removed some bogus
278 * Int3()'s in low level net code.
280 * 5 12/18/98 1:13a Dave
281 * Rough 1024x768 support for Direct3D. Proper detection and usage through
284 * 4 12/06/98 2:36p Dave
285 * Drastically improved nebula fogging.
287 * 3 11/11/98 5:37p Dave
288 * Checkin for multiplayer testing.
290 * 2 10/07/98 10:53a Dave
293 * 1 10/07/98 10:49a Dave
295 * 14 5/20/98 9:46p John
296 * added code so the places in code that change half the palette don't
297 * have to clear the screen.
299 * 13 5/06/98 5:30p John
300 * Removed unused cfilearchiver. Removed/replaced some unused/little used
301 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
302 * DirectX header files and libs that fixed the Direct3D alpha blending
305 * 12 4/14/98 12:15p John
306 * Made 16-bpp movies work.
308 * 11 3/12/98 5:36p John
309 * Took out any unused shaders. Made shader code take rgbc instead of
310 * matrix and vector since noone used it like a matrix and it would have
311 * been impossible to do in hardware. Made Glide implement a basic
312 * shader for online help.
314 * 10 3/10/98 4:18p John
315 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
316 * & Glide have popups and print screen. Took out all >8bpp software
317 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
318 * support Fred. Made zbuffering key off of functions rather than one
321 * 9 12/02/97 4:00p John
322 * Added first rev of thruster glow, along with variable levels of
323 * translucency, which retquired some restructing of palman.
325 * 8 10/03/97 9:10a John
326 * added better antialiased line drawer
328 * 7 9/23/97 10:45a John
329 * made so you can tell bitblt code to rle a bitmap by passing flag to
332 * 6 9/09/97 11:01a Sandeep
333 * fixed warning level 4 bugs
335 * 5 7/10/97 2:06p John
336 * added code to specify alphablending type for bitmaps.
338 * 4 6/17/97 7:04p John
339 * added d3d support for gradients.
340 * fixed some color bugs by adding screen signatures instead of watching
341 * flags and palette changes.
343 * 3 6/12/97 2:50a Lawrance
344 * bm_unlock() now passed bitmap number, not pointer
346 * 2 6/11/97 1:12p John
347 * Started fixing all the text colors in the game.
349 * 1 5/12/97 12:14p John
356 #include <windowsx.h>
360 #include <OpenGL/gl.h>
370 #include "floating.h"
372 #include "systemvars.h"
373 #include "grinternal.h"
374 #include "gropengl.h"
378 #include "osregistry.h"
382 static int Inited = 0;
384 typedef enum gr_texture_source {
386 TEXTURE_SOURCE_DECAL,
387 TEXTURE_SOURCE_NO_FILTERING,
390 typedef enum gr_alpha_blend {
391 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
392 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
393 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
394 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
395 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
398 typedef enum gr_zbuffer_type {
405 #define NEBULA_COLORS 20
407 volatile int GL_activate = 0;
408 volatile int GL_deactivate = 0;
410 static int GL_use_luminance_alpha;
413 static char *Gr_saved_screen = NULL;
414 static int Gr_saved_screen_bitmap;
417 // Throw in some dummy functions - DDOI
419 int D3D_32bit = 0; // grd3d.cpp
420 int D3D_fog_mode = -1; // grd3d.cpp
421 int D3D_inited = 0; // grd3d.cpp
422 int D3D_zbias = 1; // grd3d.cpp
423 int D3d_rendition_uvs = 0; // grd3d.cpp
430 void d3d_zbias (int a)
436 static void gr_opengl_set_texture_state(gr_texture_source ts);
438 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
439 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
441 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
443 gr_opengl_set_texture_state(ts);
445 if (ab != GL_current_alpha_blend) {
447 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
448 glBlendFunc(GL_ONE, GL_ZERO);
450 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
451 glBlendFunc(GL_ONE, GL_ONE);
453 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
454 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
456 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
457 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
459 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
460 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
466 GL_current_alpha_blend = ab;
469 if (zt != GL_current_zbuffer_type) {
471 case ZBUFFER_TYPE_NONE:
472 glDepthFunc(GL_ALWAYS);
473 glDepthMask(GL_FALSE);
475 case ZBUFFER_TYPE_READ:
476 glDepthFunc(GL_LESS);
477 glDepthMask(GL_FALSE);
479 case ZBUFFER_TYPE_WRITE:
480 glDepthFunc(GL_ALWAYS);
481 glDepthMask(GL_TRUE);
483 case ZBUFFER_TYPE_FULL:
484 glDepthFunc(GL_LESS);
485 glDepthMask(GL_TRUE);
491 GL_current_zbuffer_type = zt;
495 void gr_opengl_activate(int active)
500 // don't grab key/mouse if cmdline says so or if we're fullscreen
501 if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
502 SDL_WM_GrabInput(SDL_GRAB_ON);
507 // let go of mouse/keyboard
508 SDL_WM_GrabInput(SDL_GRAB_OFF);
513 static void opengl_tcache_flush ();
515 void gr_opengl_preload_init()
517 if (gr_screen.mode != GR_OPENGL) {
521 opengl_tcache_flush ();
524 int GL_should_preload = 0;
525 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
527 if ( gr_screen.mode != GR_OPENGL) {
531 if ( !GL_should_preload ) {
535 float u_scale, v_scale;
539 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1, -1, -1, 0 );
541 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1, -1, -1, 0 );
545 mprintf(("Texture upload failed!\n" ));
551 void gr_opengl_pixel(int x, int y)
556 void gr_opengl_clear()
558 glClearColor(gr_screen.current_clear_color.red / 255.0,
559 gr_screen.current_clear_color.green / 255.0,
560 gr_screen.current_clear_color.blue / 255.0, 1.0);
562 glClear ( GL_COLOR_BUFFER_BIT );
565 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
566 static void opengl_tcache_frame ();
568 void gr_opengl_flip()
576 extern int Gr_opengl_mouse_saved;
577 Gr_opengl_mouse_saved = 0;
579 if ( mouse_is_visible() ) {
583 mouse_get_pos( &mx, &my );
585 gr_opengl_save_mouse_area(mx,my,32,32);
587 if ( Gr_cursor == -1 ) {
590 gr_set_bitmap(Gr_cursor, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
596 GLenum error = glGetError();
599 error = glGetError();
601 if (error != GL_NO_ERROR) {
602 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
605 } while (error != GL_NO_ERROR);
608 SDL_GL_SwapBuffers ();
610 opengl_tcache_frame ();
612 int cnt = GL_activate;
615 opengl_tcache_flush();
616 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
622 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
626 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
631 void gr_opengl_set_clip(int x,int y,int w,int h)
633 // check for sanity of parameters
639 if (x >= gr_screen.max_w)
640 x = gr_screen.max_w - 1;
641 if (y >= gr_screen.max_h)
642 y = gr_screen.max_h - 1;
644 if (x + w > gr_screen.max_w)
645 w = gr_screen.max_w - x;
646 if (y + h > gr_screen.max_h)
647 h = gr_screen.max_h - y;
649 if (w > gr_screen.max_w)
651 if (h > gr_screen.max_h)
654 gr_screen.offset_x = x;
655 gr_screen.offset_y = y;
656 gr_screen.clip_left = 0;
657 gr_screen.clip_right = w-1;
658 gr_screen.clip_top = 0;
659 gr_screen.clip_bottom = h-1;
660 gr_screen.clip_width = w;
661 gr_screen.clip_height = h;
663 glEnable(GL_SCISSOR_TEST);
664 glScissor(x, gr_screen.max_h-y-h, w, h);
667 void gr_opengl_reset_clip()
669 gr_screen.offset_x = 0;
670 gr_screen.offset_y = 0;
671 gr_screen.clip_left = 0;
672 gr_screen.clip_top = 0;
673 gr_screen.clip_right = gr_screen.max_w - 1;
674 gr_screen.clip_bottom = gr_screen.max_h - 1;
675 gr_screen.clip_width = gr_screen.max_w;
676 gr_screen.clip_height = gr_screen.max_h;
678 glDisable(GL_SCISSOR_TEST);
679 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
682 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode = GR_ALPHABLEND_NONE, int bitblt_mode = GR_BITBLT_MODE_NORMAL, float alpha = 1.0f, int sx = -1, int sy = -1 )
684 gr_screen.current_alpha = alpha;
685 gr_screen.current_alphablend_mode = alphablend_mode;
686 gr_screen.current_bitblt_mode = bitblt_mode;
687 gr_screen.current_bitmap = bitmap_num;
689 gr_screen.current_bitmap_sx = sx;
690 gr_screen.current_bitmap_sy = sy;
693 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
695 shade->screen_sig = gr_screen.signature;
702 void gr_opengl_set_shader( shader * shade )
705 if (shade->screen_sig != gr_screen.signature) {
706 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
708 gr_screen.current_shader = *shade;
710 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
715 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
717 STUB_FUNCTION; /* who called me? */
720 void gr_opengl_bitmap(int x, int y)
722 STUB_FUNCTION; /* who called me? */
725 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
729 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
731 saved_zbuf = gr_zbuffer_get();
733 // start the frame, no zbuffering, no culling
735 gr_zbuffer_set(GR_ZBUFF_NONE);
744 v[0].flags = PF_PROJECTED;
751 v[1].sx = i2fl(x + w);
756 v[1].flags = PF_PROJECTED;
763 v[2].sx = i2fl(x + w);
764 v[2].sy = i2fl(y + h);
768 v[2].flags = PF_PROJECTED;
776 v[3].sy = i2fl(y + h);
780 v[3].flags = PF_PROJECTED;
788 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
792 // restore zbuffer and culling
793 gr_zbuffer_set(saved_zbuf);
797 void gr_opengl_rect(int x,int y,int w,int h)
799 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);
802 void gr_opengl_shade(int x,int y,int w,int h)
809 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
810 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
811 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
812 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
813 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
814 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
815 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
816 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
818 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
821 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
826 if ( !gr_screen.current_color.is_alphacolor ) return;
828 float u_scale, v_scale;
830 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 0, -1, -1, 0 ) ) {
831 // Couldn't set texture
832 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
836 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
838 float u0, u1, v0, v1;
839 float x1, x2, y1, y2;
842 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
844 u0 = u_scale*i2fl(sx)/i2fl(bw);
845 v0 = v_scale*i2fl(sy)/i2fl(bh);
847 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
848 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
850 x1 = i2fl(x+gr_screen.offset_x);
851 y1 = i2fl(y+gr_screen.offset_y);
852 x2 = i2fl(x+w+gr_screen.offset_x);
853 y2 = i2fl(y+h+gr_screen.offset_y);
855 if ( gr_screen.current_color.is_alphacolor ) {
856 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
858 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
862 glTexCoord2f (u0, v1);
863 glVertex3f (x1, y2, -0.99);
865 glTexCoord2f (u1, v1);
866 glVertex3f (x2, y2, -0.99);
868 glTexCoord2f (u1, v0);
869 glVertex3f (x2, y1, -0.99);
871 glTexCoord2f (u0, v0);
872 glVertex3f (x1, y1, -0.99);
876 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
883 int dx1=x, dx2=x+w-1;
884 int dy1=y, dy2=y+h-1;
887 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
892 if ( count > 1 ) Int3();
896 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
897 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
898 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
899 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
900 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
901 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
928 if ( w < 1 ) return; // clipped away!
929 if ( h < 1 ) return; // clipped away!
933 // Make sure clipping algorithm works
937 Assert( w == (dx2-dx1+1) );
938 Assert( h == (dy2-dy1+1) );
941 Assert( sx+w <= bw );
942 Assert( sy+h <= bh );
943 Assert( dx2 >= dx1 );
944 Assert( dy2 >= dy1 );
945 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
946 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
947 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
948 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
951 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
952 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
955 void gr_opengl_aabitmap(int x, int y)
959 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
960 int dx1=x, dx2=x+w-1;
961 int dy1=y, dy2=y+h-1;
964 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
965 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
966 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
967 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
968 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
969 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
971 if ( sx < 0 ) return;
972 if ( sy < 0 ) return;
973 if ( sx >= w ) return;
974 if ( sy >= h ) return;
976 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
977 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
980 void gr_opengl_string( int sx, int sy, char *s )
982 int width, spacing, letter;
985 if ( !Current_font ) {
989 gr_set_bitmap(Current_font->bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
994 if (sx==0x8000) { //centered
995 x = get_centered_x(s);
1005 while (*s== '\n' ) {
1007 y += Current_font->h;
1008 if (sx==0x8000) { //centered
1009 x = get_centered_x(s);
1014 if (*s == 0 ) break;
1016 letter = get_char_width(s[0],s[1],&width,&spacing);
1019 //not in font, draw as space
1027 // Check if this character is totally clipped
1028 if ( x + width < gr_screen.clip_left ) continue;
1029 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1030 if ( x > gr_screen.clip_right ) continue;
1031 if ( y > gr_screen.clip_bottom ) continue;
1034 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1035 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1039 wc = width - xd; hc = Current_font->h - yd;
1040 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1041 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1043 if ( wc < 1 ) continue;
1044 if ( hc < 1 ) continue;
1046 int u = Current_font->bm_u[letter];
1047 int v = Current_font->bm_v[letter];
1049 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1053 void gr_opengl_line(int x1,int y1,int x2,int y2)
1055 int clipped = 0, swapped=0;
1057 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1059 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);
1064 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1065 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1066 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1067 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1069 if ( x1 == x2 && y1 == y2 ) {
1070 glBegin (GL_POINTS);
1071 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1072 glVertex3f (sx1, sy1, -0.99f);
1084 } else if ( y1 == y2 ) {
1093 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1094 glVertex3f (sx2, sy2, -0.99f);
1095 glVertex3f (sx1, sy1, -0.99f);
1099 void gr_opengl_aaline(vertex *v1, vertex *v2)
1101 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1104 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1106 int clipped = 0, swapped=0;
1108 if ( !gr_screen.current_color.is_alphacolor ) {
1109 gr_line( x1, y1, x2, y2 );
1113 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);
1115 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1117 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1118 int ba = swapped ? gr_screen.current_color.alpha : 0;
1123 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1124 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1125 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1126 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1134 } else if ( y1 == y2 ) {
1143 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1144 glVertex3f (sx2, sy2, -0.99f);
1145 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1146 glVertex3f (sx1, sy1, -0.99f);
1150 void gr_opengl_circle( int xc, int yc, int d )
1160 if ( (xc+r) < gr_screen.clip_left ) return;
1161 if ( (xc-r) > gr_screen.clip_right ) return;
1162 if ( (yc+r) < gr_screen.clip_top ) return;
1163 if ( (yc-r) > gr_screen.clip_bottom ) return;
1166 // Draw the first octant
1167 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1168 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1173 // Draw the second octant
1174 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1175 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1183 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1184 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1189 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1193 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1194 if (f_float < 0.0f) {
1202 *a = (int)(f_float * 255.0);
1205 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1208 float u_scale = 1.0f, v_scale = 1.0f;
1210 // Make nebula use the texture mapper... this blends the colors better.
1211 if ( flags & TMAP_FLAG_NEBULA ){
1215 gr_texture_source texture_source = (gr_texture_source)-1;
1216 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1217 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1219 if ( gr_zbuffering ) {
1220 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1221 zbuffer_type = ZBUFFER_TYPE_READ;
1223 zbuffer_type = ZBUFFER_TYPE_FULL;
1226 zbuffer_type = ZBUFFER_TYPE_NONE;
1231 int tmap_type = TCACHE_TYPE_NORMAL;
1235 if ( flags & TMAP_FLAG_TEXTURED ) {
1238 r = gr_screen.current_color.red;
1239 g = gr_screen.current_color.green;
1240 b = gr_screen.current_color.blue;
1243 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1246 tmap_type = TCACHE_TYPE_NORMAL;
1247 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1249 // Blend with screen pixel using src*alpha+dst
1250 float factor = gr_screen.current_alpha;
1254 if ( factor <= 1.0f ) {
1255 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1256 r = (r*tmp_alpha)/255;
1257 g = (g*tmp_alpha)/255;
1258 b = (b*tmp_alpha)/255;
1261 tmap_type = TCACHE_TYPE_XPARENT;
1263 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1265 // Blend with screen pixel using src*alpha+dst
1266 float factor = gr_screen.current_alpha;
1268 if ( factor > 1.0f ) {
1271 alpha = fl2i(gr_screen.current_alpha*255.0f);
1275 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1276 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1278 alpha_blend = ALPHA_BLEND_NONE;
1283 if(flags & TMAP_FLAG_BITMAP_SECTION){
1284 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1287 texture_source = TEXTURE_SOURCE_NONE;
1289 if ( flags & TMAP_FLAG_TEXTURED ) {
1290 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, 0 ))
1292 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1296 // use nonfiltered textures for bitmap sections
1297 if(flags & TMAP_FLAG_BITMAP_SECTION){
1298 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1300 texture_source = TEXTURE_SOURCE_DECAL;
1305 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1307 if ( flags & TMAP_FLAG_TEXTURED )
1313 if (flags & TMAP_FLAG_PIXEL_FOG) {
1319 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1321 vertex * va = verts[i];
1325 x = fl2i(va->sx*16.0f);
1326 y = fl2i(va->sy*16.0f);
1328 x += gr_screen.offset_x*16;
1329 y += gr_screen.offset_y*16;
1331 sx = i2fl(x) / 16.0f;
1332 sy = i2fl(y) / 16.0f;
1334 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1345 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba, -1.0f, -1.0f);
1348 glBegin(GL_TRIANGLE_FAN);
1349 for (i = nv-1; i >= 0; i--) {
1350 vertex * va = verts[i];
1356 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1357 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1366 if ( flags & TMAP_FLAG_CORRECT ) {
1372 if (flags & TMAP_FLAG_ALPHA) {
1378 if (flags & TMAP_FLAG_NEBULA ) {
1379 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1380 r = gr_palette[pal*3+0];
1381 g = gr_palette[pal*3+1];
1382 b = gr_palette[pal*3+2];
1383 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1384 r = Gr_gamma_lookup[verts[i]->b];
1385 g = Gr_gamma_lookup[verts[i]->b];
1386 b = Gr_gamma_lookup[verts[i]->b];
1387 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1388 // Make 0.75 be 256.0f
1389 r = Gr_gamma_lookup[verts[i]->r];
1390 g = Gr_gamma_lookup[verts[i]->g];
1391 b = Gr_gamma_lookup[verts[i]->b];
1393 // use constant RGB values...
1395 glColor4ub (r,g,b,a);
1397 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1400 /* this is for GL_EXT_SECONDARY_COLOR */
1401 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1402 /* do separate color call here */
1408 x = fl2i(va->sx*16.0f);
1409 y = fl2i(va->sy*16.0f);
1411 x += gr_screen.offset_x*16;
1412 y += gr_screen.offset_y*16;
1414 sx = i2fl(x) / 16.0f;
1415 sy = i2fl(y) / 16.0f;
1417 if ( flags & TMAP_FLAG_TEXTURED ) {
1420 glTexCoord2f(tu, tv);
1423 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1428 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1430 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1433 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1435 void gr_opengl_scaler(vertex *va, vertex *vb )
1437 float x0, y0, x1, y1;
1438 float u0, v0, u1, v1;
1439 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1440 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1441 float xmin, xmax, ymin, ymax;
1442 int dx0, dy0, dx1, dy1;
1444 //============= CLIP IT =====================
1446 x0 = va->sx; y0 = va->sy;
1447 x1 = vb->sx; y1 = vb->sy;
1449 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1450 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1452 u0 = va->u; v0 = va->v;
1453 u1 = vb->u; v1 = vb->v;
1455 // Check for obviously offscreen bitmaps...
1456 if ( (y1<=y0) || (x1<=x0) ) return;
1457 if ( (x1<xmin ) || (x0>xmax) ) return;
1458 if ( (y1<ymin ) || (y0>ymax) ) return;
1460 clipped_u0 = u0; clipped_v0 = v0;
1461 clipped_u1 = u1; clipped_v1 = v1;
1463 clipped_x0 = x0; clipped_y0 = y0;
1464 clipped_x1 = x1; clipped_y1 = y1;
1466 // Clip the left, moving u0 right as necessary
1468 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1472 // Clip the right, moving u1 left as necessary
1474 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1478 // Clip the top, moving v0 down as necessary
1480 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1484 // Clip the bottom, moving v1 up as necessary
1486 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1490 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1491 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1493 if (dx1<=dx0) return;
1494 if (dy1<=dy0) return;
1496 //============= DRAW IT =====================
1510 v[1].sx = clipped_x1;
1511 v[1].sy = clipped_y0;
1514 v[1].u = clipped_u1;
1515 v[1].v = clipped_v0;
1518 v[2].sx = clipped_x1;
1519 v[2].sy = clipped_y1;
1522 v[2].u = clipped_u1;
1523 v[2].v = clipped_v1;
1526 v[3].sx = clipped_x0;
1527 v[3].sy = clipped_y1;
1530 v[3].u = clipped_u0;
1531 v[3].v = clipped_v1;
1533 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1536 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1540 void gr_opengl_get_color( int * r, int * g, int * b )
1542 if (r) *r = gr_screen.current_color.red;
1543 if (g) *g = gr_screen.current_color.green;
1544 if (b) *b = gr_screen.current_color.blue;
1547 void gr_opengl_init_color(color *c, int r, int g, int b)
1549 c->screen_sig = gr_screen.signature;
1550 c->red = (unsigned char)r;
1551 c->green = (unsigned char)g;
1552 c->blue = (unsigned char)b;
1554 c->ac_type = AC_TYPE_NONE;
1556 c->is_alphacolor = 0;
1560 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1562 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1563 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1564 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1565 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1567 gr_opengl_init_color( clr, r, g, b );
1569 clr->alpha = (unsigned char)alpha;
1570 clr->ac_type = (ubyte)type;
1571 clr->alphacolor = -1;
1572 clr->is_alphacolor = 1;
1575 void gr_opengl_set_color( int r, int g, int b )
1577 Assert((r >= 0) && (r < 256));
1578 Assert((g >= 0) && (g < 256));
1579 Assert((b >= 0) && (b < 256));
1581 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1584 void gr_opengl_set_color_fast(color *dst)
1586 if ( dst->screen_sig != gr_screen.signature ) {
1587 if ( dst->is_alphacolor ) {
1588 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1590 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1593 gr_screen.current_color = *dst;
1596 void gr_opengl_print_screen(char *filename)
1598 #ifdef GL_VERSION_1_2
1599 char tmp[MAX_FILENAME_LEN];
1602 strcpy( tmp, filename );
1603 strcat( tmp, NOX(".tga"));
1605 CFILE *f = cfopen(tmp, "wb", CFILE_NORMAL, CF_TYPE_ROOT);
1610 // Write the TGA header
1611 cfwrite_ubyte( 0, f ); // IDLength;
1612 cfwrite_ubyte( 0, f ); // ColorMapType;
1613 cfwrite_ubyte( 2, f ); // ImageType; // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
1614 cfwrite_ushort( 0, f ); // CMapStart;
1615 cfwrite_ushort( 0, f ); // CMapLength;
1616 cfwrite_ubyte( 0, f ); // CMapDepth;
1617 cfwrite_ushort( 0, f ); // XOffset;
1618 cfwrite_ushort( 0, f ); // YOffset;
1619 cfwrite_ushort( (ushort)gr_screen.max_w, f ); // Width;
1620 cfwrite_ushort( (ushort)gr_screen.max_h, f ); // Height;
1621 cfwrite_ubyte( 24, f ); //PixelDepth;
1622 cfwrite_ubyte( 0, f ); //ImageDesc;
1624 buf = (ubyte*)malloc(gr_screen.max_w * gr_screen.max_h * 3);
1629 memset(buf, 0, gr_screen.max_w * gr_screen.max_h * 3);
1631 glReadPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_BGR, GL_UNSIGNED_BYTE, buf);
1633 cfwrite(buf, gr_screen.max_w * gr_screen.max_h * 3, 1, f);
1641 int gr_opengl_supports_res_ingame(int res)
1648 int gr_opengl_supports_res_interface(int res)
1655 static void opengl_tcache_cleanup ();
1657 void gr_opengl_cleanup()
1659 if ( !Inited ) return;
1666 opengl_tcache_cleanup ();
1671 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1673 Assert((r >= 0) && (r < 256));
1674 Assert((g >= 0) && (g < 256));
1675 Assert((b >= 0) && (b < 256));
1677 if (fog_mode == GR_FOGMODE_NONE) {
1678 if (gr_screen.current_fog_mode != fog_mode) {
1681 gr_screen.current_fog_mode = fog_mode;
1686 if (gr_screen.current_fog_mode != fog_mode) {
1689 if (D3D_fog_mode == 2) {
1690 glFogi(GL_FOG_MODE, GL_LINEAR);
1693 gr_screen.current_fog_mode = fog_mode;
1696 if ( (gr_screen.current_fog_color.red != r) ||
1697 (gr_screen.current_fog_color.green != g) ||
1698 (gr_screen.current_fog_color.blue != b) ) {
1701 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1703 fc[0] = (float)r/255.0;
1704 fc[1] = (float)g/255.0;
1705 fc[2] = (float)b/255.0;
1708 glFogfv(GL_FOG_COLOR, fc);
1711 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1712 ((fog_near != gr_screen.fog_near) ||
1713 (fog_far != gr_screen.fog_far)) ) {
1714 gr_screen.fog_near = fog_near;
1715 gr_screen.fog_far = fog_far;
1717 if (D3D_fog_mode == 2) {
1718 glFogf(GL_FOG_START, fog_near);
1719 glFogf(GL_FOG_END, fog_far);
1724 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1729 void gr_opengl_set_cull(int cull)
1732 glEnable (GL_CULL_FACE);
1733 glFrontFace (GL_CCW);
1735 glDisable (GL_CULL_FACE);
1739 void gr_opengl_filter_set(int filter)
1744 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1747 gr_set_bitmap(bmap1, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1750 gr_set_bitmap(bmap2, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1756 typedef struct tcache_slot_opengl {
1757 GLuint texture_handle;
1758 float u_scale, v_scale;
1761 int used_this_frame;
1766 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1767 tcache_slot_opengl *parent;
1769 gr_texture_source texture_mode;
1770 } tcache_slot_opengl;
1772 static void *Texture_sections = NULL;
1773 static tcache_slot_opengl *Textures = NULL;
1775 static tcache_slot_opengl *GL_bound_texture;
1777 int GL_texture_sections = 0;
1778 int GL_texture_ram = 0;
1779 int GL_frame_count = 0;
1780 int GL_min_texture_width = 0;
1781 int GL_max_texture_width = 0;
1782 int GL_min_texture_height = 0;
1783 int GL_max_texture_height = 0;
1784 int GL_square_textures = 0;
1785 int GL_textures_in = 0;
1786 int GL_textures_in_frame = 0;
1787 int GL_last_bitmap_id = -1;
1788 int GL_last_detail = -1;
1789 int GL_last_bitmap_type = -1;
1790 int GL_last_section_x = -1;
1791 int GL_last_section_y = -1;
1795 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1797 static void gr_opengl_set_texture_state(gr_texture_source ts)
1799 if (ts == TEXTURE_SOURCE_NONE) {
1800 GL_bound_texture = NULL;
1802 glBindTexture(GL_TEXTURE_2D, 0);
1803 gr_tcache_set(-1, -1, NULL, NULL, 0, -1, -1, 0 );
1804 } else if (GL_bound_texture &&
1805 GL_bound_texture->texture_mode != ts) {
1807 case TEXTURE_SOURCE_DECAL:
1808 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1809 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1811 case TEXTURE_SOURCE_NO_FILTERING:
1812 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1813 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1819 GL_bound_texture->texture_mode = ts;
1822 GL_current_texture_source = ts;
1825 static int opengl_max_tex_size_get()
1827 GLint max_texture_size = 0;
1829 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1830 return max_texture_size;
1833 static void opengl_tcache_init (int use_sections)
1837 // DDOI - FIXME skipped a lot of stuff here
1838 GL_should_preload = 0;
1840 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1843 if ( tmp_pl == 0 ) {
1844 GL_should_preload = 0;
1845 } else if ( tmp_pl == 1 ) {
1846 GL_should_preload = 1;
1848 GL_should_preload = 1;
1853 GL_min_texture_width = 16;
1854 GL_min_texture_height = 16;
1856 GL_max_texture_width = opengl_max_tex_size_get();
1857 GL_max_texture_height = opengl_max_tex_size_get();
1859 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1865 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1866 if(!Texture_sections){
1869 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1872 // Init the texture structures
1873 int section_count = 0;
1874 for( i=0; i<MAX_BITMAPS; i++ ) {
1876 Textures[i].vram_texture = NULL;
1877 Textures[i].vram_texture_surface = NULL;
1879 Textures[i].texture_handle = 0;
1881 Textures[i].bitmap_id = -1;
1882 Textures[i].size = 0;
1883 Textures[i].used_this_frame = 0;
1885 Textures[i].parent = NULL;
1887 // allocate sections
1889 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1890 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1891 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1892 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1894 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1895 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1897 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1898 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1899 Textures[i].data_sections[idx][s_idx]->size = 0;
1900 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1904 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1905 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1906 Textures[i].data_sections[idx][s_idx] = NULL;
1912 GL_texture_sections = use_sections;
1914 GL_last_detail = Detail.hardware_textures;
1915 GL_last_bitmap_id = -1;
1916 GL_last_bitmap_type = -1;
1918 GL_last_section_x = -1;
1919 GL_last_section_y = -1;
1922 GL_textures_in_frame = 0;
1925 static int opengl_free_texture (tcache_slot_opengl *t);
1927 void opengl_free_texture_with_handle(int handle)
1929 for(int i=0; i<MAX_BITMAPS; i++ ) {
1930 if (Textures[i].bitmap_id == handle) {
1931 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1932 opengl_free_texture ( &Textures[i] );
1937 static void opengl_tcache_flush ()
1941 for( i=0; i<MAX_BITMAPS; i++ ) {
1942 opengl_free_texture ( &Textures[i] );
1944 if (GL_textures_in != 0) {
1945 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1949 GL_last_bitmap_id = -1;
1950 GL_last_section_x = -1;
1951 GL_last_section_y = -1;
1954 static void opengl_tcache_cleanup ()
1956 opengl_tcache_flush ();
1959 GL_textures_in_frame = 0;
1966 if( Texture_sections != NULL ){
1967 free(Texture_sections);
1968 Texture_sections = NULL;
1972 static void opengl_tcache_frame ()
1974 GL_last_bitmap_id = -1;
1975 GL_textures_in_frame = 0;
1982 for( i=0; i<MAX_BITMAPS; i++ ) {
1983 Textures[i].used_this_frame = 0;
1986 if(Textures[i].data_sections[0][0] != NULL){
1987 Assert(GL_texture_sections);
1988 if(GL_texture_sections){
1989 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1990 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1991 if(Textures[i].data_sections[idx][s_idx] != NULL){
1992 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
2002 opengl_tcache_flush();
2007 static int opengl_free_texture ( tcache_slot_opengl *t )
2013 if ( t->bitmap_id > -1 ) {
2014 // if I, or any of my children have been used this frame, bail
2015 if(t->used_this_frame == GL_frame_count){
2018 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2019 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2020 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
2026 // ok, now we know its legal to free everything safely
2027 t->texture_mode = (gr_texture_source) -1;
2028 glDeleteTextures (1, &t->texture_handle);
2029 t->texture_handle = 0;
2031 if ( GL_last_bitmap_id == t->bitmap_id ) {
2032 GL_last_bitmap_id = -1;
2035 // if this guy has children, free them too, since the children
2036 // actually make up his size
2037 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2038 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2039 if(t->data_sections[idx][s_idx] != NULL){
2040 opengl_free_texture(t->data_sections[idx][s_idx]);
2046 t->used_this_frame = 0;
2047 GL_textures_in -= t->size;
2053 static void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
2059 if((w_out == NULL) || (h_out == NULL)){
2067 // set height and width to a power of 2
2068 for (i=0; i<16; i++ ) {
2069 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2075 for (i=0; i<16; i++ ) {
2076 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2082 // try to keep an 8:1 size ratio
2083 if (tex_w/tex_h > 8)
2085 if (tex_h/tex_w > 8)
2088 if ( tex_w < GL_min_texture_width ) {
2089 tex_w = GL_min_texture_width;
2090 } else if ( tex_w > GL_max_texture_width ) {
2091 tex_w = GL_max_texture_width;
2094 if ( tex_h < GL_min_texture_height ) {
2095 tex_h = GL_min_texture_height;
2096 } else if ( tex_h > GL_max_texture_height ) {
2097 tex_h = GL_max_texture_height;
2100 if ( GL_square_textures) {
2102 // Make the both be equal to larger of the two
2103 new_size = max(tex_w, tex_h);
2108 // store the outgoing size
2113 // data == start of bitmap data
2114 // sx == x offset into bitmap
2115 // sy == y offset into bitmap
2116 // src_w == absolute width of section on source bitmap
2117 // src_h == absolute height of section on source bitmap
2118 // bmap_w == width of source bitmap
2119 // bmap_h == height of source bitmap
2120 // tex_w == width of final texture
2121 // tex_h == height of final texture
2122 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)
2132 if ( t->used_this_frame == GL_frame_count ) {
2133 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2138 if(!opengl_free_texture(t)){
2143 // get final texture size
2144 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2146 if ( (tex_w < 1) || (tex_h < 1) ) {
2147 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2151 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2152 t->u_scale = (float)bmap_w / (float)tex_w;
2153 t->v_scale = (float)bmap_h / (float)tex_h;
2154 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2155 t->u_scale = (float)src_w / (float)tex_w;
2156 t->v_scale = (float)src_h / (float)tex_h;
2163 glGenTextures (1, &t->texture_handle);
2166 if (t->texture_handle == 0) {
2167 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2171 GL_bound_texture = t;
2173 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2175 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2177 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2178 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2179 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2181 /* this should be set next anyway */
2182 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2183 // gr_opengl_set_texture_state(GL_current_texture_source);
2188 switch (bitmap_type) {
2190 case TCACHE_TYPE_AABITMAP:
2193 ubyte *bmp_data = ((ubyte*)data);
2199 for (i=0; i<16; i++) {
2200 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2203 for ( ; i<256; i++ ) {
2207 texmem = (ubyte *) malloc (tex_w*tex_h);
2210 for (i=0;i<tex_h;i++)
2212 for (j=0;j<tex_w;j++)
2214 if (i < bmap_h && j < bmap_w) {
2215 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2225 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2227 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2234 case TCACHE_TYPE_BITMAP_SECTION:
2237 ubyte *bmp_data = ((ubyte*)data);
2238 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2239 ubyte *texmemp = texmem;
2241 for (i=0;i<tex_h;i++)
2243 for (j=0;j<tex_w;j++)
2245 if (i < src_h && j < src_w) {
2246 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2247 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2255 size = tex_w*tex_h*2;
2258 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2260 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2269 ubyte *bmp_data = ((ubyte*)data);
2270 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2271 ubyte *texmemp = texmem;
2273 fix u, utmp, v, du, dv;
2277 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2278 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2280 for (j=0;j<tex_h;j++)
2283 for (i=0;i<tex_w;i++)
2285 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2286 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2292 size = tex_w*tex_h*2;
2295 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2297 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2306 t->bitmap_id = texture_handle;
2307 t->time_created = GL_frame_count;
2308 t->used_this_frame = 0;
2310 t->w = (ushort)tex_w;
2311 t->h = (ushort)tex_h;
2313 GL_textures_in_frame += t->size;
2315 GL_textures_in += t->size;
2321 static int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2325 int final_w, final_h;
2329 // setup texture/bitmap flags
2331 switch(bitmap_type){
2332 case TCACHE_TYPE_AABITMAP:
2333 flags |= BMP_AABITMAP;
2336 case TCACHE_TYPE_NORMAL:
2337 flags |= BMP_TEX_OTHER;
2338 case TCACHE_TYPE_XPARENT:
2339 flags |= BMP_TEX_XPARENT;
2341 case TCACHE_TYPE_NONDARKENING:
2343 flags |= BMP_TEX_NONDARK;
2347 // lock the bitmap into the proper format
2348 bmp = bm_lock(bitmap_handle, bpp, flags);
2349 if ( bmp == NULL ) {
2350 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2359 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2360 // max_w /= D3D_texture_divider;
2361 // max_h /= D3D_texture_divider;
2363 // Detail.debris_culling goes from 0 to 4.
2364 max_w /= (16 >> Detail.hardware_textures);
2365 max_h /= (16 >> Detail.hardware_textures);
2369 // get final texture size as it will be allocated as a DD surface
2370 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2372 // if this tcache slot has no bitmap
2373 if ( tslot->bitmap_id < 0) {
2376 // different bitmap altogether - determine if the new one can use the old one's slot
2377 else if (tslot->bitmap_id != bitmap_handle) {
2378 if((final_w == tslot->w) && (final_h == tslot->h)){
2380 //ml_printf("Reloading texture %d\n", bitmap_handle);
2387 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);
2389 // unlock the bitmap
2390 bm_unlock(bitmap_handle);
2395 static int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2399 int final_w, final_h;
2400 int section_x, section_y;
2403 // setup texture/bitmap flags
2404 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2405 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2406 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2408 flags = BMP_TEX_XPARENT;
2410 // lock the bitmap in the proper format
2411 bmp = bm_lock(bitmap_handle, 16, flags);
2412 if ( bmp == NULL ) {
2413 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2416 // determine the width and height of this section
2417 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2419 // get final texture size as it will be allocated as an opengl texture
2420 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2422 // if this tcache slot has no bitmap
2423 if ( tslot->bitmap_id < 0) {
2426 // different bitmap altogether - determine if the new one can use the old one's slot
2427 else if (tslot->bitmap_id != bitmap_handle) {
2428 if((final_w == tslot->w) && (final_h == tslot->h)){
2436 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);
2438 // unlock the bitmap
2439 bm_unlock(bitmap_handle);
2445 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2446 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)
2455 GL_last_bitmap_id = -1;
2459 if ( GL_last_detail != Detail.hardware_textures ) {
2460 GL_last_detail = Detail.hardware_textures;
2461 opengl_tcache_flush();
2468 int n = bm_get_cache_slot (bitmap_id, 1);
2469 tcache_slot_opengl *t = &Textures[n];
2471 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)) {
2472 t->used_this_frame = GL_frame_count;
2474 // mark all children as used
2475 if(GL_texture_sections){
2476 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2477 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2478 if(t->data_sections[idx][s_idx] != NULL){
2479 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2485 *u_scale = t->u_scale;
2486 *v_scale = t->v_scale;
2490 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2491 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2492 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2498 // if the texture sections haven't been created yet
2499 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2501 // lock the bitmap in the proper format
2502 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2503 bm_unlock(bitmap_id);
2505 // now lets do something for each texture
2507 for(idx=0; idx<bmp->sections.num_x; idx++){
2508 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2509 // hmm. i'd rather we didn't have to do it this way...
2510 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2514 // not used this frame
2515 t->data_sections[idx][s_idx]->used_this_frame = 0;
2519 // zero out pretty much everything in the parent struct since he's just the root
2520 t->bitmap_id = bitmap_id;
2521 t->texture_handle = 0;
2522 t->time_created = t->data_sections[sx][sy]->time_created;
2523 t->used_this_frame = 0;
2525 t->vram_texture = NULL;
2526 t->vram_texture_surface = NULL
2530 // argh. we failed to upload. free anything we can
2532 opengl_free_texture(t);
2534 // swap in the texture we want
2536 t = t->data_sections[sx][sy];
2539 // all other "normal" textures
2540 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2541 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2544 // everything went ok
2545 if(ret_val && (t->texture_handle) && !vram_full){
2546 *u_scale = t->u_scale;
2547 *v_scale = t->v_scale;
2549 GL_bound_texture = t;
2551 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2553 /* this should be set next anyway */
2554 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2555 // gr_opengl_set_texture_state(GL_current_texture_source);
2558 GL_last_bitmap_id = t->bitmap_id;
2559 GL_last_bitmap_type = bitmap_type;
2560 GL_last_section_x = sx;
2561 GL_last_section_y = sy;
2563 t->used_this_frame = GL_frame_count;
2567 GL_last_bitmap_id = -1;
2568 GL_last_bitmap_type = -1;
2570 GL_last_section_x = -1;
2571 GL_last_section_y = -1;
2573 GL_bound_texture = NULL;
2575 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2582 void gr_opengl_set_clear_color(int r, int g, int b)
2584 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2587 void gr_opengl_flash(int r, int g, int b)
2593 if ( r || g || b ) {
2594 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2596 float x1, x2, y1, y2;
2597 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2598 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2599 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2600 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2602 glColor4ub(r, g, b, 255);
2604 glVertex3f (x1, y2, -0.99);
2606 glVertex3f (x2, y2, -0.99);
2608 glVertex3f (x2, y1, -0.99);
2610 glVertex3f (x1, y1, -0.99);
2615 int gr_opengl_zbuffer_get()
2617 if ( !gr_global_zbuffering ) {
2618 return GR_ZBUFF_NONE;
2620 return gr_zbuffering_mode;
2623 int gr_opengl_zbuffer_set(int mode)
2625 int tmp = gr_zbuffering_mode;
2627 gr_zbuffering_mode = mode;
2629 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2637 void gr_opengl_zbuffer_clear(int mode)
2641 gr_zbuffering_mode = GR_ZBUFF_FULL;
2642 gr_global_zbuffering = 1;
2644 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2645 glClear ( GL_DEPTH_BUFFER_BIT );
2648 gr_zbuffering_mode = GR_ZBUFF_NONE;
2649 gr_global_zbuffering = 0;
2653 void gr_opengl_set_gamma(float gamma)
2656 Gr_gamma_int = int (Gr_gamma*10);
2658 // Create the Gamma lookup table
2660 for (i=0;i<256; i++) {
2661 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2664 } else if ( v < 0 ) {
2667 Gr_gamma_lookup[i] = v;
2670 // Flush any existing textures
2671 opengl_tcache_flush();
2674 void gr_opengl_fade_in(int instantaneous)
2679 void gr_opengl_fade_out(int instantaneous)
2684 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2687 glReadBuffer(GL_FRONT);
2689 glReadBuffer(GL_BACK);
2692 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2694 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2696 if (gr_screen.bits_per_pixel == 15) {
2697 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2698 } else if (gr_screen.bits_per_pixel == 32) {
2699 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2702 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2705 static int Gr_opengl_mouse_saved = 0;
2706 static int Gr_opengl_mouse_saved_x1 = 0;
2707 static int Gr_opengl_mouse_saved_y1 = 0;
2708 static int Gr_opengl_mouse_saved_x2 = 0;
2709 static int Gr_opengl_mouse_saved_y2 = 0;
2710 static int Gr_opengl_mouse_saved_w = 0;
2711 static int Gr_opengl_mouse_saved_h = 0;
2712 #define MAX_SAVE_SIZE (32*32)
2713 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2715 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2717 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2719 Gr_opengl_mouse_saved_x1 = x;
2720 Gr_opengl_mouse_saved_y1 = y;
2721 Gr_opengl_mouse_saved_x2 = x+w-1;
2722 Gr_opengl_mouse_saved_y2 = y+h-1;
2724 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2725 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2726 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2727 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2729 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2730 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2732 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2733 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2735 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2737 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2739 glReadBuffer(GL_BACK);
2740 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);
2742 Gr_opengl_mouse_saved = 1;
2745 int gr_opengl_save_screen()
2749 if ( Gr_saved_screen ) {
2750 mprintf(( "Screen alread saved!\n" ));
2754 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2755 if (!Gr_saved_screen) {
2756 mprintf(( "Couldn't get memory for saved screen!\n" ));
2760 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2761 if (!Gr_saved_screen_tmp) {
2762 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2766 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2768 glReadBuffer(GL_FRONT);
2769 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);
2773 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2774 dptr = (ubyte *)Gr_saved_screen;
2775 for (int j = 0; j < gr_screen.max_h; j++) {
2776 sptr -= gr_screen.max_w*2;
2777 memcpy(dptr, sptr, gr_screen.max_w*2);
2778 dptr += gr_screen.max_w*2;
2781 free(Gr_saved_screen_tmp);
2783 if (Gr_opengl_mouse_saved) {
2784 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2785 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2786 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2787 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2790 dptr -= gr_screen.max_w*2;
2794 // this leaks texture handles, and the opengl doesn't currently
2795 // perform some sort of garbage collection, so a hack was added
2796 // to bmpman to make it free textures when released
2797 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2799 return Gr_saved_screen_bitmap;
2802 void gr_opengl_restore_screen(int id)
2806 if ( !Gr_saved_screen ) {
2811 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2813 gr_set_bitmap(Gr_saved_screen_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
2817 void gr_opengl_free_screen(int id)
2819 bm_release(Gr_saved_screen_bitmap);
2821 if ( Gr_saved_screen ) {
2822 free( Gr_saved_screen );
2823 Gr_saved_screen = NULL;
2827 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2832 void gr_opengl_dump_frame_stop()
2837 void gr_opengl_dump_frame()
2842 uint gr_opengl_lock()
2849 void gr_opengl_unlock()
2853 void opengl_zbias(int bias)
2856 glEnable(GL_POLYGON_OFFSET_FILL);
2857 glPolygonOffset(0.0, -bias);
2859 glDisable(GL_POLYGON_OFFSET_FILL);
2863 extern char *Osreg_title;
2864 void gr_opengl_init()
2867 gr_opengl_cleanup();
2871 mprintf(( "Initializing opengl graphics device...\n" ));
2875 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2877 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2881 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2882 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2883 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2884 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2885 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2887 int flags = SDL_OPENGL;
2889 if (!Cmdline_window && ( (os_config_read_uint( NULL, "Fullscreen", 1 ) == 1) || Cmdline_fullscreen ))
2890 flags |= SDL_FULLSCREEN;
2892 // grab mouse/key unless told otherwise, ignore when we are going fullscreen
2893 if ( !((flags & SDL_FULLSCREEN) || Cmdline_no_grab) ) {
2894 SDL_WM_GrabInput(SDL_GRAB_ON);
2897 FSAA = os_config_read_uint( NULL, "FSAA", 1 );
2899 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
2900 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, FSAA );
2903 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2905 mprintf(( "Couldn't set FSAA video mode: %s\n", SDL_GetError () ));
2906 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0 );
2907 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0 );
2909 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2911 fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ());
2916 mprintf(( "Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel ));
2918 mprintf(( "Vendor : %s\n", glGetString(GL_VENDOR) ));
2919 mprintf(( "Renderer : %s\n", glGetString(GL_RENDERER) ));
2920 mprintf(( "Version : %s\n", glGetString(GL_VERSION) ));
2923 // print out extensions - taken from FS2_Open (credits: phreak, taylor)
2924 mprintf(( "Extensions : \n"));
2926 static const char *OGL_extensions = (const char*)glGetString(GL_EXTENSIONS);
2928 char *extlist = (char*)malloc(strlen(OGL_extensions));
2930 if (extlist != NULL) {
2931 memcpy(extlist, OGL_extensions, strlen(OGL_extensions));
2933 char *curext = strtok(extlist, " ");
2936 mprintf(( " %s\n", curext ));
2937 curext = strtok(NULL, " ");
2954 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
2955 mprintf(( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value ));
2956 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
2957 mprintf(( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value ));
2958 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
2959 mprintf(( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value ));
2960 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
2961 mprintf(( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value ));
2962 SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
2963 mprintf(( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value ));
2966 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &value );
2967 mprintf(( "SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value ));
2968 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &value );
2969 mprintf(( "SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", FSAA, value ));
2973 SDL_WM_SetCaption (Osreg_title, NULL);
2975 /* might as well put this here */
2976 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2979 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2981 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2983 glMatrixMode(GL_PROJECTION);
2985 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2986 glMatrixMode(GL_MODELVIEW);
2989 glShadeModel(GL_SMOOTH);
2990 glEnable(GL_DITHER);
2991 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2992 glHint(GL_FOG_HINT, GL_NICEST);
2994 glEnable(GL_DEPTH_TEST);
2997 glEnable(GL_TEXTURE_2D);
2999 glDepthRange(0.0, 1.0);
3001 glPixelStorei(GL_PACK_ALIGNMENT, 1);
3002 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3004 D3D_32bit = 1; // grd3d.cpp
3005 extern int D3D_enabled;
3008 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
3009 1 = use secondary color ext
3010 2 = use opengl linear fog
3012 D3D_fog_mode = 2; // grd3d.cpp
3016 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
3021 gr_screen.bits_per_pixel = 15;
3022 gr_screen.bytes_per_pixel = 2;
3027 Gr_red.mask = 0x7C00;
3032 Gr_green.mask = 0x3E0;
3037 Gr_blue.mask = 0x1F;
3042 gr_screen.bits_per_pixel = 16;
3043 gr_screen.bytes_per_pixel = 2;
3048 Gr_red.mask = 0xF800;
3053 Gr_green.mask = 0x7E0;
3058 Gr_blue.mask = 0x1F;
3063 gr_screen.bits_per_pixel = 32;
3064 gr_screen.bytes_per_pixel = 4;
3069 Gr_red.mask = 0xff0000;
3074 Gr_green.mask = 0xff00;
3079 Gr_blue.mask = 0xff;
3084 Int3(); // Illegal bpp
3087 // DDOI - set these so no one else does!
3088 Gr_t_red.mask = Gr_red.mask;
3089 Gr_t_red.shift = Gr_red.shift;
3090 Gr_t_red.scale = Gr_red.scale;
3092 Gr_t_green.mask = Gr_green.mask;
3093 Gr_t_green.shift = Gr_green.shift;
3094 Gr_t_green.scale = Gr_green.scale;
3096 Gr_t_blue.mask = Gr_blue.mask;
3097 Gr_t_blue.shift = Gr_blue.shift;
3098 Gr_t_blue.scale = Gr_blue.scale;
3100 Gr_t_alpha.mask = 0x8000;
3101 Gr_t_alpha.scale = 255;
3102 Gr_t_alpha.shift = 15;
3104 Gr_ta_red.mask = 0x0f00;
3105 Gr_ta_red.shift = 8;
3106 Gr_ta_red.scale = 16;
3108 Gr_ta_green.mask = 0x00f0;
3109 Gr_ta_green.shift = 4;
3110 Gr_ta_green.scale = 16;
3112 Gr_ta_blue.mask = 0x000f;
3113 Gr_ta_blue.shift = 0;
3114 Gr_ta_blue.scale = 16;
3116 Gr_ta_alpha.mask = 0xf000;
3117 Gr_ta_alpha.shift = 12;
3118 Gr_ta_alpha.scale = 16;
3121 opengl_tcache_init (1);
3124 Gr_current_red = &Gr_red;
3125 Gr_current_blue = &Gr_blue;
3126 Gr_current_green = &Gr_green;
3127 Gr_current_alpha = &Gr_alpha;
3129 gr_screen.gf_flip = gr_opengl_flip;
3130 gr_screen.gf_flip_window = gr_opengl_flip_window;
3131 gr_screen.gf_set_clip = gr_opengl_set_clip;
3132 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3133 gr_screen.gf_set_font = grx_set_font;
3135 gr_screen.gf_set_color = gr_opengl_set_color;
3136 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3137 gr_screen.gf_create_shader = gr_opengl_create_shader;
3138 gr_screen.gf_set_shader = gr_opengl_set_shader;
3139 gr_screen.gf_clear = gr_opengl_clear;
3140 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3141 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3142 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3143 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3145 gr_screen.gf_rect = gr_opengl_rect;
3146 gr_screen.gf_shade = gr_opengl_shade;
3147 gr_screen.gf_string = gr_opengl_string;
3148 gr_screen.gf_circle = gr_opengl_circle;
3150 gr_screen.gf_line = gr_opengl_line;
3151 gr_screen.gf_aaline = gr_opengl_aaline;
3152 gr_screen.gf_pixel = gr_opengl_pixel;
3153 gr_screen.gf_scaler = gr_opengl_scaler;
3154 gr_screen.gf_tmapper = gr_opengl_tmapper;
3156 gr_screen.gf_gradient = gr_opengl_gradient;
3158 gr_screen.gf_set_palette = gr_opengl_set_palette;
3159 gr_screen.gf_get_color = gr_opengl_get_color;
3160 gr_screen.gf_init_color = gr_opengl_init_color;
3161 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3162 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3163 gr_screen.gf_print_screen = gr_opengl_print_screen;
3165 gr_screen.gf_fade_in = gr_opengl_fade_in;
3166 gr_screen.gf_fade_out = gr_opengl_fade_out;
3167 gr_screen.gf_flash = gr_opengl_flash;
3169 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3170 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3171 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3173 gr_screen.gf_save_screen = gr_opengl_save_screen;
3174 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3175 gr_screen.gf_free_screen = gr_opengl_free_screen;
3177 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3178 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3179 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3181 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3183 gr_screen.gf_lock = gr_opengl_lock;
3184 gr_screen.gf_unlock = gr_opengl_unlock;
3186 gr_screen.gf_fog_set = gr_opengl_fog_set;
3188 gr_screen.gf_get_region = gr_opengl_get_region;
3190 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3192 gr_screen.gf_set_cull = gr_opengl_set_cull;
3194 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3196 gr_screen.gf_filter_set = gr_opengl_filter_set;
3198 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3200 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;