diff --git a/include/Map.h b/include/Map.h index db3faca..ad7944d 100644 --- a/include/Map.h +++ b/include/Map.h @@ -8,21 +8,15 @@ class Map { public: Map(sf::Vector3i dim) { list = new char[dim.x * dim.y * dim.z]; - for (int i = 0; i < dim.x * dim.y * dim.x; i++) { - list[i] = 0; - } - - for (int x = 0; x < dim.x; x++) { - for (int y = 0; y < dim.y; y++) { - for (int z = 0; z < dim.z; z++) { - - if (dim.z < 30) { - list[x + dim.x * (y + dim.z * z)] = 3; - } - else if (rand() % 1000 < 1) + //for (int i = 0; i < dim.x * dim.y * dim.x; i++) { + // list[i] = 0; + //} + + for (int x = 0; x < dim.x / 10; x++) { + for (int y = 0; y < dim.y / 10; y++) { + for (int z = 0; z < dim.z / 10; z++) { + if (rand() % 1000 < 1) list[x + dim.x * (y + dim.z * z)] = rand() % 6; - else - list[x + dim.x * (y + dim.z * z)] = 0; } } } diff --git a/include/util.hpp b/include/util.hpp index decb2b9..b55a6d9 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -19,6 +19,10 @@ public: } void frame(double delta_time){ + if (frame_count == 100){ + frame_count = 0; + fps_average = 0; + } frame_count++; fps_average += (delta_time - fps_average) / frame_count; } diff --git a/kernels/minimal_kernel.cl b/kernels/minimal_kernel.cl index b981fdb..e1339ab 100644 --- a/kernels/minimal_kernel.cl +++ b/kernels/minimal_kernel.cl @@ -73,67 +73,64 @@ __kernel void min_kern( int face = -1; // X:0, Y:1, Z:2 + + int3 mask = { 0, 0, 0 }; + // Andrew Woo's raycasting algo do { - if ((intersection_t.x) < (intersection_t.y)) { - if ((intersection_t.x) < (intersection_t.z)) { - - face = 0; - voxel.x += voxel_step.x; - intersection_t.x = intersection_t.x + delta_t.x; - } else { - - face = 2; - voxel.z += voxel_step.z; - intersection_t.z = intersection_t.z + delta_t.z; - } - } else { - if ((intersection_t.y) < (intersection_t.z)) { - - face = 1; - voxel.y += voxel_step.y; - intersection_t.y = intersection_t.y + delta_t.y; - } else { - - face = 2; - voxel.z += voxel_step.z; - intersection_t.z = intersection_t.z + delta_t.z; - } - } + + mask = intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy); + float3 thing = delta_t * fabs(convert_float3(mask.xyz)); + intersection_t += delta_t * fabs(convert_float3(mask.xyz)); + voxel.xyz += voxel_step.xyz * mask.xyz; + + // If the ray went out of bounds - if (voxel.z >= map_dim->z) { - write_imagef(image, pixel, (float4)(.5, .50, .00, 1)); - return; - } - if (voxel.x >= map_dim->x) { - write_imagef(image, pixel, (float4)(.00, .00, .99, 1)); - return; - } - if (voxel.y >= map_dim->x) { - write_imagef(image, pixel, (float4)(.00, .44, .00, 1)); - return; - } - - if (voxel.x < 0) { - write_imagef(image, pixel, (float4)(.99, .00, .99, 1)); - return; - } - if (voxel.y < 0) { - write_imagef(image, pixel, (float4)(.99, .99, .00, 1)); - return; - } - if (voxel.z < 0) { - write_imagef(image, pixel, (float4)(.00, .99, .99, 1)); - return; - } + int3 overshoot = voxel.xyz <= map_dim->xyz; + int3 undershoot = voxel > 0; + + //if (id == 240000) + // printf("%i, %i, %i\n", overshoot.x, overshoot.y, overshoot.z); + + //if (id == 240000) + // printf("%i, %i, %i\n", undershoot.x, undershoot.y, undershoot.z); + + if (overshoot.x == 0|| overshoot.y == 0 || overshoot.z == 0){ + write_imagef(image, pixel, (float4)(.50 * abs(overshoot.x), .50 * abs(overshoot.y), .50 * abs(overshoot.z), 1)); + return; + } + if (undershoot.x == 0 || undershoot.y == 0 || undershoot.z == 0) { + write_imagef(image, pixel, (float4)(.1 * abs(undershoot.x), .80 * abs(undershoot.y), .20 * abs(undershoot.z), 1)); + return; + } + //if (voxel.x >= map_dim->x) { + // write_imagef(image, pixel, (float4)(.00, .00, .99, 1)); + // return; + //} + //if (voxel.y >= map_dim->x) { + // write_imagef(image, pixel, (float4)(.00, .44, .00, 1)); + // return; + //} + + //if (voxel.x < 0) { + // write_imagef(image, pixel, (float4)(.99, .00, .99, 1)); + // return; + //} + //if (voxel.y < 0) { + // write_imagef(image, pixel, (float4)(.99, .99, .00, 1)); + // return; + //} + //if (voxel.z < 0) { + // write_imagef(image, pixel, (float4)(.00, .99, .99, 1)); + // return; + //} // If we hit a voxel int index = voxel.x + map_dim->x * (voxel.y + map_dim->z * voxel.z); int voxel_data = map[index]; - //if (id == 240000) - //printf("%i, %i, %i\n", voxel.x, voxel.y, voxel.z); + if (voxel_data != 0) { switch (voxel_data) { diff --git a/src/main.cpp b/src/main.cpp index db97a80..3544600 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,20 +62,22 @@ sf::Texture window_texture; int main() { - sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); + sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); - sf::Sprite s; - sf::Texture t; + sf::Sprite s; + sf::Texture t; - CL_Wrapper c; + CL_Wrapper c; query_platform_devices(); - c.acquire_platform_and_device(); - c.create_shared_context(); - c.create_command_queue(); + c.acquire_platform_and_device(); + c.create_shared_context(); + c.create_command_queue(); - //c.compile_kernel("../kernels/kernel.cl", true, "hello"); - c.compile_kernel("../kernels/minimal_kernel.cl", true, "min_kern"); + //c.compile_kernel("../kernels/kernel.cl", true, "hello"); + if (c.compile_kernel("../kernels/minimal_kernel.cl", true, "min_kern") < 0) { + std::cin.get(); + } std::cout << "map..."; sf::Vector3i map_dim(MAP_X, MAP_Y, MAP_Z); @@ -252,6 +254,14 @@ int main() { // If the user tries to exit the application via the GUI if (event.type == sf::Event::Closed) window.close(); + if (event.type == sf::Event::KeyPressed) { + if (event.key.code == sf::Keyboard::Space) { + if (mouse_enabled) + mouse_enabled = false; + else + mouse_enabled = true; + } + } } cam_vec.x = 0; @@ -289,15 +299,16 @@ int main() { cam_vec.y = -0.1f; } - deltas = fixed - sf::Mouse::getPosition(); - if (deltas != sf::Vector2i(0, 0) && mouse_enabled == true) { + if (mouse_enabled) { + deltas = fixed - sf::Mouse::getPosition(); + if (deltas != sf::Vector2i(0, 0) && mouse_enabled == true) { - // Mouse movement - sf::Mouse::setPosition(fixed); - cam_dir.y -= deltas.y / 300.0f; - cam_dir.z -= deltas.x / 300.0f; + // Mouse movement + sf::Mouse::setPosition(fixed); + cam_dir.y -= deltas.y / 300.0f; + cam_dir.z -= deltas.x / 300.0f; + } } - cam_pos.x += cam_vec.x / 1.0; cam_pos.y += cam_vec.y / 1.0; cam_pos.z += cam_vec.z / 1.0; @@ -355,6 +366,7 @@ int main() { fps.draw(&window); window.display(); + //std::cin.get(); } return 0; }