diff --git a/src/draw_gl.c b/src/draw_gl.c index 20d9a34a..cfb32086 100644 --- a/src/draw_gl.c +++ b/src/draw_gl.c @@ -2,6 +2,13 @@ #include "gui.h" #define TOLERANCE 1e-6 +#ifdef __EMSCRIPTEN__ + #define MAX_RESOL_X 1024 + #define MAX_RESOL_Y 768 +#else + #define MAX_RESOL_X 3840 + #define MAX_RESOL_Y 2160 +#endif /* mantain one unique element in a sorted array - array of integer values */ static int unique (int n, int * a) { @@ -203,6 +210,12 @@ int draw_gl_init (void *data, int clear){ /* init (or de-init) OpenGL */ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gui->gl_ctx.tex_w, gui->gl_ctx.tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); /* -------- test frame buffer --- */ + glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &gui->gl_ctx.fb_dims[0]); + printf("\nout size = %d, %d\n", gui->gl_ctx.fb_dims[0], gui->gl_ctx.fb_dims[1]); + + gui->gl_ctx.fb_dims[0] = (gui->gl_ctx.fb_dims[0] < MAX_RESOL_X) ? gui->gl_ctx.fb_dims[0] : MAX_RESOL_X; + gui->gl_ctx.fb_dims[1] = (gui->gl_ctx.fb_dims[1] < MAX_RESOL_Y) ? gui->gl_ctx.fb_dims[1] : MAX_RESOL_Y; + GLuint fbo = 0; glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); @@ -217,7 +230,7 @@ int draw_gl_init (void *data, int clear){ /* init (or de-init) OpenGL */ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3840, 2160, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gui->gl_ctx.fb_dims[0], gui->gl_ctx.fb_dims[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // attach it to currently bound framebuffer object glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[2], 0); @@ -231,7 +244,7 @@ int draw_gl_init (void *data, int clear){ /* init (or de-init) OpenGL */ GLuint depthrenderbuffer; glGenRenderbuffers(1, &depthrenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 3840, 2160); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, gui->gl_ctx.fb_dims[0], gui->gl_ctx.fb_dims[1]); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer); /* @@ -1903,7 +1916,7 @@ int dxf_draw_framebuffer(struct ogl *gl_ctx){ /* 1 */ j = gl_ctx->vert_count; gl_ctx->verts[j].pos[0] = 0.0; - gl_ctx->verts[j].pos[1] = 2160.0;//(float) (y + h); + gl_ctx->verts[j].pos[1] = (float) gl_ctx->fb_dims[1];//(float) (y + h); gl_ctx->verts[j].pos[2] = 0.0; gl_ctx->verts[j].norm[0] = 0.0; gl_ctx->verts[j].norm[1] = 0.0; @@ -1917,7 +1930,7 @@ int dxf_draw_framebuffer(struct ogl *gl_ctx){ gl_ctx->vert_count ++; /* 2 */ j = gl_ctx->vert_count; - gl_ctx->verts[j].pos[0] = 3840.0;//(float) (x + w); + gl_ctx->verts[j].pos[0] = (float) gl_ctx->fb_dims[0];//(float) (x + w); gl_ctx->verts[j].pos[1] = 0.0; gl_ctx->verts[j].pos[2] = 0.0; gl_ctx->verts[j].norm[0] = 0.0; @@ -1932,8 +1945,8 @@ int dxf_draw_framebuffer(struct ogl *gl_ctx){ gl_ctx->vert_count ++; /* 3 */ j = gl_ctx->vert_count; - gl_ctx->verts[j].pos[0] = 3840.0;//(float) (x + w); - gl_ctx->verts[j].pos[1] = 2160.0;//(float) (y + h); + gl_ctx->verts[j].pos[0] = (float) gl_ctx->fb_dims[0];//(float) (x + w); + gl_ctx->verts[j].pos[1] = (float) gl_ctx->fb_dims[1];//(float) (y + h); gl_ctx->verts[j].pos[2] = 0.0; gl_ctx->verts[j].norm[0] = 0.0; gl_ctx->verts[j].norm[1] = 0.0; diff --git a/src/draw_gl.h b/src/draw_gl.h index 59be191f..2e8119a5 100644 --- a/src/draw_gl.h +++ b/src/draw_gl.h @@ -66,6 +66,7 @@ struct ogl { /* openGL context to pass main parameters */ GLfloat model[3][3]; GLuint fbo; + GLuint fb_dims[2]; int timer; diff --git a/src/gui_loop.c b/src/gui_loop.c index 5dde2cbe..bd972eb9 100644 --- a/src/gui_loop.c +++ b/src/gui_loop.c @@ -51,6 +51,7 @@ int gui_main_loop (gui_obj *gui) { static int update_title = 0, changed = 0, framebuf = 1; static double prev_ofs_x, prev_ofs_y, prev_zoom; + static float prev_model_view[3][3]; static struct do_entry *prev_do = NULL; //gui->list_do.current; @@ -436,12 +437,19 @@ int gui_main_loop (gui_obj *gui) { } break; case SDL_WINDOWEVENT: - if (event.window.event == SDL_WINDOWEVENT_RESIZED){ - gui->draw = 1; - } - if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED){ - gui->draw = 1; - } + //if (event.window.event == SDL_WINDOWEVENT_RESIZED || + // event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) + //{ + /*get current window size and position*/ + SDL_GetWindowSize(gui->window, &gui->win_w, &gui->win_h); + SDL_GetWindowPosition (gui->window, &gui->win_x, &gui->win_y); + gui->win_w = (gui->win_w < gui->gl_ctx.fb_dims[0]) ? gui->win_w : gui->gl_ctx.fb_dims[0]; + gui->win_h = (gui->win_h < gui->gl_ctx.fb_dims[1]) ? gui->win_h : gui->gl_ctx.fb_dims[1]; + SDL_SetWindowSize(gui->window, gui->win_w, gui->win_h); + + + gui->draw = 1; + //} break; case SDL_DISPLAYEVENT: { @@ -1087,7 +1095,7 @@ int gui_main_loop (gui_obj *gui) { dxf_ents_parse(gui->drawing); - gui->draw = 1; + gui->draw = 2; } else if((gui->action == YANK || gui->action == CUT) && strlen(gui->clip_path) > 0) { if (gui->sel_list->next){ /* verify if has elements in list */ @@ -1266,7 +1274,7 @@ int gui_main_loop (gui_obj *gui) { current = current->next; } } - gui->draw = 1; + gui->draw = 2; } else if(gui->action == COLOR_CHANGE){ @@ -1296,7 +1304,7 @@ int gui_main_loop (gui_obj *gui) { current = current->next; } } - gui->draw = 1; + gui->draw = 2; } else if(gui->action == LTYPE_CHANGE){ gui->action = NONE; @@ -1326,7 +1334,7 @@ int gui_main_loop (gui_obj *gui) { current = current->next; } } - gui->draw = 1; + gui->draw = 2; } else if(gui->action == LW_CHANGE){ gui->action = NONE; @@ -1355,7 +1363,7 @@ int gui_main_loop (gui_obj *gui) { current = current->next; } } - gui->draw = 1; + gui->draw = 2; } /**********************************/ @@ -1479,14 +1487,14 @@ int gui_main_loop (gui_obj *gui) { file_path_len = strlen(file_path); } - if (gui_check_draw(gui) != 0){ + if (gui_check_draw(gui) != 0 && gui->draw == 0){ gui->draw = 1; } + if(memcmp(prev_model_view, gui->model_view, sizeof(prev_model_view))){ + gui->draw = 2; + } if (gui->draw != 0 || curr_ent){ - /*get current window size and position*/ - SDL_GetWindowSize(gui->window, &gui->win_w, &gui->win_h); - SDL_GetWindowPosition (gui->window, &gui->win_x, &gui->win_y); //glUniform1i(gui->gl_ctx.tex_uni, 0); @@ -1550,7 +1558,8 @@ int gui_main_loop (gui_obj *gui) { /* --------------------*/ if (prev_ofs_x != gui->ofs_x || prev_ofs_y != gui->ofs_y || - prev_zoom != gui->zoom || prev_do != gui->list_do.current) + prev_zoom != gui->zoom || prev_do != gui->list_do.current || + gui->draw == 2) { glBindFramebuffer(GL_FRAMEBUFFER, gui->gl_ctx.fbo); glViewport(0, 0, gui->gl_ctx.win_w, gui->gl_ctx.win_h); @@ -1806,6 +1815,7 @@ int gui_main_loop (gui_obj *gui) { prev_ofs_y = gui->ofs_y; prev_zoom = gui->zoom; prev_do = gui->list_do.current; + memcpy(prev_model_view, gui->model_view, sizeof(prev_model_view)); return gui->running; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 351524fb..e1b1fe30 100644 --- a/src/main.c +++ b/src/main.c @@ -351,7 +351,7 @@ int main(int argc, char** argv){ /* ------------------------------ opengl --------------------------------------*/ gui->gl_ctx.ctx = SDL_GL_CreateContext(gui->window); #endif - + draw_gl_init ((void *)gui, 0); /* ------------------------------------------------------------------------------- */