Alright, that little change fixed some of the negative coord problems, still have some really weird warping though.

This commit is contained in:
2016-08-01 20:29:11 -07:00
parent eb889f9937
commit c3be6e2240
2 changed files with 125 additions and 136 deletions

View File

@@ -1,4 +1,5 @@
#pragma once #pragma once
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <iostream> #include <iostream>
#include "Map.h" #include "Map.h"
@@ -6,144 +7,132 @@
#include "util.hpp" #include "util.hpp"
Ray::Ray( Ray::Ray(
Map *map, Map *map,
sf::Vector2<int> resolution, sf::Vector2<int> resolution,
sf::Vector2<int> pixel, sf::Vector2<int> pixel,
sf::Vector3<float> camera_position, sf::Vector3<float> camera_position,
sf::Vector3<float> ray_direction){ sf::Vector3<float> ray_direction) {
this->map = map; this->map = map;
origin = camera_position; origin = camera_position;
direction = ray_direction; direction = ray_direction;
dimensions = map->getDimensions(); 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); sf::Vector3<float> cartesian = SphereToCart(direction);
// Compute the slopes // Setup the voxel step based on what direction the ray is pointing
delta_t = sf::Vector3<float>( sf::Vector3<int> voxel_step(1, 1, 1);
(float)(1.0 / cartesian.x), voxel_step.x *= (cartesian.x > 0) - (cartesian.x < 0);
(float)(1.0 / cartesian.y), voxel_step.y *= (cartesian.y > 0) - (cartesian.y < 0);
(float)(1.0 / cartesian.z) voxel_step.z *= (cartesian.z > 0) - (cartesian.z < 0);
);
// Setup the voxel coords from the camera origin // Setup the voxel coords from the camera origin
voxel = sf::Vector3<int>( voxel = sf::Vector3<int>(
(int)origin.x, (int) origin.x,
(int)origin.y, (int) origin.y,
(int)origin.z (int) origin.z
); );
// Delta T is the units a ray must travel along an axis in order to
// traverse an integer split
// Setup the voxel step based on what direction the ray is pointing delta_t = sf::Vector3<float>(
sf::Vector3<int> voxel_step(1, 1, 1); fabsf((float) (1.0 / cartesian.x)),
fabsf((float) (1.0 / cartesian.y)),
//if (direction.x <= 0.0f || direction.x >= 3.14f) { fabsf((float) (1.0 / cartesian.z))
// voxel_step.x *= -1; );
//}
// Intersection T is the collection of the next intersection points
// Up down // for all 3 axis XYZ.
//if (direction.y < 0.0f) {
// voxel_step.z *= -1; // 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
//if (direction.y > PI * 2 + PI / 2 || direction.y < -1 *PI * 2 + PI / 2) { // camera position by voxel coord is debug only so I need to do the math to account for the
// voxel_step.x *= -1; // origin being anywhere inside a voxel
//} intersection_t = sf::Vector3<float>(
// Left right delta_t.x + origin.x,
/*if (direction.z > 1.57) { delta_t.y + origin.y,
voxel_step.y *= -1; delta_t.z + origin.z
voxel_step.x *= -1; );
}*/
//if (direction.z <= 3.14f + 1.57f && direction.z > 0.0f + 1.57f) { int dist = 0;
// voxel_step.z *= -1;
//} do {
if ((intersection_t.x) < (intersection_t.y)) {
voxel_step.x *= (cartesian.x > 0) - (cartesian.x < 0); if ((intersection_t.x) < (intersection_t.z)) {
voxel_step.y *= (cartesian.y > 0) - (cartesian.y < 0);
voxel_step.z *= (cartesian.z > 0) - (cartesian.z < 0); voxel.x += voxel_step.x;
intersection_t.x = intersection_t.x + delta_t.x;
// Set the first intersection to be offset by the VOXEL camera position } else {
intersection_t = sf::Vector3<float>(
delta_t.x * voxel_step.x + voxel.x, voxel.z += voxel_step.z;
delta_t.y * voxel_step.y + voxel.y, intersection_t.z = intersection_t.z + delta_t.z;
delta_t.z * voxel_step.z + voxel.z }
); } else {
if ((intersection_t.y) < (intersection_t.z)) {
int dist = 0;
voxel.y += voxel_step.y;
do { intersection_t.y = intersection_t.y + delta_t.y;
if(intersection_t.x < intersection_t.y) { } else {
if(intersection_t.x < intersection_t.z) {
voxel.x = voxel.x + voxel_step.x; voxel.z += voxel_step.z;
intersection_t.x = intersection_t.x + delta_t.x; intersection_t.z = intersection_t.z + delta_t.z;
} else { }
voxel.z = voxel.z + voxel_step.z; }
intersection_t.z= intersection_t.z + delta_t.z;
} // If the voxel went out of bounds
} else { if (voxel.z >= dimensions.z) {
if(intersection_t.y < intersection_t.z) { return sf::Color(0, 0, 255, 50);
voxel.y = voxel.y + voxel_step.y; }
intersection_t.y = intersection_t.y + delta_t.y; if (voxel.x >= dimensions.x) {
} else { return sf::Color(0, 0, 255, 100);
voxel.z = voxel.z + voxel_step.z; }
intersection_t.z = intersection_t.z + delta_t.z; if (voxel.y >= dimensions.x) {
} return sf::Color(0, 0, 255, 150);
} }
// If the voxel went out of bounds if (voxel.x < 0) {
if (voxel.z >= dimensions.z){ return sf::Color(0, 255, 0, 150);
return sf::Color(0, 0, 255, 50); }
} if (voxel.y < 0) {
if (voxel.x >= dimensions.x){ return sf::Color(0, 255, 0, 100);
return sf::Color(0, 0, 255, 100); }
} if (voxel.z < 0) {
if (voxel.y >= dimensions.x){ return sf::Color(0, 255, 0, 50);
return sf::Color(0, 0, 255, 150); }
} // If we found a voxel
// Registers hit on non-zero
if (voxel.x < 0) {
return sf::Color(0, 255, 0, 150); switch (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)]) {
} case 1:
if (voxel.y < 0) { return sf::Color::Red;
return sf::Color(0, 255, 0, 100); case 2:
} return sf::Color::Magenta;
if (voxel.z < 0) { case 3:
return sf::Color(0, 255, 0, 50); return sf::Color::Yellow;
} case 4:
// If we found a voxel return sf::Color(40, 230, 96, 200);
// Registers hit on non-zero case 5:
return sf::Color(80, 120, 96, 100);
switch (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)]) { case 6:
case 1: return sf::Color(150, 80, 220, 200);
return sf::Color::Red; }
case 2: //else if (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)] != 0){
return sf::Color::Magenta; //
case 3: // //TODO: Switch that assigns color on voxel data
return sf::Color::Yellow; // return sf::Color::Red;
case 4: //}
return sf::Color(40, 230, 96, 200); dist++;
case 5:
return sf::Color(80, 120, 96, 100);
case 6: } while (dist < 200);
return sf::Color(150, 80, 220, 200);
} return sf::Color::Cyan;
//else if (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)] != 0){ }
//
// //TODO: Switch that assigns color on voxel data
// return sf::Color::Red;
//}
dist++;
} while(dist < 200);
return sf::Color::Cyan;
}

View File

@@ -40,8 +40,8 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
// The radian increment each ray is spaced from one another // The radian increment each ray is spaced from one another
double y_increment_radians = DegreesToRadians(40.0 / resolution.y); double y_increment_radians = DegreesToRadians(60.0 / resolution.y);
double x_increment_radians = DegreesToRadians(50.0 / resolution.x); double x_increment_radians = DegreesToRadians(80.0 / resolution.x);
// A reference to the positive X axis as our base viewport point // A reference to the positive X axis as our base viewport point