// BEGIN KAWIGIEDIT TESTING // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof bool KawigiEdit_RunTest(int testNum, int p0, double p1, double p2, bool hasAnswer, int p3) { cout << "Test " << testNum << ": [" << p0 << "," << p1 << "," << p2; cout << "]" << endl; Mandelbrot *obj; int answer; obj = new Mandelbrot(); clock_t startTime = clock(); answer = obj->iterations(p0, p1, p2); clock_t endTime = clock(); delete obj; bool res; res = true; cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl; if (hasAnswer) { cout << "Desired answer:" << endl; cout << "\t" << p3 << endl; } cout << "Your answer:" << endl; cout << "\t" << answer << endl; if (hasAnswer) { res = answer == p3; } if (!res) { cout << "DOESN'T MATCH!!!!" << endl; } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) { cout << "FAIL the timeout" << endl; res = false; } else if (hasAnswer) { cout << "Match :-)" << endl; } else { cout << "OK, but is it right?" << endl; } cout << "" << endl; return res; }
// free any allocated resources void cleanup(void) { clMandelbrot.cleanup(); clMandelbrot.printStats(); }
/* free any allocated resources */ void cleanup(void) { if(pixels) free(pixels); clMandelbrot.cleanup(); clMandelbrot.printStats(); }
int main(int argc, char * argv[]) { /* initalise and run the mandelbrot kernel */ clMandelbrot.initialize(); if(!clMandelbrot.parseCommandLine(argc, argv)) return SDK_FAILURE; if(clMandelbrot.setup()!=SDK_SUCCESS) return SDK_FAILURE; if(clMandelbrot.run()!=SDK_SUCCESS) return SDK_FAILURE; if(clMandelbrot.verifyResults()!=SDK_SUCCESS) return SDK_FAILURE; /* show window if it is not running in quiet mode */ if(clMandelbrot.showWindow()) { width = clMandelbrot.getWidth(); height = clMandelbrot.getHeight(); int * output = clMandelbrot.getPixels(); pixels = (unsigned char *)malloc(height*width*4*sizeof(unsigned char)); for(int i=0; i< width*height; ++i) { pixels[4*i] = (unsigned char)output[i]*(i/width)/height; pixels[4*i + 1] = (unsigned char)output[i]*(i%width)/width ; pixels[4*i + 2] = (unsigned char)output[i]*i/(width*height); pixels[4*i + 3] = 255; } initDisplay(argc, argv); mainLoopGL(); } cleanup(); return SDK_SUCCESS; }
private: void setColourForIteration(QColor &farbe, int iter) { if(model->getIterationen() == iter) { farbe.setRed(0); farbe.setGreen(0); farbe.setBlue(0); return; } double anteil = (double)iter / (double)model->getIterationen(); int r = getFarbAnteilMitDif(anteil, farbenMaske[0]); int g = getFarbAnteilMitDif(anteil, farbenMaske[1]); int b = getFarbAnteilMitDif(anteil, farbenMaske[2]); farbe.setRed(r); farbe.setGreen(g); farbe.setBlue(b); }
// keyboard function void keyboardFunc(unsigned char key, int mouseX, int mouseY) { switch(key) { // If the user hits escape or Q, then exit case GLUT_ESCAPE_KEY: case 'q': case 'Q': { cleanup(); exit(0); break; } case 'c': { printf("center (%.13f, %.13f), window width %.13f\n", clMandelbrot.getXPos(), clMandelbrot.getYPos(), clMandelbrot.getXStep()); break; } case 'i': { cl_uint maxIterations = clMandelbrot.getMaxIterations(); maxIterations = ((maxIterations * 2) < MAX_ITER) ? maxIterations * 2 : MAX_ITER; printf("Setting maxIterations to %d\n", maxIterations); clMandelbrot.setMaxIterations(maxIterations); break; } case 'I': { cl_uint maxIterations = clMandelbrot.getMaxIterations(); maxIterations = ((maxIterations / 2) > MIN_ITER) ? maxIterations / 2 : MIN_ITER; printf("Setting maxIterations to %d\n", maxIterations); clMandelbrot.setMaxIterations(maxIterations); break; } case 'b': { if (clMandelbrot.getTiming()) { clMandelbrot.setBench(1); } } case 'p': { panning = (panning == false)? true : false; break; } default: break; } }
// display function void displayFunc() { if (!clMandelbrot.getBenched()) { glClear(GL_COLOR_BUFFER_BIT); glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, output); glFlush(); glutSwapBuffers(); } }
public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) { (void)(item); (void)(widget); QPoint point(0, 0); QColor farbe(0, 0, 0); int iter = 0; for(int i = 0; i < model->getBreite(); i++) { for(int j = 0; j < model->getHoehe(); j++) { iter = model->getIterationZuPunkt(i, j); point.setX(i); point.setY(j); setColourForIteration(farbe, iter); painter->setPen(farbe); painter->drawPoint(point); } } }
MandelbrotEnv<T>::MandelbrotEnv(const Mandelbrot<T>& spec) : spec_(spec), xs_(new T[spec.maxIterations()]), ys_(new T[spec.maxIterations()]) {}
int main() { if(OpenCLRuntime::Initialize() != SICKL_SUCCESS) { printf("Could not OpenCL Context\n"); return -1; } Mandelbrot mbrot; mbrot.Parse(); mbrot.GetRoot().Print(); OpenCLProgram program; OpenCLCompiler::Build(mbrot, program); #if 0 // init GLEW/GLUT and other gl setup if(!OpenGLRuntime::Initialize()) { printf("Could not create OpenGL Context\n"); return -1; } OpenGLCompiler comp; Mandelbrot mbrot; mbrot.Parse(); /// Prints the AST generated from the Mandelbrot source mbrot.GetRoot().Print(); /// Compile our OpenGL program OpenGLProgram* program = comp.Build(mbrot); /// Print the generated GLSL source printf("%s\n", program->GetSource().c_str()); const uint32_t width = 350 * 5; const uint32_t height = 200 * 5; const uint32_t colors = mbrot.max_iterations; /// Generate the color table (a nice gold) float* color_map_data = new float[3 * colors]; for(uint32_t i = 0; i < colors; i++) { float x = i/(float)colors; color_map_data[3 * i + 0] = 191.0f / 255.0f * (1.0f - x); color_map_data[3 * i + 1] = 125.0f / 255.0f * (1.0f - x); color_map_data[3 * i + 2] = 37.0f / 255.0f * (1.0f - x); } /// put it int a 1d buffer OpenGLBuffer1D color_map(colors, ReturnType::Float3, color_map_data); /// our output buffer OpenGLBuffer2D result(width, height, ReturnType::Float3, nullptr); OpenGLBuffer2D copy(width, height, ReturnType::Float3, nullptr); /// initialize our program program->Initialize(width, height); /// get our binding locations for each of the program input and outputs input_t min_loc = program->GetInputHandle("min"); input_t max_loc = program->GetInputHandle("max"); input_t color_map_loc = program->GetInputHandle("color_map"); output_t output_loc = program->GetOutputHandle("output"); /// sets min values program->SetInput(min_loc, -2.5f, -1.0f); /// sets max values program->SetInput(max_loc, 1.0f, 1.0f); /// set the scaler program->SetInput(color_map_loc, color_map); /// sets the render location program->BindOutput(output_loc, result); /// Runs the program program->Run(); /// We can copy our data to the second buffer copy.SetData(result); float* result_buffer = nullptr; /// We can either read result back from the texture copy.GetData(result_buffer); /// Or from the framebuffer (which is faster on nvidia hardware at least) program->GetOutput(output_loc, result_buffer); /// Finally, dump the image to a Bitmap to view BMP image; image.SetSize(width, height); for(uint32_t i = 0; i < height; i++) { for(uint32_t j = 0; j < width; j++) { float red = result_buffer[i * width * 3 + j * 3 + 0]; float green = result_buffer[i * width * 3 + j * 3 + 1]; float blue = result_buffer[i * width * 3 + j * 3 + 2]; auto pixel = image(j,i); pixel->Red = (uint8_t)(red * 255); pixel->Green = (uint8_t)(green * 255); pixel->Blue = (uint8_t)(blue * 255); } } image.WriteToFile("result.bmp"); /// Cleanup free(result_buffer); delete program; OpenGLRuntime::Finalize(); #endif }
// idle function void idleFunc(void) { if (panning) { if (mouseX < (width / 4)) { clMandelbrot.setXPos(clMandelbrot.getXPos() - clMandelbrot.getXStep()); } else if (mouseX > (3 * width / 4)) { clMandelbrot.setXPos(clMandelbrot.getXPos() + clMandelbrot.getXStep()); } if (mouseY < (height / 4)) { clMandelbrot.setYPos(clMandelbrot.getYPos() + clMandelbrot.getYStep()); } else if (mouseY > (3 * height / 4)) { clMandelbrot.setYPos(clMandelbrot.getYPos() - clMandelbrot.getYStep()); } if (zoomIn) { clMandelbrot.setXSize(clMandelbrot.getXSize() * 0.99); } else if (zoomOut) { clMandelbrot.setXSize(clMandelbrot.getXSize() / 0.99); } } clMandelbrot.run(); //clMandelbrot.verifyResults(); glutPostRedisplay(); }
int main(int argc, char * argv[]) { // initalise and run the mandelbrot kernel clMandelbrot.initialize(); if(clMandelbrot.parseCommandLine(argc, argv)) return SDK_FAILURE; if(clMandelbrot.isDumpBinaryEnabled()) { return clMandelbrot.genBinaryImage(); } else { int returnVal = clMandelbrot.setup(); if(returnVal != SDK_SUCCESS) return (returnVal == SDK_EXPECTED_FAILURE)? SDK_SUCCESS : SDK_FAILURE; if(clMandelbrot.run()!=SDK_SUCCESS) return SDK_FAILURE; if(clMandelbrot.verifyResults()!=SDK_SUCCESS) return SDK_FAILURE; // show window if it is not running in quiet mode if(clMandelbrot.showWindow()) { width = clMandelbrot.getWidth(); height = clMandelbrot.getHeight(); output = (unsigned char *)clMandelbrot.getPixels(); initDisplay(argc, argv); mainLoopGL(); } cleanup(); } return SDK_SUCCESS; }
int main (int argc, char* argv[]) { sf::RenderWindow App (sf::VideoMode (1024, 768), "Mandelbrot"); App.SetFramerateLimit (10); Mandelbrot mandelbrot (App); int numIterations = 50; sf::Vector2i start, end; // the start of the mouse selection bool dragging = false; sf::Color transparent (0, 0, 0, 0); // used for drawing the inside of the zoom rect while (App.IsOpened()) { sf::Event Event; while (App.GetEvent (Event)) { switch (Event.Type) { case sf::Event::Closed: App.Close (); break; case sf::Event::KeyPressed: switch (Event.Key.Code) { case sf::Key::Escape: if (dragging) { dragging = false; App.SetFramerateLimit (10); } else { App.Close (); } break; case sf::Key::R: mandelbrot.Reset (); mandelbrot.SetNumIterations (50); numIterations = 50; break; default: break; } break; case sf::Event::MouseWheelMoved: numIterations += Event.MouseWheel.Delta; numIterations = (numIterations < 0) ? 0 : numIterations; mandelbrot.SetNumIterations (numIterations); break; case sf::Event::MouseButtonPressed: start.x = Event.MouseButton.X; start.y = Event.MouseButton.Y; end = start; dragging = true; App.SetFramerateLimit (30); // so that the app doesn't lag when drawing the rect break; case sf::Event::MouseButtonReleased: if (dragging) { mandelbrot.Zoom (start, sf::Vector2i (Event.MouseButton.X, Event.MouseButton.Y)); dragging = false; App.SetFramerateLimit (10); } break; case sf::Event::MouseMoved: end.x = Event.MouseMove.X; end.y = Event.MouseMove.Y; break; default: break; } } //Clear the screen App.Clear(); mandelbrot.Draw(); if (dragging) { App.Draw (sf::Shape::Rectangle (start.x, start.y, end.x, end.y, transparent, 1, sf::Color::Blue)); } App.Display(); } return 0; }
public: Grafik(Mandelbrot *model) { this->model = model; farbenMaske = farbenMasken[0]; setBoundingRect(0, 0, model->getBreite() - 2, model->getHoehe() - 2); }
int main(int argc, char **argv) { InitAllegro(); ALLEGRO_KEYBOARD_STATE klawiatura; ALLEGRO_MOUSE_STATE mysz; ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue(); ALLEGRO_EVENT_QUEUE* key_queue = al_create_event_queue(); ALLEGRO_DISPLAY* okno = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT); ALLEGRO_FONT* defaultFont = al_create_builtin_font(); ALLEGRO_TIMER* timer = al_create_timer(1.0 / 60.0); al_register_event_source(key_queue, al_get_display_event_source(okno)); al_register_event_source(event_queue, al_get_display_event_source(okno)); al_register_event_source(key_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_set_window_title(okno, "Fraktale"); al_set_target_bitmap(al_get_backbuffer(okno)); Menu* menu = new Menu(WINDOW_WIDTH, WINDOW_HEIGHT); Mandelbrot* mandelbrot = new Mandelbrot(WINDOW_WIDTH, WINDOW_HEIGHT, defaultFont); Julia* julia = new Julia(WINDOW_WIDTH, WINDOW_HEIGHT, defaultFont); Window* window = new Window(menu, mandelbrot, julia); menu->SetWindow(window); mandelbrot->SetWindow(window); julia->SetWindow(window); const ALLEGRO_COLOR backgroundColor = al_map_rgb(255, 255, 255); const ALLEGRO_COLOR frameColor = al_map_rgb(255, 255, 255); const int frameThickness = 2; ALLEGRO_USTR* str = al_ustr_new(""); int pos = (int)al_ustr_size(str); ALLEGRO_EVENT ev; double blokadaKlikniecia = al_get_time(); int poczX = -1, poczY = -1, poprzedniStan = window->stanOkna; bool petla = true, klikniecieMyszy = false, klawiszWcisniety = false, wpisywanieIteracji = false; double screenRatio = static_cast<double>(WINDOW_HEIGHT) / static_cast<double>(WINDOW_WIDTH); while (petla) { if (poprzedniStan != window->stanOkna) { blokadaKlikniecia = al_get_time(); } poprzedniStan = window->stanOkna; al_get_next_event(event_queue, &ev); al_get_mouse_state(&mysz); int mx = mysz.x; int my = mysz.y; bool koniecKlikniecia = false; if (mysz.buttons & 1 && klikniecieMyszy == false && al_get_time() > blokadaKlikniecia + 0.3) { klikniecieMyszy = true; if (window->CzyFraktal()) { poczX = mx; poczY = my; } } else if (!(mysz.buttons & 1) && klikniecieMyszy == true && al_get_time() > blokadaKlikniecia + 0.3) { klikniecieMyszy = false; int pp = window->stanOkna; int respond = window->Click(mx, my); if (respond == Responds_t::RESPOND_CLOSE_WINDOW) petla = false; if (window->CzyFraktal() && (pp == WINDOWSTATE_MANDELBROT || pp == WINDOWSTATE_JULIA)) { koniecKlikniecia = true; } if (pp == WINDOWSTATE_MENU && window->CzyFraktal()) { window->ZaladujFraktal(); } } if (koniecKlikniecia) { if (window->CzyFraktal()) { Fraktal* fraktal = window->AktualnyFraktal(); fraktal->Powieksz(poczX, mx, poczY, SkalujY(poczY, screenRatio, poczX, mx)); } poczX = -1, poczY = -1; } al_get_next_event(key_queue, &ev); if (ev.type == ALLEGRO_EVENT_KEY_DOWN && klawiszWcisniety == false && window->CzyFraktal()) { int kod = ev.keyboard.keycode; klawiszWcisniety = true; if (kod == 9) { wpisywanieIteracji = true; } else if (kod == 10) { Fraktal* fraktal = window->AktualnyFraktal(); fraktal->Resetuj(); } else if (kod >= 27 && kod <= 36 && wpisywanieIteracji) { pos += al_ustr_append_chr(str, kod + 21); } else if (kod == ALLEGRO_KEY_ENTER) { if (wpisywanieIteracji == true) wpisywanieIteracji = false; unsigned char* tmp = str->data; int t = atoi((const char*)tmp); window->SetIteracje(t); } else if (kod == ALLEGRO_KEY_BACKSPACE) { if (al_ustr_prev(str, &pos)) al_ustr_truncate(str, pos); } else if (kod == ALLEGRO_KEY_ESCAPE) { window->stanOkna = WINDOWSTATE_MENU; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { klawiszWcisniety = false; } al_clear_to_color(backgroundColor); if (wpisywanieIteracji) window->Draw(str); else window->Draw(NULL); if(poczX != -1) al_draw_rectangle(poczX, poczY, mx, SkalujY(poczY, screenRatio, poczX, mx), frameColor, frameThickness); al_flip_display(); } al_destroy_display(okno); return 0; }