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
This commit is contained in:
68
src/Ray.cpp
68
src/Ray.cpp
@@ -15,6 +15,7 @@ Ray::Ray(
|
||||
this->map = map;
|
||||
origin = camera_position;
|
||||
direction = ray_direction;
|
||||
|
||||
dimensions = map->getDimensions();
|
||||
}
|
||||
|
||||
@@ -50,12 +51,21 @@ sf::Color Ray::Cast(){
|
||||
//if (direction.x <= 0.0f || direction.x >= 3.14f) {
|
||||
// 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) {
|
||||
voxel_step.z *= -1;
|
||||
// Left right
|
||||
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;
|
||||
|
||||
@@ -77,23 +87,51 @@ sf::Color Ray::Cast(){
|
||||
intersection_t.z = intersection_t.z + delta_t.z;
|
||||
}
|
||||
}
|
||||
|
||||
// If the voxel went out of bounds
|
||||
if (voxel.z > dimensions.z || voxel.x > dimensions.x || voxel.y > dimensions.x){
|
||||
return sf::Color::Blue;;
|
||||
if (voxel.z >= dimensions.z){
|
||||
return sf::Color(0, 0, 255, 50);
|
||||
}
|
||||
else if ( voxel.x < 0 || voxel.y < 0 || voxel.z < 0){
|
||||
return sf::Color::Green;
|
||||
if (voxel.x >= dimensions.x){
|
||||
return sf::Color(0, 0, 255, 100);
|
||||
}
|
||||
if (voxel.y >= dimensions.x){
|
||||
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
|
||||
// Registers hit on non-zero
|
||||
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;
|
||||
}
|
||||
else {
|
||||
dist++;
|
||||
|
||||
switch (map->list[voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z)]) {
|
||||
case 1:
|
||||
return sf::Color::Red;
|
||||
case 2:
|
||||
return sf::Color::Magenta;
|
||||
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);
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
|
||||
sf::Vector3f base_direction(1, 0, 0);
|
||||
|
||||
//-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 y = 0; y < resolution.y; y++) {
|
||||
|
||||
|
||||
70
src/main.cpp
70
src/main.cpp
@@ -6,8 +6,8 @@
|
||||
#include "RayCaster.h"
|
||||
#include <Map.h>
|
||||
|
||||
const int WINDOW_X = 600;
|
||||
const int WINDOW_Y = 800;
|
||||
const int WINDOW_X = 400;
|
||||
const int WINDOW_Y = 400;
|
||||
|
||||
|
||||
float elap_time(){
|
||||
@@ -55,7 +55,7 @@ int main() {
|
||||
sf::Vector3i map_dim(100, 100, 100);
|
||||
sf::Vector2i view_res(WINDOW_X, WINDOW_Y);
|
||||
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);
|
||||
RayCaster ray_caster(map, map_dim, view_res);
|
||||
@@ -74,21 +74,51 @@ int main() {
|
||||
if (event.type == sf::Event::Closed)
|
||||
window.close();
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||
cam_dir.z -= 0.1f;
|
||||
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
||||
cam_dir.z += 0.1f;
|
||||
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
||||
cam_dir.y += 0.1f;
|
||||
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
||||
cam_dir.y -= 0.1f;
|
||||
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
|
||||
if (event.type == sf::Event::KeyPressed) {
|
||||
|
||||
// CAMERA DIRECTION
|
||||
if (event.key.code == sf::Keyboard::Left) {
|
||||
cam_dir.z -= 0.1f;
|
||||
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
|
||||
}
|
||||
if (event.key.code == sf::Keyboard::Right) {
|
||||
cam_dir.z += 0.1f;
|
||||
std::cout << "X:" << cam_dir.x << " Y:" << cam_dir.y << " Z:" << cam_dir.z << std::endl;
|
||||
}
|
||||
if (event.key.code == sf::Keyboard::Down) {
|
||||
cam_dir.y += 0.1f;
|
||||
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)
|
||||
// cull the time to a more acceptable value. So instead of jumping large
|
||||
// amounts when lagging, the app only jumps in set increments
|
||||
if (delta_time > 0.05f)
|
||||
delta_time = 0.05f;
|
||||
if (delta_time > 0.2f)
|
||||
delta_time = 0.2f;
|
||||
|
||||
// Add the frame time to the accumulator, a running total of time we
|
||||
// need to account for in the application
|
||||
|
||||
Reference in New Issue
Block a user