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.58 2002/09/04 01:12:11 relnev
19 * changes to screen backup/mouse drawing code. removed a few warnings.
21 * Revision 1.57 2002/08/31 01:39:13 theoddone33
22 * Speed up the renderer a tad
24 * Revision 1.56 2002/08/01 05:03:11 relnev
27 * Revision 1.55 2002/08/01 04:55:45 relnev
28 * experimenting with texture state
30 * Revision 1.54 2002/07/30 15:00:15 relnev
31 * not use luminance alpha by default
33 * Revision 1.53 2002/06/22 17:08:16 relnev
34 * more fixes to unused alpha code
36 * Revision 1.52 2002/06/21 23:59:14 relnev
37 * moved a gr_opengl_set_state to after the gr_tcache_set
39 * Revision 1.51 2002/06/19 04:52:45 relnev
40 * MacOS X updates (Ryan)
42 * Revision 1.50 2002/06/18 23:20:44 relnev
45 * adjusted line offsets.
47 * Revision 1.49 2002/06/17 23:11:39 relnev
48 * enable sdl key repeating.
52 * Revision 1.48 2002/06/09 04:41:17 relnev
53 * added copyright header
55 * Revision 1.47 2002/06/09 03:16:04 relnev
58 * removed unneeded asm, old sdl 2d setup.
60 * fixed crash caused by opengl_get_region.
62 * Revision 1.46 2002/06/05 04:03:32 relnev
63 * finished cfilesystem.
65 * removed some old code.
67 * fixed mouse save off-by-one.
71 * Revision 1.45 2002/06/03 09:25:37 relnev
72 * implement mouse cursor and screen save/restore
74 * Revision 1.44 2002/06/02 18:46:59 relnev
77 * Revision 1.43 2002/06/02 11:34:00 relnev
80 * Revision 1.42 2002/06/02 10:28:17 relnev
81 * fix texture handle leak
83 * Revision 1.41 2002/06/01 09:00:34 relnev
84 * silly debug memmanager
86 * Revision 1.40 2002/06/01 07:12:33 relnev
87 * a few NDEBUG updates.
89 * removed a few warnings.
91 * Revision 1.39 2002/06/01 05:33:15 relnev
92 * copied more code over.
94 * added scissor clipping.
96 * Revision 1.38 2002/06/01 03:35:27 relnev
99 * Revision 1.37 2002/06/01 03:32:00 relnev
100 * fix texture loading mistake.
102 * enable some d3d stuff for opengl also
104 * Revision 1.36 2002/05/31 23:25:03 relnev
107 * Revision 1.34 2002/05/31 22:15:22 relnev
110 * Revision 1.33 2002/05/31 22:04:55 relnev
111 * use d3d rect_internal
113 * Revision 1.32 2002/05/31 06:28:23 relnev
116 * Revision 1.31 2002/05/31 06:04:39 relnev
119 * Revision 1.30 2002/05/31 03:56:11 theoddone33
120 * Change tmapper polygon winding and enable culling
122 * Revision 1.29 2002/05/31 03:34:02 theoddone33
126 * Revision 1.28 2002/05/31 00:06:59 relnev
129 * Revision 1.27 2002/05/30 23:46:29 theoddone33
130 * some minor key changes (not necessarily fixes)
132 * Revision 1.26 2002/05/30 23:33:12 relnev
133 * implemented a few more functions.
135 * Revision 1.25 2002/05/30 23:01:16 relnev
136 * implement gr_opengl_set_state.
138 * Revision 1.24 2002/05/30 22:12:57 relnev
139 * finish default texture case
141 * Revision 1.23 2002/05/30 22:02:30 theoddone33
144 * Revision 1.22 2002/05/30 21:44:48 relnev
145 * implemented some missing texture stuff.
147 * enable bitmap polys for opengl.
149 * work around greenness in bitmaps.
151 * Revision 1.21 2002/05/30 17:29:30 theoddone33
152 * Fix some more stubs, change at least one polygon winding since culling is now
155 * Revision 1.20 2002/05/30 16:50:24 theoddone33
156 * Keyboard partially fixed
158 * Revision 1.19 2002/05/30 08:13:14 relnev
161 * Revision 1.18 2002/05/29 23:37:36 relnev
164 * Revision 1.17 2002/05/29 23:17:49 theoddone33
165 * Non working text code and fixed keys
167 * Revision 1.16 2002/05/29 19:45:13 theoddone33
168 * More changes on texture loading
170 * Revision 1.15 2002/05/29 19:06:48 theoddone33
171 * Enable string printing. Enable texture mapping
173 * Revision 1.14 2002/05/29 08:54:40 relnev
174 * "fixed" bitmap drawing.
176 * copied more d3d code over.
178 * Revision 1.13 2002/05/29 06:25:13 theoddone33
179 * Keyboard input, mouse tracking now work
181 * Revision 1.12 2002/05/29 04:52:45 relnev
184 * Revision 1.11 2002/05/29 04:29:56 relnev
185 * removed some unncessary stubbing, implemented opengl rect
187 * Revision 1.10 2002/05/29 04:13:27 theoddone33
190 * Revision 1.9 2002/05/29 03:35:51 relnev
193 * Revision 1.8 2002/05/29 03:30:05 relnev
194 * update opengl stubs
196 * Revision 1.7 2002/05/29 02:52:32 theoddone33
197 * Enable OpenGL renderer
199 * Revision 1.6 2002/05/28 04:56:51 theoddone33
200 * runs a little bit now
202 * Revision 1.5 2002/05/28 04:07:28 theoddone33
203 * New graphics stubbing arrangement
205 * Revision 1.4 2002/05/27 23:39:34 relnev
208 * Revision 1.3 2002/05/27 22:35:01 theoddone33
211 * Revision 1.2 2002/05/27 22:32:02 theoddone33
212 * throw all d3d stuff at opengl
214 * Revision 1.1.1.1 2002/05/03 03:28:09 root
218 * 10 7/14/99 9:42a Dave
219 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
222 * 9 7/09/99 9:51a Dave
223 * Added thick polyline code.
225 * 8 6/29/99 10:35a Dave
226 * Interface polygon bitmaps! Whee!
228 * 7 2/03/99 11:44a Dave
229 * Fixed d3d transparent textures.
231 * 6 1/24/99 11:37p Dave
232 * First full rev of beam weapons. Very customizable. Removed some bogus
233 * Int3()'s in low level net code.
235 * 5 12/18/98 1:13a Dave
236 * Rough 1024x768 support for Direct3D. Proper detection and usage through
239 * 4 12/06/98 2:36p Dave
240 * Drastically improved nebula fogging.
242 * 3 11/11/98 5:37p Dave
243 * Checkin for multiplayer testing.
245 * 2 10/07/98 10:53a Dave
248 * 1 10/07/98 10:49a Dave
250 * 14 5/20/98 9:46p John
251 * added code so the places in code that change half the palette don't
252 * have to clear the screen.
254 * 13 5/06/98 5:30p John
255 * Removed unused cfilearchiver. Removed/replaced some unused/little used
256 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
257 * DirectX header files and libs that fixed the Direct3D alpha blending
260 * 12 4/14/98 12:15p John
261 * Made 16-bpp movies work.
263 * 11 3/12/98 5:36p John
264 * Took out any unused shaders. Made shader code take rgbc instead of
265 * matrix and vector since noone used it like a matrix and it would have
266 * been impossible to do in hardware. Made Glide implement a basic
267 * shader for online help.
269 * 10 3/10/98 4:18p John
270 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
271 * & Glide have popups and print screen. Took out all >8bpp software
272 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
273 * support Fred. Made zbuffering key off of functions rather than one
276 * 9 12/02/97 4:00p John
277 * Added first rev of thruster glow, along with variable levels of
278 * translucency, which retquired some restructing of palman.
280 * 8 10/03/97 9:10a John
281 * added better antialiased line drawer
283 * 7 9/23/97 10:45a John
284 * made so you can tell bitblt code to rle a bitmap by passing flag to
287 * 6 9/09/97 11:01a Sandeep
288 * fixed warning level 4 bugs
290 * 5 7/10/97 2:06p John
291 * added code to specify alphablending type for bitmaps.
293 * 4 6/17/97 7:04p John
294 * added d3d support for gradients.
295 * fixed some color bugs by adding screen signatures instead of watching
296 * flags and palette changes.
298 * 3 6/12/97 2:50a Lawrance
299 * bm_unlock() now passed bitmap number, not pointer
301 * 2 6/11/97 1:12p John
302 * Started fixing all the text colors in the game.
304 * 1 5/12/97 12:14p John
311 #include <windowsx.h>
325 #include "floating.h"
327 #include "systemvars.h"
328 #include "grinternal.h"
329 #include "gropengl.h"
333 #include "osregistry.h"
335 static int Inited = 0;
337 typedef enum gr_texture_source {
339 TEXTURE_SOURCE_DECAL,
340 TEXTURE_SOURCE_NO_FILTERING,
343 typedef enum gr_alpha_blend {
344 ALPHA_BLEND_NONE, // 1*SrcPixel + 0*DestPixel
345 ALPHA_BLEND_ADDITIVE, // 1*SrcPixel + 1*DestPixel
346 ALPHA_BLEND_ALPHA_ADDITIVE, // Alpha*SrcPixel + 1*DestPixel
347 ALPHA_BLEND_ALPHA_BLEND_ALPHA, // Alpha*SrcPixel + (1-Alpha)*DestPixel
348 ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR, // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
351 typedef enum gr_zbuffer_type {
358 #define NEBULA_COLORS 20
360 volatile int GL_activate = 0;
361 volatile int GL_deactivate = 0;
363 static int GL_use_luminance_alpha;
365 static int GL_single_buffer = 0;
368 // Throw in some dummy functions - DDOI
370 int D3D_32bit = 0; // grd3d.cpp
371 int D3D_fog_mode = -1; // grd3d.cpp
372 int D3D_inited = 0; // grd3d.cpp
373 int D3D_zbias = 1; // grd3d.cpp
374 int D3d_rendition_uvs = 0; // grd3d.cpp
381 void d3d_zbias (int a)
387 static void gr_opengl_set_texture_state(gr_texture_source ts);
389 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
390 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
392 static void gr_opengl_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
394 gr_opengl_set_texture_state(ts);
396 if (ab != GL_current_alpha_blend) {
398 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
399 glBlendFunc(GL_ONE, GL_ZERO);
401 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
402 glBlendFunc(GL_ONE, GL_ONE);
404 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
405 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
407 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
408 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
410 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
411 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
417 GL_current_alpha_blend = ab;
420 if (zt != GL_current_zbuffer_type) {
422 case ZBUFFER_TYPE_NONE:
423 glDepthFunc(GL_ALWAYS);
424 glDepthMask(GL_FALSE);
426 case ZBUFFER_TYPE_READ:
427 glDepthFunc(GL_LESS);
428 glDepthMask(GL_FALSE);
430 case ZBUFFER_TYPE_WRITE:
431 glDepthFunc(GL_ALWAYS);
432 glDepthMask(GL_TRUE);
434 case ZBUFFER_TYPE_FULL:
435 glDepthFunc(GL_LESS);
436 glDepthMask(GL_TRUE);
442 GL_current_zbuffer_type = zt;
446 void gr_opengl_activate(int active)
452 make sure window is active and mouse grabbed
458 make sure mouse is not grabbed and window minimized
466 void opengl_tcache_flush ();
468 void gr_opengl_preload_init()
470 if (gr_screen.mode != GR_OPENGL) {
474 opengl_tcache_flush ();
477 int GL_should_preload = 0;
478 int gr_opengl_preload(int bitmap_num, int is_aabitmap)
480 if ( gr_screen.mode != GR_OPENGL) {
484 if ( !GL_should_preload ) {
488 float u_scale, v_scale;
492 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 1 );
494 retval = gr_tcache_set(bitmap_num, TCACHE_TYPE_NORMAL, &u_scale, &v_scale, 1 );
498 mprintf(("Texture upload failed!\n" ));
504 void gr_opengl_pixel(int x, int y)
509 void gr_opengl_clear()
511 glClearColor(gr_screen.current_clear_color.red / 255.0,
512 gr_screen.current_clear_color.green / 255.0,
513 gr_screen.current_clear_color.blue / 255.0, 1.0);
515 glClear ( GL_COLOR_BUFFER_BIT );
518 void opengl_tcache_frame ();
519 void gr_opengl_flip()
528 GLenum error = glGetError();
531 error = glGetError();
533 if (error != GL_NO_ERROR) {
534 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d (%d this frame)\n", error, ic));
537 } while (error != GL_NO_ERROR);
540 if (GL_single_buffer) {
541 glDrawBuffer(GL_FRONT);
542 glReadBuffer(GL_BACK);
543 glCopyPixels(0, 0, gr_screen.max_w, gr_screen.max_h, GL_COLOR);
544 glDrawBuffer(GL_BACK);
546 SDL_GL_SwapBuffers ();
549 if ( mouse_is_visible() ) {
553 mouse_get_pos( &mx, &my );
555 glDrawBuffer(GL_FRONT);
557 if ( Gr_cursor == -1 ) {
560 gr_set_bitmap(Gr_cursor);
564 glDrawBuffer(GL_BACK);
567 int cnt = GL_activate;
570 opengl_tcache_flush();
571 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
577 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
580 opengl_tcache_frame ();
583 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
588 void gr_opengl_set_clip(int x,int y,int w,int h)
590 // check for sanity of parameters
596 if (x >= gr_screen.max_w)
597 x = gr_screen.max_w - 1;
598 if (y >= gr_screen.max_h)
599 y = gr_screen.max_h - 1;
601 if (x + w > gr_screen.max_w)
602 w = gr_screen.max_w - x;
603 if (y + h > gr_screen.max_h)
604 h = gr_screen.max_h - y;
606 if (w > gr_screen.max_w)
608 if (h > gr_screen.max_h)
611 gr_screen.offset_x = x;
612 gr_screen.offset_y = y;
613 gr_screen.clip_left = 0;
614 gr_screen.clip_right = w-1;
615 gr_screen.clip_top = 0;
616 gr_screen.clip_bottom = h-1;
617 gr_screen.clip_width = w;
618 gr_screen.clip_height = h;
620 glEnable(GL_SCISSOR_TEST);
621 glScissor(x, gr_screen.max_h-y-h, w, h);
624 void gr_opengl_reset_clip()
626 gr_screen.offset_x = 0;
627 gr_screen.offset_y = 0;
628 gr_screen.clip_left = 0;
629 gr_screen.clip_top = 0;
630 gr_screen.clip_right = gr_screen.max_w - 1;
631 gr_screen.clip_bottom = gr_screen.max_h - 1;
632 gr_screen.clip_width = gr_screen.max_w;
633 gr_screen.clip_height = gr_screen.max_h;
635 glDisable(GL_SCISSOR_TEST);
636 // glScissor(0, 0, gr_screen.max_w, gr_screen.max_h);
639 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
641 gr_screen.current_alpha = alpha;
642 gr_screen.current_alphablend_mode = alphablend_mode;
643 gr_screen.current_bitblt_mode = bitblt_mode;
644 gr_screen.current_bitmap = bitmap_num;
646 gr_screen.current_bitmap_sx = sx;
647 gr_screen.current_bitmap_sy = sy;
650 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
652 shade->screen_sig = gr_screen.signature;
659 void gr_opengl_set_shader( shader * shade )
662 if (shade->screen_sig != gr_screen.signature) {
663 gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
665 gr_screen.current_shader = *shade;
667 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
672 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
674 STUB_FUNCTION; /* who called me? */
678 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
680 STUB_FUNCTION; /* who called me? */
683 void gr_opengl_bitmap(int x, int y)
685 STUB_FUNCTION; /* who called me? */
688 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
692 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
694 saved_zbuf = gr_zbuffer_get();
696 // start the frame, no zbuffering, no culling
698 gr_zbuffer_set(GR_ZBUFF_NONE);
707 v[0].flags = PF_PROJECTED;
714 v[1].sx = i2fl(x + w);
719 v[1].flags = PF_PROJECTED;
726 v[2].sx = i2fl(x + w);
727 v[2].sy = i2fl(y + h);
731 v[2].flags = PF_PROJECTED;
739 v[3].sy = i2fl(y + h);
743 v[3].flags = PF_PROJECTED;
751 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
755 // restore zbuffer and culling
756 gr_zbuffer_set(saved_zbuf);
760 void gr_opengl_rect(int x,int y,int w,int h)
762 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);
765 void gr_opengl_shade(int x,int y,int w,int h)
772 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
773 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
774 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
775 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
776 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
777 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
778 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
779 if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
781 gr_opengl_rect_internal(x, y, w, h, r, g, b, a);
784 static void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
789 if ( !gr_screen.current_color.is_alphacolor ) return;
791 float u_scale, v_scale;
793 if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) ) {
794 // Couldn't set texture
795 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
799 gr_opengl_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
801 float u0, u1, v0, v1;
802 float x1, x2, y1, y2;
805 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
807 u0 = u_scale*i2fl(sx)/i2fl(bw);
808 v0 = v_scale*i2fl(sy)/i2fl(bh);
810 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
811 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
813 x1 = i2fl(x+gr_screen.offset_x);
814 y1 = i2fl(y+gr_screen.offset_y);
815 x2 = i2fl(x+w+gr_screen.offset_x);
816 y2 = i2fl(y+h+gr_screen.offset_y);
818 if ( gr_screen.current_color.is_alphacolor ) {
819 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
821 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
825 glTexCoord2f (u0, v1);
826 glVertex3f (x1, y2, -0.99);
828 glTexCoord2f (u1, v1);
829 glVertex3f (x2, y2, -0.99);
831 glTexCoord2f (u1, v0);
832 glVertex3f (x2, y1, -0.99);
834 glTexCoord2f (u0, v0);
835 glVertex3f (x1, y1, -0.99);
839 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
846 int dx1=x, dx2=x+w-1;
847 int dy1=y, dy2=y+h-1;
850 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
855 if ( count > 1 ) Int3();
859 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
860 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
861 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
862 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
863 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
864 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
891 if ( w < 1 ) return; // clipped away!
892 if ( h < 1 ) return; // clipped away!
896 // Make sure clipping algorithm works
900 Assert( w == (dx2-dx1+1) );
901 Assert( h == (dy2-dy1+1) );
904 Assert( sx+w <= bw );
905 Assert( sy+h <= bh );
906 Assert( dx2 >= dx1 );
907 Assert( dy2 >= dy1 );
908 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
909 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
910 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
911 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
914 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
915 gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
918 void gr_opengl_aabitmap(int x, int y)
922 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
923 int dx1=x, dx2=x+w-1;
924 int dy1=y, dy2=y+h-1;
927 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
928 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
929 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
930 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
931 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
932 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
934 if ( sx < 0 ) return;
935 if ( sy < 0 ) return;
936 if ( sx >= w ) return;
937 if ( sy >= h ) return;
939 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
940 gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
943 void gr_opengl_string( int sx, int sy, char *s )
945 int width, spacing, letter;
948 if ( !Current_font ) {
952 gr_set_bitmap(Current_font->bitmap_id);
957 if (sx==0x8000) { //centered
958 x = get_centered_x(s);
970 y += Current_font->h;
971 if (sx==0x8000) { //centered
972 x = get_centered_x(s);
979 letter = get_char_width(s[0],s[1],&width,&spacing);
982 //not in font, draw as space
990 // Check if this character is totally clipped
991 if ( x + width < gr_screen.clip_left ) continue;
992 if ( y + Current_font->h < gr_screen.clip_top ) continue;
993 if ( x > gr_screen.clip_right ) continue;
994 if ( y > gr_screen.clip_bottom ) continue;
997 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
998 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
1002 wc = width - xd; hc = Current_font->h - yd;
1003 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
1004 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
1006 if ( wc < 1 ) continue;
1007 if ( hc < 1 ) continue;
1009 int u = Current_font->bm_u[letter];
1010 int v = Current_font->bm_v[letter];
1012 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
1016 void gr_opengl_line(int x1,int y1,int x2,int y2)
1018 int clipped = 0, swapped=0;
1020 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1022 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);
1027 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1028 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1029 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1030 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1032 if ( x1 == x2 && y1 == y2 ) {
1033 glBegin (GL_POINTS);
1034 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1035 glVertex3f (sx1, sy1, -0.99f);
1047 } else if ( y1 == y2 ) {
1056 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
1057 glVertex3f (sx2, sy2, -0.99f);
1058 glVertex3f (sx1, sy1, -0.99f);
1062 void gr_opengl_aaline(vertex *v1, vertex *v2)
1064 gr_opengl_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
1067 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1069 int clipped = 0, swapped=0;
1071 if ( !gr_screen.current_color.is_alphacolor ) {
1072 gr_line( x1, y1, x2, y2 );
1076 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);
1078 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
1080 int aa = swapped ? 0 : gr_screen.current_color.alpha;
1081 int ba = swapped ? gr_screen.current_color.alpha : 0;
1086 sx1 = i2fl(x1 + gr_screen.offset_x)+0.5;
1087 sy1 = i2fl(y1 + gr_screen.offset_y)+0.5;
1088 sx2 = i2fl(x2 + gr_screen.offset_x)+0.5;
1089 sy2 = i2fl(y2 + gr_screen.offset_y)+0.5;
1097 } else if ( y1 == y2 ) {
1106 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, ba);
1107 glVertex3f (sx2, sy2, -0.99f);
1108 glColor4ub (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, aa);
1109 glVertex3f (sx1, sy1, -0.99f);
1113 void gr_opengl_circle( int xc, int yc, int d )
1123 if ( (xc+r) < gr_screen.clip_left ) return;
1124 if ( (xc-r) > gr_screen.clip_right ) return;
1125 if ( (yc+r) < gr_screen.clip_top ) return;
1126 if ( (yc-r) > gr_screen.clip_bottom ) return;
1129 // Draw the first octant
1130 gr_opengl_line( xc-y, yc-x, xc+y, yc-x );
1131 gr_opengl_line( xc-y, yc+x, xc+y, yc+x );
1136 // Draw the second octant
1137 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1138 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1146 gr_opengl_line( xc-x, yc-y, xc+x, yc-y );
1147 gr_opengl_line( xc-x, yc+y, xc+x, yc+y );
1152 static void gr_opengl_stuff_fog_value(float z, int *r, int *g, int *b, int *a)
1156 f_float = (gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near);
1157 if (f_float < 0.0f) {
1165 *a = (int)(f_float * 255.0);
1168 static void gr_opengl_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
1171 float u_scale = 1.0f, v_scale = 1.0f;
1173 // Make nebula use the texture mapper... this blends the colors better.
1174 if ( flags & TMAP_FLAG_NEBULA ){
1178 gr_texture_source texture_source = (gr_texture_source)-1;
1179 gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1180 gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1182 if ( gr_zbuffering ) {
1183 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
1184 zbuffer_type = ZBUFFER_TYPE_READ;
1186 zbuffer_type = ZBUFFER_TYPE_FULL;
1189 zbuffer_type = ZBUFFER_TYPE_NONE;
1194 int tmap_type = TCACHE_TYPE_NORMAL;
1198 if ( flags & TMAP_FLAG_TEXTURED ) {
1201 r = gr_screen.current_color.red;
1202 g = gr_screen.current_color.green;
1203 b = gr_screen.current_color.blue;
1206 if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )
1209 tmap_type = TCACHE_TYPE_NORMAL;
1210 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
1212 // Blend with screen pixel using src*alpha+dst
1213 float factor = gr_screen.current_alpha;
1217 if ( factor <= 1.0f ) {
1218 int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1219 r = (r*tmp_alpha)/255;
1220 g = (g*tmp_alpha)/255;
1221 b = (b*tmp_alpha)/255;
1224 tmap_type = TCACHE_TYPE_XPARENT;
1226 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1228 // Blend with screen pixel using src*alpha+dst
1229 float factor = gr_screen.current_alpha;
1231 if ( factor > 1.0f ) {
1234 alpha = fl2i(gr_screen.current_alpha*255.0f);
1238 if(Bm_pixel_format == BM_PIXEL_FORMAT_ARGB) {
1239 alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
1241 alpha_blend = ALPHA_BLEND_NONE;
1246 if(flags & TMAP_FLAG_BITMAP_SECTION){
1247 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
1250 texture_source = TEXTURE_SOURCE_NONE;
1252 if ( flags & TMAP_FLAG_TEXTURED ) {
1253 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 ))
1255 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1259 // use nonfiltered textures for bitmap sections
1260 if(flags & TMAP_FLAG_BITMAP_SECTION){
1261 texture_source = TEXTURE_SOURCE_NO_FILTERING;
1263 texture_source = TEXTURE_SOURCE_DECAL;
1268 gr_opengl_set_state( texture_source, alpha_blend, zbuffer_type );
1270 if ( flags & TMAP_FLAG_TEXTURED )
1276 if (flags & TMAP_FLAG_PIXEL_FOG) {
1282 for (i=nv-1;i>=0;i--) // DDOI - change polygon winding
1284 vertex * va = verts[i];
1288 x = fl2i(va->sx*16.0f);
1289 y = fl2i(va->sy*16.0f);
1291 x += gr_screen.offset_x*16;
1292 y += gr_screen.offset_y*16;
1294 sx = i2fl(x) / 16.0f;
1295 sy = i2fl(y) / 16.0f;
1297 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
1308 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba);
1311 glBegin(GL_TRIANGLE_FAN);
1312 for (i = nv-1; i >= 0; i--) {
1313 vertex * va = verts[i];
1319 if ( gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
1320 sz = 1.0 - 1.0 / (1.0 + va->z / (32768.0 / 256.0));
1329 if ( flags & TMAP_FLAG_CORRECT ) {
1335 if (flags & TMAP_FLAG_ALPHA) {
1341 if (flags & TMAP_FLAG_NEBULA ) {
1342 int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1343 r = gr_palette[pal*3+0];
1344 g = gr_palette[pal*3+1];
1345 b = gr_palette[pal*3+2];
1346 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) ) {
1347 r = Gr_gamma_lookup[verts[i]->b];
1348 g = Gr_gamma_lookup[verts[i]->b];
1349 b = Gr_gamma_lookup[verts[i]->b];
1350 } else if ( (flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD) ) {
1351 // Make 0.75 be 256.0f
1352 r = Gr_gamma_lookup[verts[i]->r];
1353 g = Gr_gamma_lookup[verts[i]->g];
1354 b = Gr_gamma_lookup[verts[i]->b];
1356 // use constant RGB values...
1358 glColor4ub (r,g,b,a);
1360 if((gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (D3D_fog_mode == 1)){
1363 /* this is for GL_EXT_SECONDARY_COLOR */
1364 gr_opengl_stuff_fog_value(va->z, &sr, &sg, &sb, &sa);
1365 /* do separate color call here */
1371 x = fl2i(va->sx*16.0f);
1372 y = fl2i(va->sy*16.0f);
1374 x += gr_screen.offset_x*16;
1375 y += gr_screen.offset_y*16;
1377 sx = i2fl(x) / 16.0f;
1378 sy = i2fl(y) / 16.0f;
1380 if ( flags & TMAP_FLAG_TEXTURED ) {
1383 glTexCoord2f(tu, tv);
1386 glVertex4f(sx/rhw, sy/rhw, -sz/rhw, 1.0/rhw);
1391 void gr_opengl_tmapper( int nverts, vertex **verts, uint flags )
1393 gr_opengl_tmapper_internal( nverts, verts, flags, 0 );
1396 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
1398 void gr_opengl_scaler(vertex *va, vertex *vb )
1400 float x0, y0, x1, y1;
1401 float u0, v0, u1, v1;
1402 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
1403 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
1404 float xmin, xmax, ymin, ymax;
1405 int dx0, dy0, dx1, dy1;
1407 //============= CLIP IT =====================
1409 x0 = va->sx; y0 = va->sy;
1410 x1 = vb->sx; y1 = vb->sy;
1412 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
1413 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
1415 u0 = va->u; v0 = va->v;
1416 u1 = vb->u; v1 = vb->v;
1418 // Check for obviously offscreen bitmaps...
1419 if ( (y1<=y0) || (x1<=x0) ) return;
1420 if ( (x1<xmin ) || (x0>xmax) ) return;
1421 if ( (y1<ymin ) || (y0>ymax) ) return;
1423 clipped_u0 = u0; clipped_v0 = v0;
1424 clipped_u1 = u1; clipped_v1 = v1;
1426 clipped_x0 = x0; clipped_y0 = y0;
1427 clipped_x1 = x1; clipped_y1 = y1;
1429 // Clip the left, moving u0 right as necessary
1431 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
1435 // Clip the right, moving u1 left as necessary
1437 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
1441 // Clip the top, moving v0 down as necessary
1443 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
1447 // Clip the bottom, moving v1 up as necessary
1449 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1453 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1454 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1456 if (dx1<=dx0) return;
1457 if (dy1<=dy0) return;
1459 //============= DRAW IT =====================
1473 v[1].sx = clipped_x1;
1474 v[1].sy = clipped_y0;
1477 v[1].u = clipped_u1;
1478 v[1].v = clipped_v0;
1481 v[2].sx = clipped_x1;
1482 v[2].sy = clipped_y1;
1485 v[2].u = clipped_u1;
1486 v[2].v = clipped_v1;
1489 v[3].sx = clipped_x0;
1490 v[3].sy = clipped_y1;
1493 v[3].u = clipped_u0;
1494 v[3].v = clipped_v1;
1496 gr_opengl_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1499 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1503 void gr_opengl_get_color( int * r, int * g, int * b )
1505 if (r) *r = gr_screen.current_color.red;
1506 if (g) *g = gr_screen.current_color.green;
1507 if (b) *b = gr_screen.current_color.blue;
1510 void gr_opengl_init_color(color *c, int r, int g, int b)
1512 c->screen_sig = gr_screen.signature;
1513 c->red = (unsigned char)r;
1514 c->green = (unsigned char)g;
1515 c->blue = (unsigned char)b;
1517 c->ac_type = AC_TYPE_NONE;
1519 c->is_alphacolor = 0;
1523 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1525 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1526 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1527 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1528 if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1530 gr_opengl_init_color( clr, r, g, b );
1532 clr->alpha = (unsigned char)alpha;
1533 clr->ac_type = (ubyte)type;
1534 clr->alphacolor = -1;
1535 clr->is_alphacolor = 1;
1538 void gr_opengl_set_color( int r, int g, int b )
1540 Assert((r >= 0) && (r < 256));
1541 Assert((g >= 0) && (g < 256));
1542 Assert((b >= 0) && (b < 256));
1544 gr_opengl_init_color( &gr_screen.current_color, r, g, b );
1547 void gr_opengl_set_color_fast(color *dst)
1549 if ( dst->screen_sig != gr_screen.signature ) {
1550 if ( dst->is_alphacolor ) {
1551 gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1553 gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1556 gr_screen.current_color = *dst;
1559 void gr_opengl_print_screen(char *filename)
1564 int gr_opengl_supports_res_ingame(int res)
1571 int gr_opengl_supports_res_interface(int res)
1578 void opengl_tcache_cleanup ();
1579 void gr_opengl_cleanup()
1581 if ( !Inited ) return;
1588 opengl_tcache_cleanup ();
1593 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
1595 Assert((r >= 0) && (r < 256));
1596 Assert((g >= 0) && (g < 256));
1597 Assert((b >= 0) && (b < 256));
1599 if (fog_mode == GR_FOGMODE_NONE) {
1600 if (gr_screen.current_fog_mode != fog_mode) {
1603 gr_screen.current_fog_mode = fog_mode;
1608 if (gr_screen.current_fog_mode != fog_mode) {
1611 if (D3D_fog_mode == 2) {
1612 glFogi(GL_FOG_MODE, GL_LINEAR);
1615 gr_screen.current_fog_mode = fog_mode;
1618 if ( (gr_screen.current_fog_color.red != r) ||
1619 (gr_screen.current_fog_color.green != g) ||
1620 (gr_screen.current_fog_color.blue != b) ) {
1623 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
1625 fc[0] = (float)r/255.0;
1626 fc[1] = (float)g/255.0;
1627 fc[2] = (float)b/255.0;
1630 glFogfv(GL_FOG_COLOR, fc);
1633 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
1634 ((fog_near != gr_screen.fog_near) ||
1635 (fog_far != gr_screen.fog_far)) ) {
1636 gr_screen.fog_near = fog_near;
1637 gr_screen.fog_far = fog_far;
1639 if (D3D_fog_mode == 2) {
1640 glFogf(GL_FOG_START, fog_near);
1641 glFogf(GL_FOG_END, fog_far);
1646 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1651 void gr_opengl_set_cull(int cull)
1654 glEnable (GL_CULL_FACE);
1655 glFrontFace (GL_CCW);
1657 glDisable (GL_CULL_FACE);
1661 void gr_opengl_filter_set(int filter)
1666 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1669 gr_set_bitmap(bmap1);
1672 gr_set_bitmap(bmap2);
1678 typedef struct tcache_slot_opengl {
1679 GLuint texture_handle;
1680 float u_scale, v_scale;
1683 int used_this_frame;
1688 tcache_slot_opengl *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1689 tcache_slot_opengl *parent;
1691 gr_texture_source texture_mode;
1692 } tcache_slot_opengl;
1694 static void *Texture_sections = NULL;
1695 static tcache_slot_opengl *Textures = NULL;
1697 static tcache_slot_opengl *GL_bound_texture;
1699 int GL_texture_sections = 0;
1700 int GL_texture_ram = 0;
1701 int GL_frame_count = 1;
1702 int GL_min_texture_width = 0;
1703 int GL_max_texture_width = 0;
1704 int GL_min_texture_height = 0;
1705 int GL_max_texture_height = 0;
1706 int GL_square_textures = 0;
1707 int GL_textures_in = 0;
1708 int GL_textures_in_frame = 0;
1709 int GL_last_bitmap_id = -1;
1710 int GL_last_detail = -1;
1711 int GL_last_bitmap_type = -1;
1712 int GL_last_section_x = -1;
1713 int GL_last_section_y = -1;
1717 static gr_texture_source GL_current_texture_source = (gr_texture_source) -1;
1719 static void gr_opengl_set_texture_state(gr_texture_source ts)
1721 if (ts == TEXTURE_SOURCE_NONE) {
1722 GL_bound_texture = NULL;
1724 glBindTexture(GL_TEXTURE_2D, 0);
1725 gr_tcache_set(-1, -1, NULL, NULL );
1726 } else if (GL_bound_texture &&
1727 GL_bound_texture->texture_mode != ts) {
1729 case TEXTURE_SOURCE_DECAL:
1730 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1731 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1733 case TEXTURE_SOURCE_NO_FILTERING:
1734 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1735 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1741 GL_bound_texture->texture_mode = ts;
1744 GL_current_texture_source = ts;
1747 void opengl_tcache_init (int use_sections)
1751 // DDOI - FIXME skipped a lot of stuff here
1752 GL_should_preload = 0;
1754 //uint tmp_pl = os_config_read_uint( NULL, NOX("D3DPreloadTextures"), 255 );
1757 if ( tmp_pl == 0 ) {
1758 GL_should_preload = 0;
1759 } else if ( tmp_pl == 1 ) {
1760 GL_should_preload = 1;
1762 GL_should_preload = 1;
1767 GL_min_texture_width = 16;
1768 GL_min_texture_height = 16;
1769 GL_max_texture_width = 256;
1770 GL_max_texture_height = 256;
1772 GL_square_textures = 1;
1774 Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1780 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1781 if(!Texture_sections){
1784 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1787 // Init the texture structures
1788 int section_count = 0;
1789 for( i=0; i<MAX_BITMAPS; i++ ) {
1790 Textures[i].texture_handle = 0;
1791 Textures[i].bitmap_id = -1;
1792 Textures[i].size = 0;
1793 Textures[i].used_this_frame = 0;
1795 Textures[i].parent = NULL;
1797 // allocate sections
1799 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1800 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1801 Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1802 Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1803 Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1804 Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1805 Textures[i].data_sections[idx][s_idx]->size = 0;
1806 Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1810 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1811 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1812 Textures[i].data_sections[idx][s_idx] = NULL;
1818 GL_texture_sections = use_sections;
1820 //GL_last_detail = Detail.hardware_textures;
1821 GL_last_bitmap_id = -1;
1822 GL_last_bitmap_type = -1;
1824 GL_last_section_x = -1;
1825 GL_last_section_y = -1;
1828 GL_textures_in_frame = 0;
1831 int opengl_free_texture (tcache_slot_opengl *t);
1833 void opengl_free_texture_with_handle(int handle)
1835 for(int i=0; i<MAX_BITMAPS; i++ ) {
1836 if (Textures[i].bitmap_id == handle) {
1837 Textures[i].used_this_frame = 0; // this bmp doesn't even exist any longer...
1838 opengl_free_texture ( &Textures[i] );
1843 void opengl_tcache_flush ()
1847 for( i=0; i<MAX_BITMAPS; i++ ) {
1848 opengl_free_texture ( &Textures[i] );
1850 if (GL_textures_in != 0) {
1851 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1855 GL_last_bitmap_id = -1;
1856 GL_last_section_x = -1;
1857 GL_last_section_y = -1;
1860 void opengl_tcache_cleanup ()
1862 opengl_tcache_flush ();
1865 GL_textures_in_frame = 0;
1872 if( Texture_sections != NULL ){
1873 free(Texture_sections);
1874 Texture_sections = NULL;
1878 void opengl_tcache_frame ()
1880 GL_last_bitmap_id = -1;
1881 GL_textures_in_frame = 0;
1883 if ((unsigned)GL_frame_count == 0xFFFFFFFF) {
1884 mprintf(("TODO: unhandled wraparound..."));
1890 opengl_tcache_flush();
1895 int opengl_free_texture ( tcache_slot_opengl *t )
1900 if ( t->bitmap_id > -1 ) {
1901 // if I, or any of my children have been used this frame, bail
1902 if(t->used_this_frame == GL_frame_count){
1905 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1906 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1907 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame == GL_frame_count)){
1913 // ok, now we know its legal to free everything safely
1914 t->texture_mode = (gr_texture_source) -1;
1915 if (t->texture_handle) {
1916 glDeleteTextures (1, &t->texture_handle);
1917 t->texture_handle = 0;
1920 if ( GL_last_bitmap_id == t->bitmap_id ) {
1921 GL_last_bitmap_id = -1;
1924 // if this guy has children, free them too, since the children
1925 // actually make up his size
1926 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1927 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1928 if(t->data_sections[idx][s_idx] != NULL){
1929 opengl_free_texture(t->data_sections[idx][s_idx]);
1935 t->used_this_frame = 0;
1936 GL_textures_in -= t->size;
1942 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1947 if((w_out == NULL) || (h_out == NULL)){
1957 for (i=0; i<16; i++ ) {
1958 if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) ) {
1964 for (i=0; i<16; i++ ) {
1965 if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) ) {
1972 if ( tex_w < GL_min_texture_width ) {
1973 tex_w = GL_min_texture_width;
1974 } else if ( tex_w > GL_max_texture_width ) {
1975 tex_w = GL_max_texture_width;
1978 if ( tex_h < GL_min_texture_height ) {
1979 tex_h = GL_min_texture_height;
1980 } else if ( tex_h > GL_max_texture_height ) {
1981 tex_h = GL_max_texture_height;
1984 if ( GL_square_textures ) {
1986 // Make the both be equal to larger of the two
1987 new_size = max(tex_w, tex_h);
1992 // store the outgoing size
1997 // data == start of bitmap data
1998 // sx == x offset into bitmap
1999 // sy == y offset into bitmap
2000 // src_w == absolute width of section on source bitmap
2001 // src_h == absolute height of section on source bitmap
2002 // bmap_w == width of source bitmap
2003 // bmap_h == height of source bitmap
2004 // tex_w == width of final texture
2005 // tex_h == height of final texture
2006 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)
2015 if ( t->used_this_frame == GL_frame_count ) {
2016 mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" ));
2022 if(!opengl_free_texture(t)){
2027 // get final texture size
2028 opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
2030 if ( (tex_w < 1) || (tex_h < 1) ) {
2031 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
2035 if ( bitmap_type == TCACHE_TYPE_AABITMAP ) {
2036 t->u_scale = (float)bmap_w / (float)tex_w;
2037 t->v_scale = (float)bmap_h / (float)tex_h;
2038 } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2039 t->u_scale = (float)src_w / (float)tex_w;
2040 t->v_scale = (float)src_h / (float)tex_h;
2047 glGenTextures (1, &t->texture_handle);
2050 if (t->texture_handle == 0) {
2051 nprintf(("Error", "!!DEBUG!! t->texture_handle == 0"));
2055 GL_bound_texture = t;
2057 GL_bound_texture->texture_mode = (gr_texture_source) -1;
2059 glBindTexture (GL_TEXTURE_2D, t->texture_handle);
2061 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2062 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2063 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2065 /* this should be set next anyway */
2066 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2067 // gr_opengl_set_texture_state(GL_current_texture_source);
2070 switch (bitmap_type) {
2072 case TCACHE_TYPE_AABITMAP:
2075 ubyte *bmp_data = ((ubyte*)data);
2081 for (i=0; i<16; i++) {
2082 xlat[i] = Gr_gamma_lookup[(i*255)/15];
2085 for ( ; i<256; i++ ) {
2089 if (GL_use_luminance_alpha) {
2090 texmem = (ubyte *) malloc (tex_w*tex_h*2);
2093 for (i=0;i<tex_h;i++)
2095 for (j=0;j<tex_w;j++)
2097 if (i < bmap_h && j < bmap_w) {
2099 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2107 glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, tex_w, tex_h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texmem);
2111 texmem = (ubyte *) malloc (tex_w*tex_h*4);
2114 for (i=0;i<tex_h;i++)
2116 for (j=0;j<tex_w;j++)
2118 if (i < bmap_h && j < bmap_w) {
2119 *texmemp++ = 0xff; /* R */
2120 *texmemp++ = 0xff; /* G */
2121 *texmemp++ = 0xff; /* B */
2122 *texmemp++ = xlat[bmp_data[i*bmap_w+j]];
2132 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texmem);
2138 case TCACHE_TYPE_BITMAP_SECTION:
2141 ubyte *bmp_data = ((ubyte*)data);
2142 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2143 ubyte *texmemp = texmem;
2145 for (i=0;i<tex_h;i++)
2147 for (j=0;j<tex_w;j++)
2149 if (i < src_h && j < src_w) {
2150 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+0];
2151 *texmemp++ = bmp_data[((i+sy)*bmap_w+(j+sx))*2+1];
2158 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2159 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2167 ubyte *bmp_data = ((ubyte*)data);
2168 ubyte *texmem = (ubyte *) malloc (tex_w*tex_h*2);
2169 ubyte *texmemp = texmem;
2171 fix u, utmp, v, du, dv;
2175 du = ( (bmap_w-1)*F1_0 ) / tex_w;
2176 dv = ( (bmap_h-1)*F1_0 ) / tex_h;
2178 for (j=0;j<tex_h;j++)
2181 for (i=0;i<tex_w;i++)
2183 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+0];
2184 *texmemp++ = bmp_data[(f2i(v)*bmap_w+f2i(utmp))*2+1];
2190 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_BGRA,
2191 GL_UNSIGNED_SHORT_1_5_5_5_REV, texmem);
2199 t->bitmap_id = texture_handle;
2200 t->time_created = GL_frame_count;
2201 t->used_this_frame = 0;
2202 t->size = tex_w * tex_h * 2;
2203 t->w = (ushort)tex_w;
2204 t->h = (ushort)tex_h;
2205 GL_textures_in_frame += t->size;
2207 GL_textures_in += t->size;
2213 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
2217 int final_w, final_h;
2221 // setup texture/bitmap flags
2223 switch(bitmap_type){
2224 case TCACHE_TYPE_AABITMAP:
2225 flags |= BMP_AABITMAP;
2228 case TCACHE_TYPE_NORMAL:
2229 flags |= BMP_TEX_OTHER;
2230 case TCACHE_TYPE_XPARENT:
2231 flags |= BMP_TEX_XPARENT;
2233 case TCACHE_TYPE_NONDARKENING:
2235 flags |= BMP_TEX_NONDARK;
2239 // lock the bitmap into the proper format
2240 bmp = bm_lock(bitmap_handle, bpp, flags);
2241 if ( bmp == NULL ) {
2242 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2251 if ( bitmap_type != TCACHE_TYPE_AABITMAP ) {
2252 // max_w /= D3D_texture_divider;
2253 // max_h /= D3D_texture_divider;
2255 // Detail.debris_culling goes from 0 to 4.
2256 max_w /= (16 >> Detail.hardware_textures);
2257 max_h /= (16 >> Detail.hardware_textures);
2261 // get final texture size as it will be allocated as a DD surface
2262 opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h);
2264 // if this tcache slot has no bitmap
2265 if ( tslot->bitmap_id < 0) {
2268 // different bitmap altogether - determine if the new one can use the old one's slot
2269 else if (tslot->bitmap_id != bitmap_handle) {
2270 if((final_w == tslot->w) && (final_h == tslot->h)){
2272 // mprintf(("Reloading texture %d\n", bitmap_handle));
2279 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);
2281 // unlock the bitmap
2282 bm_unlock(bitmap_handle);
2287 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
2291 int final_w, final_h;
2292 int section_x, section_y;
2295 // setup texture/bitmap flags
2296 Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
2297 if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
2298 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
2300 flags = BMP_TEX_XPARENT;
2302 // lock the bitmap in the proper format
2303 bmp = bm_lock(bitmap_handle, 16, flags);
2304 if ( bmp == NULL ) {
2305 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
2308 // determine the width and height of this section
2309 bm_get_section_size(bitmap_handle, sx, sy, §ion_x, §ion_y);
2311 // get final texture size as it will be allocated as an opengl texture
2312 opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
2314 // if this tcache slot has no bitmap
2315 if ( tslot->bitmap_id < 0) {
2318 // different bitmap altogether - determine if the new one can use the old one's slot
2319 else if (tslot->bitmap_id != bitmap_handle) {
2320 if((final_w == tslot->w) && (final_h == tslot->h)){
2328 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);
2330 // unlock the bitmap
2331 bm_unlock(bitmap_handle);
2337 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
2338 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)
2347 GL_last_bitmap_id = -1;
2351 if ( GL_last_detail != Detail.hardware_textures ) {
2352 GL_last_detail = Detail.hardware_textures;
2353 opengl_tcache_flush();
2360 int n = bm_get_cache_slot (bitmap_id, 1);
2361 tcache_slot_opengl *t = &Textures[n];
2363 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)) {
2364 t->used_this_frame = GL_frame_count;
2366 // mark all children as used
2367 if(GL_texture_sections){
2368 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
2369 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
2370 if(t->data_sections[idx][s_idx] != NULL){
2371 t->data_sections[idx][s_idx]->used_this_frame = GL_frame_count;
2377 *u_scale = t->u_scale;
2378 *v_scale = t->v_scale;
2382 if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
2383 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
2384 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
2390 // if the texture sections haven't been created yet
2391 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
2393 // lock the bitmap in the proper format
2394 bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
2395 bm_unlock(bitmap_id);
2397 // now lets do something for each texture
2399 for(idx=0; idx<bmp->sections.num_x; idx++){
2400 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
2401 // hmm. i'd rather we didn't have to do it this way...
2402 if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
2406 // not used this frame
2407 t->data_sections[idx][s_idx]->used_this_frame = 0;
2411 // zero out pretty much everything in the parent struct since he's just the root
2412 t->bitmap_id = bitmap_id;
2413 t->texture_handle = 0;
2414 t->time_created = t->data_sections[sx][sy]->time_created;
2415 t->used_this_frame = 0;
2418 // argh. we failed to upload. free anything we can
2420 opengl_free_texture(t);
2422 // swap in the texture we want
2424 t = t->data_sections[sx][sy];
2427 // all other "normal" textures
2428 else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
2429 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
2432 // everything went ok
2433 if(ret_val && (t->texture_handle) && !vram_full){
2434 *u_scale = t->u_scale;
2435 *v_scale = t->v_scale;
2437 GL_bound_texture = t;
2439 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
2441 /* this should be set next anyway */
2442 // if (GL_current_texture_source != TEXTURE_SOURCE_NONE) {
2443 // gr_opengl_set_texture_state(GL_current_texture_source);
2446 GL_last_bitmap_id = t->bitmap_id;
2447 GL_last_bitmap_type = bitmap_type;
2448 GL_last_section_x = sx;
2449 GL_last_section_y = sy;
2451 t->used_this_frame = GL_frame_count;
2455 GL_last_bitmap_id = -1;
2456 GL_last_bitmap_type = -1;
2458 GL_last_section_x = -1;
2459 GL_last_section_y = -1;
2461 GL_bound_texture = NULL;
2463 glBindTexture (GL_TEXTURE_2D, 0); // test - DDOI
2470 void gr_opengl_set_clear_color(int r, int g, int b)
2472 gr_init_color (&gr_screen.current_clear_color, r, g, b);
2475 void gr_opengl_flash(int r, int g, int b)
2481 if ( r || g || b ) {
2482 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
2484 float x1, x2, y1, y2;
2485 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
2486 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
2487 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
2488 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
2490 glColor4ub(r, g, b, 255);
2492 glVertex3f (x1, y2, -0.99);
2494 glVertex3f (x2, y2, -0.99);
2496 glVertex3f (x2, y1, -0.99);
2498 glVertex3f (x1, y1, -0.99);
2503 int gr_opengl_zbuffer_get()
2505 if ( !gr_global_zbuffering ) {
2506 return GR_ZBUFF_NONE;
2508 return gr_zbuffering_mode;
2511 int gr_opengl_zbuffer_set(int mode)
2513 int tmp = gr_zbuffering_mode;
2515 gr_zbuffering_mode = mode;
2517 if (gr_zbuffering_mode == GR_ZBUFF_NONE ) {
2525 void gr_opengl_zbuffer_clear(int mode)
2529 gr_zbuffering_mode = GR_ZBUFF_FULL;
2530 gr_global_zbuffering = 1;
2532 gr_opengl_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
2533 glClear ( GL_DEPTH_BUFFER_BIT );
2536 gr_zbuffering_mode = GR_ZBUFF_NONE;
2537 gr_global_zbuffering = 0;
2541 void gr_opengl_set_gamma(float gamma)
2544 Gr_gamma_int = int (Gr_gamma*10);
2546 // Create the Gamma lookup table
2548 for (i=0;i<256; i++) {
2549 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2552 } else if ( v < 0 ) {
2555 Gr_gamma_lookup[i] = v;
2558 // Flush any existing textures
2559 opengl_tcache_flush();
2562 void gr_opengl_fade_in(int instantaneous)
2567 void gr_opengl_fade_out(int instantaneous)
2572 void gr_opengl_get_region(int front, int w, int h, ubyte *data)
2575 glReadBuffer(GL_FRONT);
2577 glReadBuffer(GL_BACK);
2580 gr_opengl_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
2582 glPixelStorei(GL_UNPACK_ROW_LENGTH, gr_screen.max_w);
2584 if (gr_screen.bits_per_pixel == 15) {
2585 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
2586 } else if (gr_screen.bits_per_pixel == 32) {
2587 glReadPixels(0, gr_screen.max_h-h-1, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
2590 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2593 /* TODO - screen isn't really restored... */
2594 int gr_opengl_save_screen()
2596 if (GL_single_buffer) {
2597 mprintf(("gr_opengl_save_screen: screen already saved...\n"));
2600 GL_single_buffer = 1;
2605 void gr_opengl_restore_screen(int id)
2607 GL_single_buffer = 0;
2610 void gr_opengl_free_screen(int id)
2614 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
2619 void gr_opengl_dump_frame_stop()
2624 void gr_opengl_dump_frame()
2629 uint gr_opengl_lock()
2636 void gr_opengl_unlock()
2640 void opengl_zbias(int bias)
2643 glEnable(GL_POLYGON_OFFSET_FILL);
2644 glPolygonOffset(0.0, -bias);
2646 glDisable(GL_POLYGON_OFFSET_FILL);
2650 extern char *Osreg_title;
2651 void gr_opengl_init()
2654 gr_opengl_cleanup();
2658 mprintf(( "Initializing opengl graphics device...\n" ));
2662 if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
2664 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
2670 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
2671 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
2672 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
2673 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
2674 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
2676 int flags = SDL_OPENGL;
2678 if (os_config_read_uint( NULL, "Fullscreen", 0 ) == 1)
2679 flags |= SDL_FULLSCREEN;
2681 if (SDL_SetVideoMode (gr_screen.max_w, gr_screen.max_h,0,flags) == NULL)
2683 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
2688 SDL_WM_SetCaption (Osreg_title, "FS2");
2690 /* might as well put this here */
2691 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2693 GL_use_luminance_alpha = os_config_read_uint(NOX("OpenGL"), NOX("UseLuminanceAlpha"), 0);
2695 glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
2697 glMatrixMode(GL_PROJECTION);
2699 glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, 0.0, 1.0);
2700 glMatrixMode(GL_MODELVIEW);
2703 glShadeModel(GL_SMOOTH);
2704 glEnable(GL_DITHER);
2705 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2706 glHint(GL_FOG_HINT, GL_NICEST);
2708 glEnable(GL_DEPTH_TEST);
2711 glEnable(GL_TEXTURE_2D);
2713 glDepthRange(0.0, 1.0);
2715 glPixelStorei(GL_PACK_ALIGNMENT, 1);
2716 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2717 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2719 D3D_32bit = 1; // grd3d.cpp
2720 extern int D3D_enabled;
2723 TODO: set fog_mode to 1 if EXT_secondary_color found and wanted
2724 1 = use secondary color ext
2725 2 = use opengl linear fog
2727 D3D_fog_mode = 2; // grd3d.cpp
2731 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
2738 gr_screen.bits_per_pixel = 15;
2739 gr_screen.bytes_per_pixel = 2;
2744 Gr_red.mask = 0x7C00;
2749 Gr_green.mask = 0x3E0;
2754 Gr_blue.mask = 0x1F;
2759 gr_screen.bits_per_pixel = 16;
2760 gr_screen.bytes_per_pixel = 2;
2765 Gr_red.mask = 0xF800;
2770 Gr_green.mask = 0x7E0;
2775 Gr_blue.mask = 0x1F;
2780 gr_screen.bits_per_pixel = 32;
2781 gr_screen.bytes_per_pixel = 4;
2786 Gr_red.mask = 0xff0000;
2791 Gr_green.mask = 0xff00;
2796 Gr_blue.mask = 0xff;
2801 Int3(); // Illegal bpp
2804 // DDOI - set these so no one else does!
2805 Gr_ta_red.mask = 0x0f00;
2806 Gr_ta_red.shift = 8;
2807 Gr_ta_red.scale = 16;
2809 Gr_ta_green.mask = 0x00f0;
2810 Gr_ta_green.shift = 4;
2811 Gr_ta_green.scale = 16;
2813 Gr_ta_blue.mask = 0x000f;
2814 Gr_ta_blue.shift = 0;
2815 Gr_ta_blue.scale = 16;
2817 Gr_ta_alpha.mask = 0xf000;
2818 Gr_ta_alpha.shift = 12;
2819 Gr_ta_alpha.scale = 16;
2822 opengl_tcache_init (1);
2825 Gr_current_red = &Gr_red;
2826 Gr_current_blue = &Gr_blue;
2827 Gr_current_green = &Gr_green;
2828 Gr_current_alpha = &Gr_alpha;
2830 gr_screen.gf_flip = gr_opengl_flip;
2831 gr_screen.gf_flip_window = gr_opengl_flip_window;
2832 gr_screen.gf_set_clip = gr_opengl_set_clip;
2833 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2834 gr_screen.gf_set_font = grx_set_font;
2836 gr_screen.gf_set_color = gr_opengl_set_color;
2837 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2838 gr_screen.gf_create_shader = gr_opengl_create_shader;
2839 gr_screen.gf_set_shader = gr_opengl_set_shader;
2840 gr_screen.gf_clear = gr_opengl_clear;
2841 // gr_screen.gf_bitmap = gr_opengl_bitmap;
2842 // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2843 gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2844 gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2846 gr_screen.gf_rect = gr_opengl_rect;
2847 gr_screen.gf_shade = gr_opengl_shade;
2848 gr_screen.gf_string = gr_opengl_string;
2849 gr_screen.gf_circle = gr_opengl_circle;
2851 gr_screen.gf_line = gr_opengl_line;
2852 gr_screen.gf_aaline = gr_opengl_aaline;
2853 gr_screen.gf_pixel = gr_opengl_pixel;
2854 gr_screen.gf_scaler = gr_opengl_scaler;
2855 gr_screen.gf_tmapper = gr_opengl_tmapper;
2857 gr_screen.gf_gradient = gr_opengl_gradient;
2859 gr_screen.gf_set_palette = gr_opengl_set_palette;
2860 gr_screen.gf_get_color = gr_opengl_get_color;
2861 gr_screen.gf_init_color = gr_opengl_init_color;
2862 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
2863 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
2864 gr_screen.gf_print_screen = gr_opengl_print_screen;
2866 gr_screen.gf_fade_in = gr_opengl_fade_in;
2867 gr_screen.gf_fade_out = gr_opengl_fade_out;
2868 gr_screen.gf_flash = gr_opengl_flash;
2870 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
2871 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
2872 gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
2874 gr_screen.gf_save_screen = gr_opengl_save_screen;
2875 gr_screen.gf_restore_screen = gr_opengl_restore_screen;
2876 gr_screen.gf_free_screen = gr_opengl_free_screen;
2878 gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
2879 gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
2880 gr_screen.gf_dump_frame = gr_opengl_dump_frame;
2882 gr_screen.gf_set_gamma = gr_opengl_set_gamma;
2884 gr_screen.gf_lock = gr_opengl_lock;
2885 gr_screen.gf_unlock = gr_opengl_unlock;
2887 gr_screen.gf_fog_set = gr_opengl_fog_set;
2889 gr_screen.gf_get_region = gr_opengl_get_region;
2891 gr_screen.gf_get_pixel = gr_opengl_get_pixel;
2893 gr_screen.gf_set_cull = gr_opengl_set_cull;
2895 gr_screen.gf_cross_fade = gr_opengl_cross_fade;
2897 gr_screen.gf_filter_set = gr_opengl_filter_set;
2899 gr_screen.gf_tcache_set = gr_opengl_tcache_set;
2901 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;