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
11 #include "gropenglinternal.h"
17 #include "grinternal.h"
22 int GL_one_inited = 0;
25 volatile int GL_activate = 0;
26 volatile int GL_deactivate = 0;
28 static GLuint Gr_saved_screen_tex = 0;
30 static int Gr_opengl_mouse_saved = 0;
31 static int Gr_opengl_mouse_saved_x = 0;
32 static int Gr_opengl_mouse_saved_y = 0;
33 static int Gr_opengl_mouse_saved_w = 0;
34 static int Gr_opengl_mouse_saved_h = 0;
35 static ubyte *Gr_opengl_mouse_saved_data = NULL;
38 PFNGLSECONDARYCOLORPOINTERPROC vglSecondaryColorPointer = NULL;
41 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
42 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
44 void opengl1_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
46 opengl1_set_texture_state(ts);
48 if (ab != GL_current_alpha_blend) {
50 case ALPHA_BLEND_NONE: // 1*SrcPixel + 0*DestPixel
51 glBlendFunc(GL_ONE, GL_ZERO);
53 case ALPHA_BLEND_ADDITIVE: // 1*SrcPixel + 1*DestPixel
54 glBlendFunc(GL_ONE, GL_ONE);
56 case ALPHA_BLEND_ALPHA_ADDITIVE: // Alpha*SrcPixel + 1*DestPixel
57 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
59 case ALPHA_BLEND_ALPHA_BLEND_ALPHA: // Alpha*SrcPixel + (1-Alpha)*DestPixel
60 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
62 case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
63 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
69 GL_current_alpha_blend = ab;
72 if (zt != GL_current_zbuffer_type) {
74 case ZBUFFER_TYPE_NONE:
75 glDepthFunc(GL_ALWAYS);
76 glDepthMask(GL_FALSE);
78 case ZBUFFER_TYPE_READ:
80 glDepthMask(GL_FALSE);
82 case ZBUFFER_TYPE_WRITE:
83 glDepthFunc(GL_ALWAYS);
86 case ZBUFFER_TYPE_FULL:
94 GL_current_zbuffer_type = zt;
98 void opengl1_cleanup()
100 if ( !GL_one_inited ) {
104 gr_opengl1_reset_clip();
108 opengl1_tcache_cleanup();
113 static void opengl1_init_func_pointers()
115 gr_screen.gf_flip = gr_opengl1_flip;
116 gr_screen.gf_set_clip = gr_opengl1_set_clip;
117 gr_screen.gf_reset_clip = gr_opengl1_reset_clip;
119 gr_screen.gf_clear = gr_opengl1_clear;
121 gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
122 gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
124 gr_screen.gf_rect = gr_opengl1_rect;
125 gr_screen.gf_shade = gr_opengl1_shade;
126 gr_screen.gf_string = gr_opengl1_string;
127 gr_screen.gf_circle = gr_opengl1_circle;
129 gr_screen.gf_line = gr_opengl1_line;
130 gr_screen.gf_aaline = gr_opengl1_aaline;
131 gr_screen.gf_pixel = gr_opengl1_pixel;
132 gr_screen.gf_scaler = gr_opengl1_scaler;
133 gr_screen.gf_tmapper = gr_opengl1_tmapper;
135 gr_screen.gf_gradient = gr_opengl1_gradient;
137 gr_screen.gf_print_screen = gr_opengl1_print_screen;
139 gr_screen.gf_fade_in = gr_opengl1_fade_in;
140 gr_screen.gf_fade_out = gr_opengl1_fade_out;
141 gr_screen.gf_flash = gr_opengl1_flash;
143 gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
145 gr_screen.gf_save_screen = gr_opengl1_save_screen;
146 gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
147 gr_screen.gf_free_screen = gr_opengl1_free_screen;
149 gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
150 gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
151 gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
153 gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
155 gr_screen.gf_lock = gr_opengl1_lock;
156 gr_screen.gf_unlock = gr_opengl1_unlock;
158 gr_screen.gf_fog_set = gr_opengl1_fog_set;
160 gr_screen.gf_get_region = gr_opengl1_get_region;
162 gr_screen.gf_set_cull = gr_opengl1_set_cull;
164 gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
166 gr_screen.gf_preload_init = gr_opengl1_preload_init;
167 gr_screen.gf_preload = gr_opengl1_preload;
169 gr_screen.gf_zbias = gr_opengl1_zbias;
171 gr_screen.gf_force_windowed = gr_opengl_force_windowed;
172 gr_screen.gf_force_fullscreen = gr_opengl_force_fullscreen;
173 gr_screen.gf_toggle_fullscreen = gr_opengl_toggle_fullscreen;
175 gr_screen.gf_set_viewport = gr_opengl1_set_viewport;
177 gr_screen.gf_activate = gr_opengl1_activate;
179 gr_screen.gf_release_texture = gr_opengl1_release_texture;
189 1 = use secondary color ext
190 2 = use opengl linear fog
194 // only available with OpenGL 1.2+, must get ptr for Windows
195 vglSecondaryColorPointer = (PFNGLSECONDARYCOLORPOINTERPROC)SDL_GL_GetProcAddress("glSecondaryColorPointer");
197 if (vglSecondaryColorPointer) {
201 mprintf((" Fog mode: %s\n", (OGL_fog_mode == 1) ? "secondary color" : "linear"));
203 glShadeModel(GL_SMOOTH);
205 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
206 glHint(GL_FOG_HINT, GL_NICEST);
208 glEnable(GL_DEPTH_TEST);
211 glEnable(GL_TEXTURE_2D);
213 glDepthRange(0.0, 1.0);
215 glPixelStorei(GL_PACK_ALIGNMENT, 1);
216 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
220 opengl1_init_func_pointers();
221 opengl1_tcache_init();
228 void gr_opengl1_activate(int active)
233 // don't grab key/mouse if cmdline says so or if we're fullscreen
234 // if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
235 // SDL_WM_GrabInput(SDL_GRAB_ON);
240 // let go of mouse/keyboard
241 // SDL_WM_GrabInput(SDL_GRAB_OFF);
245 void gr_opengl1_clear()
247 glClearColor(gr_screen.current_clear_color.red / 255.0f,
248 gr_screen.current_clear_color.green / 255.0f,
249 gr_screen.current_clear_color.blue / 255.0f, 1.0f);
251 glClear( GL_COLOR_BUFFER_BIT );
254 void gr_opengl1_flip()
256 if ( !GL_one_inited ) {
264 Gr_opengl_mouse_saved = 0;
266 if ( mouse_is_visible() ) {
270 mouse_get_pos( &mx, &my );
272 gr_opengl1_save_mouse_area(mx,my,32,32);
274 if (Gr_cursor == -1) {
276 gr_set_color(255,255,255);
277 gr_line(mx, my, mx+7, my + 7);
278 gr_line(mx, my, mx+5, my );
279 gr_line(mx, my, mx, my+5);
282 gr_set_bitmap(Gr_cursor);
288 GLenum error = GL_NO_ERROR;
291 error = glGetError();
293 if (error != GL_NO_ERROR) {
294 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
296 } while (error != GL_NO_ERROR);
299 SDL_GL_SwapWindow(GL_window);
301 opengl1_tcache_frame();
303 int cnt = GL_activate;
306 opengl1_tcache_flush();
307 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
313 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
317 void gr_opengl1_set_clip(int x,int y,int w,int h)
319 // check for sanity of parameters
325 if (x >= gr_screen.max_w)
326 x = gr_screen.max_w - 1;
327 if (y >= gr_screen.max_h)
328 y = gr_screen.max_h - 1;
330 if (x + w > gr_screen.max_w)
331 w = gr_screen.max_w - x;
332 if (y + h > gr_screen.max_h)
333 h = gr_screen.max_h - y;
335 if (w > gr_screen.max_w)
337 if (h > gr_screen.max_h)
340 gr_screen.offset_x = x;
341 gr_screen.offset_y = y;
342 gr_screen.clip_left = 0;
343 gr_screen.clip_right = w-1;
344 gr_screen.clip_top = 0;
345 gr_screen.clip_bottom = h-1;
346 gr_screen.clip_width = w;
347 gr_screen.clip_height = h;
349 x = fl2i((x * GL_viewport_scale_w) + 0.5f) + GL_viewport_x;
350 y = fl2i((y * GL_viewport_scale_h) + 0.5f) + GL_viewport_y;
351 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
352 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
354 glEnable(GL_SCISSOR_TEST);
355 glScissor(x, GL_viewport_h-y-h, w, h);
358 void gr_opengl1_reset_clip()
360 gr_screen.offset_x = 0;
361 gr_screen.offset_y = 0;
362 gr_screen.clip_left = 0;
363 gr_screen.clip_top = 0;
364 gr_screen.clip_right = gr_screen.max_w - 1;
365 gr_screen.clip_bottom = gr_screen.max_h - 1;
366 gr_screen.clip_width = gr_screen.max_w;
367 gr_screen.clip_height = gr_screen.max_h;
369 glDisable(GL_SCISSOR_TEST);
372 void gr_opengl1_print_screen(const char *filename)
374 char tmp[MAX_FILENAME_LEN];
377 SDL_strlcpy( tmp, filename, sizeof(tmp) );
378 SDL_strlcat( tmp, NOX(".tga"), sizeof(tmp) );
380 buf = (ubyte*)malloc(GL_viewport_w * GL_viewport_h * 3);
386 CFILE *f = cfopen(tmp, "wb", CFILE_NORMAL, CF_TYPE_ROOT);
393 // Write the TGA header
394 cfwrite_ubyte( 0, f ); // IDLength;
395 cfwrite_ubyte( 0, f ); // ColorMapType;
396 cfwrite_ubyte( 2, f ); // ImageType; // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
397 cfwrite_ushort( 0, f ); // CMapStart;
398 cfwrite_ushort( 0, f ); // CMapLength;
399 cfwrite_ubyte( 0, f ); // CMapDepth;
400 cfwrite_ushort( 0, f ); // XOffset;
401 cfwrite_ushort( 0, f ); // YOffset;
402 cfwrite_ushort( (ushort)GL_viewport_w, f ); // Width;
403 cfwrite_ushort( (ushort)GL_viewport_h, f ); // Height;
404 cfwrite_ubyte( 24, f ); //PixelDepth;
405 cfwrite_ubyte( 0, f ); //ImageDesc;
407 memset(buf, 0, GL_viewport_w * GL_viewport_h * 3);
409 glReadPixels(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h, GL_BGR, GL_UNSIGNED_BYTE, buf);
411 cfwrite(buf, GL_viewport_w * GL_viewport_h * 3, 1, f);
418 void gr_opengl1_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
420 SDL_assert((r >= 0) && (r < 256));
421 SDL_assert((g >= 0) && (g < 256));
422 SDL_assert((b >= 0) && (b < 256));
424 if (fog_mode == GR_FOGMODE_NONE) {
425 if (gr_screen.current_fog_mode != fog_mode) {
428 if (OGL_fog_mode == 1) {
429 glDisable(GL_COLOR_SUM);
433 gr_screen.current_fog_mode = fog_mode;
438 if (gr_screen.current_fog_mode != fog_mode) {
441 if (OGL_fog_mode == 1) {
442 glEnable(GL_COLOR_SUM);
443 } else if (OGL_fog_mode == 2) {
444 glFogi(GL_FOG_MODE, GL_LINEAR);
447 gr_screen.current_fog_mode = fog_mode;
450 if ( (gr_screen.current_fog_color.red != r) ||
451 (gr_screen.current_fog_color.green != g) ||
452 (gr_screen.current_fog_color.blue != b) ) {
455 gr_init_color( &gr_screen.current_fog_color, r, g, b );
462 glFogfv(GL_FOG_COLOR, fc);
465 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
466 ((fog_near != gr_screen.fog_near) ||
467 (fog_far != gr_screen.fog_far)) ) {
468 gr_screen.fog_near = fog_near;
469 gr_screen.fog_far = fog_far;
471 if (OGL_fog_mode == 2) {
472 glFogf(GL_FOG_START, fog_near);
473 glFogf(GL_FOG_END, fog_far);
478 void gr_opengl1_set_cull(int cull)
481 glEnable (GL_CULL_FACE);
482 glFrontFace (GL_CCW);
484 glDisable (GL_CULL_FACE);
488 void gr_opengl1_zbuffer_clear(int mode)
492 Gr_zbuffering_mode = GR_ZBUFF_FULL;
493 Gr_global_zbuffering = 1;
495 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
496 glClear ( GL_DEPTH_BUFFER_BIT );
499 Gr_zbuffering_mode = GR_ZBUFF_NONE;
500 Gr_global_zbuffering = 0;
504 void gr_opengl1_fade_in(int instantaneous)
509 void gr_opengl1_fade_out(int instantaneous)
514 void gr_opengl1_get_region(int front, int w, int h, ubyte *data)
517 glReadBuffer(GL_FRONT);
519 glReadBuffer(GL_BACK);
522 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
524 glPixelStorei(GL_UNPACK_ROW_LENGTH, GL_viewport_w);
526 int x = GL_viewport_x;
527 int y = (GL_viewport_y+GL_viewport_h)-h-1;
529 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
531 if (gr_screen.bytes_per_pixel == 4) {
532 pxtype = GL_UNSIGNED_BYTE;
535 glReadPixels(x, y, w, h, GL_BGRA, pxtype, data);
537 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
540 void gr_opengl1_save_mouse_area(int x, int y, int w, int h)
544 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
545 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
552 CAP(x1, 0, GL_viewport_w);
553 CAP(x2, 0, GL_viewport_w);
554 CAP(y1, 0, GL_viewport_h);
555 CAP(y2, 0, GL_viewport_h);
557 Gr_opengl_mouse_saved_x = x1;
558 Gr_opengl_mouse_saved_y = y1;
559 Gr_opengl_mouse_saved_w = x2 - x1 + 1;
560 Gr_opengl_mouse_saved_h = y2 - y1 + 1;
562 if ( (Gr_opengl_mouse_saved_w < 1) || (Gr_opengl_mouse_saved_h < 1) ) {
566 if (Gr_opengl_mouse_saved_data == NULL) {
567 Gr_opengl_mouse_saved_data = (ubyte*)malloc(w * h * gr_screen.bytes_per_pixel);
569 if ( !Gr_opengl_mouse_saved_data ) {
574 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
576 x1 = GL_viewport_x+Gr_opengl_mouse_saved_x;
577 y1 = (GL_viewport_y+GL_viewport_h)-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
579 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
581 if (gr_screen.bytes_per_pixel == 4) {
582 pxtype = GL_UNSIGNED_BYTE;
585 glReadBuffer(GL_BACK);
586 glReadPixels(x1, y1, Gr_opengl_mouse_saved_w, Gr_opengl_mouse_saved_h,
587 GL_BGRA, pxtype, Gr_opengl_mouse_saved_data);
589 Gr_opengl_mouse_saved = 1;
592 int gr_opengl1_save_screen()
596 if (Gr_saved_screen_tex) {
597 mprintf(( "Screen already saved!\n" ));
601 glGenTextures(1, &Gr_saved_screen_tex);
603 if ( !Gr_saved_screen_tex ) {
604 mprintf(( "Couldn't create texture for saved screen!\n" ));
608 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
610 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
612 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
613 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
614 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
615 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
616 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
618 glReadBuffer(GL_FRONT);
619 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_viewport_x, GL_viewport_y,
620 GL_viewport_w, GL_viewport_h, 0);
622 if (Gr_opengl_mouse_saved) {
623 int x = Gr_opengl_mouse_saved_x;
624 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
626 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
628 if (gr_screen.bytes_per_pixel == 4) {
629 pxtype = GL_UNSIGNED_BYTE;
632 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
633 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
634 Gr_opengl_mouse_saved_data);
637 glBindTexture(GL_TEXTURE_2D, 0);
642 void gr_opengl1_restore_screen(int id)
646 if ( !Gr_saved_screen_tex ) {
651 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
653 if (Gr_opengl_mouse_saved) {
654 int x = Gr_opengl_mouse_saved_x;
655 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
657 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
659 if (gr_screen.bytes_per_pixel == 4) {
660 pxtype = GL_UNSIGNED_BYTE;
663 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
664 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
665 Gr_opengl_mouse_saved_data);
668 glMatrixMode(GL_MODELVIEW);
671 glScalef(1.0f, -1.0f, 1.0f);
673 int tex_coord[] = { 0, 0, 0, 1, 1, 0, 1, 1 };
674 int ver_coord[] = { GL_viewport_x, GL_viewport_y, GL_viewport_x,
675 GL_viewport_h, GL_viewport_w, GL_viewport_y, GL_viewport_w,
679 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
680 glEnableClientState(GL_VERTEX_ARRAY);
682 glTexCoordPointer(2, GL_INT, 0, &tex_coord);
683 glVertexPointer(2, GL_INT, 0, &ver_coord);
685 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
687 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
688 glDisableClientState(GL_VERTEX_ARRAY);
692 glBindTexture(GL_TEXTURE_2D, 0);
695 void gr_opengl1_free_screen(int id)
697 if (Gr_saved_screen_tex) {
698 glDeleteTextures(1, &Gr_saved_screen_tex);
699 Gr_saved_screen_tex = 0;
703 void gr_opengl1_dump_frame_start(int first_frame, int frames_between_dumps)
708 void gr_opengl1_dump_frame_stop()
713 void gr_opengl1_dump_frame()
718 uint gr_opengl1_lock()
723 void gr_opengl1_unlock()
727 void gr_opengl1_zbias(int bias)
730 glEnable(GL_POLYGON_OFFSET_FILL);
731 glPolygonOffset(0.0f, GLfloat(-bias));
733 glDisable(GL_POLYGON_OFFSET_FILL);
737 void gr_opengl1_set_viewport(int width, int height)
741 float ratio = gr_screen.max_w / i2fl(gr_screen.max_h);
744 h = fl2i((width / ratio) + 0.5f);
748 w = fl2i((height * ratio) + 0.5f);
752 y = (height - h) / 2;
758 GL_viewport_scale_w = w / i2fl(gr_screen.max_w);
759 GL_viewport_scale_h = h / i2fl(gr_screen.max_h);
761 glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h);
763 glMatrixMode(GL_PROJECTION);
765 glOrtho(0, GL_viewport_w, GL_viewport_h, 0, 0.0, 1.0);
766 glMatrixMode(GL_MODELVIEW);
768 glScalef(GL_viewport_scale_w, GL_viewport_scale_h, 1.0f);