Saving minor tweaks

master
MitchellHansen 8 years ago
parent 518cc757a3
commit 5528e03c69

@ -40,20 +40,20 @@ public:
virtual ~Hardware_Caster(); virtual ~Hardware_Caster();
int init(); int init() override;
// In interop mode, this will create a gl texture that we share // In interop mode, this will create a gl texture that we share
// Otherwise, it will create the pixel buffer and pass that in as an image, retreiving it each draw // Otherwise, it will create the pixel buffer and pass that in as an image, retreiving it each draw
// Both will create the view matrix, view res buffer // Both will create the view matrix, view res buffer
void create_viewport(int width, int height, float v_fov, float h_fov); void create_viewport(int width, int height, float v_fov, float h_fov) override;
void assign_light(Light light); void assign_light(std::string light_id, Light light) override;
void assign_map(Old_Map *map); void assign_map(Old_Map *map) override;
void assign_camera(Camera *camera); void assign_camera(Camera *camera) override;
// draw will abstract the gl sharing and software rendering // draw will abstract the gl sharing and software rendering
// methods of retrieving the screen buffer // methods of retrieving the screen buffer
void draw(sf::RenderWindow* window) = 0; void draw(sf::RenderWindow* window) override;
private: private:
@ -86,13 +86,6 @@ private:
cl_kernel getKernel(std::string kernel_name); cl_kernel getKernel(std::string kernel_name);
cl_command_queue getCommandQueue(); cl_command_queue getCommandQueue();
bool was_init_valid();
int error = 0;
bool initialized = false;
bool cl_khr_gl_sharing_fallback = false;
bool cl_supported = false;
cl_platform_id platform_id; cl_platform_id platform_id;
cl_device_id device_id; cl_device_id device_id;
cl_context context; cl_context context;

@ -13,7 +13,7 @@ public:
SHARING_NOT_SUPPORTED = 800, SHARING_NOT_SUPPORTED = 800,
OPENCL_NOT_SUPPORTED = 801, OPENCL_NOT_SUPPORTED = 801,
OPENCL_ERROR = 802, OPENCL_ERROR = 802,
ERROR = 803 ERR = 803
}; };
RayCaster(); RayCaster();
@ -24,7 +24,7 @@ public:
virtual void assign_map(Old_Map *map) = 0; virtual void assign_map(Old_Map *map) = 0;
virtual void assign_camera(Camera *camera) = 0; virtual void assign_camera(Camera *camera) = 0;
virtual void create_viewport(int width, int height, float v_fov, float h_fov) = 0; virtual void create_viewport(int width, int height, float v_fov, float h_fov) = 0;
virtual void assign_light(Light light) = 0; virtual void assign_light(std::string light_id, Light light) = 0;
// draw will abstract the gl sharing and software rendering // draw will abstract the gl sharing and software rendering
// methods of retrieving the screen buffer // methods of retrieving the screen buffer
@ -35,30 +35,13 @@ protected:
sf::Sprite viewport_sprite; sf::Sprite viewport_sprite;
sf::Texture viewport_texture; sf::Texture viewport_texture;
// A reference to the map
Old_Map * map; Old_Map * map;
Camera *camera;
std::map<std::string, Light> light_map;
sf::Uint8 *viewport_image;
sf::Vector4f *viewport_matrix;
// The XY resolution of the viewport int error = 0;
sf::Vector2<int> resolution;
// The pixel array, maybe do RBGA? Are there even 4 byte data types?
sf::Color *image;
// The direction of the camera in POLAR coordinates
sf::Vector3<float> camera_direction;
// Convert the polar coordinates to CARTESIAN
sf::Vector3<float> camera_direction_cartesian;
// The world-space position of the camera
sf::Vector3<float> camera_position;
// The distance in units the view plane is from the iris point
int view_plane_distance = 200;
// Precalculated values for the view plane rays
sf::Vector3f *view_plane_vectors;
}; };

@ -14,8 +14,6 @@ int Hardware_Caster::init() {
// Initialize opencl up to the point where we start assigning buffers // Initialize opencl up to the point where we start assigning buffers
int error = 0;
error = acquire_platform_and_device(); error = acquire_platform_and_device();
if(assert(error, "aquire_platform_and_device")) if(assert(error, "aquire_platform_and_device"))
return error; return error;
@ -44,6 +42,7 @@ int Hardware_Caster::init() {
void Hardware_Caster::assign_map(Old_Map *map) { void Hardware_Caster::assign_map(Old_Map *map) {
this->map = map;
auto dimensions = map->getDimensions(); auto dimensions = map->getDimensions();
create_buffer("map_buffer", sizeof(char) * dimensions.x * dimensions.y * dimensions.z, map->get_voxel_data()); create_buffer("map_buffer", sizeof(char) * dimensions.x * dimensions.y * dimensions.z, map->get_voxel_data());
@ -53,6 +52,8 @@ void Hardware_Caster::assign_map(Old_Map *map) {
void Hardware_Caster::assign_camera(Camera *camera) { void Hardware_Caster::assign_camera(Camera *camera) {
this->camera = camera;
create_buffer("cam_dir_buffer", sizeof(float) * 4, (void*)camera->get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); create_buffer("cam_dir_buffer", sizeof(float) * 4, (void*)camera->get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR);
create_buffer("cam_pos_buffer", sizeof(float) * 4, (void*)camera->get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); create_buffer("cam_pos_buffer", sizeof(float) * 4, (void*)camera->get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR);
} }
@ -74,7 +75,7 @@ void Hardware_Caster::create_viewport(int width, int height, float v_fov, float
double y_increment_radians = DegreesToRadians(v_fov / view_res.y); double y_increment_radians = DegreesToRadians(v_fov / view_res.y);
double x_increment_radians = DegreesToRadians(h_fov / view_res.x); double x_increment_radians = DegreesToRadians(h_fov / view_res.x);
sf::Vector4f* view_matrix = new sf::Vector4f[width * height * 4]; viewport_matrix = new sf::Vector4f[width * height * 4];
for (int y = -view_res.y / 2; y < view_res.y / 2; y++) { 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++) { for (int x = -view_res.x / 2; x < view_res.x / 2; x++) {
@ -100,7 +101,7 @@ void Hardware_Caster::create_viewport(int width, int height, float v_fov, float
int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2); int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2);
ray = Normalize(ray); ray = Normalize(ray);
view_matrix[index] = sf::Vector4f( viewport_matrix[index] = sf::Vector4f(
ray.x, ray.x,
ray.y, ray.y,
ray.z, ray.z,
@ -109,30 +110,30 @@ void Hardware_Caster::create_viewport(int width, int height, float v_fov, float
} }
} }
create_buffer("view_matrix_buffer", sizeof(float) * 4 * view_res.x * view_res.y, view_matrix); create_buffer("view_matrix_buffer", sizeof(float) * 4 * view_res.x * view_res.y, viewport_matrix);
// Create the image that opencl's rays write to // Create the image that opencl's rays write to
unsigned char* pixel_array = new sf::Uint8[width * height * 4]; viewport_image = new sf::Uint8[width * height * 4];
for (int i = 0; i < width * height * 4; i += 4) { for (int i = 0; i < width * height * 4; i += 4) {
pixel_array[i] = 255; // R viewport_image[i] = 255; // R
pixel_array[i + 1] = 255; // G viewport_image[i + 1] = 255; // G
pixel_array[i + 2] = 255; // B viewport_image[i + 2] = 255; // B
pixel_array[i + 3] = 100; // A viewport_image[i + 3] = 100; // A
} }
// Interop lets us keep a reference to it as a texture // Interop lets us keep a reference to it as a texture
viewport_texture.create(width, height); viewport_texture.create(width, height);
viewport_texture.update(pixel_array); viewport_texture.update(viewport_image);
viewport_sprite.setTexture(viewport_texture); viewport_sprite.setTexture(viewport_texture);
// Pass the buffer to opencl // Pass the buffer to opencl
create_image_buffer("image_buffer", sizeof(sf::Uint8) * width * height * 4, pixel_array); create_image_buffer("image_buffer", sizeof(sf::Uint8) * width * height * 4, viewport_image);
} }
void Hardware_Caster::assign_light(Light light) { void Hardware_Caster::assign_light(std::string light_id, Light light) {
} }
@ -166,7 +167,6 @@ int Hardware_Caster::acquire_platform_and_device() {
// Check to see if we even have opencl on this machine // Check to see if we even have opencl on this machine
if (deviceIdCount == 0) { if (deviceIdCount == 0) {
cl_supported = false;
std::cout << "There appears to be no platforms supporting opencl" << std::endl; std::cout << "There appears to be no platforms supporting opencl" << std::endl;
return OPENCL_NOT_SUPPORTED; return OPENCL_NOT_SUPPORTED;
} }
@ -523,10 +523,6 @@ cl_context Hardware_Caster::getContext() { return context; };
cl_kernel Hardware_Caster::getKernel(std::string kernel_name) { return kernel_map.at(kernel_name); }; cl_kernel Hardware_Caster::getKernel(std::string kernel_name) { return kernel_map.at(kernel_name); };
cl_command_queue Hardware_Caster::getCommandQueue() { return command_queue; }; cl_command_queue Hardware_Caster::getCommandQueue() { return command_queue; };
bool Hardware_Caster::was_init_valid() {
return cl_supported;
}
bool Hardware_Caster::assert(int error_code, std::string function_name) { bool Hardware_Caster::assert(int error_code, std::string function_name) {
// Just gonna do a little jump table here, just error codes so who cares // Just gonna do a little jump table here, just error codes so who cares
@ -723,7 +719,7 @@ bool Hardware_Caster::assert(int error_code, std::string function_name) {
case RayCaster::OPENCL_ERROR: case RayCaster::OPENCL_ERROR:
err_msg = "OPENCL_ERROR"; err_msg = "OPENCL_ERROR";
break; break;
case RayCaster::ERROR: case RayCaster::ERR:
err_msg = "ERROR"; err_msg = "ERROR";
break; break;
} }

@ -66,77 +66,45 @@ sf::Texture window_texture;
int main() { int main() {
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); // It looks like I got the bulk of the stuff moved over to hardware caster.
// The lights still need work. Adding them to a map and checking for collisions
// will probably be the route I take.
// Need to hook up the assignment of kernel args
// Also need to hook up the rendering with the draw function.
RayCaster rc = new Hardware_Caster()
CL_Wrapper c;
if (c.compile_kernel("../kernels/ray_caster_kernel.cl", true, "min_kern") < 0) {
std::cin.get(); sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML");
return -1;
// Initialize the raycaster hardware, compat, or software
RayCaster *rc = new Hardware_Caster();
if (rc->init() != 0) {
delete rc;
// rc = new Hardware_Caster_Compat();
// if (rc->init() != 0) {
// delete rc;
// rc = new Software_Caster();
// }
} }
// This will be removed
CL_Wrapper c;
std::cout << "map..."; std::cout << "map...";
sf::Vector3i map_dim(MAP_X, MAP_Y, MAP_Z); sf::Vector3i map_dim(MAP_X, MAP_Y, MAP_Z);
Old_Map* map = new Old_Map(map_dim); Old_Map* map = new Old_Map(map_dim);
map->generate_terrain(); map->generate_terrain();
c.create_buffer("map_buffer", sizeof(char) * map_dim.x * map_dim.y * map_dim.z, map->get_voxel_data()); rc->assign_map(map);
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.0f / view_res.y);
double x_increment_radians = DegreesToRadians(80.0f / view_res.x);
std::cout << "view matrix...";
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++) {
// The base ray direction to slew from
sf::Vector3f ray(1, 0, 0);
// Y axis, pitch
ray = sf::Vector3f(
static_cast<float>(ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y)),
static_cast<float>(ray.y),
static_cast<float>(ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y))
);
// Z axis, yaw Camera *camera = new Camera(
ray = sf::Vector3f(
static_cast<float>(ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x)),
static_cast<float>(ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x)),
static_cast<float>(ray.z)
);
int index = (x + view_res.x / 2) + view_res.x * (y + view_res.y / 2);
ray = Normalize(ray);
view_matrix[index] = sf::Vector4f(
ray.x,
ray.y,
ray.z,
0
);
}
}
c.create_buffer("view_matrix_buffer", sizeof(float) * 4 * view_res.x * view_res.y, view_matrix);
Camera camera(
sf::Vector3f(0, 0, 0), sf::Vector3f(0, 0, 0),
sf::Vector2f(0.0f, 1.00f) sf::Vector2f(0.0f, 1.00f)
); );
c.create_buffer("cam_dir_buffer", sizeof(float) * 4, (void*)camera.get_direction_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); rc->assign_camera(camera);
c.create_buffer("cam_pos_buffer", sizeof(float) * 4, (void*)camera.get_position_pointer(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR);
rc->create_viewport(WINDOW_X, WINDOW_Y, 50.0f, 80.0f);
int light_count = 2; int light_count = 2;
c.create_buffer("light_count_buffer", sizeof(int), &light_count); c.create_buffer("light_count_buffer", sizeof(int), &light_count);
@ -148,30 +116,6 @@ int main() {
0.4f, 0.8f, 0.1f, 1.0f, 50.0f, 50.0f, 50.0f, v2.x, v2.y, v2.z}; 0.4f, 0.8f, 0.1f, 1.0f, 50.0f, 50.0f, 50.0f, v2.x, v2.y, v2.z};
c.create_buffer("light_buffer", sizeof(float) * 10 * light_count, light, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); c.create_buffer("light_buffer", sizeof(float) * 10 * light_count, light, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR);
// 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) {
pixel_array[i] = 255; // R?
pixel_array[i + 1] = 255; // G?
pixel_array[i + 2] = 255; // B?
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);
if (c.assert(error, "clCreateFromGLTexture"))
return -1;
c.store_buffer(image_buff, "image_buffer");
c.set_kernel_arg("min_kern", 0, "map_buffer"); c.set_kernel_arg("min_kern", 0, "map_buffer");
c.set_kernel_arg("min_kern", 1, "dim_buffer"); c.set_kernel_arg("min_kern", 1, "dim_buffer");
@ -183,9 +127,6 @@ int main() {
c.set_kernel_arg("min_kern", 7, "light_count_buffer"); c.set_kernel_arg("min_kern", 7, "light_count_buffer");
c.set_kernel_arg("min_kern", 8, "image_buffer"); c.set_kernel_arg("min_kern", 8, "image_buffer");
sf::Sprite s;
s.setTexture(t);
s.setPosition(0, 0);
// The step size in milliseconds between calls to Update() // The step size in milliseconds between calls to Update()
// Lets set it to 16.6 milliseonds (60FPS) // Lets set it to 16.6 milliseonds (60FPS)
@ -212,11 +153,11 @@ int main() {
//RayCaster ray_caster(map, map_dim, view_res); //RayCaster ray_caster(map, map_dim, view_res);
sf::Vector2f *dp = camera.get_direction_pointer(); sf::Vector2f *dp = camera->get_direction_pointer();
debug_text cam_text_x(1, 30, &dp->x, "incli: "); debug_text cam_text_x(1, 30, &dp->x, "incli: ");
debug_text cam_text_y(2, 30, &dp->y, "asmth: "); debug_text cam_text_y(2, 30, &dp->y, "asmth: ");
sf::Vector3f *mp = camera.get_movement_pointer(); sf::Vector3f *mp = camera->get_movement_pointer();
debug_text cam_text_mov_x(4, 30, &mp->x, "X: "); debug_text cam_text_mov_x(4, 30, &mp->x, "X: ");
debug_text cam_text_mov_y(5, 30, &mp->y, "Y: "); debug_text cam_text_mov_y(5, 30, &mp->y, "Y: ");
debug_text cam_text_mov_z(6, 30, &mp->y, "Z: "); debug_text cam_text_mov_z(6, 30, &mp->y, "Z: ");
@ -263,28 +204,28 @@ int main() {
speed = 0.2f; speed = 0.2f;
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
camera.add_relative_impulse(Camera::DIRECTION::DOWN, speed); camera->add_relative_impulse(Camera::DIRECTION::DOWN, speed);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
camera.add_relative_impulse(Camera::DIRECTION::UP, speed); camera->add_relative_impulse(Camera::DIRECTION::UP, speed);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
camera.add_relative_impulse(Camera::DIRECTION::FORWARD, speed); camera->add_relative_impulse(Camera::DIRECTION::FORWARD, speed);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
camera.add_relative_impulse(Camera::DIRECTION::REARWARD, speed); camera->add_relative_impulse(Camera::DIRECTION::REARWARD, speed);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
camera.add_relative_impulse(Camera::DIRECTION::LEFT, speed); camera->add_relative_impulse(Camera::DIRECTION::LEFT, speed);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
camera.add_relative_impulse(Camera::DIRECTION::RIGHT, speed); camera->add_relative_impulse(Camera::DIRECTION::RIGHT, speed);
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::T)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::T)) {
camera.set_position(sf::Vector3f(50, 50, 50)); camera->set_position(sf::Vector3f(50, 50, 50));
} }
camera.add_static_impulse(cam_vec); camera->add_static_impulse(cam_vec);
if (mouse_enabled) { if (mouse_enabled) {
deltas = fixed - sf::Mouse::getPosition(); deltas = fixed - sf::Mouse::getPosition();
@ -292,7 +233,7 @@ int main() {
// Mouse movement // Mouse movement
sf::Mouse::setPosition(fixed); sf::Mouse::setPosition(fixed);
camera.slew_camera(sf::Vector2f( camera->slew_camera(sf::Vector2f(
deltas.y / 300.0f, deltas.y / 300.0f,
deltas.x / 300.0f deltas.x / 300.0f
)); ));
@ -329,10 +270,11 @@ int main() {
light[9] = l[2]; light[9] = l[2];
// ==== FPS LOCKED ==== // ==== FPS LOCKED ====
camera.update(delta_time); camera->update(delta_time);
// Run the raycast // Run the raycast
c.run_kernel("min_kern", WORK_SIZE); rc->draw(&window);
//c.run_kernel("min_kern", WORK_SIZE);
// ==== RENDER ==== // ==== RENDER ====

Loading…
Cancel
Save