From 8e5bc917659288a257d5de9827458039f5498ef5 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Tue, 23 Jan 2018 01:08:38 +0100 Subject: OpenGL rendering added, cleaned up paths --- src/defs.h | 2 +- src/mandelbrot-zoom.c | 20 ++++++- src/mandelbrot-zoom.h | 4 ++ src/render.c | 146 +++++++++++++++++++++++++++++++++++--------------- src/render.h | 45 +++++++++++----- src/sets.c | 16 +++--- src/sets.h | 8 +-- 7 files changed, 171 insertions(+), 70 deletions(-) (limited to 'src') diff --git a/src/defs.h b/src/defs.h index 1ed2282..00b51bd 100644 --- a/src/defs.h +++ b/src/defs.h @@ -31,6 +31,6 @@ typedef signed long int s32; typedef unsigned long long u64; typedef signed long long s64; -typedef u8 bool; +//typedef u8 bool; #endif /* DEFS_H_ */ diff --git a/src/mandelbrot-zoom.c b/src/mandelbrot-zoom.c index bd97b66..e208940 100644 --- a/src/mandelbrot-zoom.c +++ b/src/mandelbrot-zoom.c @@ -11,14 +11,18 @@ int main(int argc, char **argv) { GtkBuilder *builder; gtk_init(&argc, &argv); + glutInit(&argc, argv); builder = gtk_builder_new(); gtk_builder_add_from_file (builder, "glade/settings.glade", NULL); // TODO: Glade-Zeug mit in die exe ui_settings.settings = GTK_WINDOW(gtk_builder_get_object(builder, "settings")); //gtk_builder_connect_signals(builder, NULL); ui_settings.iterationsSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "iterationsSp")); + ui_settings.threadsSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "threadsSp")); ui_settings.colorFromBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorFromBtn")); ui_settings.colorToBtn = GTK_BUTTON(gtk_builder_get_object(builder, "colorToBtn")); + ui_settings.zoomToXEntry = GTK_ENTRY(gtk_builder_get_object(builder, "zoomToXEntry")); + ui_settings.zoomToYEntry = GTK_ENTRY(gtk_builder_get_object(builder, "zoomToYEntry")); ui_settings.exportCb = GTK_CHECK_BUTTON(gtk_builder_get_object(builder, "exportCb")); ui_settings.gifRd = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "gifRd")); ui_settings.widthSp = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "widthSp")); @@ -32,6 +36,7 @@ int main(int argc, char **argv) ui_settings.colorDialog = GTK_COLOR_SELECTION_DIALOG(gtk_builder_get_object(builder, "colorDialog")); gtk_spin_button_set_increments(ui_settings.iterationsSp, 1, 2); + gtk_spin_button_set_increments(ui_settings.threadsSp, 1, 2); gtk_spin_button_set_increments(ui_settings.widthSp, 1, 2); gtk_spin_button_set_increments(ui_settings.heightSp, 1, 2); gtk_spin_button_set_increments(ui_settings.fpsRenderSp, 1, 2); @@ -39,6 +44,7 @@ int main(int argc, char **argv) gtk_spin_button_set_increments(ui_settings.bitrateSp, 1, 2); gtk_spin_button_set_value(ui_settings.iterationsSp, 1000); + gtk_spin_button_set_value(ui_settings.threadsSp, 4); gtk_spin_button_set_value(ui_settings.widthSp, 1920); gtk_spin_button_set_value(ui_settings.heightSp, 1080); gtk_spin_button_set_value(ui_settings.fpsRenderSp, 30); @@ -46,6 +52,7 @@ int main(int argc, char **argv) gtk_spin_button_set_value(ui_settings.bitrateSp, 1000); gtk_spin_button_set_range(ui_settings.iterationsSp, 1, 16777215); // 3 bytes á 8 bit = 24 bit + gtk_spin_button_set_range(ui_settings.threadsSp, 1, 255); gtk_spin_button_set_range(ui_settings.widthSp, 1, 1920); gtk_spin_button_set_range(ui_settings.heightSp, 1, 1080); gtk_spin_button_set_range(ui_settings.fpsRenderSp, 1, 60); @@ -53,6 +60,7 @@ int main(int argc, char **argv) gtk_spin_button_set_range(ui_settings.bitrateSp, 1, 65535); g_signal_connect(ui_settings.iterationsSp, "value-changed", G_CALLBACK(on_iterationsSp_valueChanged), NULL); + g_signal_connect(ui_settings.threadsSp, "value-changed", G_CALLBACK(on_threadsSp_valueChanged), NULL); g_signal_connect(ui_settings.colorFromBtn, "clicked", G_CALLBACK(on_colorFromBtn_clicked), NULL); g_signal_connect(ui_settings.colorToBtn, "clicked", G_CALLBACK(on_colorToBtn_clicked), NULL); g_signal_connect(ui_settings.exportCb, "toggled", G_CALLBACK(on_exportCb_toggled), NULL); @@ -76,6 +84,11 @@ void on_iterationsSp_valueChanged() config.iterations = gtk_spin_button_get_value(ui_settings.iterationsSp); } +void on_threadsSp_valueChanged() +{ + config.threads = gtk_spin_button_get_value(ui_settings.threadsSp); +} + void on_colorFromBtn_clicked() { currentColor = COLOR_FROM; @@ -125,7 +138,12 @@ void on_bitrateSp_valueChanged() void on_startBtn_clicked() { - printf("config {\n\t.iterations = %u\n\t.video = %u\n\t.filetype = %u\n\t.width = %u\n\t.height = %u\n\t.renderFPS = %u\n\t.videoFPS = %u\n\t.bitrate = %u\n\t.path = %s\n}\n", config.iterations, config.video, 0, config.width, config.height, config.renderFPS, config.videoFPS, config.bitrate, config.path); + double x, y; + sscanf(gtk_entry_get_text(ui_settings.zoomToXEntry), "%lf", &x); + sscanf(gtk_entry_get_text(ui_settings.zoomToYEntry), "%lf", &y); + config.to_x = x; + config.to_y = y; + printf("config {\n\t.iterations = %u\n\t.tox = %f\n\t.toy = %f\n\t.video = %u\n\t.filetype = %u\n\t.width = %u\n\t.height = %u\n\t.renderFPS = %u\n\t.videoFPS = %u\n\t.bitrate = %u\n\t.path = %s\n}\n", config.iterations, x, y, config.video, 0, config.width, config.height, config.renderFPS, config.videoFPS, config.bitrate, config.path); render_init(&config, mandelbrot_r); render_show(); } diff --git a/src/mandelbrot-zoom.h b/src/mandelbrot-zoom.h index 80d3259..3953b2d 100644 --- a/src/mandelbrot-zoom.h +++ b/src/mandelbrot-zoom.h @@ -17,8 +17,11 @@ typedef struct Ui_settings { GtkWindow *settings; GtkSpinButton *iterationsSp; + GtkSpinButton *threadsSp; GtkButton *colorFromBtn; GtkButton *colorToBtn; + GtkEntry *zoomToXEntry; + GtkEntry *zoomToYEntry; GtkCheckButton *exportCb; GtkRadioButton *gifRd; GtkSpinButton *widthSp; @@ -38,6 +41,7 @@ u8 currentColor; int main(int argc, char **argv); void on_iterationsSp_valueChanged(); +void on_threadsSp_valueChanged(); void on_colorFromBtn_clicked(); void on_colorToBtn_clicked(); void on_exportCb_toggled(); diff --git a/src/render.c b/src/render.c index e06c967..9e7f1a4 100644 --- a/src/render.c +++ b/src/render.c @@ -6,67 +6,125 @@ */ #include "render.h" +#define HAVE_STRUCT_TIMESPEC -void render_init(Config *config, u32 (*sfunc) (double, double, u32)) +void render_init(Config *config, u32 (*sfunc) (long double, long double, u32)) { + delta = glutGet(GLUT_ELAPSED_TIME); + 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; _sfunc = sfunc; + s_arr = (u32 *) malloc((_config->width) * (_config->height) * sizeof(u32)); + calculate(x_min, y_min, x_max, y_max, _sfunc, s_arr); + //glutInit(0, NULL); + glutInitWindowPosition(0, 0); + glutInitWindowSize(_config->width, _config->height); + glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); + glutCreateWindow("Renderer"); + glutDisplayFunc(gl_render); + glutIdleFunc(gl_idle); - ui_render.window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL)); - gtk_window_set_default_size(ui_render.window, config->width, config->height); - - ui_render.frame = GTK_FRAME(gtk_frame_new(NULL)); - gtk_container_add(GTK_CONTAINER(ui_render.window), ui_render.frame); - - ui_render.drawing_area = GTK_DRAWING_AREA(gtk_drawing_area_new()); - gtk_widget_set_size_request(GTK_WIDGET(ui_render.drawing_area), config->width, config->height); - g_signal_connect(G_OBJECT(ui_render.drawing_area), "draw", G_CALLBACK(on_draw), NULL); - gtk_container_add(GTK_CONTAINER(ui_render.frame), ui_render.drawing_area); + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, 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, s_arr); + glBindTexture(GL_TEXTURE_2D, 0); + //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _config->width, _config->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, s_arr); + glMatrixMode(GL_PROJECTION); + glOrtho(0, _config->width, 0, _config->height, -1, 1); + glMatrixMode(GL_MODELVIEW); + glutMainLoop(); } void render_show() { - gtk_widget_show_all(GTK_WIDGET(ui_render.window)); + } -void on_draw(GtkWidget *widget, cairo_t *cr, gpointer data) +void gl_render(void) { - cairo_surface_t *image; - // TODO: MUSS UNBEDINGT SEPARAT GECALLT WERDEN, SONST KEINE 60 FPS - image = render_surface(); - cairo_set_source_surface(cr, image, 0, 0); - cairo_paint(cr); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBindTexture(GL_TEXTURE_2D, 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); + +// glBegin(GL_TRIANGLES); +// glVertex3f(-0.5,-0.5,0.0); +// glVertex3f(0.5,0.0,0.0); +// glVertex3f(0.0,0.5,0.0); +// glEnd(); + + glutSwapBuffers(); } -cairo_surface_t *render_surface() +void calculate(long double x_min, long double y_min, long double x_max, long double y_max, u32 (*sfunc) (long double, long double, u32), u32 *arr) { - cairo_surface_t *result; - unsigned char *current_row; - int stride; - double x_math, y_math; - u32 iterations; + pthread_t thread; + ThreadArgs *args = (ThreadArgs *) malloc(_config->threads * sizeof(ThreadArgs)); + for(u8 i = 0; i < _config->threads; i++) + { + args[i] = (ThreadArgs) { .tc = _config->threads, .tid = i, .x_min = x_min, .y_min = y_min, .x_max = x_max, .y_max = y_max, .sfunc = sfunc, .arr = arr }; + pthread_create(&thread, NULL, calculate_t, (void *)&args[i]); + } + pthread_join(thread, NULL); +} - result = cairo_image_surface_create(CAIRO_FORMAT_RGB24, _config->width, _config->height); - if (cairo_surface_status(result) != CAIRO_STATUS_SUCCESS) - return result; +void calculate_t(void *args) +{ + ThreadArgs *_args = (ThreadArgs *)args; + long double x_math, y_math; + u32 iterations; + for (u32 y = (_config->height/_args->tc)*(_args->tid); y < _config->height; y++) + { + for (u32 x = 0; x < _config->width; x++) + { + x_math = _args->x_min + ((long double) x * (_args->x_max - _args->x_min)) / _config->width; + y_math = _args->y_min + ((long double) (_config->height - y) * (_args->y_max - _args->y_min)) / _config->height; + iterations = _args->sfunc(x_math, y_math, _config->iterations); + _args->arr[COORDS(x, y, _config->width)] = (((1<<24)-1)*iterations)/_config->iterations; + } + } +} - cairo_surface_flush(result); - current_row = cairo_image_surface_get_data(result); - stride = cairo_image_surface_get_stride(result); - for (int y = 0; y < _config->height; y++) { - u32 *row = (void *) current_row; - for (int x = 0; x < _config->width; x++) { - x_math = /*x_MIN*/-2.0 + ((double) x * (/*x_MAX*/1.0 - /*x_MIN*/-2.0)) / _config->width; - y_math = /*y_MIN*/-1.0 + ((double) (_config->height - y) * (/*y_MAX*/1.0 - /*y_MIN*/-1.0)) / _config->height; - iterations = _sfunc(x_math, y_math, _config->iterations); - row[x] = (((1<<24)-1)*iterations)/_config->iterations; -// if (iterations > 1<<8) -// printf("schon gruen\n"); - } +long double zoom_func(long double ft, long double s) +{ + return (s - expl(-ft)); +} - current_row += stride; - } - cairo_surface_mark_dirty(result); - return result; +void gl_idle(void) +{ + calculate(x_min, y_min, x_max, y_max, _sfunc, s_arr); + //glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, 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, s_arr); + glBindTexture(GL_TEXTURE_2D, 0); + int t = glutGet(GLUT_ELAPSED_TIME); + dt = (t - delta) / 1000.0; + delta = t; + ft+=(0.1*dt); + x_min = x_min_s + zoom_func(ft, (long double)2.0 + _config->to_x); + y_min = y_min_s + zoom_func(ft, (long double)1.0 + _config->to_y); + x_max = x_max_s - zoom_func(ft, (long double)1.0 - _config->to_x); + y_max = y_max_s - zoom_func(ft, (long double)1.0 - _config->to_y); + glutPostRedisplay(); } + + diff --git a/src/render.h b/src/render.h index d9850fd..83a5c1a 100644 --- a/src/render.h +++ b/src/render.h @@ -8,14 +8,22 @@ #ifndef RENDER_H_ #define RENDER_H_ -#include +#define COORDS(x, y, width) ((y)*(width)+(x)) + #include "defs.h" +#include +#include +#include +#include typedef struct config { u32 iterations; + u8 threads; u32 colorFrom; u32 colorTo; - bool video; + long double to_x; + long double to_y; + u8 video; u8 filetype; u16 width; u16 height; @@ -26,20 +34,33 @@ typedef struct config { // TODO: key mapping als option in die struct } Config; -typedef struct Ui_render { - GtkWindow *window; - GtkFrame *frame; - GtkDrawingArea *drawing_area; -} Ui_render; +typedef struct t_args { + u8 tc; + u8 tid; + long double x_min; + long double x_max; + long double y_min; + long double y_max; + u32 (*sfunc) (long double, long double, u32); + u32 *arr; +} ThreadArgs; Config *_config; -Ui_render ui_render; -u32 (*_sfunc) (double, double, u32); +u32 (*_sfunc) (long double, long double, u32); +u32 *s_arr; +GLuint tex; +u32 rendercnt; +long double x_min, x_max, y_min, y_max; +long double x_min_s, x_max_s, y_min_s, y_max_s; +int delta; +long double dt, ft; -void render_init(Config *config, u32 (*sfunc) (double, double, u32)); +void render_init(Config *config, u32 (*sfunc) (long double, long double, u32)); void render_show(); -cairo_surface_t *render_surface(); +void gl_render(void); +void gl_idle(void); -void on_draw(GtkWidget *widget, cairo_t *cr, gpointer data); +void calculate(long double x_min, long double y_min, long double x_max, long double y_max, u32 (*sfunc) (long double, long double, u32), u32 *arr); +void calculate_t(void *args); #endif /* RENDER_H_ */ diff --git a/src/sets.c b/src/sets.c index 6787a6e..2c01987 100644 --- a/src/sets.c +++ b/src/sets.c @@ -7,9 +7,9 @@ #include "sets.h" -u32 mandelbrot_s(double x, double y, u32 iterations) +u32 mandelbrot_s(long double x, long double y, u32 iterations) { - double cx = x, cy = y, x2; + long double cx = x, cy = y, x2; u32 m = 0; while(m <= iterations && (x*x)+(y*y) <= 4) { @@ -21,16 +21,16 @@ u32 mandelbrot_s(double x, double y, u32 iterations) return m; } -u32 mandelbrot_r(double x, double y, u32 iterations) +u32 mandelbrot_r(long double x, long double y, u32 iterations) { return _mandelbrot_r(x, y, 0.0, 0.0, 0, iterations, 4.0); } -u32 _mandelbrot_r(double x, double y, double zx, double zy, u32 n, u32 iterations, double threshold) +u32 _mandelbrot_r(long double x, long double y, long double zx, long double zy, u32 n, u32 iterations, long double threshold) { - if ((n <= iterations) && ((zx * zx + zy * zy) < threshold)) { - double zx_new = (zx * zx - zy * zy + x); - double zy_new = (2 * zx * zy + y); + if ((n < iterations) && ((zx * zx + zy * zy) < threshold)) { + long double zx_new = (zx * zx - zy * zy + x); + long double zy_new = (2 * zx * zy + y); if ((zx_new == zx) && (zy_new == zy)) { return iterations; } @@ -39,7 +39,7 @@ u32 _mandelbrot_r(double x, double y, double zx, double zy, u32 n, u32 iteration return n; } -u32 julia(double x, double y, u32 iterations) +u32 julia(long double x, long double y, u32 iterations) { // TODO: Julia-Menge return 0; diff --git a/src/sets.h b/src/sets.h index 76d3ace..1ae237c 100644 --- a/src/sets.h +++ b/src/sets.h @@ -10,9 +10,9 @@ #include "defs.h" -u32 mandelbrot_s(double x, double y, u32 iterations); -u32 mandelbrot_r(double x, double y, u32 iterations); -u32 _mandelbrot_r(double x, double y, double zx, double zy, u32 n, u32 iterations, double threshold); -u32 julia(double x, double y, u32 iterations); +u32 mandelbrot_s(long double x, long double y, u32 iterations); +u32 mandelbrot_r(long double x, long double y, u32 iterations); +u32 _mandelbrot_r(long double x, long double y, long double zx, long double zy, u32 n, u32 iterations, long double threshold); +u32 julia(long double x, long double y, u32 iterations); #endif /* SETS_H_ */ -- cgit v1.2.1