From efbf0720e60c337f28828d45a79ab5071790532c Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Fri, 17 Jun 2016 18:42:25 -0400 Subject: [PATCH] cleanup, fixes, and optimizations - remove secondary color fog mode in GL1 - remove old/dead code - optimize drawing functions a bit - let GL do more of the clipping work (for aabitmaps) - reorganize GL1 texture fucntions to better match previous GL2 fixes - remove unnecessary -1 from SDL_snprintf's in cmdline - fix b0rked textures in wxGL - faster drawing of mouse cursor in flip() - glClear after flip in GL1 (better fullscreen/window fix) - fix scaling of movies on viewport resize in GL1 - remove u/v_scale from GL2 (always 1.0 anyway) - set_cull(1) in wxGL during init - proper cleanup() for wxGL --- include/grgl1.h | 3 - include/grgl2.h | 4 +- include/gropenglinternal.h | 2 - src/cmdline/cmdline.cpp | 4 +- src/graphics/grgl1.cpp | 98 ++++++++------ src/graphics/grgl1render.cpp | 199 ++++++--------------------- src/graphics/grgl1texture.cpp | 244 ++++++++++++++-------------------- src/graphics/grgl2.cpp | 32 ++--- src/graphics/grgl2render.cpp | 236 +++++++------------------------- src/graphics/grgl2shader.cpp | 2 +- src/graphics/grgl2texture.cpp | 20 +-- src/graphics/gropengl.cpp | 3 - src/graphics/grwxgl.cpp | 49 ++++--- 13 files changed, 296 insertions(+), 600 deletions(-) diff --git a/include/grgl1.h b/include/grgl1.h index e3210be..10188cd 100644 --- a/include/grgl1.h +++ b/include/grgl1.h @@ -12,9 +12,6 @@ #include "pstypes.h" - -extern PFNGLSECONDARYCOLORPOINTERPROC vglSecondaryColorPointer; - int opengl1_init(); void opengl1_cleanup(); diff --git a/include/grgl2.h b/include/grgl2.h index 188e3aa..073583d 100644 --- a/include/grgl2.h +++ b/include/grgl2.h @@ -20,7 +20,7 @@ void opengl2_tcache_init(); void opengl2_tcache_cleanup(); void opengl2_tcache_frame(); void opengl2_tcache_flush(); -int opengl2_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int fail_on_full); +int opengl2_tcache_set(int bitmap_id, int bitmap_type, int fail_on_full = 0); void opengl2_set_texture_state(gr_texture_source ts); // shader program types @@ -47,8 +47,6 @@ void opengl2_shader_cleanup(); void opengl2_shader_use(sdr_prog_t prog); void opengl2_shader_update(); -void opengl2_error_check(const char *name, int lno); - // gr_* pointer functions void gr_opengl2_flip(); void gr_opengl2_set_clip(int x, int y, int w, int h); diff --git a/include/gropenglinternal.h b/include/gropenglinternal.h index c94775e..1748fce 100644 --- a/include/gropenglinternal.h +++ b/include/gropenglinternal.h @@ -39,8 +39,6 @@ extern volatile int GL_deactivate; extern SDL_Window *GL_window; extern SDL_GLContext GL_context; -extern int GL_version; - extern int GL_viewport_x; extern int GL_viewport_y; extern int GL_viewport_w; diff --git a/src/cmdline/cmdline.cpp b/src/cmdline/cmdline.cpp index d032f1d..1ab1e86 100644 --- a/src/cmdline/cmdline.cpp +++ b/src/cmdline/cmdline.cpp @@ -378,14 +378,14 @@ static void os_parse_parms(char *cmdline) for (parmp = GET_FIRST(&Parm_list); parmp !=END_OF_LIST(&Parm_list); parmp = GET_NEXT(parmp) ) { // check with space to make sure we get the correct option name - SDL_snprintf(pname, SDL_arraysize(pname)-1, "%s ", parmp->name); + SDL_snprintf(pname, SDL_arraysize(pname), "%s ", parmp->name); cmdline_offset = strstr(cmdline, pname); if (cmdline_offset) { cmdline_offset += strlen(parmp->name); } else if (parmp->name2 != NULL) { // check with space to make sure we get the correct option name - SDL_snprintf(pname, SDL_arraysize(pname)-1, "%s ", parmp->name2); + SDL_snprintf(pname, SDL_arraysize(pname), "%s ", parmp->name2); cmdline_offset = strstr(cmdline, pname); if (cmdline_offset) { diff --git a/src/graphics/grgl1.cpp b/src/graphics/grgl1.cpp index 51b287a..0c5692d 100644 --- a/src/graphics/grgl1.cpp +++ b/src/graphics/grgl1.cpp @@ -11,13 +11,9 @@ #include "gropengl.h" #include "gropenglinternal.h" #include "grgl1.h" -#include "2d.h" #include "mouse.h" -#include "pstypes.h" -#include "cfile.h" #include "bmpman.h" #include "grinternal.h" -#include "osapi.h" #include "osregistry.h" @@ -35,9 +31,6 @@ static int Gr_opengl_mouse_saved_h = 0; static ubyte *Gr_opengl_mouse_saved_data = NULL; -PFNGLSECONDARYCOLORPOINTERPROC vglSecondaryColorPointer = NULL; - - static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1; static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1; @@ -252,59 +245,85 @@ void gr_opengl1_flip() return; } - gr_reset_clip(); + gr_opengl_reset_clip(); mouse_eval_deltas(); Gr_opengl_mouse_saved = 0; - if ( mouse_is_visible() ) { + if ( mouse_is_visible() ) { int mx, my; mouse_get_pos( &mx, &my ); - gr_opengl1_save_mouse_area(mx,my,32,32); + gr_opengl1_save_mouse_area(mx, my, 32, 32); + + float u_scale, v_scale; + + if ( opengl1_tcache_set(Gr_cursor, TCACHE_TYPE_BITMAP_INTERFACE, &u_scale, &v_scale) ) { + opengl1_set_state(TEXTURE_SOURCE_DECAL, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE); + + int bw, bh; + bm_get_info(Gr_cursor, &bw, &bh); + + int x = mx; + int y = my; + int w = mx + bw; + int h = my + bh; - if (Gr_cursor == -1) { + const float tex_coord[] = { 0.0f, 0.0f, 0.0f, 1.0f * v_scale, + 1.0f * u_scale, 0.0f, 1.0f * u_scale, + 1.0f * v_scale }; + const int ver_coord[] = { x, y, x, h, w, y, w, h }; + + glColor4ub(255, 255, 255, 255); + + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + + glTexCoordPointer(2, GL_FLOAT, 0, &tex_coord); + glVertexPointer(2, GL_INT, 0, &ver_coord); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + } #ifndef NDEBUG - gr_set_color(255,255,255); - gr_line(mx, my, mx+7, my + 7); - gr_line(mx, my, mx+5, my ); - gr_line(mx, my, mx, my+5); + else { + gr_set_color(255,255,255); + gr_opengl1_line(mx, my, mx+7, my + 7); + gr_opengl1_line(mx, my, mx+5, my ); + gr_opengl1_line(mx, my, mx, my+5); + } #endif - } else { - gr_set_bitmap(Gr_cursor); - gr_bitmap(mx, my); - } } #ifndef NDEBUG - GLenum error = GL_NO_ERROR; - - do { - error = glGetError(); + GLenum error = glGetError(); - if (error != GL_NO_ERROR) { - nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error)); - } - } while (error != GL_NO_ERROR); + if (error != GL_NO_ERROR) { + mprintf(("!!DEBUG!! OpenGL Error: %d\n", error)); + } #endif SDL_GL_SwapWindow(GL_window); + glClear(GL_COLOR_BUFFER_BIT); + opengl1_tcache_frame(); int cnt = GL_activate; - if ( cnt ) { - GL_activate-=cnt; + + if (cnt) { + GL_activate -= cnt; opengl1_tcache_flush(); - // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */ } cnt = GL_deactivate; - if ( cnt ) { - GL_deactivate-=cnt; - // gr_opengl_clip_cursor(0); /* mouse grab, see opengl_activate */ + + if (cnt) { + GL_deactivate -= cnt; } } @@ -487,7 +506,7 @@ void gr_opengl1_save_mouse_area(int x, int y, int w, int h) int gr_opengl1_save_screen() { - gr_reset_clip(); + gr_opengl_reset_clip(); if (Gr_saved_screen_tex) { mprintf(( "Screen already saved!\n" )); @@ -530,10 +549,10 @@ int gr_opengl1_save_screen() void gr_opengl1_restore_screen(int) { - gr_reset_clip(); + gr_opengl_reset_clip(); if ( !Gr_saved_screen_tex ) { - gr_clear(); + gr_opengl_clear(); return; } @@ -626,7 +645,6 @@ void gr_opengl1_stream_start(int x, int y, int w, int h) if (scale) { GL_stream_scale = true; - GL_stream_scale_by = GL_viewport_w / i2fl(w); } else { GL_stream_scale = false; @@ -756,6 +774,8 @@ void gr_opengl1_set_viewport(int width, int height) Gr_opengl_mouse_saved_data = NULL; } - // clear screen once to fix issues with edges on non-4:3 - gr_opengl_clear(); + // adjust scale factor for gr_stream (movies) + if (GL_stream_tex && GL_stream_scale) { + GL_stream_scale_by = GL_viewport_w / i2fl(GL_stream_w); + } } diff --git a/src/graphics/grgl1render.cpp b/src/graphics/grgl1render.cpp index 86f5e2a..f982198 100644 --- a/src/graphics/grgl1render.cpp +++ b/src/graphics/grgl1render.cpp @@ -26,74 +26,39 @@ static void opengl1_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a) { - int saved_zbuf; - vertex v[4]; - vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]}; - - saved_zbuf = gr_zbuffer_get(); + int saved_zbuf = gr_zbuffer_get(); - // start the frame, no zbuffering, no culling - g3_start_frame(1); + // no zbuffering, no culling gr_zbuffer_set(GR_ZBUFF_NONE); - gr_set_cull(0); - - // stuff coords - v[0].sx = i2fl(x); - v[0].sy = i2fl(y); - v[0].sw = 0.0f; - v[0].u = 0.0f; - v[0].v = 0.0f; - v[0].flags = PF_PROJECTED; - v[0].codes = 0; - v[0].r = (ubyte)r; - v[0].g = (ubyte)g; - v[0].b = (ubyte)b; - v[0].a = (ubyte)a; - - v[1].sx = i2fl(x + w); - v[1].sy = i2fl(y); - v[1].sw = 0.0f; - v[1].u = 0.0f; - v[1].v = 0.0f; - v[1].flags = PF_PROJECTED; - v[1].codes = 0; - v[1].r = (ubyte)r; - v[1].g = (ubyte)g; - v[1].b = (ubyte)b; - v[1].a = (ubyte)a; - - v[2].sx = i2fl(x + w); - v[2].sy = i2fl(y + h); - v[2].sw = 0.0f; - v[2].u = 0.0f; - v[2].v = 0.0f; - v[2].flags = PF_PROJECTED; - v[2].codes = 0; - v[2].r = (ubyte)r; - v[2].g = (ubyte)g; - v[2].b = (ubyte)b; - v[2].a = (ubyte)a; - - v[3].sx = i2fl(x); - v[3].sy = i2fl(y + h); - v[3].sw = 0.0f; - v[3].u = 0.0f; - v[3].v = 0.0f; - v[3].flags = PF_PROJECTED; - v[3].codes = 0; - v[3].r = (ubyte)r; - v[3].g = (ubyte)g; - v[3].b = (ubyte)b; - v[3].a = (ubyte)a; - - // draw the polys - g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f); - - g3_end_frame(); + gr_opengl_set_cull(0); + + opengl_alloc_render_buffer(4); + + render_buffer[0].x = i2fl(x); + render_buffer[0].y = i2fl(y); + + render_buffer[1].x = i2fl(x); + render_buffer[1].y = i2fl(y + h); + + render_buffer[2].x = i2fl(x + w); + render_buffer[2].y = i2fl(y); + + render_buffer[3].x = i2fl(x + w); + render_buffer[3].y = i2fl(y + h); + + glColor4ub((ubyte)r, (ubyte)g, (ubyte)b, (ubyte)a); + + glEnableClientState(GL_VERTEX_ARRAY); + + glVertexPointer(2, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glDisableClientState(GL_VERTEX_ARRAY); // restore zbuffer and culling gr_zbuffer_set(saved_zbuf); - gr_set_cull(1); + gr_opengl_set_cull(1); } static void opengl1_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy) @@ -375,94 +340,26 @@ void gr_opengl1_shade(int x,int y,int w,int h) float shade2 = 6.0f; r = fl2i(gr_screen.current_shader.r*255.0f*shade1); - if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255; + CAP(r, 0, 255); g = fl2i(gr_screen.current_shader.g*255.0f*shade1); - if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255; + CAP(g, 0, 255); b = fl2i(gr_screen.current_shader.b*255.0f*shade1); - if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255; + CAP(b, 0, 255); a = fl2i(gr_screen.current_shader.c*255.0f*shade2); - if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255; + CAP(a, 0, 255); opengl1_rect_internal(x, y, w, h, r, g, b, a); } void gr_opengl1_aabitmap_ex(int x,int y,int w,int h,int sx,int sy) { - int reclip; - #ifndef NDEBUG - int count = 0; - #endif - - int dx1=x, dx2=x+w-1; - int dy1=y, dy2=y+h-1; - - int bw, bh; - bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL ); - - do { - reclip = 0; - #ifndef NDEBUG - if ( count > 1 ) Int3(); - count++; - #endif - - if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return; - if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return; - if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; } - if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; } - if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; } - if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; } - - if ( sx < 0 ) { - dx1 -= sx; - sx = 0; - reclip = 1; - } - - if ( sy < 0 ) { - dy1 -= sy; - sy = 0; - reclip = 1; - } - - w = dx2-dx1+1; - h = dy2-dy1+1; - - if ( sx + w > bw ) { - w = bw - sx; - dx2 = dx1 + w - 1; - } + if ( (x > gr_screen.clip_right ) || ((x+w-1) < gr_screen.clip_left) ) + return; - if ( sy + h > bh ) { - h = bh - sy; - dy2 = dy1 + h - 1; - } + if ( (y > gr_screen.clip_bottom ) || ((y+h-1) < gr_screen.clip_top) ) + return; - if ( w < 1 ) return; // clipped away! - if ( h < 1 ) return; // clipped away! - - } while (reclip); - - // Make sure clipping algorithm works - #ifndef NDEBUG - SDL_assert( w > 0 ); - SDL_assert( h > 0 ); - SDL_assert( w == (dx2-dx1+1) ); - SDL_assert( h == (dy2-dy1+1) ); - SDL_assert( sx >= 0 ); - SDL_assert( sy >= 0 ); - SDL_assert( sx+w <= bw ); - SDL_assert( sy+h <= bh ); - SDL_assert( dx2 >= dx1 ); - SDL_assert( dy2 >= dy1 ); - SDL_assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) ); - SDL_assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) ); - SDL_assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) ); - SDL_assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) ); - #endif - - // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions. - opengl1_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy); + opengl1_aabitmap_ex_internal(x, y, w, h, sx, sy); } void gr_opengl1_aabitmap(int x, int y) @@ -470,24 +367,8 @@ void gr_opengl1_aabitmap(int x, int y) int w, h; bm_get_info( gr_screen.current_bitmap, &w, &h, NULL ); - int dx1=x, dx2=x+w-1; - int dy1=y, dy2=y+h-1; - int sx=0, sy=0; - - if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return; - if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return; - if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; } - if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; } - if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; } - if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; } - - if ( sx < 0 ) return; - if ( sy < 0 ) return; - if ( sx >= w ) return; - if ( sy >= h ) return; - - // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2) - gr_opengl1_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy); + + gr_opengl1_aabitmap_ex(x, y, w, h, 0, 0); } void gr_opengl1_string( int sx, int sy, const char *s ) @@ -824,7 +705,7 @@ void gr_opengl1_circle( int xc, int yc, int d ) void gr_opengl1_pixel(int x, int y) { - gr_line(x,y,x,y); + gr_opengl1_line(x, y, x, y); } diff --git a/src/graphics/grgl1texture.cpp b/src/graphics/grgl1texture.cpp index db4f91d..9b2cee0 100644 --- a/src/graphics/grgl1texture.cpp +++ b/src/graphics/grgl1texture.cpp @@ -323,7 +323,7 @@ static void opengl1_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_ *h_out = tex_h; } -// data == start of bitmap data +// bmp == bitmap structure with w, h, and data // sx == x offset into bitmap // sy == y offset into bitmap // src_w == absolute width of section on source bitmap @@ -332,34 +332,35 @@ static void opengl1_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_ // bmap_h == height of source bitmap // tex_w == width of final texture // tex_h == height of final texture -static int opengl1_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 resize, int fail_on_full) +static int opengl1_create_texture_sub(int bitmap_handle, int bitmap_type, bitmap *bmp, tcache_slot_opengl *t, int sx, int sy, int src_w, int src_h, int tex_w, int tex_h, bool reload, bool resize, int fail_on_full) { - int ret_val = 1; - int size = 0; - int i, j; - ubyte *bmp_data = ((ubyte*)data); - ubyte *texmem = NULL, *texmemp; - // bogus - if((t == NULL) || (bmp_data == NULL)){ + if ( (bmp == NULL) || (t == NULL) ) { return 0; } - if ( t->used_this_frame == GL_frame_count ) { - mprintf(( "ARGHH!!! Texture already used this frame! Cannot free it!\n" )); + if (t->used_this_frame == GL_frame_count) { + mprintf(("ARGHH!!! Texture already used this frame! Cannot free it!\n")); return 0; } - if ( !reload ) { - // gah - if(!opengl1_free_texture(t)){ + + if ( !reload ) { + if ( !opengl1_free_texture(t) ) { + return 0; + } + + glGenTextures(1, &t->texture_handle); + + if ( !t->texture_handle ) { + nprintf(("Error", "!!DEBUG!! t->texture_handle == 0")); return 0; } } switch (bitmap_type) { case TCACHE_TYPE_AABITMAP: - t->u_scale = (float)bmap_w / (float)tex_w; - t->v_scale = (float)bmap_h / (float)tex_h; + t->u_scale = (float)bmp->w / (float)tex_w; + t->v_scale = (float)bmp->h / (float)tex_h; break; case TCACHE_TYPE_BITMAP_INTERFACE: @@ -374,54 +375,44 @@ static int opengl1_create_texture_sub(int bitmap_type, int texture_handle, ushor break; } - if (!reload) { - glGenTextures (1, &t->texture_handle); - } + t->texture_mode = TEXTURE_SOURCE_NO_FILTERING; - if (t->texture_handle == 0) { - nprintf(("Error", "!!DEBUG!! t->texture_handle == 0")); - return 0; - } + glBindTexture(GL_TEXTURE_2D, t->texture_handle); - GL_bound_texture = t; + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - GL_bound_texture->texture_mode = (gr_texture_source) -1; + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glBindTexture (GL_TEXTURE_2D, t->texture_handle); - - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - /* this should be set next anyway */ -// if (GL_current_texture_source != TEXTURE_SOURCE_NONE) { -// gr_opengl_set_texture_state(GL_current_texture_source); -// } + ubyte *bmp_data = (ubyte*)bmp->data; + ubyte *texmem = NULL, *texmemp; + int i, j; + int size = 0; switch (bitmap_type) { - case TCACHE_TYPE_AABITMAP: - { - texmem = (ubyte *) malloc (tex_w*tex_h); + case TCACHE_TYPE_AABITMAP: { + texmem = (ubyte *) malloc(tex_w * tex_h); texmemp = texmem; - for (i=0;ih) && (j < bmp->w) ) { + *texmemp++ = GL_xlat[bmp_data[i*bmp->w+j]]; } else { *texmemp++ = 0; } } } - size = tex_w*tex_h; + size = tex_w * tex_h; - if (!reload) { - glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem); + if (reload) { + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_ALPHA, GL_UNSIGNED_BYTE, texmem); } else { - glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_ALPHA, GL_UNSIGNED_BYTE, texmem); + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texmem); } free (texmem); @@ -430,18 +421,17 @@ static int opengl1_create_texture_sub(int bitmap_type, int texture_handle, ushor } case TCACHE_TYPE_BITMAP_INTERFACE: - case TCACHE_TYPE_BITMAP_SECTION: - { + case TCACHE_TYPE_BITMAP_SECTION: { // if we aren't resizing in any way then we can just use bmp_data directly - if ( resize ) { - texmem = (ubyte *) malloc (tex_w*tex_h*2); + if (resize) { + texmem = (ubyte *) malloc(tex_w * tex_h * 2); texmemp = texmem; - for (i=0;iw+(j+sx))*2+0]; + *texmemp++ = bmp_data[((i+sy)*bmp->w+(j+sx))*2+1]; } else { *texmemp++ = 0; *texmemp++ = 0; @@ -450,63 +440,65 @@ static int opengl1_create_texture_sub(int bitmap_type, int texture_handle, ushor } } - size = tex_w*tex_h*2; + size = tex_w * tex_h * 2; - if (!reload) { - glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); + if (reload) { + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); } else { - glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); } - if (texmem != NULL) + if (texmem) { free(texmem); + } break; } - default: - { + default: { // if we aren't resizing then we can just use bmp_data directly - if ( resize ) { - texmem = (ubyte *) malloc (tex_w*tex_h*2); + if (resize) { + texmem = (ubyte *) malloc (tex_w * tex_h * 2); texmemp = texmem; - SDL_assert( texmem != NULL ); - - fix u, utmp, v, du, dv; + SDL_assert(texmem); - u = v = 0; + fix u = 0, utmp, v = 0, du, dv; - du = ( (bmap_w-1)*F1_0 ) / tex_w; - dv = ( (bmap_h-1)*F1_0 ) / tex_h; + du = ((bmp->w - 1) * F1_0) / tex_w; + dv = ((bmp->h - 1) * F1_0) / tex_h; - for (j=0;jw+f2i(utmp))*2+0]; + *texmemp++ = bmp_data[(f2i(v)*bmp->w+f2i(utmp))*2+1]; + utmp += du; } + v += dv; } } - size = tex_w*tex_h*2; + size = tex_w * tex_h * 2; - if (!reload) { - glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); + if (reload) { + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); } else { - glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, tex_w, tex_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (resize) ? texmem : bmp_data); } - if (texmem != NULL) + if (texmem) { free(texmem); + } break; } } - t->bitmap_id = texture_handle; + t->bitmap_id = bitmap_handle; t->time_created = GL_frame_count; t->used_this_frame = 0; t->size = size; @@ -517,21 +509,18 @@ static int opengl1_create_texture_sub(int bitmap_type, int texture_handle, ushor Gr_textures_in += t->size; } - return ret_val; + return 1; } static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full) { - ubyte flags; - bitmap *bmp; + ubyte flags = 0; int final_w, final_h; ubyte bpp = 16; - int reload = 0; - int resize = 0; - int cull_size = 0; + bool resize = false; + bool cull_size = false; // setup texture/bitmap flags - flags = 0; switch(bitmap_type){ case TCACHE_TYPE_AABITMAP: flags |= BMP_AABITMAP; @@ -539,7 +528,7 @@ static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slo break; case TCACHE_TYPE_NORMAL: flags |= BMP_TEX_OTHER; - cull_size = 1; + cull_size = true; break; case TCACHE_TYPE_BITMAP_INTERFACE: case TCACHE_TYPE_XPARENT: @@ -551,7 +540,7 @@ static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slo } // lock the bitmap into the proper format - bmp = bm_lock(bitmap_handle, bpp, flags); + bitmap *bmp = bm_lock(bitmap_handle, bpp, flags); if ( bmp == NULL ) { mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle )); return 0; @@ -560,17 +549,16 @@ static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slo int max_w = bmp->w; int max_h = bmp->h; - if (cull_size) { + if ( cull_size && (Detail.hardware_textures < 4) ) { // if we are going to cull the size then we need to force a resize - if (Detail.hardware_textures < 4) { - resize = 1; + resize = true; - // Detail.debris_culling goes from 0 to 4. - max_w /= (16 >> Detail.hardware_textures); - max_h /= (16 >> Detail.hardware_textures); - } - } + // Detail.hardware_textures goes form 0 to 4 + int val = 16 >> Detail.hardware_textures; + max_w /= val; + max_h /= val; + } // get final texture size as it will be allocated as a DD surface opengl1_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h); @@ -582,25 +570,20 @@ static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slo // if we don't have to resize the image (to get power of 2, etc.) then skip that extra work if ( (max_w != final_w) || (max_h != final_h) ) { - resize = 1; + resize = true; } - // if this tcache slot has no bitmap - if ( tslot->bitmap_id < 0) { - reload = 0; - } - // different bitmap altogether - determine if the new one can use the old one's slot - else if (tslot->bitmap_id != bitmap_handle) { - if((final_w == tslot->w) && (final_h == tslot->h)){ - reload = 1; - //ml_printf("Reloading texture %d\n", bitmap_handle); - } else { - reload = 0; + bool reload = false; + + // see if we can reuse this slot for a new bitmap + if ( tslot->texture_handle && (tslot->bitmap_id != bitmap_handle) ) { + if ( (final_w == tslot->w) && (final_h == tslot->h) ) { + reload = true; } } // call the helper - int ret_val = opengl1_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, 0, 0, bmp->w, bmp->h, bmp->w, bmp->h, final_w, final_h, tslot, reload, resize, fail_on_full); + int ret_val = opengl1_create_texture_sub(bitmap_handle, bitmap_type, bmp, tslot, 0, 0, bmp->w, bmp->h, final_w, final_h, reload, resize, fail_on_full); // unlock the bitmap bm_unlock(bitmap_handle); @@ -610,12 +593,9 @@ static int opengl1_create_texture(int bitmap_handle, int bitmap_type, tcache_slo static int opengl1_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full) { - ubyte flags; - bitmap *bmp; int final_w, final_h; int section_x, section_y; - int reload = 0; - int resize = 1; + bool resize = true; SDL_assert( gr_screen.use_sections ); @@ -624,10 +604,9 @@ static int opengl1_create_texture_sectioned(int bitmap_handle, int bitmap_type, if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){ bitmap_type = TCACHE_TYPE_BITMAP_SECTION; } - flags = BMP_TEX_XPARENT; // lock the bitmap in the proper format - bmp = bm_lock(bitmap_handle, 16, flags); + bitmap *bmp = bm_lock(bitmap_handle, 16, BMP_TEX_XPARENT); if ( bmp == NULL ) { mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle )); return 0; @@ -645,24 +624,20 @@ static int opengl1_create_texture_sectioned(int bitmap_handle, int bitmap_type, // if we don't have to resize the image (to get power of 2, etc.) then skip that extra work if ( (bmp->sections.num_x == 1) && (bmp->sections.num_y == 1) && (section_x == final_w) && (section_y == final_h) ) { - resize = 0; + resize = false; } - // if this tcache slot has no bitmap - if ( tslot->bitmap_id < 0) { - reload = 0; - } - // different bitmap altogether - determine if the new one can use the old one's slot - else if (tslot->bitmap_id != bitmap_handle) { - if((final_w == tslot->w) && (final_h == tslot->h)){ - reload = 1; - } else { - reload = 0; + bool reload = false; + + // see if we can reuse this slot for a new bitmap + if ( tslot->texture_handle && (tslot->bitmap_id != bitmap_handle) ) { + if ( (final_w == tslot->w) && (final_h == tslot->h) ) { + reload = true; } } // call the helper - int ret_val = opengl1_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, final_w, final_h, tslot, reload, resize, fail_on_full); + int ret_val = opengl1_create_texture_sub(bitmap_handle, bitmap_type, bmp, tslot, bmp->sections.sx[sx], bmp->sections.sy[sy], section_x, section_y, final_w, final_h, reload, resize, fail_on_full); // unlock the bitmap bm_unlock(bitmap_handle); @@ -774,11 +749,6 @@ int opengl1_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_ glBindTexture (GL_TEXTURE_2D, t->texture_handle ); - /* this should be set next anyway */ -// if (GL_current_texture_source != TEXTURE_SOURCE_NONE) { -// gr_opengl_set_texture_state(GL_current_texture_source); -// } - GL_last_bitmap_id = t->bitmap_id; GL_last_bitmap_type = bitmap_type; GL_last_section_x = sx; @@ -805,19 +775,11 @@ int opengl1_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_ void gr_opengl1_preload_init() { -// if (gr_screen.mode != GR_OPENGL) { -// return; -// } - opengl1_tcache_flush(); } int gr_opengl1_preload(int bitmap_num, int is_aabitmap) { -// if ( gr_screen.mode != GR_OPENGL) { -// return 0; -// } - if ( !GL_should_preload ) { return 0; } diff --git a/src/graphics/grgl2.cpp b/src/graphics/grgl2.cpp index 197403e..23eadf9 100644 --- a/src/graphics/grgl2.cpp +++ b/src/graphics/grgl2.cpp @@ -14,10 +14,8 @@ #include "2d.h" #include "mouse.h" #include "pstypes.h" -#include "cfile.h" #include "bmpman.h" #include "grinternal.h" -#include "osapi.h" #include "osregistry.h" @@ -193,8 +191,6 @@ static int opengl2_create_framebuffer() glBindFramebuffer(GL_FRAMEBUFFER, 0); if (status != GL_FRAMEBUFFER_COMPLETE) { - glBindFramebuffer(GL_FRAMEBUFFER, 0); - if (FB_texture) { glDeleteTextures(1, &FB_texture); FB_texture = 0; @@ -322,10 +318,6 @@ void gr_opengl2_flip() return; } -#ifndef NDEBUG - static bool show_cursor = false; -#endif - glBindFramebuffer(GL_FRAMEBUFFER, 0); gr_opengl_reset_clip(); @@ -369,9 +361,7 @@ void gr_opengl2_flip() mouse_get_pos(&mx, &my); - float u_scale, v_scale; - - if ( opengl2_tcache_set(Gr_cursor, TCACHE_TYPE_BITMAP_INTERFACE, &u_scale, &v_scale, 0) ) { + if ( opengl2_tcache_set(Gr_cursor, TCACHE_TYPE_BITMAP_INTERFACE) ) { opengl2_set_state(TEXTURE_SOURCE_DECAL, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE); int bw, bh; @@ -399,23 +389,21 @@ void gr_opengl2_flip() glDisableVertexAttribArray(SDRI_POSITION); } #ifndef NDEBUG - else if ( !show_cursor ) { - SDL_ShowCursor(1); - show_cursor = true; + else { + gr_set_color(255,255,255); + gr_opengl2_line(mx, my, mx+7, my + 7); + gr_opengl2_line(mx, my, mx+5, my ); + gr_opengl2_line(mx, my, mx, my+5); } #endif } #ifndef NDEBUG - GLenum error = GL_NO_ERROR; - - do { - error = glGetError(); + GLenum error = glGetError(); - if (error != GL_NO_ERROR) { - nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error)); - } - } while (error != GL_NO_ERROR); + if (error != GL_NO_ERROR) { + mprintf(("!!DEBUG!! OpenGL Error: %d\n", error)); + } #endif SDL_GL_SwapWindow(GL_window); diff --git a/src/graphics/grgl2render.cpp b/src/graphics/grgl2render.cpp index 3f3196e..f144213 100644 --- a/src/graphics/grgl2render.cpp +++ b/src/graphics/grgl2render.cpp @@ -23,17 +23,10 @@ #define NEBULA_COLORS 20 -int cntZval = 0; -int cntCorrect = 0; -int cntAlpha = 0; -int cntNebula = 0; -int cntRamp = 0; -int cntRGB = 0; static void opengl2_tmapper_internal(int nv, vertex **verts, uint flags, int is_scaler) { int i; - float u_scale = 1.0f, v_scale = 1.0f; gr_texture_source texture_source = TEXTURE_SOURCE_NONE; gr_alpha_blend alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA; @@ -78,9 +71,7 @@ static void opengl2_tmapper_internal(int nv, vertex **verts, uint flags, int is_ } if (flags & TMAP_FLAG_TEXTURED) { - if ( !opengl2_tcache_set(gr_screen.current_bitmap, tmap_type, &u_scale, - &v_scale, 0) ) - { + if ( !opengl2_tcache_set(gr_screen.current_bitmap, tmap_type) ) { mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" )); return; } @@ -199,8 +190,8 @@ static void opengl2_tmapper_internal(int nv, vertex **verts, uint flags, int is_ sy = (va->sy * 16.0f + oy) / 16.0f; if (bTextured) { - render_buffer[rb_offset].u = va->u * u_scale; - render_buffer[rb_offset].v = va->v * v_scale; + render_buffer[rb_offset].u = va->u; + render_buffer[rb_offset].v = va->v; } render_buffer[rb_offset].x = sx * rhw; @@ -250,70 +241,36 @@ static void opengl2_tmapper_internal(int nv, vertex **verts, uint flags, int is_ void opengl2_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a) { - int saved_zbuf; - vertex v[4]; - vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]}; - - saved_zbuf = gr_zbuffer_get(); + int saved_zbuf = gr_zbuffer_get(); - // start the frame, no zbuffering, no culling - g3_start_frame(1); + // no zbuffering, no culling gr_zbuffer_set(GR_ZBUFF_NONE); gr_opengl_set_cull(0); - // stuff coords - v[0].sx = i2fl(x); - v[0].sy = i2fl(y); - v[0].sw = 0.0f; - v[0].u = 0.0f; - v[0].v = 0.0f; - v[0].flags = PF_PROJECTED; - v[0].codes = 0; - v[0].r = (ubyte)r; - v[0].g = (ubyte)g; - v[0].b = (ubyte)b; - v[0].a = (ubyte)a; - - v[1].sx = i2fl(x + w); - v[1].sy = i2fl(y); - v[1].sw = 0.0f; - v[1].u = 0.0f; - v[1].v = 0.0f; - v[1].flags = PF_PROJECTED; - v[1].codes = 0; - v[1].r = (ubyte)r; - v[1].g = (ubyte)g; - v[1].b = (ubyte)b; - v[1].a = (ubyte)a; - - v[2].sx = i2fl(x + w); - v[2].sy = i2fl(y + h); - v[2].sw = 0.0f; - v[2].u = 0.0f; - v[2].v = 0.0f; - v[2].flags = PF_PROJECTED; - v[2].codes = 0; - v[2].r = (ubyte)r; - v[2].g = (ubyte)g; - v[2].b = (ubyte)b; - v[2].a = (ubyte)a; - - v[3].sx = i2fl(x); - v[3].sy = i2fl(y + h); - v[3].sw = 0.0f; - v[3].u = 0.0f; - v[3].v = 0.0f; - v[3].flags = PF_PROJECTED; - v[3].codes = 0; - v[3].r = (ubyte)r; - v[3].g = (ubyte)g; - v[3].b = (ubyte)b; - v[3].a = (ubyte)a; - - // draw the polys - g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f); - - g3_end_frame(); + opengl_alloc_render_buffer(4); + + render_buffer[0].x = i2fl(x); + render_buffer[0].y = i2fl(y); + + render_buffer[1].x = i2fl(x); + render_buffer[1].y = i2fl(y + h); + + render_buffer[2].x = i2fl(x + w); + render_buffer[2].y = i2fl(y); + + render_buffer[3].x = i2fl(x + w); + render_buffer[3].y = i2fl(y + h); + + opengl2_shader_use(PROG_COLOR); + + glVertexAttrib4f(SDRI_COLOR, r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); + + glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].x); + glEnableVertexAttribArray(SDRI_POSITION); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glDisableVertexAttribArray(SDRI_POSITION); // restore zbuffer and culling gr_zbuffer_set(saved_zbuf); @@ -330,11 +287,7 @@ void opengl2_aabitmap_ex_internal(int x, int y, int w, int h, int sx, int sy) return; } - float u_scale, v_scale; - - if ( !opengl2_tcache_set(gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, - &u_scale, &v_scale, 0) ) - { + if ( !opengl2_tcache_set(gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP) ) { // Couldn't set texture mprintf(( "WARNING: Error setting aabitmap texture!\n" )); return; @@ -348,11 +301,11 @@ void opengl2_aabitmap_ex_internal(int x, int y, int w, int h, int sx, int sy) bm_get_info( gr_screen.current_bitmap, &bw, &bh ); - u0 = u_scale*i2fl(sx)/i2fl(bw); - v0 = v_scale*i2fl(sy)/i2fl(bh); + u0 = i2fl(sx)/i2fl(bw); + v0 = i2fl(sy)/i2fl(bh); - u1 = u_scale*i2fl(sx+w)/i2fl(bw); - v1 = v_scale*i2fl(sy+h)/i2fl(bh); + u1 = i2fl(sx+w)/i2fl(bw); + v1 = i2fl(sy+h)/i2fl(bh); x1 = i2fl(x+gr_screen.offset_x); y1 = i2fl(y+gr_screen.offset_y); @@ -428,81 +381,13 @@ void gr_opengl2_shade(int x, int y, int w, int h) void gr_opengl2_aabitmap_ex(int x, int y, int w, int h, int sx, int sy) { - int reclip; - #ifndef NDEBUG - int count = 0; - #endif - - int dx1=x, dx2=x+w-1; - int dy1=y, dy2=y+h-1; - - int bw, bh; - bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL ); - - do { - reclip = 0; - #ifndef NDEBUG - if ( count > 1 ) Int3(); - count++; - #endif - - if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return; - if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return; - if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; } - if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; } - if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; } - if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; } - - if ( sx < 0 ) { - dx1 -= sx; - sx = 0; - reclip = 1; - } - - if ( sy < 0 ) { - dy1 -= sy; - sy = 0; - reclip = 1; - } - - w = dx2-dx1+1; - h = dy2-dy1+1; - - if ( sx + w > bw ) { - w = bw - sx; - dx2 = dx1 + w - 1; - } + if ( (x > gr_screen.clip_right ) || ((x+w-1) < gr_screen.clip_left) ) + return; - if ( sy + h > bh ) { - h = bh - sy; - dy2 = dy1 + h - 1; - } + if ( (y > gr_screen.clip_bottom ) || ((y+h-1) < gr_screen.clip_top) ) + return; - if ( w < 1 ) return; // clipped away! - if ( h < 1 ) return; // clipped away! - - } while (reclip); - - // Make sure clipping algorithm works - #ifndef NDEBUG - SDL_assert( w > 0 ); - SDL_assert( h > 0 ); - SDL_assert( w == (dx2-dx1+1) ); - SDL_assert( h == (dy2-dy1+1) ); - SDL_assert( sx >= 0 ); - SDL_assert( sy >= 0 ); - SDL_assert( sx+w <= bw ); - SDL_assert( sy+h <= bh ); - SDL_assert( dx2 >= dx1 ); - SDL_assert( dy2 >= dy1 ); - SDL_assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) ); - SDL_assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) ); - SDL_assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) ); - SDL_assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) ); - #endif - - // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions. - opengl2_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy); + opengl2_aabitmap_ex_internal(x, y, w, h, sx, sy); } void gr_opengl2_aabitmap(int x, int y) @@ -510,46 +395,15 @@ void gr_opengl2_aabitmap(int x, int y) int w, h; bm_get_info( gr_screen.current_bitmap, &w, &h, NULL ); - int dx1=x, dx2=x+w-1; - int dy1=y, dy2=y+h-1; - int sx=0, sy=0; - - if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return; - if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return; - if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; } - if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; } - if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; } - if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; } - - if ( sx < 0 ) return; - if ( sy < 0 ) return; - if ( sx >= w ) return; - if ( sy >= h ) return; - - // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2) - gr_opengl2_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy); -} -void opengl2_error_check(const char *name, int lno) -{ - GLenum error = GL_NO_ERROR; - - do { - error = glGetError(); - - if (error != GL_NO_ERROR) { - nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d at %s:%d\n", error, name, lno)); - } - } while (error != GL_NO_ERROR); + gr_opengl2_aabitmap_ex(x, y, w, h, 0, 0); } - void gr_opengl2_string(int sx, int sy, const char *s) { int width, spacing, letter; int x, y; int rb_offset; - float u_scale, v_scale; float u0, u1, v0, v1; float x1, x2, y1, y2; int bw, bh; @@ -561,7 +415,7 @@ void gr_opengl2_string(int sx, int sy, const char *s) gr_set_bitmap(Current_font->bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1); - if ( !opengl2_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 0 ) ) { + if ( !opengl2_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP ) ) { // Couldn't set texture mprintf(( "WARNING: Error setting aabitmap texture!\n" )); return; @@ -654,11 +508,11 @@ void gr_opengl2_string(int sx, int sy, const char *s) x2 = x1 + i2fl(wc); y2 = y1 + i2fl(hc); - u0 = u_scale * (u * fbw); - v0 = v_scale * (v * fbh); + u0 = u * fbw; + v0 = v * fbh; - u1 = u_scale * ((u+i2fl(wc)) * fbw); - v1 = v_scale * ((v+i2fl(hc)) * fbh); + u1 = (u+i2fl(wc)) * fbw; + v1 = (v+i2fl(hc)) * fbh; // maybe go ahead and draw if (rb_offset == alocsize) { diff --git a/src/graphics/grgl2shader.cpp b/src/graphics/grgl2shader.cpp index 16541af..02ffe02 100644 --- a/src/graphics/grgl2shader.cpp +++ b/src/graphics/grgl2shader.cpp @@ -118,7 +118,7 @@ static const char f_aabitmap_src[] = "varying vec2 texCoordVar;\n" "void main()\n" "{\n" - " gl_FragColor = colorVar * texture2D(texture, texCoordVar).aaaa;\n" + " gl_FragColor = colorVar * texture2D(texture, texCoordVar).a;\n" "}\n"; static const char f_color_src[] = diff --git a/src/graphics/grgl2texture.cpp b/src/graphics/grgl2texture.cpp index 18ca596..f004a0a 100644 --- a/src/graphics/grgl2texture.cpp +++ b/src/graphics/grgl2texture.cpp @@ -23,8 +23,6 @@ static bool vram_full = false; struct tcache_slot_opengl2 { GLuint texture_handle; - float u_scale; - float v_scale; int bitmap_id; int size; int used_this_frame; @@ -60,7 +58,7 @@ void opengl2_set_texture_state(gr_texture_source ts) GL_bound_texture = NULL; glBindTexture(GL_TEXTURE_2D, 0); - opengl2_tcache_set(-1, -1, NULL, NULL, 0); + opengl2_tcache_set(-1, -1); } else if (GL_bound_texture && GL_bound_texture->texture_mode != ts) { switch (ts) { case TEXTURE_SOURCE_DECAL: @@ -203,9 +201,6 @@ static int opengl2_create_texture_sub(int bitmap_handle, int bitmap_type, bitmap } } - t->u_scale = 1.0f; - t->v_scale = 1.0f; - t->texture_mode = TEXTURE_SOURCE_NO_FILTERING; glBindTexture(GL_TEXTURE_2D, t->texture_handle); @@ -343,7 +338,7 @@ static int opengl2_create_texture(int bitmap_handle, int bitmap_type, tcache_slo case TCACHE_TYPE_NORMAL: { flags |= BMP_TEX_OTHER; - cull_size = 1; + cull_size = true; break; } @@ -404,7 +399,7 @@ static int opengl2_create_texture(int bitmap_handle, int bitmap_type, tcache_slo return ret_val; } -int opengl2_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int fail_on_full) +int opengl2_tcache_set(int bitmap_id, int bitmap_type, int fail_on_full) { if (bitmap_id < 0) { GL_last_bitmap_id = -1; @@ -427,9 +422,6 @@ int opengl2_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_ if ( (GL_last_bitmap_id == bitmap_id) && (GL_last_bitmap_type == bitmap_type) && (t->bitmap_id == bitmap_id) ) { t->used_this_frame = GL_frame_count; - *u_scale = t->u_scale; - *v_scale = t->v_scale; - return 1; } @@ -442,9 +434,6 @@ int opengl2_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_ if (ret_val && t->texture_handle && !vram_full) { glBindTexture(GL_TEXTURE_2D, t->texture_handle); - *u_scale = t->u_scale; - *v_scale = t->v_scale; - GL_bound_texture = t; GL_last_bitmap_id = t->bitmap_id; @@ -477,9 +466,8 @@ int gr_opengl2_preload(int bitmap_num, int is_aabitmap) } int bitmap_type = (is_aabitmap) ? TCACHE_TYPE_AABITMAP : TCACHE_TYPE_NORMAL; - float u_scale, v_scale; - int retval = opengl2_tcache_set(bitmap_num, bitmap_type, &u_scale, &v_scale, 1); + int retval = opengl2_tcache_set(bitmap_num, bitmap_type, 1); if ( !retval ) { mprintf(("Texture upload failed bit bitmap %d!\n", bitmap_num)); diff --git a/src/graphics/gropengl.cpp b/src/graphics/gropengl.cpp index 2b75397..602bda9 100644 --- a/src/graphics/gropengl.cpp +++ b/src/graphics/gropengl.cpp @@ -14,8 +14,6 @@ #include "gropenglinternal.h" #include "grgl1.h" #include "grgl2.h" -#include "2d.h" -#include "bmpman.h" #include "grinternal.h" #include "cmdline.h" #include "mouse.h" @@ -24,7 +22,6 @@ bool OGL_inited = false; -int GL_version = 0; SDL_Window *GL_window = NULL; SDL_GLContext GL_context; diff --git a/src/graphics/grwxgl.cpp b/src/graphics/grwxgl.cpp index 398aa7e..e506808 100644 --- a/src/graphics/grwxgl.cpp +++ b/src/graphics/grwxgl.cpp @@ -121,6 +121,7 @@ static void wxgl_init() opengl1_tcache_init(); gr_opengl_clear(); + gr_opengl_set_cull(1); GL_one_inited = 1; @@ -128,6 +129,13 @@ static void wxgl_init() void gr_wxgl_flip() { +#ifndef NDEBUG + GLenum error = glGetError(); + + if (error != GL_NO_ERROR) { + mprintf(("!!DEBUG!! OpenGL Error: %d\n", error)); + } +#endif } void gr_wxgl_set_viewport(int width, int height) @@ -155,7 +163,12 @@ void gr_wxgl_set_viewport(int width, int height) void gr_wxgl_cleanup() { - gr_opengl_cleanup(); + opengl1_tcache_cleanup(); + + opengl_free_render_buffer(); + + OGL_inited = false; + GL_one_inited = 0; } void gr_wxgl_init() @@ -174,7 +187,7 @@ void gr_wxgl_init() sscanf(gl_version, "%d.%d", &v_major, &v_minor); - GL_version = (v_major * 10) + v_minor; + int GL_version = (v_major * 10) + v_minor; // version check, require 1.2+ for sake of simplicity if (GL_version < 12) { @@ -202,42 +215,42 @@ void gr_wxgl_init() // screen values Gr_red.bits = 5; - Gr_red.shift = 10; + Gr_red.shift = 11; Gr_red.scale = 8; - Gr_red.mask = 0x7C00; + Gr_red.mask = 0x7C01; Gr_green.bits = 5; - Gr_green.shift = 5; + Gr_green.shift = 6; Gr_green.scale = 8; - Gr_green.mask = 0x3E0; + Gr_green.mask = 0x3E1; Gr_blue.bits = 5; - Gr_blue.shift = 0; + Gr_blue.shift = 1; Gr_blue.scale = 8; - Gr_blue.mask = 0x1F; + Gr_blue.mask = 0x20; Gr_alpha.bits = 1; - Gr_alpha.shift = 15; + Gr_alpha.shift = 0; Gr_alpha.scale = 255; - Gr_alpha.mask = 0x8000; + Gr_alpha.mask = 0x1; // DDOI - set these so no one else does! // texture values, always 1555 - 16-bit - Gr_t_red.mask = 0x7C00; - Gr_t_red.shift = 10; + Gr_t_red.mask = 0x7C01; + Gr_t_red.shift = 11; Gr_t_red.scale = 8; - Gr_t_green.mask = 0x3E0; - Gr_t_green.shift = 5; + Gr_t_green.mask = 0x3E1; + Gr_t_green.shift = 6; Gr_t_green.scale = 8; - Gr_t_blue.mask = 0x1F; - Gr_t_blue.shift = 0; + Gr_t_blue.mask = 0x20; + Gr_t_blue.shift = 1; Gr_t_blue.scale = 8; - Gr_t_alpha.mask = 0x8000; + Gr_t_alpha.mask = 0x1; + Gr_t_alpha.shift = 0; Gr_t_alpha.scale = 255; - Gr_t_alpha.shift = 15; // alpha-texture values Gr_ta_red.mask = 0x0f00; -- 2.39.2