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
9 #include "SDL_opengl.h"
12 #include "gropenglinternal.h"
19 #include "grinternal.h"
21 #include "osregistry.h"
24 int GL_one_inited = 0;
27 static GLuint GL_stream_tex = 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_opengl_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();
118 SDL_GL_DeleteContext(GL_context);
125 static void opengl1_init_func_pointers()
127 gr_screen.gf_flip = gr_opengl1_flip;
128 gr_screen.gf_set_clip = gr_opengl1_set_clip;
129 gr_screen.gf_reset_clip = gr_opengl_reset_clip;
131 gr_screen.gf_clear = gr_opengl_clear;
133 gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
134 gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
136 gr_screen.gf_rect = gr_opengl1_rect;
137 gr_screen.gf_shade = gr_opengl1_shade;
138 gr_screen.gf_string = gr_opengl1_string;
139 gr_screen.gf_circle = gr_opengl1_circle;
141 gr_screen.gf_line = gr_opengl1_line;
142 gr_screen.gf_aaline = gr_opengl1_aaline;
143 gr_screen.gf_pixel = gr_opengl1_pixel;
144 gr_screen.gf_scaler = gr_opengl1_scaler;
145 gr_screen.gf_tmapper = gr_opengl1_tmapper;
147 gr_screen.gf_gradient = gr_opengl1_gradient;
149 gr_screen.gf_print_screen = gr_opengl_print_screen;
151 gr_screen.gf_fade_in = gr_opengl1_fade_in;
152 gr_screen.gf_fade_out = gr_opengl1_fade_out;
153 gr_screen.gf_flash = gr_opengl1_flash;
155 gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
157 gr_screen.gf_save_screen = gr_opengl1_save_screen;
158 gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
159 gr_screen.gf_free_screen = gr_opengl1_free_screen;
161 gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
162 gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
163 gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
165 gr_screen.gf_stream_start = gr_opengl1_stream_start;
166 gr_screen.gf_stream_frame = gr_opengl1_stream_frame;
167 gr_screen.gf_stream_stop = gr_opengl1_stream_stop;
169 gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
171 gr_screen.gf_lock = gr_opengl_lock;
172 gr_screen.gf_unlock = gr_opengl_unlock;
174 gr_screen.gf_fog_set = gr_opengl1_fog_set;
176 gr_screen.gf_get_region = gr_opengl1_get_region;
178 gr_screen.gf_set_cull = gr_opengl_set_cull;
180 gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
182 gr_screen.gf_preload_init = gr_opengl1_preload_init;
183 gr_screen.gf_preload = gr_opengl1_preload;
185 gr_screen.gf_zbias = gr_opengl_zbias;
187 gr_screen.gf_set_viewport = gr_opengl1_set_viewport;
189 gr_screen.gf_activate = gr_opengl_activate;
191 gr_screen.gf_release_texture = gr_opengl1_release_texture;
202 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
203 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
204 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
206 GL_context = SDL_GL_CreateContext(GL_window);
213 mprintf((" Vendor : %s\n", glGetString(GL_VENDOR)));
214 mprintf((" Renderer : %s\n", glGetString(GL_RENDERER)));
215 mprintf((" Version : %s\n", glGetString(GL_VERSION)));
217 // set up generic variables
218 opengl_set_variables();
220 opengl1_init_func_pointers();
221 opengl1_tcache_init();
223 // initial viewport setup
224 gr_opengl1_set_viewport(gr_screen.max_w, gr_screen.max_h);
226 glShadeModel(GL_SMOOTH);
228 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
229 glHint(GL_FOG_HINT, GL_NICEST);
231 glEnable(GL_DEPTH_TEST);
234 glEnable(GL_TEXTURE_2D);
236 glDepthRange(0.0, 1.0);
238 glPixelStorei(GL_PACK_ALIGNMENT, 1);
239 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
244 gr_opengl_set_cull(1);
249 void gr_opengl1_flip()
251 if ( !GL_one_inited ) {
259 Gr_opengl_mouse_saved = 0;
261 if ( mouse_is_visible() ) {
264 mouse_get_pos( &mx, &my );
266 gr_opengl1_save_mouse_area(mx,my,32,32);
268 if (Gr_cursor == -1) {
270 gr_set_color(255,255,255);
271 gr_line(mx, my, mx+7, my + 7);
272 gr_line(mx, my, mx+5, my );
273 gr_line(mx, my, mx, my+5);
276 gr_set_bitmap(Gr_cursor);
282 GLenum error = GL_NO_ERROR;
285 error = glGetError();
287 if (error != GL_NO_ERROR) {
288 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
290 } while (error != GL_NO_ERROR);
293 SDL_GL_SwapWindow(GL_window);
295 opengl1_tcache_frame();
297 int cnt = GL_activate;
300 opengl1_tcache_flush();
301 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
307 // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */
311 void gr_opengl1_set_clip(int x, int y, int w, int h)
313 // check for sanity of parameters
314 CAP(x, 0, gr_screen.max_w - 1);
315 CAP(y, 0, gr_screen.max_h - 1);
316 CAP(w, 0, gr_screen.max_w - x);
317 CAP(h, 0, gr_screen.max_h - y);
319 gr_screen.offset_x = x;
320 gr_screen.offset_y = y;
321 gr_screen.clip_left = 0;
322 gr_screen.clip_right = w-1;
323 gr_screen.clip_top = 0;
324 gr_screen.clip_bottom = h-1;
325 gr_screen.clip_width = w;
326 gr_screen.clip_height = h;
328 x = fl2i((x * GL_viewport_scale_w) + 0.5f) + GL_viewport_x;
329 y = fl2i((y * GL_viewport_scale_h) + 0.5f) + GL_viewport_y;
330 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
331 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
333 glEnable(GL_SCISSOR_TEST);
334 glScissor(x, GL_viewport_h-y-h, w, h);
337 void gr_opengl1_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
339 SDL_assert((r >= 0) && (r < 256));
340 SDL_assert((g >= 0) && (g < 256));
341 SDL_assert((b >= 0) && (b < 256));
343 if (fog_mode == GR_FOGMODE_NONE) {
344 if (gr_screen.current_fog_mode != fog_mode) {
348 gr_screen.current_fog_mode = fog_mode;
353 if (gr_screen.current_fog_mode != fog_mode) {
355 glFogi(GL_FOG_MODE, GL_LINEAR);
357 gr_screen.current_fog_mode = fog_mode;
360 if ( (gr_screen.current_fog_color.red != r) ||
361 (gr_screen.current_fog_color.green != g) ||
362 (gr_screen.current_fog_color.blue != b) ) {
363 gr_init_color( &gr_screen.current_fog_color, r, g, b );
372 glFogfv(GL_FOG_COLOR, fc);
375 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
376 ((fog_near != gr_screen.fog_near) ||
377 (fog_far != gr_screen.fog_far)) ) {
378 gr_screen.fog_near = fog_near;
379 gr_screen.fog_far = fog_far;
381 glFogf(GL_FOG_START, fog_near);
382 glFogf(GL_FOG_END, fog_far);
386 void gr_opengl1_zbuffer_clear(int mode)
390 Gr_zbuffering_mode = GR_ZBUFF_FULL;
391 Gr_global_zbuffering = 1;
393 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
394 glClear ( GL_DEPTH_BUFFER_BIT );
397 Gr_zbuffering_mode = GR_ZBUFF_NONE;
398 Gr_global_zbuffering = 0;
402 void gr_opengl1_fade_in(int instantaneous)
407 void gr_opengl1_fade_out(int instantaneous)
412 void gr_opengl1_get_region(int front, int w, int h, ubyte *data)
415 glReadBuffer(GL_FRONT);
417 glReadBuffer(GL_BACK);
420 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
422 glPixelStorei(GL_UNPACK_ROW_LENGTH, GL_viewport_w);
424 int x = GL_viewport_x;
425 int y = (GL_viewport_y+GL_viewport_h)-h-1;
427 GLenum pxtype = GL_UNSIGNED_SHORT_5_5_5_1;
429 if (gr_screen.bytes_per_pixel == 4) {
430 pxtype = GL_UNSIGNED_BYTE;
433 glReadPixels(x, y, w, h, GL_RGBA, pxtype, data);
435 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
438 void gr_opengl1_save_mouse_area(int x, int y, int w, int h)
442 if (Gr_saved_screen_tex) {
443 // already saved, don't need it again
447 w = fl2i((w * GL_viewport_scale_w) + 0.5f);
448 h = fl2i((h * GL_viewport_scale_h) + 0.5f);
455 CAP(x1, 0, GL_viewport_w);
456 CAP(x2, 0, GL_viewport_w);
457 CAP(y1, 0, GL_viewport_h);
458 CAP(y2, 0, GL_viewport_h);
460 Gr_opengl_mouse_saved_x = x1;
461 Gr_opengl_mouse_saved_y = y1;
462 Gr_opengl_mouse_saved_w = x2 - x1 + 1;
463 Gr_opengl_mouse_saved_h = y2 - y1 + 1;
465 if ( (Gr_opengl_mouse_saved_w < 1) || (Gr_opengl_mouse_saved_h < 1) ) {
469 if (Gr_opengl_mouse_saved_data == NULL) {
470 Gr_opengl_mouse_saved_data = (ubyte*)malloc(w * h * 3);
472 if ( !Gr_opengl_mouse_saved_data ) {
477 x1 = GL_viewport_x+Gr_opengl_mouse_saved_x;
478 y1 = (GL_viewport_y+GL_viewport_h)-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
480 glReadBuffer(GL_BACK);
482 glReadPixels(x1, y1, Gr_opengl_mouse_saved_w, Gr_opengl_mouse_saved_h,
483 GL_RGB, GL_UNSIGNED_BYTE, Gr_opengl_mouse_saved_data);
485 Gr_opengl_mouse_saved = 1;
488 int gr_opengl1_save_screen()
492 if (Gr_saved_screen_tex) {
493 mprintf(( "Screen already saved!\n" ));
497 glGenTextures(1, &Gr_saved_screen_tex);
499 if ( !Gr_saved_screen_tex ) {
500 mprintf(( "Couldn't create texture for saved screen!\n" ));
504 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
506 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
507 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
508 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
509 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
510 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
512 glReadBuffer(GL_FRONT);
514 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_viewport_x, GL_viewport_y,
515 GL_viewport_w, GL_viewport_h, 0);
517 if (Gr_opengl_mouse_saved) {
518 int x = Gr_opengl_mouse_saved_x;
519 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
521 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
522 Gr_opengl_mouse_saved_h, GL_RGB, GL_UNSIGNED_BYTE,
523 Gr_opengl_mouse_saved_data);
526 glBindTexture(GL_TEXTURE_2D, 0);
531 void gr_opengl1_restore_screen(int)
535 if ( !Gr_saved_screen_tex ) {
542 int w = fl2i(GL_viewport_w / GL_viewport_scale_w + 0.5f);
543 int h = fl2i(GL_viewport_h / GL_viewport_scale_h + 0.5f);
545 const int tex_coord[] = { 0, 1, 0, 0, 1, 1, 1, 0 }; // y-flipped
546 const int ver_coord[] = { x, y, x, h, w, y, w, h };
548 glColor4ub(255, 255, 255, 255);
550 glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
552 opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
554 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
555 glEnableClientState(GL_VERTEX_ARRAY);
557 glTexCoordPointer(2, GL_INT, 0, &tex_coord);
558 glVertexPointer(2, GL_INT, 0, &ver_coord);
560 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
562 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
563 glDisableClientState(GL_VERTEX_ARRAY);
565 glBindTexture(GL_TEXTURE_2D, 0);
568 void gr_opengl1_free_screen(int)
570 if (Gr_saved_screen_tex) {
571 glDeleteTextures(1, &Gr_saved_screen_tex);
572 Gr_saved_screen_tex = 0;
576 void gr_opengl1_dump_frame_start(int first_frame, int frames_between_dumps)
581 void gr_opengl1_dump_frame_stop()
586 void gr_opengl1_dump_frame()
591 static int GL_stream_w = 0;
592 static int GL_stream_h = 0;
593 static bool GL_stream_scale = false;
594 static float GL_stream_scale_by = 1.0f;
596 static rb_t GL_stream[4];
598 void gr_opengl1_stream_start(int x, int y, int w, int h)
604 if (gr_screen.use_sections) {
605 mprintf(("GR_STREAM: Bitmap sections not supported\n"));
609 int tex_w = next_pow2(w);
610 int tex_h = next_pow2(h);
612 glGenTextures(1, &GL_stream_tex);
614 glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
616 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
617 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
618 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
619 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
621 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, NULL);
623 glBindTexture(GL_TEXTURE_2D, 0);
625 uint scale = os_config_read_uint("Video", "ScaleMovies", 1);
628 GL_stream_scale = true;
630 GL_stream_scale_by = GL_viewport_w / i2fl(w);
632 GL_stream_scale = false;
638 sx = scale ? 0 : ((gr_screen.max_w - w) / 2);
644 sy = scale ? ((480 - h) / 2) : ((gr_screen.max_h - h) / 2);
652 GL_stream[0].x = i2fl(sx);
653 GL_stream[0].y = i2fl(sy);
654 GL_stream[0].u = 0.0f;
655 GL_stream[0].v = 0.0f;
657 GL_stream[1].x = i2fl(sx);
658 GL_stream[1].y = i2fl(sy + h);
659 GL_stream[1].u = 0.0f;
660 GL_stream[1].v = i2fl(h) / i2fl(tex_h);
662 GL_stream[2].x = i2fl(sx + w);
663 GL_stream[2].y = i2fl(sy);
664 GL_stream[2].u = i2fl(w) / i2fl(tex_w);
665 GL_stream[2].v = 0.0f;
667 GL_stream[3].x = i2fl(sx + w);
668 GL_stream[3].y = i2fl(sy + h);
669 GL_stream[3].u = i2fl(w) / i2fl(tex_w);
670 GL_stream[3].v = i2fl(h) / i2fl(tex_h);
672 glDisable(GL_DEPTH_TEST);
675 void gr_opengl1_stream_frame(ubyte *frame)
677 if ( !GL_stream_tex ) {
681 glEnableClientState(GL_VERTEX_ARRAY);
682 glVertexPointer(2, GL_FLOAT, sizeof(rb_t), &GL_stream[0].x);
684 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
685 glTexCoordPointer(2, GL_FLOAT, sizeof(rb_t), &GL_stream[0].u);
687 glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
689 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GL_stream_w, GL_stream_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, frame);
691 if (GL_stream_scale) {
694 glScalef(GL_stream_scale_by, GL_stream_scale_by, 1.0f);
697 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
699 if (GL_stream_scale) {
703 glBindTexture(GL_TEXTURE_2D, 0);
705 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
706 glDisableClientState(GL_VERTEX_ARRAY);
709 void gr_opengl1_stream_stop()
712 glBindTexture(GL_TEXTURE_2D, 0);
713 glDeleteTextures(1, &GL_stream_tex);
716 glEnable(GL_DEPTH_TEST);
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;
759 // clear screen once to fix issues with edges on non-4:3