Set up camera and bg in demo
This commit is contained in:
64
demo.c
64
demo.c
@@ -1,26 +1,78 @@
|
|||||||
#include "ff.h"
|
#include "ff.h"
|
||||||
|
#include "vec3.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define W 800
|
#define W 800
|
||||||
#define H 600
|
#define H 600
|
||||||
|
|
||||||
|
#define VP_H 2.0
|
||||||
|
#define VP_W (VP_H * W / H)
|
||||||
|
|
||||||
|
#define FOCLEN 1.0
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
vec3_t orig, dir;
|
||||||
|
} ray_t;
|
||||||
|
|
||||||
|
static const vec3_t lightblue = { 0.5, 0.7, 1.0 };
|
||||||
|
static const vec3_t white = { 1.0, 1.0, 1.0 };
|
||||||
|
|
||||||
|
static const vec3_t camera = { 0, 0, 0 };
|
||||||
|
static const vec3_t vp_disp = { 0, 0, FOCLEN };
|
||||||
|
static const vec3_t vp_u = { VP_W, 0, 0 };
|
||||||
|
static const vec3_t vp_v = { 0, -VP_H, 0 };
|
||||||
|
static const vec3_t pix_x_step = { VP_W / W, 0, 0 };
|
||||||
|
static const vec3_t pix_y_step = { 0, -VP_H / H, 0 };
|
||||||
|
|
||||||
|
static pix_t pix[W * H];
|
||||||
|
|
||||||
|
static vec3_t raycol(ray_t ray)
|
||||||
|
{
|
||||||
|
const vec3_t u = vec3_unit(ray.dir);
|
||||||
|
const double a = (u.y + 1.0) / 2.0;
|
||||||
|
return vec3_add(vec3_scale(lightblue, a), vec3_scale(white, 1 - a));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setpix(vec3_t col, pix_t *out)
|
||||||
|
{
|
||||||
|
out->r = UINT16_MAX * col.x;
|
||||||
|
out->g = UINT16_MAX * col.y;
|
||||||
|
out->b = UINT16_MAX * col.z;
|
||||||
|
out->a = UINT16_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
pix_t pix[W * H] = {};
|
|
||||||
img_t img = { .w = W, .h = H, .pix = pix };
|
img_t img = { .w = W, .h = H, .pix = pix };
|
||||||
|
|
||||||
|
const vec3_t vp_topleft = vec3_sub(
|
||||||
|
vec3_sub(vec3_sub(camera, vp_disp), vec3_scale(vp_u, 0.5)),
|
||||||
|
vec3_scale(vp_v, 0.5));
|
||||||
|
const vec3_t pix_orig = vec3_add(
|
||||||
|
vec3_add(vp_topleft, vec3_scale(pix_x_step, 0.5)),
|
||||||
|
vec3_scale(pix_y_step, 0.5));
|
||||||
|
|
||||||
for (unsigned y = 0; y < H; ++y) {
|
for (unsigned y = 0; y < H; ++y) {
|
||||||
|
fprintf(stderr, "\r[%3d/%3d]", y, H);
|
||||||
|
fflush(stderr);
|
||||||
|
|
||||||
|
const vec3_t row = vec3_add(pix_orig, vec3_scale(pix_y_step, y));
|
||||||
for (unsigned x = 0; x < W; ++x) {
|
for (unsigned x = 0; x < W; ++x) {
|
||||||
pix_t *out = pix + (W * y + x);
|
const ray_t ray = {
|
||||||
out->r = 0xffff * x / W;
|
.orig = camera,
|
||||||
out->g = 0;
|
.dir = vec3_add(row, vec3_scale(pix_x_step, x)),
|
||||||
out->b = 0xffff * y / H;
|
};
|
||||||
out->a = 0xffff;
|
const vec3_t col = raycol(ray);
|
||||||
|
setpix(col, pix + (W * y + x));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "\r[%3d/%3d]\n", H, H);
|
||||||
|
|
||||||
ff_write(STDOUT_FILENO, img);
|
ff_write(STDOUT_FILENO, img);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user