Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.xcodeproj
old
gloss
.*.d
4 changes: 2 additions & 2 deletions Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ typedef struct {
float red, green, blue;
} Color;

Color makeColorWhite();
Color makeColorBlack();
Color makeColorWhite(void);
Color makeColorBlack(void);
Color makeColorLightness(const float l);
Color makeColor(const float red, const float green, const float blue);

Expand Down
1 change: 1 addition & 0 deletions Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type * lowercaseType##ContainerAddValue(type##Container *container, const type v
assert(0); \
} \
} \
__attribute__((noreturn)) \
void lowercaseType##ContainerAddValues(type##Container *container, const type##Container values) { \
/* TODO: Actually implement. */ \
assert(0); \
Expand Down
84 changes: 84 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
.SUFFIXES:

CC := $(CROSS_COMPILE)gcc

FAST := -flto -fwhole-program \
-msse4.2 -ftree-vectorize \
-mtune=corei7 -march=corei7 \
-ffast-math

EXTRA_DEFS := -D_FILE_OFFSET_BITS=64
CFLAGS := -g -pipe -O2 -Wall \
$(FAST) \
-Wsign-compare -Wcast-align \
-Wstrict-prototypes \
-Wmissing-prototypes \
-Wmissing-declarations \
-Wmissing-noreturn \
-finline-functions \
-Wmissing-format-attribute \
-Wno-cast-align \
-I. -I/usr/include/SDL \
-std=gnu99 \
$(EXTRA_DEFS)

GLOSS_BIN := gloss
GLOSS_LIBS := -lSDL -lm
GLOSS_OBJ = \
Box.o \
Color.o \
Intersection.o \
main.o \
Material.o \
MaterialContainer.o \
Matrix.o \
Photon.o \
PhotonContainer.o \
PhotonEndPoint.o \
PhotonEndPointContainer.o \
Plane.o \
randf.o \
Ray.o \
Scene.o \
SceneObjectBox.o \
SceneObject.o \
SceneObjectContainer.o \
SceneObjectPlane.o \
SceneObjectSphere.o \
SceneObjectUnitPlane.o \
Sphere.o \
Vector.o

ALL_BIN := $(GLOSS_BIN)
ALL_OBJ := $(GLOSS_OBJ)
ALL_DEP := $(patsubst %.o, .%.d, $(ALL_OBJ))
ALL_TARGETS := $(ALL_BIN)

TARGET: all

.PHONY: all clean

all: $(ALL_BIN)

ifeq ($(filter clean, $(MAKECMDGOALS)),clean)
CLEAN_DEP := clean
else
CLEAN_DEP :=
endif

%.o %.d: %.c $(CLEAN_DEP) $(CONFIG_MAK) Makefile
@echo " [C] $<"
@$(CC) $(CFLAGS) -MMD -MF $(patsubst %.o, .%.d, $@) \
-MT $(patsubst .%.d, %.o, $@) \
-c -o $(patsubst .%.d, %.o, $@) $<

$(GLOSS_BIN): $(GLOSS_OBJ)
@echo " [LINK] $@"
@$(CC) $(CFLAGS) -o $@ $(GLOSS_OBJ) $(GLOSS_LIBS)

clean:
rm -f $(ALL_TARGETS) $(ALL_OBJ) $(ALL_DEP)

ifneq ($(MAKECMDGOALS),clean)
-include $(ALL_DEP)
endif
76 changes: 41 additions & 35 deletions Matrix.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
#include "Matrix.h"
#include <math.h>

__attribute__((const))
Matrix makeMatrixZero() {

return (Matrix) {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
return (Matrix) {{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
}};
}

__attribute__((const))
Matrix makeMatrixIdentity() {

return (Matrix) {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
return (Matrix) {{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
}};
}

__attribute__((const))
Matrix makeMatrixTranslation(const Vector v) {

return (Matrix) {
1, 0, 0, v.x,
0, 1, 0, v.y,
0, 0, 1, v.z,
0, 0, 0, 1
};
return (Matrix) {{
{1, 0, 0, v.x},
{0, 1, 0, v.y},
{0, 0, 1, v.z},
{0, 0, 0, 1},
}};
}

__attribute__((const))
Matrix makeMatrixAxisAngle(const Vector axis, const float angle) {

float length = vLength(axis);
if(length < 0.0005){

Expand All @@ -59,63 +62,69 @@ Matrix makeMatrixAxisAngle(const Vector axis, const float angle) {
return matrix;
}

__attribute__((const))
bool mEqual(const Matrix a, const Matrix b) {

for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
if (a.values[i][j] + vEpsilon < b.values[i][j] || b.values[i][j] + vEpsilon < a.values[i][j])
return false;

return true;
}

Matrix mMul(const Matrix a, const Matrix b) {
__attribute__((pure))
Matrix mMul(const Matrix *a, const Matrix *b) {

Matrix matrix = makeMatrixZero();

for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
for (int k=0; k<4; k++)
matrix.values[i][j] += a.values[i][k] * b.values[k][j];
matrix.values[i][j] += a->values[i][k] * b->values[k][j];

return matrix;
}

Vector mvMul(const Matrix matrix, const Vector vector) {
__attribute__((pure))
Vector mvMul(const Matrix *matrix, const Vector *vector) {

Vector newVector = mvMulDir(matrix, vector);

// Access x,y,z as an array.
float* newVectorValues = (float*)&newVector.x;

for (int i=0; i<3; i++)
newVectorValues[i] += matrix.values[i][3];
newVectorValues[i] += matrix->values[i][3];

return newVector;
}

Vector mvMulDir(const Matrix matrix, const Vector vector) {
__attribute__((pure))
Vector mvMulDir(const Matrix *matrix, const Vector *vector) {

Vector newVector = makeVector(0, 0, 0);

// Access x,y,z as an array.
float* vectorValues = (float*)&vector.x;
float* vectorValues = (float*)&vector->x;
float* newVectorValues = (float*)&newVector.x;

for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
newVectorValues[i] += matrix.values[i][j] * vectorValues[j];
newVectorValues[i] += matrix->values[i][j] * vectorValues[j];

return newVector;
}

Ray mrMul(const Matrix matrix, const Ray ray) {
__attribute__((pure))
Ray mrMul(const Matrix *matrix, const Ray *ray) {

return makeRay(mvMul(matrix, ray.origin), vNormalized(mvMulDir(matrix, ray.direction)));
return makeRay(mvMul(matrix, &ray->origin),
vNormalized(mvMulDir(matrix, &ray->direction)));
}

Matrix mInversed(const Matrix matrix) {
float* m = (float*)&matrix.values[0][0];
__attribute__((pure))
Matrix mInversed(const Matrix *matrix) {
float* m = (float*)&matrix->values[0][0];
Matrix returnValue;
float* out = &returnValue.values[0][0];

Expand Down Expand Up @@ -289,6 +298,3 @@ Matrix mInversed(const Matrix matrix) {

return returnValue;
}



25 changes: 18 additions & 7 deletions Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,31 @@ typedef struct {
float values[4][4];
} Matrix;

Matrix makeMatrixZero();
Matrix makeMatrixIdentity();
__attribute__((const))
Matrix makeMatrixZero(void);
__attribute__((const))
Matrix makeMatrixIdentity(void);
__attribute__((const))
Matrix makeMatrixTranslation(const Vector v);
__attribute__((const))
Matrix makeMatrixAxisAngle(const Vector axis, const float angle);

__attribute__((const))
bool mEqual(const Matrix a, const Matrix b);

Matrix mMul(const Matrix a, const Matrix b);
__attribute__((pure))
Matrix mMul(const Matrix *a, const Matrix *b);

Vector mvMul (const Matrix Matrix, const Vector Vector);
Vector mvMulDir(const Matrix Matrix, const Vector Vector);
__attribute__((pure))
Vector mvMul (const Matrix *Matrix, const Vector *Vector);

Ray mrMul(const Matrix matrix, const Ray ray);
__attribute__((pure))
Vector mvMulDir(const Matrix *Matrix, const Vector *Vector);

Matrix mInversed();
__attribute__((pure))
Ray mrMul(const Matrix *matrix, const Ray *ray);

__attribute__((pure))
Matrix mInversed(const Matrix *);

#endif // MATRIX_H
19 changes: 14 additions & 5 deletions Scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "Matrix.h"
#include "Color.h"
#include "Intersection.h"
#include "PhotonEndpoint.h"
#include "PhotonEndPoint.h"
#include "PhotonEndPointContainer.h"
#include "PhotonContainer.h"
#include "pi.h"
Expand All @@ -28,8 +28,10 @@ Color sceneTraceRayAtPixel(const Scene *scene, const int currentPixel, const int
float maxX = tanf(cameraFov/360.0*PI);
float x = ((((currentPixel % width) + randf()) / width ) * 2 - 1) * maxX;
float y = -((((currentPixel / width) + randf()) / height) * 2 - 1) * maxX / cameraAspectRatio;
Matrix m = mInversed(&scene->cameraOrientation);
Ray r = makeRay(makeVectorOrigo(), vNormalized(makeVector(x, y, 1)));

return sceneTraceRay(scene, mrMul(mInversed(scene->cameraOrientation), makeRay(makeVectorOrigo(), vNormalized(makeVector(x, y, 1)))), numCameraRayBounces);
return sceneTraceRay(scene, mrMul(&m, &r), numCameraRayBounces);
}

void sceneGeneratePhotons(Scene *scene, const int lightRayBounces, const int numPhotonsPerLightSource) {
Expand Down Expand Up @@ -187,14 +189,20 @@ void buildCornellBox(Scene *scene) {


// Boxes
Matrix a, b;
a = makeMatrixAxisAngle(makeVector(0, 1, 0), .3);
b = makeMatrixTranslation(makeVector(-.3, -.3, .3));
sceneObjectContainerAddValue(&scene->objects, makeSceneObjectBox(
makeVector(.3, .7, .3),
mMul(makeMatrixAxisAngle(makeVector(0, 1, 0), .3), makeMatrixTranslation(makeVector(-.3, -.3, .3))),
mMul(&a, &b),
whiteMaterial
));

a = makeMatrixAxisAngle(makeVector(0, 1, 0), -.3);
b = makeMatrixTranslation(makeVector(.3, -.75, -.3));
sceneObjectContainerAddValue(&scene->objects, makeSceneObjectBox(
makeVector(.3, .3, .3),
mMul(makeMatrixAxisAngle(makeVector(0, 1, 0), -.3), makeMatrixTranslation(makeVector(.3, -.75, -.3))),
mMul(&a, &b),
whiteMaterial
));

Expand Down Expand Up @@ -256,7 +264,8 @@ void buildCornellBox(Scene *scene) {



void buildSpherePhotonSpawnTest(Scene *scene) {
__attribute__((unused))
static void buildSpherePhotonSpawnTest(Scene *scene) {

// Camera
scene->cameraOrientation = makeMatrixTranslation(makeVector(0, 0, 3.8));
Expand Down
2 changes: 1 addition & 1 deletion Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct {
} Scene;


Scene makeScene();
Scene makeScene(void);

Color sceneTraceRayAtPixel(const Scene *scene, const int currentPixel, const int width, const int height, const int numCameraRayBounces);
void sceneGeneratePhotons(Scene *scene, const int lightRayBounces, const int numPhotonsPerLightSource);
Expand Down
11 changes: 6 additions & 5 deletions SceneObjectBox.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
#include "randf.h"
#include <math.h>

const SceneObjectVTable sceneObjectBoxVTable = (SceneObjectVTable) {
const SceneObjectVTable sceneObjectBoxVTable = {
&sceneObjectBoxIntersectRay,
&sceneObjectBoxEmitPhotons
};

SceneObject makeSceneObjectBox (const Vector size, const Matrix transform, const Material *material) {

return (SceneObject) {&sceneObjectBoxVTable, material, transform, mInversed(transform), {.box = makeBox(size)}};
return (SceneObject) {&sceneObjectBoxVTable, material, transform, mInversed(&transform), {.box = makeBox(size)}};
}

Intersection sceneObjectBoxIntersectRay(const SceneObject object, const Ray ray) {

Intersection intersection = bIntersect(object.box, mrMul(object.inversedTransform, ray));
Intersection intersection = bIntersect(object.box,
mrMul(&object.inversedTransform, &ray));

if (intersection.hitType) {

intersection.normal = mvMulDir(object.transform, intersection.normal );
intersection.position = mvMul (object.transform, intersection.position);
intersection.normal = mvMulDir(&object.transform, &intersection.normal );
intersection.position = mvMul (&object.transform, &intersection.position);
intersection.material = object.material;

if (intersection.material->isPerfectBlack) {
Expand Down
Loading