Compare commits

..

No commits in common. "4a9d85ba1c84e660d0d8bba33bb0c989b46b337e" and "787b25239eba37a6984090b6b0b5e84649a10962" have entirely different histories.

5 changed files with 27 additions and 40 deletions

View File

@ -1,5 +1,4 @@
#include "camera.h" #include "camera.h"
#include <stdio.h>
#include "maths.h" #include "maths.h"
#include "level.h" #include "level.h"
@ -55,12 +54,22 @@ void camera_destroy(Camera *camera)
static void castRays(Camera* camera) static void castRays(Camera* camera)
{ {
float rayDirection = 0;
float rayDirectionStep = camera->fov / (float)camera->resolution; float rayDirectionStep = camera->fov / (float)camera->resolution;
float rayDirection = camera->direction - camera->fov/2.f + rayDirectionStep/2.f; bool isOddResolution = (camera->resolution % 2);
float rayDirectionOffset = isOddResolution? 0 : rayDirectionStep / 2.f;
for (unsigned int i = 0; i < camera->resolution; i++) { for (unsigned int i = 0; i < camera->resolution; i++) {
if (isOddResolution && i == 0)
rayDirection = camera->direction;
else if (i % 2)
rayDirection = camera->direction - rayDirectionOffset;
else
rayDirection = camera->direction + rayDirectionOffset;
camera->rays[i].direction = rayDirection; camera->rays[i].direction = rayDirection;
camera->rays[i].distance = level_rayCastDistance(camera->pos, rayDirection); camera->rays[i].distance = level_rayCastDistance(camera->pos, rayDirection);
rayDirection += rayDirectionStep; if ((i + isOddResolution) % 2) rayDirectionOffset += rayDirectionStep;
} }
} }

View File

@ -20,32 +20,16 @@ int firstperson_init(unsigned int _width, unsigned int _height)
return 1; return 1;
} }
void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera) void firstperson_update(sf::RenderTarget* renderTarget)
{ {
const float columnWidth = (float)width/(float)camera->resolution;
float columnHeight = 0;
renderTexture.clear(); renderTexture.clear();
sf::CircleShape shape(50);
for (unsigned int i = 0; i < camera->resolution; i++) { renderTexture.draw(shape);
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(); renderTexture.display();
sf::Sprite sprite(renderTexture.getTexture()); sf::Sprite sprite(renderTexture.getTexture());
sprite.setPosition(renderTexturePosition); sprite.setPosition(renderTexturePosition);
renderTarget->draw(sprite); renderTarget->draw(sprite);
} }
void firstperson_setTexturePosition(float x, float y) void firstperson_setTexturePosition(float x, float y)

View File

@ -2,10 +2,9 @@
#define FIRSTPERSON_H #define FIRSTPERSON_H
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "camera.h"
int firstperson_init(unsigned int width, unsigned int height); int firstperson_init(unsigned int width, unsigned int height);
void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera); void firstperson_update(sf::RenderTarget* renderTarget);
void firstperson_setTexturePosition(float x, float y); void firstperson_setTexturePosition(float x, float y);
#endif #endif

View File

@ -2,23 +2,18 @@
#include "maths.h" #include "maths.h"
#define WIDTH 10 #define WIDTH 5
#define HEIGHT 10 #define HEIGHT 5
static float castRay(sf::Vector2f point, float direction); static float castRay(sf::Vector2f point, float direction);
static void getGridIndex(sf::Vector2f point, int* x, int* y); static void getGridIndex(sf::Vector2f point, int* x, int* y);
static unsigned int level[WIDTH * HEIGHT] = { static unsigned int level[WIDTH * HEIGHT] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
}; };
int level_init() int level_init()

View File

@ -7,7 +7,7 @@
#include "minimap.h" #include "minimap.h"
#include "firstperson.h" #include "firstperson.h"
#define MINIMAP_SIZE 720 #define MINIMAP_SIZE 460
#define VIEW_SIZE MINIMAP_SIZE*2 #define VIEW_SIZE MINIMAP_SIZE*2
static int handleKeyCode(sf::Keyboard::Key key); static int handleKeyCode(sf::Keyboard::Key key);
@ -21,7 +21,7 @@ static sf::Clock timer;
int view_init() int view_init()
{ {
printf("view_init()\n"); printf("view_init()\n");
if (!camera_init(&camera, sf::Vector2f(10.f/2.f, 10.f/2.f), 0.f, 128, 0.5f*PI)) return 0; if (!camera_init(&camera, sf::Vector2f(5.f/2.f, 5.f/2.f), 0.f, 100, 0.5f*PI)) return 0;
if (!minimap_init(MINIMAP_SIZE)) return 0; if (!minimap_init(MINIMAP_SIZE)) return 0;
if (!firstperson_init(VIEW_SIZE, MINIMAP_SIZE)) return 0; if (!firstperson_init(VIEW_SIZE, MINIMAP_SIZE)) return 0;
@ -53,7 +53,7 @@ int view_update()
window.clear(); window.clear();
minimap_update(&window, &camera); minimap_update(&window, &camera);
firstperson_update(&window, &camera); firstperson_update(&window);
window.display(); window.display();
return 1; return 1;