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.54 2002/07/30 15:00:15 relnev
19 * not use luminance alpha by default
21 * Revision 1.53 2002/06/22 17:08:16 relnev
22 * more fixes to unused alpha code
24 * Revision 1.52 2002/06/21 23:59:14 relnev
25 * moved a gr_opengl_set_state to after the gr_tcache_set
27 * Revision 1.51 2002/06/19 04:52:45 relnev
28 * MacOS X updates (Ryan)
30 * Revision 1.50 2002/06/18 23:20:44 relnev
33 * adjusted line offsets.
35 * Revision 1.49 2002/06/17 23:11:39 relnev
36 * enable sdl key repeating.
40 * Revision 1.48 2002/06/09 04:41:17 relnev
41 * added copyright header
43 * Revision 1.47 2002/06/09 03:16:04 relnev
46 * removed unneeded asm, old sdl 2d setup.
48 * fixed crash caused by opengl_get_region.
50 * Revision 1.46 2002/06/05 04:03:32 relnev
51 * finished cfilesystem.
53 * removed some old code.
55 * fixed mouse save off-by-one.
59 * Revision 1.45 2002/06/03 09:25:37 relnev
60 * implement mouse cursor and screen save/restore
62 * Revision 1.44 2002/06/02 18:46:59 relnev
65 * Revision 1.43 2002/06/02 11:34:00 relnev
68 * Revision 1.42 2002/06/02 10:28:17 relnev
69 * fix texture handle leak
71 * Revision 1.41 2002/06/01 09:00:34 relnev
72 * silly debug memmanager
74 * Revision 1.40 2002/06/01 07:12:33 relnev
75 * a few NDEBUG updates.
77 * removed a few warnings.
79 * Revision 1.39 2002/06/01 05:33:15 relnev
80 * copied more code over.
82 * added scissor clipping.
84 * Revision 1.38 2002/06/01 03:35:27 relnev
87 * Revision 1.37 2002/06/01 03:32:00 relnev
88 * fix texture loading mistake.
90 * enable some d3d stuff for opengl also
92 * Revision 1.36 2002/05/31 23:25:03 relnev
95 * Revision 1.34 2002/05/31 22:15:22 relnev
98 * Revision 1.33 2002/05/31 22:04:55 relnev
99 * use d3d rect_internal
101 * Revision 1.32 2002/05/31 06:28:23 relnev
104 * Revision 1.31 2002/05/31 06:04:39 relnev
107 * Revision 1.30 2002/05/31 03:56:11 theoddone33
108 * Change tmapper polygon winding and enable culling
110 * Revision 1.29 2002/05/31 03:34:02 theoddone33
114 * Revision 1.28 2002/05/31 00:06:59 relnev
117 * Revision 1.27 2002/05/30 23:46:29 theoddone33
118 * some minor key changes (not necessarily fixes)
120 * Revision 1.26 2002/05/30 23:33:12 relnev
121 * implemented a few more functions.
123 * Revision 1.25 2002/05/30 23:01:16 relnev
124 * implement gr_opengl_set_state.
126 * Revision 1.24 2002/05/30 22:12:57 relnev
127 * finish default texture case
129 * Revision 1.23 2002/05/30 22:02:30 theoddone33
132 * Revision 1.22 2002/05/30 21:44:48 relnev
133 * implemented some missing texture stuff.
135 * enable bitmap polys for opengl.
137 * work around greenness in bitmaps.
139 * Revision 1.21 2002/05/30 17:29:30 theoddone33
140 * Fix some more stubs, change at least one polygon winding since culling is now
143 * Revision 1.20 2002/05/30 16:50:24 theoddone33
144 * Keyboard partially fixed
146 * Revision 1.19 2002/05/30 08:13:14 relnev
149 * Revision 1.18 2002/05/29 23:37:36 relnev
152 * Revision 1.17 2002/05/29 23:17:49 theoddone33
153 * Non working text code and fixed keys
155 * Revision 1.16 2002/05/29 19:45:13 theoddone33
156 * More changes on texture loading
158 * Revision 1.15 2002/05/29 19:06:48 theoddone33
159 * Enable string printing. Enable texture mapping
161 * Revision 1.14 2002/05/29 08:54:40 relnev
162 * "fixed" bitmap drawing.
164 * copied more d3d code over.
166 * Revision 1.13 2002/05/29 06:25:13 theoddone33
167 * Keyboard input, mouse tracking now work
169 * Revision 1.12 2002/05/29 04:52:45 relnev
172 * Revision 1.11 2002/05/29 04:29:56 relnev
173 * removed some unncessary stubbing, implemented opengl rect
175 * Revision 1.10 2002/05/29 04:13:27 theoddone33
178 * Revision 1.9 2002/05/29 03:35:51 relnev
181 * Revision 1.8 2002/05/29 03:30:05 relnev
182 * update opengl stubs
184 * Revision 1.7 2002/05/29 02:52:32 theoddone33
185 * Enable OpenGL renderer
187 * Revision 1.6 2002/05/28 04:56:51 theoddone33
188 * runs a little bit now
190 * Revision 1.5 2002/05/28 04:07:28 theoddone33
191 * New graphics stubbing arrangement
193 * Revision 1.4 2002/05/27 23:39:34 relnev
196 * Revision 1.3 2002/05/27 22:35:01 theoddone33
199 * Revision 1.2 2002/05/27 22:32:02 theoddone33
200 * throw all d3d stuff at opengl
202 * Revision 1.1.1.1 2002/05/03 03:28:09 root
206 * 10 7/14/99 9:42a Dave
207 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
210 * 9 7/09/99 9:51a Dave
211 * Added thick polyline code.
213 * 8 6/29/99 10:35a Dave
214 * Interface polygon bitmaps! Whee!
216 * 7 2/03/99 11:44a Dave
217 * Fixed d3d transparent textures.
219 * 6 1/24/99 11:37p Dave
220 * First full rev of beam weapons. Very customizable. Removed some bogus
221 * Int3()'s in low level net code.
223 * 5 12/18/98 1:13a Dave
224 * Rough 1024x768 support for Direct3D. Proper detection and usage through
227 * 4 12/06/98 2:36p Dave
228 * Drastically improved nebula fogging.
230 * 3 11/11/98 5:37p Dave
231 * Checkin for multiplayer testing.
233 * 2 10/07/98 10:53a Dave
236 * 1 10/07/98 10:49a Dave
238 * 14 5/20/98 9:46p John
239 * added code so the places in code that change half the palette don't
240 * have to clear the screen.
242 * 13 5/06/98 5:30p John
243 * Removed unused cfilearchiver. Removed/replaced some unused/little used
244 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
245 * DirectX header files and libs that fixed the Direct3D alpha blending
248 * 12 4/14/98 12:15p John
249 * Made 16-bpp movies work.
251 * 11 3/12/98 5:36p John
252 * Took out any unused shaders. Made shader code take rgbc instead of
253 * matrix and vector since noone used it like a matrix and it would have
254 * been impossible to do in hardware. Made Glide implement a basic
255 * shader for online help.
257 * 10 3/10/98 4:18p John
258 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
259 * & Glide have popups and print screen. Took out all >8bpp software
260 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
261 * support Fred. Made zbuffering key off of functions rather than one
264 * 9 12/02/97 4:00p John
265 * Added first rev of thruster glow, along with variable levels of
266 * translucency, which retquired some restructing of palman.
268 * 8 10/03/97 9:10a John
269 * added better antialiased line drawer
271 * 7 9/23/97 10:45a John
272 * made so you can tell bitblt code to rle a bitmap by passing flag to
275 * 6 9/09/97 11:01a Sandeep
276 * fixed warning level 4 bugs
278 * 5 7/10/97 2:06p John
279 * added code to specify alphablending type for bitmaps.
281 * 4 6/17/97 7:04p John
282 * added d3d support for gradients.
283 * fixed some color bugs by adding screen signatures instead of watching
284 * flags and palette changes.
286 * 3 6/12/97 2:50a Lawrance
287 * bm_unlock() now passed bitmap number, not pointer
289 * 2 6/11/97 1:12p John
290 * Started fixing all the text colors in the game.
292 * 1 5/12/97 12:14p John
299 #include <windowsx.h>
313 #include "floating.h"
315 #include "systemvars.h"
316 #include "grinternal.h"
317 #include "gropengl.h"
321 #include "osregistry.h"
323 static int Inited = 0;
325 typedef enum gr_texture_source {
327 TEXTURE_SOURCE_DECAL,
328 TEXTURE_SOURCE_NO_FILTERING,
331 typedef enum gr_alpha_blend {
332 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
333 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
334 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
335 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
336 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
339 typedef enum gr_zbuffer_type {
346 #define NEBULA_COLORS 20
348 volatile int GL_activate = 0;
349 volatile int GL_deactivate = 0;
351 static int GL_use_luminance_alpha;
353 static char *Gr_saved_screen = NULL;
354 static int Gr_saved_screen_bitmap;
357 // Throw in some dummy functions - DDOI
359 int D3D_32bit = 0; // grd3d.cpp
360 int D3D_fog_mode = -1; // grd3d.cpp
361 int D3D_inited = 0; // grd3d.cpp
362 int D3D_zbias = 1; // grd3d.cpp
363 int D3d_rendition_uvs = 0; // grd3d.cpp
370 void d3d_zbias (int a)
377 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
378 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
379 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
381 void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
383 /* TODO: this doesn't work unless it's handled at the per-texture level */
384 /* if (ts != GL_current_texture_source) */ {
386 case TEXTURE_SOURCE_NONE:
387 glBindTexture(GL_TEXTURE_2D, 0);
388 gr_tcache_set(-1, -1, NULL, NULL );
390 case TEXTURE_SOURCE_DECAL:
391 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
392 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
393 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
395 case TEXTURE_SOURCE_NO_FILTERING:
396 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
397 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
398 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
404 GL_current_texture_source = ts;
407 if (ab != GL_current_alpha_blend) {
409 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
410 glBlendFunc(GL_ONE, GL_ZERO);
412 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
413 glBlendFunc(GL_ONE, GL_ONE);
415 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
416 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
418 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
419 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
421 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
422 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
428 GL_current_alpha_blend = ab;
431 if (zt != GL_current_zbuffer_type) {
433 case ZBUFFER_TYPE_NONE:
434 glDepthFunc(GL_ALWAYS);
435 glDepthMask(GL_FALSE);
437 case ZBUFFER_TYPE_READ:
438 glDepthFunc(GL_LESS);
439 glDepthMask(GL_FALSE);
441 case ZBUFFER_TYPE_WRITE:
442 glDepthFunc(GL_ALWAYS);
443 glDepthMask(GL_TRUE);
445 case ZBUFFER_TYPE_FULL:
446 glDepthFunc(GL_LESS);
447 glDepthMask(GL_TRUE);
453 GL_current_zbuffer_type = zt;
457 void gr_opengl_activate(int active)
463 make sure window is active and mouse grabbed
469 make sure mouse is not grabbed and window minimized
477 void opengl_tcache_flush ();
479 void gr_opengl_preload_init()
481 if (gr_screen.mode != GR_OPENGL) {
485 opengl_tcache_flush ();
488 int GL_should_preload = 0;
489 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
491 if ( gr_screen.mode != GR_OPENGL) {
495 if ( !GL_should_preload ) {
499 float u_scale, v_scale;
503 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
505 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
509 mprintf(("Texture upload failed!\n" ));
515 void gr_opengl_pixel(int x, int y)
520 void gr_opengl_clear()
522 glClearColor(gr_screen.current_clear_color.red / 255.0,
523 gr_screen.current_clear_color.green / 255.0,
524 gr_screen.current_clear_color.blue / 255.0, 1.0);
526 glClear ( GL_COLOR_BUFFER_BIT );
529 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
530 void opengl_tcache_frame ();
531 void gr_opengl_flip()
539 extern int Gr_opengl_mouse_saved;
540 Gr_opengl_mouse_saved = 0;
542 if ( mouse_is_visible() ) {
546 mouse_get_pos( &mx, &my );
548 gr_opengl_save_mouse_area(mx,my,32,32);
550 if ( Gr_cursor == -1 ) {
553 gr_set_bitmap(Gr_cursor);
559 GLenum error = glGetError();
562 error = glGetError();
564 if (error != GL_NO_ERROR) {
565 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
568 } while (error != GL_NO_ERROR);
571 SDL_GL_SwapBuffers ();
573 opengl_tcache_frame ();
575 int cnt = GL_activate;
578 opengl_tcache_flush();
579 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
585 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
589 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
594 void gr_opengl_set_clip(int x,int y,int w,int h)
596 // check for sanity of parameters
602 if (x >= gr_screen.max_w)
603 x = gr_screen.max_w - 1;
604 if (y >= gr_screen.max_h)
605 y = gr_screen.max_h - 1;
607 if (x + w > gr_screen.max_w)
608 w = gr_screen.max_w - x;
609 if (y + h > gr_screen.max_h)
610 h = gr_screen.max_h - y;
612 if (w > gr_screen.max_w)
614 if (h > gr_screen.max_h)
617 gr_screen.offset_x = x;
618 gr_screen.offset_y = y;
619 gr_screen.clip_left = 0;
620 gr_screen.clip_right = w-1;
621 gr_screen.clip_top = 0;
622 gr_screen.clip_bottom = h-1;
623 gr_screen.clip_width = w;
624 gr_screen.clip_height = h;
626 glEnable(GL_SCISSOR_TEST);
627 glScissor(x, gr_screen.max_h-y-h, w, h);
630 void gr_opengl_reset_clip()
632 gr_screen.offset_x = 0;
633 gr_screen.offset_y = 0;
634 gr_screen.clip_left = 0;
635 gr_screen.clip_top = 0;
636 gr_screen.clip_right = gr_screen.max_w - 1;
637 gr_screen.clip_bottom = gr_screen.max_h - 1;
638 gr_screen.clip_width = gr_screen.max_w;
639 gr_screen.clip_height = gr_screen.max_h;
641 glDisable(GL_SCISSOR_TEST);
642 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
645 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
647 gr_screen.current_alpha = alpha;
648 gr_screen.current_alphablend_mode = alphablend_mode;
649 gr_screen.current_bitblt_mode = bitblt_mode;
650 gr_screen.current_bitmap = bitmap_num;
652 gr_screen.current_bitmap_sx = sx;
653 gr_screen.current_bitmap_sy = sy;
656 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
658 shade->screen_sig = gr_screen.signature;
665 void gr_opengl_set_shader( shader * shade )
668 if (shade->screen_sig != gr_screen.signature) {
669 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
671 gr_screen.current_shader = *shade;
673 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
678 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
680 STUB_FUNCTION; /* who called me? */
683 extern int GL_last_bitmap_id;
684 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
688 GLubyte *sptr, *dptr;
692 int cw = min(bmp->w, w);
693 int ch = min(bmp->h, h);
695 GL_last_bitmap_id = -1; /* HACK! */
697 glColor4f(1.0, 1.0, 1.0, 1.0);
698 glBindTexture(GL_TEXTURE_2D, bitmapTex);
701 for (iy = sy; iy < ch; iy += 256) {
703 ih = min(256, (ch-iy));
705 for (ix = sx; ix < cw; ix += 256) {
707 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
709 iw = min(256, (cw-ix));
715 memcpy(dptr, sptr, iw*2);
723 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
733 t = (float)ih / 256.0;
738 s = (float)iw / 256.0;
739 t = (float)ih / 256.0;
744 s = (float)iw / 256.0;
756 bm_unlock(gr_screen.current_bitmap);
761 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
763 STUB_FUNCTION; /* who called me? */
770 int dx1=x, dx2=x+w-1;
771 int dy1=y, dy2=y+h-1;
774 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
779 if ( count > 1 ) Int3();
783 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
784 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
785 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
786 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
787 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
788 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
815 if ( w < 1 ) return; // clipped away!
816 if ( h < 1 ) return; // clipped away!
820 // Make sure clipping algorithm works
824 Assert( w == (dx2-dx1+1) );
825 Assert( h == (dy2-dy1+1) );
828 Assert( sx+w <= bw );
829 Assert( sy+h <= bh );
830 Assert( dx2 >= dx1 );
831 Assert( dy2 >= dy1 );
832 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
833 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
834 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
835 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
838 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
839 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
841 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
845 void gr_opengl_bitmap(int x, int y)
847 STUB_FUNCTION; /* who called me? */
851 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
852 int dx1=x, dx2=x+w-1;
853 int dy1=y, dy2=y+h-1;
856 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
857 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
858 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
859 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
860 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
861 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
863 if ( sx < 0 ) return;
864 if ( sy < 0 ) return;
865 if ( sx >= w ) return;
866 if ( sy >= h ) return;
868 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
870 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
874 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
878 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
880 saved_zbuf = gr_zbuffer_get();
882 // start the frame, no zbuffering, no culling
884 gr_zbuffer_set(GR_ZBUFF_NONE);
893 v[0].flags = PF_PROJECTED;
900 v[1].sx = i2fl(x + w);
905 v[1].flags = PF_PROJECTED;
912 v[2].sx = i2fl(x + w);
913 v[2].sy = i2fl(y + h);
917 v[2].flags = PF_PROJECTED;
925 v[3].sy = i2fl(y + h);
929 v[3].flags = PF_PROJECTED;
937 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
941 // restore zbuffer and culling
942 gr_zbuffer_set(saved_zbuf);
946 void gr_opengl_rect(int x,int y,int w,int h)
948 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);
951 void gr_opengl_shade(int x,int y,int w,int h)
958 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
959 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
960 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
961 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
962 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
963 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
964 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
965 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
967 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
970 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
975 if ( !gr_screen.current_color.is_alphacolor ) return;
977 float u_scale, v_scale;
979 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
980 // Couldn't set texture
981 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
985 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
987 float u0, u1, v0, v1;
988 float x1, x2, y1, y2;
991 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
993 u0 = u_scale*i2fl(sx)/i2fl(bw);
994 v0 = v_scale*i2fl(sy)/i2fl(bh);
996 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
997 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
999 x1 = i2fl(x+gr_screen.offset_x);
1000 y1 = i2fl(y+gr_screen.offset_y);
1001 x2 = i2fl(x+w+gr_screen.offset_x);
1002 y2 = i2fl(y+h+gr_screen.offset_y);
1004 if ( gr_screen.current_color.is_alphacolor ) {
1005 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
1007 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
1011 glTexCoord2f (u0, v1);
1012 glVertex3f (x1, y2, -0.99);
1014 glTexCoord2f (u1, v1);
1015 glVertex3f (x2, y2, -0.99);
1017 glTexCoord2f (u1, v0);
1018 glVertex3f (x2, y1, -0.99);
1020 glTexCoord2f (u0, v0);
1021 glVertex3f (x1, y1, -0.99);
1025 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
1032 int dx1=x, dx2=x+w-1;
1033 int dy1=y, dy2=y+h-1;
1036 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
1041 if ( count > 1 ) Int3();
1045 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1046 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1047 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1048 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1049 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1050 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1067 if ( sx + w > bw ) {
1072 if ( sy + h > bh ) {
1077 if ( w < 1 ) return; // clipped away!
1078 if ( h < 1 ) return; // clipped away!
1082 // Make sure clipping algorithm works
1086 Assert( w == (dx2-dx1+1) );
1087 Assert( h == (dy2-dy1+1) );
1090 Assert( sx+w <= bw );
1091 Assert( sy+h <= bh );
1092 Assert( dx2 >= dx1 );
1093 Assert( dy2 >= dy1 );
1094 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
1095 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
1096 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
1097 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
1100 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
1101 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1104 void gr_opengl_aabitmap(int x, int y)
1108 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
1109 int dx1=x, dx2=x+w-1;
1110 int dy1=y, dy2=y+h-1;
1113 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1114 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1115 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1116 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1117 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1118 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1120 if ( sx < 0 ) return;
1121 if ( sy < 0 ) return;
1122 if ( sx >= w ) return;
1123 if ( sy >= h ) return;
1125 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
1126 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1129 void gr_opengl_string( int sx, int sy, char *s )
1131 int width, spacing, letter;
1134 if ( !Current_font ) {
1138 gr_set_bitmap(Current_font->bitmap_id);
1143 if (sx==0x8000) { //centered
1144 x = get_centered_x(s);
1154 while (*s== '\n' ) {
1156 y += Current_font->h;
1157 if (sx==0x8000) { //centered
1158 x = get_centered_x(s);
1163 if (*s == 0 ) break;
1165 letter = get_char_width(s[0],s[1],&width,&spacing);
1168 //not in font, draw as space
1176 // Check if this character is totally clipped
1177 if ( x + width < gr_screen.clip_left ) continue;
1178 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1179 if ( x > gr_screen.clip_right ) continue;
1180 if ( y > gr_screen.clip_bottom ) continue;
1183 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1184 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1188 wc = width - xd; hc = Current_font->h - yd;
1189 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1190 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1192 if ( wc < 1 ) continue;
1193 if ( hc < 1 ) continue;
1195 int u = Current_font->bm_u[letter];
1196 int v = Current_font->bm_v[letter];
1198 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1202 void gr_opengl_line(int x1,int y1,int x2,int y2)
1204 int clipped = 0, swapped=0;
1206 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1208 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);
1213 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1214 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1215 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1216 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1218 if ( x1 == x2 && y1 == y2 ) {
1219 glBegin (GL_POINTS);
1220 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1221 glVertex3f (sx1, sy1, -0.99f);
1233 } else if ( y1 == y2 ) {
1242 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1243 glVertex3f (sx2, sy2, -0.99f);
1244 glVertex3f (sx1, sy1, -0.99f);
1248 void gr_opengl_aaline(vertex *v1, vertex *v2)
1250 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1253 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1255 int clipped = 0, swapped=0;
1257 if ( !gr_screen.current_color.is_alphacolor ) {
1258 gr_line( x1, y1, x2, y2 );
1262 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);
1264 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1266 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1267 int ba = swapped ? gr_screen.current_color.alpha : 0;
1272 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1273 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1274 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1275 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1283 } else if ( y1 == y2 ) {
1292 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1293 glVertex3f (sx2, sy2, -0.99f);
1294 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1295 glVertex3f (sx1, sy1, -0.99f);
1299 void gr_opengl_circle( int xc, int yc, int d )
1309 if ( (xc+r) < gr_screen.clip_left ) return;
1310 if ( (xc-r) > gr_screen.clip_right ) return;
1311 if ( (yc+r) < gr_screen.clip_top ) return;
1312 if ( (yc-r) > gr_screen.clip_bottom ) return;
1315 // Draw the first octant
1316 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1317 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1322 // Draw the second octant
1323 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1324 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1332 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1333 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1338 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1342 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1343 if (f_float < 0.0f) {
1351 *a = (int)(f_float * 255.0);
1354 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1357 float u_scale = 1.0f, v_scale = 1.0f;
1359 // Make nebula use the texture mapper... this blends the colors better.
1360 if ( flags & TMAP_FLAG_NEBULA ){
1364 gr_texture_source texture_source = (gr_texture_source)-1;
1365 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1366 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1368 if ( gr_zbuffering ) {
1369 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1370 zbuffer_type = ZBUFFER_TYPE_READ;
1372 zbuffer_type = ZBUFFER_TYPE_FULL;
1375 zbuffer_type = ZBUFFER_TYPE_NONE;
1380 int tmap_type = TCACHE_TYPE_NORMAL;
1384 if ( flags & TMAP_FLAG_TEXTURED ) {
1387 r = gr_screen.current_color.red;
1388 g = gr_screen.current_color.green;
1389 b = gr_screen.current_color.blue;
1392 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1395 tmap_type = TCACHE_TYPE_NORMAL;
1396 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1398 // Blend with screen pixel using src*alpha+dst
1399 float factor = gr_screen.current_alpha;
1403 if ( factor <= 1.0f ) {
1404 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1405 r = (r*tmp_alpha)/255;
1406 g = (g*tmp_alpha)/255;
1407 b = (b*tmp_alpha)/255;
1410 tmap_type = TCACHE_TYPE_XPARENT;
1412 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1414 // Blend with screen pixel using src*alpha+dst
1415 float factor = gr_screen.current_alpha;
1417 if ( factor > 1.0f ) {
1420 alpha = fl2i(gr_screen.current_alpha*255.0f);
1424 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1425 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1427 alpha_blend = ALPHA_BLEND_NONE;
1432 if(flags & TMAP_FLAG_BITMAP_SECTION){
1433 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1436 texture_source = TEXTURE_SOURCE_NONE;
1438 if ( flags & TMAP_FLAG_TEXTURED ) {
1439 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 ))
1441 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1445 // use nonfiltered textures for bitmap sections
1446 if(flags & TMAP_FLAG_BITMAP_SECTION){
1447 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1449 texture_source = TEXTURE_SOURCE_DECAL;
1454 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1456 if ( flags & TMAP_FLAG_TEXTURED )
1462 if (flags & TMAP_FLAG_PIXEL_FOG) {
1468 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1470 vertex * va = verts[i];
1474 x = fl2i(va->sx*16.0f);
1475 y = fl2i(va->sy*16.0f);
1477 x += gr_screen.offset_x*16;
1478 y += gr_screen.offset_y*16;
1480 sx = i2fl(x) / 16.0f;
1481 sy = i2fl(y) / 16.0f;
1483 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1494 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1497 glBegin(GL_TRIANGLE_FAN);
1498 for (i = nv-1; i >= 0; i--) {
1499 vertex * va = verts[i];
1505 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1506 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1515 if ( flags & TMAP_FLAG_CORRECT ) {
1521 if (flags & TMAP_FLAG_ALPHA) {
1527 if (flags & TMAP_FLAG_NEBULA ) {
1528 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1529 r = gr_palette[pal*3+0];
1530 g = gr_palette[pal*3+1];
1531 b = gr_palette[pal*3+2];
1532 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1533 r = Gr_gamma_lookup[verts[i]->b];
1534 g = Gr_gamma_lookup[verts[i]->b];
1535 b = Gr_gamma_lookup[verts[i]->b];
1536 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1537 // Make 0.75 be 256.0f
1538 r = Gr_gamma_lookup[verts[i]->r];
1539 g = Gr_gamma_lookup[verts[i]->g];
1540 b = Gr_gamma_lookup[verts[i]->b];
1542 // use constant RGB values...
1544 glColor4ub (r,g,b,a);
1546 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1549 /* this is for GL_EXT_SECONDARY_COLOR */
1550 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1551 /* do separate color call here */
1557 x = fl2i(va->sx*16.0f);
1558 y = fl2i(va->sy*16.0f);
1560 x += gr_screen.offset_x*16;
1561 y += gr_screen.offset_y*16;
1563 sx = i2fl(x) / 16.0f;
1564 sy = i2fl(y) / 16.0f;
1566 if ( flags & TMAP_FLAG_TEXTURED ) {
1569 glTexCoord2f(tu, tv);
1572 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1577 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1579 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1582 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1584 void gr_opengl_scaler(vertex *va, vertex *vb )
1586 float x0, y0, x1, y1;
1587 float u0, v0, u1, v1;
1588 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1589 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1590 float xmin, xmax, ymin, ymax;
1591 int dx0, dy0, dx1, dy1;
1593 //============= CLIP IT =====================
1595 x0 = va->sx; y0 = va->sy;
1596 x1 = vb->sx; y1 = vb->sy;
1598 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1599 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1601 u0 = va->u; v0 = va->v;
1602 u1 = vb->u; v1 = vb->v;
1604 // Check for obviously offscreen bitmaps...
1605 if ( (y1<=y0) || (x1<=x0) ) return;
1606 if ( (x1<xmin ) || (x0>xmax) ) return;
1607 if ( (y1<ymin ) || (y0>ymax) ) return;
1609 clipped_u0 = u0; clipped_v0 = v0;
1610 clipped_u1 = u1; clipped_v1 = v1;
1612 clipped_x0 = x0; clipped_y0 = y0;
1613 clipped_x1 = x1; clipped_y1 = y1;
1615 // Clip the left, moving u0 right as necessary
1617 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1621 // Clip the right, moving u1 left as necessary
1623 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1627 // Clip the top, moving v0 down as necessary
1629 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1633 // Clip the bottom, moving v1 up as necessary
1635 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1639 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1640 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1642 if (dx1<=dx0) return;
1643 if (dy1<=dy0) return;
1645 //============= DRAW IT =====================
1659 v[1].sx = clipped_x1;
1660 v[1].sy = clipped_y0;
1663 v[1].u = clipped_u1;
1664 v[1].v = clipped_v0;
1667 v[2].sx = clipped_x1;
1668 v[2].sy = clipped_y1;
1671 v[2].u = clipped_u1;
1672 v[2].v = clipped_v1;
1675 v[3].sx = clipped_x0;
1676 v[3].sy = clipped_y1;
1679 v[3].u = clipped_u0;
1680 v[3].v = clipped_v1;
1682 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1685 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1689 void gr_opengl_get_color( int * r, int * g, int * b )
1691 if (r) *r = gr_screen.current_color.red;
1692 if (g) *g = gr_screen.current_color.green;
1693 if (b) *b = gr_screen.current_color.blue;
1696 void gr_opengl_init_color(color *c, int r, int g, int b)
1698 c->screen_sig = gr_screen.signature;
1699 c->red = (unsigned char)r;
1700 c->green = (unsigned char)g;
1701 c->blue = (unsigned char)b;
1703 c->ac_type = AC_TYPE_NONE;
1705 c->is_alphacolor = 0;
1709 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1711 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1712 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1713 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1714 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1716 gr_opengl_init_color( clr, r, g, b );
1718 clr->alpha = (unsigned char)alpha;
1719 clr->ac_type = (ubyte)type;
1720 clr->alphacolor = -1;
1721 clr->is_alphacolor = 1;
1724 void gr_opengl_set_color( int r, int g, int b )
1726 Assert((r >= 0) && (r < 256));
1727 Assert((g >= 0) && (g < 256));
1728 Assert((b >= 0) && (b < 256));
1730 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1733 void gr_opengl_set_color_fast(color *dst)
1735 if ( dst->screen_sig != gr_screen.signature ) {
1736 if ( dst->is_alphacolor ) {
1737 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1739 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1742 gr_screen.current_color = *dst;
1745 void gr_opengl_print_screen(char *filename)
1750 int gr_opengl_supports_res_ingame(int res)
1757 int gr_opengl_supports_res_interface(int res)
1764 void opengl_tcache_cleanup ();
1765 void gr_opengl_cleanup()
1767 if ( !Inited ) return;
1774 opengl_tcache_cleanup ();
1779 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1781 Assert((r >= 0) && (r < 256));
1782 Assert((g >= 0) && (g < 256));
1783 Assert((b >= 0) && (b < 256));
1785 if (fog_mode == GR_FOGMODE_NONE) {
1786 if (gr_screen.current_fog_mode != fog_mode) {
1789 gr_screen.current_fog_mode = fog_mode;
1794 if (gr_screen.current_fog_mode != fog_mode) {
1797 if (D3D_fog_mode == 2) {
1798 glFogi(GL_FOG_MODE, GL_LINEAR);
1801 gr_screen.current_fog_mode = fog_mode;
1804 if ( (gr_screen.current_fog_color.red != r) ||
1805 (gr_screen.current_fog_color.green != g) ||
1806 (gr_screen.current_fog_color.blue != b) ) {
1809 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1811 fc[0] = (float)r/255.0;
1812 fc[1] = (float)g/255.0;
1813 fc[2] = (float)b/255.0;
1816 glFogfv(GL_FOG_COLOR, fc);
1819 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1820 ((fog_near != gr_screen.fog_near) ||
1821 (fog_far != gr_screen.fog_far)) ) {
1822 gr_screen.fog_near = fog_near;
1823 gr_screen.fog_far = fog_far;
1825 if (D3D_fog_mode == 2) {
1826 glFogf(GL_FOG_START, fog_near);
1827 glFogf(GL_FOG_END, fog_far);
1832 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1837 void gr_opengl_set_cull(int cull)
1840 glEnable (GL_CULL_FACE);
1841 glFrontFace (GL_CCW);
1843 glDisable (GL_CULL_FACE);
1847 void gr_opengl_filter_set(int filter)
1852 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1855 gr_set_bitmap(bmap1);
1858 gr_set_bitmap(bmap2);
1864 typedef struct tcache_slot_opengl {
1865 GLuint texture_handle;
1866 float u_scale, v_scale;
1869 char used_this_frame;
1874 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1875 tcache_slot_opengl *parent;
1876 } tcache_slot_opengl;
1878 static void *Texture_sections = NULL;
1879 tcache_slot_opengl *Textures = NULL;
1881 int GL_texture_sections = 0;
1882 int GL_texture_ram = 0;
1883 int GL_frame_count = 0;
1884 int GL_min_texture_width = 0;
1885 int GL_max_texture_width = 0;
1886 int GL_min_texture_height = 0;
1887 int GL_max_texture_height = 0;
1888 int GL_square_textures = 0;
1889 int GL_textures_in = 0;
1890 int GL_textures_in_frame = 0;
1891 int GL_last_bitmap_id = -1;
1892 int GL_last_detail = -1;
1893 int GL_last_bitmap_type = -1;
1894 int GL_last_section_x = -1;
1895 int GL_last_section_y = -1;
1899 void opengl_tcache_init (int use_sections)
1903 // DDOI - FIXME skipped a lot of stuff here
1904 GL_should_preload = 0;
1906 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1909 if ( tmp_pl == 0 ) {
1910 GL_should_preload = 0;
1911 } else if ( tmp_pl == 1 ) {
1912 GL_should_preload = 1;
1914 GL_should_preload = 1;
1919 GL_min_texture_width = 16;
1920 GL_min_texture_height = 16;
1921 GL_max_texture_width = 256;
1922 GL_max_texture_height = 256;
1924 GL_square_textures = 1;
1926 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1932 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1933 if(!Texture_sections){
1936 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1939 // Init the texture structures
1940 int section_count = 0;
1941 for( i=0; i<MAX_BITMAPS; i++ ) {
1943 Textures[i].vram_texture = NULL;
1944 Textures[i].vram_texture_surface = NULL;
1946 Textures[i].texture_handle = 0;
1948 Textures[i].bitmap_id = -1;
1949 Textures[i].size = 0;
1950 Textures[i].used_this_frame = 0;
1952 Textures[i].parent = NULL;
1954 // allocate sections
1956 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1957 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1958 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1959 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1961 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1962 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1964 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1965 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1966 Textures[i].data_sections[idx][s_idx]->size = 0;
1967 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1971 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1972 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1973 Textures[i].data_sections[idx][s_idx] = NULL;
1979 GL_texture_sections = use_sections;
1981 //GL_last_detail = Detail.hardware_textures;
1982 GL_last_bitmap_id = -1;
1983 GL_last_bitmap_type = -1;
1985 GL_last_section_x = -1;
1986 GL_last_section_y = -1;
1989 GL_textures_in_frame = 0;
1992 int opengl_free_texture (tcache_slot_opengl *t);
1994 void opengl_free_texture_with_handle(int handle)
1996 for(int i=0; i<MAX_BITMAPS; i++ ) {
1997 if (Textures[i].bitmap_id == handle) {
1998 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1999 opengl_free_texture ( &Textures[i] );
2004 void opengl_tcache_flush ()
2008 for( i=0; i<MAX_BITMAPS; i++ ) {
2009 opengl_free_texture ( &Textures[i] );
2011 if (GL_textures_in != 0) {
2012 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
2016 GL_last_bitmap_id = -1;
2017 GL_last_section_x = -1;
2018 GL_last_section_y = -1;
2021 void opengl_tcache_cleanup ()
2023 opengl_tcache_flush ();
2026 GL_textures_in_frame = 0;
2033 if( Texture_sections != NULL ){
2034 free(Texture_sections);
2035 Texture_sections = NULL;
2039 void opengl_tcache_frame ()
2043 GL_last_bitmap_id = -1;
2044 GL_textures_in_frame = 0;
2049 for( i=0; i<MAX_BITMAPS; i++ ) {
2050 Textures[i].used_this_frame = 0;
2053 if(Textures[i].data_sections[0][0] != NULL){
2054 Assert(GL_texture_sections);
2055 if(GL_texture_sections){
2056 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2057 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2058 if(Textures[i].data_sections[idx][s_idx] != NULL){
2059 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
2068 opengl_tcache_flush();
2073 int opengl_free_texture ( tcache_slot_opengl *t )
2079 if ( t->bitmap_id > -1 ) {
2080 // if I, or any of my children have been used this frame, bail
2081 if(t->used_this_frame){
2084 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2085 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2086 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
2092 // ok, now we know its legal to free everything safely
2093 glDeleteTextures (1, &t->texture_handle);
2094 t->texture_handle = 0;
2096 if ( GL_last_bitmap_id == t->bitmap_id ) {
2097 GL_last_bitmap_id = -1;
2100 // if this guy has children, free them too, since the children
2101 // actually make up his size
2102 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2103 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2104 if(t->data_sections[idx][s_idx] != NULL){
2105 opengl_free_texture(t->data_sections[idx][s_idx]);
2111 t->used_this_frame = 0;
2112 GL_textures_in -= t->size;
2118 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
2123 if((w_out == NULL) || (h_out == NULL)){
2133 for (i=0; i<16; i++ ) {
2134 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2140 for (i=0; i<16; i++ ) {
2141 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2148 if ( tex_w < GL_min_texture_width ) {
2149 tex_w = GL_min_texture_width;
2150 } else if ( tex_w > GL_max_texture_width ) {
2151 tex_w = GL_max_texture_width;
2154 if ( tex_h < GL_min_texture_height ) {
2155 tex_h = GL_min_texture_height;
2156 } else if ( tex_h > GL_max_texture_height ) {
2157 tex_h = GL_max_texture_height;
2160 if ( GL_square_textures ) {
2162 // Make the both be equal to larger of the two
2163 new_size = max(tex_w, tex_h);
2168 // store the outgoing size
2173 // data == start of bitmap data
2174 // sx == x offset into bitmap
2175 // sy == y offset into bitmap
2176 // src_w == absolute width of section on source bitmap
2177 // src_h == absolute height of section on source bitmap
2178 // bmap_w == width of source bitmap
2179 // bmap_h == height of source bitmap
2180 // tex_w == width of final texture
2181 // tex_h == height of final texture
2182 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)
2191 if ( t->used_this_frame ) {
2192 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2197 if(!opengl_free_texture(t)){
2202 // get final texture size
2203 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2205 if ( (tex_w < 1) || (tex_h < 1) ) {
2206 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2210 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2211 t->u_scale = (float)bmap_w / (float)tex_w;
2212 t->v_scale = (float)bmap_h / (float)tex_h;
2213 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2214 t->u_scale = (float)src_w / (float)tex_w;
2215 t->v_scale = (float)src_h / (float)tex_h;
2222 glGenTextures (1, &t->texture_handle);
2225 if (t->texture_handle == 0) {
2226 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2230 GL_current_texture_source = (gr_texture_source) -1;
2232 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2234 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2235 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2236 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2237 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2238 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2240 switch (bitmap_type) {
2242 case TCACHE_TYPE_AABITMAP:
2245 ubyte *bmp_data = ((ubyte*)data);
2251 for (i=0; i<16; i++) {
2252 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2255 for ( ; i<256; i++ ) {
2259 if (GL_use_luminance_alpha) {
2260 texmem = (ubyte *) malloc (tex_w*tex_h*2);
2263 for (i=0;i<tex_h;i++)
2265 for (j=0;j<tex_w;j++)
2267 if (i < bmap_h && j < bmap_w) {
2269 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2277 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2281 texmem = (ubyte *) malloc (tex_w*tex_h*4);
2284 for (i=0;i<tex_h;i++)
2286 for (j=0;j<tex_w;j++)
2288 if (i < bmap_h && j < bmap_w) {
2289 *texmemp++ = 0xff; /* R */
2290 *texmemp++ = 0xff; /* G */
2291 *texmemp++ = 0xff; /* B */
2292 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2302 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texmem);
2308 case TCACHE_TYPE_BITMAP_SECTION:
2311 ubyte *bmp_data = ((ubyte*)data);
2312 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2313 ubyte *texmemp = texmem;
2315 for (i=0;i<tex_h;i++)
2317 for (j=0;j<tex_w;j++)
2319 if (i < src_h && j < src_w) {
2320 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2321 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2328 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2329 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2337 ubyte *bmp_data = ((ubyte*)data);
2338 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2339 ubyte *texmemp = texmem;
2341 fix u, utmp, v, du, dv;
2345 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2346 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2348 for (j=0;j<tex_h;j++)
2351 for (i=0;i<tex_w;i++)
2353 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2354 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2360 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2361 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2369 t->bitmap_id = texture_handle;
2370 t->time_created = GL_frame_count;
2371 t->used_this_frame = 0;
2372 t->size = tex_w * tex_h * 2;
2373 t->w = (ushort)tex_w;
2374 t->h = (ushort)tex_h;
2375 GL_textures_in_frame += t->size;
2377 GL_textures_in += t->size;
2383 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2387 int final_w, final_h;
2391 // setup texture/bitmap flags
2393 switch(bitmap_type){
2394 case TCACHE_TYPE_AABITMAP:
2395 flags |= BMP_AABITMAP;
2398 case TCACHE_TYPE_NORMAL:
2399 flags |= BMP_TEX_OTHER;
2400 case TCACHE_TYPE_XPARENT:
2401 flags |= BMP_TEX_XPARENT;
2403 case TCACHE_TYPE_NONDARKENING:
2405 flags |= BMP_TEX_NONDARK;
2409 // lock the bitmap into the proper format
2410 bmp = bm_lock(bitmap_handle, bpp, flags);
2411 if ( bmp == NULL ) {
2412 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2421 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2422 // max_w /= D3D_texture_divider;
2423 // max_h /= D3D_texture_divider;
2425 // Detail.debris_culling goes from 0 to 4.
2426 max_w /= (16 >> Detail.hardware_textures);
2427 max_h /= (16 >> Detail.hardware_textures);
2431 // get final texture size as it will be allocated as a DD surface
2432 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2434 // if this tcache slot has no bitmap
2435 if ( tslot->bitmap_id < 0) {
2438 // different bitmap altogether - determine if the new one can use the old one's slot
2439 else if (tslot->bitmap_id != bitmap_handle) {
2440 if((final_w == tslot->w) && (final_h == tslot->h)){
2442 //ml_printf("Reloading texture %d\n", bitmap_handle);
2449 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);
2451 // unlock the bitmap
2452 bm_unlock(bitmap_handle);
2457 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2461 int final_w, final_h;
2462 int section_x, section_y;
2465 // setup texture/bitmap flags
2466 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2467 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2468 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2470 flags = BMP_TEX_XPARENT;
2472 // lock the bitmap in the proper format
2473 bmp = bm_lock(bitmap_handle, 16, flags);
2474 if ( bmp == NULL ) {
2475 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2478 // determine the width and height of this section
2479 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2481 // get final texture size as it will be allocated as an opengl texture
2482 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2484 // if this tcache slot has no bitmap
2485 if ( tslot->bitmap_id < 0) {
2488 // different bitmap altogether - determine if the new one can use the old one's slot
2489 else if (tslot->bitmap_id != bitmap_handle) {
2490 if((final_w == tslot->w) && (final_h == tslot->h)){
2498 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);
2500 // unlock the bitmap
2501 bm_unlock(bitmap_handle);
2507 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2508 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)
2517 GL_last_bitmap_id = -1;
2521 if ( GL_last_detail != Detail.hardware_textures ) {
2522 GL_last_detail = Detail.hardware_textures;
2523 opengl_tcache_flush();
2530 int n = bm_get_cache_slot (bitmap_id, 1);
2531 tcache_slot_opengl *t = &Textures[n];
2533 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)) {
2534 t->used_this_frame++;
2536 // mark all children as used
2537 if(GL_texture_sections){
2538 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2539 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2540 if(t->data_sections[idx][s_idx] != NULL){
2541 t->data_sections[idx][s_idx]->used_this_frame++;
2547 *u_scale = t->u_scale;
2548 *v_scale = t->v_scale;
2552 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2553 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2554 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2560 // if the texture sections haven't been created yet
2561 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2563 // lock the bitmap in the proper format
2564 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2565 bm_unlock(bitmap_id);
2567 // now lets do something for each texture
2569 for(idx=0; idx<bmp->sections.num_x; idx++){
2570 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2571 // hmm. i'd rather we didn't have to do it this way...
2572 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2576 // not used this frame
2577 t->data_sections[idx][s_idx]->used_this_frame = 0;
2581 // zero out pretty much everything in the parent struct since he's just the root
2582 t->bitmap_id = bitmap_id;
2583 t->texture_handle = 0;
2584 t->time_created = t->data_sections[sx][sy]->time_created;
2585 t->used_this_frame = 0;
2587 t->vram_texture = NULL;
2588 t->vram_texture_surface = NULL
2592 // argh. we failed to upload. free anything we can
2594 opengl_free_texture(t);
2596 // swap in the texture we want
2598 t = t->data_sections[sx][sy];
2601 // all other "normal" textures
2602 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2603 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2606 // everything went ok
2607 if(ret_val && (t->texture_handle) && !vram_full){
2608 *u_scale = t->u_scale;
2609 *v_scale = t->v_scale;
2611 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2613 GL_last_bitmap_id = t->bitmap_id;
2614 GL_last_bitmap_type = bitmap_type;
2615 GL_last_section_x = sx;
2616 GL_last_section_y = sy;
2618 t->used_this_frame++;
2622 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2629 void gr_opengl_set_clear_color(int r, int g, int b)
2631 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2634 void gr_opengl_flash(int r, int g, int b)
2640 if ( r || g || b ) {
2641 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2643 float x1, x2, y1, y2;
2644 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2645 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2646 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2647 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2649 glColor4ub(r, g, b, 255);
2651 glVertex3f (x1, y2, -0.99);
2653 glVertex3f (x2, y2, -0.99);
2655 glVertex3f (x2, y1, -0.99);
2657 glVertex3f (x1, y1, -0.99);
2662 int gr_opengl_zbuffer_get()
2664 if ( !gr_global_zbuffering ) {
2665 return GR_ZBUFF_NONE;
2667 return gr_zbuffering_mode;
2670 int gr_opengl_zbuffer_set(int mode)
2672 int tmp = gr_zbuffering_mode;
2674 gr_zbuffering_mode = mode;
2676 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2684 void gr_opengl_zbuffer_clear(int mode)
2688 gr_zbuffering_mode = GR_ZBUFF_FULL;
2689 gr_global_zbuffering = 1;
2691 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2692 glClear ( GL_DEPTH_BUFFER_BIT );
2695 gr_zbuffering_mode = GR_ZBUFF_NONE;
2696 gr_global_zbuffering = 0;
2700 void gr_opengl_set_gamma(float gamma)
2703 Gr_gamma_int = int (Gr_gamma*10);
2705 // Create the Gamma lookup table
2707 for (i=0;i<256; i++) {
2708 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2711 } else if ( v < 0 ) {
2714 Gr_gamma_lookup[i] = v;
2717 // Flush any existing textures
2718 opengl_tcache_flush();
2721 void gr_opengl_fade_in(int instantaneous)
2726 void gr_opengl_fade_out(int instantaneous)
2731 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2734 glReadBuffer(GL_FRONT);
2736 glReadBuffer(GL_BACK);
2739 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2741 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2743 if (gr_screen.bits_per_pixel == 15) {
2744 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2745 } else if (gr_screen.bits_per_pixel == 32) {
2746 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2749 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2752 static int Gr_opengl_mouse_saved = 0;
2753 static int Gr_opengl_mouse_saved_x1 = 0;
2754 static int Gr_opengl_mouse_saved_y1 = 0;
2755 static int Gr_opengl_mouse_saved_x2 = 0;
2756 static int Gr_opengl_mouse_saved_y2 = 0;
2757 static int Gr_opengl_mouse_saved_w = 0;
2758 static int Gr_opengl_mouse_saved_h = 0;
2759 #define MAX_SAVE_SIZE (32*32)
2760 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2762 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2764 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2766 Gr_opengl_mouse_saved_x1 = x;
2767 Gr_opengl_mouse_saved_y1 = y;
2768 Gr_opengl_mouse_saved_x2 = x+w-1;
2769 Gr_opengl_mouse_saved_y2 = y+h-1;
2771 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2772 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2773 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2774 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2776 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2777 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2779 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2780 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2782 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2784 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2786 glReadBuffer(GL_BACK);
2787 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);
2789 Gr_opengl_mouse_saved = 1;
2792 int gr_opengl_save_screen()
2796 if ( Gr_saved_screen ) {
2797 mprintf(( "Screen alread saved!\n" ));
2801 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2802 if (!Gr_saved_screen) {
2803 mprintf(( "Couldn't get memory for saved screen!\n" ));
2807 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2808 if (!Gr_saved_screen_tmp) {
2809 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2813 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2815 glReadBuffer(GL_FRONT);
2816 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);
2820 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2821 dptr = (ubyte *)Gr_saved_screen;
2822 for (int j = 0; j < gr_screen.max_h; j++) {
2823 sptr -= gr_screen.max_w*2;
2824 memcpy(dptr, sptr, gr_screen.max_w*2);
2825 dptr += gr_screen.max_w*2;
2828 free(Gr_saved_screen_tmp);
2830 if (Gr_opengl_mouse_saved) {
2831 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2832 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2833 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2834 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2837 dptr -= gr_screen.max_w*2;
2841 // this leaks texture handles, and the opengl doesn't currently
2842 // perform some sort of garbage collection, so a hack was added
2843 // to bmpman to make it free textures when released
2844 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2846 return Gr_saved_screen_bitmap;
2849 void gr_opengl_restore_screen(int id)
2853 if ( !Gr_saved_screen ) {
2858 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2860 gr_set_bitmap(Gr_saved_screen_bitmap);
2864 void gr_opengl_free_screen(int id)
2866 bm_release(Gr_saved_screen_bitmap);
2868 if ( Gr_saved_screen ) {
2869 free( Gr_saved_screen );
2870 Gr_saved_screen = NULL;
2874 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2879 void gr_opengl_dump_frame_stop()
2884 void gr_opengl_dump_frame()
2889 uint gr_opengl_lock()
2896 void gr_opengl_unlock()
2900 void opengl_zbias(int bias)
2903 glEnable(GL_POLYGON_OFFSET_FILL);
2904 glPolygonOffset(0.0, -bias);
2906 glDisable(GL_POLYGON_OFFSET_FILL);
2910 extern char *Osreg_title;
2911 void gr_opengl_init()
2914 gr_opengl_cleanup();
2918 mprintf(( "Initializing opengl graphics device...\n" ));
2922 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2924 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2930 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2931 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2932 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2933 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2934 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2936 int flags = SDL_OPENGL;
2938 if (os_config_read_uint( NULL, "Fullscreen", 0 ) == 1)
2939 flags |= SDL_FULLSCREEN;
2941 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2943 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2948 SDL_WM_SetCaption (Osreg_title, "FS2");
2950 /* might as well put this here */
2951 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2953 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2955 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2957 glMatrixMode(GL_PROJECTION);
2959 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2960 glMatrixMode(GL_MODELVIEW);
2963 glShadeModel(GL_SMOOTH);
2964 glEnable(GL_DITHER);
2965 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2966 glHint(GL_FOG_HINT, GL_NICEST);
2968 glEnable(GL_DEPTH_TEST);
2971 glEnable(GL_TEXTURE_2D);
2973 glDepthRange(0.0, 1.0);
2975 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2976 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2978 D3D_32bit = 1; // grd3d.cpp
2979 extern int D3D_enabled;
2982 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2983 1 = use secondary color ext
2984 2 = use opengl linear fog
2986 D3D_fog_mode = 2; // grd3d.cpp
2990 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2997 gr_screen.bits_per_pixel = 15;
2998 gr_screen.bytes_per_pixel = 2;
3003 Gr_red.mask = 0x7C00;
3008 Gr_green.mask = 0x3E0;
3013 Gr_blue.mask = 0x1F;
3018 gr_screen.bits_per_pixel = 16;
3019 gr_screen.bytes_per_pixel = 2;
3024 Gr_red.mask = 0xF800;
3029 Gr_green.mask = 0x7E0;
3034 Gr_blue.mask = 0x1F;
3039 gr_screen.bits_per_pixel = 32;
3040 gr_screen.bytes_per_pixel = 4;
3045 Gr_red.mask = 0xff0000;
3050 Gr_green.mask = 0xff00;
3055 Gr_blue.mask = 0xff;
3060 Int3(); // Illegal bpp
3063 // DDOI - set these so no one else does!
3064 Gr_ta_red.mask = 0x0f00;
3065 Gr_ta_red.shift = 8;
3066 Gr_ta_red.scale = 16;
3068 Gr_ta_green.mask = 0x00f0;
3069 Gr_ta_green.shift = 4;
3070 Gr_ta_green.scale = 16;
3072 Gr_ta_blue.mask = 0x000f;
3073 Gr_ta_blue.shift = 0;
3074 Gr_ta_blue.scale = 16;
3076 Gr_ta_alpha.mask = 0xf000;
3077 Gr_ta_alpha.shift = 12;
3078 Gr_ta_alpha.scale = 16;
3081 opengl_tcache_init (1);
3084 Gr_current_red = &Gr_red;
3085 Gr_current_blue = &Gr_blue;
3086 Gr_current_green = &Gr_green;
3087 Gr_current_alpha = &Gr_alpha;
3089 gr_screen.gf_flip = gr_opengl_flip;
3090 gr_screen.gf_flip_window = gr_opengl_flip_window;
3091 gr_screen.gf_set_clip = gr_opengl_set_clip;
3092 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3093 gr_screen.gf_set_font = grx_set_font;
3095 gr_screen.gf_set_color = gr_opengl_set_color;
3096 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3097 gr_screen.gf_create_shader = gr_opengl_create_shader;
3098 gr_screen.gf_set_shader = gr_opengl_set_shader;
3099 gr_screen.gf_clear = gr_opengl_clear;
3100 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3101 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3102 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3103 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3105 gr_screen.gf_rect = gr_opengl_rect;
3106 gr_screen.gf_shade = gr_opengl_shade;
3107 gr_screen.gf_string = gr_opengl_string;
3108 gr_screen.gf_circle = gr_opengl_circle;
3110 gr_screen.gf_line = gr_opengl_line;
3111 gr_screen.gf_aaline = gr_opengl_aaline;
3112 gr_screen.gf_pixel = gr_opengl_pixel;
3113 gr_screen.gf_scaler = gr_opengl_scaler;
3114 gr_screen.gf_tmapper = gr_opengl_tmapper;
3116 gr_screen.gf_gradient = gr_opengl_gradient;
3118 gr_screen.gf_set_palette = gr_opengl_set_palette;
3119 gr_screen.gf_get_color = gr_opengl_get_color;
3120 gr_screen.gf_init_color = gr_opengl_init_color;
3121 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3122 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3123 gr_screen.gf_print_screen = gr_opengl_print_screen;
3125 gr_screen.gf_fade_in = gr_opengl_fade_in;
3126 gr_screen.gf_fade_out = gr_opengl_fade_out;
3127 gr_screen.gf_flash = gr_opengl_flash;
3129 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3130 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3131 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3133 gr_screen.gf_save_screen = gr_opengl_save_screen;
3134 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3135 gr_screen.gf_free_screen = gr_opengl_free_screen;
3137 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3138 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3139 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3141 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3143 gr_screen.gf_lock = gr_opengl_lock;
3144 gr_screen.gf_unlock = gr_opengl_unlock;
3146 gr_screen.gf_fog_set = gr_opengl_fog_set;
3148 gr_screen.gf_get_region = gr_opengl_get_region;
3150 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3152 gr_screen.gf_set_cull = gr_opengl_set_cull;
3154 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3156 gr_screen.gf_filter_set = gr_opengl_filter_set;
3158 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3160 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;