From 4a9d85ba1c84e660d0d8bba33bb0c989b46b337e Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Thu, 20 Apr 2023 02:21:21 +0100 Subject: [PATCH] Implement basic first person view rendering. --- firstperson.cpp | 24 ++++++++++++++++++++---- firstperson.h | 3 ++- view.cpp | 6 +++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/firstperson.cpp b/firstperson.cpp index 5af43a0..9373aeb 100644 --- a/firstperson.cpp +++ b/firstperson.cpp @@ -20,16 +20,32 @@ int firstperson_init(unsigned int _width, unsigned int _height) return 1; } -void firstperson_update(sf::RenderTarget* renderTarget) +void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera) { + const float columnWidth = (float)width/(float)camera->resolution; + float columnHeight = 0; renderTexture.clear(); - sf::CircleShape shape(50); - renderTexture.draw(shape); - renderTexture.display(); + for (unsigned int i = 0; i < camera->resolution; i++) { + float distance = camera->rays[i].distance; + if (distance > 0.f) columnHeight = 0.5f * (float)height/camera->rays[i].distance; + + float centeredHeight = (float)height/2.f - columnHeight/2.f; + float distanceScale = distance/10.f; + distanceScale = (distanceScale > 1.f)? 1.f : distanceScale; + float brightness = 255.f*(1.f-distanceScale); + + sf::RectangleShape rectangle(sf::Vector2f(columnWidth, columnHeight)); + rectangle.setPosition(sf::Vector2f(i*columnWidth, centeredHeight)); + rectangle.setFillColor(sf::Color(brightness, brightness, brightness)); + renderTexture.draw(rectangle); + } + + renderTexture.display(); sf::Sprite sprite(renderTexture.getTexture()); sprite.setPosition(renderTexturePosition); renderTarget->draw(sprite); + } void firstperson_setTexturePosition(float x, float y) diff --git a/firstperson.h b/firstperson.h index 11434ab..1e4e3ae 100644 --- a/firstperson.h +++ b/firstperson.h @@ -2,9 +2,10 @@ #define FIRSTPERSON_H #include +#include "camera.h" int firstperson_init(unsigned int width, unsigned int height); -void firstperson_update(sf::RenderTarget* renderTarget); +void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera); void firstperson_setTexturePosition(float x, float y); #endif diff --git a/view.cpp b/view.cpp index a4a7fcf..7cae58e 100644 --- a/view.cpp +++ b/view.cpp @@ -7,7 +7,7 @@ #include "minimap.h" #include "firstperson.h" -#define MINIMAP_SIZE 460 +#define MINIMAP_SIZE 720 #define VIEW_SIZE MINIMAP_SIZE*2 static int handleKeyCode(sf::Keyboard::Key key); @@ -21,7 +21,7 @@ static sf::Clock timer; int view_init() { printf("view_init()\n"); - if (!camera_init(&camera, sf::Vector2f(5.f/2.f, 5.f/2.f), 0.f, 100, 0.5f*PI)) return 0; + if (!camera_init(&camera, sf::Vector2f(10.f/2.f, 10.f/2.f), 0.f, 128, 0.5f*PI)) return 0; if (!minimap_init(MINIMAP_SIZE)) return 0; if (!firstperson_init(VIEW_SIZE, MINIMAP_SIZE)) return 0; @@ -53,7 +53,7 @@ int view_update() window.clear(); minimap_update(&window, &camera); - firstperson_update(&window); + firstperson_update(&window, &camera); window.display(); return 1;