Last active
March 18, 2017 18:19
-
-
Save abstractalgo/dee12083ec418bd8b980 to your computer and use it in GitHub Desktop.
pathtrace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
#include <iostream> | |
#define SCREEN_WIDTH 640 | |
#define SCREEN_HEIGHT 480 | |
#define STEPS 10 | |
#define BLACK Color(0, 0, 0) | |
struct Color | |
{ | |
int r, g, b; | |
Color(int _x=0, int _y=0, int _z=0) : r(_x), g(_y), b(_z) {} | |
Color operator*(double t) { return BLACK; } | |
}; | |
#define BRDF(a,b) _brdf(a,b) | |
vec3 _brdf(vec3 r, IP h) | |
{ | |
return vec3(); | |
} | |
#define SHADE(a,b) _shade(a,b) | |
Color _shade(IP i, Color ir) | |
{ | |
return Color(); | |
} | |
double shadowCoeff(vec3 p) | |
{ | |
return 1.0; | |
} | |
struct vec3 | |
{ | |
double x, y, z; | |
vec3(int _x=0, int _y=0, int _z=0) : x(_x), y(_y), z(_z) {} | |
}; | |
double distance(vec3 a, vec3 b) | |
{ | |
return 0; | |
} | |
vec3 reflect(vec3 a, vec3 b) | |
{ | |
return vec3(); | |
} | |
vec3 refract(vec3 a, Material m) | |
{ | |
return vec3(); | |
} | |
struct Ray | |
{ | |
vec3 origin, direction; | |
}; | |
struct Material | |
{ | |
char type; // d - diffuse, s - specular, r - refractive | |
Color color; | |
double kD; | |
double kS; | |
double kR; | |
}; | |
struct Primitive | |
{ | |
Material material; | |
virtual IP intersect(Ray r) {} | |
}; | |
struct IP | |
{ | |
Primitive &primitive; | |
vec3 position; | |
vec3 normal; | |
bool operator!() { return !(normal.x == 0 && normal.y == 0 && normal.z == 0); } | |
}; | |
class Sphere : public Primitive { /*...*/ }; | |
class Box : public Primitive { /*...*/ }; | |
class Triangle : public Primitive { /*...*/ }; | |
class BezierSurface : public Primitive { /*...*/ }; | |
class Light : public Primitive { /*...*/ }; | |
int obj_cnt = 5; | |
Primitive* scene = 0; | |
void main() | |
{ | |
// setup scene | |
/*scene = new Primitive[obj_cnt]; | |
for (int i = 0; i<obj_cnt; i++) | |
scene[i] = ...;*/ | |
// setup resulting image | |
Color image[SCREEN_WIDTH][SCREEN_HEIGHT]; | |
// foreach pixel in image | |
for (int i = 0; i<SCREEN_HEIGHT; i++) | |
for (int j = 0; j<SCREEN_WIDTH; j++) | |
{ | |
Ray r; | |
r.origin = vec3(i, j, 0); | |
r.direction = vec3(0, 0, -1); | |
// pathtrace | |
image[i][j] = trace(r, 0); | |
} | |
} | |
Color trace(Ray r, int step) | |
{ | |
// get intersection point | |
IP hit; | |
for (int idx = 0; idx<obj_cnt; idx++) | |
{ | |
IP point = scene[idx].intersect(r); | |
if (distance(hit.position, r.origin) < distance(point.position, r.origin)) | |
hit = point; | |
} | |
// nothing hit -> black | |
if (!hit || step >= STEPS) | |
return BLACK; | |
// light hit -> get light's color | |
if (typeid(hit.primitive) == typeid(Light)) | |
return hit.primitive.material.color; | |
// mesh hit | |
Color color; | |
switch(hit.primitive.material.type) | |
{ | |
case 'd':// diffuse | |
color = hit.primitive.material.color; | |
break; | |
case 's':// specular | |
Ray reflectedRay; | |
reflectedRay.origin = hit.position; | |
reflectedRay.direction = reflect(r.direction, hit.normal); | |
color = trace(reflectedRay, step + 1); | |
break; | |
case 'r':// refractive | |
Ray refractedRay; | |
refractedRay.origin = hit.position; | |
refractedRay.direction = refract(r.direction, hit.primitive.material); | |
color = trace(refractedRay, step + 1); | |
break; | |
} | |
return color * shadowCoeff(hit.position); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment