Don't know what is in this one
This commit is contained in:
2
src/Map.cpp
Normal file
2
src/Map.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
104
src/Ray.cpp
Normal file
104
src/Ray.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <iostream>
|
||||
#include "Map.h"
|
||||
|
||||
Ray::Ray(
|
||||
Map *m,
|
||||
sf::Vector2<int> resolution,
|
||||
sf::Vector2<int> pixel,
|
||||
sf::Vector3<float> camera_position,
|
||||
sf::Vector3<float> ray_direction){
|
||||
|
||||
this.m = m;
|
||||
origin = camera_position;
|
||||
direction = ray_direction;
|
||||
dimensions = m->getDimensions();
|
||||
}
|
||||
|
||||
sf::Color Ray::Cast(){
|
||||
|
||||
// Get the cartesian direction for computing slope
|
||||
sf::Vector3<float> cartesian = direction.toCartesian();
|
||||
|
||||
// Compute the slopes
|
||||
delta_t = sf::Vector3<float>(
|
||||
(float)(1.0 / cartesian.x),
|
||||
(float)(1.0 / cartesian.y),
|
||||
(float)(1.0 / cartesian.z)
|
||||
);
|
||||
|
||||
// Set the first intersection to be offset by the VOXEL camera position
|
||||
intersection_t = sf::Vector3<float>(
|
||||
delta_t.x + (int)origin.x,
|
||||
delta_t.y + (int)origin.y,
|
||||
delta_t.z + (int)origin.z
|
||||
);
|
||||
|
||||
// Setup the voxel coords from the camera origin
|
||||
voxel = sf::Vector3<float>(
|
||||
(int)origin.x,
|
||||
(int)origin.y,
|
||||
(int)origin.z
|
||||
);
|
||||
|
||||
|
||||
// Setup the voxel step based on what direction the ray is pointing
|
||||
sf::Vector3<int> voxel_step(1, 1, 1);
|
||||
|
||||
if (direction.x <= 0f || direction.x >= 3.14f) {
|
||||
voxel_step.x *= -1;
|
||||
}
|
||||
if (directoin.y <= 0f || direction.y >= 3.14f) {
|
||||
voxel_step.y *= -1;
|
||||
}
|
||||
if (direction.z <= 0f || direction.z >= 3.14f) {
|
||||
voxel_step.z *= -1;
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
if(intersection_t.y < intersection_t.z) {
|
||||
voxel.y = voxel.y + voxel_step.y;
|
||||
intersection_t.y = intersection_t.y + delta_t.y;
|
||||
} else {
|
||||
voxel.z = voxel.z + voxel_step.z;
|
||||
intersection_t.z = intersection_t.z + delta_t.z;
|
||||
}
|
||||
}
|
||||
|
||||
// If the voxel went out of bounds
|
||||
if (voxel.x > 49 || voxel.y > 49){
|
||||
return sf::Color::Blue;;
|
||||
}
|
||||
else if (voxel.z > 49 || voxel.x < 0 || voxel.y < 0 || voxel.z < 0){
|
||||
return sf::Color:Green;
|
||||
}
|
||||
// If we found a voxel
|
||||
// Registers hit on non-zero
|
||||
else if (map.list[voxel.x, voxel.y, voxel.z] != 0){
|
||||
|
||||
//TODO: Switch that assigns color on voxel data
|
||||
return sf::Color:Red;
|
||||
}
|
||||
else {
|
||||
dist++;
|
||||
}
|
||||
|
||||
} while(dist < 200);
|
||||
|
||||
return sf::Color::Grey;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
27
src/main.cpp
27
src/main.cpp
@@ -2,36 +2,11 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include "util.hpp"
|
||||
|
||||
const int WINDOW_X = 600;
|
||||
const int WINDOW_Y = 800;
|
||||
|
||||
struct fps_counter {
|
||||
public:
|
||||
fps_counter(){
|
||||
if(!f.loadFromFile("../assets/fonts/Arial.ttf")){
|
||||
std::cout << "couldn't find the fallback Arial font "
|
||||
"in ../assets/fonts/" << std::endl;
|
||||
} else {
|
||||
t.setFont(f);
|
||||
}
|
||||
}
|
||||
void frame(double delta_time){
|
||||
frame_count++;
|
||||
fps_average += (delta_time - fps_average) / frame_count;
|
||||
}
|
||||
void draw(sf::RenderWindow *r){
|
||||
t.setString(std::to_string(fps_average));
|
||||
r->draw(t);
|
||||
}
|
||||
|
||||
private:
|
||||
sf::Font f;
|
||||
sf::Text t;
|
||||
int frame_count = 0;
|
||||
double fps_average = 0;
|
||||
};
|
||||
|
||||
float elap_time(){
|
||||
static std::chrono::time_point<std::chrono::system_clock> start;
|
||||
static bool started = false;
|
||||
|
||||
Reference in New Issue
Block a user