fixed some stuff

rework
Anton Lydike 5 years ago
parent 66f483e920
commit f848658aff

@ -20,7 +20,7 @@ Color circle_color(Point hit, Point direction, SceneObject *self) {
Color color = self->color; Color color = self->color;
if (angle > 90) angle = 180 - angle ; // clamp angle to 0-90 if (angle > 90) angle = 180 - angle ; // clamp angle to 0-90
color = color_mix(color, color_new(0,0,0), 1 - (angle / (double) 90)); color = color_mix(color, color_new(0,0,0), 1 - (angle / (double) 120));
return color; return color;
} }
@ -32,15 +32,18 @@ SceneObject circle_new(Point loc, double radius) {
so.args[0] = radius; so.args[0] = radius;
so.distance = circle_dist; so.distance = circle_dist;
so.get_color = circle_color; so.get_color = circle_color;
so.color = color_new(255,0,0); so.color = color_new(255,255,255);
return so; return so;
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
int threads = 1; int threads = 1;
Camera cam; Camera cam;
cam.fov = 90; cam.fov = 90;
camera_set_looking_at(&cam, pt_new(-.6,0,0), pt_new(-.5,1,0)); camera_set_looking_at(&cam, pt_new(0,0,0), pt_new(1,1,1));
if (argc > 1) { if (argc > 1) {
threads = atoi(argv[1]); threads = atoi(argv[1]);
@ -49,11 +52,11 @@ int main(int argc, char* argv[]) {
printf("threads: %d\n", threads); printf("threads: %d\n", threads);
// create basic scene with up to 10 objects // create basic scene with up to 10 objects
Scene scene = scene_new(800, 600, 10); Scene scene = scene_new(1920, 1080, 10);
scene.max_steps = 64; scene.max_steps = 256;
scene.threshold = 0.02; scene.threshold = 0.02;
scene_add_obj(&scene, circle_new(pt_new(0,1,0), .2)); scene_add_obj(&scene, circle_new(pt_new(1,1,1), .2));
//scene_add_obj(&scene, circle_new(pt_new(0,2,0), 0.5)); //scene_add_obj(&scene, circle_new(pt_new(0,2,0), 0.5));
//scene_add_obj(&scene, circle_new(pt_new(0,-2,0), 0.5)); //scene_add_obj(&scene, circle_new(pt_new(0,-2,0), 0.5));

Binary file not shown.

@ -8,6 +8,12 @@
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#endif #endif
struct __myvec;
struct __mymtrx;
struct __mycam;
struct __myobject;
struct __myscene;
typedef struct __myvec { typedef struct __myvec {
double x; double x;
double y; double y;
@ -19,10 +25,10 @@ typedef struct __mymtrx {
} Matrix; } Matrix;
inline Point pt_new(double x, double y, double z); inline Point pt_new(double x, double y, double z);
Point pt_scale(Point pt, double length); inline Point pt_scale(Point pt, double length);
inline Point pt_normalize(Point pt); inline Point pt_normalize(Point pt);
inline double pt_length(Point pt); inline double pt_length(Point pt);
void pt_add(Point* pt, Point add); inline void pt_add(Point* pt, Point add);
inline void pt_sub(Point* pt, Point sub); inline void pt_sub(Point* pt, Point sub);
inline double pt_dist(Point p1, Point p2); inline double pt_dist(Point p1, Point p2);
inline Point pt_mod(Point pt, double mod); inline Point pt_mod(Point pt, double mod);
@ -53,6 +59,7 @@ typedef struct __myobject {
double (*distance)(Point, struct __myobject *); double (*distance)(Point, struct __myobject *);
Color (*get_color)(Point, Point, struct __myobject *); Color (*get_color)(Point, Point, struct __myobject *);
Color color; Color color;
struct __myscene* scene;
} SceneObject; } SceneObject;
typedef struct __myscene { typedef struct __myscene {

@ -11,7 +11,7 @@ inline Point pt_new(double x, double y, double z) {
} }
// scale vector to length // scale vector to length
Point pt_scale(Point pt, double length) { inline Point pt_scale(Point pt, double length) {
double f = length / pt_length(pt); double f = length / pt_length(pt);
return pt_new( return pt_new(
pt.x * f, pt.x * f,
@ -42,7 +42,7 @@ inline double pt_length(Point pt) {
} }
// add the vector add to the vector pt // add the vector add to the vector pt
void pt_add(Point* pt, Point add) { inline void pt_add(Point* pt, Point add) {
pt->x = pt->x + add.x; pt->x = pt->x + add.x;
pt->y = pt->y + add.y; pt->y = pt->y + add.y;
pt->z = pt->z + add.z; pt->z = pt->z + add.z;
@ -103,9 +103,9 @@ void pt_orthogonal_plane(Point pt, Point *span_z, Point *span_xy) {
inline Point pt_mod(Point pt, double mod) { inline Point pt_mod(Point pt, double mod) {
return pt_new( return pt_new(
fmod(pt.x, mod), fabs(fmod(pt.x, mod)),
fmod(pt.y, mod), fabs(fmod(pt.y, mod)),
fmod(pt.z, mod) fabs(fmod(pt.z, mod))
); );
} }

@ -28,7 +28,10 @@ void scene_add_obj(Scene* scene, SceneObject object) {
if (scene->object_count >= scene->allocated_space) return; // limit reached if (scene->object_count >= scene->allocated_space) return; // limit reached
// TODO realloc // TODO realloc
scene->objects[scene->object_count] = object; scene->objects[scene->object_count] = object;
// link containing scene
scene->objects[scene->object_count].scene = scene;
scene->object_count++; scene->object_count++;
} }
@ -102,7 +105,11 @@ Color march_ray(Point origin, Point direction, Scene* scene) {
// check for a hit // check for a hit
if (dist <= threshold) { if (dist <= threshold) {
// a hit! // a hit!
return closest_obj->get_color(pos, direction, closest_obj); double f = (steps / (double) scene->max_steps);
f = f * f * f * f;
Color c = closest_obj->get_color(pos, direction, closest_obj);
return color_mix(c, color_new(0,0,0), f);
} else { } else {
// a miss :( // a miss :(

Loading…
Cancel
Save