Added zooming and scrolling, everything draws nicely now

master
MitchellHansen 8 years ago
parent 2dfed7958a
commit ed651853f6

@ -23,6 +23,7 @@
#include <SFML/Graphics.hpp>
#include <unordered_map>
#include <iostream>
#include "Vector4.hpp"
struct device {
@ -47,7 +48,7 @@ public:
OpenCL(sf::Vector2i resolution);
~OpenCL();
bool init();
bool init(sf::Vector4f *range);
void run_kernel(std::string kernel_name);
@ -95,9 +96,11 @@ private:
// Create a buffer with CL_MEM_READ_ONLY and CL_MEM_COPY_HOST_PTR
int create_buffer(std::string buffer_name, cl_uint size, void* data);
// Create a buffer with user defined data access flags
int create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags);
// Store a cl_mem object in the buffer map <string:name, cl_mem:buffer>
int store_buffer(cl_mem buffer, std::string buffer_name);

@ -21,7 +21,7 @@ __kernel void mandlebrot (
float y = 0.0;
int iteration_count = 0;
int interation_threshold = 1000;
int interation_threshold = 2000;
while (x*x + y*y < 4 && iteration_count < interation_threshold) {
float x_temp = x*x - y*y + x0;
@ -31,8 +31,15 @@ __kernel void mandlebrot (
}
int val = scale(iteration_count, 0, 1000, 0, 16777216);
//printf("%i", ((val >> 8) & 0xff));
write_imagef(image, pixel, (float4)(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, 200));
float r = scale((val & 0xff), 0, 255, 0, 1);
float g = scale((val >> 8) & 0xff, 0, 255, 0, 1);
float b = scale((val >> 16) & 0xff, 0, 255, 0, 1);
// write_imagei(image, pixel, (int4)((val & 0xff), ((val >> 8) & 0xff), ((val >> 16) & 0xff), 200));
write_imagef(image, pixel, (float4)(r, g, b, 200));
return;

@ -442,7 +442,8 @@ OpenCL::~OpenCL() {
}
bool OpenCL::init() {
bool OpenCL::init(sf::Vector4f *range)
{
// Initialize opencl up to the point where we start assigning buffers
aquire_hardware();
@ -457,9 +458,8 @@ bool OpenCL::init() {
create_image_buffer("viewport_image", viewport_texture.getSize().x * viewport_texture.getSize().x * 4 * sizeof(float), &viewport_texture, CL_MEM_WRITE_ONLY);
create_buffer("image_res", sizeof(sf::Vector2i), &viewport_resolution);
create_buffer("range", sizeof(sf::Vector4f), range, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR);
sf::Vector4i range(-1.0f, 1.0f, -1.0f, 1.0f);
create_buffer("range", sizeof(sf::Vector4i), &range);
set_kernel_arg("mandlebrot", 0, "image_res");
set_kernel_arg("mandlebrot", 1, "viewport_image");

@ -79,33 +79,11 @@ int main() {
fps_counter fps;
OpenCL cl(sf::Vector2i(WINDOW_X, WINDOW_Y));
cl.init();
sf::Vector4f range(-1.0f, 1.0f, -1.0f, 1.0f);
cl.init(&range);
sf::Uint8 *pixels = new sf::Uint8[WINDOW_X * WINDOW_Y * 4];
sf::Sprite viewport_sprite;
sf::Texture viewport_texture;
viewport_texture.create(WINDOW_X, WINDOW_Y);
viewport_texture.update(pixels);
viewport_sprite.setTexture(viewport_texture);
std::vector<std::thread> thread_pool;
for (int i = 0; i < 10; i++) {
thread_pool.emplace_back(std::thread(func, i, 10, pixels));
}
for (auto &t: thread_pool) {
t.join();
}
viewport_texture.update(pixels);
while (window.isOpen())
{
sf::Event event; // Handle input
@ -113,6 +91,36 @@ int main() {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Down) {
range.z += 0.001;
range.w += 0.001;
}
if (event.key.code == sf::Keyboard::Up) {
range.z -= 0.001;
range.w -= 0.001;
}
if (event.key.code == sf::Keyboard::Right) {
range.x += 0.001;
range.y += 0.001;
}
if (event.key.code == sf::Keyboard::Left) {
range.x -= 0.001;
range.y -= 0.001;
}
if (event.key.code == sf::Keyboard::Equal) {
range.x *= 1.02;
range.y *= 1.02;
range.z *= 1.02;
range.w *= 1.02;
}
if (event.key.code == sf::Keyboard::Dash) {
range.x *= 0.98;
range.y *= 0.98;
range.z *= 0.98;
range.w *= 0.98;
}
}
}
elapsed_time = elap_time(); // Handle time
@ -130,9 +138,13 @@ int main() {
// Do physics at 60fps
}
cl.run_kernel("mandlebrot");
window.clear(sf::Color::White);
window.draw(viewport_sprite);
cl.draw(&window);
//window.draw(viewport_sprite);
fps.draw(&window);
fps.frame(delta_time);

Loading…
Cancel
Save