So it's rendering pretty much perfectly in the XYZ+ range but things break

down when I start hitting negative values. I have a feeling this is going
to be a lot of trial and error. Oh well
master
MitchellHansen 8 years ago
parent 0b8cd9a194
commit f1c84c85b5

@ -10,11 +10,45 @@ public:
list[i] = 0; list[i] = 0;
} }
for (int x = 0; x < dim.x; x++) { for (int i = 50; i < 52; i++) {
for (int y = 0; y < dim.y; y++) { list[55 + dim.x * (55 + dim.z * i)] = 1;
}
for (int x = 0; x < dim.x; x += 2) {
for (int y = 0; y < dim.y; y += 2) {
list[x + dim.x * (y + dim.z * 1)] = 1; list[x + dim.x * (y + dim.z * 1)] = 1;
} }
} }
for (int x = 0; x < dim.x; x += 2) {
for (int y = 0; y < dim.y; y += 2) {
list[x + dim.x * (y + dim.z * 99)] = 2;
}
}
for (int x = 0; x < dim.x; x += 2) {
for (int z = 0; z < dim.z; z += 2) {
list[x + dim.x * (99 + dim.z * z)] = 3;
}
}
for (int x = 0; x < dim.x; x += 2) {
for (int z = 0; z < dim.z; z += 2) {
list[x + dim.x * (1 + dim.z * z)] = 4;
}
}
for (int y = 0; y < dim.y; y += 2) {
for (int z = 0; z < dim.z; z += 2) {
list[99 + dim.x * (y + dim.z * z)] = 5;
}
}
for (int y = 0; y < dim.y; y += 2) {
for (int z = 0; z < dim.z; z += 2) {
list[1 + dim.x * (y + dim.z * z)] = 6;
}
}
dimensions = dim; dimensions = dim;
} }

@ -15,6 +15,7 @@ Ray::Ray(
this->map = map; this->map = map;
origin = camera_position; origin = camera_position;
direction = ray_direction; direction = ray_direction;
dimensions = map->getDimensions(); dimensions = map->getDimensions();
} }
@ -50,12 +51,21 @@ sf::Color Ray::Cast(){
//if (direction.x <= 0.0f || direction.x >= 3.14f) { //if (direction.x <= 0.0f || direction.x >= 3.14f) {
// voxel_step.x *= -1; // voxel_step.x *= -1;
//} //}
if (direction.y > 0.0f || direction.y < PI/2) {
voxel_step.y *= -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;
} }
if (direction.z <= 0.0f || direction.z >= 3.14f) { // Left right
voxel_step.z *= -1; if (direction.z > 1.57) {
//voxel_step.z *= -1;
} }
//if (direction.z <= 3.14f + 1.57f && direction.z > 0.0f + 1.57f) {
// voxel_step.z *= -1;
//}
int dist = 0; int dist = 0;
@ -77,23 +87,51 @@ sf::Color Ray::Cast(){
intersection_t.z = intersection_t.z + delta_t.z; intersection_t.z = intersection_t.z + delta_t.z;
} }
} }
// If the voxel went out of bounds // If the voxel went out of bounds
if (voxel.z > dimensions.z || voxel.x > dimensions.x || voxel.y > dimensions.x){ if (voxel.z >= dimensions.z){
return sf::Color::Blue;; return sf::Color(0, 0, 255, 50);
}
if (voxel.x >= dimensions.x){
return sf::Color(0, 0, 255, 100);
} }
else if ( voxel.x < 0 || voxel.y < 0 || voxel.z < 0){ if (voxel.y >= dimensions.x){
return sf::Color::Green; return sf::Color(0, 0, 255, 150);
}
if (voxel.x < 0) {
return sf::Color(0, 255, 0, 150);
}
if (voxel.y < 0) {
return sf::Color(0, 255, 0, 100);
}
if (voxel.z < 0) {
return sf::Color(0, 255, 0, 50);
} }
// If we found a voxel // If we found a voxel
// Registers hit on non-zero // Registers hit on non-zero
else if (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)] != 0){
switch (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)]) {
//TODO: Switch that assigns color on voxel data case 1:
return sf::Color::Red; return sf::Color::Red;
} case 2:
else { return sf::Color::Magenta;
dist++; case 3:
return sf::Color::Yellow;
case 4:
return sf::Color(40, 230, 96, 200);
case 5:
return sf::Color(80, 120, 96, 100);
case 6:
return sf::Color(150, 80, 220, 200);
} }
//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); } while(dist < 200);

@ -48,7 +48,7 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
sf::Vector3f base_direction(1, 0, 0); sf::Vector3f base_direction(1, 0, 0);
//-resolution.y / 2 //-resolution.y / 2
// Start the loop at the bottom left, scan right and work up // Start the loop at the top left, scan right and work down
for (int x = 0; x < resolution.x; x++) { for (int x = 0; x < resolution.x; x++) {
for (int y = 0; y < resolution.y; y++) { for (int y = 0; y < resolution.y; y++) {

@ -6,8 +6,8 @@
#include "RayCaster.h" #include "RayCaster.h"
#include <Map.h> #include <Map.h>
const int WINDOW_X = 600; const int WINDOW_X = 400;
const int WINDOW_Y = 800; const int WINDOW_Y = 400;
float elap_time(){ float elap_time(){
@ -55,7 +55,7 @@ int main() {
sf::Vector3i map_dim(100, 100, 100); sf::Vector3i map_dim(100, 100, 100);
sf::Vector2i view_res(WINDOW_X, WINDOW_Y); sf::Vector2i view_res(WINDOW_X, WINDOW_Y);
sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f); sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f);
sf::Vector3f cam_pos(10, 10, 10); sf::Vector3f cam_pos(50, 50, 50);
Map* map = new Map(map_dim); Map* map = new Map(map_dim);
RayCaster ray_caster(map, map_dim, view_res); RayCaster ray_caster(map, map_dim, view_res);
@ -74,21 +74,51 @@ int main() {
if (event.type == sf::Event::Closed) if (event.type == sf::Event::Closed)
window.close(); window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { if (event.type == sf::Event::KeyPressed) {
cam_dir.z -= 0.1f;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl; // CAMERA DIRECTION
} if (event.key.code == sf::Keyboard::Left) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { cam_dir.z -= 0.1f;
cam_dir.z += 0.1f; std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl; }
} if (event.key.code == sf::Keyboard::Right) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { cam_dir.z += 0.1f;
cam_dir.y += 0.1f; std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl; }
} if (event.key.code == sf::Keyboard::Down) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { cam_dir.y += 0.1f;
cam_dir.y -= 0.1f; std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl; }
if (event.key.code == sf::Keyboard::Up) {
cam_dir.y -= 0.1f;
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
}
// CAMERA POSITION
if (event.key.code == sf::Keyboard::Q) {
cam_pos.z -= 1;
std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
}
if (event.key.code == sf::Keyboard::E) {
cam_pos.z += 1;
std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
}
if (event.key.code == sf::Keyboard::W) {
cam_pos.y += 1;
std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
}
if (event.key.code == sf::Keyboard::S) {
cam_pos.y -= 1;
std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
}
if (event.key.code == sf::Keyboard::A) {
cam_pos.x += 1;
std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
}
if (event.key.code == sf::Keyboard::D) {
cam_pos.x -= 1;
std::cout << "X:" << cam_pos.x << " Y:" << cam_pos.y << " Z:" << cam_pos.z << std::endl;
}
} }
} }
@ -104,8 +134,8 @@ int main() {
// If the time between the last frame and now was too large (lag) // If the time between the last frame and now was too large (lag)
// cull the time to a more acceptable value. So instead of jumping large // cull the time to a more acceptable value. So instead of jumping large
// amounts when lagging, the app only jumps in set increments // amounts when lagging, the app only jumps in set increments
if (delta_time > 0.05f) if (delta_time > 0.2f)
delta_time = 0.05f; delta_time = 0.2f;
// Add the frame time to the accumulator, a running total of time we // Add the frame time to the accumulator, a running total of time we
// need to account for in the application // need to account for in the application

Loading…
Cancel
Save