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.48 2002/06/09 04:41:17 relnev
19 * added copyright header
21 * Revision 1.47 2002/06/09 03:16:04 relnev
24 * removed unneeded asm, old sdl 2d setup.
26 * fixed crash caused by opengl_get_region.
28 * Revision 1.46 2002/06/05 04:03:32 relnev
29 * finished cfilesystem.
31 * removed some old code.
33 * fixed mouse save off-by-one.
37 * Revision 1.45 2002/06/03 09:25:37 relnev
38 * implement mouse cursor and screen save/restore
40 * Revision 1.44 2002/06/02 18:46:59 relnev
43 * Revision 1.43 2002/06/02 11:34:00 relnev
46 * Revision 1.42 2002/06/02 10:28:17 relnev
47 * fix texture handle leak
49 * Revision 1.41 2002/06/01 09:00:34 relnev
50 * silly debug memmanager
52 * Revision 1.40 2002/06/01 07:12:33 relnev
53 * a few NDEBUG updates.
55 * removed a few warnings.
57 * Revision 1.39 2002/06/01 05:33:15 relnev
58 * copied more code over.
60 * added scissor clipping.
62 * Revision 1.38 2002/06/01 03:35:27 relnev
65 * Revision 1.37 2002/06/01 03:32:00 relnev
66 * fix texture loading mistake.
68 * enable some d3d stuff for opengl also
70 * Revision 1.36 2002/05/31 23:25:03 relnev
73 * Revision 1.34 2002/05/31 22:15:22 relnev
76 * Revision 1.33 2002/05/31 22:04:55 relnev
77 * use d3d rect_internal
79 * Revision 1.32 2002/05/31 06:28:23 relnev
82 * Revision 1.31 2002/05/31 06:04:39 relnev
85 * Revision 1.30 2002/05/31 03:56:11 theoddone33
86 * Change tmapper polygon winding and enable culling
88 * Revision 1.29 2002/05/31 03:34:02 theoddone33
92 * Revision 1.28 2002/05/31 00:06:59 relnev
95 * Revision 1.27 2002/05/30 23:46:29 theoddone33
96 * some minor key changes (not necessarily fixes)
98 * Revision 1.26 2002/05/30 23:33:12 relnev
99 * implemented a few more functions.
101 * Revision 1.25 2002/05/30 23:01:16 relnev
102 * implement gr_opengl_set_state.
104 * Revision 1.24 2002/05/30 22:12:57 relnev
105 * finish default texture case
107 * Revision 1.23 2002/05/30 22:02:30 theoddone33
110 * Revision 1.22 2002/05/30 21:44:48 relnev
111 * implemented some missing texture stuff.
113 * enable bitmap polys for opengl.
115 * work around greenness in bitmaps.
117 * Revision 1.21 2002/05/30 17:29:30 theoddone33
118 * Fix some more stubs, change at least one polygon winding since culling is now
121 * Revision 1.20 2002/05/30 16:50:24 theoddone33
122 * Keyboard partially fixed
124 * Revision 1.19 2002/05/30 08:13:14 relnev
127 * Revision 1.18 2002/05/29 23:37:36 relnev
130 * Revision 1.17 2002/05/29 23:17:49 theoddone33
131 * Non working text code and fixed keys
133 * Revision 1.16 2002/05/29 19:45:13 theoddone33
134 * More changes on texture loading
136 * Revision 1.15 2002/05/29 19:06:48 theoddone33
137 * Enable string printing. Enable texture mapping
139 * Revision 1.14 2002/05/29 08:54:40 relnev
140 * "fixed" bitmap drawing.
142 * copied more d3d code over.
144 * Revision 1.13 2002/05/29 06:25:13 theoddone33
145 * Keyboard input, mouse tracking now work
147 * Revision 1.12 2002/05/29 04:52:45 relnev
150 * Revision 1.11 2002/05/29 04:29:56 relnev
151 * removed some unncessary stubbing, implemented opengl rect
153 * Revision 1.10 2002/05/29 04:13:27 theoddone33
156 * Revision 1.9 2002/05/29 03:35:51 relnev
159 * Revision 1.8 2002/05/29 03:30:05 relnev
160 * update opengl stubs
162 * Revision 1.7 2002/05/29 02:52:32 theoddone33
163 * Enable OpenGL renderer
165 * Revision 1.6 2002/05/28 04:56:51 theoddone33
166 * runs a little bit now
168 * Revision 1.5 2002/05/28 04:07:28 theoddone33
169 * New graphics stubbing arrangement
171 * Revision 1.4 2002/05/27 23:39:34 relnev
174 * Revision 1.3 2002/05/27 22:35:01 theoddone33
177 * Revision 1.2 2002/05/27 22:32:02 theoddone33
178 * throw all d3d stuff at opengl
180 * Revision 1.1.1.1 2002/05/03 03:28:09 root
184 * 10 7/14/99 9:42a Dave
185 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
188 * 9 7/09/99 9:51a Dave
189 * Added thick polyline code.
191 * 8 6/29/99 10:35a Dave
192 * Interface polygon bitmaps! Whee!
194 * 7 2/03/99 11:44a Dave
195 * Fixed d3d transparent textures.
197 * 6 1/24/99 11:37p Dave
198 * First full rev of beam weapons. Very customizable. Removed some bogus
199 * Int3()'s in low level net code.
201 * 5 12/18/98 1:13a Dave
202 * Rough 1024x768 support for Direct3D. Proper detection and usage through
205 * 4 12/06/98 2:36p Dave
206 * Drastically improved nebula fogging.
208 * 3 11/11/98 5:37p Dave
209 * Checkin for multiplayer testing.
211 * 2 10/07/98 10:53a Dave
214 * 1 10/07/98 10:49a Dave
216 * 14 5/20/98 9:46p John
217 * added code so the places in code that change half the palette don't
218 * have to clear the screen.
220 * 13 5/06/98 5:30p John
221 * Removed unused cfilearchiver. Removed/replaced some unused/little used
222 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
223 * DirectX header files and libs that fixed the Direct3D alpha blending
226 * 12 4/14/98 12:15p John
227 * Made 16-bpp movies work.
229 * 11 3/12/98 5:36p John
230 * Took out any unused shaders. Made shader code take rgbc instead of
231 * matrix and vector since noone used it like a matrix and it would have
232 * been impossible to do in hardware. Made Glide implement a basic
233 * shader for online help.
235 * 10 3/10/98 4:18p John
236 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
237 * & Glide have popups and print screen. Took out all >8bpp software
238 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
239 * support Fred. Made zbuffering key off of functions rather than one
242 * 9 12/02/97 4:00p John
243 * Added first rev of thruster glow, along with variable levels of
244 * translucency, which retquired some restructing of palman.
246 * 8 10/03/97 9:10a John
247 * added better antialiased line drawer
249 * 7 9/23/97 10:45a John
250 * made so you can tell bitblt code to rle a bitmap by passing flag to
253 * 6 9/09/97 11:01a Sandeep
254 * fixed warning level 4 bugs
256 * 5 7/10/97 2:06p John
257 * added code to specify alphablending type for bitmaps.
259 * 4 6/17/97 7:04p John
260 * added d3d support for gradients.
261 * fixed some color bugs by adding screen signatures instead of watching
262 * flags and palette changes.
264 * 3 6/12/97 2:50a Lawrance
265 * bm_unlock() now passed bitmap number, not pointer
267 * 2 6/11/97 1:12p John
268 * Started fixing all the text colors in the game.
270 * 1 5/12/97 12:14p John
277 #include <windowsx.h>
286 #include "floating.h"
288 #include "systemvars.h"
289 #include "grinternal.h"
290 #include "gropengl.h"
294 #include "osregistry.h"
296 static int Inited = 0;
298 typedef enum gr_texture_source {
300 TEXTURE_SOURCE_DECAL,
301 TEXTURE_SOURCE_NO_FILTERING,
304 typedef enum gr_alpha_blend {
305 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
306 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
307 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
308 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
311 typedef enum gr_zbuffer_type {
318 #define NEBULA_COLORS 20
320 volatile int GL_activate = 0;
321 volatile int GL_deactivate = 0;
323 static char *Gr_saved_screen = NULL;
324 static int Gr_saved_screen_bitmap;
327 // Throw in some dummy functions - DDOI
329 int D3D_32bit = 0; // grd3d.cpp
330 int D3D_fog_mode = -1; // grd3d.cpp
331 int D3D_inited = 0; // grd3d.cpp
332 int D3D_zbias = 1; // grd3d.cpp
333 int D3d_rendition_uvs = 0; // grd3d.cpp
340 void d3d_zbias (int a)
347 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
348 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
349 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
351 void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
353 /* TODO: this doesn't work unless it's handled at the per-texture level */
354 /* if (ts != GL_current_texture_source) */ {
356 case TEXTURE_SOURCE_NONE:
357 glBindTexture(GL_TEXTURE_2D, 0);
358 gr_tcache_set(-1, -1, NULL, NULL );
360 case TEXTURE_SOURCE_DECAL:
361 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
362 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
363 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
365 case TEXTURE_SOURCE_NO_FILTERING:
366 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
367 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
368 // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
374 GL_current_texture_source = ts;
377 if (ab != GL_current_alpha_blend) {
379 case ALPHA_BLEND_NONE:
380 glBlendFunc(GL_ONE, GL_ZERO);
382 case ALPHA_BLEND_ALPHA_ADDITIVE:
383 glBlendFunc(GL_ONE, GL_ONE);
385 case ALPHA_BLEND_ALPHA_BLEND_ALPHA:
386 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
388 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR:
389 glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
395 GL_current_alpha_blend = ab;
398 if (zt != GL_current_zbuffer_type) {
400 case ZBUFFER_TYPE_NONE:
401 glDepthFunc(GL_ALWAYS);
402 glDepthMask(GL_FALSE);
404 case ZBUFFER_TYPE_READ:
405 glDepthFunc(GL_LESS);
406 glDepthMask(GL_FALSE);
408 case ZBUFFER_TYPE_WRITE:
409 glDepthFunc(GL_ALWAYS);
410 glDepthMask(GL_TRUE);
412 case ZBUFFER_TYPE_FULL:
413 glDepthFunc(GL_LESS);
414 glDepthMask(GL_TRUE);
420 GL_current_zbuffer_type = zt;
424 void gr_opengl_activate(int active)
430 make sure window is active and mouse grabbed
436 make sure mouse is not grabbed and window minimized
444 void opengl_tcache_flush ();
446 void gr_opengl_preload_init()
448 if (gr_screen.mode != GR_OPENGL) {
452 opengl_tcache_flush ();
455 int GL_should_preload = 0;
456 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
458 if ( gr_screen.mode != GR_OPENGL) {
462 if ( !GL_should_preload ) {
466 float u_scale, v_scale;
470 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
472 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
476 mprintf(("Texture upload failed!\n" ));
482 void gr_opengl_pixel(int x, int y)
487 void gr_opengl_clear()
489 glClearColor(gr_screen.current_clear_color.red / 255.0,
490 gr_screen.current_clear_color.green / 255.0,
491 gr_screen.current_clear_color.blue / 255.0, 1.0);
493 glClear ( GL_COLOR_BUFFER_BIT );
496 void gr_opengl_save_mouse_area(int x, int y, int w, int h);
497 void opengl_tcache_frame ();
498 void gr_opengl_flip()
506 extern int Gr_opengl_mouse_saved;
507 Gr_opengl_mouse_saved = 0;
509 if ( mouse_is_visible() ) {
513 mouse_get_pos( &mx, &my );
515 gr_opengl_save_mouse_area(mx,my,32,32);
517 if ( Gr_cursor == -1 ) {
520 gr_set_bitmap(Gr_cursor);
526 GLenum error = glGetError();
529 error = glGetError();
531 if (error != GL_NO_ERROR) {
532 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
535 } while (error != GL_NO_ERROR);
538 SDL_GL_SwapBuffers ();
540 opengl_tcache_frame ();
542 int cnt = GL_activate;
545 opengl_tcache_flush();
546 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
552 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
556 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
561 void gr_opengl_set_clip(int x,int y,int w,int h)
563 // check for sanity of parameters
569 if (x >= gr_screen.max_w)
570 x = gr_screen.max_w - 1;
571 if (y >= gr_screen.max_h)
572 y = gr_screen.max_h - 1;
574 if (x + w > gr_screen.max_w)
575 w = gr_screen.max_w - x;
576 if (y + h > gr_screen.max_h)
577 h = gr_screen.max_h - y;
579 if (w > gr_screen.max_w)
581 if (h > gr_screen.max_h)
584 gr_screen.offset_x = x;
585 gr_screen.offset_y = y;
586 gr_screen.clip_left = 0;
587 gr_screen.clip_right = w-1;
588 gr_screen.clip_top = 0;
589 gr_screen.clip_bottom = h-1;
590 gr_screen.clip_width = w;
591 gr_screen.clip_height = h;
593 glEnable(GL_SCISSOR_TEST);
594 glScissor(x, gr_screen.max_h-y-h, w, h);
597 void gr_opengl_reset_clip()
599 gr_screen.offset_x = 0;
600 gr_screen.offset_y = 0;
601 gr_screen.clip_left = 0;
602 gr_screen.clip_top = 0;
603 gr_screen.clip_right = gr_screen.max_w - 1;
604 gr_screen.clip_bottom = gr_screen.max_h - 1;
605 gr_screen.clip_width = gr_screen.max_w;
606 gr_screen.clip_height = gr_screen.max_h;
608 glDisable(GL_SCISSOR_TEST);
609 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
612 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
614 gr_screen.current_alpha = alpha;
615 gr_screen.current_alphablend_mode = alphablend_mode;
616 gr_screen.current_bitblt_mode = bitblt_mode;
617 gr_screen.current_bitmap = bitmap_num;
619 gr_screen.current_bitmap_sx = sx;
620 gr_screen.current_bitmap_sy = sy;
623 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
625 shade->screen_sig = gr_screen.signature;
632 void gr_opengl_set_shader( shader * shade )
635 if (shade->screen_sig != gr_screen.signature) {
636 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
638 gr_screen.current_shader = *shade;
640 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
645 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
647 STUB_FUNCTION; /* who called me? */
650 extern int GL_last_bitmap_id;
651 bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
655 GLubyte *sptr, *dptr;
659 int cw = min(bmp->w, w);
660 int ch = min(bmp->h, h);
662 GL_last_bitmap_id = -1; /* HACK! */
664 glColor4f(1.0, 1.0, 1.0, 1.0);
665 glBindTexture(GL_TEXTURE_2D, bitmapTex);
668 for (iy = sy; iy < ch; iy += 256) {
670 ih = min(256, (ch-iy));
672 for (ix = sx; ix < cw; ix += 256) {
674 sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
676 iw = min(256, (cw-ix));
682 memcpy(dptr, sptr, iw*2);
690 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
700 t = (float)ih / 256.0;
705 s = (float)iw / 256.0;
706 t = (float)ih / 256.0;
711 s = (float)iw / 256.0;
723 bm_unlock(gr_screen.current_bitmap);
728 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
730 STUB_FUNCTION; /* who called me? */
737 int dx1=x, dx2=x+w-1;
738 int dy1=y, dy2=y+h-1;
741 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
746 if ( count > 1 ) Int3();
750 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
751 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
752 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
753 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
754 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
755 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
782 if ( w < 1 ) return; // clipped away!
783 if ( h < 1 ) return; // clipped away!
787 // Make sure clipping algorithm works
791 Assert( w == (dx2-dx1+1) );
792 Assert( h == (dy2-dy1+1) );
795 Assert( sx+w <= bw );
796 Assert( sy+h <= bh );
797 Assert( dx2 >= dx1 );
798 Assert( dy2 >= dy1 );
799 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
800 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
801 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
802 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
805 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
806 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
808 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
812 void gr_opengl_bitmap(int x, int y)
814 STUB_FUNCTION; /* who called me? */
818 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
819 int dx1=x, dx2=x+w-1;
820 int dy1=y, dy2=y+h-1;
823 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
824 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
825 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
826 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
827 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
828 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
830 if ( sx < 0 ) return;
831 if ( sy < 0 ) return;
832 if ( sx >= w ) return;
833 if ( sy >= h ) return;
835 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
837 gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
841 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
845 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
847 saved_zbuf = gr_zbuffer_get();
849 // start the frame, no zbuffering, no culling
851 gr_zbuffer_set(GR_ZBUFF_NONE);
860 v[0].flags = PF_PROJECTED;
867 v[1].sx = i2fl(x + w);
872 v[1].flags = PF_PROJECTED;
879 v[2].sx = i2fl(x + w);
880 v[2].sy = i2fl(y + h);
884 v[2].flags = PF_PROJECTED;
892 v[3].sy = i2fl(y + h);
896 v[3].flags = PF_PROJECTED;
904 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
908 // restore zbuffer and culling
909 gr_zbuffer_set(saved_zbuf);
913 void gr_opengl_rect(int x,int y,int w,int h)
915 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);
918 void gr_opengl_shade(int x,int y,int w,int h)
925 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
926 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
927 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
928 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
929 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
930 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
931 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
932 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
934 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
937 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
942 if ( !gr_screen.current_color.is_alphacolor ) return;
944 float u_scale, v_scale;
946 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
948 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
949 // Couldn't set texture
950 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
954 float u0, u1, v0, v1;
955 float x1, x2, y1, y2;
958 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
960 u0 = u_scale*i2fl(sx)/i2fl(bw);
961 v0 = v_scale*i2fl(sy)/i2fl(bh);
963 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
964 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
966 x1 = i2fl(x+gr_screen.offset_x);
967 y1 = i2fl(y+gr_screen.offset_y);
968 x2 = i2fl(x+w+gr_screen.offset_x);
969 y2 = i2fl(y+h+gr_screen.offset_y);
971 if ( gr_screen.current_color.is_alphacolor ) {
972 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
974 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
978 glTexCoord2f (u0, v1);
979 glVertex3f (x1, y2, -0.99);
981 glTexCoord2f (u1, v1);
982 glVertex3f (x2, y2, -0.99);
984 glTexCoord2f (u1, v0);
985 glVertex3f (x2, y1, -0.99);
987 glTexCoord2f (u0, v0);
988 glVertex3f (x1, y1, -0.99);
992 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
999 int dx1=x, dx2=x+w-1;
1000 int dy1=y, dy2=y+h-1;
1003 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
1008 if ( count > 1 ) Int3();
1012 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1013 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1014 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1015 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1016 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1017 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1034 if ( sx + w > bw ) {
1039 if ( sy + h > bh ) {
1044 if ( w < 1 ) return; // clipped away!
1045 if ( h < 1 ) return; // clipped away!
1049 // Make sure clipping algorithm works
1053 Assert( w == (dx2-dx1+1) );
1054 Assert( h == (dy2-dy1+1) );
1057 Assert( sx+w <= bw );
1058 Assert( sy+h <= bh );
1059 Assert( dx2 >= dx1 );
1060 Assert( dy2 >= dy1 );
1061 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
1062 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
1063 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
1064 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
1067 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
1068 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1071 void gr_opengl_aabitmap(int x, int y)
1075 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
1076 int dx1=x, dx2=x+w-1;
1077 int dy1=y, dy2=y+h-1;
1080 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
1081 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
1082 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
1083 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
1084 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
1085 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
1087 if ( sx < 0 ) return;
1088 if ( sy < 0 ) return;
1089 if ( sx >= w ) return;
1090 if ( sy >= h ) return;
1092 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
1093 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
1096 void gr_opengl_string( int sx, int sy, char *s )
1098 int width, spacing, letter;
1101 if ( !Current_font ) {
1105 gr_set_bitmap(Current_font->bitmap_id);
1110 if (sx==0x8000) { //centered
1111 x = get_centered_x(s);
1121 while (*s== '\n' ) {
1123 y += Current_font->h;
1124 if (sx==0x8000) { //centered
1125 x = get_centered_x(s);
1130 if (*s == 0 ) break;
1132 letter = get_char_width(s[0],s[1],&width,&spacing);
1135 //not in font, draw as space
1143 // Check if this character is totally clipped
1144 if ( x + width < gr_screen.clip_left ) continue;
1145 if ( y + Current_font->h < gr_screen.clip_top ) continue;
1146 if ( x > gr_screen.clip_right ) continue;
1147 if ( y > gr_screen.clip_bottom ) continue;
1150 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
1151 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1155 wc = width - xd; hc = Current_font->h - yd;
1156 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1157 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1159 if ( wc < 1 ) continue;
1160 if ( hc < 1 ) continue;
1162 int u = Current_font->bm_u[letter];
1163 int v = Current_font->bm_v[letter];
1165 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1169 void gr_opengl_line(int x1,int y1,int x2,int y2)
1171 int clipped = 0, swapped=0;
1173 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1175 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);
1180 sx1 = i2fl(x1 + gr_screen.offset_x);
1181 sy1 = i2fl(y1 + gr_screen.offset_y);
1182 sx2 = i2fl(x2 + gr_screen.offset_x);
1183 sy2 = i2fl(y2 + gr_screen.offset_y);
1185 if ( x1 == x2 && y1 == y2 ) {
1186 glBegin (GL_POINTS);
1187 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1188 glVertex3f (sx1, sy1, -0.99f);
1200 } else if ( y1 == y2 ) {
1209 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1210 glVertex3f (sx2, sy2, -0.99f);
1211 glVertex3f (sx1, sy1, -0.99f);
1215 void gr_opengl_aaline(vertex *v1, vertex *v2)
1217 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1220 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1222 int clipped = 0, swapped=0;
1224 if ( !gr_screen.current_color.is_alphacolor ) {
1225 gr_line( x1, y1, x2, y2 );
1229 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);
1231 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1233 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1234 int ba = swapped ? gr_screen.current_color.alpha : 0;
1239 sx1 = i2fl(x1 + gr_screen.offset_x);
1240 sy1 = i2fl(y1 + gr_screen.offset_y);
1241 sx2 = i2fl(x2 + gr_screen.offset_x);
1242 sy2 = i2fl(y2 + gr_screen.offset_y);
1250 } else if ( y1 == y2 ) {
1259 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1260 glVertex3f (sx2, sy2, -0.99f);
1261 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1262 glVertex3f (sx1, sy1, -0.99f);
1266 void gr_opengl_circle( int xc, int yc, int d )
1276 if ( (xc+r) < gr_screen.clip_left ) return;
1277 if ( (xc-r) > gr_screen.clip_right ) return;
1278 if ( (yc+r) < gr_screen.clip_top ) return;
1279 if ( (yc-r) > gr_screen.clip_bottom ) return;
1282 // Draw the first octant
1283 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1284 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1289 // Draw the second octant
1290 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1291 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1299 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1300 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1305 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1309 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1310 if (f_float < 0.0f) {
1318 *a = (int)(f_float * 255.0);
1321 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1324 float u_scale = 1.0f, v_scale = 1.0f;
1326 // Make nebula use the texture mapper... this blends the colors better.
1327 if ( flags & TMAP_FLAG_NEBULA ){
1331 gr_texture_source texture_source = (gr_texture_source)-1;
1332 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1333 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1335 if ( gr_zbuffering ) {
1336 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1337 zbuffer_type = ZBUFFER_TYPE_READ;
1339 zbuffer_type = ZBUFFER_TYPE_FULL;
1342 zbuffer_type = ZBUFFER_TYPE_NONE;
1347 int tmap_type = TCACHE_TYPE_NORMAL;
1351 if ( flags & TMAP_FLAG_TEXTURED ) {
1354 r = gr_screen.current_color.red;
1355 g = gr_screen.current_color.green;
1356 b = gr_screen.current_color.blue;
1359 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1362 tmap_type = TCACHE_TYPE_NORMAL;
1363 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1365 // Blend with screen pixel using src*alpha+dst
1366 float factor = gr_screen.current_alpha;
1370 if ( factor <= 1.0f ) {
1371 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1372 r = (r*tmp_alpha)/255;
1373 g = (g*tmp_alpha)/255;
1374 b = (b*tmp_alpha)/255;
1377 tmap_type = TCACHE_TYPE_XPARENT;
1379 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1381 // Blend with screen pixel using src*alpha+dst
1382 float factor = gr_screen.current_alpha;
1384 if ( factor > 1.0f ) {
1387 alpha = fl2i(gr_screen.current_alpha*255.0f);
1391 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1392 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1394 alpha_blend = ALPHA_BLEND_NONE;
1399 if(flags & TMAP_FLAG_BITMAP_SECTION){
1400 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1403 texture_source = TEXTURE_SOURCE_NONE;
1405 if ( flags & TMAP_FLAG_TEXTURED ) {
1406 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 ))
1408 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1412 // use nonfiltered textures for bitmap sections
1413 if(flags & TMAP_FLAG_BITMAP_SECTION){
1414 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1416 texture_source = TEXTURE_SOURCE_DECAL;
1421 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1423 if ( flags & TMAP_FLAG_TEXTURED )
1429 if (flags & TMAP_FLAG_PIXEL_FOG) {
1435 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1437 vertex * va = verts[i];
1441 x = fl2i(va->sx*16.0f);
1442 y = fl2i(va->sy*16.0f);
1444 x += gr_screen.offset_x*16;
1445 y += gr_screen.offset_y*16;
1447 sx = i2fl(x) / 16.0f;
1448 sy = i2fl(y) / 16.0f;
1450 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1461 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1464 glBegin(GL_TRIANGLE_FAN);
1465 for (i = nv-1; i >= 0; i--) {
1466 vertex * va = verts[i];
1472 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1473 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1482 if ( flags & TMAP_FLAG_CORRECT ) {
1488 if (flags & TMAP_FLAG_ALPHA) {
1494 if (flags & TMAP_FLAG_NEBULA ) {
1495 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1496 r = gr_palette[pal*3+0];
1497 g = gr_palette[pal*3+1];
1498 b = gr_palette[pal*3+2];
1499 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1500 r = Gr_gamma_lookup[verts[i]->b];
1501 g = Gr_gamma_lookup[verts[i]->b];
1502 b = Gr_gamma_lookup[verts[i]->b];
1503 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1504 // Make 0.75 be 256.0f
1505 r = Gr_gamma_lookup[verts[i]->r];
1506 g = Gr_gamma_lookup[verts[i]->g];
1507 b = Gr_gamma_lookup[verts[i]->b];
1509 // use constant RGB values...
1511 glColor4ub (r,g,b,a);
1513 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1516 /* this is for GL_EXT_SECONDARY_COLOR */
1517 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1518 /* do separate color call here */
1524 x = fl2i(va->sx*16.0f);
1525 y = fl2i(va->sy*16.0f);
1527 x += gr_screen.offset_x*16;
1528 y += gr_screen.offset_y*16;
1530 sx = i2fl(x) / 16.0f;
1531 sy = i2fl(y) / 16.0f;
1533 if ( flags & TMAP_FLAG_TEXTURED ) {
1536 glTexCoord2f(tu, tv);
1539 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1544 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1546 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1549 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1551 void gr_opengl_scaler(vertex *va, vertex *vb )
1553 float x0, y0, x1, y1;
1554 float u0, v0, u1, v1;
1555 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1556 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1557 float xmin, xmax, ymin, ymax;
1558 int dx0, dy0, dx1, dy1;
1560 //============= CLIP IT =====================
1562 x0 = va->sx; y0 = va->sy;
1563 x1 = vb->sx; y1 = vb->sy;
1565 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1566 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1568 u0 = va->u; v0 = va->v;
1569 u1 = vb->u; v1 = vb->v;
1571 // Check for obviously offscreen bitmaps...
1572 if ( (y1<=y0) || (x1<=x0) ) return;
1573 if ( (x1<xmin ) || (x0>xmax) ) return;
1574 if ( (y1<ymin ) || (y0>ymax) ) return;
1576 clipped_u0 = u0; clipped_v0 = v0;
1577 clipped_u1 = u1; clipped_v1 = v1;
1579 clipped_x0 = x0; clipped_y0 = y0;
1580 clipped_x1 = x1; clipped_y1 = y1;
1582 // Clip the left, moving u0 right as necessary
1584 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1588 // Clip the right, moving u1 left as necessary
1590 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1594 // Clip the top, moving v0 down as necessary
1596 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1600 // Clip the bottom, moving v1 up as necessary
1602 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1606 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1607 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1609 if (dx1<=dx0) return;
1610 if (dy1<=dy0) return;
1612 //============= DRAW IT =====================
1626 v[1].sx = clipped_x1;
1627 v[1].sy = clipped_y0;
1630 v[1].u = clipped_u1;
1631 v[1].v = clipped_v0;
1634 v[2].sx = clipped_x1;
1635 v[2].sy = clipped_y1;
1638 v[2].u = clipped_u1;
1639 v[2].v = clipped_v1;
1642 v[3].sx = clipped_x0;
1643 v[3].sy = clipped_y1;
1646 v[3].u = clipped_u0;
1647 v[3].v = clipped_v1;
1649 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1652 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1656 void gr_opengl_get_color( int * r, int * g, int * b )
1658 if (r) *r = gr_screen.current_color.red;
1659 if (g) *g = gr_screen.current_color.green;
1660 if (b) *b = gr_screen.current_color.blue;
1663 void gr_opengl_init_color(color *c, int r, int g, int b)
1665 c->screen_sig = gr_screen.signature;
1666 c->red = (unsigned char)r;
1667 c->green = (unsigned char)g;
1668 c->blue = (unsigned char)b;
1670 c->ac_type = AC_TYPE_NONE;
1672 c->is_alphacolor = 0;
1676 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1678 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1679 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1680 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1681 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1683 gr_opengl_init_color( clr, r, g, b );
1685 clr->alpha = (unsigned char)alpha;
1686 clr->ac_type = (ubyte)type;
1687 clr->alphacolor = -1;
1688 clr->is_alphacolor = 1;
1691 void gr_opengl_set_color( int r, int g, int b )
1693 Assert((r >= 0) && (r < 256));
1694 Assert((g >= 0) && (g < 256));
1695 Assert((b >= 0) && (b < 256));
1697 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1700 void gr_opengl_set_color_fast(color *dst)
1702 if ( dst->screen_sig != gr_screen.signature ) {
1703 if ( dst->is_alphacolor ) {
1704 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1706 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1709 gr_screen.current_color = *dst;
1712 void gr_opengl_print_screen(char *filename)
1717 int gr_opengl_supports_res_ingame(int res)
1724 int gr_opengl_supports_res_interface(int res)
1731 void opengl_tcache_cleanup ();
1732 void gr_opengl_cleanup()
1734 if ( !Inited ) return;
1741 opengl_tcache_cleanup ();
1746 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1748 Assert((r >= 0) && (r < 256));
1749 Assert((g >= 0) && (g < 256));
1750 Assert((b >= 0) && (b < 256));
1752 if (fog_mode == GR_FOGMODE_NONE) {
1753 if (gr_screen.current_fog_mode != fog_mode) {
1756 gr_screen.current_fog_mode = fog_mode;
1761 if (gr_screen.current_fog_mode != fog_mode) {
1764 if (D3D_fog_mode == 2) {
1765 glFogi(GL_FOG_MODE, GL_LINEAR);
1768 gr_screen.current_fog_mode = fog_mode;
1771 if ( (gr_screen.current_fog_color.red != r) ||
1772 (gr_screen.current_fog_color.green != g) ||
1773 (gr_screen.current_fog_color.blue != b) ) {
1776 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1778 fc[0] = (float)r/255.0;
1779 fc[1] = (float)g/255.0;
1780 fc[2] = (float)b/255.0;
1783 glFogfv(GL_FOG_COLOR, fc);
1786 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1787 ((fog_near != gr_screen.fog_near) ||
1788 (fog_far != gr_screen.fog_far)) ) {
1789 gr_screen.fog_near = fog_near;
1790 gr_screen.fog_far = fog_far;
1792 if (D3D_fog_mode == 2) {
1793 glFogf(GL_FOG_START, fog_near);
1794 glFogf(GL_FOG_END, fog_far);
1799 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1804 void gr_opengl_set_cull(int cull)
1807 glEnable (GL_CULL_FACE);
1808 glFrontFace (GL_CCW);
1810 glDisable (GL_CULL_FACE);
1814 void gr_opengl_filter_set(int filter)
1819 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1822 gr_set_bitmap(bmap1);
1825 gr_set_bitmap(bmap2);
1831 typedef struct tcache_slot_opengl {
1832 GLuint texture_handle;
1833 float u_scale, v_scale;
1836 char used_this_frame;
1841 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1842 tcache_slot_opengl *parent;
1843 } tcache_slot_opengl;
1845 static void *Texture_sections = NULL;
1846 tcache_slot_opengl *Textures = NULL;
1848 int GL_texture_sections = 0;
1849 int GL_texture_ram = 0;
1850 int GL_frame_count = 0;
1851 int GL_min_texture_width = 0;
1852 int GL_max_texture_width = 0;
1853 int GL_min_texture_height = 0;
1854 int GL_max_texture_height = 0;
1855 int GL_square_textures = 0;
1856 int GL_textures_in = 0;
1857 int GL_textures_in_frame = 0;
1858 int GL_last_bitmap_id = -1;
1859 int GL_last_detail = -1;
1860 int GL_last_bitmap_type = -1;
1861 int GL_last_section_x = -1;
1862 int GL_last_section_y = -1;
1866 void opengl_tcache_init (int use_sections)
1870 // DDOI - FIXME skipped a lot of stuff here
1871 GL_should_preload = 0;
1873 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1876 if ( tmp_pl == 0 ) {
1877 GL_should_preload = 0;
1878 } else if ( tmp_pl == 1 ) {
1879 GL_should_preload = 1;
1881 GL_should_preload = 1;
1886 GL_min_texture_width = 16;
1887 GL_min_texture_height = 16;
1888 GL_max_texture_width = 256;
1889 GL_max_texture_height = 256;
1891 GL_square_textures = 1;
1893 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1899 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1900 if(!Texture_sections){
1903 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1906 // Init the texture structures
1907 int section_count = 0;
1908 for( i=0; i<MAX_BITMAPS; i++ ) {
1910 Textures[i].vram_texture = NULL;
1911 Textures[i].vram_texture_surface = NULL;
1913 Textures[i].texture_handle = 0;
1915 Textures[i].bitmap_id = -1;
1916 Textures[i].size = 0;
1917 Textures[i].used_this_frame = 0;
1919 Textures[i].parent = NULL;
1921 // allocate sections
1923 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1924 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1925 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1926 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1928 Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1929 Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1931 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1932 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1933 Textures[i].data_sections[idx][s_idx]->size = 0;
1934 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1938 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1939 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1940 Textures[i].data_sections[idx][s_idx] = NULL;
1946 GL_texture_sections = use_sections;
1948 //GL_last_detail = Detail.hardware_textures;
1949 GL_last_bitmap_id = -1;
1950 GL_last_bitmap_type = -1;
1952 GL_last_section_x = -1;
1953 GL_last_section_y = -1;
1956 GL_textures_in_frame = 0;
1959 int opengl_free_texture (tcache_slot_opengl *t);
1961 void opengl_free_texture_with_handle(int handle)
1963 for(int i=0; i<MAX_BITMAPS; i++ ) {
1964 if (Textures[i].bitmap_id == handle) {
1965 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1966 opengl_free_texture ( &Textures[i] );
1971 void opengl_tcache_flush ()
1975 for( i=0; i<MAX_BITMAPS; i++ ) {
1976 opengl_free_texture ( &Textures[i] );
1978 if (GL_textures_in != 0) {
1979 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1983 GL_last_bitmap_id = -1;
1984 GL_last_section_x = -1;
1985 GL_last_section_y = -1;
1988 void opengl_tcache_cleanup ()
1990 opengl_tcache_flush ();
1993 GL_textures_in_frame = 0;
2000 if( Texture_sections != NULL ){
2001 free(Texture_sections);
2002 Texture_sections = NULL;
2006 void opengl_tcache_frame ()
2010 GL_last_bitmap_id = -1;
2011 GL_textures_in_frame = 0;
2016 for( i=0; i<MAX_BITMAPS; i++ ) {
2017 Textures[i].used_this_frame = 0;
2020 if(Textures[i].data_sections[0][0] != NULL){
2021 Assert(GL_texture_sections);
2022 if(GL_texture_sections){
2023 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2024 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2025 if(Textures[i].data_sections[idx][s_idx] != NULL){
2026 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
2035 opengl_tcache_flush();
2040 int opengl_free_texture ( tcache_slot_opengl *t )
2046 if ( t->bitmap_id > -1 ) {
2047 // if I, or any of my children have been used this frame, bail
2048 if(t->used_this_frame){
2051 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2052 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2053 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
2059 // ok, now we know its legal to free everything safely
2060 glDeleteTextures (1, &t->texture_handle);
2061 t->texture_handle = 0;
2063 if ( GL_last_bitmap_id == t->bitmap_id ) {
2064 GL_last_bitmap_id = -1;
2067 // if this guy has children, free them too, since the children
2068 // actually make up his size
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){
2072 opengl_free_texture(t->data_sections[idx][s_idx]);
2078 t->used_this_frame = 0;
2079 GL_textures_in -= t->size;
2085 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
2090 if((w_out == NULL) || (h_out == NULL)){
2100 for (i=0; i<16; i++ ) {
2101 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
2107 for (i=0; i<16; i++ ) {
2108 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
2115 if ( tex_w < GL_min_texture_width ) {
2116 tex_w = GL_min_texture_width;
2117 } else if ( tex_w > GL_max_texture_width ) {
2118 tex_w = GL_max_texture_width;
2121 if ( tex_h < GL_min_texture_height ) {
2122 tex_h = GL_min_texture_height;
2123 } else if ( tex_h > GL_max_texture_height ) {
2124 tex_h = GL_max_texture_height;
2127 if ( GL_square_textures ) {
2129 // Make the both be equal to larger of the two
2130 new_size = max(tex_w, tex_h);
2135 // store the outgoing size
2140 // data == start of bitmap data
2141 // sx == x offset into bitmap
2142 // sy == y offset into bitmap
2143 // src_w == absolute width of section on source bitmap
2144 // src_h == absolute height of section on source bitmap
2145 // bmap_w == width of source bitmap
2146 // bmap_h == height of source bitmap
2147 // tex_w == width of final texture
2148 // tex_h == height of final texture
2149 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)
2158 if ( t->used_this_frame ) {
2159 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2164 if(!opengl_free_texture(t)){
2169 // get final texture size
2170 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2172 if ( (tex_w < 1) || (tex_h < 1) ) {
2173 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2177 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2178 t->u_scale = (float)bmap_w / (float)tex_w;
2179 t->v_scale = (float)bmap_h / (float)tex_h;
2180 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2181 t->u_scale = (float)src_w / (float)tex_w;
2182 t->v_scale = (float)src_h / (float)tex_h;
2189 glGenTextures (1, &t->texture_handle);
2192 if (t->texture_handle == 0) {
2193 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2197 GL_current_texture_source = (gr_texture_source) -1;
2199 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2201 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2202 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2203 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2204 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2205 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2207 switch (bitmap_type) {
2209 case TCACHE_TYPE_AABITMAP:
2212 ubyte *bmp_data = ((ubyte*)data);
2213 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2214 ubyte *texmemp = texmem;
2217 for (i=0; i<16; i++) {
2218 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2221 for ( ; i<256; i++ ) {
2225 for (i=0;i<tex_h;i++)
2227 for (j=0;j<tex_w;j++)
2229 if (i < bmap_h && j < bmap_w) {
2231 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2239 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2244 case TCACHE_TYPE_BITMAP_SECTION:
2247 ubyte *bmp_data = ((ubyte*)data);
2248 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2249 ubyte *texmemp = texmem;
2251 for (i=0;i<tex_h;i++)
2253 for (j=0;j<tex_w;j++)
2255 if (i < src_h && j < src_w) {
2256 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2257 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2264 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2265 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2273 ubyte *bmp_data = ((ubyte*)data);
2274 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2275 ubyte *texmemp = texmem;
2277 fix u, utmp, v, du, dv;
2281 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2282 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2284 for (j=0;j<tex_h;j++)
2287 for (i=0;i<tex_w;i++)
2289 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2290 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2296 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2297 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2305 t->bitmap_id = texture_handle;
2306 t->time_created = GL_frame_count;
2307 t->used_this_frame = 0;
2308 t->size = tex_w * tex_h * 2;
2309 t->w = (ushort)tex_w;
2310 t->h = (ushort)tex_h;
2311 GL_textures_in_frame += t->size;
2313 GL_textures_in += t->size;
2319 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2323 int final_w, final_h;
2327 // setup texture/bitmap flags
2329 switch(bitmap_type){
2330 case TCACHE_TYPE_AABITMAP:
2331 flags |= BMP_AABITMAP;
2334 case TCACHE_TYPE_NORMAL:
2335 flags |= BMP_TEX_OTHER;
2336 case TCACHE_TYPE_XPARENT:
2337 flags |= BMP_TEX_XPARENT;
2339 case TCACHE_TYPE_NONDARKENING:
2341 flags |= BMP_TEX_NONDARK;
2345 // lock the bitmap into the proper format
2346 bmp = bm_lock(bitmap_handle, bpp, flags);
2347 if ( bmp == NULL ) {
2348 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2357 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2358 // max_w /= D3D_texture_divider;
2359 // max_h /= D3D_texture_divider;
2361 // Detail.debris_culling goes from 0 to 4.
2362 max_w /= (16 >> Detail.hardware_textures);
2363 max_h /= (16 >> Detail.hardware_textures);
2367 // get final texture size as it will be allocated as a DD surface
2368 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2370 // if this tcache slot has no bitmap
2371 if ( tslot->bitmap_id < 0) {
2374 // different bitmap altogether - determine if the new one can use the old one's slot
2375 else if (tslot->bitmap_id != bitmap_handle) {
2376 if((final_w == tslot->w) && (final_h == tslot->h)){
2378 //ml_printf("Reloading texture %d\n", bitmap_handle);
2385 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);
2387 // unlock the bitmap
2388 bm_unlock(bitmap_handle);
2393 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2397 int final_w, final_h;
2398 int section_x, section_y;
2401 // setup texture/bitmap flags
2402 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2403 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2404 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2406 flags = BMP_TEX_XPARENT;
2408 // lock the bitmap in the proper format
2409 bmp = bm_lock(bitmap_handle, 16, flags);
2410 if ( bmp == NULL ) {
2411 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2414 // determine the width and height of this section
2415 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2417 // get final texture size as it will be allocated as an opengl texture
2418 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2420 // if this tcache slot has no bitmap
2421 if ( tslot->bitmap_id < 0) {
2424 // different bitmap altogether - determine if the new one can use the old one's slot
2425 else if (tslot->bitmap_id != bitmap_handle) {
2426 if((final_w == tslot->w) && (final_h == tslot->h)){
2434 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);
2436 // unlock the bitmap
2437 bm_unlock(bitmap_handle);
2443 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2444 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)
2453 GL_last_bitmap_id = -1;
2457 if ( GL_last_detail != Detail.hardware_textures ) {
2458 GL_last_detail = Detail.hardware_textures;
2459 opengl_tcache_flush();
2466 int n = bm_get_cache_slot (bitmap_id, 1);
2467 tcache_slot_opengl *t = &Textures[n];
2469 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)) {
2470 t->used_this_frame++;
2472 // mark all children as used
2473 if(GL_texture_sections){
2474 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2475 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2476 if(t->data_sections[idx][s_idx] != NULL){
2477 t->data_sections[idx][s_idx]->used_this_frame++;
2483 *u_scale = t->u_scale;
2484 *v_scale = t->v_scale;
2488 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2489 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2490 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2496 // if the texture sections haven't been created yet
2497 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2499 // lock the bitmap in the proper format
2500 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2501 bm_unlock(bitmap_id);
2503 // now lets do something for each texture
2505 for(idx=0; idx<bmp->sections.num_x; idx++){
2506 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2507 // hmm. i'd rather we didn't have to do it this way...
2508 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2512 // not used this frame
2513 t->data_sections[idx][s_idx]->used_this_frame = 0;
2517 // zero out pretty much everything in the parent struct since he's just the root
2518 t->bitmap_id = bitmap_id;
2519 t->texture_handle = 0;
2520 t->time_created = t->data_sections[sx][sy]->time_created;
2521 t->used_this_frame = 0;
2523 t->vram_texture = NULL;
2524 t->vram_texture_surface = NULL
2528 // argh. we failed to upload. free anything we can
2530 opengl_free_texture(t);
2532 // swap in the texture we want
2534 t = t->data_sections[sx][sy];
2537 // all other "normal" textures
2538 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2539 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2542 // everything went ok
2543 if(ret_val && (t->texture_handle) && !vram_full){
2544 *u_scale = t->u_scale;
2545 *v_scale = t->v_scale;
2547 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2549 GL_last_bitmap_id = t->bitmap_id;
2550 GL_last_bitmap_type = bitmap_type;
2551 GL_last_section_x = sx;
2552 GL_last_section_y = sy;
2554 t->used_this_frame++;
2558 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2565 void gr_opengl_set_clear_color(int r, int g, int b)
2567 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2570 void gr_opengl_flash(int r, int g, int b)
2576 if ( r || g || b ) {
2577 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2579 float x1, x2, y1, y2;
2580 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2581 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2582 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2583 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2585 glColor4ub(r, g, b, 255);
2587 glVertex3f (x1, y2, -0.99);
2589 glVertex3f (x2, y2, -0.99);
2591 glVertex3f (x2, y1, -0.99);
2593 glVertex3f (x1, y1, -0.99);
2598 int gr_opengl_zbuffer_get()
2600 if ( !gr_global_zbuffering ) {
2601 return GR_ZBUFF_NONE;
2603 return gr_zbuffering_mode;
2606 int gr_opengl_zbuffer_set(int mode)
2608 int tmp = gr_zbuffering_mode;
2610 gr_zbuffering_mode = mode;
2612 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2620 void gr_opengl_zbuffer_clear(int mode)
2624 gr_zbuffering_mode = GR_ZBUFF_FULL;
2625 gr_global_zbuffering = 1;
2627 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2628 glClear ( GL_DEPTH_BUFFER_BIT );
2631 gr_zbuffering_mode = GR_ZBUFF_NONE;
2632 gr_global_zbuffering = 0;
2636 void gr_opengl_set_gamma(float gamma)
2639 Gr_gamma_int = int (Gr_gamma*10);
2641 // Create the Gamma lookup table
2643 for (i=0;i<256; i++) {
2644 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2647 } else if ( v < 0 ) {
2650 Gr_gamma_lookup[i] = v;
2653 // Flush any existing textures
2654 opengl_tcache_flush();
2657 void gr_opengl_fade_in(int instantaneous)
2662 void gr_opengl_fade_out(int instantaneous)
2667 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2670 glReadBuffer(GL_FRONT);
2672 glReadBuffer(GL_BACK);
2675 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2677 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2679 if (gr_screen.bits_per_pixel == 15) {
2680 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2681 } else if (gr_screen.bits_per_pixel == 32) {
2682 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2685 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2688 static int Gr_opengl_mouse_saved = 0;
2689 static int Gr_opengl_mouse_saved_x1 = 0;
2690 static int Gr_opengl_mouse_saved_y1 = 0;
2691 static int Gr_opengl_mouse_saved_x2 = 0;
2692 static int Gr_opengl_mouse_saved_y2 = 0;
2693 static int Gr_opengl_mouse_saved_w = 0;
2694 static int Gr_opengl_mouse_saved_h = 0;
2695 #define MAX_SAVE_SIZE (32*32)
2696 static ubyte Gr_opengl_mouse_saved_data[MAX_SAVE_SIZE*2];
2698 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
2700 void gr_opengl_save_mouse_area(int x, int y, int w, int h)
2702 Gr_opengl_mouse_saved_x1 = x;
2703 Gr_opengl_mouse_saved_y1 = y;
2704 Gr_opengl_mouse_saved_x2 = x+w-1;
2705 Gr_opengl_mouse_saved_y2 = y+h-1;
2707 CLAMP(Gr_opengl_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
2708 CLAMP(Gr_opengl_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
2709 CLAMP(Gr_opengl_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
2710 CLAMP(Gr_opengl_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
2712 Gr_opengl_mouse_saved_w = Gr_opengl_mouse_saved_x2 - Gr_opengl_mouse_saved_x1 + 1;
2713 Gr_opengl_mouse_saved_h = Gr_opengl_mouse_saved_y2 - Gr_opengl_mouse_saved_y1 + 1;
2715 if ( Gr_opengl_mouse_saved_w < 1 ) return;
2716 if ( Gr_opengl_mouse_saved_h < 1 ) return;
2718 Assert( (Gr_opengl_mouse_saved_w*Gr_opengl_mouse_saved_h) <= MAX_SAVE_SIZE );
2720 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2722 glReadBuffer(GL_BACK);
2723 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);
2725 Gr_opengl_mouse_saved = 1;
2728 int gr_opengl_save_screen()
2732 if ( Gr_saved_screen ) {
2733 mprintf(( "Screen alread saved!\n" ));
2737 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2738 if (!Gr_saved_screen) {
2739 mprintf(( "Couldn't get memory for saved screen!\n" ));
2743 char *Gr_saved_screen_tmp = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
2744 if (!Gr_saved_screen_tmp) {
2745 mprintf(( "Couldn't get memory for temporary saved screen!\n" ));
2749 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2751 glReadBuffer(GL_FRONT);
2752 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);
2756 sptr = (ubyte *)&Gr_saved_screen_tmp[gr_screen.max_w*gr_screen.max_h*2];
2757 dptr = (ubyte *)Gr_saved_screen;
2758 for (int j = 0; j < gr_screen.max_h; j++) {
2759 sptr -= gr_screen.max_w*2;
2760 memcpy(dptr, sptr, gr_screen.max_w*2);
2761 dptr += gr_screen.max_w*2;
2764 free(Gr_saved_screen_tmp);
2766 if (Gr_opengl_mouse_saved) {
2767 sptr = (ubyte *)Gr_opengl_mouse_saved_data;
2768 dptr = (ubyte *)&Gr_saved_screen[2*(Gr_opengl_mouse_saved_x1+(Gr_opengl_mouse_saved_y2)*gr_screen.max_w)];
2769 for (int i = 0; i < Gr_opengl_mouse_saved_h; i++) {
2770 memcpy(dptr, sptr, Gr_opengl_mouse_saved_w*2);
2773 dptr -= gr_screen.max_w*2;
2777 // this leaks texture handles, and the opengl doesn't currently
2778 // perform some sort of garbage collection, so a hack was added
2779 // to bmpman to make it free textures when released
2780 Gr_saved_screen_bitmap = bm_create(16, gr_screen.max_w, gr_screen.max_h, Gr_saved_screen, 0);
2782 return Gr_saved_screen_bitmap;
2785 void gr_opengl_restore_screen(int id)
2789 if ( !Gr_saved_screen ) {
2794 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2796 gr_set_bitmap(Gr_saved_screen_bitmap);
2800 void gr_opengl_free_screen(int id)
2802 bm_release(Gr_saved_screen_bitmap);
2804 if ( Gr_saved_screen ) {
2805 free( Gr_saved_screen );
2806 Gr_saved_screen = NULL;
2810 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2815 void gr_opengl_dump_frame_stop()
2820 void gr_opengl_dump_frame()
2825 uint gr_opengl_lock()
2832 void gr_opengl_unlock()
2836 void opengl_zbias(int bias)
2839 glEnable(GL_POLYGON_OFFSET_FILL);
2840 glPolygonOffset(0.0, -bias);
2842 glDisable(GL_POLYGON_OFFSET_FILL);
2846 extern char *Osreg_title;
2847 void gr_opengl_init()
2850 gr_opengl_cleanup();
2854 mprintf(( "Initializing opengl graphics device...\n" ));
2858 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2860 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2866 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2867 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2868 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2869 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2870 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2872 int flags = SDL_OPENGL;
2874 if (os_config_read_uint( NULL, "Fullscreen", 0 ) == 1)
2875 flags |= SDL_FULLSCREEN;
2877 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2879 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2884 SDL_WM_SetCaption (Osreg_title, "FS2");
2886 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2888 glMatrixMode(GL_PROJECTION);
2890 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2891 glMatrixMode(GL_MODELVIEW);
2894 glShadeModel(GL_SMOOTH);
2895 glEnable(GL_DITHER);
2896 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2897 glHint(GL_FOG_HINT, GL_NICEST);
2899 glEnable(GL_DEPTH_TEST);
2902 glEnable(GL_TEXTURE_2D);
2904 glDepthRange(0.0, 1.0);
2906 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2907 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2909 D3D_32bit = 1; // grd3d.cpp
2910 extern int D3D_enabled;
2913 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2914 1 = use secondary color ext
2915 2 = use opengl linear fog
2917 D3D_fog_mode = 2; // grd3d.cpp
2921 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2928 gr_screen.bits_per_pixel = 15;
2929 gr_screen.bytes_per_pixel = 2;
2934 Gr_red.mask = 0x7C00;
2939 Gr_green.mask = 0x3E0;
2944 Gr_blue.mask = 0x1F;
2949 gr_screen.bits_per_pixel = 16;
2950 gr_screen.bytes_per_pixel = 2;
2955 Gr_red.mask = 0xF800;
2960 Gr_green.mask = 0x7E0;
2965 Gr_blue.mask = 0x1F;
2970 gr_screen.bits_per_pixel = 32;
2971 gr_screen.bytes_per_pixel = 4;
2976 Gr_red.mask = 0xff0000;
2981 Gr_green.mask = 0xff00;
2986 Gr_blue.mask = 0xff;
2991 Int3(); // Illegal bpp
2994 // DDOI - set these so no one else does!
2995 Gr_ta_red.mask = 0x0f00;
2996 Gr_ta_red.shift = 8;
2997 Gr_ta_red.scale = 16;
2999 Gr_ta_green.mask = 0x00f0;
3000 Gr_ta_green.shift = 4;
3001 Gr_ta_green.scale = 16;
3003 Gr_ta_blue.mask = 0x000f;
3004 Gr_ta_blue.shift = 0;
3005 Gr_ta_blue.scale = 16;
3007 Gr_ta_alpha.mask = 0xf000;
3008 Gr_ta_alpha.shift = 12;
3009 Gr_ta_alpha.scale = 16;
3012 opengl_tcache_init (1);
3015 Gr_current_red = &Gr_red;
3016 Gr_current_blue = &Gr_blue;
3017 Gr_current_green = &Gr_green;
3018 Gr_current_alpha = &Gr_alpha;
3020 gr_screen.gf_flip = gr_opengl_flip;
3021 gr_screen.gf_flip_window = gr_opengl_flip_window;
3022 gr_screen.gf_set_clip = gr_opengl_set_clip;
3023 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
3024 gr_screen.gf_set_font = grx_set_font;
3026 gr_screen.gf_set_color = gr_opengl_set_color;
3027 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
3028 gr_screen.gf_create_shader = gr_opengl_create_shader;
3029 gr_screen.gf_set_shader = gr_opengl_set_shader;
3030 gr_screen.gf_clear = gr_opengl_clear;
3031 // gr_screen.gf_bitmap = gr_opengl_bitmap;
3032 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
3033 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
3034 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
3036 gr_screen.gf_rect = gr_opengl_rect;
3037 gr_screen.gf_shade = gr_opengl_shade;
3038 gr_screen.gf_string = gr_opengl_string;
3039 gr_screen.gf_circle = gr_opengl_circle;
3041 gr_screen.gf_line = gr_opengl_line;
3042 gr_screen.gf_aaline = gr_opengl_aaline;
3043 gr_screen.gf_pixel = gr_opengl_pixel;
3044 gr_screen.gf_scaler = gr_opengl_scaler;
3045 gr_screen.gf_tmapper = gr_opengl_tmapper;
3047 gr_screen.gf_gradient = gr_opengl_gradient;
3049 gr_screen.gf_set_palette = gr_opengl_set_palette;
3050 gr_screen.gf_get_color = gr_opengl_get_color;
3051 gr_screen.gf_init_color = gr_opengl_init_color;
3052 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
3053 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
3054 gr_screen.gf_print_screen = gr_opengl_print_screen;
3056 gr_screen.gf_fade_in = gr_opengl_fade_in;
3057 gr_screen.gf_fade_out = gr_opengl_fade_out;
3058 gr_screen.gf_flash = gr_opengl_flash;
3060 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
3061 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
3062 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
3064 gr_screen.gf_save_screen = gr_opengl_save_screen;
3065 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
3066 gr_screen.gf_free_screen = gr_opengl_free_screen;
3068 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
3069 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
3070 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
3072 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
3074 gr_screen.gf_lock = gr_opengl_lock;
3075 gr_screen.gf_unlock = gr_opengl_unlock;
3077 gr_screen.gf_fog_set = gr_opengl_fog_set;
3079 gr_screen.gf_get_region = gr_opengl_get_region;
3081 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
3083 gr_screen.gf_set_cull = gr_opengl_set_cull;
3085 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
3087 gr_screen.gf_filter_set = gr_opengl_filter_set;
3089 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
3091 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;