Add ability to change FOV

This commit is contained in:
Abdelrahman Said 2023-08-27 20:58:12 +01:00
parent cc9f3f06db
commit b76e73d57b
2 changed files with 39 additions and 7 deletions

View File

@ -71,11 +71,11 @@
"-x", "-x",
"c", "c",
"-o", "-o",
"/tmp/main-b05c2f.o", "/tmp/main-f47303.o",
"main.c" "main.c"
], ],
"directory": "/home/abdelrahman/Sources/programming/starfield", "directory": "/home/abdelrahman/Sources/programming/starfield",
"file": "/home/abdelrahman/Sources/programming/starfield/main.c", "file": "/home/abdelrahman/Sources/programming/starfield/main.c",
"output": "/tmp/main-b05c2f.o" "output": "/tmp/main-f47303.o"
} }
] ]

42
main.c
View File

@ -1,5 +1,6 @@
#include "aliases.h" #include "aliases.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_pixels.h> #include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_rect.h> #include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
@ -8,6 +9,7 @@
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -26,6 +28,13 @@
#define PROJECTION_PLANE 1.0 #define PROJECTION_PLANE 1.0
#define RAD(DEG) ((DEG * M_PI) / 180.0)
#define DEG(RAD) ((RAD * 180.0) / M_PI)
#define FOV_MIN 1.0
#define FOV_MAX 170.0
#define FOV_INC 3.0
typedef struct { typedef struct {
u32 x; u32 x;
u32 y; u32 y;
@ -58,7 +67,7 @@ void init_starfield(vec3f_t *stars, u32 count, i32 spread);
void update_startfield(vec3f_t *stars, u32 count, i32 spread, u32 speed, void update_startfield(vec3f_t *stars, u32 count, i32 spread, u32 speed,
f64 delta); f64 delta);
void render_starfield(SDL_Surface *surface, vec3f_t *stars, u32 count, void render_starfield(SDL_Surface *surface, vec3f_t *stars, u32 count,
i32 spread); i32 spread, f64 fov);
int main(void) { int main(void) {
srand(time(NULL)); srand(time(NULL));
@ -83,6 +92,7 @@ int main(void) {
init_starfield(stars, STAR_COUNT, STAR_SPREAD); init_starfield(stars, STAR_COUNT, STAR_SPREAD);
f64 delta = 1.0 / 60.0; // constant delta f64 delta = 1.0 / 60.0; // constant delta
f64 fov = 60.0;
while (running) { while (running) {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
@ -90,6 +100,26 @@ int main(void) {
case SDL_QUIT: case SDL_QUIT:
running = false; running = false;
break; break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
fov -= FOV_INC;
if (fov < FOV_MIN) {
fov = FOV_MIN;
}
break;
case SDLK_DOWN:
fov += FOV_INC;
if (fov > FOV_MAX) {
fov = FOV_MAX;
}
break;
}
break;
} }
} }
@ -97,7 +127,7 @@ int main(void) {
render_clear(canvas, BG_COLOR); render_clear(canvas, BG_COLOR);
render_starfield(canvas, stars, STAR_COUNT, STAR_SPREAD); render_starfield(canvas, stars, STAR_COUNT, STAR_SPREAD, fov);
SDL_BlitSurface(canvas, NULL, surface, NULL); SDL_BlitSurface(canvas, NULL, surface, NULL);
@ -215,17 +245,19 @@ void update_startfield(vec3f_t *stars, u32 count, i32 spread, u32 speed,
} }
void render_starfield(SDL_Surface *surface, vec3f_t *stars, u32 count, void render_starfield(SDL_Surface *surface, vec3f_t *stars, u32 count,
i32 spread) { i32 spread, f64 fov) {
vec2f_t projected[count]; vec2f_t projected[count];
vec2i_t coords; vec2i_t coords;
f64 tangent = tan(RAD(fov * 0.5));
for (u32 i = 0; i < count; ++i) { for (u32 i = 0; i < count; ++i) {
if (stars[i].z == 0.0) { if (stars[i].z == 0.0) {
continue; continue;
} }
projected[i] = projected[i] = (vec2f_t){.x = stars[i].x / stars[i].z / tangent,
(vec2f_t){.x = stars[i].x / stars[i].z, .y = stars[i].y / stars[i].z}; .y = stars[i].y / stars[i].z / tangent};
if (projected[i].x < NORMALISED_MIN || projected[i].x > NORMALISED_MAX || if (projected[i].x < NORMALISED_MIN || projected[i].x > NORMALISED_MAX ||
projected[i].y < NORMALISED_MIN || projected[i].y > NORMALISED_MAX || projected[i].y < NORMALISED_MIN || projected[i].y > NORMALISED_MAX ||