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.51 2002/06/19 04:52:45 relnev
19 * MacOS X updates (Ryan)
21 * Revision 1.50 2002/06/18 23:20:44 relnev
24 * adjusted line offsets.
26 * Revision 1.49 2002/06/17 23:11:39 relnev
27 * enable sdl key repeating.
31 * Revision 1.48 2002/06/09 04:41:17 relnev
32 * added copyright header
34 * Revision 1.47 2002/06/09 03:16:04 relnev
37 * removed unneeded asm, old sdl 2d setup.
39 * fixed crash caused by opengl_get_region.
41 * Revision 1.46 2002/06/05 04:03:32 relnev
42 * finished cfilesystem.
44 * removed some old code.
46 * fixed mouse save off-by-one.
50 * Revision 1.45 2002/06/03 09:25:37 relnev
51 * implement mouse cursor and screen save/restore
53 * Revision 1.44 2002/06/02 18:46:59 relnev
56 * Revision 1.43 2002/06/02 11:34:00 relnev
59 * Revision 1.42 2002/06/02 10:28:17 relnev
60 * fix texture handle leak
62 * Revision 1.41 2002/06/01 09:00:34 relnev
63 * silly debug memmanager
65 * Revision 1.40 2002/06/01 07:12:33 relnev
66 * a few NDEBUG updates.
68 * removed a few warnings.
70 * Revision 1.39 2002/06/01 05:33:15 relnev
71 * copied more code over.
73 * added scissor clipping.
75 * Revision 1.38 2002/06/01 03:35:27 relnev
78 * Revision 1.37 2002/06/01 03:32:00 relnev
79 * fix texture loading mistake.
81 * enable some d3d stuff for opengl also
83 * Revision 1.36 2002/05/31 23:25:03 relnev
86 * Revision 1.34 2002/05/31 22:15:22 relnev
89 * Revision 1.33 2002/05/31 22:04:55 relnev
90 * use d3d rect_internal
92 * Revision 1.32 2002/05/31 06:28:23 relnev
95 * Revision 1.31 2002/05/31 06:04:39 relnev
98 * Revision 1.30 2002/05/31 03:56:11 theoddone33
99 * Change tmapper polygon winding and enable culling
101 * Revision 1.29 2002/05/31 03:34:02 theoddone33
105 * Revision 1.28 2002/05/31 00:06:59 relnev
108 * Revision 1.27 2002/05/30 23:46:29 theoddone33
109 * some minor key changes (not necessarily fixes)
111 * Revision 1.26 2002/05/30 23:33:12 relnev
112 * implemented a few more functions.
114 * Revision 1.25 2002/05/30 23:01:16 relnev
115 * implement gr_opengl_set_state.
117 * Revision 1.24 2002/05/30 22:12:57 relnev
118 * finish default texture case
120 * Revision 1.23 2002/05/30 22:02:30 theoddone33
123 * Revision 1.22 2002/05/30 21:44:48 relnev
124 * implemented some missing texture stuff.
126 * enable bitmap polys for opengl.
128 * work around greenness in bitmaps.
130 * Revision 1.21 2002/05/30 17:29:30 theoddone33
131 * Fix some more stubs, change at least one polygon winding since culling is now
134 * Revision 1.20 2002/05/30 16:50:24 theoddone33
135 * Keyboard partially fixed
137 * Revision 1.19 2002/05/30 08:13:14 relnev
140 * Revision 1.18 2002/05/29 23:37:36 relnev
143 * Revision 1.17 2002/05/29 23:17:49 theoddone33
144 * Non working text code and fixed keys
146 * Revision 1.16 2002/05/29 19:45:13 theoddone33
147 * More changes on texture loading
149 * Revision 1.15 2002/05/29 19:06:48 theoddone33
150 * Enable string printing. Enable texture mapping
152 * Revision 1.14 2002/05/29 08:54:40 relnev
153 * "fixed" bitmap drawing.
155 * copied more d3d code over.
157 * Revision 1.13 2002/05/29 06:25:13 theoddone33
158 * Keyboard input, mouse tracking now work
160 * Revision 1.12 2002/05/29 04:52:45 relnev
163 * Revision 1.11 2002/05/29 04:29:56 relnev
164 * removed some unncessary stubbing, implemented opengl rect
166 * Revision 1.10 2002/05/29 04:13:27 theoddone33
169 * Revision 1.9 2002/05/29 03:35:51 relnev
172 * Revision 1.8 2002/05/29 03:30:05 relnev
173 * update opengl stubs
175 * Revision 1.7 2002/05/29 02:52:32 theoddone33
176 * Enable OpenGL renderer
178 * Revision 1.6 2002/05/28 04:56:51 theoddone33
179 * runs a little bit now
181 * Revision 1.5 2002/05/28 04:07:28 theoddone33
182 * New graphics stubbing arrangement
184 * Revision 1.4 2002/05/27 23:39:34 relnev
187 * Revision 1.3 2002/05/27 22:35:01 theoddone33
190 * Revision 1.2 2002/05/27 22:32:02 theoddone33
191 * throw all d3d stuff at opengl
193 * Revision 1.1.1.1 2002/05/03 03:28:09 root
197 * 10 7/14/99 9:42a Dave
198 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
201 * 9 7/09/99 9:51a Dave
202 * Added thick polyline code.
204 * 8 6/29/99 10:35a Dave
205 * Interface polygon bitmaps! Whee!
207 * 7 2/03/99 11:44a Dave
208 * Fixed d3d transparent textures.
210 * 6 1/24/99 11:37p Dave
211 * First full rev of beam weapons. Very customizable. Removed some bogus
212 * Int3()'s in low level net code.
214 * 5 12/18/98 1:13a Dave
215 * Rough 1024x768 support for Direct3D. Proper detection and usage through
218 * 4 12/06/98 2:36p Dave
219 * Drastically improved nebula fogging.
221 * 3 11/11/98 5:37p Dave
222 * Checkin for multiplayer testing.
224 * 2 10/07/98 10:53a Dave
227 * 1 10/07/98 10:49a Dave
229 * 14 5/20/98 9:46p John
230 * added code so the places in code that change half the palette don't
231 * have to clear the screen.
233 * 13 5/06/98 5:30p John
234 * Removed unused cfilearchiver. Removed/replaced some unused/little used
235 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
236 * DirectX header files and libs that fixed the Direct3D alpha blending
239 * 12 4/14/98 12:15p John
240 * Made 16-bpp movies work.
242 * 11 3/12/98 5:36p John
243 * Took out any unused shaders. Made shader code take rgbc instead of
244 * matrix and vector since noone used it like a matrix and it would have
245 * been impossible to do in hardware. Made Glide implement a basic
246 * shader for online help.
248 * 10 3/10/98 4:18p John
249 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
250 * & Glide have popups and print screen. Took out all >8bpp software
251 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
252 * support Fred. Made zbuffering key off of functions rather than one
255 * 9 12/02/97 4:00p John
256 * Added first rev of thruster glow, along with variable levels of
257 * translucency, which retquired some restructing of palman.
259 * 8 10/03/97 9:10a John
260 * added better antialiased line drawer
262 * 7 9/23/97 10:45a John
263 * made so you can tell bitblt code to rle a bitmap by passing flag to
266 * 6 9/09/97 11:01a Sandeep
267 * fixed warning level 4 bugs
269 * 5 7/10/97 2:06p John
270 * added code to specify alphablending type for bitmaps.
272 * 4 6/17/97 7:04p John
273 * added d3d support for gradients.
274 * fixed some color bugs by adding screen signatures instead of watching
275 * flags and palette changes.
277 * 3 6/12/97 2:50a Lawrance
278 * bm_unlock() now passed bitmap number, not pointer
280 * 2 6/11/97 1:12p John
281 * Started fixing all the text colors in the game.
283 * 1 5/12/97 12:14p John
290 #include <windowsx.h>
304 #include "floating.h"
306 #include "systemvars.h"
307 #include "grinternal.h"
308 #include "gropengl.h"
312 #include "osregistry.h"
314 static int Inited = 0;
316 typedef enum gr_texture_source {
318 TEXTURE_SOURCE_DECAL,
319 TEXTURE_SOURCE_NO_FILTERING,
322 typedef enum gr_alpha_blend {
323 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
324 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
325 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
326 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
329 typedef enum gr_zbuffer_type {
336 #define NEBULA_COLORS 20
338 volatile int GL_activate = 0;
339 volatile int GL_deactivate = 0;
341 static char *Gr_saved_screen = NULL;
342 static int Gr_saved_screen_bitmap;
345 // Throw in some dummy functions - DDOI
347 int D3D_32bit = 0; // grd3d.cpp
348 int D3D_fog_mode = -1; // grd3d.cpp
349 int D3D_inited = 0; // grd3d.cpp
350 int D3D_zbias = 1; // grd3d.cpp
351 int D3d_rendition_uvs = 0; // grd3d.cpp
358 void d3d_zbias (int a)
365 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
366 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
367 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
369 void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
371 /* TODO: this doesn't work unless it's handled at the per-texture level */
372 /* if (ts != GL_current_texture_source) */ {
374 case TEXTURE_SOURCE_NONE:
375 glBindTexture(GL_TEXTURE_2D, 0);
376 gr_tcache_set(-1, -1, NULL, NULL );
378 case TEXTURE_SOURCE_DECAL:
379 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
380 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
381 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
383 case TEXTURE_SOURCE_NO_FILTERING:
384 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
385 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
386 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
392 GL_current_texture_source = ts;
395 if (ab != GL_current_alpha_blend) {
397 case ALPHA_BLEND_NONE:
398 glBlendFunc(GL_ONE, GL_ZERO);
400 case ALPHA_BLEND_ALPHA_ADDITIVE:
401 glBlendFunc(GL_ONE, GL_ONE);
403 case ALPHA_BLEND_ALPHA_BLEND_ALPHA:
404 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
406 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR:
407 glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
413 GL_current_alpha_blend = ab;
416 if (zt != GL_current_zbuffer_type) {
418 case ZBUFFER_TYPE_NONE:
419 glDepthFunc(GL_ALWAYS);
420 glDepthMask(GL_FALSE);
422 case ZBUFFER_TYPE_READ:
423 glDepthFunc(GL_LESS);
424 glDepthMask(GL_FALSE);
426 case ZBUFFER_TYPE_WRITE:
427 glDepthFunc(GL_ALWAYS);
428 glDepthMask(GL_TRUE);
430 case ZBUFFER_TYPE_FULL:
431 glDepthFunc(GL_LESS);
432 glDepthMask(GL_TRUE);
438 GL_current_zbuffer_type = zt;
442 void gr_opengl_activate(int active)
448 make sure window is active and mouse grabbed
454 make sure mouse is not grabbed and window minimized
462 void opengl_tcache_flush ();
464 void gr_opengl_preload_init()
466 if (gr_screen.mode != GR_OPENGL) {
470 opengl_tcache_flush ();
473 int GL_should_preload = 0;
474 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
476 if ( gr_screen.mode != GR_OPENGL) {
480 if ( !GL_should_preload ) {
484 float u_scale, v_scale;
488 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
490 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
494 mprintf(("Texture upload failed!\n" ));
500 void gr_opengl_pixel(int x, int y)
505 void gr_opengl_clear()
507 glClearColor(gr_screen.current_clear_color.red / 255.0,
508 gr_screen.current_clear_color.green / 255.0,
509 gr_screen.current_clear_color.blue / 255.0, 1.0);
511 glClear ( GL_COLOR_BUFFER_BIT );
514 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
515 void opengl_tcache_frame ();
516 void gr_opengl_flip()
524 extern int Gr_opengl_mouse_saved;
525 Gr_opengl_mouse_saved = 0;
527 if ( mouse_is_visible() ) {
531 mouse_get_pos( &mx, &my );
533 gr_opengl_save_mouse_area(mx,my,32,32);
535 if ( Gr_cursor == -1 ) {
538 gr_set_bitmap(Gr_cursor);
544 GLenum error = glGetError();
547 error = glGetError();
549 if (error != GL_NO_ERROR) {
550 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
553 } while (error != GL_NO_ERROR);
556 SDL_GL_SwapBuffers ();
558 opengl_tcache_frame ();
560 int cnt = GL_activate;
563 opengl_tcache_flush();
564 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
570 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
574 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
579 void gr_opengl_set_clip(int x,int y,int w,int h)
581 // check for sanity of parameters
587 if (x >= gr_screen.max_w)
588 x = gr_screen.max_w - 1;
589 if (y >= gr_screen.max_h)
590 y = gr_screen.max_h - 1;
592 if (x + w > gr_screen.max_w)
593 w = gr_screen.max_w - x;
594 if (y + h > gr_screen.max_h)
595 h = gr_screen.max_h - y;
597 if (w > gr_screen.max_w)
599 if (h > gr_screen.max_h)
602 gr_screen.offset_x = x;
603 gr_screen.offset_y = y;
604 gr_screen.clip_left = 0;
605 gr_screen.clip_right = w-1;
606 gr_screen.clip_top = 0;
607 gr_screen.clip_bottom = h-1;
608 gr_screen.clip_width = w;
609 gr_screen.clip_height = h;
611 glEnable(GL_SCISSOR_TEST);
612 glScissor(x, gr_screen.max_h-y-h, w, h);
615 void gr_opengl_reset_clip()
617 gr_screen.offset_x = 0;
618 gr_screen.offset_y = 0;
619 gr_screen.clip_left = 0;
620 gr_screen.clip_top = 0;
621 gr_screen.clip_right = gr_screen.max_w - 1;
622 gr_screen.clip_bottom = gr_screen.max_h - 1;
623 gr_screen.clip_width = gr_screen.max_w;
624 gr_screen.clip_height = gr_screen.max_h;
626 glDisable(GL_SCISSOR_TEST);
627 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
630 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
632 gr_screen.current_alpha = alpha;
633 gr_screen.current_alphablend_mode = alphablend_mode;
634 gr_screen.current_bitblt_mode = bitblt_mode;
635 gr_screen.current_bitmap = bitmap_num;
637 gr_screen.current_bitmap_sx = sx;
638 gr_screen.current_bitmap_sy = sy;
641 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
643 shade->screen_sig = gr_screen.signature;
650 void gr_opengl_set_shader( shader * shade )
653 if (shade->screen_sig != gr_screen.signature) {
654 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
656 gr_screen.current_shader = *shade;
658 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
663 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
665 STUB_FUNCTION; /* who called me? */
668 extern int GL_last_bitmap_id;
669 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
673 GLubyte *sptr, *dptr;
677 int cw = min(bmp->w, w);
678 int ch = min(bmp->h, h);
680 GL_last_bitmap_id = -1; /* HACK! */
682 glColor4f(1.0, 1.0, 1.0, 1.0);
683 glBindTexture(GL_TEXTURE_2D, bitmapTex);
686 for (iy = sy; iy < ch; iy += 256) {
688 ih = min(256, (ch-iy));
690 for (ix = sx; ix < cw; ix += 256) {
692 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
694 iw = min(256, (cw-ix));
700 memcpy(dptr, sptr, iw*2);
708 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
718 t = (float)ih / 256.0;
723 s = (float)iw / 256.0;
724 t = (float)ih / 256.0;
729 s = (float)iw / 256.0;
741 bm_unlock(gr_screen.current_bitmap);
746 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
748 STUB_FUNCTION; /* who called me? */
755 int dx1=x, dx2=x+w-1;
756 int dy1=y, dy2=y+h-1;
759 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
764 if ( count > 1 ) Int3();
768 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
769 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
770 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
771 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
772 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
773 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
800 if ( w < 1 ) return; // clipped away!
801 if ( h < 1 ) return; // clipped away!
805 // Make sure clipping algorithm works
809 Assert( w == (dx2-dx1+1) );
810 Assert( h == (dy2-dy1+1) );
813 Assert( sx+w <= bw );
814 Assert( sy+h <= bh );
815 Assert( dx2 >= dx1 );
816 Assert( dy2 >= dy1 );
817 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
818 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
819 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
820 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
823 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
824 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
826 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
830 void gr_opengl_bitmap(int x, int y)
832 STUB_FUNCTION; /* who called me? */
836 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
837 int dx1=x, dx2=x+w-1;
838 int dy1=y, dy2=y+h-1;
841 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
842 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
843 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
844 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
845 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
846 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
848 if ( sx < 0 ) return;
849 if ( sy < 0 ) return;
850 if ( sx >= w ) return;
851 if ( sy >= h ) return;
853 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
855 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
859 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
863 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
865 saved_zbuf = gr_zbuffer_get();
867 // start the frame, no zbuffering, no culling
869 gr_zbuffer_set(GR_ZBUFF_NONE);
878 v[0].flags = PF_PROJECTED;
885 v[1].sx = i2fl(x + w);
890 v[1].flags = PF_PROJECTED;
897 v[2].sx = i2fl(x + w);
898 v[2].sy = i2fl(y + h);
902 v[2].flags = PF_PROJECTED;
910 v[3].sy = i2fl(y + h);
914 v[3].flags = PF_PROJECTED;
922 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
926 // restore zbuffer and culling
927 gr_zbuffer_set(saved_zbuf);
931 void gr_opengl_rect(int x,int y,int w,int h)
933 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);
936 void gr_opengl_shade(int x,int y,int w,int h)
943 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
944 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
945 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
946 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
947 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
948 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
949 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
950 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
952 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
955 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
960 if ( !gr_screen.current_color.is_alphacolor ) return;
962 float u_scale, v_scale;
964 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
966 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
967 // Couldn't set texture
968 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
972 float u0, u1, v0, v1;
973 float x1, x2, y1, y2;
976 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
978 u0 = u_scale*i2fl(sx)/i2fl(bw);
979 v0 = v_scale*i2fl(sy)/i2fl(bh);
981 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
982 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
984 x1 = i2fl(x+gr_screen.offset_x);
985 y1 = i2fl(y+gr_screen.offset_y);
986 x2 = i2fl(x+w+gr_screen.offset_x);
987 y2 = i2fl(y+h+gr_screen.offset_y);
989 if ( gr_screen.current_color.is_alphacolor ) {
990 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
992 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
996 glTexCoord2f (u0, v1);
997 glVertex3f (x1, y2, -0.99);
999 glTexCoord2f (u1, v1);
1000 glVertex3f (x2, y2, -0.99);
1002 glTexCoord2f (u1, v0);
1003 glVertex3f (x2, y1, -0.99);
1005 glTexCoord2f (u0, v0);
1006 glVertex3f (x1, y1, -0.99);
1010 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
1017 int dx1=x, dx2=x+w-1;
1018 int dy1=y, dy2=y+h-1;
1021 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
1026 if ( count > 1 ) Int3();
1030 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1031 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1032 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1033 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1034 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1035 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1052 if ( sx + w > bw ) {
1057 if ( sy + h > bh ) {
1062 if ( w < 1 ) return; // clipped away!
1063 if ( h < 1 ) return; // clipped away!
1067 // Make sure clipping algorithm works
1071 Assert( w == (dx2-dx1+1) );
1072 Assert( h == (dy2-dy1+1) );
1075 Assert( sx+w <= bw );
1076 Assert( sy+h <= bh );
1077 Assert( dx2 >= dx1 );
1078 Assert( dy2 >= dy1 );
1079 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
1080 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
1081 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
1082 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
1085 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
1086 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1089 void gr_opengl_aabitmap(int x, int y)
1093 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
1094 int dx1=x, dx2=x+w-1;
1095 int dy1=y, dy2=y+h-1;
1098 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1099 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1100 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1101 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1102 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1103 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1105 if ( sx < 0 ) return;
1106 if ( sy < 0 ) return;
1107 if ( sx >= w ) return;
1108 if ( sy >= h ) return;
1110 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
1111 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1114 void gr_opengl_string( int sx, int sy, char *s )
1116 int width, spacing, letter;
1119 if ( !Current_font ) {
1123 gr_set_bitmap(Current_font->bitmap_id);
1128 if (sx==0x8000) { //centered
1129 x = get_centered_x(s);
1139 while (*s== '\n' ) {
1141 y += Current_font->h;
1142 if (sx==0x8000) { //centered
1143 x = get_centered_x(s);
1148 if (*s == 0 ) break;
1150 letter = get_char_width(s[0],s[1],&width,&spacing);
1153 //not in font, draw as space
1161 // Check if this character is totally clipped
1162 if ( x + width < gr_screen.clip_left ) continue;
1163 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1164 if ( x > gr_screen.clip_right ) continue;
1165 if ( y > gr_screen.clip_bottom ) continue;
1168 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1169 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1173 wc = width - xd; hc = Current_font->h - yd;
1174 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1175 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1177 if ( wc < 1 ) continue;
1178 if ( hc < 1 ) continue;
1180 int u = Current_font->bm_u[letter];
1181 int v = Current_font->bm_v[letter];
1183 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1187 void gr_opengl_line(int x1,int y1,int x2,int y2)
1189 int clipped = 0, swapped=0;
1191 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1193 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);
1198 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1199 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1200 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1201 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1203 if ( x1 == x2 && y1 == y2 ) {
1204 glBegin (GL_POINTS);
1205 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1206 glVertex3f (sx1, sy1, -0.99f);
1218 } else if ( y1 == y2 ) {
1227 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1228 glVertex3f (sx2, sy2, -0.99f);
1229 glVertex3f (sx1, sy1, -0.99f);
1233 void gr_opengl_aaline(vertex *v1, vertex *v2)
1235 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1238 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1240 int clipped = 0, swapped=0;
1242 if ( !gr_screen.current_color.is_alphacolor ) {
1243 gr_line( x1, y1, x2, y2 );
1247 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);
1249 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1251 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1252 int ba = swapped ? gr_screen.current_color.alpha : 0;
1257 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1258 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1259 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1260 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1268 } else if ( y1 == y2 ) {
1277 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1278 glVertex3f (sx2, sy2, -0.99f);
1279 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1280 glVertex3f (sx1, sy1, -0.99f);
1284 void gr_opengl_circle( int xc, int yc, int d )
1294 if ( (xc+r) < gr_screen.clip_left ) return;
1295 if ( (xc-r) > gr_screen.clip_right ) return;
1296 if ( (yc+r) < gr_screen.clip_top ) return;
1297 if ( (yc-r) > gr_screen.clip_bottom ) return;
1300 // Draw the first octant
1301 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1302 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1307 // Draw the second octant
1308 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1309 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1317 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1318 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1323 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1327 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1328 if (f_float < 0.0f) {
1336 *a = (int)(f_float * 255.0);
1339 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1342 float u_scale = 1.0f, v_scale = 1.0f;
1344 // Make nebula use the texture mapper... this blends the colors better.
1345 if ( flags & TMAP_FLAG_NEBULA ){
1349 gr_texture_source texture_source = (gr_texture_source)-1;
1350 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1351 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1353 if ( gr_zbuffering ) {
1354 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1355 zbuffer_type = ZBUFFER_TYPE_READ;
1357 zbuffer_type = ZBUFFER_TYPE_FULL;
1360 zbuffer_type = ZBUFFER_TYPE_NONE;
1365 int tmap_type = TCACHE_TYPE_NORMAL;
1369 if ( flags & TMAP_FLAG_TEXTURED ) {
1372 r = gr_screen.current_color.red;
1373 g = gr_screen.current_color.green;
1374 b = gr_screen.current_color.blue;
1377 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1380 tmap_type = TCACHE_TYPE_NORMAL;
1381 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1383 // Blend with screen pixel using src*alpha+dst
1384 float factor = gr_screen.current_alpha;
1388 if ( factor <= 1.0f ) {
1389 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1390 r = (r*tmp_alpha)/255;
1391 g = (g*tmp_alpha)/255;
1392 b = (b*tmp_alpha)/255;
1395 tmap_type = TCACHE_TYPE_XPARENT;
1397 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1399 // Blend with screen pixel using src*alpha+dst
1400 float factor = gr_screen.current_alpha;
1402 if ( factor > 1.0f ) {
1405 alpha = fl2i(gr_screen.current_alpha*255.0f);
1409 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1410 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1412 alpha_blend = ALPHA_BLEND_NONE;
1417 if(flags & TMAP_FLAG_BITMAP_SECTION){
1418 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1421 texture_source = TEXTURE_SOURCE_NONE;
1423 if ( flags & TMAP_FLAG_TEXTURED ) {
1424 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 ))
1426 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1430 // use nonfiltered textures for bitmap sections
1431 if(flags & TMAP_FLAG_BITMAP_SECTION){
1432 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1434 texture_source = TEXTURE_SOURCE_DECAL;
1439 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1441 if ( flags & TMAP_FLAG_TEXTURED )
1447 if (flags & TMAP_FLAG_PIXEL_FOG) {
1453 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1455 vertex * va = verts[i];
1459 x = fl2i(va->sx*16.0f);
1460 y = fl2i(va->sy*16.0f);
1462 x += gr_screen.offset_x*16;
1463 y += gr_screen.offset_y*16;
1465 sx = i2fl(x) / 16.0f;
1466 sy = i2fl(y) / 16.0f;
1468 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1479 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1482 glBegin(GL_TRIANGLE_FAN);
1483 for (i = nv-1; i >= 0; i--) {
1484 vertex * va = verts[i];
1490 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1491 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1500 if ( flags & TMAP_FLAG_CORRECT ) {
1506 if (flags & TMAP_FLAG_ALPHA) {
1512 if (flags & TMAP_FLAG_NEBULA ) {
1513 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1514 r = gr_palette[pal*3+0];
1515 g = gr_palette[pal*3+1];
1516 b = gr_palette[pal*3+2];
1517 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1518 r = Gr_gamma_lookup[verts[i]->b];
1519 g = Gr_gamma_lookup[verts[i]->b];
1520 b = Gr_gamma_lookup[verts[i]->b];
1521 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1522 // Make 0.75 be 256.0f
1523 r = Gr_gamma_lookup[verts[i]->r];
1524 g = Gr_gamma_lookup[verts[i]->g];
1525 b = Gr_gamma_lookup[verts[i]->b];
1527 // use constant RGB values...
1529 glColor4ub (r,g,b,a);
1531 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1534 /* this is for GL_EXT_SECONDARY_COLOR */
1535 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1536 /* do separate color call here */
1542 x = fl2i(va->sx*16.0f);
1543 y = fl2i(va->sy*16.0f);
1545 x += gr_screen.offset_x*16;
1546 y += gr_screen.offset_y*16;
1548 sx = i2fl(x) / 16.0f;
1549 sy = i2fl(y) / 16.0f;
1551 if ( flags & TMAP_FLAG_TEXTURED ) {
1554 glTexCoord2f(tu, tv);
1557 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1562 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1564 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1567 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1569 void gr_opengl_scaler(vertex *va, vertex *vb )
1571 float x0, y0, x1, y1;
1572 float u0, v0, u1, v1;
1573 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1574 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1575 float xmin, xmax, ymin, ymax;
1576 int dx0, dy0, dx1, dy1;
1578 //============= CLIP IT =====================
1580 x0 = va->sx; y0 = va->sy;
1581 x1 = vb->sx; y1 = vb->sy;
1583 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1584 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1586 u0 = va->u; v0 = va->v;
1587 u1 = vb->u; v1 = vb->v;
1589 // Check for obviously offscreen bitmaps...
1590 if ( (y1<=y0) || (x1<=x0) ) return;
1591 if ( (x1<xmin ) || (x0>xmax) ) return;
1592 if ( (y1<ymin ) || (y0>ymax) ) return;
1594 clipped_u0 = u0; clipped_v0 = v0;
1595 clipped_u1 = u1; clipped_v1 = v1;
1597 clipped_x0 = x0; clipped_y0 = y0;
1598 clipped_x1 = x1; clipped_y1 = y1;
1600 // Clip the left, moving u0 right as necessary
1602 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1606 // Clip the right, moving u1 left as necessary
1608 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1612 // Clip the top, moving v0 down as necessary
1614 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1618 // Clip the bottom, moving v1 up as necessary
1620 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1624 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1625 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1627 if (dx1<=dx0) return;
1628 if (dy1<=dy0) return;
1630 //============= DRAW IT =====================
1644 v[1].sx = clipped_x1;
1645 v[1].sy = clipped_y0;
1648 v[1].u = clipped_u1;
1649 v[1].v = clipped_v0;
1652 v[2].sx = clipped_x1;
1653 v[2].sy = clipped_y1;
1656 v[2].u = clipped_u1;
1657 v[2].v = clipped_v1;
1660 v[3].sx = clipped_x0;
1661 v[3].sy = clipped_y1;
1664 v[3].u = clipped_u0;
1665 v[3].v = clipped_v1;
1667 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1670 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1674 void gr_opengl_get_color( int * r, int * g, int * b )
1676 if (r) *r = gr_screen.current_color.red;
1677 if (g) *g = gr_screen.current_color.green;
1678 if (b) *b = gr_screen.current_color.blue;
1681 void gr_opengl_init_color(color *c, int r, int g, int b)
1683 c->screen_sig = gr_screen.signature;
1684 c->red = (unsigned char)r;
1685 c->green = (unsigned char)g;
1686 c->blue = (unsigned char)b;
1688 c->ac_type = AC_TYPE_NONE;
1690 c->is_alphacolor = 0;
1694 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1696 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1697 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1698 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1699 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1701 gr_opengl_init_color( clr, r, g, b );
1703 clr->alpha = (unsigned char)alpha;
1704 clr->ac_type = (ubyte)type;
1705 clr->alphacolor = -1;
1706 clr->is_alphacolor = 1;
1709 void gr_opengl_set_color( int r, int g, int b )
1711 Assert((r >= 0) && (r < 256));
1712 Assert((g >= 0) && (g < 256));
1713 Assert((b >= 0) && (b < 256));
1715 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1718 void gr_opengl_set_color_fast(color *dst)
1720 if ( dst->screen_sig != gr_screen.signature ) {
1721 if ( dst->is_alphacolor ) {
1722 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1724 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1727 gr_screen.current_color = *dst;
1730 void gr_opengl_print_screen(char *filename)
1735 int gr_opengl_supports_res_ingame(int res)
1742 int gr_opengl_supports_res_interface(int res)
1749 void opengl_tcache_cleanup ();
1750 void gr_opengl_cleanup()
1752 if ( !Inited ) return;
1759 opengl_tcache_cleanup ();
1764 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1766 Assert((r >= 0) && (r < 256));
1767 Assert((g >= 0) && (g < 256));
1768 Assert((b >= 0) && (b < 256));
1770 if (fog_mode == GR_FOGMODE_NONE) {
1771 if (gr_screen.current_fog_mode != fog_mode) {
1774 gr_screen.current_fog_mode = fog_mode;
1779 if (gr_screen.current_fog_mode != fog_mode) {
1782 if (D3D_fog_mode == 2) {
1783 glFogi(GL_FOG_MODE, GL_LINEAR);
1786 gr_screen.current_fog_mode = fog_mode;
1789 if ( (gr_screen.current_fog_color.red != r) ||
1790 (gr_screen.current_fog_color.green != g) ||
1791 (gr_screen.current_fog_color.blue != b) ) {
1794 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1796 fc[0] = (float)r/255.0;
1797 fc[1] = (float)g/255.0;
1798 fc[2] = (float)b/255.0;
1801 glFogfv(GL_FOG_COLOR, fc);
1804 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1805 ((fog_near != gr_screen.fog_near) ||
1806 (fog_far != gr_screen.fog_far)) ) {
1807 gr_screen.fog_near = fog_near;
1808 gr_screen.fog_far = fog_far;
1810 if (D3D_fog_mode == 2) {
1811 glFogf(GL_FOG_START, fog_near);
1812 glFogf(GL_FOG_END, fog_far);
1817 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1822 void gr_opengl_set_cull(int cull)
1825 glEnable (GL_CULL_FACE);
1826 glFrontFace (GL_CCW);
1828 glDisable (GL_CULL_FACE);
1832 void gr_opengl_filter_set(int filter)
1837 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1840 gr_set_bitmap(bmap1);
1843 gr_set_bitmap(bmap2);
1849 typedef struct tcache_slot_opengl {
1850 GLuint texture_handle;
1851 float u_scale, v_scale;
1854 char used_this_frame;
1859 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1860 tcache_slot_opengl *parent;
1861 } tcache_slot_opengl;
1863 static void *Texture_sections = NULL;
1864 tcache_slot_opengl *Textures = NULL;
1866 int GL_texture_sections = 0;
1867 int GL_texture_ram = 0;
1868 int GL_frame_count = 0;
1869 int GL_min_texture_width = 0;
1870 int GL_max_texture_width = 0;
1871 int GL_min_texture_height = 0;
1872 int GL_max_texture_height = 0;
1873 int GL_square_textures = 0;
1874 int GL_textures_in = 0;
1875 int GL_textures_in_frame = 0;
1876 int GL_last_bitmap_id = -1;
1877 int GL_last_detail = -1;
1878 int GL_last_bitmap_type = -1;
1879 int GL_last_section_x = -1;
1880 int GL_last_section_y = -1;
1884 void opengl_tcache_init (int use_sections)
1888 // DDOI - FIXME skipped a lot of stuff here
1889 GL_should_preload = 0;
1891 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1894 if ( tmp_pl == 0 ) {
1895 GL_should_preload = 0;
1896 } else if ( tmp_pl == 1 ) {
1897 GL_should_preload = 1;
1899 GL_should_preload = 1;
1904 GL_min_texture_width = 16;
1905 GL_min_texture_height = 16;
1906 GL_max_texture_width = 256;
1907 GL_max_texture_height = 256;
1909 GL_square_textures = 1;
1911 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1917 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1918 if(!Texture_sections){
1921 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1924 // Init the texture structures
1925 int section_count = 0;
1926 for( i=0; i<MAX_BITMAPS; i++ ) {
1928 Textures[i].vram_texture = NULL;
1929 Textures[i].vram_texture_surface = NULL;
1931 Textures[i].texture_handle = 0;
1933 Textures[i].bitmap_id = -1;
1934 Textures[i].size = 0;
1935 Textures[i].used_this_frame = 0;
1937 Textures[i].parent = NULL;
1939 // allocate sections
1941 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1942 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1943 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1944 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1946 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1947 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1949 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1950 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1951 Textures[i].data_sections[idx][s_idx]->size = 0;
1952 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
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] = NULL;
1964 GL_texture_sections = use_sections;
1966 //GL_last_detail = Detail.hardware_textures;
1967 GL_last_bitmap_id = -1;
1968 GL_last_bitmap_type = -1;
1970 GL_last_section_x = -1;
1971 GL_last_section_y = -1;
1974 GL_textures_in_frame = 0;
1977 int opengl_free_texture (tcache_slot_opengl *t);
1979 void opengl_free_texture_with_handle(int handle)
1981 for(int i=0; i<MAX_BITMAPS; i++ ) {
1982 if (Textures[i].bitmap_id == handle) {
1983 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1984 opengl_free_texture ( &Textures[i] );
1989 void opengl_tcache_flush ()
1993 for( i=0; i<MAX_BITMAPS; i++ ) {
1994 opengl_free_texture ( &Textures[i] );
1996 if (GL_textures_in != 0) {
1997 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
2001 GL_last_bitmap_id = -1;
2002 GL_last_section_x = -1;
2003 GL_last_section_y = -1;
2006 void opengl_tcache_cleanup ()
2008 opengl_tcache_flush ();
2011 GL_textures_in_frame = 0;
2018 if( Texture_sections != NULL ){
2019 free(Texture_sections);
2020 Texture_sections = NULL;
2024 void opengl_tcache_frame ()
2028 GL_last_bitmap_id = -1;
2029 GL_textures_in_frame = 0;
2034 for( i=0; i<MAX_BITMAPS; i++ ) {
2035 Textures[i].used_this_frame = 0;
2038 if(Textures[i].data_sections[0][0] != NULL){
2039 Assert(GL_texture_sections);
2040 if(GL_texture_sections){
2041 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2042 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2043 if(Textures[i].data_sections[idx][s_idx] != NULL){
2044 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
2053 opengl_tcache_flush();
2058 int opengl_free_texture ( tcache_slot_opengl *t )
2064 if ( t->bitmap_id > -1 ) {
2065 // if I, or any of my children have been used this frame, bail
2066 if(t->used_this_frame){
2069 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2070 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2071 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
2077 // ok, now we know its legal to free everything safely
2078 glDeleteTextures (1, &t->texture_handle);
2079 t->texture_handle = 0;
2081 if ( GL_last_bitmap_id == t->bitmap_id ) {
2082 GL_last_bitmap_id = -1;
2085 // if this guy has children, free them too, since the children
2086 // actually make up his size
2087 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2088 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2089 if(t->data_sections[idx][s_idx] != NULL){
2090 opengl_free_texture(t->data_sections[idx][s_idx]);
2096 t->used_this_frame = 0;
2097 GL_textures_in -= t->size;
2103 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
2108 if((w_out == NULL) || (h_out == NULL)){
2118 for (i=0; i<16; i++ ) {
2119 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2125 for (i=0; i<16; i++ ) {
2126 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2133 if ( tex_w < GL_min_texture_width ) {
2134 tex_w = GL_min_texture_width;
2135 } else if ( tex_w > GL_max_texture_width ) {
2136 tex_w = GL_max_texture_width;
2139 if ( tex_h < GL_min_texture_height ) {
2140 tex_h = GL_min_texture_height;
2141 } else if ( tex_h > GL_max_texture_height ) {
2142 tex_h = GL_max_texture_height;
2145 if ( GL_square_textures ) {
2147 // Make the both be equal to larger of the two
2148 new_size = max(tex_w, tex_h);
2153 // store the outgoing size
2158 // data == start of bitmap data
2159 // sx == x offset into bitmap
2160 // sy == y offset into bitmap
2161 // src_w == absolute width of section on source bitmap
2162 // src_h == absolute height of section on source bitmap
2163 // bmap_w == width of source bitmap
2164 // bmap_h == height of source bitmap
2165 // tex_w == width of final texture
2166 // tex_h == height of final texture
2167 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)
2176 if ( t->used_this_frame ) {
2177 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2182 if(!opengl_free_texture(t)){
2187 // get final texture size
2188 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2190 if ( (tex_w < 1) || (tex_h < 1) ) {
2191 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2195 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2196 t->u_scale = (float)bmap_w / (float)tex_w;
2197 t->v_scale = (float)bmap_h / (float)tex_h;
2198 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2199 t->u_scale = (float)src_w / (float)tex_w;
2200 t->v_scale = (float)src_h / (float)tex_h;
2207 glGenTextures (1, &t->texture_handle);
2210 if (t->texture_handle == 0) {
2211 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2215 GL_current_texture_source = (gr_texture_source) -1;
2217 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2219 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2220 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2221 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2222 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2223 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2225 switch (bitmap_type) {
2227 case TCACHE_TYPE_AABITMAP:
2230 ubyte *bmp_data = ((ubyte*)data);
2231 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2232 ubyte *texmemp = texmem;
2235 for (i=0; i<16; i++) {
2236 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2239 for ( ; i<256; i++ ) {
2243 for (i=0;i<tex_h;i++)
2245 for (j=0;j<tex_w;j++)
2247 if (i < bmap_h && j < bmap_w) {
2249 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2257 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2262 case TCACHE_TYPE_BITMAP_SECTION:
2265 ubyte *bmp_data = ((ubyte*)data);
2266 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2267 ubyte *texmemp = texmem;
2269 for (i=0;i<tex_h;i++)
2271 for (j=0;j<tex_w;j++)
2273 if (i < src_h && j < src_w) {
2274 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2275 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2282 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2283 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2291 ubyte *bmp_data = ((ubyte*)data);
2292 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2293 ubyte *texmemp = texmem;
2295 fix u, utmp, v, du, dv;
2299 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2300 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2302 for (j=0;j<tex_h;j++)
2305 for (i=0;i<tex_w;i++)
2307 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2308 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2314 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2315 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2323 t->bitmap_id = texture_handle;
2324 t->time_created = GL_frame_count;
2325 t->used_this_frame = 0;
2326 t->size = tex_w * tex_h * 2;
2327 t->w = (ushort)tex_w;
2328 t->h = (ushort)tex_h;
2329 GL_textures_in_frame += t->size;
2331 GL_textures_in += t->size;
2337 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2341 int final_w, final_h;
2345 // setup texture/bitmap flags
2347 switch(bitmap_type){
2348 case TCACHE_TYPE_AABITMAP:
2349 flags |= BMP_AABITMAP;
2352 case TCACHE_TYPE_NORMAL:
2353 flags |= BMP_TEX_OTHER;
2354 case TCACHE_TYPE_XPARENT:
2355 flags |= BMP_TEX_XPARENT;
2357 case TCACHE_TYPE_NONDARKENING:
2359 flags |= BMP_TEX_NONDARK;
2363 // lock the bitmap into the proper format
2364 bmp = bm_lock(bitmap_handle, bpp, flags);
2365 if ( bmp == NULL ) {
2366 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2375 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2376 // max_w /= D3D_texture_divider;
2377 // max_h /= D3D_texture_divider;
2379 // Detail.debris_culling goes from 0 to 4.
2380 max_w /= (16 >> Detail.hardware_textures);
2381 max_h /= (16 >> Detail.hardware_textures);
2385 // get final texture size as it will be allocated as a DD surface
2386 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2388 // if this tcache slot has no bitmap
2389 if ( tslot->bitmap_id < 0) {
2392 // different bitmap altogether - determine if the new one can use the old one's slot
2393 else if (tslot->bitmap_id != bitmap_handle) {
2394 if((final_w == tslot->w) && (final_h == tslot->h)){
2396 //ml_printf("Reloading texture %d\n", bitmap_handle);
2403 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);
2405 // unlock the bitmap
2406 bm_unlock(bitmap_handle);
2411 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2415 int final_w, final_h;
2416 int section_x, section_y;
2419 // setup texture/bitmap flags
2420 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2421 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2422 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2424 flags = BMP_TEX_XPARENT;
2426 // lock the bitmap in the proper format
2427 bmp = bm_lock(bitmap_handle, 16, flags);
2428 if ( bmp == NULL ) {
2429 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2432 // determine the width and height of this section
2433 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2435 // get final texture size as it will be allocated as an opengl texture
2436 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2438 // if this tcache slot has no bitmap
2439 if ( tslot->bitmap_id < 0) {
2442 // different bitmap altogether - determine if the new one can use the old one's slot
2443 else if (tslot->bitmap_id != bitmap_handle) {
2444 if((final_w == tslot->w) && (final_h == tslot->h)){
2452 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);
2454 // unlock the bitmap
2455 bm_unlock(bitmap_handle);
2461 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2462 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)
2471 GL_last_bitmap_id = -1;
2475 if ( GL_last_detail != Detail.hardware_textures ) {
2476 GL_last_detail = Detail.hardware_textures;
2477 opengl_tcache_flush();
2484 int n = bm_get_cache_slot (bitmap_id, 1);
2485 tcache_slot_opengl *t = &Textures[n];
2487 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)) {
2488 t->used_this_frame++;
2490 // mark all children as used
2491 if(GL_texture_sections){
2492 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2493 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2494 if(t->data_sections[idx][s_idx] != NULL){
2495 t->data_sections[idx][s_idx]->used_this_frame++;
2501 *u_scale = t->u_scale;
2502 *v_scale = t->v_scale;
2506 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2507 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2508 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2514 // if the texture sections haven't been created yet
2515 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2517 // lock the bitmap in the proper format
2518 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2519 bm_unlock(bitmap_id);
2521 // now lets do something for each texture
2523 for(idx=0; idx<bmp->sections.num_x; idx++){
2524 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2525 // hmm. i'd rather we didn't have to do it this way...
2526 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2530 // not used this frame
2531 t->data_sections[idx][s_idx]->used_this_frame = 0;
2535 // zero out pretty much everything in the parent struct since he's just the root
2536 t->bitmap_id = bitmap_id;
2537 t->texture_handle = 0;
2538 t->time_created = t->data_sections[sx][sy]->time_created;
2539 t->used_this_frame = 0;
2541 t->vram_texture = NULL;
2542 t->vram_texture_surface = NULL
2546 // argh. we failed to upload. free anything we can
2548 opengl_free_texture(t);
2550 // swap in the texture we want
2552 t = t->data_sections[sx][sy];
2555 // all other "normal" textures
2556 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2557 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2560 // everything went ok
2561 if(ret_val && (t->texture_handle) && !vram_full){
2562 *u_scale = t->u_scale;
2563 *v_scale = t->v_scale;
2565 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2567 GL_last_bitmap_id = t->bitmap_id;
2568 GL_last_bitmap_type = bitmap_type;
2569 GL_last_section_x = sx;
2570 GL_last_section_y = sy;
2572 t->used_this_frame++;
2576 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2583 void gr_opengl_set_clear_color(int r, int g, int b)
2585 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2588 void gr_opengl_flash(int r, int g, int b)
2594 if ( r || g || b ) {
2595 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2597 float x1, x2, y1, y2;
2598 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2599 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2600 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2601 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2603 glColor4ub(r, g, b, 255);
2605 glVertex3f (x1, y2, -0.99);
2607 glVertex3f (x2, y2, -0.99);
2609 glVertex3f (x2, y1, -0.99);
2611 glVertex3f (x1, y1, -0.99);
2616 int gr_opengl_zbuffer_get()
2618 if ( !gr_global_zbuffering ) {
2619 return GR_ZBUFF_NONE;
2621 return gr_zbuffering_mode;
2624 int gr_opengl_zbuffer_set(int mode)
2626 int tmp = gr_zbuffering_mode;
2628 gr_zbuffering_mode = mode;
2630 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2638 void gr_opengl_zbuffer_clear(int mode)
2642 gr_zbuffering_mode = GR_ZBUFF_FULL;
2643 gr_global_zbuffering = 1;
2645 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2646 glClear ( GL_DEPTH_BUFFER_BIT );
2649 gr_zbuffering_mode = GR_ZBUFF_NONE;
2650 gr_global_zbuffering = 0;
2654 void gr_opengl_set_gamma(float gamma)
2657 Gr_gamma_int = int (Gr_gamma*10);
2659 // Create the Gamma lookup table
2661 for (i=0;i<256; i++) {
2662 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2665 } else if ( v < 0 ) {
2668 Gr_gamma_lookup[i] = v;
2671 // Flush any existing textures
2672 opengl_tcache_flush();
2675 void gr_opengl_fade_in(int instantaneous)
2680 void gr_opengl_fade_out(int instantaneous)
2685 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2688 glReadBuffer(GL_FRONT);
2690 glReadBuffer(GL_BACK);
2693 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2695 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2697 if (gr_screen.bits_per_pixel == 15) {
2698 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2699 } else if (gr_screen.bits_per_pixel == 32) {
2700 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2703 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2706 static int Gr_opengl_mouse_saved = 0;
2707 static int Gr_opengl_mouse_saved_x1 = 0;
2708 static int Gr_opengl_mouse_saved_y1 = 0;
2709 static int Gr_opengl_mouse_saved_x2 = 0;
2710 static int Gr_opengl_mouse_saved_y2 = 0;
2711 static int Gr_opengl_mouse_saved_w = 0;
2712 static int Gr_opengl_mouse_saved_h = 0;
2713 #define MAX_SAVE_SIZE (32*32)
2714 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2716 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2718 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2720 Gr_opengl_mouse_saved_x1 = x;
2721 Gr_opengl_mouse_saved_y1 = y;
2722 Gr_opengl_mouse_saved_x2 = x+w-1;
2723 Gr_opengl_mouse_saved_y2 = y+h-1;
2725 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2726 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2727 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2728 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2730 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2731 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2733 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2734 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2736 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2738 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2740 glReadBuffer(GL_BACK);
2741 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);
2743 Gr_opengl_mouse_saved = 1;
2746 int gr_opengl_save_screen()
2750 if ( Gr_saved_screen ) {
2751 mprintf(( "Screen alread saved!\n" ));
2755 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2756 if (!Gr_saved_screen) {
2757 mprintf(( "Couldn't get memory for saved screen!\n" ));
2761 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2762 if (!Gr_saved_screen_tmp) {
2763 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2767 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2769 glReadBuffer(GL_FRONT);
2770 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);
2774 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2775 dptr = (ubyte *)Gr_saved_screen;
2776 for (int j = 0; j < gr_screen.max_h; j++) {
2777 sptr -= gr_screen.max_w*2;
2778 memcpy(dptr, sptr, gr_screen.max_w*2);
2779 dptr += gr_screen.max_w*2;
2782 free(Gr_saved_screen_tmp);
2784 if (Gr_opengl_mouse_saved) {
2785 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2786 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2787 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2788 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2791 dptr -= gr_screen.max_w*2;
2795 // this leaks texture handles, and the opengl doesn't currently
2796 // perform some sort of garbage collection, so a hack was added
2797 // to bmpman to make it free textures when released
2798 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2800 return Gr_saved_screen_bitmap;
2803 void gr_opengl_restore_screen(int id)
2807 if ( !Gr_saved_screen ) {
2812 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2814 gr_set_bitmap(Gr_saved_screen_bitmap);
2818 void gr_opengl_free_screen(int id)
2820 bm_release(Gr_saved_screen_bitmap);
2822 if ( Gr_saved_screen ) {
2823 free( Gr_saved_screen );
2824 Gr_saved_screen = NULL;
2828 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2833 void gr_opengl_dump_frame_stop()
2838 void gr_opengl_dump_frame()
2843 uint gr_opengl_lock()
2850 void gr_opengl_unlock()
2854 void opengl_zbias(int bias)
2857 glEnable(GL_POLYGON_OFFSET_FILL);
2858 glPolygonOffset(0.0, -bias);
2860 glDisable(GL_POLYGON_OFFSET_FILL);
2864 extern char *Osreg_title;
2865 void gr_opengl_init()
2868 gr_opengl_cleanup();
2872 mprintf(( "Initializing opengl graphics device...\n" ));
2876 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2878 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2884 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2885 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2886 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2887 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2888 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2890 int flags = SDL_OPENGL;
2892 if (os_config_read_uint( NULL, "Fullscreen", 0 ) == 1)
2893 flags |= SDL_FULLSCREEN;
2895 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2897 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2902 SDL_WM_SetCaption (Osreg_title, "FS2");
2904 /* might as well put this here */
2905 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2907 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2909 glMatrixMode(GL_PROJECTION);
2911 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2912 glMatrixMode(GL_MODELVIEW);
2915 glShadeModel(GL_SMOOTH);
2916 glEnable(GL_DITHER);
2917 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2918 glHint(GL_FOG_HINT, GL_NICEST);
2920 glEnable(GL_DEPTH_TEST);
2923 glEnable(GL_TEXTURE_2D);
2925 glDepthRange(0.0, 1.0);
2927 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2928 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2930 D3D_32bit = 1; // grd3d.cpp
2931 extern int D3D_enabled;
2934 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2935 1 = use secondary color ext
2936 2 = use opengl linear fog
2938 D3D_fog_mode = 2; // grd3d.cpp
2942 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2949 gr_screen.bits_per_pixel = 15;
2950 gr_screen.bytes_per_pixel = 2;
2955 Gr_red.mask = 0x7C00;
2960 Gr_green.mask = 0x3E0;
2965 Gr_blue.mask = 0x1F;
2970 gr_screen.bits_per_pixel = 16;
2971 gr_screen.bytes_per_pixel = 2;
2976 Gr_red.mask = 0xF800;
2981 Gr_green.mask = 0x7E0;
2986 Gr_blue.mask = 0x1F;
2991 gr_screen.bits_per_pixel = 32;
2992 gr_screen.bytes_per_pixel = 4;
2997 Gr_red.mask = 0xff0000;
3002 Gr_green.mask = 0xff00;
3007 Gr_blue.mask = 0xff;
3012 Int3(); // Illegal bpp
3015 // DDOI - set these so no one else does!
3016 Gr_ta_red.mask = 0x0f00;
3017 Gr_ta_red.shift = 8;
3018 Gr_ta_red.scale = 16;
3020 Gr_ta_green.mask = 0x00f0;
3021 Gr_ta_green.shift = 4;
3022 Gr_ta_green.scale = 16;
3024 Gr_ta_blue.mask = 0x000f;
3025 Gr_ta_blue.shift = 0;
3026 Gr_ta_blue.scale = 16;
3028 Gr_ta_alpha.mask = 0xf000;
3029 Gr_ta_alpha.shift = 12;
3030 Gr_ta_alpha.scale = 16;
3033 opengl_tcache_init (1);
3036 Gr_current_red = &Gr_red;
3037 Gr_current_blue = &Gr_blue;
3038 Gr_current_green = &Gr_green;
3039 Gr_current_alpha = &Gr_alpha;
3041 gr_screen.gf_flip = gr_opengl_flip;
3042 gr_screen.gf_flip_window = gr_opengl_flip_window;
3043 gr_screen.gf_set_clip = gr_opengl_set_clip;
3044 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3045 gr_screen.gf_set_font = grx_set_font;
3047 gr_screen.gf_set_color = gr_opengl_set_color;
3048 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3049 gr_screen.gf_create_shader = gr_opengl_create_shader;
3050 gr_screen.gf_set_shader = gr_opengl_set_shader;
3051 gr_screen.gf_clear = gr_opengl_clear;
3052 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3053 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3054 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3055 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3057 gr_screen.gf_rect = gr_opengl_rect;
3058 gr_screen.gf_shade = gr_opengl_shade;
3059 gr_screen.gf_string = gr_opengl_string;
3060 gr_screen.gf_circle = gr_opengl_circle;
3062 gr_screen.gf_line = gr_opengl_line;
3063 gr_screen.gf_aaline = gr_opengl_aaline;
3064 gr_screen.gf_pixel = gr_opengl_pixel;
3065 gr_screen.gf_scaler = gr_opengl_scaler;
3066 gr_screen.gf_tmapper = gr_opengl_tmapper;
3068 gr_screen.gf_gradient = gr_opengl_gradient;
3070 gr_screen.gf_set_palette = gr_opengl_set_palette;
3071 gr_screen.gf_get_color = gr_opengl_get_color;
3072 gr_screen.gf_init_color = gr_opengl_init_color;
3073 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3074 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3075 gr_screen.gf_print_screen = gr_opengl_print_screen;
3077 gr_screen.gf_fade_in = gr_opengl_fade_in;
3078 gr_screen.gf_fade_out = gr_opengl_fade_out;
3079 gr_screen.gf_flash = gr_opengl_flash;
3081 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3082 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3083 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3085 gr_screen.gf_save_screen = gr_opengl_save_screen;
3086 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3087 gr_screen.gf_free_screen = gr_opengl_free_screen;
3089 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3090 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3091 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3093 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3095 gr_screen.gf_lock = gr_opengl_lock;
3096 gr_screen.gf_unlock = gr_opengl_unlock;
3098 gr_screen.gf_fog_set = gr_opengl_fog_set;
3100 gr_screen.gf_get_region = gr_opengl_get_region;
3102 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3104 gr_screen.gf_set_cull = gr_opengl_set_cull;
3106 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3108 gr_screen.gf_filter_set = gr_opengl_filter_set;
3110 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3112 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;