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 #include "gropengl1.h"
11 #include "gropenglinternal.h"
17 #include "grinternal.h"
22 static 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;
118 gr_screen.gf_set_font = grx_set_font;
120 gr_screen.gf_set_color = gr_opengl_set_color;
121 gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
122 gr_screen.gf_create_shader = gr_opengl_create_shader;
123 gr_screen.gf_set_shader = gr_opengl_set_shader;
124 gr_screen.gf_clear = gr_opengl1_clear;
126 gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
127 gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
129 gr_screen.gf_rect = gr_opengl1_rect;
130 gr_screen.gf_shade = gr_opengl1_shade;
131 gr_screen.gf_string = gr_opengl1_string;
132 gr_screen.gf_circle = gr_opengl1_circle;
134 gr_screen.gf_line = gr_opengl1_line;
135 gr_screen.gf_aaline = gr_opengl1_aaline;
136 gr_screen.gf_pixel = gr_opengl1_pixel;
137 gr_screen.gf_scaler = gr_opengl1_scaler;
138 gr_screen.gf_tmapper = gr_opengl1_tmapper;
140 gr_screen.gf_gradient = gr_opengl1_gradient;
142 gr_screen.gf_get_color = gr_opengl_get_color;
143 gr_screen.gf_init_color = gr_opengl_init_color;
144 gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
145 gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
146 gr_screen.gf_print_screen = gr_opengl1_print_screen;
148 gr_screen.gf_fade_in = gr_opengl1_fade_in;
149 gr_screen.gf_fade_out = gr_opengl1_fade_out;
150 gr_screen.gf_flash = gr_opengl1_flash;
152 gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
153 gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
154 gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
156 gr_screen.gf_save_screen = gr_opengl1_save_screen;
157 gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
158 gr_screen.gf_free_screen = gr_opengl1_free_screen;
160 gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
161 gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
162 gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
164 gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
166 gr_screen.gf_lock = gr_opengl1_lock;
167 gr_screen.gf_unlock = gr_opengl1_unlock;
169 gr_screen.gf_fog_set = gr_opengl1_fog_set;
171 gr_screen.gf_get_region = gr_opengl1_get_region;
173 gr_screen.gf_set_cull = gr_opengl1_set_cull;
175 gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
177 gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;
179 gr_screen.gf_preload_init = gr_opengl1_preload_init;
180 gr_screen.gf_preload = gr_opengl1_preload;
182 gr_screen.gf_zbias = gr_opengl1_zbias;
184 gr_screen.gf_force_windowed = gr_opengl_force_windowed;
185 gr_screen.gf_force_fullscreen = gr_opengl_force_fullscreen;
186 gr_screen.gf_set_viewport = gr_opengl_set_viewport;
188 gr_screen.gf_activate = gr_opengl1_activate;
198 1 = use secondary color ext
199 2 = use opengl linear fog
203 // only available with OpenGL 1.2+, must get ptr for Windows
204 vglSecondaryColorPointer = (PFNGLSECONDARYCOLORPOINTERPROC)SDL_GL_GetProcAddress("glSecondaryColorPointer");
206 if (vglSecondaryColorPointer) {
210 mprintf((" Fog mode: %s\n", (OGL_fog_mode == 1) ? "secondary color" : "linear"));
212 glShadeModel(GL_SMOOTH);
214 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
215 glHint(GL_FOG_HINT, GL_NICEST);
217 glEnable(GL_DEPTH_TEST);
220 glEnable(GL_TEXTURE_2D);
222 glDepthRange(0.0, 1.0);
224 glPixelStorei(GL_PACK_ALIGNMENT, 1);
225 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
229 opengl1_init_func_pointers();
230 opengl1_tcache_init();
237 void gr_opengl1_activate(int active)
242 // don't grab key/mouse if cmdline says so or if we're fullscreen
243 // if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
244 // SDL_WM_GrabInput(SDL_GRAB_ON);
249 // let go of mouse/keyboard
250 // SDL_WM_GrabInput(SDL_GRAB_OFF);
254 void gr_opengl1_clear()
256 glClearColor(gr_screen.current_clear_color.red / 255.0,
257 gr_screen.current_clear_color.green / 255.0,
258 gr_screen.current_clear_color.blue / 255.0, 1.0);
260 glClear( GL_COLOR_BUFFER_BIT );
263 void gr_opengl1_flip()
265 if ( !GL_one_inited ) {
273 Gr_opengl_mouse_saved = 0;
275 if ( mouse_is_visible() ) {
279 mouse_get_pos( &mx, &my );
281 gr_opengl1_save_mouse_area(mx,my,32,32);
283 if (Gr_cursor == -1) {
285 gr_set_color(255,255,255);
286 gr_line(mx, my, mx+7, my + 7);
287 gr_line(mx, my, mx+5, my );
288 gr_line(mx, my, mx, my+5);
291 gr_set_bitmap(Gr_cursor);
297 GLenum error = GL_NO_ERROR;
300 error = glGetError();
302 if (error != GL_NO_ERROR) {
303 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
305 } while (error != GL_NO_ERROR);
308 SDL_GL_SwapWindow(GL_window);
310 opengl1_tcache_frame();
312 int cnt = GL_activate;
315 opengl1_tcache_flush();
316 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
322 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
326 void gr_opengl1_set_clip(int x,int y,int w,int h)
328 // check for sanity of parameters
334 if (x >= gr_screen.max_w)
335 x = gr_screen.max_w - 1;
336 if (y >= gr_screen.max_h)
337 y = gr_screen.max_h - 1;
339 if (x + w > gr_screen.max_w)
340 w = gr_screen.max_w - x;
341 if (y + h > gr_screen.max_h)
342 h = gr_screen.max_h - y;
344 if (w > gr_screen.max_w)
346 if (h > gr_screen.max_h)
349 gr_screen.offset_x = x;
350 gr_screen.offset_y = y;
351 gr_screen.clip_left = 0;
352 gr_screen.clip_right = w-1;
353 gr_screen.clip_top = 0;
354 gr_screen.clip_bottom = h-1;
355 gr_screen.clip_width = w;
356 gr_screen.clip_height = h;
358 x = fl2i((x * GL_viewport_scale_w) + 0.5f) + GL_viewport_x;
359 y = fl2i((y * GL_viewport_scale_h) + 0.5f) + GL_viewport_y;
360 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
361 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
363 glEnable(GL_SCISSOR_TEST);
364 glScissor(x, GL_viewport_h-y-h, w, h);
367 void gr_opengl1_reset_clip()
369 gr_screen.offset_x = 0;
370 gr_screen.offset_y = 0;
371 gr_screen.clip_left = 0;
372 gr_screen.clip_top = 0;
373 gr_screen.clip_right = gr_screen.max_w - 1;
374 gr_screen.clip_bottom = gr_screen.max_h - 1;
375 gr_screen.clip_width = gr_screen.max_w;
376 gr_screen.clip_height = gr_screen.max_h;
378 glDisable(GL_SCISSOR_TEST);
381 void gr_opengl1_print_screen(const char *filename)
383 char tmp[MAX_FILENAME_LEN];
386 strcpy( tmp, filename );
387 strcat( tmp, NOX(".tga"));
389 buf = (ubyte*)malloc(GL_viewport_w * GL_viewport_h * 3);
395 CFILE *f = cfopen(tmp, "wb", CFILE_NORMAL, CF_TYPE_ROOT);
402 // Write the TGA header
403 cfwrite_ubyte( 0, f ); // IDLength;
404 cfwrite_ubyte( 0, f ); // ColorMapType;
405 cfwrite_ubyte( 2, f ); // ImageType; // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
406 cfwrite_ushort( 0, f ); // CMapStart;
407 cfwrite_ushort( 0, f ); // CMapLength;
408 cfwrite_ubyte( 0, f ); // CMapDepth;
409 cfwrite_ushort( 0, f ); // XOffset;
410 cfwrite_ushort( 0, f ); // YOffset;
411 cfwrite_ushort( (ushort)GL_viewport_w, f ); // Width;
412 cfwrite_ushort( (ushort)GL_viewport_h, f ); // Height;
413 cfwrite_ubyte( 24, f ); //PixelDepth;
414 cfwrite_ubyte( 0, f ); //ImageDesc;
416 memset(buf, 0, GL_viewport_w * GL_viewport_h * 3);
418 glReadPixels(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h, GL_BGR, GL_UNSIGNED_BYTE, buf);
420 cfwrite(buf, GL_viewport_w * GL_viewport_h * 3, 1, f);
427 void gr_opengl1_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
429 Assert((r >= 0) && (r < 256));
430 Assert((g >= 0) && (g < 256));
431 Assert((b >= 0) && (b < 256));
433 if (fog_mode == GR_FOGMODE_NONE) {
434 if (gr_screen.current_fog_mode != fog_mode) {
437 if (OGL_fog_mode == 1) {
438 glDisable(GL_COLOR_SUM);
442 gr_screen.current_fog_mode = fog_mode;
447 if (gr_screen.current_fog_mode != fog_mode) {
450 if (OGL_fog_mode == 1) {
451 glEnable(GL_COLOR_SUM);
452 } else if (OGL_fog_mode == 2) {
453 glFogi(GL_FOG_MODE, GL_LINEAR);
456 gr_screen.current_fog_mode = fog_mode;
459 if ( (gr_screen.current_fog_color.red != r) ||
460 (gr_screen.current_fog_color.green != g) ||
461 (gr_screen.current_fog_color.blue != b) ) {
464 gr_opengl_init_color( &gr_screen.current_fog_color, r, g, b );
466 fc[0] = (float)r/255.0;
467 fc[1] = (float)g/255.0;
468 fc[2] = (float)b/255.0;
471 glFogfv(GL_FOG_COLOR, fc);
474 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
475 ((fog_near != gr_screen.fog_near) ||
476 (fog_far != gr_screen.fog_far)) ) {
477 gr_screen.fog_near = fog_near;
478 gr_screen.fog_far = fog_far;
480 if (OGL_fog_mode == 2) {
481 glFogf(GL_FOG_START, fog_near);
482 glFogf(GL_FOG_END, fog_far);
487 void gr_opengl1_set_cull(int cull)
490 glEnable (GL_CULL_FACE);
491 glFrontFace (GL_CCW);
493 glDisable (GL_CULL_FACE);
497 void gr_opengl1_zbuffer_clear(int mode)
501 Gr_zbuffering_mode = GR_ZBUFF_FULL;
502 Gr_global_zbuffering = 1;
504 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
505 glClear ( GL_DEPTH_BUFFER_BIT );
508 Gr_zbuffering_mode = GR_ZBUFF_NONE;
509 Gr_global_zbuffering = 0;
513 void gr_opengl1_fade_in(int instantaneous)
518 void gr_opengl1_fade_out(int instantaneous)
523 void gr_opengl1_get_region(int front, int w, int h, ubyte *data)
526 glReadBuffer(GL_FRONT);
528 glReadBuffer(GL_BACK);
531 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
533 glPixelStorei(GL_UNPACK_ROW_LENGTH, GL_viewport_w);
535 int x = GL_viewport_x;
536 int y = (GL_viewport_y+GL_viewport_h)-h-1;
538 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
540 if (gr_screen.bytes_per_pixel == 4) {
541 pxtype = GL_UNSIGNED_BYTE;
544 glReadPixels(x, y, w, h, GL_BGRA, pxtype, data);
546 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
549 void gr_opengl1_save_mouse_area(int x, int y, int w, int h)
553 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
554 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
561 CAP(x1, 0, GL_viewport_w);
562 CAP(x2, 0, GL_viewport_w);
563 CAP(y1, 0, GL_viewport_h);
564 CAP(y2, 0, GL_viewport_h);
566 Gr_opengl_mouse_saved_x = x1;
567 Gr_opengl_mouse_saved_y = y1;
568 Gr_opengl_mouse_saved_w = x2 - x1 + 1;
569 Gr_opengl_mouse_saved_h = y2 - y1 + 1;
571 if ( (Gr_opengl_mouse_saved_w < 1) || (Gr_opengl_mouse_saved_h < 1) ) {
575 if (Gr_opengl_mouse_saved_data == NULL) {
576 Gr_opengl_mouse_saved_data = (ubyte*)malloc(w * h * gr_screen.bytes_per_pixel);
578 if ( !Gr_opengl_mouse_saved_data ) {
583 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
585 x1 = GL_viewport_x+Gr_opengl_mouse_saved_x;
586 y1 = (GL_viewport_y+GL_viewport_h)-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
588 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
590 if (gr_screen.bytes_per_pixel == 4) {
591 pxtype = GL_UNSIGNED_BYTE;
594 glReadBuffer(GL_BACK);
595 glReadPixels(x1, y1, Gr_opengl_mouse_saved_w, Gr_opengl_mouse_saved_h,
596 GL_BGRA, pxtype, Gr_opengl_mouse_saved_data);
598 Gr_opengl_mouse_saved = 1;
601 int gr_opengl1_save_screen()
605 if (Gr_saved_screen_tex) {
606 mprintf(( "Screen already saved!\n" ));
610 glGenTextures(1, &Gr_saved_screen_tex);
612 if ( !Gr_saved_screen_tex ) {
613 mprintf(( "Couldn't create texture for saved screen!\n" ));
617 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
619 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
621 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
622 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
623 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
624 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
625 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
627 glReadBuffer(GL_FRONT);
628 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_viewport_x, GL_viewport_y,
629 GL_viewport_w, GL_viewport_h, 0);
631 if (Gr_opengl_mouse_saved) {
632 int x = Gr_opengl_mouse_saved_x;
633 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
635 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
637 if (gr_screen.bytes_per_pixel == 4) {
638 pxtype = GL_UNSIGNED_BYTE;
641 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
642 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
643 Gr_opengl_mouse_saved_data);
646 glBindTexture(GL_TEXTURE_2D, 0);
651 void gr_opengl1_restore_screen(int id)
655 if ( !Gr_saved_screen_tex ) {
660 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
662 if (Gr_opengl_mouse_saved) {
663 int x = Gr_opengl_mouse_saved_x;
664 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
666 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
668 if (gr_screen.bytes_per_pixel == 4) {
669 pxtype = GL_UNSIGNED_BYTE;
672 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
673 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
674 Gr_opengl_mouse_saved_data);
677 glMatrixMode(GL_MODELVIEW);
680 glScalef(1.0f, -1.0f, 1.0f);
682 int tex_coord[] = { 0, 0, 0, 1, 1, 0, 1, 1 };
683 int ver_coord[] = { GL_viewport_x, GL_viewport_y, GL_viewport_x,
684 GL_viewport_h, GL_viewport_w, GL_viewport_y, GL_viewport_w,
688 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
689 glEnableClientState(GL_VERTEX_ARRAY);
691 glTexCoordPointer(2, GL_INT, 0, &tex_coord);
692 glVertexPointer(2, GL_INT, 0, &ver_coord);
694 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
696 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
697 glDisableClientState(GL_VERTEX_ARRAY);
701 glBindTexture(GL_TEXTURE_2D, 0);
704 void gr_opengl1_free_screen(int id)
706 if (Gr_saved_screen_tex) {
707 glDeleteTextures(1, &Gr_saved_screen_tex);
708 Gr_saved_screen_tex = 0;
712 void gr_opengl1_dump_frame_start(int first_frame, int frames_between_dumps)
717 void gr_opengl1_dump_frame_stop()
722 void gr_opengl1_dump_frame()
727 uint gr_opengl1_lock()
732 void gr_opengl1_unlock()
736 void gr_opengl1_zbias(int bias)
739 glEnable(GL_POLYGON_OFFSET_FILL);
740 glPolygonOffset(0.0, -bias);
742 glDisable(GL_POLYGON_OFFSET_FILL);