Basic render loop is taking shape. Digging back into the

raycasting will be slightly annoying
master
MitchellHansen 8 years ago
parent 37ef472f61
commit 2b46bef923

@ -3,11 +3,10 @@
#include <Ray.h> #include <Ray.h>
RayCaster::RayCaster(Map *map, RayCaster::RayCaster(
sf::Vector3<int> map_dimensions, Map *map,
sf::Vector2<int> viewport_resolution, sf::Vector3<int> map_dimensions,
sf::Vector3<float> camera_direction, sf::Vector2<int> viewport_resolution ) {
sf::Vector3<float> camera_position) {
// Override values // Override values
//this.map_dimensions = new Vector3<int> (50, 50, 50); //this.map_dimensions = new Vector3<int> (50, 50, 50);
@ -17,16 +16,12 @@ RayCaster::RayCaster(Map *map,
this->map_dimensions = map_dimensions; this->map_dimensions = map_dimensions;
map = map; this->map = map;
resolution = viewport_resolution; resolution = viewport_resolution;
image = new sf::Uint8[resolution.x, resolution.y]; image = new sf::Color[resolution.x, resolution.y];
this->camera_direction = camera_direction;
camera_direction_cartesian = Normalize(SphereToCart(camera_direction));
this->camera_position = camera_position;
} }
@ -36,7 +31,13 @@ RayCaster::~RayCaster() {
sf::Uint8* RayCaster::Cast() { sf::Color* RayCaster::CastRays(sf::Vector3<float> camera_direction, sf::Vector3<float> camera_position) {
// Setup the camera for this cast
this->camera_direction = camera_direction;
camera_direction_cartesian = Normalize(SphereToCart(camera_direction));
this->camera_position = camera_position;
// 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(40.0 / resolution.y);

@ -7,13 +7,11 @@ class RayCaster {
public: public:
RayCaster(Map *map, RayCaster(Map *map,
sf::Vector3<int> map_dimensions, sf::Vector3<int> map_dimensions,
sf::Vector2<int> viewport_resolution, sf::Vector2<int> viewport_resolution);
sf::Vector3<float> camera_direction,
sf::Vector3<float> camera_position);
~RayCaster(); ~RayCaster();
sf::Uint8* Cast(); sf::Color* CastRays(sf::Vector3<float> camera_direction, sf::Vector3<float> camera_position);
private: private:
@ -24,7 +22,7 @@ private:
sf::Vector2<int> resolution; sf::Vector2<int> resolution;
// The pixel array, maybe do RBGA? Are there even 4 byte data types? // The pixel array, maybe do RBGA? Are there even 4 byte data types?
sf::Uint8 *image; sf::Color *image;
// The direction of the camera in POLAR coordinates // The direction of the camera in POLAR coordinates
sf::Vector3<float> camera_direction; sf::Vector3<float> camera_direction;

@ -6,14 +6,15 @@ class Map {
public: public:
Map(sf::Vector3i dim) { Map(sf::Vector3i dim) {
list = new char[dim.x * dim.y * dim.z]; list = new char[dim.x * dim.y * dim.z];
dimensions = dim;
} }
~Map() { ~Map() {
} }
sf::Vector3<int> getDimensions(); sf::Vector3i getDimensions();
char *list; char *list;
sf::Vector3i dimensions;
protected: protected:

@ -3,6 +3,6 @@
#include <iostream> #include <iostream>
#include <SFML/System/Vector3.hpp> #include <SFML/System/Vector3.hpp>
sf::Vector3<int> Map::getDimensions() { sf::Vector3i Map::getDimensions() {
throw std::logic_error("The method or operation is not implemented."); return dimensions;
} }

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <chrono> #include <chrono>
#include "util.hpp" #include "util.hpp"
#include "../build/RayCaster.h"
const int WINDOW_X = 600; const int WINDOW_X = 600;
const int WINDOW_Y = 800; const int WINDOW_Y = 800;
@ -22,12 +23,10 @@ float elap_time(){
return elapsed_time.count(); return elapsed_time.count();
} }
int main() { sf::Sprite window_sprite;
sf::Texture window_texture;
sf::Uint8 c;
std::cout << sizeof(c);
int main() {
// Initialize the render window // Initialize the render window
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML");
@ -45,6 +44,25 @@ int main() {
fps_counter fps; fps_counter fps;
// ============================= RAYCASTER SETUP ==================================
// Setup the sprite and texture
window_texture.create(WINDOW_X, WINDOW_Y);
window_sprite.setPosition(0, 0);
// State values
sf::Vector3i map_dim(100, 100, 100);
sf::Vector2i view_res(200, 200);
sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f);
sf::Vector3f cam_pos(10, 10, 10);
Map* map = new Map(map_dim);
RayCaster ray_caster(map, map_dim, view_res);
// ===============================================================================
while (window.isOpen()) { while (window.isOpen()) {
// Poll for events from the user // Poll for events from the user
@ -85,10 +103,21 @@ int main() {
// Update(step_size); // Update(step_size);
} }
// Rendering code goes here
window.clear(sf::Color::Black); window.clear(sf::Color::Black);
// Cast the rays and get the image
sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos);
/*for (int i = 0; i < img_size; i++) {
pixel_colors[i] = sf::Color::Green;
}*/
auto out = (sf::Uint8*)pixel_colors;
window_texture.update(out);
window_sprite.setTexture(window_texture);
window.draw(window_sprite);
// Give the frame counter the frame time and draw the average frame time // Give the frame counter the frame time and draw the average frame time
fps.frame(delta_time); fps.frame(delta_time);
fps.draw(&window); fps.draw(&window);

Loading…
Cancel
Save