Compare commits

...

8 Commits

Author SHA1 Message Date
sheldonmlee 5a51a375a4 Initialize camera resolution from VIEW_SIZE constant 2024-01-15 18:33:22 +08:00
sheldonmlee 22cd090f92 Implement basic shading and colors.
CameraRay struct now stores information about tile intersection.
2023-05-01 00:10:01 +01:00
sheldonmlee 67c41106ff Fix fisheye effect with cosine 2023-04-30 22:47:45 +01:00
sheldonmlee 4a9d85ba1c Implement basic first person view rendering. 2023-04-20 02:21:21 +01:00
sheldonmlee d186b948a7 Update and make level bigger 2023-04-20 02:21:11 +01:00
sheldonmlee 900504971b Make ray generation sane 2023-04-20 02:19:54 +01:00
sheldonmlee 787b25239e Add first person view. 2023-04-20 00:31:41 +01:00
sheldonmlee 2c06859473 Refactor storing of ray-casted distances.
The ray-casting is done when camera_update() is called, and distances
are stored in the Camera struct.
2023-04-20 00:28:17 +01:00
9 changed files with 298 additions and 115 deletions
+32 -3
View File
@@ -1,18 +1,30 @@
#include "camera.h"
#include <stdio.h>
#include "maths.h"
#include "level.h"
#define ROTATION_SPEED PI
#define TRANSLATIONAL_SPEED 1.f
static void move(Camera* camera, float t);
static void castRays(Camera* camera);
int camera_init(Camera* camera, sf::Vector2f pos, float direction, unsigned int resolution, float fov)
{
camera->pos = pos;
camera->direction = direction;
camera->resolution = resolution;
camera->fov = fov;
camera->rays = (CameraRay*)malloc(sizeof(CameraRay)*resolution);
return 1;
}
void camera_update(Camera* camera, float t)
{
if (!camera) return;
move(camera, t);
castRays(camera);
}
static void move(Camera* camera, float t)
@@ -29,8 +41,25 @@ static void move(Camera* camera, float t)
rotation -=1;
float magnitude = forward * t * TRANSLATIONAL_SPEED;
sf::Vector2f offset(magnitude * cos(camera->direction), magnitude *sin(camera->direction));
sf::Vector2f offset(magnitude * cos(camera->direction), magnitude * sin(camera->direction));
camera->pos += offset;
camera->direction += rotation * t * ROTATION_SPEED;
}
void camera_destroy(Camera *camera)
{
free(camera->rays);
}
static void castRays(Camera* camera)
{
float rayDirectionStep = camera->fov / (float)camera->resolution;
float rayDirection = camera->direction - camera->fov/2.f + rayDirectionStep/2.f;
for (unsigned int i = 0; i < camera->resolution; i++) {
camera->rays[i].direction = rayDirection;
camera->rays[i].distance = level_rayCast(camera->pos, rayDirection, &camera->rays[i].tileData);
rayDirection += rayDirectionStep;
}
}
+12
View File
@@ -2,6 +2,14 @@
#define CAMERA_H
#include <SFML/Graphics.hpp>
#include "level.h"
typedef struct
{
float direction;
float distance;
TileData tileData;
} CameraRay;
typedef struct
{
@@ -9,8 +17,12 @@ typedef struct
float direction;
unsigned int resolution;
float fov;
CameraRay* rays;
} Camera;
int camera_init(Camera* camera, sf::Vector2f pos, float direction, unsigned int resolution, float fov);
void camera_update(Camera* camera, float t);
void camera_destroy(Camera* camera);
#endif
+84
View File
@@ -0,0 +1,84 @@
#include "firstperson.h"
#include <stdio.h>
#include "maths.h"
static sf::RenderTexture renderTexture;
static sf::Vector2f renderTexturePosition;
static bool init = false;
static unsigned int width;
static unsigned int height;
int firstperson_init(unsigned int _width, unsigned int _height)
{
printf("firstperson_init()\n");
if (!renderTexture.create(_width, _height)) return 0;
width = _width;
height = _height;
init = true;
return 1;
}
void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera)
{
const float columnWidth = (float)width/(float)camera->resolution;
float columnHeight = 0;
renderTexture.clear();
for (unsigned int i = 0; i < camera->resolution; i++) {
// Get distance of ray intersection in the direction of the camera,
// instead of the actual ray distance to avoid fish eye effect.
float angleDiff = camera->rays[i].direction - camera->direction;
float distance = camera->rays[i].distance * cos(angleDiff);
if (distance > 0.f) columnHeight = 0.5f * (float)height/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);
switch (camera->rays[i].tileData.side) {
case NORTH:
break;
case EAST:
case WEST:
brightness *= 0.9;
break;
case SOUTH:
brightness *= 0.8;
break;
}
sf::Color color(brightness, brightness, brightness);
switch (camera->rays[i].tileData.value) {
case 2:
color *= sf::Color::Red;
break;
case 3:
color *= sf::Color::Green;
break;
case 4:
color *= sf::Color::Blue;
break;
};
sf::RectangleShape rectangle(sf::Vector2f(columnWidth, columnHeight));
rectangle.setPosition(sf::Vector2f(i*columnWidth, centeredHeight));
rectangle.setFillColor(color);
renderTexture.draw(rectangle);
}
renderTexture.display();
sf::Sprite sprite(renderTexture.getTexture());
sprite.setPosition(renderTexturePosition);
renderTarget->draw(sprite);
}
void firstperson_setTexturePosition(float x, float y)
{
renderTexturePosition.x = x;
renderTexturePosition.y = y;
}
+11
View File
@@ -0,0 +1,11 @@
#ifndef FIRSTPERSON_H
#define FIRSTPERSON_H
#include <SFML/Graphics.hpp>
#include "camera.h"
int firstperson_init(unsigned int width, unsigned int height);
void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera);
void firstperson_setTexturePosition(float x, float y);
#endif
+33 -72
View File
@@ -1,48 +1,45 @@
#include "level.h"
#include <stdio.h>
#include "maths.h"
#define WIDTH 5
#define HEIGHT 5
#define WIDTH 10
#define HEIGHT 10
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize);
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal);
static sf::Vertex getGridLineVertex(float n, float maxDimension, bool isStart, bool isHorizontal);
static float castRay(sf::Vector2f point, float direction);
static float castRay(sf::Vector2f point, float direction, TileData* tileData);
static void getGridIndex(sf::Vector2f point, int* x, int* y);
static unsigned int level[WIDTH * HEIGHT] = {
0, 0, 1, 1, 1,
0, 0, 0, 0, 0,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 0, 0, 0, 0, 2, 0, 1,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 3, 0, 1,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 4, 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()
{
printf("level_init()\n");
return 1;
}
void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize)
{
if (!renderTarget) return;
drawGrid(renderTarget, drawSize/WIDTH);
}
void level_end()
{
printf("level_end()\n");
return;
}
float level_rayCastDistance(sf::Vector2f point, float direction)
float level_rayCast(sf::Vector2f point, float direction, TileData* tileData)
{
return castRay(point, direction);
if (!tileData) return -1.f;
return castRay(point, direction, tileData);
}
void level_getDimensions(unsigned int* width, unsigned int* height)
@@ -51,61 +48,15 @@ void level_getDimensions(unsigned int* width, unsigned int* height)
*height = HEIGHT;
}
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize)
unsigned int level_getGridValue(unsigned int x, unsigned int y)
{
for (unsigned int x = 0; x < WIDTH; x++) {
for (unsigned int y = 0; y < HEIGHT; y++) {
if (!level[y * HEIGHT + x]) continue;
if (x < 0 || WIDTH <= x) return 0;
if (y < 0 || HEIGHT <= y) return 0;
sf::RectangleShape rectangle(sf::Vector2f(tileSize, tileSize));
rectangle.setPosition((float)x * tileSize, (float)y * tileSize);
renderTarget->draw(rectangle);
}
}
drawGridLine(renderTarget, tileSize, true);
drawGridLine(renderTarget, tileSize, false);
return level[y * HEIGHT + x];
}
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal)
{
unsigned int lines = isHorizontal? WIDTH : HEIGHT;
for (unsigned int n = 0; n < lines; n++) {
if (n == 0) continue;
float offset = (float)n * step;
float maxDimension = (float)lines * step;
sf::Vertex line[] =
{
getGridLineVertex(offset, maxDimension, true, isHorizontal),
getGridLineVertex(offset, maxDimension, false, isHorizontal)
};
renderTarget->draw(line, 2, sf::Lines);
}
}
static sf::Vertex getGridLineVertex(float offset, float maxDimension, bool isStart, bool isHorizontal)
{
sf::Vertex start;
sf::Vertex end;
if (isHorizontal) {
start = sf::Vertex(sf::Vector2f(offset, 0));
end = sf::Vertex(sf::Vector2f(offset, maxDimension));
}
else {
start = sf::Vertex(sf::Vector2f(0, offset));
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
}
sf::Color color(100, 100, 100);
start.color = color;
end.color = color;
return isStart? start : end;
}
static float castRay(sf::Vector2f point, float direction)
static float castRay(sf::Vector2f point, float direction, TileData* tileData)
{
int indexX, indexY;
getGridIndex(point, &indexX, &indexY);
@@ -175,7 +126,12 @@ static float castRay(sf::Vector2f point, float direction)
if (!(inLevel0 || inLevel1)) break;
if (horizontalRayDist < verticalRayDist) {
if (level[indexY0 * WIDTH + indexX0]) return horizontalRayDist;
unsigned int gridValue = level[indexY0 * WIDTH + indexX0];
if (gridValue) {
tileData->value = gridValue;
tileData->side = goingDown? NORTH : SOUTH;
return horizontalRayDist;
}
horizontalProjectedX += horizontalStepX;
horizontalProjectedY += horizontalStepY;
@@ -183,7 +139,12 @@ static float castRay(sf::Vector2f point, float direction)
horizontalRayDist += std::abs(horizontalStepY/horizontalDistCoeff);
}
else {
if (level[indexY1 * WIDTH + indexX1]) return verticalRayDist;
unsigned int gridValue = level[indexY1 * WIDTH + indexX1];
if (gridValue) {
tileData->value = gridValue;
tileData->side = goingRight? WEST : EAST;
return verticalRayDist;
}
verticalProjectedX += verticalStepX;
verticalProjectedY += verticalStepY;
+16 -2
View File
@@ -3,10 +3,24 @@
#include <SFML/Graphics.hpp>
enum level_tileSide
{
NORTH,
EAST,
SOUTH,
WEST
};
typedef struct
{
unsigned int value;
level_tileSide side;
} TileData;
int level_init();
void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize);
void level_end();
float level_rayCastDistance(sf::Vector2f point, float direction);
float level_rayCast(sf::Vector2f point, float direction, TileData* tileData);
void level_getDimensions(unsigned int* width, unsigned int* height);
unsigned int level_getGridValue(unsigned int x, unsigned int y);
#endif
+93 -30
View File
@@ -3,28 +3,31 @@
#include "maths.h"
#include "level.h"
// Camera drawing
static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera);
static void drawRays(sf::RenderTarget* renderTarget, Camera* camera);
static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float angle, float length, sf::Color color);
// Grid drawing
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize);
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal);
static sf::Vertex getGridLineVertex(float n, float maxDimension, bool isStart, bool isHorizontal);
static sf::RenderTexture minimap;
static sf::RenderTexture renderTexture;
static sf::Vector2f renderTexturePosition;
static bool init = false;
static unsigned int minimapSize;
static unsigned int gridWidth, gridHeight;
static float drawScale;
int minimap_init(unsigned int size)
{
printf("minimap_init()\n");
if (!minimap.create(size, size)) return 0;
level_init();
unsigned int width, height;
level_getDimensions(&width, &height);
if (!renderTexture.create(size, size)) return 0;
minimapSize = size;
drawScale = (float)size/(float)width;
level_getDimensions(&gridWidth, &gridHeight);
drawScale = (float)size/(float)gridWidth;
init = true;
return 1;
@@ -34,15 +37,22 @@ void minimap_update(sf::RenderTarget* renderTarget, Camera* camera)
{
if (!init) return;
if (!renderTarget || !camera) return;
minimap.clear();
level_update(&minimap, minimapSize);
drawCamera(&minimap, camera);
minimap.display();
sf::Sprite sprite(minimap.getTexture());
renderTexture.clear();
drawGrid(&renderTexture, minimapSize/gridWidth);
drawCamera(&renderTexture, camera);
renderTexture.display();
sf::Sprite sprite(renderTexture.getTexture());
renderTarget->draw(sprite);
}
void minimap_setTexturePosition(float x, float y)
{
renderTexturePosition.x = x;
renderTexturePosition.y = y;
}
static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera)
{
const sf::Vector2f scaledPos = camera->pos * drawScale;
@@ -61,24 +71,9 @@ static void drawRays(sf::RenderTarget* renderTarget, Camera* camera)
{
const sf::Vector2f scaledPos = camera->pos * drawScale;
float rayDirection = 0;
float rayDirectionStep = camera->fov / (float)camera->resolution;
bool isOddResolution = (camera->resolution % 2);
float rayDirectionOffset = isOddResolution? 0 : rayDirectionStep / 2.f;
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;
float distance = level_rayCastDistance(camera->pos, rayDirection) * drawScale;
drawLine(renderTarget, scaledPos, rayDirection, distance, sf::Color(150, 150, 100));
if ((i + isOddResolution) % 2) rayDirectionOffset += rayDirectionStep;
CameraRay* ray = &camera->rays[i];
drawLine(renderTarget, scaledPos, ray->direction, ray->distance * drawScale, sf::Color(150, 150, 100));
}
}
@@ -98,3 +93,71 @@ static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float ang
renderTarget->draw(line, 2, sf::Lines);
}
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize)
{
for (unsigned int x = 0; x < gridWidth; x++) {
for (unsigned int y = 0; y < gridHeight; y++) {
unsigned int value = level_getGridValue(x, y);
if (!value) continue;
sf::Color color(255, 255, 255);
switch (value) {
case 2:
color = sf::Color::Red;
break;
case 3:
color = sf::Color::Green;
break;
case 4:
color = sf::Color::Blue;
break;
};
sf::RectangleShape rectangle(sf::Vector2f(tileSize, tileSize));
rectangle.setPosition((float)x * tileSize, (float)y * tileSize);
rectangle.setFillColor(color);
renderTarget->draw(rectangle);
}
}
drawGridLine(renderTarget, tileSize, true);
drawGridLine(renderTarget, tileSize, false);
}
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal)
{
unsigned int lines = isHorizontal? gridWidth : gridHeight;
for (unsigned int n = 0; n < lines; n++) {
if (n == 0) continue;
float offset = (float)n * step;
float maxDimension = (float)lines * step;
sf::Vertex line[] =
{
getGridLineVertex(offset, maxDimension, true, isHorizontal),
getGridLineVertex(offset, maxDimension, false, isHorizontal)
};
renderTarget->draw(line, 2, sf::Lines);
}
}
static sf::Vertex getGridLineVertex(float offset, float maxDimension, bool isStart, bool isHorizontal)
{
sf::Vertex start;
sf::Vertex end;
if (isHorizontal) {
start = sf::Vertex(sf::Vector2f(offset, 0));
end = sf::Vertex(sf::Vector2f(offset, maxDimension));
}
else {
start = sf::Vertex(sf::Vector2f(0, offset));
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
}
sf::Color color(100, 100, 100);
start.color = color;
end.color = color;
return isStart? start : end;
}
+1
View File
@@ -6,5 +6,6 @@
int minimap_init(unsigned int size);
void minimap_update(sf::RenderTarget* renderTarget, Camera* camera);
void minimap_setTexturePosition(float x, float y);
#endif
+16 -8
View File
@@ -5,23 +5,29 @@
#include "maths.h"
#include "camera.h"
#include "minimap.h"
#include "firstperson.h"
#define MINIMAP_SIZE 250
#define HALF_MINIMAP_SIZE MINIMAP_SIZE/2.f
#define DRAW_SCALE MINIMAP_SIZE/5.f
#define MINIMAP_SIZE 512
#define VIEW_SIZE MINIMAP_SIZE*2
static int handleKeyCode(sf::Keyboard::Key key);
static Camera camera = { sf::Vector2f(5.f/2.f, 5.f/2.f), 0.f, 300, 0.5f*PI };
static Camera camera;
static sf::Uint32 style = sf::Style::Titlebar;
static sf::RenderWindow window(sf::VideoMode(MINIMAP_SIZE + camera.resolution, MINIMAP_SIZE), "Raycasting", style);
static sf::RenderWindow window(sf::VideoMode(MINIMAP_SIZE + VIEW_SIZE, MINIMAP_SIZE), "Raycasting", style);
static sf::Clock timer;
int view_init()
{
printf("view_init()\n");
minimap_init(MINIMAP_SIZE);
if (!camera_init(&camera, sf::Vector2f(10.f/2.f, 10.f/2.f), 0.f, VIEW_SIZE, 0.5f*PI)) return 0;
if (!minimap_init(MINIMAP_SIZE)) return 0;
if (!firstperson_init(VIEW_SIZE, MINIMAP_SIZE)) return 0;
minimap_setTexturePosition(0.f, 0.f);
firstperson_setTexturePosition(MINIMAP_SIZE, 0.f);
return 1;
}
@@ -48,6 +54,8 @@ int view_update()
window.clear();
minimap_update(&window, &camera);
firstperson_update(&window, &camera);
window.display();
return 1;
@@ -55,9 +63,9 @@ int view_update()
void view_end()
{
printf("view_end()\n");
camera_destroy(&camera);
window.close();
printf("view_end()\n");
}
static int handleKeyCode(sf::Keyboard::Key key)