Added a kernel to help test opencl data passing renamed the kernels, buffers, etc.master
parent
5528e03c69
commit
d1bd4ce667
@ -1,138 +0,0 @@
|
||||
#pragma once
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <list>
|
||||
|
||||
class Curses {
|
||||
public:
|
||||
|
||||
struct Slot {
|
||||
Slot(wchar_t unicode_value_,
|
||||
sf::Color font_color_,
|
||||
sf::Color backfill_color_) :
|
||||
unicode_value(unicode_value_),
|
||||
font_color(font_color_),
|
||||
backfill_color(backfill_color_)
|
||||
{};
|
||||
|
||||
wchar_t unicode_value;
|
||||
sf::Color font_color;
|
||||
sf::Color backfill_color;
|
||||
};
|
||||
|
||||
struct Tile {
|
||||
public:
|
||||
Tile(sf::Vector2i position_) :
|
||||
blank_standby(L'\u0020', sf::Color::Transparent, sf::Color::Black),
|
||||
position(position_)
|
||||
{ };
|
||||
|
||||
private:
|
||||
Slot blank_standby;
|
||||
|
||||
int index = 0; // What index in the vector are we. Backbone for blinking and scrolling
|
||||
int ratio_counter = 0; // Secondary counter to hold index positions for (ratio) length of time
|
||||
int ratio_value = 0;
|
||||
|
||||
std::vector<Slot> slot_stack; // The icon that aligns with the index
|
||||
|
||||
sf::Vector2i position; // Position of the text, and backfill
|
||||
|
||||
public:
|
||||
void set_ratio(int ratio_) {
|
||||
ratio_value = ratio_;
|
||||
}
|
||||
|
||||
sf::Vector2i getPosition() const {
|
||||
return position;
|
||||
}
|
||||
|
||||
void push_back(Slot s) {
|
||||
slot_stack.push_back(s);
|
||||
}
|
||||
|
||||
void clear_and_set(Slot s) {
|
||||
slot_stack.clear();
|
||||
slot_stack.push_back(s);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
slot_stack.clear();
|
||||
}
|
||||
|
||||
sf::Color current_font_color() {
|
||||
if (slot_stack.size() > 0)
|
||||
return slot_stack.at(index).font_color;
|
||||
else
|
||||
return blank_standby.font_color;
|
||||
}
|
||||
|
||||
sf::Color current_backfill_color() {
|
||||
if (slot_stack.size() > 0)
|
||||
return slot_stack.at(index).backfill_color;
|
||||
else
|
||||
return blank_standby.backfill_color;
|
||||
}
|
||||
|
||||
wchar_t current_unicode_value() {
|
||||
if (slot_stack.size() > 0)
|
||||
return slot_stack.at(index).unicode_value;
|
||||
else
|
||||
return blank_standby.unicode_value;
|
||||
}
|
||||
|
||||
void inc_index() {
|
||||
if (index >= slot_stack.size() - 1) {
|
||||
index = 0;
|
||||
}
|
||||
else if (ratio_counter == ratio_value) {
|
||||
ratio_counter = 0;
|
||||
index++;
|
||||
}
|
||||
else
|
||||
ratio_counter++;
|
||||
}
|
||||
};
|
||||
|
||||
Curses(sf::Vector2i tile_size_, sf::Vector2i grid_dimensions);
|
||||
~Curses();
|
||||
|
||||
|
||||
void Update(double delta_time_);
|
||||
void Render();
|
||||
|
||||
|
||||
void setTile(Tile tile_);
|
||||
void setTiles(std::vector<Tile> tiles_); // Can be seperate, non-adjacent tiles
|
||||
|
||||
void Clear();
|
||||
|
||||
Tile* getTile(sf::Vector2i position_);
|
||||
std::vector<Curses::Tile*> getTiles(sf::Vector2i start_, sf::Vector2i end_);
|
||||
|
||||
void ResizeTiles(sf::Vector2i size_);
|
||||
void ResizeTileGrid(sf::Vector2i grid_dimensions_);
|
||||
|
||||
void setBlink(int ratio_, sf::Vector2i position_);
|
||||
void setScroll(int ratio_, sf::Vector2i start_, sf::Vector2i end_);
|
||||
void setScroll(int ratio_, sf::Vector2i start_, std::list<Slot> tiles_);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
sf::Vector2i grid_dimensions;
|
||||
sf::Vector2i tile_pixel_dimensions;
|
||||
|
||||
sf::RenderWindow window;
|
||||
|
||||
std::vector<Tile> tiles;
|
||||
|
||||
sf::Font font;
|
||||
|
||||
int multi_to_linear(sf::Vector2i position_) const;
|
||||
sf::Vector2i linear_to_multi(int position_) const;
|
||||
|
||||
void set_tile_ratio(int ratio_, sf::Vector2i tile_position_);
|
||||
void append_slots(sf::Vector2i start_, std::list<Slot> values_);
|
||||
|
||||
};
|
||||
|
@ -1,125 +0,0 @@
|
||||
// global : local : constant : private
|
||||
|
||||
// Function arguments of type image2d_t, image3d_t, image2d_array_t, image1d_t, image1d_buffer_t,
|
||||
// and image1d_array_t refer to image memory objects allocated in the **global** address space.
|
||||
|
||||
// http://downloads.ti.com/mctools/esd/docs/opencl/memory/buffers.html
|
||||
|
||||
// Open CL C
|
||||
// https://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook/opencl-c/
|
||||
|
||||
__kernel void hello(
|
||||
global int2* resolution,
|
||||
global char* map,
|
||||
global float3* projection_matrix,
|
||||
global float3* cam_dir,
|
||||
global float3* cam_pos,
|
||||
global image2d_t* canvas) {
|
||||
|
||||
printf("%s\n", "this is a test string\n");
|
||||
|
||||
|
||||
|
||||
const int MAX_RAY_STEPS = 64;
|
||||
|
||||
// The pixel coord we are at
|
||||
int2 screenPos = (int2)(get_global_id(0) % resolution->x, get_global_id(0) / resolution->x);
|
||||
|
||||
// The X and Y planes
|
||||
//float3 cameraPlaneU = vec3(1.0, 0.0, 0.0)
|
||||
|
||||
// Y being multiplied by the aspect ratio, usually around .5-6ish;
|
||||
//cl_float3 cameraPlaneV = vec3(0.0, 1.0, 0.0) * iResolution.y / iResolution.x;
|
||||
|
||||
// So this is how they do that ray aiming! hah this is so tiny
|
||||
// (camera direction) + (pixel.x * the X plane) + (product of pixel.y * Y plane)
|
||||
// Oh all it's doing is adding the x and y coords of the pixel to the camera direction vector, interesting
|
||||
|
||||
//cl_float3 rayDir = cameraDir + screenPos.x * cameraPlaneU + screenPos.y * cameraPlaneV;
|
||||
|
||||
// the origin of the ray
|
||||
// So the sign thing is for the up and down motion
|
||||
|
||||
//cl_float3 rayPos = vec3(0.0, 2.0 * sin(iGlobalTime * 2.7), -12.0);
|
||||
|
||||
// Ah, and here is where it spins around the center axis
|
||||
// So it looks like its applying a function to rotate the x and z axis
|
||||
//rayPos.xz = rotate2d(rayPos.xz, iGlobalTime);
|
||||
//rayDir.xz = rotate2d(rayDir.xz, iGlobalTime);
|
||||
|
||||
// Just an intvec of out coords
|
||||
//ivec3 mapPos = ivec3(floor(rayPos));
|
||||
|
||||
// I think this is the delta t value
|
||||
// the magnitude of the vector divided by the rays direction. Not sure what the aim of that is
|
||||
// The ray direction might always be normalized, so that would be the dame as my delta_T
|
||||
//vec3 deltaDist = abs(vec3(length(rayDir)) / rayDir);
|
||||
|
||||
// The steps are the signs of the ray direction
|
||||
//ivec3 rayStep = ivec3(sign(rayDir));
|
||||
|
||||
// ithe sign of the rays direction
|
||||
// *
|
||||
// Convert map position to a floating point vector and take away the ray position
|
||||
// +
|
||||
// the sign of the rays direction by 0.5
|
||||
// +
|
||||
// 0.5
|
||||
// Now multyply everything by 0.5
|
||||
//vec3 sideDist = (sign(rayDir) * (vec3(mapPos) - rayPos) + (sign(rayDir) * 0.5) + 0.5) * deltaDist;
|
||||
|
||||
// A byte mask
|
||||
//bvec3 mask;
|
||||
|
||||
// repeat until the max steps
|
||||
//for (int i = 0; i < MAX_RAY_STEPS; i++) {
|
||||
|
||||
// If there is a voxel at the map position, continue?
|
||||
//if (getVoxel(mapPos))
|
||||
// break;
|
||||
|
||||
//
|
||||
// find which is smaller
|
||||
// y ? z --> x`
|
||||
// z ? x --> y`
|
||||
// x ? y --> z`
|
||||
//
|
||||
// find which os is less or equal
|
||||
// x` ? x --> x
|
||||
// y` ? y --> y
|
||||
// z` ? z --> z
|
||||
|
||||
// Now find which ons is
|
||||
//mask = lessThanEqual(sideDist.xyz, min(sideDist.yzx, sideDist.zxy));
|
||||
|
||||
|
||||
// Originally he used a component wise
|
||||
/*bvec3 b1 = lessThan(sideDist.xyz, sideDist.yzx);
|
||||
bvec3 b2 = lessThanEqual(sideDist.xyz, sideDist.zxy);
|
||||
mask.x = b1.x && b2.x;
|
||||
mask.y = b1.y && b2.y;
|
||||
mask.z = b1.z && b2.z;*/
|
||||
//Would've done mask = b1 && b2 but the compiler is making me do it component wise.
|
||||
|
||||
//All components of mask are false except for the corresponding largest component
|
||||
//of sideDist, which is the axis along which the ray should be incremented.
|
||||
|
||||
//sideDist += vec3(mask) * deltaDist;
|
||||
//mapPos += ivec3(mask) * rayStep;
|
||||
//}
|
||||
|
||||
// Ah this is for coloring obviously, seems to be odd though, no indexing
|
||||
//vec4 color;
|
||||
//if (mask.x) {
|
||||
// color = vec4(0.5);
|
||||
//}
|
||||
//if (mask.y) {
|
||||
// color = vec4(1.0);
|
||||
//}
|
||||
//if (mask.z) {
|
||||
// color = vec4(0.75);
|
||||
//}
|
||||
//write_imagef(image, pixel, color);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
__kernel void printer(
|
||||
global char* map,
|
||||
global int3* map_dim,
|
||||
global int2* resolution,
|
||||
global float3* projection_matrix,
|
||||
global float2* cam_dir,
|
||||
global float3* cam_pos,
|
||||
global float* lights,
|
||||
global int* light_count,
|
||||
__write_only image2d_t image
|
||||
) {
|
||||
|
||||
size_t id = get_global_id(0);
|
||||
|
||||
if (id == 0) {
|
||||
|
||||
printf("MAP: %i, %i, %i, %i", map[0], map[1], map[2], map[3]);
|
||||
printf("MAP_DIMENSIONS: %i, %i, %i", map_dim[0].x, map_dim[0].y, map_dim[0].z);
|
||||
printf("RESOLUTION: %i, %i", resolution[0].x, resolution[0].y);
|
||||
printf("PROJECTION_MATRIX: %f, %f, %f", projection_matrix[0].x, projection_matrix[0].y, projection_matrix[0].z);
|
||||
printf("CAMERA_DIRECTION: %f, %f", cam_dir[0].x, cam_dir[0].y);
|
||||
printf("CAMERA_POSITION: %f, %f, %f", cam_pos[0].x, cam_pos[0].y, cam_pos[0].z);
|
||||
printf("LIGHTS: %f, %f, %f, %f, %f, %f, %f, %f, %f, %f", lights[0], lights[1], lights[2], lights[3], lights[4], lights[5], lights[6], lights[7], lights[8], lights[9]);
|
||||
printf("LIGHT_COUNT: %i", light_count);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
}
|
@ -1,187 +0,0 @@
|
||||
#pragma once
|
||||
#include "Curses.h"
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
Curses::Curses(sf::Vector2i tile_size_, sf::Vector2i grid_dimensions_) :
|
||||
window(sf::VideoMode(tile_size_.x * grid_dimensions_.x, tile_size_.y * grid_dimensions_.y), "SimpleFML Curses"),
|
||||
grid_dimensions(grid_dimensions_),
|
||||
tile_pixel_dimensions(tile_size_){
|
||||
|
||||
font.loadFromFile("unifont.ttf");
|
||||
|
||||
for (int y = 0; y < grid_dimensions_.y; y++) {
|
||||
for (int x = 0 ; x < grid_dimensions_.x; x++) {
|
||||
tiles.emplace_back(Tile(sf::Vector2i(x, y)));
|
||||
|
||||
// L'\u0020' = space char
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Set screen values to increasing unicode chars
|
||||
/*wchar_t char_ind = L'\u0041';
|
||||
for (int i = 0; i < tiles.size(); i++) {
|
||||
tiles.at(i).push_clear(char_ind++, sf::Color::White, sf::Color::Black);
|
||||
}*/
|
||||
}
|
||||
|
||||
Curses::~Curses() {
|
||||
}
|
||||
|
||||
void Curses::Update(double delta_time_) {
|
||||
|
||||
for (Tile &tile : tiles) {
|
||||
tile.inc_index();
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed)
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
void Curses::Render() {
|
||||
window.clear();
|
||||
|
||||
sf::Texture font_texture = font.getTexture(tile_pixel_dimensions.x);
|
||||
|
||||
// Draw text and backfills
|
||||
sf::VertexArray backfill_v_arr(sf::Quads, grid_dimensions.x * grid_dimensions.y * 4);
|
||||
sf::VertexArray font_v_arr(sf::Quads, grid_dimensions.x * grid_dimensions.y * 4);
|
||||
|
||||
int tile_index = 0;
|
||||
|
||||
for (int i = 0; i < backfill_v_arr.getVertexCount(); i += 4) {
|
||||
|
||||
Tile* tile = &tiles.at(tile_index);
|
||||
sf::Glyph glyph = font.getGlyph(tile->current_unicode_value(), tile_pixel_dimensions.x, false);
|
||||
|
||||
// Backfill the tile with the specified backfill color
|
||||
backfill_v_arr[i + 0].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y);
|
||||
backfill_v_arr[i + 1].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y);
|
||||
backfill_v_arr[i + 2].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + tile_pixel_dimensions.y);
|
||||
backfill_v_arr[i + 3].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + tile_pixel_dimensions.y);
|
||||
|
||||
backfill_v_arr[i + 0].color = tile->current_backfill_color();
|
||||
backfill_v_arr[i + 1].color = tile->current_backfill_color();
|
||||
backfill_v_arr[i + 2].color = tile->current_backfill_color();
|
||||
backfill_v_arr[i + 3].color = tile->current_backfill_color();
|
||||
|
||||
// Draw the font with the correct size, texture location, window location, and color
|
||||
font_v_arr[i + 0].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y);
|
||||
font_v_arr[i + 1].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + glyph.textureRect.width, tile->getPosition().y * tile_pixel_dimensions.y);
|
||||
font_v_arr[i + 2].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + glyph.textureRect.width, tile->getPosition().y * tile_pixel_dimensions.y + glyph.textureRect.height);
|
||||
font_v_arr[i + 3].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + glyph.textureRect.height);
|
||||
|
||||
// Make the letter appear in the center of the tile by applying an offset
|
||||
sf::Vector2f position_offset = sf::Vector2f(tile_pixel_dimensions.x / 4, tile_pixel_dimensions.y / 4);
|
||||
font_v_arr[i + 0].position += position_offset;
|
||||
font_v_arr[i + 1].position += position_offset;
|
||||
font_v_arr[i + 2].position += position_offset;
|
||||
font_v_arr[i + 3].position += position_offset;
|
||||
|
||||
font_v_arr[i + 0].texCoords = sf::Vector2f(glyph.textureRect.left, glyph.textureRect.top);
|
||||
font_v_arr[i + 1].texCoords = sf::Vector2f(glyph.textureRect.width + glyph.textureRect.left, glyph.textureRect.top);
|
||||
font_v_arr[i + 2].texCoords = sf::Vector2f(glyph.textureRect.width + glyph.textureRect.left, glyph.textureRect.top + glyph.textureRect.height);
|
||||
font_v_arr[i + 3].texCoords = sf::Vector2f(glyph.textureRect.left, glyph.textureRect.top + glyph.textureRect.height);
|
||||
|
||||
font_v_arr[i + 0].color = tile->current_font_color();
|
||||
font_v_arr[i + 1].color = tile->current_font_color();
|
||||
font_v_arr[i + 2].color = tile->current_font_color();
|
||||
font_v_arr[i + 3].color = tile->current_font_color();
|
||||
|
||||
tile_index++;
|
||||
}
|
||||
|
||||
window.draw(backfill_v_arr);
|
||||
window.draw(font_v_arr, &font_texture);
|
||||
|
||||
window.display();
|
||||
}
|
||||
|
||||
void Curses::setTile(Tile tile_) {
|
||||
tiles.at(multi_to_linear(tile_.getPosition())) = tile_;
|
||||
}
|
||||
|
||||
void Curses::setTiles(std::vector<Tile> tiles_) {
|
||||
for (Tile tile: tiles_) {
|
||||
tiles.at(multi_to_linear(tile.getPosition())) = tile;
|
||||
}
|
||||
}
|
||||
|
||||
void Curses::Clear() {
|
||||
for (Tile &tile : tiles) {
|
||||
tile.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Curses::Tile* Curses::getTile(sf::Vector2i position_) {
|
||||
return &tiles.at(multi_to_linear(position_));
|
||||
}
|
||||
|
||||
std::vector<Curses::Tile*> Curses::getTiles(sf::Vector2i start_, sf::Vector2i end_) {
|
||||
std::vector<Curses::Tile*> ret;
|
||||
for (int i = multi_to_linear(start_); i < multi_to_linear(end_); i++) {
|
||||
ret.push_back(&tiles.at(i));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Curses::setScroll(int ratio_, sf::Vector2i start_, std::list<Slot> scroll_text_) {
|
||||
// Scrolling and it's scroll ratio is faux implemented by
|
||||
// essentially stacking values and repeating them to slow the scroll down
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < scroll_text_.size(); i++) {
|
||||
append_slots(start_, scroll_text_);
|
||||
scroll_text_.push_back(scroll_text_.front());
|
||||
scroll_text_.pop_front();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Curses::set_tile_ratio(int ratio_, sf::Vector2i tile_position_) {
|
||||
getTile(tile_position_)->set_ratio(ratio_);
|
||||
}
|
||||
|
||||
void Curses::append_slots(sf::Vector2i start_, std::list<Slot> values_)
|
||||
{
|
||||
std::vector<Tile*> tiles = getTiles(start_,
|
||||
sf::Vector2i((values_.size() + start_.x) % grid_dimensions.x,
|
||||
(values_.size() + start_.x) / grid_dimensions.x + start_.y));
|
||||
|
||||
if (tiles.size() != values_.size()) {
|
||||
std::cout << "Values did not match up to slots when appending\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::list<Slot>::iterator beg_slot_it = values_.begin();
|
||||
std::list<Slot>::iterator end_slot_it = values_.end();
|
||||
std::list<Slot>::iterator slot_it;
|
||||
|
||||
std::vector<Tile*>::iterator beg_tile_it = tiles.begin();
|
||||
std::vector<Tile*>::iterator end_tile_it = tiles.end();
|
||||
std::vector<Tile*>::iterator tile_it;
|
||||
|
||||
for (slot_it = beg_slot_it, tile_it = beg_tile_it;
|
||||
(slot_it != end_slot_it) && (tile_it != end_tile_it);
|
||||
++slot_it, ++tile_it) {
|
||||
|
||||
Tile* t = *tile_it;
|
||||
t->push_back(*slot_it);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
sf::Vector2i Curses::linear_to_multi(int position_) const {
|
||||
return sf::Vector2i(position_ % grid_dimensions.x, position_ / grid_dimensions.x);
|
||||
}
|
||||
int Curses::multi_to_linear(sf::Vector2i position_) const {
|
||||
return position_.y * grid_dimensions.x + position_.x;
|
||||
}
|
@ -1,110 +1,111 @@
|
||||
#include "RayCaster.h"
|
||||
#include <util.hpp>
|
||||
#include <Ray.h>
|
||||
|
||||
RayCaster::RayCaster() {
|
||||
}
|
||||
|
||||
void RayCaster::assign_map(Old_Map* map) {
|
||||
this->map = map;
|
||||
}
|
||||
// Override values
|
||||
//this.map_dimensions = new Vector3<int> (50, 50, 50);
|
||||
//this.resolution = new Vector2<int> (200, 200);
|
||||
//this.camera_direction = new Vector3<float> (1f, 0f, .8f);
|
||||
//this.camera_position = new Vector3<float> (1, 10, 10);
|
||||
|
||||
this->map_dimensions = map_dimensions;
|
||||
this->map = map;
|
||||
|
||||
resolution = viewport_resolution;
|
||||
image = new sf::Color[resolution.x * resolution.y];
|
||||
|
||||
|
||||
// Calculate the view plane vectors
|
||||
// Because casting to individual pixels causes barrel distortion,
|
||||
// Get the radian increments
|
||||
// Set the camera origin
|
||||
// Rotate the ray by the specified pixel * increment
|
||||
|
||||
double y_increment_radians = DegreesToRadians(50.0 / resolution.y);
|
||||
double x_increment_radians = DegreesToRadians(80.0 / resolution.x);
|
||||
|
||||
view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y];
|
||||
for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) {
|
||||
for (int x = -resolution.x / 2; x < resolution.x / 2; x++) {
|
||||
|
||||
// The base ray direction to slew from
|
||||
sf::Vector3f ray(1, 0, 0);
|
||||
|
||||
// Y axis, pitch
|
||||
ray = sf::Vector3f(
|
||||
ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y),
|
||||
ray.y,
|
||||
ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y)
|
||||
);
|
||||
|
||||
// Z axis, yaw
|
||||
ray = sf::Vector3f(
|
||||
ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x),
|
||||
ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x),
|
||||
ray.z
|
||||
);
|
||||
|
||||
int index = (x + resolution.x / 2) + resolution.x * (y + resolution.y / 2);
|
||||
view_plane_vectors[index] = Normalize(ray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RayCaster::~RayCaster() {
|
||||
delete image;
|
||||
delete view_plane_vectors;
|
||||
}
|
||||
|
||||
sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<float> camera_position) {
|
||||
|
||||
// Setup the camera for this cast
|
||||
this->camera_direction = camera_direction;
|
||||
camera_direction_cartesian = Normalize(SphereToCart(camera_direction));
|
||||
this->camera_position = camera_position;
|
||||
|
||||
// Start the loop at the top left, scan right and work down
|
||||
for (int y = 0; y < resolution.y; y++) {
|
||||
for (int x = 0; x < resolution.x; x++) {
|
||||
|
||||
// Get the ray at the base direction
|
||||
sf::Vector3f ray = view_plane_vectors[x + resolution.x * y];
|
||||
|
||||
// Rotate it to the correct pitch and yaw
|
||||
|
||||
// Y axis, pitch
|
||||
ray = sf::Vector3f(
|
||||
ray.z * sin(camera_direction.y) + ray.x * cos(camera_direction.y),
|
||||
ray.y,
|
||||
ray.z * cos(camera_direction.y) - ray.x * sin(camera_direction.y)
|
||||
);
|
||||
|
||||
// Z axis, yaw
|
||||
ray = sf::Vector3f(
|
||||
ray.x * cos(camera_direction.z) - ray.y * sin(camera_direction.z),
|
||||
ray.x * sin(camera_direction.z) + ray.y * cos(camera_direction.z),
|
||||
ray.z
|
||||
);
|
||||
|
||||
|
||||
// Setup the ray
|
||||
Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray);
|
||||
|
||||
// Cast it and assign its return value
|
||||
image[x + resolution.x * y] = r.Cast();
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
void RayCaster::moveCamera(sf::Vector2f v) {
|
||||
camera_direction.y += v.x;
|
||||
camera_direction.z += v.y;
|
||||
}
|
||||
//void RayCaster::assign_map(Old_Map* map) {
|
||||
// this->map = map;
|
||||
//}
|
||||
// // Override values
|
||||
// //this.map_dimensions = new Vector3<int> (50, 50, 50);
|
||||
// //this.resolution = new Vector2<int> (200, 200);
|
||||
// //this.camera_direction = new Vector3<float> (1f, 0f, .8f);
|
||||
// //this.camera_position = new Vector3<float> (1, 10, 10);
|
||||
//
|
||||
// this->map_dimensions = map_dimensions;
|
||||
// this->map = map;
|
||||
//
|
||||
// resolution = viewport_resolution;
|
||||
// image = new sf::Color[resolution.x * resolution.y];
|
||||
//
|
||||
//
|
||||
// // Calculate the view plane vectors
|
||||
// // Because casting to individual pixels causes barrel distortion,
|
||||
// // Get the radian increments
|
||||
// // Set the camera origin
|
||||
// // Rotate the ray by the specified pixel * increment
|
||||
//
|
||||
// double y_increment_radians = DegreesToRadians(50.0 / resolution.y);
|
||||
// double x_increment_radians = DegreesToRadians(80.0 / resolution.x);
|
||||
//
|
||||
// view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y];
|
||||
// for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) {
|
||||
// for (int x = -resolution.x / 2; x < resolution.x / 2; x++) {
|
||||
//
|
||||
// // The base ray direction to slew from
|
||||
// sf::Vector3f ray(1, 0, 0);
|
||||
//
|
||||
// // Y axis, pitch
|
||||
// ray = sf::Vector3f(
|
||||
// ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y),
|
||||
// ray.y,
|
||||
// ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y)
|
||||
// );
|
||||
//
|
||||
// // Z axis, yaw
|
||||
// ray = sf::Vector3f(
|
||||
// ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x),
|
||||
// ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x),
|
||||
// ray.z
|
||||
// );
|
||||
//
|
||||
// int index = (x + resolution.x / 2) + resolution.x * (y + resolution.y / 2);
|
||||
// view_plane_vectors[index] = Normalize(ray);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//RayCaster::~RayCaster() {
|
||||
// delete image;
|
||||
// delete view_plane_vectors;
|
||||
//}
|
||||
//
|
||||
//sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<float> camera_position) {
|
||||
//
|
||||
// // Setup the camera for this cast
|
||||
// this->camera_direction = camera_direction;
|
||||
// camera_direction_cartesian = Normalize(SphereToCart(camera_direction));
|
||||
// this->camera_position = camera_position;
|
||||
//
|
||||
// // Start the loop at the top left, scan right and work down
|
||||
// for (int y = 0; y < resolution.y; y++) {
|
||||
// for (int x = 0; x < resolution.x; x++) {
|
||||
//
|
||||
// // Get the ray at the base direction
|
||||
// sf::Vector3f ray = view_plane_vectors[x + resolution.x * y];
|
||||
//
|
||||
// // Rotate it to the correct pitch and yaw
|
||||
//
|
||||
// // Y axis, pitch
|
||||
// ray = sf::Vector3f(
|
||||
// ray.z * sin(camera_direction.y) + ray.x * cos(camera_direction.y),
|
||||
// ray.y,
|
||||
// ray.z * cos(camera_direction.y) - ray.x * sin(camera_direction.y)
|
||||
// );
|
||||
//
|
||||
// // Z axis, yaw
|
||||
// ray = sf::Vector3f(
|
||||
// ray.x * cos(camera_direction.z) - ray.y * sin(camera_direction.z),
|
||||
// ray.x * sin(camera_direction.z) + ray.y * cos(camera_direction.z),
|
||||
// ray.z
|
||||
// );
|
||||
//
|
||||
//
|
||||
// // Setup the ray
|
||||
// Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray);
|
||||
//
|
||||
// // Cast it and assign its return value
|
||||
// image[x + resolution.x * y] = r.Cast();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return image;
|
||||
//}
|
||||
//
|
||||
//void RayCaster::moveCamera(sf::Vector2f v) {
|
||||
// camera_direction.y += v.x;
|
||||
// camera_direction.z += v.y;
|
||||
//}
|
||||
|
Loading…
Reference in new issue