Finished chapter 4.
This commit is contained in:
parent
3f6c04cde4
commit
37f1a18d1d
2
build.sh
2
build.sh
@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
mkdir -p build
|
||||
pushd build
|
||||
rm -rf *
|
||||
rm -rf sdl_platform.dSYM
|
||||
|
||||
# Plugin library
|
||||
c++ -Wall -std=c++11 -c -fpic -g ../code/rt_weekend.cpp -o librt_weekend.o
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#ifndef RAYH
|
||||
#define RAYH
|
||||
#ifndef RAY_H
|
||||
#define RAY_H
|
||||
|
||||
#include "vec3.h"
|
||||
|
||||
|
||||
@ -1,17 +1,18 @@
|
||||
#include "rt_weekend.h"
|
||||
#include "ray.h"
|
||||
|
||||
bool hit_sphere(const vec3& center, float radius, const ray& r)
|
||||
bool hit_sphere(const vec3 ¢er, float radius, const ray &r)
|
||||
{
|
||||
vec3 oc = r.origin() - center;
|
||||
float a = dot(r.direction(), r.direction());
|
||||
float b = 2.0 * dot(oc, r.direction());
|
||||
float c = dot(oc, oc) - radius * radius;
|
||||
float discriminant = b * b - 4 * a * c;
|
||||
|
||||
return (discriminant > 0);
|
||||
}
|
||||
|
||||
vec3 color(const ray& r)
|
||||
vec3 color (const ray &r)
|
||||
{
|
||||
if (hit_sphere(vec3(0, 0, -1), 0.5, r))
|
||||
return vec3(1, 0, 0);
|
||||
@ -26,21 +27,25 @@ PluginUpdateAndRender(plugin_offscreen_buffer *Buffer)
|
||||
{
|
||||
int Pitch = Buffer->Pitch;
|
||||
uint8_t *Row = (uint8_t *)Buffer->Memory;
|
||||
for(int Y = 0;
|
||||
Y < Buffer->Height;
|
||||
++Y)
|
||||
|
||||
vec3 lower_left_corner(-2.0, -1.0, -1.0);
|
||||
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;
|
||||
for(int X = 0;
|
||||
X < Buffer->Width;
|
||||
++X)
|
||||
for(int i = 0; i < Buffer->Width; i++)
|
||||
{
|
||||
float r = float(X) / float(Buffer->Width);
|
||||
float g = float(Y) / float(Buffer->Height);
|
||||
float b = 0.2;
|
||||
int ir = int(255.99 * r);
|
||||
int ig = int(255.99 * g);
|
||||
int ib = int(255.99 * b);
|
||||
float u = float(i) / float(Buffer->Width);
|
||||
float v = float(j) / float(Buffer->Height);
|
||||
ray r(origin, lower_left_corner + u * horizontal + v * vertical);
|
||||
vec3 col = color(r);
|
||||
int ir = int(255.99 * col[0]);
|
||||
int ig = int(255.99 * col[1]);
|
||||
int ib = int(255.99 * col[2]);
|
||||
|
||||
*Pixel++ = (ir << 24) + (ig << 16) + (ib << 8) + 0xFF;
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
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 WINDOW_WIDTH = 500;
|
||||
const uint32_t WINDOW_HEIGHT = 250;
|
||||
|
||||
Reference in New Issue
Block a user