Skip to content

Commit

Permalink
View and render improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zecruel authored May 9, 2024
1 parent 01e02bb commit 9122d46
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
25 changes: 19 additions & 6 deletions src/draw_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

/*
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/draw_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct ogl { /* openGL context to pass main parameters */
GLfloat model[3][3];

GLuint fbo;
GLuint fb_dims[2];

int timer;

Expand Down
42 changes: 26 additions & 16 deletions src/gui_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
{
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1355,7 +1363,7 @@ int gui_main_loop (gui_obj *gui) {
current = current->next;
}
}
gui->draw = 1;
gui->draw = 2;
}

/**********************************/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/* ------------------------------------------------------------------------------- */
Expand Down

0 comments on commit 9122d46

Please sign in to comment.