|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <SFML/Graphics.hpp>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "Map.h"
|
|
|
|
@ -10,99 +11,87 @@ Ray::Ray(
|
|
|
|
|
sf::Vector2<int> resolution,
|
|
|
|
|
sf::Vector2<int> pixel,
|
|
|
|
|
sf::Vector3<float> camera_position,
|
|
|
|
|
sf::Vector3<float> ray_direction){
|
|
|
|
|
sf::Vector3<float> ray_direction) {
|
|
|
|
|
|
|
|
|
|
this->map = map;
|
|
|
|
|
origin = camera_position;
|
|
|
|
|
direction = ray_direction;
|
|
|
|
|
|
|
|
|
|
dimensions = map->getDimensions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sf::Color Ray::Cast(){
|
|
|
|
|
sf::Color Ray::Cast() {
|
|
|
|
|
|
|
|
|
|
// Get the cartesian direction for computing slope
|
|
|
|
|
// Get the cartesian direction for computing
|
|
|
|
|
sf::Vector3<float> cartesian = SphereToCart(direction);
|
|
|
|
|
|
|
|
|
|
// Compute the slopes
|
|
|
|
|
delta_t = sf::Vector3<float>(
|
|
|
|
|
(float)(1.0 / cartesian.x),
|
|
|
|
|
(float)(1.0 / cartesian.y),
|
|
|
|
|
(float)(1.0 / cartesian.z)
|
|
|
|
|
);
|
|
|
|
|
// Setup the voxel step based on what direction the ray is pointing
|
|
|
|
|
sf::Vector3<int> voxel_step(1, 1, 1);
|
|
|
|
|
voxel_step.x *= (cartesian.x > 0) - (cartesian.x < 0);
|
|
|
|
|
voxel_step.y *= (cartesian.y > 0) - (cartesian.y < 0);
|
|
|
|
|
voxel_step.z *= (cartesian.z > 0) - (cartesian.z < 0);
|
|
|
|
|
|
|
|
|
|
// Setup the voxel coords from the camera origin
|
|
|
|
|
voxel = sf::Vector3<int>(
|
|
|
|
|
(int)origin.x,
|
|
|
|
|
(int)origin.y,
|
|
|
|
|
(int)origin.z
|
|
|
|
|
(int) origin.x,
|
|
|
|
|
(int) origin.y,
|
|
|
|
|
(int) origin.z
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Delta T is the units a ray must travel along an axis in order to
|
|
|
|
|
// traverse an integer split
|
|
|
|
|
delta_t = sf::Vector3<float>(
|
|
|
|
|
fabsf((float) (1.0 / cartesian.x)),
|
|
|
|
|
fabsf((float) (1.0 / cartesian.y)),
|
|
|
|
|
fabsf((float) (1.0 / cartesian.z))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Intersection T is the collection of the next intersection points
|
|
|
|
|
// for all 3 axis XYZ.
|
|
|
|
|
|
|
|
|
|
// Setup the voxel step based on what direction the ray is pointing
|
|
|
|
|
sf::Vector3<int> voxel_step(1, 1, 1);
|
|
|
|
|
|
|
|
|
|
//if (direction.x <= 0.0f || direction.x >= 3.14f) {
|
|
|
|
|
// voxel_step.x *= -1;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
// Up down
|
|
|
|
|
//if (direction.y < 0.0f) {
|
|
|
|
|
// voxel_step.z *= -1;
|
|
|
|
|
//}
|
|
|
|
|
//if (direction.y > PI * 2 + PI / 2 || direction.y < -1 *PI * 2 + PI / 2) {
|
|
|
|
|
// voxel_step.x *= -1;
|
|
|
|
|
//}
|
|
|
|
|
// Left right
|
|
|
|
|
/*if (direction.z > 1.57) {
|
|
|
|
|
voxel_step.y *= -1;
|
|
|
|
|
voxel_step.x *= -1;
|
|
|
|
|
}*/
|
|
|
|
|
//if (direction.z <= 3.14f + 1.57f && direction.z > 0.0f + 1.57f) {
|
|
|
|
|
// voxel_step.z *= -1;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
voxel_step.x *= (cartesian.x > 0) - (cartesian.x < 0);
|
|
|
|
|
voxel_step.y *= (cartesian.y > 0) - (cartesian.y < 0);
|
|
|
|
|
voxel_step.z *= (cartesian.z > 0) - (cartesian.z < 0);
|
|
|
|
|
|
|
|
|
|
// Set the first intersection to be offset by the VOXEL camera position
|
|
|
|
|
// I think this is where the hangup is currently. It's taking the delta_t which is signed
|
|
|
|
|
// and multiplying it by the voxel_step which is also signed. On top of this. Computing the
|
|
|
|
|
// camera position by voxel coord is debug only so I need to do the math to account for the
|
|
|
|
|
// origin being anywhere inside a voxel
|
|
|
|
|
intersection_t = sf::Vector3<float>(
|
|
|
|
|
delta_t.x * voxel_step.x + voxel.x,
|
|
|
|
|
delta_t.y * voxel_step.y + voxel.y,
|
|
|
|
|
delta_t.z * voxel_step.z + voxel.z
|
|
|
|
|
delta_t.x + origin.x,
|
|
|
|
|
delta_t.y + origin.y,
|
|
|
|
|
delta_t.z + origin.z
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
int dist = 0;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
if(intersection_t.x < intersection_t.y) {
|
|
|
|
|
if(intersection_t.x < intersection_t.z) {
|
|
|
|
|
voxel.x = voxel.x + voxel_step.x;
|
|
|
|
|
if ((intersection_t.x) < (intersection_t.y)) {
|
|
|
|
|
if ((intersection_t.x) < (intersection_t.z)) {
|
|
|
|
|
|
|
|
|
|
voxel.x += voxel_step.x;
|
|
|
|
|
intersection_t.x = intersection_t.x + delta_t.x;
|
|
|
|
|
} else {
|
|
|
|
|
voxel.z = voxel.z + voxel_step.z;
|
|
|
|
|
intersection_t.z= intersection_t.z + delta_t.z;
|
|
|
|
|
|
|
|
|
|
voxel.z += voxel_step.z;
|
|
|
|
|
intersection_t.z = intersection_t.z + delta_t.z;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(intersection_t.y < intersection_t.z) {
|
|
|
|
|
voxel.y = voxel.y + voxel_step.y;
|
|
|
|
|
if ((intersection_t.y) < (intersection_t.z)) {
|
|
|
|
|
|
|
|
|
|
voxel.y += voxel_step.y;
|
|
|
|
|
intersection_t.y = intersection_t.y + delta_t.y;
|
|
|
|
|
} else {
|
|
|
|
|
voxel.z = voxel.z + voxel_step.z;
|
|
|
|
|
|
|
|
|
|
voxel.z += voxel_step.z;
|
|
|
|
|
intersection_t.z = intersection_t.z + delta_t.z;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the voxel went out of bounds
|
|
|
|
|
if (voxel.z >= dimensions.z){
|
|
|
|
|
if (voxel.z >= dimensions.z) {
|
|
|
|
|
return sf::Color(0, 0, 255, 50);
|
|
|
|
|
}
|
|
|
|
|
if (voxel.x >= dimensions.x){
|
|
|
|
|
if (voxel.x >= dimensions.x) {
|
|
|
|
|
return sf::Color(0, 0, 255, 100);
|
|
|
|
|
}
|
|
|
|
|
if (voxel.y >= dimensions.x){
|
|
|
|
|
if (voxel.y >= dimensions.x) {
|
|
|
|
|
return sf::Color(0, 0, 255, 150);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -140,10 +129,10 @@ sf::Color Ray::Cast(){
|
|
|
|
|
dist++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} while(dist < 200);
|
|
|
|
|
} while (dist < 200);
|
|
|
|
|
|
|
|
|
|
return sf::Color::Cyan;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|