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.60 2003/02/02 20:20:03 relnev
19 * add large texture support (Taylor Richards)
21 * Revision 1.59 2002/09/04 01:38:09 relnev
24 * Revision 1.57 2002/08/31 01:39:13 theoddone33
25 * Speed up the renderer a tad
27 * Revision 1.56 2002/08/01 05:03:11 relnev
30 * Revision 1.55 2002/08/01 04:55:45 relnev
31 * experimenting with texture state
33 * Revision 1.54 2002/07/30 15:00:15 relnev
34 * not use luminance alpha by default
36 * Revision 1.53 2002/06/22 17:08:16 relnev
37 * more fixes to unused alpha code
39 * Revision 1.52 2002/06/21 23:59:14 relnev
40 * moved a gr_opengl_set_state to after the gr_tcache_set
42 * Revision 1.51 2002/06/19 04:52:45 relnev
43 * MacOS X updates (Ryan)
45 * Revision 1.50 2002/06/18 23:20:44 relnev
48 * adjusted line offsets.
50 * Revision 1.49 2002/06/17 23:11:39 relnev
51 * enable sdl key repeating.
55 * Revision 1.48 2002/06/09 04:41:17 relnev
56 * added copyright header
58 * Revision 1.47 2002/06/09 03:16:04 relnev
61 * removed unneeded asm, old sdl 2d setup.
63 * fixed crash caused by opengl_get_region.
65 * Revision 1.46 2002/06/05 04:03:32 relnev
66 * finished cfilesystem.
68 * removed some old code.
70 * fixed mouse save off-by-one.
74 * Revision 1.45 2002/06/03 09:25:37 relnev
75 * implement mouse cursor and screen save/restore
77 * Revision 1.44 2002/06/02 18:46:59 relnev
80 * Revision 1.43 2002/06/02 11:34:00 relnev
83 * Revision 1.42 2002/06/02 10:28:17 relnev
84 * fix texture handle leak
86 * Revision 1.41 2002/06/01 09:00:34 relnev
87 * silly debug memmanager
89 * Revision 1.40 2002/06/01 07:12:33 relnev
90 * a few NDEBUG updates.
92 * removed a few warnings.
94 * Revision 1.39 2002/06/01 05:33:15 relnev
95 * copied more code over.
97 * added scissor clipping.
99 * Revision 1.38 2002/06/01 03:35:27 relnev
102 * Revision 1.37 2002/06/01 03:32:00 relnev
103 * fix texture loading mistake.
105 * enable some d3d stuff for opengl also
107 * Revision 1.36 2002/05/31 23:25:03 relnev
110 * Revision 1.34 2002/05/31 22:15:22 relnev
113 * Revision 1.33 2002/05/31 22:04:55 relnev
114 * use d3d rect_internal
116 * Revision 1.32 2002/05/31 06:28:23 relnev
119 * Revision 1.31 2002/05/31 06:04:39 relnev
122 * Revision 1.30 2002/05/31 03:56:11 theoddone33
123 * Change tmapper polygon winding and enable culling
125 * Revision 1.29 2002/05/31 03:34:02 theoddone33
129 * Revision 1.28 2002/05/31 00:06:59 relnev
132 * Revision 1.27 2002/05/30 23:46:29 theoddone33
133 * some minor key changes (not necessarily fixes)
135 * Revision 1.26 2002/05/30 23:33:12 relnev
136 * implemented a few more functions.
138 * Revision 1.25 2002/05/30 23:01:16 relnev
139 * implement gr_opengl_set_state.
141 * Revision 1.24 2002/05/30 22:12:57 relnev
142 * finish default texture case
144 * Revision 1.23 2002/05/30 22:02:30 theoddone33
147 * Revision 1.22 2002/05/30 21:44:48 relnev
148 * implemented some missing texture stuff.
150 * enable bitmap polys for opengl.
152 * work around greenness in bitmaps.
154 * Revision 1.21 2002/05/30 17:29:30 theoddone33
155 * Fix some more stubs, change at least one polygon winding since culling is now
158 * Revision 1.20 2002/05/30 16:50:24 theoddone33
159 * Keyboard partially fixed
161 * Revision 1.19 2002/05/30 08:13:14 relnev
164 * Revision 1.18 2002/05/29 23:37:36 relnev
167 * Revision 1.17 2002/05/29 23:17:49 theoddone33
168 * Non working text code and fixed keys
170 * Revision 1.16 2002/05/29 19:45:13 theoddone33
171 * More changes on texture loading
173 * Revision 1.15 2002/05/29 19:06:48 theoddone33
174 * Enable string printing. Enable texture mapping
176 * Revision 1.14 2002/05/29 08:54:40 relnev
177 * "fixed" bitmap drawing.
179 * copied more d3d code over.
181 * Revision 1.13 2002/05/29 06:25:13 theoddone33
182 * Keyboard input, mouse tracking now work
184 * Revision 1.12 2002/05/29 04:52:45 relnev
187 * Revision 1.11 2002/05/29 04:29:56 relnev
188 * removed some unncessary stubbing, implemented opengl rect
190 * Revision 1.10 2002/05/29 04:13:27 theoddone33
193 * Revision 1.9 2002/05/29 03:35:51 relnev
196 * Revision 1.8 2002/05/29 03:30:05 relnev
197 * update opengl stubs
199 * Revision 1.7 2002/05/29 02:52:32 theoddone33
200 * Enable OpenGL renderer
202 * Revision 1.6 2002/05/28 04:56:51 theoddone33
203 * runs a little bit now
205 * Revision 1.5 2002/05/28 04:07:28 theoddone33
206 * New graphics stubbing arrangement
208 * Revision 1.4 2002/05/27 23:39:34 relnev
211 * Revision 1.3 2002/05/27 22:35:01 theoddone33
214 * Revision 1.2 2002/05/27 22:32:02 theoddone33
215 * throw all d3d stuff at opengl
217 * Revision 1.1.1.1 2002/05/03 03:28:09 root
221 * 10 7/14/99 9:42a Dave
222 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
225 * 9 7/09/99 9:51a Dave
226 * Added thick polyline code.
228 * 8 6/29/99 10:35a Dave
229 * Interface polygon bitmaps! Whee!
231 * 7 2/03/99 11:44a Dave
232 * Fixed d3d transparent textures.
234 * 6 1/24/99 11:37p Dave
235 * First full rev of beam weapons. Very customizable. Removed some bogus
236 * Int3()'s in low level net code.
238 * 5 12/18/98 1:13a Dave
239 * Rough 1024x768 support for Direct3D. Proper detection and usage through
242 * 4 12/06/98 2:36p Dave
243 * Drastically improved nebula fogging.
245 * 3 11/11/98 5:37p Dave
246 * Checkin for multiplayer testing.
248 * 2 10/07/98 10:53a Dave
251 * 1 10/07/98 10:49a Dave
253 * 14 5/20/98 9:46p John
254 * added code so the places in code that change half the palette don't
255 * have to clear the screen.
257 * 13 5/06/98 5:30p John
258 * Removed unused cfilearchiver. Removed/replaced some unused/little used
259 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
260 * DirectX header files and libs that fixed the Direct3D alpha blending
263 * 12 4/14/98 12:15p John
264 * Made 16-bpp movies work.
266 * 11 3/12/98 5:36p John
267 * Took out any unused shaders. Made shader code take rgbc instead of
268 * matrix and vector since noone used it like a matrix and it would have
269 * been impossible to do in hardware. Made Glide implement a basic
270 * shader for online help.
272 * 10 3/10/98 4:18p John
273 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
274 * & Glide have popups and print screen. Took out all >8bpp software
275 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
276 * support Fred. Made zbuffering key off of functions rather than one
279 * 9 12/02/97 4:00p John
280 * Added first rev of thruster glow, along with variable levels of
281 * translucency, which retquired some restructing of palman.
283 * 8 10/03/97 9:10a John
284 * added better antialiased line drawer
286 * 7 9/23/97 10:45a John
287 * made so you can tell bitblt code to rle a bitmap by passing flag to
290 * 6 9/09/97 11:01a Sandeep
291 * fixed warning level 4 bugs
293 * 5 7/10/97 2:06p John
294 * added code to specify alphablending type for bitmaps.
296 * 4 6/17/97 7:04p John
297 * added d3d support for gradients.
298 * fixed some color bugs by adding screen signatures instead of watching
299 * flags and palette changes.
301 * 3 6/12/97 2:50a Lawrance
302 * bm_unlock() now passed bitmap number, not pointer
304 * 2 6/11/97 1:12p John
305 * Started fixing all the text colors in the game.
307 * 1 5/12/97 12:14p John
314 #include <windowsx.h>
328 #include "floating.h"
330 #include "systemvars.h"
331 #include "grinternal.h"
332 #include "gropengl.h"
336 #include "osregistry.h"
338 static int Inited = 0;
340 typedef enum gr_texture_source {
342 TEXTURE_SOURCE_DECAL,
343 TEXTURE_SOURCE_NO_FILTERING,
346 typedef enum gr_alpha_blend {
347 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
348 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
349 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
350 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
351 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
354 typedef enum gr_zbuffer_type {
361 #define NEBULA_COLORS 20
363 volatile int GL_activate = 0;
364 volatile int GL_deactivate = 0;
366 static int GL_use_luminance_alpha;
368 static char *Gr_saved_screen = NULL;
369 static int Gr_saved_screen_bitmap;
372 // Throw in some dummy functions - DDOI
374 int D3D_32bit = 0; // grd3d.cpp
375 int D3D_fog_mode = -1; // grd3d.cpp
376 int D3D_inited = 0; // grd3d.cpp
377 int D3D_zbias = 1; // grd3d.cpp
378 int D3d_rendition_uvs = 0; // grd3d.cpp
385 void d3d_zbias (int a)
391 static void gr_opengl_set_texture_state(gr_texture_source ts);
393 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
394 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
396 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
398 gr_opengl_set_texture_state(ts);
400 if (ab != GL_current_alpha_blend) {
402 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
403 glBlendFunc(GL_ONE, GL_ZERO);
405 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
406 glBlendFunc(GL_ONE, GL_ONE);
408 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
409 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
411 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
412 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
414 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
415 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
421 GL_current_alpha_blend = ab;
424 if (zt != GL_current_zbuffer_type) {
426 case ZBUFFER_TYPE_NONE:
427 glDepthFunc(GL_ALWAYS);
428 glDepthMask(GL_FALSE);
430 case ZBUFFER_TYPE_READ:
431 glDepthFunc(GL_LESS);
432 glDepthMask(GL_FALSE);
434 case ZBUFFER_TYPE_WRITE:
435 glDepthFunc(GL_ALWAYS);
436 glDepthMask(GL_TRUE);
438 case ZBUFFER_TYPE_FULL:
439 glDepthFunc(GL_LESS);
440 glDepthMask(GL_TRUE);
446 GL_current_zbuffer_type = zt;
450 void gr_opengl_activate(int active)
456 make sure window is active and mouse grabbed
462 make sure mouse is not grabbed and window minimized
470 void opengl_tcache_flush ();
472 void gr_opengl_preload_init()
474 if (gr_screen.mode != GR_OPENGL) {
478 opengl_tcache_flush ();
481 int GL_should_preload = 0;
482 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
484 if ( gr_screen.mode != GR_OPENGL) {
488 if ( !GL_should_preload ) {
492 float u_scale, v_scale;
496 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
498 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
502 mprintf(("Texture upload failed!\n" ));
508 void gr_opengl_pixel(int x, int y)
513 void gr_opengl_clear()
515 glClearColor(gr_screen.current_clear_color.red / 255.0,
516 gr_screen.current_clear_color.green / 255.0,
517 gr_screen.current_clear_color.blue / 255.0, 1.0);
519 glClear ( GL_COLOR_BUFFER_BIT );
522 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
523 void opengl_tcache_frame ();
524 void gr_opengl_flip()
532 extern int Gr_opengl_mouse_saved;
533 Gr_opengl_mouse_saved = 0;
535 if ( mouse_is_visible() ) {
539 mouse_get_pos( &mx, &my );
541 gr_opengl_save_mouse_area(mx,my,32,32);
543 if ( Gr_cursor == -1 ) {
546 gr_set_bitmap(Gr_cursor);
552 GLenum error = glGetError();
555 error = glGetError();
557 if (error != GL_NO_ERROR) {
558 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
561 } while (error != GL_NO_ERROR);
564 SDL_GL_SwapBuffers ();
566 opengl_tcache_frame ();
568 int cnt = GL_activate;
571 opengl_tcache_flush();
572 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
578 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
582 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
587 void gr_opengl_set_clip(int x,int y,int w,int h)
589 // check for sanity of parameters
595 if (x >= gr_screen.max_w)
596 x = gr_screen.max_w - 1;
597 if (y >= gr_screen.max_h)
598 y = gr_screen.max_h - 1;
600 if (x + w > gr_screen.max_w)
601 w = gr_screen.max_w - x;
602 if (y + h > gr_screen.max_h)
603 h = gr_screen.max_h - y;
605 if (w > gr_screen.max_w)
607 if (h > gr_screen.max_h)
610 gr_screen.offset_x = x;
611 gr_screen.offset_y = y;
612 gr_screen.clip_left = 0;
613 gr_screen.clip_right = w-1;
614 gr_screen.clip_top = 0;
615 gr_screen.clip_bottom = h-1;
616 gr_screen.clip_width = w;
617 gr_screen.clip_height = h;
619 glEnable(GL_SCISSOR_TEST);
620 glScissor(x, gr_screen.max_h-y-h, w, h);
623 void gr_opengl_reset_clip()
625 gr_screen.offset_x = 0;
626 gr_screen.offset_y = 0;
627 gr_screen.clip_left = 0;
628 gr_screen.clip_top = 0;
629 gr_screen.clip_right = gr_screen.max_w - 1;
630 gr_screen.clip_bottom = gr_screen.max_h - 1;
631 gr_screen.clip_width = gr_screen.max_w;
632 gr_screen.clip_height = gr_screen.max_h;
634 glDisable(GL_SCISSOR_TEST);
635 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
638 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
640 gr_screen.current_alpha = alpha;
641 gr_screen.current_alphablend_mode = alphablend_mode;
642 gr_screen.current_bitblt_mode = bitblt_mode;
643 gr_screen.current_bitmap = bitmap_num;
645 gr_screen.current_bitmap_sx = sx;
646 gr_screen.current_bitmap_sy = sy;
649 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
651 shade->screen_sig = gr_screen.signature;
658 void gr_opengl_set_shader( shader * shade )
661 if (shade->screen_sig != gr_screen.signature) {
662 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
664 gr_screen.current_shader = *shade;
666 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
671 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
673 STUB_FUNCTION; /* who called me? */
677 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
679 STUB_FUNCTION; /* who called me? */
682 void gr_opengl_bitmap(int x, int y)
684 STUB_FUNCTION; /* who called me? */
687 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
691 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
693 saved_zbuf = gr_zbuffer_get();
695 // start the frame, no zbuffering, no culling
697 gr_zbuffer_set(GR_ZBUFF_NONE);
706 v[0].flags = PF_PROJECTED;
713 v[1].sx = i2fl(x + w);
718 v[1].flags = PF_PROJECTED;
725 v[2].sx = i2fl(x + w);
726 v[2].sy = i2fl(y + h);
730 v[2].flags = PF_PROJECTED;
738 v[3].sy = i2fl(y + h);
742 v[3].flags = PF_PROJECTED;
750 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
754 // restore zbuffer and culling
755 gr_zbuffer_set(saved_zbuf);
759 void gr_opengl_rect(int x,int y,int w,int h)
761 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);
764 void gr_opengl_shade(int x,int y,int w,int h)
771 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
772 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
773 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
774 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
775 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
776 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
777 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
778 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
780 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
783 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
788 if ( !gr_screen.current_color.is_alphacolor ) return;
790 float u_scale, v_scale;
792 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
793 // Couldn't set texture
794 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
798 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
800 float u0, u1, v0, v1;
801 float x1, x2, y1, y2;
804 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
806 u0 = u_scale*i2fl(sx)/i2fl(bw);
807 v0 = v_scale*i2fl(sy)/i2fl(bh);
809 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
810 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
812 x1 = i2fl(x+gr_screen.offset_x);
813 y1 = i2fl(y+gr_screen.offset_y);
814 x2 = i2fl(x+w+gr_screen.offset_x);
815 y2 = i2fl(y+h+gr_screen.offset_y);
817 if ( gr_screen.current_color.is_alphacolor ) {
818 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
820 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
824 glTexCoord2f (u0, v1);
825 glVertex3f (x1, y2, -0.99);
827 glTexCoord2f (u1, v1);
828 glVertex3f (x2, y2, -0.99);
830 glTexCoord2f (u1, v0);
831 glVertex3f (x2, y1, -0.99);
833 glTexCoord2f (u0, v0);
834 glVertex3f (x1, y1, -0.99);
838 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
845 int dx1=x, dx2=x+w-1;
846 int dy1=y, dy2=y+h-1;
849 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
854 if ( count > 1 ) Int3();
858 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
859 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
860 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
861 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
862 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
863 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
890 if ( w < 1 ) return; // clipped away!
891 if ( h < 1 ) return; // clipped away!
895 // Make sure clipping algorithm works
899 Assert( w == (dx2-dx1+1) );
900 Assert( h == (dy2-dy1+1) );
903 Assert( sx+w <= bw );
904 Assert( sy+h <= bh );
905 Assert( dx2 >= dx1 );
906 Assert( dy2 >= dy1 );
907 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
908 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
909 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
910 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
913 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
914 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
917 void gr_opengl_aabitmap(int x, int y)
921 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
922 int dx1=x, dx2=x+w-1;
923 int dy1=y, dy2=y+h-1;
926 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
927 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
928 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
929 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
930 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
931 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
933 if ( sx < 0 ) return;
934 if ( sy < 0 ) return;
935 if ( sx >= w ) return;
936 if ( sy >= h ) return;
938 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
939 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
942 void gr_opengl_string( int sx, int sy, char *s )
944 int width, spacing, letter;
947 if ( !Current_font ) {
951 gr_set_bitmap(Current_font->bitmap_id);
956 if (sx==0x8000) { //centered
957 x = get_centered_x(s);
969 y += Current_font->h;
970 if (sx==0x8000) { //centered
971 x = get_centered_x(s);
978 letter = get_char_width(s[0],s[1],&width,&spacing);
981 //not in font, draw as space
989 // Check if this character is totally clipped
990 if ( x + width < gr_screen.clip_left ) continue;
991 if ( y + Current_font->h < gr_screen.clip_top ) continue;
992 if ( x > gr_screen.clip_right ) continue;
993 if ( y > gr_screen.clip_bottom ) continue;
996 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
997 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1001 wc = width - xd; hc = Current_font->h - yd;
1002 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1003 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1005 if ( wc < 1 ) continue;
1006 if ( hc < 1 ) continue;
1008 int u = Current_font->bm_u[letter];
1009 int v = Current_font->bm_v[letter];
1011 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1015 void gr_opengl_line(int x1,int y1,int x2,int y2)
1017 int clipped = 0, swapped=0;
1019 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1021 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);
1026 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1027 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1028 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1029 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1031 if ( x1 == x2 && y1 == y2 ) {
1032 glBegin (GL_POINTS);
1033 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1034 glVertex3f (sx1, sy1, -0.99f);
1046 } else if ( y1 == y2 ) {
1055 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1056 glVertex3f (sx2, sy2, -0.99f);
1057 glVertex3f (sx1, sy1, -0.99f);
1061 void gr_opengl_aaline(vertex *v1, vertex *v2)
1063 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1066 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1068 int clipped = 0, swapped=0;
1070 if ( !gr_screen.current_color.is_alphacolor ) {
1071 gr_line( x1, y1, x2, y2 );
1075 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);
1077 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1079 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1080 int ba = swapped ? gr_screen.current_color.alpha : 0;
1085 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1086 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1087 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1088 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1096 } else if ( y1 == y2 ) {
1105 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1106 glVertex3f (sx2, sy2, -0.99f);
1107 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1108 glVertex3f (sx1, sy1, -0.99f);
1112 void gr_opengl_circle( int xc, int yc, int d )
1122 if ( (xc+r) < gr_screen.clip_left ) return;
1123 if ( (xc-r) > gr_screen.clip_right ) return;
1124 if ( (yc+r) < gr_screen.clip_top ) return;
1125 if ( (yc-r) > gr_screen.clip_bottom ) return;
1128 // Draw the first octant
1129 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1130 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1135 // Draw the second octant
1136 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1137 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1145 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1146 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1151 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1155 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1156 if (f_float < 0.0f) {
1164 *a = (int)(f_float * 255.0);
1167 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1170 float u_scale = 1.0f, v_scale = 1.0f;
1172 // Make nebula use the texture mapper... this blends the colors better.
1173 if ( flags & TMAP_FLAG_NEBULA ){
1177 gr_texture_source texture_source = (gr_texture_source)-1;
1178 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1179 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1181 if ( gr_zbuffering ) {
1182 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1183 zbuffer_type = ZBUFFER_TYPE_READ;
1185 zbuffer_type = ZBUFFER_TYPE_FULL;
1188 zbuffer_type = ZBUFFER_TYPE_NONE;
1193 int tmap_type = TCACHE_TYPE_NORMAL;
1197 if ( flags & TMAP_FLAG_TEXTURED ) {
1200 r = gr_screen.current_color.red;
1201 g = gr_screen.current_color.green;
1202 b = gr_screen.current_color.blue;
1205 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1208 tmap_type = TCACHE_TYPE_NORMAL;
1209 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1211 // Blend with screen pixel using src*alpha+dst
1212 float factor = gr_screen.current_alpha;
1216 if ( factor <= 1.0f ) {
1217 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1218 r = (r*tmp_alpha)/255;
1219 g = (g*tmp_alpha)/255;
1220 b = (b*tmp_alpha)/255;
1223 tmap_type = TCACHE_TYPE_XPARENT;
1225 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1227 // Blend with screen pixel using src*alpha+dst
1228 float factor = gr_screen.current_alpha;
1230 if ( factor > 1.0f ) {
1233 alpha = fl2i(gr_screen.current_alpha*255.0f);
1237 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1238 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1240 alpha_blend = ALPHA_BLEND_NONE;
1245 if(flags & TMAP_FLAG_BITMAP_SECTION){
1246 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1249 texture_source = TEXTURE_SOURCE_NONE;
1251 if ( flags & TMAP_FLAG_TEXTURED ) {
1252 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 ))
1254 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1258 // use nonfiltered textures for bitmap sections
1259 if(flags & TMAP_FLAG_BITMAP_SECTION){
1260 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1262 texture_source = TEXTURE_SOURCE_DECAL;
1267 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1269 if ( flags & TMAP_FLAG_TEXTURED )
1275 if (flags & TMAP_FLAG_PIXEL_FOG) {
1281 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1283 vertex * va = verts[i];
1287 x = fl2i(va->sx*16.0f);
1288 y = fl2i(va->sy*16.0f);
1290 x += gr_screen.offset_x*16;
1291 y += gr_screen.offset_y*16;
1293 sx = i2fl(x) / 16.0f;
1294 sy = i2fl(y) / 16.0f;
1296 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1307 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1310 glBegin(GL_TRIANGLE_FAN);
1311 for (i = nv-1; i >= 0; i--) {
1312 vertex * va = verts[i];
1318 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1319 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1328 if ( flags & TMAP_FLAG_CORRECT ) {
1334 if (flags & TMAP_FLAG_ALPHA) {
1340 if (flags & TMAP_FLAG_NEBULA ) {
1341 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1342 r = gr_palette[pal*3+0];
1343 g = gr_palette[pal*3+1];
1344 b = gr_palette[pal*3+2];
1345 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1346 r = Gr_gamma_lookup[verts[i]->b];
1347 g = Gr_gamma_lookup[verts[i]->b];
1348 b = Gr_gamma_lookup[verts[i]->b];
1349 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1350 // Make 0.75 be 256.0f
1351 r = Gr_gamma_lookup[verts[i]->r];
1352 g = Gr_gamma_lookup[verts[i]->g];
1353 b = Gr_gamma_lookup[verts[i]->b];
1355 // use constant RGB values...
1357 glColor4ub (r,g,b,a);
1359 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1362 /* this is for GL_EXT_SECONDARY_COLOR */
1363 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1364 /* do separate color call here */
1370 x = fl2i(va->sx*16.0f);
1371 y = fl2i(va->sy*16.0f);
1373 x += gr_screen.offset_x*16;
1374 y += gr_screen.offset_y*16;
1376 sx = i2fl(x) / 16.0f;
1377 sy = i2fl(y) / 16.0f;
1379 if ( flags & TMAP_FLAG_TEXTURED ) {
1382 glTexCoord2f(tu, tv);
1385 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1390 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1392 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1395 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1397 void gr_opengl_scaler(vertex *va, vertex *vb )
1399 float x0, y0, x1, y1;
1400 float u0, v0, u1, v1;
1401 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1402 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1403 float xmin, xmax, ymin, ymax;
1404 int dx0, dy0, dx1, dy1;
1406 //============= CLIP IT =====================
1408 x0 = va->sx; y0 = va->sy;
1409 x1 = vb->sx; y1 = vb->sy;
1411 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1412 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1414 u0 = va->u; v0 = va->v;
1415 u1 = vb->u; v1 = vb->v;
1417 // Check for obviously offscreen bitmaps...
1418 if ( (y1<=y0) || (x1<=x0) ) return;
1419 if ( (x1<xmin ) || (x0>xmax) ) return;
1420 if ( (y1<ymin ) || (y0>ymax) ) return;
1422 clipped_u0 = u0; clipped_v0 = v0;
1423 clipped_u1 = u1; clipped_v1 = v1;
1425 clipped_x0 = x0; clipped_y0 = y0;
1426 clipped_x1 = x1; clipped_y1 = y1;
1428 // Clip the left, moving u0 right as necessary
1430 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1434 // Clip the right, moving u1 left as necessary
1436 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1440 // Clip the top, moving v0 down as necessary
1442 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1446 // Clip the bottom, moving v1 up as necessary
1448 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1452 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1453 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1455 if (dx1<=dx0) return;
1456 if (dy1<=dy0) return;
1458 //============= DRAW IT =====================
1472 v[1].sx = clipped_x1;
1473 v[1].sy = clipped_y0;
1476 v[1].u = clipped_u1;
1477 v[1].v = clipped_v0;
1480 v[2].sx = clipped_x1;
1481 v[2].sy = clipped_y1;
1484 v[2].u = clipped_u1;
1485 v[2].v = clipped_v1;
1488 v[3].sx = clipped_x0;
1489 v[3].sy = clipped_y1;
1492 v[3].u = clipped_u0;
1493 v[3].v = clipped_v1;
1495 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1498 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1502 void gr_opengl_get_color( int * r, int * g, int * b )
1504 if (r) *r = gr_screen.current_color.red;
1505 if (g) *g = gr_screen.current_color.green;
1506 if (b) *b = gr_screen.current_color.blue;
1509 void gr_opengl_init_color(color *c, int r, int g, int b)
1511 c->screen_sig = gr_screen.signature;
1512 c->red = (unsigned char)r;
1513 c->green = (unsigned char)g;
1514 c->blue = (unsigned char)b;
1516 c->ac_type = AC_TYPE_NONE;
1518 c->is_alphacolor = 0;
1522 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1524 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1525 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1526 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1527 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1529 gr_opengl_init_color( clr, r, g, b );
1531 clr->alpha = (unsigned char)alpha;
1532 clr->ac_type = (ubyte)type;
1533 clr->alphacolor = -1;
1534 clr->is_alphacolor = 1;
1537 void gr_opengl_set_color( int r, int g, int b )
1539 Assert((r >= 0) && (r < 256));
1540 Assert((g >= 0) && (g < 256));
1541 Assert((b >= 0) && (b < 256));
1543 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1546 void gr_opengl_set_color_fast(color *dst)
1548 if ( dst->screen_sig != gr_screen.signature ) {
1549 if ( dst->is_alphacolor ) {
1550 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1552 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1555 gr_screen.current_color = *dst;
1558 void gr_opengl_print_screen(char *filename)
1563 int gr_opengl_supports_res_ingame(int res)
1570 int gr_opengl_supports_res_interface(int res)
1577 void opengl_tcache_cleanup ();
1578 void gr_opengl_cleanup()
1580 if ( !Inited ) return;
1587 opengl_tcache_cleanup ();
1592 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1594 Assert((r >= 0) && (r < 256));
1595 Assert((g >= 0) && (g < 256));
1596 Assert((b >= 0) && (b < 256));
1598 if (fog_mode == GR_FOGMODE_NONE) {
1599 if (gr_screen.current_fog_mode != fog_mode) {
1602 gr_screen.current_fog_mode = fog_mode;
1607 if (gr_screen.current_fog_mode != fog_mode) {
1610 if (D3D_fog_mode == 2) {
1611 glFogi(GL_FOG_MODE, GL_LINEAR);
1614 gr_screen.current_fog_mode = fog_mode;
1617 if ( (gr_screen.current_fog_color.red != r) ||
1618 (gr_screen.current_fog_color.green != g) ||
1619 (gr_screen.current_fog_color.blue != b) ) {
1622 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1624 fc[0] = (float)r/255.0;
1625 fc[1] = (float)g/255.0;
1626 fc[2] = (float)b/255.0;
1629 glFogfv(GL_FOG_COLOR, fc);
1632 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1633 ((fog_near != gr_screen.fog_near) ||
1634 (fog_far != gr_screen.fog_far)) ) {
1635 gr_screen.fog_near = fog_near;
1636 gr_screen.fog_far = fog_far;
1638 if (D3D_fog_mode == 2) {
1639 glFogf(GL_FOG_START, fog_near);
1640 glFogf(GL_FOG_END, fog_far);
1645 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1650 void gr_opengl_set_cull(int cull)
1653 glEnable (GL_CULL_FACE);
1654 glFrontFace (GL_CCW);
1656 glDisable (GL_CULL_FACE);
1660 void gr_opengl_filter_set(int filter)
1665 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1668 gr_set_bitmap(bmap1);
1671 gr_set_bitmap(bmap2);
1677 typedef struct tcache_slot_opengl {
1678 GLuint texture_handle;
1679 float u_scale, v_scale;
1682 int used_this_frame;
1687 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1688 tcache_slot_opengl *parent;
1690 gr_texture_source texture_mode;
1691 } tcache_slot_opengl;
1693 static void *Texture_sections = NULL;
1694 static tcache_slot_opengl *Textures = NULL;
1696 static tcache_slot_opengl *GL_bound_texture;
1698 int GL_texture_sections = 0;
1699 int GL_texture_ram = 0;
1700 int GL_frame_count = 0;
1701 int GL_min_texture_width = 0;
1702 int GL_max_texture_width = 0;
1703 int GL_min_texture_height = 0;
1704 int GL_max_texture_height = 0;
1705 int GL_square_textures = 0;
1706 int GL_textures_in = 0;
1707 int GL_textures_in_frame = 0;
1708 int GL_last_bitmap_id = -1;
1709 int GL_last_detail = -1;
1710 int GL_last_bitmap_type = -1;
1711 int GL_last_section_x = -1;
1712 int GL_last_section_y = -1;
1716 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1718 static void gr_opengl_set_texture_state(gr_texture_source ts)
1720 if (ts == TEXTURE_SOURCE_NONE) {
1721 GL_bound_texture = NULL;
1723 glBindTexture(GL_TEXTURE_2D, 0);
1724 gr_tcache_set(-1, -1, NULL, NULL );
1725 } else if (GL_bound_texture &&
1726 GL_bound_texture->texture_mode != ts) {
1728 case TEXTURE_SOURCE_DECAL:
1729 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1730 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1732 case TEXTURE_SOURCE_NO_FILTERING:
1733 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1734 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1740 GL_bound_texture->texture_mode = ts;
1743 GL_current_texture_source = ts;
1746 int gr_opengl_max_tex_size_get()
1748 GLint max_texture_size = 0;
1749 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
1750 return max_texture_size;
1753 void opengl_tcache_init (int use_sections)
1757 // DDOI - FIXME skipped a lot of stuff here
1758 GL_should_preload = 0;
1760 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1763 if ( tmp_pl == 0 ) {
1764 GL_should_preload = 0;
1765 } else if ( tmp_pl == 1 ) {
1766 GL_should_preload = 1;
1768 GL_should_preload = 1;
1773 GL_min_texture_width = 16;
1774 GL_min_texture_height = 16;
1776 if ( !os_config_read_uint( NULL, NOX("UseLargeTextures"), 0 )) {
1777 GL_max_texture_width = 256;
1778 GL_max_texture_height = 256;
1780 GL_max_texture_width = gr_opengl_max_tex_size_get();
1781 GL_max_texture_height = gr_opengl_max_tex_size_get();
1783 mprintf(( "Large textures enabled! Size: %i\n", GL_max_texture_width ));
1787 GL_square_textures = 1;
1789 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1795 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1796 if(!Texture_sections){
1799 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1802 // Init the texture structures
1803 int section_count = 0;
1804 for( i=0; i<MAX_BITMAPS; i++ ) {
1806 Textures[i].vram_texture = NULL;
1807 Textures[i].vram_texture_surface = NULL;
1809 Textures[i].texture_handle = 0;
1811 Textures[i].bitmap_id = -1;
1812 Textures[i].size = 0;
1813 Textures[i].used_this_frame = 0;
1815 Textures[i].parent = NULL;
1817 // allocate sections
1819 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1820 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1821 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1822 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1824 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1825 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1827 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1828 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1829 Textures[i].data_sections[idx][s_idx]->size = 0;
1830 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1834 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1835 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1836 Textures[i].data_sections[idx][s_idx] = NULL;
1842 GL_texture_sections = use_sections;
1844 //GL_last_detail = Detail.hardware_textures;
1845 GL_last_bitmap_id = -1;
1846 GL_last_bitmap_type = -1;
1848 GL_last_section_x = -1;
1849 GL_last_section_y = -1;
1852 GL_textures_in_frame = 0;
1855 int opengl_free_texture (tcache_slot_opengl *t);
1857 void opengl_free_texture_with_handle(int handle)
1859 for(int i=0; i<MAX_BITMAPS; i++ ) {
1860 if (Textures[i].bitmap_id == handle) {
1861 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1862 opengl_free_texture ( &Textures[i] );
1867 void opengl_tcache_flush ()
1871 for( i=0; i<MAX_BITMAPS; i++ ) {
1872 opengl_free_texture ( &Textures[i] );
1874 if (GL_textures_in != 0) {
1875 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1879 GL_last_bitmap_id = -1;
1880 GL_last_section_x = -1;
1881 GL_last_section_y = -1;
1884 void opengl_tcache_cleanup ()
1886 opengl_tcache_flush ();
1889 GL_textures_in_frame = 0;
1896 if( Texture_sections != NULL ){
1897 free(Texture_sections);
1898 Texture_sections = NULL;
1902 void opengl_tcache_frame ()
1906 GL_last_bitmap_id = -1;
1907 GL_textures_in_frame = 0;
1913 for( i=0; i<MAX_BITMAPS; i++ ) {
1914 Textures[i].used_this_frame = 0;
1917 if(Textures[i].data_sections[0][0] != NULL){
1918 Assert(GL_texture_sections);
1919 if(GL_texture_sections){
1920 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1921 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1922 if(Textures[i].data_sections[idx][s_idx] != NULL){
1923 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1933 opengl_tcache_flush();
1938 int opengl_free_texture ( tcache_slot_opengl *t )
1944 if ( t->bitmap_id > -1 ) {
1945 // if I, or any of my children have been used this frame, bail
1946 if(t->used_this_frame == GL_frame_count){
1949 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1950 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1951 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
1957 // ok, now we know its legal to free everything safely
1958 t->texture_mode = (gr_texture_source) -1;
1959 glDeleteTextures (1, &t->texture_handle);
1960 t->texture_handle = 0;
1962 if ( GL_last_bitmap_id == t->bitmap_id ) {
1963 GL_last_bitmap_id = -1;
1966 // if this guy has children, free them too, since the children
1967 // actually make up his size
1968 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1969 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1970 if(t->data_sections[idx][s_idx] != NULL){
1971 opengl_free_texture(t->data_sections[idx][s_idx]);
1977 t->used_this_frame = 0;
1978 GL_textures_in -= t->size;
1984 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1989 if((w_out == NULL) || (h_out == NULL)){
1999 for (i=0; i<16; i++ ) {
2000 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2006 for (i=0; i<16; i++ ) {
2007 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2014 if ( tex_w < GL_min_texture_width ) {
2015 tex_w = GL_min_texture_width;
2016 } else if ( tex_w > GL_max_texture_width ) {
2017 tex_w = GL_max_texture_width;
2020 if ( tex_h < GL_min_texture_height ) {
2021 tex_h = GL_min_texture_height;
2022 } else if ( tex_h > GL_max_texture_height ) {
2023 tex_h = GL_max_texture_height;
2026 if ( GL_square_textures ) {
2028 // Make the both be equal to larger of the two
2029 new_size = max(tex_w, tex_h);
2034 // store the outgoing size
2039 // data == start of bitmap data
2040 // sx == x offset into bitmap
2041 // sy == y offset into bitmap
2042 // src_w == absolute width of section on source bitmap
2043 // src_h == absolute height of section on source bitmap
2044 // bmap_w == width of source bitmap
2045 // bmap_h == height of source bitmap
2046 // tex_w == width of final texture
2047 // tex_h == height of final texture
2048 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)
2057 if ( t->used_this_frame == GL_frame_count ) {
2058 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2063 if(!opengl_free_texture(t)){
2068 // get final texture size
2069 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2071 if ( (tex_w < 1) || (tex_h < 1) ) {
2072 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2076 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2077 t->u_scale = (float)bmap_w / (float)tex_w;
2078 t->v_scale = (float)bmap_h / (float)tex_h;
2079 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2080 t->u_scale = (float)src_w / (float)tex_w;
2081 t->v_scale = (float)src_h / (float)tex_h;
2088 glGenTextures (1, &t->texture_handle);
2091 if (t->texture_handle == 0) {
2092 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2096 GL_bound_texture = t;
2098 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2100 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2102 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2103 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2104 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2106 /* this should be set next anyway */
2107 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2108 // gr_opengl_set_texture_state(GL_current_texture_source);
2111 switch (bitmap_type) {
2113 case TCACHE_TYPE_AABITMAP:
2116 ubyte *bmp_data = ((ubyte*)data);
2122 for (i=0; i<16; i++) {
2123 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2126 for ( ; i<256; i++ ) {
2130 if (GL_use_luminance_alpha) {
2131 texmem = (ubyte *) malloc (tex_w*tex_h*2);
2134 for (i=0;i<tex_h;i++)
2136 for (j=0;j<tex_w;j++)
2138 if (i < bmap_h && j < bmap_w) {
2140 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2148 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2152 texmem = (ubyte *) malloc (tex_w*tex_h*4);
2155 for (i=0;i<tex_h;i++)
2157 for (j=0;j<tex_w;j++)
2159 if (i < bmap_h && j < bmap_w) {
2160 *texmemp++ = 0xff; /* R */
2161 *texmemp++ = 0xff; /* G */
2162 *texmemp++ = 0xff; /* B */
2163 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2173 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texmem);
2179 case TCACHE_TYPE_BITMAP_SECTION:
2182 ubyte *bmp_data = ((ubyte*)data);
2183 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2184 ubyte *texmemp = texmem;
2186 for (i=0;i<tex_h;i++)
2188 for (j=0;j<tex_w;j++)
2190 if (i < src_h && j < src_w) {
2191 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2192 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2199 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2200 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2208 ubyte *bmp_data = ((ubyte*)data);
2209 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2210 ubyte *texmemp = texmem;
2212 fix u, utmp, v, du, dv;
2216 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2217 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2219 for (j=0;j<tex_h;j++)
2222 for (i=0;i<tex_w;i++)
2224 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2225 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2231 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 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;
2243 t->size = tex_w * tex_h * 2;
2244 t->w = (ushort)tex_w;
2245 t->h = (ushort)tex_h;
2246 GL_textures_in_frame += t->size;
2248 GL_textures_in += t->size;
2254 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2258 int final_w, final_h;
2262 // setup texture/bitmap flags
2264 switch(bitmap_type){
2265 case TCACHE_TYPE_AABITMAP:
2266 flags |= BMP_AABITMAP;
2269 case TCACHE_TYPE_NORMAL:
2270 flags |= BMP_TEX_OTHER;
2271 case TCACHE_TYPE_XPARENT:
2272 flags |= BMP_TEX_XPARENT;
2274 case TCACHE_TYPE_NONDARKENING:
2276 flags |= BMP_TEX_NONDARK;
2280 // lock the bitmap into the proper format
2281 bmp = bm_lock(bitmap_handle, bpp, flags);
2282 if ( bmp == NULL ) {
2283 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2292 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2293 // max_w /= D3D_texture_divider;
2294 // max_h /= D3D_texture_divider;
2296 // Detail.debris_culling goes from 0 to 4.
2297 max_w /= (16 >> Detail.hardware_textures);
2298 max_h /= (16 >> Detail.hardware_textures);
2302 // get final texture size as it will be allocated as a DD surface
2303 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2305 // if this tcache slot has no bitmap
2306 if ( tslot->bitmap_id < 0) {
2309 // different bitmap altogether - determine if the new one can use the old one's slot
2310 else if (tslot->bitmap_id != bitmap_handle) {
2311 if((final_w == tslot->w) && (final_h == tslot->h)){
2313 //ml_printf("Reloading texture %d\n", bitmap_handle);
2320 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);
2322 // unlock the bitmap
2323 bm_unlock(bitmap_handle);
2328 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2332 int final_w, final_h;
2333 int section_x, section_y;
2336 // setup texture/bitmap flags
2337 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2338 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2339 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2341 flags = BMP_TEX_XPARENT;
2343 // lock the bitmap in the proper format
2344 bmp = bm_lock(bitmap_handle, 16, flags);
2345 if ( bmp == NULL ) {
2346 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2349 // determine the width and height of this section
2350 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2352 // get final texture size as it will be allocated as an opengl texture
2353 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2355 // if this tcache slot has no bitmap
2356 if ( tslot->bitmap_id < 0) {
2359 // different bitmap altogether - determine if the new one can use the old one's slot
2360 else if (tslot->bitmap_id != bitmap_handle) {
2361 if((final_w == tslot->w) && (final_h == tslot->h)){
2369 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);
2371 // unlock the bitmap
2372 bm_unlock(bitmap_handle);
2378 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2379 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)
2388 GL_last_bitmap_id = -1;
2392 if ( GL_last_detail != Detail.hardware_textures ) {
2393 GL_last_detail = Detail.hardware_textures;
2394 opengl_tcache_flush();
2401 int n = bm_get_cache_slot (bitmap_id, 1);
2402 tcache_slot_opengl *t = &Textures[n];
2404 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)) {
2405 t->used_this_frame = GL_frame_count;
2407 // mark all children as used
2408 if(GL_texture_sections){
2409 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2410 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2411 if(t->data_sections[idx][s_idx] != NULL){
2412 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2418 *u_scale = t->u_scale;
2419 *v_scale = t->v_scale;
2423 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2424 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2425 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2431 // if the texture sections haven't been created yet
2432 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2434 // lock the bitmap in the proper format
2435 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2436 bm_unlock(bitmap_id);
2438 // now lets do something for each texture
2440 for(idx=0; idx<bmp->sections.num_x; idx++){
2441 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2442 // hmm. i'd rather we didn't have to do it this way...
2443 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2447 // not used this frame
2448 t->data_sections[idx][s_idx]->used_this_frame = 0;
2452 // zero out pretty much everything in the parent struct since he's just the root
2453 t->bitmap_id = bitmap_id;
2454 t->texture_handle = 0;
2455 t->time_created = t->data_sections[sx][sy]->time_created;
2456 t->used_this_frame = 0;
2458 t->vram_texture = NULL;
2459 t->vram_texture_surface = NULL
2463 // argh. we failed to upload. free anything we can
2465 opengl_free_texture(t);
2467 // swap in the texture we want
2469 t = t->data_sections[sx][sy];
2472 // all other "normal" textures
2473 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2474 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2477 // everything went ok
2478 if(ret_val && (t->texture_handle) && !vram_full){
2479 *u_scale = t->u_scale;
2480 *v_scale = t->v_scale;
2482 GL_bound_texture = t;
2484 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2486 /* this should be set next anyway */
2487 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2488 // gr_opengl_set_texture_state(GL_current_texture_source);
2491 GL_last_bitmap_id = t->bitmap_id;
2492 GL_last_bitmap_type = bitmap_type;
2493 GL_last_section_x = sx;
2494 GL_last_section_y = sy;
2496 t->used_this_frame = GL_frame_count;
2500 GL_last_bitmap_id = -1;
2501 GL_last_bitmap_type = -1;
2503 GL_last_section_x = -1;
2504 GL_last_section_y = -1;
2506 GL_bound_texture = NULL;
2508 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2515 void gr_opengl_set_clear_color(int r, int g, int b)
2517 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2520 void gr_opengl_flash(int r, int g, int b)
2526 if ( r || g || b ) {
2527 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2529 float x1, x2, y1, y2;
2530 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2531 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2532 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2533 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2535 glColor4ub(r, g, b, 255);
2537 glVertex3f (x1, y2, -0.99);
2539 glVertex3f (x2, y2, -0.99);
2541 glVertex3f (x2, y1, -0.99);
2543 glVertex3f (x1, y1, -0.99);
2548 int gr_opengl_zbuffer_get()
2550 if ( !gr_global_zbuffering ) {
2551 return GR_ZBUFF_NONE;
2553 return gr_zbuffering_mode;
2556 int gr_opengl_zbuffer_set(int mode)
2558 int tmp = gr_zbuffering_mode;
2560 gr_zbuffering_mode = mode;
2562 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2570 void gr_opengl_zbuffer_clear(int mode)
2574 gr_zbuffering_mode = GR_ZBUFF_FULL;
2575 gr_global_zbuffering = 1;
2577 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2578 glClear ( GL_DEPTH_BUFFER_BIT );
2581 gr_zbuffering_mode = GR_ZBUFF_NONE;
2582 gr_global_zbuffering = 0;
2586 void gr_opengl_set_gamma(float gamma)
2589 Gr_gamma_int = int (Gr_gamma*10);
2591 // Create the Gamma lookup table
2593 for (i=0;i<256; i++) {
2594 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2597 } else if ( v < 0 ) {
2600 Gr_gamma_lookup[i] = v;
2603 // Flush any existing textures
2604 opengl_tcache_flush();
2607 void gr_opengl_fade_in(int instantaneous)
2612 void gr_opengl_fade_out(int instantaneous)
2617 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2620 glReadBuffer(GL_FRONT);
2622 glReadBuffer(GL_BACK);
2625 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2627 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2629 if (gr_screen.bits_per_pixel == 15) {
2630 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2631 } else if (gr_screen.bits_per_pixel == 32) {
2632 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2635 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2638 static int Gr_opengl_mouse_saved = 0;
2639 static int Gr_opengl_mouse_saved_x1 = 0;
2640 static int Gr_opengl_mouse_saved_y1 = 0;
2641 static int Gr_opengl_mouse_saved_x2 = 0;
2642 static int Gr_opengl_mouse_saved_y2 = 0;
2643 static int Gr_opengl_mouse_saved_w = 0;
2644 static int Gr_opengl_mouse_saved_h = 0;
2645 #define MAX_SAVE_SIZE (32*32)
2646 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2648 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2650 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2652 Gr_opengl_mouse_saved_x1 = x;
2653 Gr_opengl_mouse_saved_y1 = y;
2654 Gr_opengl_mouse_saved_x2 = x+w-1;
2655 Gr_opengl_mouse_saved_y2 = y+h-1;
2657 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2658 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2659 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2660 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2662 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2663 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2665 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2666 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2668 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2670 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2672 glReadBuffer(GL_BACK);
2673 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);
2675 Gr_opengl_mouse_saved = 1;
2678 int gr_opengl_save_screen()
2682 if ( Gr_saved_screen ) {
2683 mprintf(( "Screen alread saved!\n" ));
2687 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2688 if (!Gr_saved_screen) {
2689 mprintf(( "Couldn't get memory for saved screen!\n" ));
2693 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2694 if (!Gr_saved_screen_tmp) {
2695 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2699 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2701 glReadBuffer(GL_FRONT);
2702 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);
2706 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2707 dptr = (ubyte *)Gr_saved_screen;
2708 for (int j = 0; j < gr_screen.max_h; j++) {
2709 sptr -= gr_screen.max_w*2;
2710 memcpy(dptr, sptr, gr_screen.max_w*2);
2711 dptr += gr_screen.max_w*2;
2714 free(Gr_saved_screen_tmp);
2716 if (Gr_opengl_mouse_saved) {
2717 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2718 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2719 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2720 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2723 dptr -= gr_screen.max_w*2;
2727 // this leaks texture handles, and the opengl doesn't currently
2728 // perform some sort of garbage collection, so a hack was added
2729 // to bmpman to make it free textures when released
2730 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2732 return Gr_saved_screen_bitmap;
2735 void gr_opengl_restore_screen(int id)
2739 if ( !Gr_saved_screen ) {
2744 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2746 gr_set_bitmap(Gr_saved_screen_bitmap);
2750 void gr_opengl_free_screen(int id)
2752 bm_release(Gr_saved_screen_bitmap);
2754 if ( Gr_saved_screen ) {
2755 free( Gr_saved_screen );
2756 Gr_saved_screen = NULL;
2760 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2765 void gr_opengl_dump_frame_stop()
2770 void gr_opengl_dump_frame()
2775 uint gr_opengl_lock()
2782 void gr_opengl_unlock()
2786 void opengl_zbias(int bias)
2789 glEnable(GL_POLYGON_OFFSET_FILL);
2790 glPolygonOffset(0.0, -bias);
2792 glDisable(GL_POLYGON_OFFSET_FILL);
2796 extern char *Osreg_title;
2797 void gr_opengl_init()
2800 gr_opengl_cleanup();
2804 mprintf(( "Initializing opengl graphics device...\n" ));
2808 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2810 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2816 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2817 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2818 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2819 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2820 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2822 int flags = SDL_OPENGL;
2824 if (os_config_read_uint( NULL, "Fullscreen", 0 ) == 1)
2825 flags |= SDL_FULLSCREEN;
2827 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2829 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2834 SDL_WM_SetCaption (Osreg_title, "FS2");
2836 /* might as well put this here */
2837 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2839 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2841 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2843 glMatrixMode(GL_PROJECTION);
2845 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2846 glMatrixMode(GL_MODELVIEW);
2849 glShadeModel(GL_SMOOTH);
2850 glEnable(GL_DITHER);
2851 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2852 glHint(GL_FOG_HINT, GL_NICEST);
2854 glEnable(GL_DEPTH_TEST);
2857 glEnable(GL_TEXTURE_2D);
2859 glDepthRange(0.0, 1.0);
2861 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2862 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2864 D3D_32bit = 1; // grd3d.cpp
2865 extern int D3D_enabled;
2868 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2869 1 = use secondary color ext
2870 2 = use opengl linear fog
2872 D3D_fog_mode = 2; // grd3d.cpp
2876 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2883 gr_screen.bits_per_pixel = 15;
2884 gr_screen.bytes_per_pixel = 2;
2889 Gr_red.mask = 0x7C00;
2894 Gr_green.mask = 0x3E0;
2899 Gr_blue.mask = 0x1F;
2904 gr_screen.bits_per_pixel = 16;
2905 gr_screen.bytes_per_pixel = 2;
2910 Gr_red.mask = 0xF800;
2915 Gr_green.mask = 0x7E0;
2920 Gr_blue.mask = 0x1F;
2925 gr_screen.bits_per_pixel = 32;
2926 gr_screen.bytes_per_pixel = 4;
2931 Gr_red.mask = 0xff0000;
2936 Gr_green.mask = 0xff00;
2941 Gr_blue.mask = 0xff;
2946 Int3(); // Illegal bpp
2949 // DDOI - set these so no one else does!
2950 Gr_ta_red.mask = 0x0f00;
2951 Gr_ta_red.shift = 8;
2952 Gr_ta_red.scale = 16;
2954 Gr_ta_green.mask = 0x00f0;
2955 Gr_ta_green.shift = 4;
2956 Gr_ta_green.scale = 16;
2958 Gr_ta_blue.mask = 0x000f;
2959 Gr_ta_blue.shift = 0;
2960 Gr_ta_blue.scale = 16;
2962 Gr_ta_alpha.mask = 0xf000;
2963 Gr_ta_alpha.shift = 12;
2964 Gr_ta_alpha.scale = 16;
2967 opengl_tcache_init (1);
2970 Gr_current_red = &Gr_red;
2971 Gr_current_blue = &Gr_blue;
2972 Gr_current_green = &Gr_green;
2973 Gr_current_alpha = &Gr_alpha;
2975 gr_screen.gf_flip = gr_opengl_flip;
2976 gr_screen.gf_flip_window = gr_opengl_flip_window;
2977 gr_screen.gf_set_clip = gr_opengl_set_clip;
2978 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2979 gr_screen.gf_set_font = grx_set_font;
2981 gr_screen.gf_set_color = gr_opengl_set_color;
2982 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2983 gr_screen.gf_create_shader = gr_opengl_create_shader;
2984 gr_screen.gf_set_shader = gr_opengl_set_shader;
2985 gr_screen.gf_clear = gr_opengl_clear;
2986 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2987 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2988 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2989 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2991 gr_screen.gf_rect = gr_opengl_rect;
2992 gr_screen.gf_shade = gr_opengl_shade;
2993 gr_screen.gf_string = gr_opengl_string;
2994 gr_screen.gf_circle = gr_opengl_circle;
2996 gr_screen.gf_line = gr_opengl_line;
2997 gr_screen.gf_aaline = gr_opengl_aaline;
2998 gr_screen.gf_pixel = gr_opengl_pixel;
2999 gr_screen.gf_scaler = gr_opengl_scaler;
3000 gr_screen.gf_tmapper = gr_opengl_tmapper;
3002 gr_screen.gf_gradient = gr_opengl_gradient;
3004 gr_screen.gf_set_palette = gr_opengl_set_palette;
3005 gr_screen.gf_get_color = gr_opengl_get_color;
3006 gr_screen.gf_init_color = gr_opengl_init_color;
3007 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3008 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3009 gr_screen.gf_print_screen = gr_opengl_print_screen;
3011 gr_screen.gf_fade_in = gr_opengl_fade_in;
3012 gr_screen.gf_fade_out = gr_opengl_fade_out;
3013 gr_screen.gf_flash = gr_opengl_flash;
3015 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3016 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3017 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3019 gr_screen.gf_save_screen = gr_opengl_save_screen;
3020 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3021 gr_screen.gf_free_screen = gr_opengl_free_screen;
3023 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3024 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3025 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3027 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3029 gr_screen.gf_lock = gr_opengl_lock;
3030 gr_screen.gf_unlock = gr_opengl_unlock;
3032 gr_screen.gf_fog_set = gr_opengl_fog_set;
3034 gr_screen.gf_get_region = gr_opengl_get_region;
3036 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3038 gr_screen.gf_set_cull = gr_opengl_set_cull;
3040 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3042 gr_screen.gf_filter_set = gr_opengl_filter_set;
3044 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3046 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;