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.69 2004/07/04 11:31:43 taylor
19 * amd64 support, compiler warning fixes, don't use software rendering
21 * Revision 1.68 2004/06/11 01:01:07 tigital
22 * added FSAA support: switched on in .ini
24 * Revision 1.67 2003/08/03 15:59:40 taylor
25 * GL_RGB5_A1 as TexImage internal format; cleaner input grab; min window title; cleanup
27 * Revision 1.66 2003/06/22 12:52:34 taylor
28 * more texture size fixin
30 * Revision 1.65 2003/06/19 11:52:47 taylor
31 * fix texture size issue with lower detail settings
33 * Revision 1.64 2003/05/28 06:02:04 taylor
34 * fix transparency in green weapon blobs
36 * Revision 1.63 2003/05/09 05:05:52 taylor
37 * improve gr_(de)activate, mouse grab, default fullscreen
39 * Revision 1.62 2003/05/04 04:56:53 taylor
40 * move SDL_Quit to os_deinit to fix fonttool segfault
42 * Revision 1.61 2003/02/02 21:13:27 relnev
43 * minor updates (made functions static, tried to improve texture sizes)
45 * Revision 1.60 2003/02/02 20:20:03 relnev
46 * add large texture support (Taylor Richards)
48 * Revision 1.59 2002/09/04 01:38:09 relnev
51 * Revision 1.57 2002/08/31 01:39:13 theoddone33
52 * Speed up the renderer a tad
54 * Revision 1.56 2002/08/01 05:03:11 relnev
57 * Revision 1.55 2002/08/01 04:55:45 relnev
58 * experimenting with texture state
60 * Revision 1.54 2002/07/30 15:00:15 relnev
61 * not use luminance alpha by default
63 * Revision 1.53 2002/06/22 17:08:16 relnev
64 * more fixes to unused alpha code
66 * Revision 1.52 2002/06/21 23:59:14 relnev
67 * moved a gr_opengl_set_state to after the gr_tcache_set
69 * Revision 1.51 2002/06/19 04:52:45 relnev
70 * MacOS X updates (Ryan)
72 * Revision 1.50 2002/06/18 23:20:44 relnev
75 * adjusted line offsets.
77 * Revision 1.49 2002/06/17 23:11:39 relnev
78 * enable sdl key repeating.
82 * Revision 1.48 2002/06/09 04:41:17 relnev
83 * added copyright header
85 * Revision 1.47 2002/06/09 03:16:04 relnev
88 * removed unneeded asm, old sdl 2d setup.
90 * fixed crash caused by opengl_get_region.
92 * Revision 1.46 2002/06/05 04:03:32 relnev
93 * finished cfilesystem.
95 * removed some old code.
97 * fixed mouse save off-by-one.
101 * Revision 1.45 2002/06/03 09:25:37 relnev
102 * implement mouse cursor and screen save/restore
104 * Revision 1.44 2002/06/02 18:46:59 relnev
107 * Revision 1.43 2002/06/02 11:34:00 relnev
110 * Revision 1.42 2002/06/02 10:28:17 relnev
111 * fix texture handle leak
113 * Revision 1.41 2002/06/01 09:00:34 relnev
114 * silly debug memmanager
116 * Revision 1.40 2002/06/01 07:12:33 relnev
117 * a few NDEBUG updates.
119 * removed a few warnings.
121 * Revision 1.39 2002/06/01 05:33:15 relnev
122 * copied more code over.
124 * added scissor clipping.
126 * Revision 1.38 2002/06/01 03:35:27 relnev
129 * Revision 1.37 2002/06/01 03:32:00 relnev
130 * fix texture loading mistake.
132 * enable some d3d stuff for opengl also
134 * Revision 1.36 2002/05/31 23:25:03 relnev
137 * Revision 1.34 2002/05/31 22:15:22 relnev
140 * Revision 1.33 2002/05/31 22:04:55 relnev
141 * use d3d rect_internal
143 * Revision 1.32 2002/05/31 06:28:23 relnev
146 * Revision 1.31 2002/05/31 06:04:39 relnev
149 * Revision 1.30 2002/05/31 03:56:11 theoddone33
150 * Change tmapper polygon winding and enable culling
152 * Revision 1.29 2002/05/31 03:34:02 theoddone33
156 * Revision 1.28 2002/05/31 00:06:59 relnev
159 * Revision 1.27 2002/05/30 23:46:29 theoddone33
160 * some minor key changes (not necessarily fixes)
162 * Revision 1.26 2002/05/30 23:33:12 relnev
163 * implemented a few more functions.
165 * Revision 1.25 2002/05/30 23:01:16 relnev
166 * implement gr_opengl_set_state.
168 * Revision 1.24 2002/05/30 22:12:57 relnev
169 * finish default texture case
171 * Revision 1.23 2002/05/30 22:02:30 theoddone33
174 * Revision 1.22 2002/05/30 21:44:48 relnev
175 * implemented some missing texture stuff.
177 * enable bitmap polys for opengl.
179 * work around greenness in bitmaps.
181 * Revision 1.21 2002/05/30 17:29:30 theoddone33
182 * Fix some more stubs, change at least one polygon winding since culling is now
185 * Revision 1.20 2002/05/30 16:50:24 theoddone33
186 * Keyboard partially fixed
188 * Revision 1.19 2002/05/30 08:13:14 relnev
191 * Revision 1.18 2002/05/29 23:37:36 relnev
194 * Revision 1.17 2002/05/29 23:17:49 theoddone33
195 * Non working text code and fixed keys
197 * Revision 1.16 2002/05/29 19:45:13 theoddone33
198 * More changes on texture loading
200 * Revision 1.15 2002/05/29 19:06:48 theoddone33
201 * Enable string printing. Enable texture mapping
203 * Revision 1.14 2002/05/29 08:54:40 relnev
204 * "fixed" bitmap drawing.
206 * copied more d3d code over.
208 * Revision 1.13 2002/05/29 06:25:13 theoddone33
209 * Keyboard input, mouse tracking now work
211 * Revision 1.12 2002/05/29 04:52:45 relnev
214 * Revision 1.11 2002/05/29 04:29:56 relnev
215 * removed some unncessary stubbing, implemented opengl rect
217 * Revision 1.10 2002/05/29 04:13:27 theoddone33
220 * Revision 1.9 2002/05/29 03:35:51 relnev
223 * Revision 1.8 2002/05/29 03:30:05 relnev
224 * update opengl stubs
226 * Revision 1.7 2002/05/29 02:52:32 theoddone33
227 * Enable OpenGL renderer
229 * Revision 1.6 2002/05/28 04:56:51 theoddone33
230 * runs a little bit now
232 * Revision 1.5 2002/05/28 04:07:28 theoddone33
233 * New graphics stubbing arrangement
235 * Revision 1.4 2002/05/27 23:39:34 relnev
238 * Revision 1.3 2002/05/27 22:35:01 theoddone33
241 * Revision 1.2 2002/05/27 22:32:02 theoddone33
242 * throw all d3d stuff at opengl
244 * Revision 1.1.1.1 2002/05/03 03:28:09 root
248 * 10 7/14/99 9:42a Dave
249 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
252 * 9 7/09/99 9:51a Dave
253 * Added thick polyline code.
255 * 8 6/29/99 10:35a Dave
256 * Interface polygon bitmaps! Whee!
258 * 7 2/03/99 11:44a Dave
259 * Fixed d3d transparent textures.
261 * 6 1/24/99 11:37p Dave
262 * First full rev of beam weapons. Very customizable. Removed some bogus
263 * Int3()'s in low level net code.
265 * 5 12/18/98 1:13a Dave
266 * Rough 1024x768 support for Direct3D. Proper detection and usage through
269 * 4 12/06/98 2:36p Dave
270 * Drastically improved nebula fogging.
272 * 3 11/11/98 5:37p Dave
273 * Checkin for multiplayer testing.
275 * 2 10/07/98 10:53a Dave
278 * 1 10/07/98 10:49a Dave
280 * 14 5/20/98 9:46p John
281 * added code so the places in code that change half the palette don't
282 * have to clear the screen.
284 * 13 5/06/98 5:30p John
285 * Removed unused cfilearchiver. Removed/replaced some unused/little used
286 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
287 * DirectX header files and libs that fixed the Direct3D alpha blending
290 * 12 4/14/98 12:15p John
291 * Made 16-bpp movies work.
293 * 11 3/12/98 5:36p John
294 * Took out any unused shaders. Made shader code take rgbc instead of
295 * matrix and vector since noone used it like a matrix and it would have
296 * been impossible to do in hardware. Made Glide implement a basic
297 * shader for online help.
299 * 10 3/10/98 4:18p John
300 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
301 * & Glide have popups and print screen. Took out all >8bpp software
302 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
303 * support Fred. Made zbuffering key off of functions rather than one
306 * 9 12/02/97 4:00p John
307 * Added first rev of thruster glow, along with variable levels of
308 * translucency, which retquired some restructing of palman.
310 * 8 10/03/97 9:10a John
311 * added better antialiased line drawer
313 * 7 9/23/97 10:45a John
314 * made so you can tell bitblt code to rle a bitmap by passing flag to
317 * 6 9/09/97 11:01a Sandeep
318 * fixed warning level 4 bugs
320 * 5 7/10/97 2:06p John
321 * added code to specify alphablending type for bitmaps.
323 * 4 6/17/97 7:04p John
324 * added d3d support for gradients.
325 * fixed some color bugs by adding screen signatures instead of watching
326 * flags and palette changes.
328 * 3 6/12/97 2:50a Lawrance
329 * bm_unlock() now passed bitmap number, not pointer
331 * 2 6/11/97 1:12p John
332 * Started fixing all the text colors in the game.
334 * 1 5/12/97 12:14p John
341 #include <windowsx.h>
345 #include <OpenGL/gl.h>
355 #include "floating.h"
357 #include "systemvars.h"
358 #include "grinternal.h"
359 #include "gropengl.h"
363 #include "osregistry.h"
366 static int Inited = 0;
368 typedef enum gr_texture_source {
370 TEXTURE_SOURCE_DECAL,
371 TEXTURE_SOURCE_NO_FILTERING,
374 typedef enum gr_alpha_blend {
375 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
376 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
377 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
378 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
379 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
382 typedef enum gr_zbuffer_type {
389 #define NEBULA_COLORS 20
391 volatile int GL_activate = 0;
392 volatile int GL_deactivate = 0;
394 static int GL_use_luminance_alpha;
397 static char *Gr_saved_screen = NULL;
398 static int Gr_saved_screen_bitmap;
401 // Throw in some dummy functions - DDOI
403 int D3D_32bit = 0; // grd3d.cpp
404 int D3D_fog_mode = -1; // grd3d.cpp
405 int D3D_inited = 0; // grd3d.cpp
406 int D3D_zbias = 1; // grd3d.cpp
407 int D3d_rendition_uvs = 0; // grd3d.cpp
414 void d3d_zbias (int a)
420 static void gr_opengl_set_texture_state(gr_texture_source ts);
422 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
423 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
425 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
427 gr_opengl_set_texture_state(ts);
429 if (ab != GL_current_alpha_blend) {
431 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
432 glBlendFunc(GL_ONE, GL_ZERO);
434 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
435 glBlendFunc(GL_ONE, GL_ONE);
437 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
438 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
440 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
441 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
443 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
444 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
450 GL_current_alpha_blend = ab;
453 if (zt != GL_current_zbuffer_type) {
455 case ZBUFFER_TYPE_NONE:
456 glDepthFunc(GL_ALWAYS);
457 glDepthMask(GL_FALSE);
459 case ZBUFFER_TYPE_READ:
460 glDepthFunc(GL_LESS);
461 glDepthMask(GL_FALSE);
463 case ZBUFFER_TYPE_WRITE:
464 glDepthFunc(GL_ALWAYS);
465 glDepthMask(GL_TRUE);
467 case ZBUFFER_TYPE_FULL:
468 glDepthFunc(GL_LESS);
469 glDepthMask(GL_TRUE);
475 GL_current_zbuffer_type = zt;
479 void gr_opengl_activate(int active)
484 // don't grab key/mouse if cmdline says so or if we're fullscreen
485 if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
486 SDL_WM_GrabInput(SDL_GRAB_ON);
491 // let go of mouse/keyboard
492 SDL_WM_GrabInput(SDL_GRAB_OFF);
497 static void opengl_tcache_flush ();
499 void gr_opengl_preload_init()
501 if (gr_screen.mode != GR_OPENGL) {
505 opengl_tcache_flush ();
508 int GL_should_preload = 0;
509 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
511 if ( gr_screen.mode != GR_OPENGL) {
515 if ( !GL_should_preload ) {
519 float u_scale, v_scale;
523 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
525 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
529 mprintf(("Texture upload failed!\n" ));
535 void gr_opengl_pixel(int x, int y)
540 void gr_opengl_clear()
542 glClearColor(gr_screen.current_clear_color.red / 255.0,
543 gr_screen.current_clear_color.green / 255.0,
544 gr_screen.current_clear_color.blue / 255.0, 1.0);
546 glClear ( GL_COLOR_BUFFER_BIT );
549 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
550 static void opengl_tcache_frame ();
552 void gr_opengl_flip()
560 extern int Gr_opengl_mouse_saved;
561 Gr_opengl_mouse_saved = 0;
563 if ( mouse_is_visible() ) {
567 mouse_get_pos( &mx, &my );
569 gr_opengl_save_mouse_area(mx,my,32,32);
571 if ( Gr_cursor == -1 ) {
574 gr_set_bitmap(Gr_cursor);
580 GLenum error = glGetError();
583 error = glGetError();
585 if (error != GL_NO_ERROR) {
586 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
589 } while (error != GL_NO_ERROR);
592 SDL_GL_SwapBuffers ();
594 opengl_tcache_frame ();
596 int cnt = GL_activate;
599 opengl_tcache_flush();
600 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
606 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
610 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
615 void gr_opengl_set_clip(int x,int y,int w,int h)
617 // check for sanity of parameters
623 if (x >= gr_screen.max_w)
624 x = gr_screen.max_w - 1;
625 if (y >= gr_screen.max_h)
626 y = gr_screen.max_h - 1;
628 if (x + w > gr_screen.max_w)
629 w = gr_screen.max_w - x;
630 if (y + h > gr_screen.max_h)
631 h = gr_screen.max_h - y;
633 if (w > gr_screen.max_w)
635 if (h > gr_screen.max_h)
638 gr_screen.offset_x = x;
639 gr_screen.offset_y = y;
640 gr_screen.clip_left = 0;
641 gr_screen.clip_right = w-1;
642 gr_screen.clip_top = 0;
643 gr_screen.clip_bottom = h-1;
644 gr_screen.clip_width = w;
645 gr_screen.clip_height = h;
647 glEnable(GL_SCISSOR_TEST);
648 glScissor(x, gr_screen.max_h-y-h, w, h);
651 void gr_opengl_reset_clip()
653 gr_screen.offset_x = 0;
654 gr_screen.offset_y = 0;
655 gr_screen.clip_left = 0;
656 gr_screen.clip_top = 0;
657 gr_screen.clip_right = gr_screen.max_w - 1;
658 gr_screen.clip_bottom = gr_screen.max_h - 1;
659 gr_screen.clip_width = gr_screen.max_w;
660 gr_screen.clip_height = gr_screen.max_h;
662 glDisable(GL_SCISSOR_TEST);
663 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
666 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
668 gr_screen.current_alpha = alpha;
669 gr_screen.current_alphablend_mode = alphablend_mode;
670 gr_screen.current_bitblt_mode = bitblt_mode;
671 gr_screen.current_bitmap = bitmap_num;
673 gr_screen.current_bitmap_sx = sx;
674 gr_screen.current_bitmap_sy = sy;
677 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
679 shade->screen_sig = gr_screen.signature;
686 void gr_opengl_set_shader( shader * shade )
689 if (shade->screen_sig != gr_screen.signature) {
690 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
692 gr_screen.current_shader = *shade;
694 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
699 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
701 STUB_FUNCTION; /* who called me? */
704 void gr_opengl_bitmap(int x, int y)
706 STUB_FUNCTION; /* who called me? */
709 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
713 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
715 saved_zbuf = gr_zbuffer_get();
717 // start the frame, no zbuffering, no culling
719 gr_zbuffer_set(GR_ZBUFF_NONE);
728 v[0].flags = PF_PROJECTED;
735 v[1].sx = i2fl(x + w);
740 v[1].flags = PF_PROJECTED;
747 v[2].sx = i2fl(x + w);
748 v[2].sy = i2fl(y + h);
752 v[2].flags = PF_PROJECTED;
760 v[3].sy = i2fl(y + h);
764 v[3].flags = PF_PROJECTED;
772 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
776 // restore zbuffer and culling
777 gr_zbuffer_set(saved_zbuf);
781 void gr_opengl_rect(int x,int y,int w,int h)
783 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);
786 void gr_opengl_shade(int x,int y,int w,int h)
793 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
794 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
795 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
796 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
797 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
798 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
799 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
800 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
802 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
805 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
810 if ( !gr_screen.current_color.is_alphacolor ) return;
812 float u_scale, v_scale;
814 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
815 // Couldn't set texture
816 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
820 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
822 float u0, u1, v0, v1;
823 float x1, x2, y1, y2;
826 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
828 u0 = u_scale*i2fl(sx)/i2fl(bw);
829 v0 = v_scale*i2fl(sy)/i2fl(bh);
831 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
832 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
834 x1 = i2fl(x+gr_screen.offset_x);
835 y1 = i2fl(y+gr_screen.offset_y);
836 x2 = i2fl(x+w+gr_screen.offset_x);
837 y2 = i2fl(y+h+gr_screen.offset_y);
839 if ( gr_screen.current_color.is_alphacolor ) {
840 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
842 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
846 glTexCoord2f (u0, v1);
847 glVertex3f (x1, y2, -0.99);
849 glTexCoord2f (u1, v1);
850 glVertex3f (x2, y2, -0.99);
852 glTexCoord2f (u1, v0);
853 glVertex3f (x2, y1, -0.99);
855 glTexCoord2f (u0, v0);
856 glVertex3f (x1, y1, -0.99);
860 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
867 int dx1=x, dx2=x+w-1;
868 int dy1=y, dy2=y+h-1;
871 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
876 if ( count > 1 ) Int3();
880 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
881 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
882 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
883 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
884 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
885 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
912 if ( w < 1 ) return; // clipped away!
913 if ( h < 1 ) return; // clipped away!
917 // Make sure clipping algorithm works
921 Assert( w == (dx2-dx1+1) );
922 Assert( h == (dy2-dy1+1) );
925 Assert( sx+w <= bw );
926 Assert( sy+h <= bh );
927 Assert( dx2 >= dx1 );
928 Assert( dy2 >= dy1 );
929 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
930 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
931 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
932 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
935 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
936 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
939 void gr_opengl_aabitmap(int x, int y)
943 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
944 int dx1=x, dx2=x+w-1;
945 int dy1=y, dy2=y+h-1;
948 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
949 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
950 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
951 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
952 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
953 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
955 if ( sx < 0 ) return;
956 if ( sy < 0 ) return;
957 if ( sx >= w ) return;
958 if ( sy >= h ) return;
960 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
961 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
964 void gr_opengl_string( int sx, int sy, char *s )
966 int width, spacing, letter;
969 if ( !Current_font ) {
973 gr_set_bitmap(Current_font->bitmap_id);
978 if (sx==0x8000) { //centered
979 x = get_centered_x(s);
991 y += Current_font->h;
992 if (sx==0x8000) { //centered
993 x = get_centered_x(s);
1000 letter = get_char_width(s[0],s[1],&width,&spacing);
1003 //not in font, draw as space
1011 // Check if this character is totally clipped
1012 if ( x + width < gr_screen.clip_left ) continue;
1013 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1014 if ( x > gr_screen.clip_right ) continue;
1015 if ( y > gr_screen.clip_bottom ) continue;
1018 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1019 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1023 wc = width - xd; hc = Current_font->h - yd;
1024 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1025 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1027 if ( wc < 1 ) continue;
1028 if ( hc < 1 ) continue;
1030 int u = Current_font->bm_u[letter];
1031 int v = Current_font->bm_v[letter];
1033 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1037 void gr_opengl_line(int x1,int y1,int x2,int y2)
1039 int clipped = 0, swapped=0;
1041 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1043 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);
1048 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1049 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1050 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1051 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1053 if ( x1 == x2 && y1 == y2 ) {
1054 glBegin (GL_POINTS);
1055 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1056 glVertex3f (sx1, sy1, -0.99f);
1068 } else if ( y1 == y2 ) {
1077 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1078 glVertex3f (sx2, sy2, -0.99f);
1079 glVertex3f (sx1, sy1, -0.99f);
1083 void gr_opengl_aaline(vertex *v1, vertex *v2)
1085 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1088 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1090 int clipped = 0, swapped=0;
1092 if ( !gr_screen.current_color.is_alphacolor ) {
1093 gr_line( x1, y1, x2, y2 );
1097 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);
1099 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1101 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1102 int ba = swapped ? gr_screen.current_color.alpha : 0;
1107 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1108 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1109 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1110 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1118 } else if ( y1 == y2 ) {
1127 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1128 glVertex3f (sx2, sy2, -0.99f);
1129 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1130 glVertex3f (sx1, sy1, -0.99f);
1134 void gr_opengl_circle( int xc, int yc, int d )
1144 if ( (xc+r) < gr_screen.clip_left ) return;
1145 if ( (xc-r) > gr_screen.clip_right ) return;
1146 if ( (yc+r) < gr_screen.clip_top ) return;
1147 if ( (yc-r) > gr_screen.clip_bottom ) return;
1150 // Draw the first octant
1151 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1152 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1157 // Draw the second octant
1158 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1159 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1167 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1168 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1173 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1177 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1178 if (f_float < 0.0f) {
1186 *a = (int)(f_float * 255.0);
1189 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1192 float u_scale = 1.0f, v_scale = 1.0f;
1194 // Make nebula use the texture mapper... this blends the colors better.
1195 if ( flags & TMAP_FLAG_NEBULA ){
1199 gr_texture_source texture_source = (gr_texture_source)-1;
1200 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1201 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1203 if ( gr_zbuffering ) {
1204 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1205 zbuffer_type = ZBUFFER_TYPE_READ;
1207 zbuffer_type = ZBUFFER_TYPE_FULL;
1210 zbuffer_type = ZBUFFER_TYPE_NONE;
1215 int tmap_type = TCACHE_TYPE_NORMAL;
1219 if ( flags & TMAP_FLAG_TEXTURED ) {
1222 r = gr_screen.current_color.red;
1223 g = gr_screen.current_color.green;
1224 b = gr_screen.current_color.blue;
1227 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1230 tmap_type = TCACHE_TYPE_NORMAL;
1231 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1233 // Blend with screen pixel using src*alpha+dst
1234 float factor = gr_screen.current_alpha;
1238 if ( factor <= 1.0f ) {
1239 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1240 r = (r*tmp_alpha)/255;
1241 g = (g*tmp_alpha)/255;
1242 b = (b*tmp_alpha)/255;
1245 tmap_type = TCACHE_TYPE_XPARENT;
1247 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1249 // Blend with screen pixel using src*alpha+dst
1250 float factor = gr_screen.current_alpha;
1252 if ( factor > 1.0f ) {
1255 alpha = fl2i(gr_screen.current_alpha*255.0f);
1259 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1260 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1262 alpha_blend = ALPHA_BLEND_NONE;
1267 if(flags & TMAP_FLAG_BITMAP_SECTION){
1268 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1271 texture_source = TEXTURE_SOURCE_NONE;
1273 if ( flags & TMAP_FLAG_TEXTURED ) {
1274 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 ))
1276 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1280 // use nonfiltered textures for bitmap sections
1281 if(flags & TMAP_FLAG_BITMAP_SECTION){
1282 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1284 texture_source = TEXTURE_SOURCE_DECAL;
1289 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1291 if ( flags & TMAP_FLAG_TEXTURED )
1297 if (flags & TMAP_FLAG_PIXEL_FOG) {
1303 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1305 vertex * va = verts[i];
1309 x = fl2i(va->sx*16.0f);
1310 y = fl2i(va->sy*16.0f);
1312 x += gr_screen.offset_x*16;
1313 y += gr_screen.offset_y*16;
1315 sx = i2fl(x) / 16.0f;
1316 sy = i2fl(y) / 16.0f;
1318 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1329 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1332 glBegin(GL_TRIANGLE_FAN);
1333 for (i = nv-1; i >= 0; i--) {
1334 vertex * va = verts[i];
1340 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1341 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1350 if ( flags & TMAP_FLAG_CORRECT ) {
1356 if (flags & TMAP_FLAG_ALPHA) {
1362 if (flags & TMAP_FLAG_NEBULA ) {
1363 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1364 r = gr_palette[pal*3+0];
1365 g = gr_palette[pal*3+1];
1366 b = gr_palette[pal*3+2];
1367 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1368 r = Gr_gamma_lookup[verts[i]->b];
1369 g = Gr_gamma_lookup[verts[i]->b];
1370 b = Gr_gamma_lookup[verts[i]->b];
1371 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1372 // Make 0.75 be 256.0f
1373 r = Gr_gamma_lookup[verts[i]->r];
1374 g = Gr_gamma_lookup[verts[i]->g];
1375 b = Gr_gamma_lookup[verts[i]->b];
1377 // use constant RGB values...
1379 glColor4ub (r,g,b,a);
1381 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1384 /* this is for GL_EXT_SECONDARY_COLOR */
1385 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1386 /* do separate color call here */
1392 x = fl2i(va->sx*16.0f);
1393 y = fl2i(va->sy*16.0f);
1395 x += gr_screen.offset_x*16;
1396 y += gr_screen.offset_y*16;
1398 sx = i2fl(x) / 16.0f;
1399 sy = i2fl(y) / 16.0f;
1401 if ( flags & TMAP_FLAG_TEXTURED ) {
1404 glTexCoord2f(tu, tv);
1407 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1412 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1414 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1417 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1419 void gr_opengl_scaler(vertex *va, vertex *vb )
1421 float x0, y0, x1, y1;
1422 float u0, v0, u1, v1;
1423 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1424 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1425 float xmin, xmax, ymin, ymax;
1426 int dx0, dy0, dx1, dy1;
1428 //============= CLIP IT =====================
1430 x0 = va->sx; y0 = va->sy;
1431 x1 = vb->sx; y1 = vb->sy;
1433 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1434 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1436 u0 = va->u; v0 = va->v;
1437 u1 = vb->u; v1 = vb->v;
1439 // Check for obviously offscreen bitmaps...
1440 if ( (y1<=y0) || (x1<=x0) ) return;
1441 if ( (x1<xmin ) || (x0>xmax) ) return;
1442 if ( (y1<ymin ) || (y0>ymax) ) return;
1444 clipped_u0 = u0; clipped_v0 = v0;
1445 clipped_u1 = u1; clipped_v1 = v1;
1447 clipped_x0 = x0; clipped_y0 = y0;
1448 clipped_x1 = x1; clipped_y1 = y1;
1450 // Clip the left, moving u0 right as necessary
1452 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1456 // Clip the right, moving u1 left as necessary
1458 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1462 // Clip the top, moving v0 down as necessary
1464 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1468 // Clip the bottom, moving v1 up as necessary
1470 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1474 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1475 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1477 if (dx1<=dx0) return;
1478 if (dy1<=dy0) return;
1480 //============= DRAW IT =====================
1494 v[1].sx = clipped_x1;
1495 v[1].sy = clipped_y0;
1498 v[1].u = clipped_u1;
1499 v[1].v = clipped_v0;
1502 v[2].sx = clipped_x1;
1503 v[2].sy = clipped_y1;
1506 v[2].u = clipped_u1;
1507 v[2].v = clipped_v1;
1510 v[3].sx = clipped_x0;
1511 v[3].sy = clipped_y1;
1514 v[3].u = clipped_u0;
1515 v[3].v = clipped_v1;
1517 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1520 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1524 void gr_opengl_get_color( int * r, int * g, int * b )
1526 if (r) *r = gr_screen.current_color.red;
1527 if (g) *g = gr_screen.current_color.green;
1528 if (b) *b = gr_screen.current_color.blue;
1531 void gr_opengl_init_color(color *c, int r, int g, int b)
1533 c->screen_sig = gr_screen.signature;
1534 c->red = (unsigned char)r;
1535 c->green = (unsigned char)g;
1536 c->blue = (unsigned char)b;
1538 c->ac_type = AC_TYPE_NONE;
1540 c->is_alphacolor = 0;
1544 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1546 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1547 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1548 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1549 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1551 gr_opengl_init_color( clr, r, g, b );
1553 clr->alpha = (unsigned char)alpha;
1554 clr->ac_type = (ubyte)type;
1555 clr->alphacolor = -1;
1556 clr->is_alphacolor = 1;
1559 void gr_opengl_set_color( int r, int g, int b )
1561 Assert((r >= 0) && (r < 256));
1562 Assert((g >= 0) && (g < 256));
1563 Assert((b >= 0) && (b < 256));
1565 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1568 void gr_opengl_set_color_fast(color *dst)
1570 if ( dst->screen_sig != gr_screen.signature ) {
1571 if ( dst->is_alphacolor ) {
1572 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1574 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1577 gr_screen.current_color = *dst;
1580 void gr_opengl_print_screen(char *filename)
1585 int gr_opengl_supports_res_ingame(int res)
1592 int gr_opengl_supports_res_interface(int res)
1599 static void opengl_tcache_cleanup ();
1601 void gr_opengl_cleanup()
1603 if ( !Inited ) return;
1610 opengl_tcache_cleanup ();
1615 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1617 Assert((r >= 0) && (r < 256));
1618 Assert((g >= 0) && (g < 256));
1619 Assert((b >= 0) && (b < 256));
1621 if (fog_mode == GR_FOGMODE_NONE) {
1622 if (gr_screen.current_fog_mode != fog_mode) {
1625 gr_screen.current_fog_mode = fog_mode;
1630 if (gr_screen.current_fog_mode != fog_mode) {
1633 if (D3D_fog_mode == 2) {
1634 glFogi(GL_FOG_MODE, GL_LINEAR);
1637 gr_screen.current_fog_mode = fog_mode;
1640 if ( (gr_screen.current_fog_color.red != r) ||
1641 (gr_screen.current_fog_color.green != g) ||
1642 (gr_screen.current_fog_color.blue != b) ) {
1645 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1647 fc[0] = (float)r/255.0;
1648 fc[1] = (float)g/255.0;
1649 fc[2] = (float)b/255.0;
1652 glFogfv(GL_FOG_COLOR, fc);
1655 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1656 ((fog_near != gr_screen.fog_near) ||
1657 (fog_far != gr_screen.fog_far)) ) {
1658 gr_screen.fog_near = fog_near;
1659 gr_screen.fog_far = fog_far;
1661 if (D3D_fog_mode == 2) {
1662 glFogf(GL_FOG_START, fog_near);
1663 glFogf(GL_FOG_END, fog_far);
1668 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1673 void gr_opengl_set_cull(int cull)
1676 glEnable (GL_CULL_FACE);
1677 glFrontFace (GL_CCW);
1679 glDisable (GL_CULL_FACE);
1683 void gr_opengl_filter_set(int filter)
1688 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1691 gr_set_bitmap(bmap1);
1694 gr_set_bitmap(bmap2);
1700 typedef struct tcache_slot_opengl {
1701 GLuint texture_handle;
1702 float u_scale, v_scale;
1705 int used_this_frame;
1710 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1711 tcache_slot_opengl *parent;
1713 gr_texture_source texture_mode;
1714 } tcache_slot_opengl;
1716 static void *Texture_sections = NULL;
1717 static tcache_slot_opengl *Textures = NULL;
1719 static tcache_slot_opengl *GL_bound_texture;
1721 int GL_texture_sections = 0;
1722 int GL_texture_ram = 0;
1723 int GL_frame_count = 0;
1724 int GL_min_texture_width = 0;
1725 int GL_max_texture_width = 0;
1726 int GL_min_texture_height = 0;
1727 int GL_max_texture_height = 0;
1728 int GL_square_textures = 0;
1729 int GL_textures_in = 0;
1730 int GL_textures_in_frame = 0;
1731 int GL_last_bitmap_id = -1;
1732 int GL_last_detail = -1;
1733 int GL_last_bitmap_type = -1;
1734 int GL_last_section_x = -1;
1735 int GL_last_section_y = -1;
1739 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1741 static void gr_opengl_set_texture_state(gr_texture_source ts)
1743 if (ts == TEXTURE_SOURCE_NONE) {
1744 GL_bound_texture = NULL;
1746 glBindTexture(GL_TEXTURE_2D, 0);
1747 gr_tcache_set(-1, -1, NULL, NULL );
1748 } else if (GL_bound_texture &&
1749 GL_bound_texture->texture_mode != ts) {
1751 case TEXTURE_SOURCE_DECAL:
1752 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1753 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1755 case TEXTURE_SOURCE_NO_FILTERING:
1756 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1757 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1763 GL_bound_texture->texture_mode = ts;
1766 GL_current_texture_source = ts;
1769 static int opengl_max_tex_size_get()
1771 GLint max_texture_size = 0;
1773 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1774 return max_texture_size;
1777 static void opengl_tcache_init (int use_sections)
1781 // DDOI - FIXME skipped a lot of stuff here
1782 GL_should_preload = 0;
1784 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1787 if ( tmp_pl == 0 ) {
1788 GL_should_preload = 0;
1789 } else if ( tmp_pl == 1 ) {
1790 GL_should_preload = 1;
1792 GL_should_preload = 1;
1797 GL_min_texture_width = 16;
1798 GL_min_texture_height = 16;
1800 GL_max_texture_width = opengl_max_tex_size_get();
1801 GL_max_texture_height = opengl_max_tex_size_get();
1803 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1809 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1810 if(!Texture_sections){
1813 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1816 // Init the texture structures
1817 int section_count = 0;
1818 for( i=0; i<MAX_BITMAPS; i++ ) {
1820 Textures[i].vram_texture = NULL;
1821 Textures[i].vram_texture_surface = NULL;
1823 Textures[i].texture_handle = 0;
1825 Textures[i].bitmap_id = -1;
1826 Textures[i].size = 0;
1827 Textures[i].used_this_frame = 0;
1829 Textures[i].parent = NULL;
1831 // allocate sections
1833 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1834 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1835 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1836 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1838 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1839 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1841 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1842 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1843 Textures[i].data_sections[idx][s_idx]->size = 0;
1844 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1848 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1849 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1850 Textures[i].data_sections[idx][s_idx] = NULL;
1856 GL_texture_sections = use_sections;
1858 GL_last_detail = Detail.hardware_textures;
1859 GL_last_bitmap_id = -1;
1860 GL_last_bitmap_type = -1;
1862 GL_last_section_x = -1;
1863 GL_last_section_y = -1;
1866 GL_textures_in_frame = 0;
1869 static int opengl_free_texture (tcache_slot_opengl *t);
1871 void opengl_free_texture_with_handle(int handle)
1873 for(int i=0; i<MAX_BITMAPS; i++ ) {
1874 if (Textures[i].bitmap_id == handle) {
1875 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1876 opengl_free_texture ( &Textures[i] );
1881 static void opengl_tcache_flush ()
1885 for( i=0; i<MAX_BITMAPS; i++ ) {
1886 opengl_free_texture ( &Textures[i] );
1888 if (GL_textures_in != 0) {
1889 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1893 GL_last_bitmap_id = -1;
1894 GL_last_section_x = -1;
1895 GL_last_section_y = -1;
1898 static void opengl_tcache_cleanup ()
1900 opengl_tcache_flush ();
1903 GL_textures_in_frame = 0;
1910 if( Texture_sections != NULL ){
1911 free(Texture_sections);
1912 Texture_sections = NULL;
1916 static void opengl_tcache_frame ()
1918 GL_last_bitmap_id = -1;
1919 GL_textures_in_frame = 0;
1926 for( i=0; i<MAX_BITMAPS; i++ ) {
1927 Textures[i].used_this_frame = 0;
1930 if(Textures[i].data_sections[0][0] != NULL){
1931 Assert(GL_texture_sections);
1932 if(GL_texture_sections){
1933 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1934 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1935 if(Textures[i].data_sections[idx][s_idx] != NULL){
1936 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1946 opengl_tcache_flush();
1951 static int opengl_free_texture ( tcache_slot_opengl *t )
1957 if ( t->bitmap_id > -1 ) {
1958 // if I, or any of my children have been used this frame, bail
1959 if(t->used_this_frame == GL_frame_count){
1962 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1963 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1964 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
1970 // ok, now we know its legal to free everything safely
1971 t->texture_mode = (gr_texture_source) -1;
1972 glDeleteTextures (1, &t->texture_handle);
1973 t->texture_handle = 0;
1975 if ( GL_last_bitmap_id == t->bitmap_id ) {
1976 GL_last_bitmap_id = -1;
1979 // if this guy has children, free them too, since the children
1980 // actually make up his size
1981 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1982 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1983 if(t->data_sections[idx][s_idx] != NULL){
1984 opengl_free_texture(t->data_sections[idx][s_idx]);
1990 t->used_this_frame = 0;
1991 GL_textures_in -= t->size;
1997 static void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
2003 if((w_out == NULL) || (h_out == NULL)){
2011 // set height and width to a power of 2
2012 for (i=0; i<16; i++ ) {
2013 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2019 for (i=0; i<16; i++ ) {
2020 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2026 // try to keep an 8:1 size ratio
2027 if (tex_w/tex_h > 8)
2029 if (tex_h/tex_w > 8)
2032 if ( tex_w < GL_min_texture_width ) {
2033 tex_w = GL_min_texture_width;
2034 } else if ( tex_w > GL_max_texture_width ) {
2035 tex_w = GL_max_texture_width;
2038 if ( tex_h < GL_min_texture_height ) {
2039 tex_h = GL_min_texture_height;
2040 } else if ( tex_h > GL_max_texture_height ) {
2041 tex_h = GL_max_texture_height;
2044 if ( GL_square_textures) {
2046 // Make the both be equal to larger of the two
2047 new_size = max(tex_w, tex_h);
2052 // store the outgoing size
2057 // data == start of bitmap data
2058 // sx == x offset into bitmap
2059 // sy == y offset into bitmap
2060 // src_w == absolute width of section on source bitmap
2061 // src_h == absolute height of section on source bitmap
2062 // bmap_w == width of source bitmap
2063 // bmap_h == height of source bitmap
2064 // tex_w == width of final texture
2065 // tex_h == height of final texture
2066 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)
2076 if ( t->used_this_frame == GL_frame_count ) {
2077 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2082 if(!opengl_free_texture(t)){
2087 // get final texture size
2088 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2090 if ( (tex_w < 1) || (tex_h < 1) ) {
2091 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2095 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2096 t->u_scale = (float)bmap_w / (float)tex_w;
2097 t->v_scale = (float)bmap_h / (float)tex_h;
2098 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2099 t->u_scale = (float)src_w / (float)tex_w;
2100 t->v_scale = (float)src_h / (float)tex_h;
2107 glGenTextures (1, &t->texture_handle);
2110 if (t->texture_handle == 0) {
2111 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2115 GL_bound_texture = t;
2117 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2119 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2121 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2122 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2123 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2125 /* this should be set next anyway */
2126 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2127 // gr_opengl_set_texture_state(GL_current_texture_source);
2132 switch (bitmap_type) {
2134 case TCACHE_TYPE_AABITMAP:
2137 ubyte *bmp_data = ((ubyte*)data);
2143 for (i=0; i<16; i++) {
2144 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2147 for ( ; i<256; i++ ) {
2151 texmem = (ubyte *) malloc (tex_w*tex_h);
2154 for (i=0;i<tex_h;i++)
2156 for (j=0;j<tex_w;j++)
2158 if (i < bmap_h && j < bmap_w) {
2159 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2168 glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem);
2174 case TCACHE_TYPE_BITMAP_SECTION:
2177 ubyte *bmp_data = ((ubyte*)data);
2178 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2179 ubyte *texmemp = texmem;
2181 for (i=0;i<tex_h;i++)
2183 for (j=0;j<tex_w;j++)
2185 if (i < src_h && j < src_w) {
2186 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2187 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2195 size = tex_w*tex_h*2;
2197 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA,
2198 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2206 ubyte *bmp_data = ((ubyte*)data);
2207 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2208 ubyte *texmemp = texmem;
2210 fix u, utmp, v, du, dv;
2214 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2215 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2217 for (j=0;j<tex_h;j++)
2220 for (i=0;i<tex_w;i++)
2222 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2223 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2229 size = tex_w*tex_h*2;
2231 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB5_A1, tex_w, tex_h, 0, GL_BGRA,
2232 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2240 t->bitmap_id = texture_handle;
2241 t->time_created = GL_frame_count;
2242 t->used_this_frame = 0;
2244 t->w = (ushort)tex_w;
2245 t->h = (ushort)tex_h;
2247 GL_textures_in_frame += t->size;
2249 GL_textures_in += t->size;
2255 static int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2259 int final_w, final_h;
2263 // setup texture/bitmap flags
2265 switch(bitmap_type){
2266 case TCACHE_TYPE_AABITMAP:
2267 flags |= BMP_AABITMAP;
2270 case TCACHE_TYPE_NORMAL:
2271 flags |= BMP_TEX_OTHER;
2272 case TCACHE_TYPE_XPARENT:
2273 flags |= BMP_TEX_XPARENT;
2275 case TCACHE_TYPE_NONDARKENING:
2277 flags |= BMP_TEX_NONDARK;
2281 // lock the bitmap into the proper format
2282 bmp = bm_lock(bitmap_handle, bpp, flags);
2283 if ( bmp == NULL ) {
2284 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2293 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2294 // max_w /= D3D_texture_divider;
2295 // max_h /= D3D_texture_divider;
2297 // Detail.debris_culling goes from 0 to 4.
2298 max_w /= (16 >> Detail.hardware_textures);
2299 max_h /= (16 >> Detail.hardware_textures);
2303 // get final texture size as it will be allocated as a DD surface
2304 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2306 // if this tcache slot has no bitmap
2307 if ( tslot->bitmap_id < 0) {
2310 // different bitmap altogether - determine if the new one can use the old one's slot
2311 else if (tslot->bitmap_id != bitmap_handle) {
2312 if((final_w == tslot->w) && (final_h == tslot->h)){
2314 //ml_printf("Reloading texture %d\n", bitmap_handle);
2321 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);
2323 // unlock the bitmap
2324 bm_unlock(bitmap_handle);
2329 static int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2333 int final_w, final_h;
2334 int section_x, section_y;
2337 // setup texture/bitmap flags
2338 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2339 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2340 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2342 flags = BMP_TEX_XPARENT;
2344 // lock the bitmap in the proper format
2345 bmp = bm_lock(bitmap_handle, 16, flags);
2346 if ( bmp == NULL ) {
2347 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2350 // determine the width and height of this section
2351 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2353 // get final texture size as it will be allocated as an opengl texture
2354 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2356 // if this tcache slot has no bitmap
2357 if ( tslot->bitmap_id < 0) {
2360 // different bitmap altogether - determine if the new one can use the old one's slot
2361 else if (tslot->bitmap_id != bitmap_handle) {
2362 if((final_w == tslot->w) && (final_h == tslot->h)){
2370 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);
2372 // unlock the bitmap
2373 bm_unlock(bitmap_handle);
2379 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2380 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)
2389 GL_last_bitmap_id = -1;
2393 if ( GL_last_detail != Detail.hardware_textures ) {
2394 GL_last_detail = Detail.hardware_textures;
2395 opengl_tcache_flush();
2402 int n = bm_get_cache_slot (bitmap_id, 1);
2403 tcache_slot_opengl *t = &Textures[n];
2405 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)) {
2406 t->used_this_frame = GL_frame_count;
2408 // mark all children as used
2409 if(GL_texture_sections){
2410 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2411 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2412 if(t->data_sections[idx][s_idx] != NULL){
2413 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2419 *u_scale = t->u_scale;
2420 *v_scale = t->v_scale;
2424 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2425 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2426 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2432 // if the texture sections haven't been created yet
2433 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2435 // lock the bitmap in the proper format
2436 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2437 bm_unlock(bitmap_id);
2439 // now lets do something for each texture
2441 for(idx=0; idx<bmp->sections.num_x; idx++){
2442 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2443 // hmm. i'd rather we didn't have to do it this way...
2444 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2448 // not used this frame
2449 t->data_sections[idx][s_idx]->used_this_frame = 0;
2453 // zero out pretty much everything in the parent struct since he's just the root
2454 t->bitmap_id = bitmap_id;
2455 t->texture_handle = 0;
2456 t->time_created = t->data_sections[sx][sy]->time_created;
2457 t->used_this_frame = 0;
2459 t->vram_texture = NULL;
2460 t->vram_texture_surface = NULL
2464 // argh. we failed to upload. free anything we can
2466 opengl_free_texture(t);
2468 // swap in the texture we want
2470 t = t->data_sections[sx][sy];
2473 // all other "normal" textures
2474 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2475 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2478 // everything went ok
2479 if(ret_val && (t->texture_handle) && !vram_full){
2480 *u_scale = t->u_scale;
2481 *v_scale = t->v_scale;
2483 GL_bound_texture = t;
2485 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2487 /* this should be set next anyway */
2488 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2489 // gr_opengl_set_texture_state(GL_current_texture_source);
2492 GL_last_bitmap_id = t->bitmap_id;
2493 GL_last_bitmap_type = bitmap_type;
2494 GL_last_section_x = sx;
2495 GL_last_section_y = sy;
2497 t->used_this_frame = GL_frame_count;
2501 GL_last_bitmap_id = -1;
2502 GL_last_bitmap_type = -1;
2504 GL_last_section_x = -1;
2505 GL_last_section_y = -1;
2507 GL_bound_texture = NULL;
2509 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2516 void gr_opengl_set_clear_color(int r, int g, int b)
2518 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2521 void gr_opengl_flash(int r, int g, int b)
2527 if ( r || g || b ) {
2528 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2530 float x1, x2, y1, y2;
2531 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2532 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2533 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2534 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2536 glColor4ub(r, g, b, 255);
2538 glVertex3f (x1, y2, -0.99);
2540 glVertex3f (x2, y2, -0.99);
2542 glVertex3f (x2, y1, -0.99);
2544 glVertex3f (x1, y1, -0.99);
2549 int gr_opengl_zbuffer_get()
2551 if ( !gr_global_zbuffering ) {
2552 return GR_ZBUFF_NONE;
2554 return gr_zbuffering_mode;
2557 int gr_opengl_zbuffer_set(int mode)
2559 int tmp = gr_zbuffering_mode;
2561 gr_zbuffering_mode = mode;
2563 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2571 void gr_opengl_zbuffer_clear(int mode)
2575 gr_zbuffering_mode = GR_ZBUFF_FULL;
2576 gr_global_zbuffering = 1;
2578 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2579 glClear ( GL_DEPTH_BUFFER_BIT );
2582 gr_zbuffering_mode = GR_ZBUFF_NONE;
2583 gr_global_zbuffering = 0;
2587 void gr_opengl_set_gamma(float gamma)
2590 Gr_gamma_int = int (Gr_gamma*10);
2592 // Create the Gamma lookup table
2594 for (i=0;i<256; i++) {
2595 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2598 } else if ( v < 0 ) {
2601 Gr_gamma_lookup[i] = v;
2604 // Flush any existing textures
2605 opengl_tcache_flush();
2608 void gr_opengl_fade_in(int instantaneous)
2613 void gr_opengl_fade_out(int instantaneous)
2618 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2621 glReadBuffer(GL_FRONT);
2623 glReadBuffer(GL_BACK);
2626 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2628 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2630 if (gr_screen.bits_per_pixel == 15) {
2631 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2632 } else if (gr_screen.bits_per_pixel == 32) {
2633 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2636 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2639 static int Gr_opengl_mouse_saved = 0;
2640 static int Gr_opengl_mouse_saved_x1 = 0;
2641 static int Gr_opengl_mouse_saved_y1 = 0;
2642 static int Gr_opengl_mouse_saved_x2 = 0;
2643 static int Gr_opengl_mouse_saved_y2 = 0;
2644 static int Gr_opengl_mouse_saved_w = 0;
2645 static int Gr_opengl_mouse_saved_h = 0;
2646 #define MAX_SAVE_SIZE (32*32)
2647 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2649 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2651 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2653 Gr_opengl_mouse_saved_x1 = x;
2654 Gr_opengl_mouse_saved_y1 = y;
2655 Gr_opengl_mouse_saved_x2 = x+w-1;
2656 Gr_opengl_mouse_saved_y2 = y+h-1;
2658 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2659 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2660 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2661 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2663 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2664 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2666 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2667 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2669 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2671 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2673 glReadBuffer(GL_BACK);
2674 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);
2676 Gr_opengl_mouse_saved = 1;
2679 int gr_opengl_save_screen()
2683 if ( Gr_saved_screen ) {
2684 mprintf(( "Screen alread saved!\n" ));
2688 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2689 if (!Gr_saved_screen) {
2690 mprintf(( "Couldn't get memory for saved screen!\n" ));
2694 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2695 if (!Gr_saved_screen_tmp) {
2696 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2700 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2702 glReadBuffer(GL_FRONT);
2703 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);
2707 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2708 dptr = (ubyte *)Gr_saved_screen;
2709 for (int j = 0; j < gr_screen.max_h; j++) {
2710 sptr -= gr_screen.max_w*2;
2711 memcpy(dptr, sptr, gr_screen.max_w*2);
2712 dptr += gr_screen.max_w*2;
2715 free(Gr_saved_screen_tmp);
2717 if (Gr_opengl_mouse_saved) {
2718 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2719 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2720 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2721 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2724 dptr -= gr_screen.max_w*2;
2728 // this leaks texture handles, and the opengl doesn't currently
2729 // perform some sort of garbage collection, so a hack was added
2730 // to bmpman to make it free textures when released
2731 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2733 return Gr_saved_screen_bitmap;
2736 void gr_opengl_restore_screen(int id)
2740 if ( !Gr_saved_screen ) {
2745 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2747 gr_set_bitmap(Gr_saved_screen_bitmap);
2751 void gr_opengl_free_screen(int id)
2753 bm_release(Gr_saved_screen_bitmap);
2755 if ( Gr_saved_screen ) {
2756 free( Gr_saved_screen );
2757 Gr_saved_screen = NULL;
2761 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2766 void gr_opengl_dump_frame_stop()
2771 void gr_opengl_dump_frame()
2776 uint gr_opengl_lock()
2783 void gr_opengl_unlock()
2787 void opengl_zbias(int bias)
2790 glEnable(GL_POLYGON_OFFSET_FILL);
2791 glPolygonOffset(0.0, -bias);
2793 glDisable(GL_POLYGON_OFFSET_FILL);
2797 extern char *Osreg_title;
2798 void gr_opengl_init()
2801 gr_opengl_cleanup();
2805 mprintf(( "Initializing opengl graphics device...\n" ));
2809 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2811 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2815 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2816 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2817 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2818 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2819 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2821 int flags = SDL_OPENGL;
2823 if (!Cmdline_window && ( (os_config_read_uint( NULL, "Fullscreen", 1 ) == 1) || Cmdline_fullscreen ))
2824 flags |= SDL_FULLSCREEN;
2826 // grab mouse/key unless told otherwise, ignore when we are going fullscreen
2827 if ( !((flags & SDL_FULLSCREEN) || Cmdline_no_grab) ) {
2828 SDL_WM_GrabInput(SDL_GRAB_ON);
2831 FSAA = os_config_read_uint( NULL, "FSAA", 1 );
2833 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
2834 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, FSAA );
2837 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2839 mprintf(( "Couldn't set FSAA video mode: %s\n", SDL_GetError () ));
2840 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0 );
2841 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0 );
2843 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2845 fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ());
2850 mprintf(( "Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel ));
2852 mprintf(( "Vendor : %s\n", glGetString(GL_VENDOR) ));
2853 mprintf(( "Renderer : %s\n", glGetString(GL_RENDERER) ));
2854 mprintf(( "Version : %s\n", glGetString(GL_VERSION) ));
2855 mprintf(( "Extensions : %s\n", glGetString(GL_EXTENSIONS) ));
2865 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
2866 mprintf(( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value ));
2867 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
2868 mprintf(( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value ));
2869 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
2870 mprintf(( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value ));
2871 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
2872 mprintf(( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value ));
2873 SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
2874 mprintf(( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value ));
2877 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &value );
2878 mprintf(( "SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value ));
2879 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &value );
2880 mprintf(( "SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", FSAA, value ));
2884 SDL_WM_SetCaption (Osreg_title, NULL);
2886 /* might as well put this here */
2887 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2890 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2892 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2894 glMatrixMode(GL_PROJECTION);
2896 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2897 glMatrixMode(GL_MODELVIEW);
2900 glShadeModel(GL_SMOOTH);
2901 glEnable(GL_DITHER);
2902 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2903 glHint(GL_FOG_HINT, GL_NICEST);
2905 glEnable(GL_DEPTH_TEST);
2908 glEnable(GL_TEXTURE_2D);
2910 glDepthRange(0.0, 1.0);
2912 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2913 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2915 D3D_32bit = 1; // grd3d.cpp
2916 extern int D3D_enabled;
2919 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2920 1 = use secondary color ext
2921 2 = use opengl linear fog
2923 D3D_fog_mode = 2; // grd3d.cpp
2927 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2932 gr_screen.bits_per_pixel = 15;
2933 gr_screen.bytes_per_pixel = 2;
2938 Gr_red.mask = 0x7C00;
2943 Gr_green.mask = 0x3E0;
2948 Gr_blue.mask = 0x1F;
2953 gr_screen.bits_per_pixel = 16;
2954 gr_screen.bytes_per_pixel = 2;
2959 Gr_red.mask = 0xF800;
2964 Gr_green.mask = 0x7E0;
2969 Gr_blue.mask = 0x1F;
2974 gr_screen.bits_per_pixel = 32;
2975 gr_screen.bytes_per_pixel = 4;
2980 Gr_red.mask = 0xff0000;
2985 Gr_green.mask = 0xff00;
2990 Gr_blue.mask = 0xff;
2995 Int3(); // Illegal bpp
2998 // DDOI - set these so no one else does!
2999 Gr_t_red.mask = Gr_red.mask;
3000 Gr_t_red.shift = Gr_red.shift;
3001 Gr_t_red.scale = Gr_red.scale;
3003 Gr_t_green.mask = Gr_green.mask;
3004 Gr_t_green.shift = Gr_green.shift;
3005 Gr_t_green.scale = Gr_green.scale;
3007 Gr_t_blue.mask = Gr_blue.mask;
3008 Gr_t_blue.shift = Gr_blue.shift;
3009 Gr_t_blue.scale = Gr_blue.scale;
3011 Gr_t_alpha.mask = 0x8000;
3012 Gr_t_alpha.scale = 255;
3013 Gr_t_alpha.shift = 15;
3015 Gr_ta_red.mask = 0x0f00;
3016 Gr_ta_red.shift = 8;
3017 Gr_ta_red.scale = 16;
3019 Gr_ta_green.mask = 0x00f0;
3020 Gr_ta_green.shift = 4;
3021 Gr_ta_green.scale = 16;
3023 Gr_ta_blue.mask = 0x000f;
3024 Gr_ta_blue.shift = 0;
3025 Gr_ta_blue.scale = 16;
3027 Gr_ta_alpha.mask = 0xf000;
3028 Gr_ta_alpha.shift = 12;
3029 Gr_ta_alpha.scale = 16;
3032 opengl_tcache_init (1);
3035 Gr_current_red = &Gr_red;
3036 Gr_current_blue = &Gr_blue;
3037 Gr_current_green = &Gr_green;
3038 Gr_current_alpha = &Gr_alpha;
3040 gr_screen.gf_flip = gr_opengl_flip;
3041 gr_screen.gf_flip_window = gr_opengl_flip_window;
3042 gr_screen.gf_set_clip = gr_opengl_set_clip;
3043 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3044 gr_screen.gf_set_font = grx_set_font;
3046 gr_screen.gf_set_color = gr_opengl_set_color;
3047 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3048 gr_screen.gf_create_shader = gr_opengl_create_shader;
3049 gr_screen.gf_set_shader = gr_opengl_set_shader;
3050 gr_screen.gf_clear = gr_opengl_clear;
3051 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3052 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3053 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3054 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3056 gr_screen.gf_rect = gr_opengl_rect;
3057 gr_screen.gf_shade = gr_opengl_shade;
3058 gr_screen.gf_string = gr_opengl_string;
3059 gr_screen.gf_circle = gr_opengl_circle;
3061 gr_screen.gf_line = gr_opengl_line;
3062 gr_screen.gf_aaline = gr_opengl_aaline;
3063 gr_screen.gf_pixel = gr_opengl_pixel;
3064 gr_screen.gf_scaler = gr_opengl_scaler;
3065 gr_screen.gf_tmapper = gr_opengl_tmapper;
3067 gr_screen.gf_gradient = gr_opengl_gradient;
3069 gr_screen.gf_set_palette = gr_opengl_set_palette;
3070 gr_screen.gf_get_color = gr_opengl_get_color;
3071 gr_screen.gf_init_color = gr_opengl_init_color;
3072 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3073 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3074 gr_screen.gf_print_screen = gr_opengl_print_screen;
3076 gr_screen.gf_fade_in = gr_opengl_fade_in;
3077 gr_screen.gf_fade_out = gr_opengl_fade_out;
3078 gr_screen.gf_flash = gr_opengl_flash;
3080 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3081 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3082 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3084 gr_screen.gf_save_screen = gr_opengl_save_screen;
3085 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3086 gr_screen.gf_free_screen = gr_opengl_free_screen;
3088 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3089 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3090 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3092 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3094 gr_screen.gf_lock = gr_opengl_lock;
3095 gr_screen.gf_unlock = gr_opengl_unlock;
3097 gr_screen.gf_fog_set = gr_opengl_fog_set;
3099 gr_screen.gf_get_region = gr_opengl_get_region;
3101 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3103 gr_screen.gf_set_cull = gr_opengl_set_cull;
3105 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3107 gr_screen.gf_filter_set = gr_opengl_filter_set;
3109 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3111 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;