Compare commits
7 Commits
16c6048296
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 44a84bbc53 | |||
| b76e73d57b | |||
| cc9f3f06db | |||
| 06d685d6a6 | |||
| 33ce544a7e | |||
| 3ab2f14235 | |||
| 61681f5e62 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
main
|
||||
result.m4v
|
||||
.cache
|
||||
compile_commands.json
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
[
|
||||
{
|
||||
"arguments": [
|
||||
"/usr/bin/clang",
|
||||
"-c",
|
||||
"-g",
|
||||
"-o",
|
||||
"main",
|
||||
"main.c"
|
||||
],
|
||||
"directory": "/home/abdelrahman/Sources/programming/starfield",
|
||||
"file": "/home/abdelrahman/Sources/programming/starfield/main.c",
|
||||
"output": "/home/abdelrahman/Sources/programming/starfield/main"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/usr/bin/clang-15",
|
||||
"-cc1",
|
||||
"-triple",
|
||||
"x86_64-pc-linux-gnu",
|
||||
"-emit-obj",
|
||||
"-mrelax-all",
|
||||
"--mrelax-relocations",
|
||||
"-disable-free",
|
||||
"-clear-ast-before-backend",
|
||||
"-disable-llvm-verifier",
|
||||
"-discard-value-names",
|
||||
"-main-file-name",
|
||||
"-mrelocation-model",
|
||||
"pic",
|
||||
"-pic-level",
|
||||
"2",
|
||||
"-pic-is-pie",
|
||||
"-mframe-pointer=all",
|
||||
"-fmath-errno",
|
||||
"-ffp-contract=on",
|
||||
"-fno-rounding-math",
|
||||
"-mconstructor-aliases",
|
||||
"-funwind-tables=2",
|
||||
"-target-cpu",
|
||||
"x86-64",
|
||||
"-tune-cpu",
|
||||
"generic",
|
||||
"-mllvm",
|
||||
"-treat-scalable-fixed-error-as-warning",
|
||||
"-debug-info-kind=constructor",
|
||||
"-dwarf-version=5",
|
||||
"-debugger-tuning=gdb",
|
||||
"-fcoverage-compilation-dir=/home/abdelrahman/Sources/programming/starfield",
|
||||
"-resource-dir",
|
||||
"/usr/lib/clang/15.0.7",
|
||||
"-internal-isystem",
|
||||
"/usr/lib/clang/15.0.7/include",
|
||||
"-internal-isystem",
|
||||
"/usr/local/include",
|
||||
"-internal-isystem",
|
||||
"/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../x86_64-pc-linux-gnu/include",
|
||||
"-internal-externc-isystem",
|
||||
"/include",
|
||||
"-internal-externc-isystem",
|
||||
"/usr/include",
|
||||
"-fdebug-compilation-dir=/home/abdelrahman/Sources/programming/starfield",
|
||||
"-ferror-limit",
|
||||
"19",
|
||||
"-stack-protector",
|
||||
"2",
|
||||
"-fgnuc-version=4.2.1",
|
||||
"-fcolor-diagnostics",
|
||||
"-faddrsig",
|
||||
"-D__GCC_HAVE_DWARF2_CFI_ASM=1",
|
||||
"-x",
|
||||
"c",
|
||||
"-o",
|
||||
"/tmp/main-118336.o",
|
||||
"main.c"
|
||||
],
|
||||
"directory": "/home/abdelrahman/Sources/programming/starfield",
|
||||
"file": "/home/abdelrahman/Sources/programming/starfield/main.c",
|
||||
"output": "/tmp/main-118336.o"
|
||||
}
|
||||
]
|
||||
48
main.c
48
main.c
@@ -1,5 +1,6 @@
|
||||
#include "aliases.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_keycode.h>
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
@@ -8,10 +9,8 @@
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define WINDOW_WIDTH 800
|
||||
@@ -29,6 +28,13 @@
|
||||
|
||||
#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 {
|
||||
u32 x;
|
||||
u32 y;
|
||||
@@ -61,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,
|
||||
f64 delta);
|
||||
void render_starfield(SDL_Surface *surface, vec3f_t *stars, u32 count,
|
||||
i32 spread, f64 aov);
|
||||
i32 spread, f64 fov);
|
||||
|
||||
int main(void) {
|
||||
srand(time(NULL));
|
||||
@@ -86,6 +92,7 @@ int main(void) {
|
||||
init_starfield(stars, STAR_COUNT, STAR_SPREAD);
|
||||
|
||||
f64 delta = 1.0 / 60.0; // constant delta
|
||||
f64 fov = 60.0;
|
||||
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
@@ -93,6 +100,26 @@ int main(void) {
|
||||
case SDL_QUIT:
|
||||
running = false;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,16 +127,15 @@ int main(void) {
|
||||
|
||||
render_clear(canvas, BG_COLOR);
|
||||
|
||||
u32 scalar = 10;
|
||||
vec2i_t pos;
|
||||
|
||||
render_starfield(canvas, stars, STAR_COUNT, STAR_SPREAD, 60.0);
|
||||
render_starfield(canvas, stars, STAR_COUNT, STAR_SPREAD, fov);
|
||||
|
||||
SDL_BlitSurface(canvas, NULL, surface, NULL);
|
||||
|
||||
SDL_UpdateWindowSurface(window);
|
||||
}
|
||||
|
||||
SDL_FreeSurface(canvas);
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
SDL_Quit();
|
||||
@@ -219,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,
|
||||
i32 spread, f64 aov) {
|
||||
i32 spread, f64 fov) {
|
||||
vec2f_t projected[count];
|
||||
vec2i_t coords;
|
||||
|
||||
f64 tangent = tan(RAD(fov * 0.5));
|
||||
|
||||
for (u32 i = 0; i < count; ++i) {
|
||||
if (stars[i].z == 0.0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
projected[i] =
|
||||
(vec2f_t){.x = stars[i].x / stars[i].z, .y = stars[i].y / stars[i].z};
|
||||
projected[i] = (vec2f_t){.x = stars[i].x / stars[i].z / tangent,
|
||||
.y = stars[i].y / stars[i].z / tangent};
|
||||
|
||||
if (projected[i].x < NORMALISED_MIN || projected[i].x > NORMALISED_MAX ||
|
||||
projected[i].y < NORMALISED_MIN || projected[i].y > NORMALISED_MAX ||
|
||||
|
||||
Reference in New Issue
Block a user