From 18e5b2554b4cd73207e936e76e32c9d376cd1ba8 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Thu, 1 Feb 2018 01:27:30 +0100 Subject: abstracted rendering, cpu currently crashing randomly while zooming --- src/render.c | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 162 insertions(+), 22 deletions(-) (limited to 'src/render.c') diff --git a/src/render.c b/src/render.c index e10e44b..30f1b7a 100644 --- a/src/render.c +++ b/src/render.c @@ -11,6 +11,15 @@ void init_render(config_t *config) { + x_min_s = -2.0; + x_max_s = 1.0; + y_min_s = -1.0; + y_max_s = 1.0; + x_min = x_min_s; + x_max = x_max_s; + y_min = y_min_s; + y_max = y_max_s; + _config = config; s_arr = (u32 *) malloc((_config->width) * (_config->height) * sizeof(u32)); _config->arr = s_arr; @@ -40,34 +49,31 @@ void init_render(config_t *config) { case MODE_CPU: init_cpu(_config); - glutDisplayFunc(render_cpu); - if (_config->zoomSpecific) - { - glutIdleFunc(idle_cpu); - } - else - { - glutIdleFunc(idle_cpu_dummy); - glutKeyboardFunc(keyboard_cpu); - glutMouseFunc(mouse_cpu); - } + _render = render_cpu; + _idle = idle_cpu; break; case MODE_OPENCL: init_opencl(_config); - glutDisplayFunc(render_opencl); - if (_config->zoomSpecific) - { - glutIdleFunc(idle_opencl); - } - else - { - glutIdleFunc(idle_opencl_dummy); - glutKeyboardFunc(keyboard_opencl); - glutMouseFunc(mouse_opencl); - } + _render = render_opencl; + _idle = idle_opencl; break; } + if (_config->zoomSpecific) + { + glutIdleFunc(idle); + } + else + { + glutIdleFunc(idle_dummy); + glutKeyboardFunc(keyboard); + glutMouseFunc(mouse); + } + + glutDisplayFunc(render); + + t_old = glutGet(GLUT_ELAPSED_TIME); + glutMainLoop(); } @@ -81,3 +87,137 @@ d64 zoom_func(d64 ft, d64 s) { return (s - expl(-ft)); } + +void idle_dummy(void) +{ + double xmin = (double) x_min; + double ymin = (double) y_min; + double xmax = (double) x_max; + double ymax = (double) y_max; + printf("x_min: %lf, y_min: %lf, x_max: %lf, y_max: %lf\n", xmin, ymin, xmax, ymax); + glutPostRedisplay(); +} + +void idle(void) +{ + int t = 0, delta = 0; + do + { + t = glutGet(GLUT_ELAPSED_TIME); + delta = t - t_old; + } while (delta < 16); // TODO: Hardcoded FPS + t_old = t; + + _idle(); + + //glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, _config->tex); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _config->width, + _config->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, + _config->arr); + glBindTexture(GL_TEXTURE_2D, 0); + ft += (_config->speed * (delta / 1000.0)); + x_min = x_min_s + + _config->zoom_func(ft, + (double) 2.0 + _config->to_x); + y_min = y_min_s + + _config->zoom_func(ft, + (double) 1.0 + _config->to_y); + x_max = x_max_s + - _config->zoom_func(ft, + (double) 1.0 - _config->to_x); + y_max = y_max_s + - _config->zoom_func(ft, + (double) 1.0 - _config->to_y); + glutPostRedisplay(); +} + +void render(void) +{ + _render(x_min, y_min, x_max, y_max); + + glBindTexture(GL_TEXTURE_2D, _config->tex); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _config->width, + _config->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, + _config->arr); + glBindTexture(GL_TEXTURE_2D, 0); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBindTexture(GL_TEXTURE_2D, _config->tex); + glEnable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + glTexCoord2i(0, 0); + glVertex2i(0, 0); + glTexCoord2i(0, 1); + glVertex2i(0, _config->height); + glTexCoord2i(1, 1); + glVertex2i(_config->width, _config->height); + glTexCoord2i(1, 0); + glVertex2i(_config->width, 0); + glEnd(); + + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + glutSwapBuffers(); +} + +void keyboard(unsigned char key, int mouseX, int mouseY) +{ + d64 w = (x_max - x_min), h = (y_max - y_min); + switch (key) + { + case 'w': + y_max += (h / 10.0); + y_min += (h / 10.0); + break; + case 'a': + x_max -= (w / 10.0); + x_min -= (w / 10.0); + break; + case 's': + y_max -= (h / 10.0); + y_min -= (h / 10.0); + break; + case 'd': + x_max += (w / 10.0); + x_min += (w / 10.0); + break; + case 'i': + _config->iterations++; + break; + case 'k': + _config->iterations--; + break; + } +} + +void mouse(int button, int state, int x, int y) +{ + if (state == GLUT_DOWN) + { + switch (button) + { + case GLUT_LEFT_BUTTON: + if (_config->speed < 0) + _config->speed = (-1) * _config->speed; + break; + case GLUT_RIGHT_BUTTON: + if (_config->speed > 0) + _config->speed = (-1) * _config->speed; + break; + } + _config->to_x = x_min + + ((d64) x * (x_max - x_min)) / _config->width; + _config->to_y = y_min + + ((d64) y * (y_max - y_min)) / _config->height; + t_old = glutGet(GLUT_ELAPSED_TIME); + glutIdleFunc(idle); + } + else if (state == GLUT_UP) + { + glutIdleFunc(idle_dummy); + } +} -- cgit v1.2.1