diff --git a/CMakeLists.txt b/CMakeLists.txt index b67f8d4..24ddff4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,8 @@ pkg_check_modules(GTK3 REQUIRED gtk+-3.0) pkg_check_modules(CAIRO REQUIRED cairo) find_package(OpenCL REQUIRED) -find_package(OpenGL REQUIRED) -find_package(GLUT REQUIRED) + + add_subdirectory(glade) @@ -34,4 +34,3 @@ message(STATUS "OpenCL found: ${OPENCL_FOUND}") message(STATUS "OpenCL includes: ${OPENCL_INCLUDE_DIRS}") message(STATUS "OpenCL CXX includes: ${OPENCL_HAS_CPP_BINDINGS}") message(STATUS "OpenCL libraries: ${OPENCL_LIBRARIES}") -message(STATUS "OpenGL libraries: ${OPENGL_gl_LIBRARIES}") diff --git a/src/main.c b/src/main.c index dabb0a2..88ea71d 100644 --- a/src/main.c +++ b/src/main.c @@ -30,7 +30,6 @@ static const char * const orange_framgment_shader = " \ FragColor = texture(ourTexture, texCoord);\n \ }"; - struct canvas_buffer { unsigned char *mandelbrot_buffer; unsigned int iterations; @@ -48,13 +47,13 @@ static gboolean on_main_window_close(GtkWidget *window, gpointer data) return FALSE; } -#define check_error(error) \ - do { \ - if (error != CL_SUCCESS) { \ - printf("OpenCL call failed with error %d. %s:%d\n", (int)error, __func__, __LINE__); \ - exit(error); \ - } } while(0) - +void check_error(cl_int error) +{ + if (error != CL_SUCCESS) { + printf("OpenCL call failed with error %d\n", (int)error); + exit(error); + } +} void load_kernel_from_file(char *file, char **src) { @@ -84,6 +83,51 @@ cl_program create_program(char *src, cl_context context) return program; } +unsigned int *data_array = NULL; +static gboolean drawing_callback(GtkWidget *widget, cairo_t *cr, gpointer data) +{ + struct canvas_buffer *buff = (struct canvas_buffer *)data; + int width; + int height; + GtkStyleContext *style_context; + int x, y; + double color_scale; + + style_context = gtk_widget_get_style_context(widget); + width = gtk_widget_get_allocated_width(widget); + height = gtk_widget_get_allocated_height(widget); + + cairo_save(cr); + /* Drawing code start */ + cairo_scale(cr, 1, -1); + cairo_translate(cr, (double)width/2.0, -(double)height/2.0); + + + for (x = 0; x < buff->width; x++) { + for (y = 0; y < buff->height; y++) { + cairo_rectangle(cr, + ((double)x - (double)(buff->width - 1) / 2), + ((double)y - (double)(buff->height - 1) / 2), + 1, + 1); + if (data_array[y * buff->width + x] == buff->iterations) { + cairo_set_source_rgb (cr, 0, 0, 0); + } else { + color_scale = (double)data_array[y * buff->width + x] / buff->iterations; + cairo_set_source_rgb (cr, (sqrt(1-color_scale)), (color_scale*color_scale), color_scale); + } + cairo_fill(cr); + + } + } + + + /* Drawing code end */ + cairo_restore(cr); + + return FALSE; +} + static int calculate_mandelbrot(struct canvas_buffer *buff) { @@ -189,8 +233,6 @@ static int calculate_mandelbrot(struct canvas_buffer *buff) clEnqueueReadBuffer(queue, dat_buff, CL_TRUE, 0, data_size*3, buff->mandelbrot_buffer, 0, NULL, NULL); - printf("Calculation finished!\n"); - ret = clFlush(queue); ret = clFinish(queue); ret = clReleaseKernel(kernel); @@ -355,14 +397,7 @@ int main(int argc, char **argv) mandelbrot_buff.height = 3000; mandelbrot_buff.iterations = 200; mandelbrot_buff.mandelbrot_buffer = NULL; - //calculate_mandelbrot(&mandelbrot_buff); - - gtk_gl_area_make_current(glarea); - - - - GError *error = NULL; - g_signal_connect(glarea, "render", G_CALLBACK(render), NULL); + calculate_mandelbrot(&mandelbrot_buff);