Finished chapter 4.

This commit is contained in:
Michael Smith 2019-01-15 18:11:20 +01:00
parent 3f6c04cde4
commit 37f1a18d1d
4 changed files with 23 additions and 18 deletions

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
mkdir -p build mkdir -p build
pushd build pushd build
rm -rf * rm -rf sdl_platform.dSYM
# Plugin library # Plugin library
c++ -Wall -std=c++11 -c -fpic -g ../code/rt_weekend.cpp -o librt_weekend.o c++ -Wall -std=c++11 -c -fpic -g ../code/rt_weekend.cpp -o librt_weekend.o

View File

@ -1,5 +1,5 @@
#ifndef RAYH #ifndef RAY_H
#define RAYH #define RAY_H
#include "vec3.h" #include "vec3.h"

View File

@ -1,17 +1,18 @@
#include "rt_weekend.h" #include "rt_weekend.h"
#include "ray.h" #include "ray.h"
bool hit_sphere(const vec3& center, float radius, const ray& r) bool hit_sphere(const vec3 &center, float radius, const ray &r)
{ {
vec3 oc = r.origin() - center; vec3 oc = r.origin() - center;
float a = dot(r.direction(), r.direction()); float a = dot(r.direction(), r.direction());
float b = 2.0 * dot(oc, r.direction()); float b = 2.0 * dot(oc, r.direction());
float c = dot(oc, oc) - radius * radius; float c = dot(oc, oc) - radius * radius;
float discriminant = b * b - 4 * a * c; float discriminant = b * b - 4 * a * c;
return (discriminant > 0); return (discriminant > 0);
} }
vec3 color(const ray& r) vec3 color (const ray &r)
{ {
if (hit_sphere(vec3(0, 0, -1), 0.5, r)) if (hit_sphere(vec3(0, 0, -1), 0.5, r))
return vec3(1, 0, 0); return vec3(1, 0, 0);
@ -26,21 +27,25 @@ PluginUpdateAndRender(plugin_offscreen_buffer *Buffer)
{ {
int Pitch = Buffer->Pitch; int Pitch = Buffer->Pitch;
uint8_t *Row = (uint8_t *)Buffer->Memory; uint8_t *Row = (uint8_t *)Buffer->Memory;
for(int Y = 0;
Y < Buffer->Height; vec3 lower_left_corner(-2.0, -1.0, -1.0);
++Y) vec3 horizontal(4.0, 0.0, 0.0);
vec3 vertical(0.0, 2.0, 0.0);
vec3 origin(0.0, 0.0, 0.0);
for(int j = Buffer->Height - 1; j >= 0; j--)
{ {
uint32_t *Pixel = (uint32_t *)Row; uint32_t *Pixel = (uint32_t *)Row;
for(int X = 0; for(int i = 0; i < Buffer->Width; i++)
X < Buffer->Width;
++X)
{ {
float r = float(X) / float(Buffer->Width); float u = float(i) / float(Buffer->Width);
float g = float(Y) / float(Buffer->Height); float v = float(j) / float(Buffer->Height);
float b = 0.2; ray r(origin, lower_left_corner + u * horizontal + v * vertical);
int ir = int(255.99 * r); vec3 col = color(r);
int ig = int(255.99 * g); int ir = int(255.99 * col[0]);
int ib = int(255.99 * b); int ig = int(255.99 * col[1]);
int ib = int(255.99 * col[2]);
*Pixel++ = (ir << 24) + (ig << 16) + (ib << 8) + 0xFF; *Pixel++ = (ir << 24) + (ig << 16) + (ib << 8) + 0xFF;
} }

View File

@ -12,7 +12,7 @@
const char *WindowTitle = "Ray Tracing in a Weekend"; const char *WindowTitle = "Ray Tracing in a Weekend";
const uint32_t TARGET_FRAME_RATE = 30; const uint32_t TARGET_FRAME_RATE = 15;
const uint32_t TICKS_PER_FRAME = 1000 / TARGET_FRAME_RATE; const uint32_t TICKS_PER_FRAME = 1000 / TARGET_FRAME_RATE;
const uint32_t WINDOW_WIDTH = 500; const uint32_t WINDOW_WIDTH = 500;
const uint32_t WINDOW_HEIGHT = 250; const uint32_t WINDOW_HEIGHT = 250;