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 gr_opengl1_free_screen(0);
110 if (Gr_opengl_mouse_saved_data) {
111 free(Gr_opengl_mouse_saved_data);
112 Gr_opengl_mouse_saved_data = NULL;
115 opengl1_tcache_cleanup();
120 static void opengl1_init_func_pointers()
122 gr_screen.gf_flip = gr_opengl1_flip;
123 gr_screen.gf_set_clip = gr_opengl1_set_clip;
124 gr_screen.gf_reset_clip = gr_opengl1_reset_clip;
126 gr_screen.gf_clear = gr_opengl1_clear;
128 gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
129 gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
131 gr_screen.gf_rect = gr_opengl1_rect;
132 gr_screen.gf_shade = gr_opengl1_shade;
133 gr_screen.gf_string = gr_opengl1_string;
134 gr_screen.gf_circle = gr_opengl1_circle;
136 gr_screen.gf_line = gr_opengl1_line;
137 gr_screen.gf_aaline = gr_opengl1_aaline;
138 gr_screen.gf_pixel = gr_opengl1_pixel;
139 gr_screen.gf_scaler = gr_opengl1_scaler;
140 gr_screen.gf_tmapper = gr_opengl1_tmapper;
142 gr_screen.gf_gradient = gr_opengl1_gradient;
144 gr_screen.gf_print_screen = gr_opengl1_print_screen;
146 gr_screen.gf_fade_in = gr_opengl1_fade_in;
147 gr_screen.gf_fade_out = gr_opengl1_fade_out;
148 gr_screen.gf_flash = gr_opengl1_flash;
150 gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
152 gr_screen.gf_save_screen = gr_opengl1_save_screen;
153 gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
154 gr_screen.gf_free_screen = gr_opengl1_free_screen;
156 gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
157 gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
158 gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
160 gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
162 gr_screen.gf_lock = gr_opengl1_lock;
163 gr_screen.gf_unlock = gr_opengl1_unlock;
165 gr_screen.gf_fog_set = gr_opengl1_fog_set;
167 gr_screen.gf_get_region = gr_opengl1_get_region;
169 gr_screen.gf_set_cull = gr_opengl1_set_cull;
171 gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
173 gr_screen.gf_preload_init = gr_opengl1_preload_init;
174 gr_screen.gf_preload = gr_opengl1_preload;
176 gr_screen.gf_zbias = gr_opengl1_zbias;
178 gr_screen.gf_force_windowed = gr_opengl_force_windowed;
179 gr_screen.gf_force_fullscreen = gr_opengl_force_fullscreen;
180 gr_screen.gf_toggle_fullscreen = gr_opengl_toggle_fullscreen;
182 gr_screen.gf_set_viewport = gr_opengl1_set_viewport;
184 gr_screen.gf_activate = gr_opengl1_activate;
186 gr_screen.gf_release_texture = gr_opengl1_release_texture;
196 1 = use secondary color ext
197 2 = use opengl linear fog
201 // only available with OpenGL 1.2+, must get ptr for Windows
202 vglSecondaryColorPointer = (PFNGLSECONDARYCOLORPOINTERPROC)SDL_GL_GetProcAddress("glSecondaryColorPointer");
204 if (vglSecondaryColorPointer) {
208 mprintf((" Fog mode : %s\n", (OGL_fog_mode == 1) ? "secondary color" : "linear"));
210 glShadeModel(GL_SMOOTH);
212 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
213 glHint(GL_FOG_HINT, GL_NICEST);
215 glEnable(GL_DEPTH_TEST);
218 glEnable(GL_TEXTURE_2D);
220 glDepthRange(0.0, 1.0);
222 glPixelStorei(GL_PACK_ALIGNMENT, 1);
223 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
227 opengl1_init_func_pointers();
228 opengl1_tcache_init();
235 void gr_opengl1_activate(int active)
240 // don't grab key/mouse if cmdline says so or if we're fullscreen
241 // if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
242 // SDL_WM_GrabInput(SDL_GRAB_ON);
247 // let go of mouse/keyboard
248 // SDL_WM_GrabInput(SDL_GRAB_OFF);
252 void gr_opengl1_clear()
254 glClearColor(gr_screen.current_clear_color.red / 255.0f,
255 gr_screen.current_clear_color.green / 255.0f,
256 gr_screen.current_clear_color.blue / 255.0f, 1.0f);
258 glClear( GL_COLOR_BUFFER_BIT );
261 void gr_opengl1_flip()
263 if ( !GL_one_inited ) {
271 Gr_opengl_mouse_saved = 0;
273 if ( mouse_is_visible() ) {
277 mouse_get_pos( &mx, &my );
279 gr_opengl1_save_mouse_area(mx,my,32,32);
281 if (Gr_cursor == -1) {
283 gr_set_color(255,255,255);
284 gr_line(mx, my, mx+7, my + 7);
285 gr_line(mx, my, mx+5, my );
286 gr_line(mx, my, mx, my+5);
289 gr_set_bitmap(Gr_cursor);
295 GLenum error = GL_NO_ERROR;
298 error = glGetError();
300 if (error != GL_NO_ERROR) {
301 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
303 } while (error != GL_NO_ERROR);
306 SDL_GL_SwapWindow(GL_window);
308 opengl1_tcache_frame();
310 int cnt = GL_activate;
313 opengl1_tcache_flush();
314 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
320 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
324 void gr_opengl1_set_clip(int x,int y,int w,int h)
326 // check for sanity of parameters
332 if (x >= gr_screen.max_w)
333 x = gr_screen.max_w - 1;
334 if (y >= gr_screen.max_h)
335 y = gr_screen.max_h - 1;
337 if (x + w > gr_screen.max_w)
338 w = gr_screen.max_w - x;
339 if (y + h > gr_screen.max_h)
340 h = gr_screen.max_h - y;
342 if (w > gr_screen.max_w)
344 if (h > gr_screen.max_h)
347 gr_screen.offset_x = x;
348 gr_screen.offset_y = y;
349 gr_screen.clip_left = 0;
350 gr_screen.clip_right = w-1;
351 gr_screen.clip_top = 0;
352 gr_screen.clip_bottom = h-1;
353 gr_screen.clip_width = w;
354 gr_screen.clip_height = h;
356 x = fl2i((x * GL_viewport_scale_w) + 0.5f) + GL_viewport_x;
357 y = fl2i((y * GL_viewport_scale_h) + 0.5f) + GL_viewport_y;
358 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
359 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
361 glEnable(GL_SCISSOR_TEST);
362 glScissor(x, GL_viewport_h-y-h, w, h);
365 void gr_opengl1_reset_clip()
367 gr_screen.offset_x = 0;
368 gr_screen.offset_y = 0;
369 gr_screen.clip_left = 0;
370 gr_screen.clip_top = 0;
371 gr_screen.clip_right = gr_screen.max_w - 1;
372 gr_screen.clip_bottom = gr_screen.max_h - 1;
373 gr_screen.clip_width = gr_screen.max_w;
374 gr_screen.clip_height = gr_screen.max_h;
376 glDisable(GL_SCISSOR_TEST);
379 void gr_opengl1_print_screen(const char *filename)
381 char tmp[MAX_FILENAME_LEN];
384 SDL_strlcpy( tmp, filename, sizeof(tmp) );
385 SDL_strlcat( tmp, NOX(".tga"), sizeof(tmp) );
387 buf = (ubyte*)malloc(GL_viewport_w * GL_viewport_h * 3);
393 CFILE *f = cfopen(tmp, "wb", CFILE_NORMAL, CF_TYPE_ROOT);
400 // Write the TGA header
401 cfwrite_ubyte( 0, f ); // IDLength;
402 cfwrite_ubyte( 0, f ); // ColorMapType;
403 cfwrite_ubyte( 2, f ); // ImageType; // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
404 cfwrite_ushort( 0, f ); // CMapStart;
405 cfwrite_ushort( 0, f ); // CMapLength;
406 cfwrite_ubyte( 0, f ); // CMapDepth;
407 cfwrite_ushort( 0, f ); // XOffset;
408 cfwrite_ushort( 0, f ); // YOffset;
409 cfwrite_ushort( (ushort)GL_viewport_w, f ); // Width;
410 cfwrite_ushort( (ushort)GL_viewport_h, f ); // Height;
411 cfwrite_ubyte( 24, f ); //PixelDepth;
412 cfwrite_ubyte( 0, f ); //ImageDesc;
414 memset(buf, 0, GL_viewport_w * GL_viewport_h * 3);
416 glReadPixels(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h, GL_BGR, GL_UNSIGNED_BYTE, buf);
418 cfwrite(buf, GL_viewport_w * GL_viewport_h * 3, 1, f);
425 void gr_opengl1_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
427 SDL_assert((r >= 0) && (r < 256));
428 SDL_assert((g >= 0) && (g < 256));
429 SDL_assert((b >= 0) && (b < 256));
431 if (fog_mode == GR_FOGMODE_NONE) {
432 if (gr_screen.current_fog_mode != fog_mode) {
435 if (OGL_fog_mode == 1) {
436 glDisable(GL_COLOR_SUM);
440 gr_screen.current_fog_mode = fog_mode;
445 if (gr_screen.current_fog_mode != fog_mode) {
448 if (OGL_fog_mode == 1) {
449 glEnable(GL_COLOR_SUM);
450 } else if (OGL_fog_mode == 2) {
451 glFogi(GL_FOG_MODE, GL_LINEAR);
454 gr_screen.current_fog_mode = fog_mode;
457 if ( (gr_screen.current_fog_color.red != r) ||
458 (gr_screen.current_fog_color.green != g) ||
459 (gr_screen.current_fog_color.blue != b) ) {
462 gr_init_color( &gr_screen.current_fog_color, r, g, b );
469 glFogfv(GL_FOG_COLOR, fc);
472 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
473 ((fog_near != gr_screen.fog_near) ||
474 (fog_far != gr_screen.fog_far)) ) {
475 gr_screen.fog_near = fog_near;
476 gr_screen.fog_far = fog_far;
478 if (OGL_fog_mode == 2) {
479 glFogf(GL_FOG_START, fog_near);
480 glFogf(GL_FOG_END, fog_far);
485 void gr_opengl1_set_cull(int cull)
488 glEnable (GL_CULL_FACE);
489 glFrontFace (GL_CCW);
491 glDisable (GL_CULL_FACE);
495 void gr_opengl1_zbuffer_clear(int mode)
499 Gr_zbuffering_mode = GR_ZBUFF_FULL;
500 Gr_global_zbuffering = 1;
502 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
503 glClear ( GL_DEPTH_BUFFER_BIT );
506 Gr_zbuffering_mode = GR_ZBUFF_NONE;
507 Gr_global_zbuffering = 0;
511 void gr_opengl1_fade_in(int instantaneous)
516 void gr_opengl1_fade_out(int instantaneous)
521 void gr_opengl1_get_region(int front, int w, int h, ubyte *data)
524 glReadBuffer(GL_FRONT);
526 glReadBuffer(GL_BACK);
529 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
531 glPixelStorei(GL_UNPACK_ROW_LENGTH, GL_viewport_w);
533 int x = GL_viewport_x;
534 int y = (GL_viewport_y+GL_viewport_h)-h-1;
536 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
538 if (gr_screen.bytes_per_pixel == 4) {
539 pxtype = GL_UNSIGNED_BYTE;
542 glReadPixels(x, y, w, h, GL_BGRA, pxtype, data);
544 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
547 void gr_opengl1_save_mouse_area(int x, int y, int w, int h)
551 if (Gr_saved_screen_tex) {
552 // already saved, don't need it again
556 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
557 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
564 CAP(x1, 0, GL_viewport_w);
565 CAP(x2, 0, GL_viewport_w);
566 CAP(y1, 0, GL_viewport_h);
567 CAP(y2, 0, GL_viewport_h);
569 Gr_opengl_mouse_saved_x = x1;
570 Gr_opengl_mouse_saved_y = y1;
571 Gr_opengl_mouse_saved_w = x2 - x1 + 1;
572 Gr_opengl_mouse_saved_h = y2 - y1 + 1;
574 if ( (Gr_opengl_mouse_saved_w < 1) || (Gr_opengl_mouse_saved_h < 1) ) {
578 if (Gr_opengl_mouse_saved_data == NULL) {
579 Gr_opengl_mouse_saved_data = (ubyte*)malloc(w * h * 3);
581 if ( !Gr_opengl_mouse_saved_data ) {
586 x1 = GL_viewport_x+Gr_opengl_mouse_saved_x;
587 y1 = (GL_viewport_y+GL_viewport_h)-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
589 glReadBuffer(GL_BACK);
591 glReadPixels(x1, y1, Gr_opengl_mouse_saved_w, Gr_opengl_mouse_saved_h,
592 GL_BGR, GL_UNSIGNED_BYTE, Gr_opengl_mouse_saved_data);
594 Gr_opengl_mouse_saved = 1;
597 int gr_opengl1_save_screen()
601 if (Gr_saved_screen_tex) {
602 mprintf(( "Screen already saved!\n" ));
606 glGenTextures(1, &Gr_saved_screen_tex);
608 if ( !Gr_saved_screen_tex ) {
609 mprintf(( "Couldn't create texture for saved screen!\n" ));
613 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
615 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
616 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
617 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
618 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
619 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
621 glReadBuffer(GL_FRONT);
623 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_viewport_x, GL_viewport_y,
624 GL_viewport_w, GL_viewport_h, 0);
626 if (Gr_opengl_mouse_saved) {
627 int x = Gr_opengl_mouse_saved_x;
628 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
630 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
631 Gr_opengl_mouse_saved_h, GL_BGR, GL_UNSIGNED_BYTE,
632 Gr_opengl_mouse_saved_data);
635 glBindTexture(GL_TEXTURE_2D, 0);
640 void gr_opengl1_restore_screen(int)
644 if ( !Gr_saved_screen_tex ) {
649 const float scale_w = 1.0f / GL_viewport_scale_w;
650 const float scale_h = 1.0f / GL_viewport_scale_h;
652 const int tex_coord[] = { 0, 1, 0, 0, 1, 1, 1, 0 }; // y-flipped
653 const int ver_coord[] = { GL_viewport_x, GL_viewport_y, GL_viewport_x,
654 (GL_viewport_h * scale_h), (GL_viewport_w * scale_w), GL_viewport_y,
655 (GL_viewport_w * scale_w), (GL_viewport_h * scale_h)
658 glColor4ub(255, 255, 255, 255);
660 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
662 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
664 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
665 glEnableClientState(GL_VERTEX_ARRAY);
667 glTexCoordPointer(2, GL_INT, 0, &tex_coord);
668 glVertexPointer(2, GL_INT, 0, &ver_coord);
670 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
672 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
673 glDisableClientState(GL_VERTEX_ARRAY);
675 glBindTexture(GL_TEXTURE_2D, 0);
678 void gr_opengl1_free_screen(int)
680 if (Gr_saved_screen_tex) {
681 glDeleteTextures(1, &Gr_saved_screen_tex);
682 Gr_saved_screen_tex = 0;
686 void gr_opengl1_dump_frame_start(int first_frame, int frames_between_dumps)
691 void gr_opengl1_dump_frame_stop()
696 void gr_opengl1_dump_frame()
701 uint gr_opengl1_lock()
706 void gr_opengl1_unlock()
710 void gr_opengl1_zbias(int bias)
713 glEnable(GL_POLYGON_OFFSET_FILL);
714 glPolygonOffset(0.0f, GLfloat(-bias));
716 glDisable(GL_POLYGON_OFFSET_FILL);
720 void gr_opengl1_set_viewport(int width, int height)
724 float ratio = gr_screen.max_w / i2fl(gr_screen.max_h);
727 h = fl2i((width / ratio) + 0.5f);
731 w = fl2i((height * ratio) + 0.5f);
735 y = (height - h) / 2;
741 GL_viewport_scale_w = w / i2fl(gr_screen.max_w);
742 GL_viewport_scale_h = h / i2fl(gr_screen.max_h);
744 glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h);
746 glMatrixMode(GL_PROJECTION);
748 glOrtho(0, GL_viewport_w, GL_viewport_h, 0, 0.0, 1.0);
749 glMatrixMode(GL_MODELVIEW);
751 glScalef(GL_viewport_scale_w, GL_viewport_scale_h, 1.0f);
753 // free mouse cursor storage, since the size might have changed
754 if (Gr_opengl_mouse_saved_data) {
755 free(Gr_opengl_mouse_saved_data);
756 Gr_opengl_mouse_saved_data = NULL;