things are casting now and sfml is rendering the way I want it to.
The voxel stepping is still all messed up, and it seems that there is some data corruption on the map that I'll need to dig into
This commit is contained in:
@@ -6,6 +6,15 @@ class Map {
|
||||
public:
|
||||
Map(sf::Vector3i dim) {
|
||||
list = new char[dim.x * dim.y * dim.z];
|
||||
for (int i = 0; i < dim.x * dim.y * dim.x; i++) {
|
||||
list[i] = 0;
|
||||
}
|
||||
|
||||
for (int x = 0; x < dim.x; x++) {
|
||||
for (int y = 0; y < dim.y; y++) {
|
||||
list[x + dim.x * (y + dim.z * 1)] = 1;
|
||||
}
|
||||
}
|
||||
dimensions = dim;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ public:
|
||||
|
||||
|
||||
sf::Color* CastRays(sf::Vector3<float> camera_direction, sf::Vector3<float> camera_position);
|
||||
|
||||
void moveCamera(sf::Vector2f v);
|
||||
private:
|
||||
|
||||
sf::Vector3<int> map_dimensions;
|
||||
15
src/Ray.cpp
15
src/Ray.cpp
@@ -47,10 +47,10 @@ sf::Color Ray::Cast(){
|
||||
// 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;
|
||||
}
|
||||
if (direction.y <= 0.0f || direction.y >= 3.14f) {
|
||||
//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;
|
||||
}
|
||||
if (direction.z <= 0.0f || direction.z >= 3.14f) {
|
||||
@@ -77,17 +77,16 @@ sf::Color Ray::Cast(){
|
||||
intersection_t.z = intersection_t.z + delta_t.z;
|
||||
}
|
||||
}
|
||||
|
||||
// If the voxel went out of bounds
|
||||
if (voxel.x > 49 || voxel.y > 49){
|
||||
if (voxel.z > dimensions.z || voxel.x > dimensions.x || voxel.y > dimensions.x){
|
||||
return sf::Color::Blue;;
|
||||
}
|
||||
else if (voxel.z > 49 || voxel.x < 0 || voxel.y < 0 || voxel.z < 0){
|
||||
else if ( 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){
|
||||
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;
|
||||
|
||||
@@ -19,7 +19,7 @@ RayCaster::RayCaster(
|
||||
this->map = map;
|
||||
|
||||
resolution = viewport_resolution;
|
||||
image = new sf::Color[resolution.x, resolution.y];
|
||||
image = new sf::Color[resolution.x * resolution.y];
|
||||
|
||||
|
||||
|
||||
@@ -46,10 +46,11 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
|
||||
|
||||
// A reference to the positive X axis as our base viewport point
|
||||
sf::Vector3f base_direction(1, 0, 0);
|
||||
|
||||
|
||||
//-resolution.y / 2
|
||||
// Start the loop at the bottom left, scan right and work up
|
||||
for (int x = 0; x < resolution.x / 2; x++) {
|
||||
for (int y = -resolution.y / 2; y < resolution.y / 2; y++) {
|
||||
for (int x = 0; x < resolution.x; x++) {
|
||||
for (int y = 0; y < resolution.y; y++) {
|
||||
|
||||
// The direction the final ray will point.
|
||||
// First take a reference to the base direction to setup the viewport
|
||||
@@ -60,7 +61,7 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
|
||||
|
||||
|
||||
sf::Vector3f ray_direction(
|
||||
camera_direction.x + 1.0f,
|
||||
camera_direction.x,
|
||||
camera_direction.y + (float)(y_increment_radians * y),
|
||||
camera_direction.z + (float)(x_increment_radians * x)
|
||||
);
|
||||
@@ -78,8 +79,18 @@ sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<
|
||||
Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray_direction);
|
||||
|
||||
// Cast it
|
||||
///image[x + 100, y + 100] = r.Cast();
|
||||
sf::Color c = r.Cast();
|
||||
if (c.a == 0)
|
||||
std::cout << "BLACK";
|
||||
image[x + resolution.x*y] = c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
void RayCaster::moveCamera(sf::Vector2f v) {
|
||||
camera_direction.y += v.x;
|
||||
camera_direction.z += v.y;
|
||||
}
|
||||
22
src/main.cpp
22
src/main.cpp
@@ -3,7 +3,8 @@
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include "util.hpp"
|
||||
#include "../build/RayCaster.h"
|
||||
#include "RayCaster.h"
|
||||
#include <Map.h>
|
||||
|
||||
const int WINDOW_X = 600;
|
||||
const int WINDOW_Y = 800;
|
||||
@@ -52,7 +53,7 @@ int main() {
|
||||
|
||||
// State values
|
||||
sf::Vector3i map_dim(100, 100, 100);
|
||||
sf::Vector2i view_res(200, 200);
|
||||
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);
|
||||
|
||||
@@ -72,6 +73,23 @@ int main() {
|
||||
// If the user tries to exit the application via the GUI
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the elapsed time from the start of the application
|
||||
|
||||
Reference in New Issue
Block a user