diff --git a/include/CL_Wrapper.h b/include/CL_Wrapper.h index 693a5e0..1cff709 100644 --- a/include/CL_Wrapper.h +++ b/include/CL_Wrapper.h @@ -38,11 +38,12 @@ public: int create_shared_context(); int create_command_queue(); int compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name); + int create_buffer(std::string buffer_name, cl_uint size, void* data); + int create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags); int set_kernel_arg(std::string kernel_name, int index, std::string buffer_name); int store_buffer(cl_mem, std::string buffer_name); int run_kernel(std::string kernel_name, const int work_size); - - + bool assert(int error_code, std::string function_name); cl_device_id getDeviceID(); diff --git a/include/Camera.h b/include/Camera.h new file mode 100644 index 0000000..201262f --- /dev/null +++ b/include/Camera.h @@ -0,0 +1,45 @@ +#pragma once +#include +#include +#include "util.hpp" + +class Camera { +public: + + enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN }; + + Camera(); + Camera(sf::Vector3f position, sf::Vector2f direction); + ~Camera(); + + int set_position(sf::Vector3f position); + + int add_static_impulse(sf::Vector3f impulse); + int add_relative_impulse(DIRECTION direction); + + int slew_camera(sf::Vector2f input); + + int update(); + + void* get_direction_pointer(); + void* get_position_pointer(); + + sf::Vector3f get_movement(); + sf::Vector3f get_position(); + sf::Vector2f get_direction(); + +private: + + float friction_coefficient = 0.1; + float default_impulse = 1.0; + + // 3D vector + sf::Vector3f movement; + + // XYZ + sf::Vector3f position; + + // These are spherical coords + sf::Vector2f direction; +}; + diff --git a/include/Vector3.h b/include/Vector3.h deleted file mode 100644 index b75d367..0000000 --- a/include/Vector3.h +++ /dev/null @@ -1,139 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2016 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef GAME_VECTOR3_H -#define GAME_VECTOR3_H - -template -class Vector3 -{ -public: - - // Default constructor - // Creates a Vector3(0, 0, 0). - Vector3(); - - - // Construct the vector from its coordinates - Vector3(T X, T Y, T Z); - - - // Construct the vector from another type of vector - // This constructor doesn't replace the copy constructor, - // it's called only when U != T. - // A call to this constructor will fail to compile if U - // is not convertible to T. - template - explicit Vector3(const Vector3& vector); - - // Member data - T x; - T y; - T z; -}; - - // Vector3 - // Overload of unary operator - - // left Vector to negate - // Memberwise opposite of the vector - template - Vector3 operator -(const Vector3& left); - - // Overload of binary operator += - // This operator performs a memberwise addition of both vectors, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator +=(Vector3& left, const Vector3& right); - - // Overload of binary operator -= - // This operator performs a memberwise subtraction of both vectors, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator -=(Vector3& left, const Vector3& right); - - // Overload of binary operator + - // returns Memberwise addition of both vectors - template - Vector3 operator +(const Vector3& left, const Vector3& right); - // Overload of binary operator - - // returns Memberwise subtraction of both vectors - template - Vector3 operator -(const Vector3& left, const Vector3& right); - - // Overload of binary operator * - // returns Memberwise multiplication by right - template - Vector3 operator *(const Vector3& left, T right); - - // Overload of binary operator * - // returns Memberwise multiplication by left - template - Vector3 operator *(T left, const Vector3& right); - - // Overload of binary operator *= - // This operator performs a memberwise multiplication by right, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator *=(Vector3& left, T right); - - // Overload of binary operator / - // returns Memberwise division by right - template - Vector3 operator /(const Vector3& left, T right); - - // Overload of binary operator /= - // This operator performs a memberwise division by right, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator /=(Vector3& left, T right); - - // Overload of binary operator == - // This operator compares strict equality between two vectors. - // returns True if left is equal to right - template - bool operator ==(const Vector3& left, const Vector3& right); - - // Overload of binary operator != - // This operator compares strict difference between two vectors. - // returns True if left is not equal to right - template - bool operator !=(const Vector3& left, const Vector3& right); - -#include - - // Define the most common types - typedef Vector3 Vector3i; - typedef Vector3 Vector3f; - - - -#endif - - - - diff --git a/include/Vector4.hpp b/include/Vector4.hpp new file mode 100644 index 0000000..62c3421 --- /dev/null +++ b/include/Vector4.hpp @@ -0,0 +1,474 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2016 Laurent Gomila (laurent@sfml-dev.org) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_VECTOR4_H +#define SFML_VECTOR4_H + +namespace sf { + //////////////////////////////////////////////////////////// + /// \brief Utility template class for manipulating + /// 2-dimensional vectors + /// + //////////////////////////////////////////////////////////// + template + class Vector4 { + public: + + //////////////////////////////////////////////////////////// + /// \brief Default constructor + /// + /// Creates a Vector4(0, 0). + /// + //////////////////////////////////////////////////////////// + Vector4(); + + //////////////////////////////////////////////////////////// + /// \brief Construct the vector from its coordinates + /// + /// \param X X coordinate + /// \param Y Y coordinate + /// + //////////////////////////////////////////////////////////// + Vector4(T X, T Y, T Z, T W); + + //////////////////////////////////////////////////////////// + /// \brief Construct the vector from another type of vector + /// + /// This constructor doesn't replace the copy constructor, + /// it's called only when U != T. + /// A call to this constructor will fail to compile if U + /// is not convertible to T. + /// + /// \param vector Vector to convert + /// + //////////////////////////////////////////////////////////// + template + explicit Vector4(const Vector4& vector); + + //////////////////////////////////////////////////////////// + // Member data + //////////////////////////////////////////////////////////// + T x; ///< X coordinate of the vector + T y; ///< Y coordinate of the vector + T z; ///< Z coordinate of the vector + T w; ///< W coordinate of the vector + }; + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of unary operator - + /// + /// \param right Vector to negate + /// + /// \return Memberwise opposite of the vector + /// + //////////////////////////////////////////////////////////// + template + Vector4 operator -(const Vector4& right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator += + /// + /// This operator performs a memberwise addition of both vectors, + /// and assigns the result to \a left. + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a vector) + /// + /// \return Reference to \a left + /// + //////////////////////////////////////////////////////////// + template + Vector4& operator +=(Vector4& left, const Vector4& right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator -= + /// + /// This operator performs a memberwise subtraction of both vectors, + /// and assigns the result to \a left. + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a vector) + /// + /// \return Reference to \a left + /// + //////////////////////////////////////////////////////////// + template + Vector4& operator -=(Vector4& left, const Vector4& right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator + + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a vector) + /// + /// \return Memberwise addition of both vectors + /// + //////////////////////////////////////////////////////////// + template + Vector4 operator +(const Vector4& left, const Vector4& right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator - + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a vector) + /// + /// \return Memberwise subtraction of both vectors + /// + //////////////////////////////////////////////////////////// + template + Vector4 operator -(const Vector4& left, const Vector4& right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator * + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a scalar value) + /// + /// \return Memberwise multiplication by \a right + /// + //////////////////////////////////////////////////////////// + template + Vector4 operator *(const Vector4& left, T right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator * + /// + /// \param left Left operand (a scalar value) + /// \param right Right operand (a vector) + /// + /// \return Memberwise multiplication by \a left + /// + //////////////////////////////////////////////////////////// + template + Vector4 operator *(T left, const Vector4& right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator *= + /// + /// This operator performs a memberwise multiplication by \a right, + /// and assigns the result to \a left. + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a scalar value) + /// + /// \return Reference to \a left + /// + //////////////////////////////////////////////////////////// + template + Vector4& operator *=(Vector4& left, T right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator / + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a scalar value) + /// + /// \return Memberwise division by \a right + /// + //////////////////////////////////////////////////////////// + template + Vector4 operator /(const Vector4& left, T right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator /= + /// + /// This operator performs a memberwise division by \a right, + /// and assigns the result to \a left. + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a scalar value) + /// + /// \return Reference to \a left + /// + //////////////////////////////////////////////////////////// + template + Vector4& operator /=(Vector4& left, T right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator == + /// + /// This operator compares strict equality between two vectors. + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a vector) + /// + /// \return True if \a left is equal to \a right + /// + //////////////////////////////////////////////////////////// + template + bool operator ==(const Vector4& left, const Vector4& right); + + //////////////////////////////////////////////////////////// + /// \relates Vector4 + /// \brief Overload of binary operator != + /// + /// This operator compares strict difference between two vectors. + /// + /// \param left Left operand (a vector) + /// \param right Right operand (a vector) + /// + /// \return True if \a left is not equal to \a right + /// + //////////////////////////////////////////////////////////// + template + bool operator !=(const Vector4& left, const Vector4& right); + + + + //////////////////////////////////////////////////////////// + template + inline Vector4::Vector4() : + x(0), + y(0), + z(0), + w(0){ + + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4::Vector4(T X, T Y, T Z, T W) : + x(X), + y(Y), + z(Z), + w(W) { + + } + + + //////////////////////////////////////////////////////////// + template + template + inline Vector4::Vector4(const Vector4& vector) : + x(static_cast(vector.x)), + y(static_cast(vector.y)), + z(static_cast(vector.z)), + w(static_cast(vector.w)) { + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4 operator -(const Vector4& right) { + return Vector4( + -right.x, + -right.y, + -right.z, + -right.w + ); + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4& operator +=(Vector4& left, const Vector4& right) { + left.x += right.x; + left.y += right.y; + left.z += right.z; + left.w += right.w; + + return left; + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4& operator -=(Vector4& left, const Vector4& right) { + left.x -= right.x; + left.y -= right.y; + left.z -= right.z; + left.w -= right.w; + + return left; + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4 operator +(const Vector4& left, const Vector4& right) { + return Vector4( + left.x + right.x, + left.y + right.y, + left.z + right.z, + left.w + right.w + ); + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4 operator -(const Vector4& left, const Vector4& right) { + return Vector4( + left.x - right.x, + left.y - right.y, + left.z - right.z, + left.w - right.w + ); + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4 operator *(const Vector4& left, T right) { + return Vector4( + left.x * right, + left.y * right, + left.z * right, + left.w * right + ); + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4 operator *(T left, const Vector4& right) { + return Vector4( + right.x * left, + right.y * left, + right.z * left, + right.w * left + ); + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4& operator *=(Vector4& left, T right) { + left.x *= right; + left.y *= right; + left.z *= right; + left.w *= right; + + return left; + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4 operator /(const Vector4& left, T right) { + return Vector4( + left.x / right, + left.y / right, + left.z / right, + left.w / right + ); + } + + + //////////////////////////////////////////////////////////// + template + inline Vector4& operator /=(Vector4& left, T right) { + left.x /= right; + left.y /= right; + left.z /= right; + left.w /= right; + + return left; + } + + + //////////////////////////////////////////////////////////// + template + inline bool operator ==(const Vector4& left, const Vector4& right) { + return + (left.x == right.x) && + (left.y == right.y) && + (left.z == right.z) && + (left.w == right.w);; + } + + + //////////////////////////////////////////////////////////// + template + inline bool operator !=(const Vector4& left, const Vector4& right) { + return + (left.x != right.x) || + (left.y != right.y) || + (left.z != right.z) || + (left.w != right.w); + } + + // Define the most common types + typedef Vector4 Vector4i; + typedef Vector4 Vector4u; + typedef Vector4 Vector4f; + +} // namespace sf + + +#endif // SFML_Vector4_HPP + + + //////////////////////////////////////////////////////////// + /// \class sf::Vector4 + /// \ingroup system + /// + /// sf::Vector4 is a simple class that defines a mathematical + /// vector with two coordinates (x and y). It can be used to + /// represent anything that has two dimensions: a size, a point, + /// a velocity, etc. + /// + /// The template parameter T is the type of the coordinates. It + /// can be any type that supports arithmetic operations (+, -, /, *) + /// and comparisons (==, !=), for example int or float. + /// + /// You generally don't have to care about the templated form (sf::Vector4), + /// the most common specializations have special typedefs: + /// \li sf::Vector4 is sf::Vector4f + /// \li sf::Vector4 is sf::Vector4i + /// \li sf::Vector4 is sf::Vector4u + /// + /// The sf::Vector4 class has a small and simple interface, its x and y members + /// can be accessed directly (there are no accessors like setX(), getX()) and it + /// contains no mathematical function like dot product, cross product, length, etc. + /// + /// Usage example: + /// \code + /// sf::Vector4f v1(16.5f, 24.f); + /// v1.x = 18.2f; + /// float y = v1.y; + /// + /// sf::Vector4f v2 = v1 * 5.f; + /// sf::Vector4 + /// v3 = v1 + v2; + /// + /// bool different = (v2 != v3); + /// \endcode + /// + /// Note: for 3-dimensional vectors, see sf::Vector3. + /// + //////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/include/util.hpp b/include/util.hpp index b55a6d9..4b693f3 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -40,6 +40,16 @@ private: }; +inline sf::Vector3f SphereToCart(sf::Vector2f i) { + + auto r = sf::Vector3f( + (1 * sin(i.y) * cos(i.x)), + (1 * sin(i.y) * sin(i.x)), + (1 * cos(i.y)) + ); + return r; +}; + inline sf::Vector3f SphereToCart(sf::Vector3f i) { auto r = sf::Vector3f( diff --git a/kernels/minimal_kernel.cl b/kernels/minimal_kernel.cl index 565df95..7ed7ec3 100644 --- a/kernels/minimal_kernel.cl +++ b/kernels/minimal_kernel.cl @@ -18,7 +18,7 @@ __kernel void min_kern( global int3* map_dim, global int2* resolution, global float3* projection_matrix, - global float3* cam_dir, + global float2* cam_dir, global float3* cam_pos, global float* lights, global int* light_count, @@ -26,18 +26,18 @@ __kernel void min_kern( ){ size_t id = get_global_id(0); - int2 pixel = {id % resolution->x, id / resolution->x}; - float3 ray_dir = projection_matrix[pixel.x + resolution->x * pixel.y]; + int2 pixel = {id % (*resolution).x, id / (*resolution).x}; + float3 ray_dir = projection_matrix[pixel.x + (*resolution).x * pixel.y]; ray_dir = (float3)( - ray_dir.z * sin(cam_dir->y) + ray_dir.x * cos(cam_dir->y), + ray_dir.z * sin((*cam_dir).x) + ray_dir.x * cos((*cam_dir).x), ray_dir.y, - ray_dir.z * cos(cam_dir->y) - ray_dir.x * sin(cam_dir->y) + ray_dir.z * cos((*cam_dir).x) - ray_dir.x * sin((*cam_dir).x) ); ray_dir = (float3)( - ray_dir.x * cos(cam_dir->z) - ray_dir.y * sin(cam_dir->z), - ray_dir.x * sin(cam_dir->z) + ray_dir.y * cos(cam_dir->z), + ray_dir.x * cos((*cam_dir).y) - ray_dir.y * sin((*cam_dir).y), + ray_dir.x * sin((*cam_dir).y) + ray_dir.y * cos((*cam_dir).y), ray_dir.z ); @@ -92,7 +92,7 @@ __kernel void min_kern( } // If we hit a voxel - int index = voxel.x + map_dim->x * (voxel.y + map_dim->z * voxel.z); + int index = voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * voxel.z); int voxel_data = map[index]; if (voxel_data != 0) { diff --git a/src/CL_Wrapper.cpp b/src/CL_Wrapper.cpp index d531f02..1118a36 100644 --- a/src/CL_Wrapper.cpp +++ b/src/CL_Wrapper.cpp @@ -233,6 +233,38 @@ int CL_Wrapper::set_kernel_arg( } +int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags) { + + cl_mem buff = clCreateBuffer( + getContext(), flags, + size, data, &error + ); + + if (assert(error, "clCreateBuffer")) + return -1; + + store_buffer(buff, buffer_name); + + return 1; + +} + +int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data) { + + cl_mem buff = clCreateBuffer( + getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, + size, data, &error + ); + + if (assert(error, "clCreateBuffer")) + return -1; + + store_buffer(buff, buffer_name); + + return 1; + +} + int CL_Wrapper::store_buffer(cl_mem buffer, std::string buffer_name){ buffer_map.emplace(std::make_pair(buffer_name, buffer)); return 1; diff --git a/src/Camera.cpp b/src/Camera.cpp new file mode 100644 index 0000000..e87209f --- /dev/null +++ b/src/Camera.cpp @@ -0,0 +1,67 @@ +#include "Camera.h" + + + +Camera::Camera() { +} + + +Camera::Camera(sf::Vector3f position, sf::Vector2f direction) : + position(position), direction(direction) + { + +} + +Camera::~Camera() { +} + +int Camera::set_position(sf::Vector3f position) { + this->position = position; + return 1; +} + +int Camera::add_static_impulse(sf::Vector3f impulse) { + movement += impulse; + return 1; +} + +int Camera::add_relative_impulse(DIRECTION impulse_direction) { + + SphereToCart(direction); + + return 1; +} + +int Camera::slew_camera(sf::Vector2f input) { + direction -= input; + return 1; +} + +int Camera::update() { + + position += movement; + + movement *= friction_coefficient; + + return 1; +} + +void* Camera::get_direction_pointer() { + return &direction; +} + +void* Camera::get_position_pointer() { + return &position; +} + +sf::Vector3f Camera::get_movement() { + return movement; +} + +sf::Vector3f Camera::get_position() { + return position; +} + +sf::Vector2f Camera::get_direction() { + return direction; +} \ No newline at end of file diff --git a/src/Vector3.cpp b/src/Vector3.cpp deleted file mode 100644 index 3dfcd90..0000000 --- a/src/Vector3.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by Mitchell Hansen on 8/7/16. -// - -#include "Vector3.h" diff --git a/src/main.cpp b/src/main.cpp index 8e89114..28c7ea6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,9 +31,12 @@ #include "util.hpp" #include "RayCaster.h" #include "CL_Wrapper.h" +#include "Vector4.hpp" +#include const int WINDOW_X = 1000; const int WINDOW_Y = 1000; +const int WORK_SIZE = WINDOW_X * WINDOW_Y; const int MAP_X = 1024; const int MAP_Y = 1024; @@ -65,9 +68,7 @@ int main() { sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); - sf::Sprite s; - sf::Texture t; - + // Setup CL, instantiate and pass in values to the kernel CL_Wrapper c; query_platform_devices(); c.acquire_platform_and_device(); @@ -82,35 +83,21 @@ int main() { std::cout << "map..."; sf::Vector3i map_dim(MAP_X, MAP_Y, MAP_Z); Map* map = new Map(map_dim); - std::cout << "done..."; - map->setVoxel(sf::Vector3i(77, 50, 85), 5); - - cl_mem map_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - sizeof(char) * map_dim.x * map_dim.y * map_dim.z, map->list, NULL - ); - - cl_mem dim_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - sizeof(int) * 3, &map_dim, NULL - ); - - sf::Vector2i view_res(WINDOW_X, WINDOW_Y); - - cl_mem res_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - sizeof(int) * 2, &view_res, NULL - ); + c.create_buffer("map_buffer", sizeof(char) * map_dim.x * map_dim.y * map_dim.z, map->list); + c.create_buffer("dim_buffer", sizeof(int) * 3, &map_dim); + sf::Vector2i view_res(WINDOW_X, WINDOW_Y); + c.create_buffer("res_buffer", sizeof(int) * 2, &view_res); + double y_increment_radians = DegreesToRadians(50.0 / view_res.y); double x_increment_radians = DegreesToRadians(80.0 / view_res.x); - // SFML 2.4 has Vector4 datatypes....... - std::cout << "view matrix..."; - float* view_matrix = new float[WINDOW_X * WINDOW_Y * 4]; + + sf::Vector4f* view_matrix = new sf::Vector4f[WINDOW_X * WINDOW_Y * 4]; + for (int y = -view_res.y / 2; y < view_res.y / 2; y++) { for (int x = -view_res.x / 2; x < view_res.x / 2; x++) { @@ -133,51 +120,34 @@ int main() { int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2); ray = Normalize(ray); - view_matrix[index * 4 + 0] = ray.x; - view_matrix[index * 4 + 1] = ray.y; - view_matrix[index * 4 + 2] = ray.z; - view_matrix[index * 4 + 3] = 0; + + view_matrix[index] = sf::Vector4f( + ray.x, + ray.y, + ray.z, + 0 + ); } } - std::cout << "done\n"; - int ind = 367; - printf("%i === %f, %f, %f\n", ind, view_matrix[ind * 4 + 0], view_matrix[ind * 4 + 1], view_matrix[ind * 4 + 2]); - - cl_mem view_matrix_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - sizeof(float) * 3 * view_res.x * view_res.y, view_matrix, NULL - ); - - sf::Vector3f cam_dir(1.0f, 0.0f, 1.00f); - - cl_mem cam_dir_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, - sizeof(float) * 4, &cam_dir, NULL - ); - - sf::Vector3f cam_pos(55, 50, 50); - - cl_mem cam_pos_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, - sizeof(float) * 4, &cam_pos, NULL - ); + c.create_buffer("view_matrix_buffer", sizeof(float) * 4 * view_res.x * view_res.y, view_matrix); + Camera camera( + sf::Vector3f(55, 50, 50), + sf::Vector2f(0.0f, 1.00f) + ); + + c.create_buffer("cam_dir_buffer", sizeof(float) * 4, camera.get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); + c.create_buffer("cam_pos_buffer", sizeof(float) * 4, camera.get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); + // {r, g, b, i, x, y, z, x', y', z'} float light[] = { 0.4, 0.8, 0.1, 1, 50, 50, 50, 1.1, 0.4, 0.7}; - - cl_mem light_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - sizeof(float) * 10, light, NULL - ); + c.create_buffer("light_buffer", sizeof(float) * 10, light); int light_count = 1; + c.create_buffer("light_count_buffer", sizeof(int), &light_count); - cl_mem light_cnt_buff = clCreateBuffer( - c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - sizeof(int), &light_count, NULL - ); - + // The drawing canvas unsigned char* pixel_array = new sf::Uint8[WINDOW_X * WINDOW_Y * 4]; for (int i = 0; i < WINDOW_X * WINDOW_Y * 4; i += 4) { @@ -188,12 +158,11 @@ int main() { pixel_array[i + 3] = 100; // A? } + sf::Texture t; t.create(WINDOW_X, WINDOW_Y); t.update(pixel_array); - int error; - cl_mem image_buff = clCreateFromGLTexture( c.getContext(), CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, t.getNativeHandle(), &error); @@ -201,14 +170,6 @@ int main() { if (c.assert(error, "clCreateFromGLTexture")) return -1; - c.store_buffer(map_buff, "map_buffer"); - c.store_buffer(dim_buff, "dim_buffer"); - c.store_buffer(res_buff, "res_buffer"); - c.store_buffer(view_matrix_buff, "view_matrix_buffer"); - c.store_buffer(cam_dir_buff, "cam_dir_buffer"); - c.store_buffer(cam_pos_buff, "cam_pos_buffer"); - c.store_buffer(light_buff, "light_buffer"); - c.store_buffer(light_cnt_buff, "light_count_buffer"); c.store_buffer(image_buff, "image_buffer"); c.set_kernel_arg("min_kern", 0, "map_buffer"); @@ -221,9 +182,9 @@ int main() { c.set_kernel_arg("min_kern", 7, "light_count_buffer"); c.set_kernel_arg("min_kern", 8, "image_buffer"); - const int size = WINDOW_X * WINDOW_Y; - - s.setTexture(t); + sf::Sprite s; + s.setTexture(t); + s.setPosition(0, 0); // The step size in milliseconds between calls to Update() // Lets set it to 16.6 milliseonds (60FPS) @@ -258,6 +219,8 @@ int main() { sf::Vector2i fixed(window.getSize()); bool mouse_enabled = true; + sf::Vector3f cam_mov_vec; + while (window.isOpen()) { // Poll for events from the user @@ -299,18 +262,8 @@ int main() { if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { cam_vec.x = -1; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - cam_dir.z = -0.1f; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - cam_vec.z = +0.1f; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { - cam_vec.y = +0.1f; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { - cam_vec.y = -0.1f; - } + + camera.add_static_impulse(cam_vec); if (mouse_enabled) { deltas = fixed - sf::Mouse::getPosition(); @@ -318,16 +271,12 @@ int main() { // Mouse movement sf::Mouse::setPosition(fixed); - cam_dir.y -= deltas.y / 300.0f; - cam_dir.z -= deltas.x / 300.0f; + camera.slew_camera(sf::Vector2f( + deltas.y / 300.0f, + 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; - - std::cout << cam_vec.x << " : " << cam_vec.y << " : " << cam_vec.z << std::endl; - // Time keeping elapsed_time = elap_time(); @@ -339,32 +288,20 @@ int main() { while ((accumulator_time - step_size) >= step_size) { accumulator_time -= step_size; - // Update cycle - } - - // Fps cycle - // map->moveLight(sf::Vector2f(0.3, 0)); - - window.clear(sf::Color::Black); - - // Cast the rays and get the image - //sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos); - - // Cast it to an array of Uint8's - //auto out = (sf::Uint8*)pixel_colors; - - //window_texture.update(out); - //window_sprite.setTexture(window_texture); - //window.draw(window_sprite); - - // Give the frame counter the frame time and draw the average frame time + + // ==== DELTA TIME LOCKED ==== + camera.update(); + } + // ==== FPS LOCKED ==== + // Run the raycast error = clEnqueueAcquireGLObjects(c.getCommandQueue(), 1, &image_buff, 0, 0, 0); if (c.assert(error, "clEnqueueAcquireGLObjects")) return -1; - c.run_kernel("min_kern", size); + + c.run_kernel("min_kern", WORK_SIZE); clFinish(c.getCommandQueue()); @@ -372,14 +309,19 @@ int main() { if (c.assert(error, "clEnqueueReleaseGLObjects")) return -1; - s.setPosition(0, 0); - window.draw(s); + // ==== RENDER ==== + + window.clear(sf::Color::Black); + + window.draw(s); + + // Give the frame counter the frame time and draw the average frame time fps.frame(delta_time); fps.draw(&window); + window.display(); - //std::cin.get(); } return 0; }