Yay!
This commit is contained in:
parent
aa694e8128
commit
72c0d1475e
3 changed files with 48 additions and 13 deletions
|
@ -34,8 +34,8 @@ public:
|
||||||
Shape();
|
Shape();
|
||||||
Shape(Vector3 o);
|
Shape(Vector3 o);
|
||||||
|
|
||||||
virtual int does_intersect(const Ray &ray, float **t) = 0;
|
virtual int does_intersect(const Ray &ray, float **t) const = 0;
|
||||||
virtual Vector3 compute_normal(const Vector3 &p) = 0;
|
virtual Vector3 compute_normal(const Vector3 &p) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
44
src/scene.cc
44
src/scene.cc
|
@ -6,13 +6,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
#include "basics.h"
|
#include "basics.h"
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
#include "scene.h"
|
#include "scene.h"
|
||||||
|
#include "writer.h"
|
||||||
|
|
||||||
|
|
||||||
Scene::Scene()
|
Scene::Scene()
|
||||||
: height(640), width(480),
|
: width(640), height(480),
|
||||||
pixels(NULL)
|
pixels(NULL)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
@ -34,13 +37,37 @@ Scene::is_rendered()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
Scene::get_width()
|
||||||
|
const
|
||||||
|
{
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
Scene::get_height()
|
||||||
|
const
|
||||||
|
{
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const Color *
|
||||||
|
Scene::get_pixels()
|
||||||
|
const
|
||||||
|
{
|
||||||
|
return pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* scene_load --
|
* scene_load --
|
||||||
*
|
*
|
||||||
* Load scene objects into this Scene from the given file.
|
* Load scene objects into this Scene from the given file.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Scene::read(FILE *file)
|
Scene::read(const std::string &filename)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,8 +77,10 @@ Scene::read(FILE *file)
|
||||||
* Write a rendered scene to the given file.
|
* Write a rendered scene to the given file.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Scene::write(FILE *file)
|
Scene::write(Writer &writer, const std::string &filename)
|
||||||
{ }
|
{
|
||||||
|
writer.write_scene(*this, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -77,7 +106,8 @@ Scene::render()
|
||||||
d = Vector3(0, 0, 1);
|
d = Vector3(0, 0, 1);
|
||||||
d.normalize();
|
d.normalize();
|
||||||
primary_ray = Ray(o, d);
|
primary_ray = Ray(o, d);
|
||||||
pixels[y * width + x] = trace_ray(primary_ray, 0);
|
Color c = trace_ray(primary_ray, 0);
|
||||||
|
pixels[y * width + x] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,8 +125,6 @@ Scene::add_shape(Shape *shape)
|
||||||
Color
|
Color
|
||||||
Scene::trace_ray(const Ray &ray, const int depth)
|
Scene::trace_ray(const Ray &ray, const int depth)
|
||||||
{
|
{
|
||||||
Color out_color = Color::Black;
|
|
||||||
|
|
||||||
// Find intersections of this ray with objects in the scene.
|
// Find intersections of this ray with objects in the scene.
|
||||||
Shape *intersected_shape = NULL;
|
Shape *intersected_shape = NULL;
|
||||||
float *t = NULL;
|
float *t = NULL;
|
||||||
|
@ -117,7 +145,7 @@ Scene::trace_ray(const Ray &ray, const int depth)
|
||||||
|
|
||||||
// If there was no intersection, return black.
|
// If there was no intersection, return black.
|
||||||
if (intersected_shape == NULL) {
|
if (intersected_shape == NULL) {
|
||||||
return out_color;
|
return Color::Black;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Lighting.
|
// TODO: Lighting.
|
||||||
|
|
13
src/scene.h
13
src/scene.h
|
@ -12,8 +12,12 @@
|
||||||
#define __SCENE_H__
|
#define __SCENE_H__
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <string>
|
||||||
|
#include "basics.h"
|
||||||
|
|
||||||
|
|
||||||
class Shape;
|
class Shape;
|
||||||
|
class Writer;
|
||||||
|
|
||||||
|
|
||||||
class Scene
|
class Scene
|
||||||
|
@ -23,9 +27,12 @@ public:
|
||||||
~Scene();
|
~Scene();
|
||||||
|
|
||||||
bool is_rendered() const;
|
bool is_rendered() const;
|
||||||
|
int get_width() const;
|
||||||
|
int get_height() const;
|
||||||
|
const Color *get_pixels() const;
|
||||||
|
|
||||||
void read(FILE *f);
|
void read(const std::string &filename);
|
||||||
void write(FILE *f);
|
void write(Writer &writer, const std::string &filename);
|
||||||
void render();
|
void render();
|
||||||
|
|
||||||
void add_shape(Shape *obj);
|
void add_shape(Shape *obj);
|
||||||
|
@ -33,7 +40,7 @@ public:
|
||||||
private:
|
private:
|
||||||
Color trace_ray(const Ray &ray, const int depth);
|
Color trace_ray(const Ray &ray, const int depth);
|
||||||
|
|
||||||
int height, width;
|
int width, height;
|
||||||
std::list<Shape *> shapes;
|
std::list<Shape *> shapes;
|
||||||
|
|
||||||
bool _is_rendered;
|
bool _is_rendered;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue