Update object.cc for C++
This commit is contained in:
parent
6ad2f16acd
commit
a02e31952e
2 changed files with 33 additions and 151 deletions
|
@ -14,156 +14,39 @@
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
|
||||||
|
|
||||||
struct _Object {
|
|
||||||
ObjectType type;
|
|
||||||
Vector3 location;
|
|
||||||
Texture *texture;
|
|
||||||
void *shape;
|
|
||||||
|
|
||||||
int (*does_intersect)(Object *obj, Ray ray, float **t);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct _Sphere {
|
|
||||||
float radius;
|
|
||||||
} Sphere;
|
|
||||||
|
|
||||||
static int sphere_does_intersect(Object *obj, Ray ray, float **t);
|
static int sphere_does_intersect(Object *obj, Ray ray, float **t);
|
||||||
static int sphere_point_lies_on_surface(Object *obj, Vector3 p);
|
static int sphere_point_lies_on_surface(Object *obj, Vector3 p);
|
||||||
static Vector3 sphere_compute_normal(Object *obj, Vector3 p);
|
static Vector3 sphere_compute_normal(Object *obj, Vector3 p);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* object_init ---
|
* Object::Object --
|
||||||
*
|
*
|
||||||
* Create a new object of the given type.
|
* Default constructor. Create a new Object with an origin at (0, 0, 0).
|
||||||
*/
|
*/
|
||||||
Object *
|
Object::Object()
|
||||||
object_init(ObjectType type)
|
: origin()
|
||||||
{
|
{ }
|
||||||
Object *obj = malloc(sizeof(Object));
|
|
||||||
if (obj == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
obj->type = type;
|
|
||||||
obj->location = Vector3Zero;
|
|
||||||
obj->texture = NULL;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case ObjectTypeSphere: {
|
|
||||||
Sphere *s = malloc(sizeof(Sphere));
|
|
||||||
if (s == NULL) {
|
|
||||||
// TODO: DANGER! WILL ROBINSON!
|
|
||||||
}
|
|
||||||
obj->shape = s;
|
|
||||||
s->radius = 0.0;
|
|
||||||
obj->does_intersect = sphere_does_intersect;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* object_destroy --
|
* Object::get_origin --
|
||||||
|
* Object::set_origin --
|
||||||
*
|
*
|
||||||
* Destroy the given object.
|
* Get and set the Object's origin.
|
||||||
*/
|
|
||||||
void
|
|
||||||
object_destroy(Object *obj)
|
|
||||||
{
|
|
||||||
assert(obj != NULL);
|
|
||||||
assert(obj->shape != NULL);
|
|
||||||
|
|
||||||
free(obj->shape);
|
|
||||||
free(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* object_get_location --
|
|
||||||
* object_set_location --
|
|
||||||
*
|
|
||||||
* Get and set the location of the object.
|
|
||||||
*/
|
*/
|
||||||
Vector3
|
Vector3
|
||||||
object_get_location(Object *obj)
|
Object::get_origin()
|
||||||
{
|
{
|
||||||
assert(obj != NULL);
|
return origin;
|
||||||
return obj->location;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
object_set_location(Object *obj, Vector3 location)
|
Object::set_origin(Vector3 v)
|
||||||
{
|
{
|
||||||
assert(obj != NULL);
|
origin = v;
|
||||||
obj->location = location;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* object_get_texture --
|
|
||||||
* object_set_texture --
|
|
||||||
*
|
|
||||||
* Get and set the object's texture.
|
|
||||||
*/
|
|
||||||
Texture *
|
|
||||||
object_get_texture(Object *obj)
|
|
||||||
{
|
|
||||||
assert(obj != NULL);
|
|
||||||
return obj->texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
object_set_texture(Object *obj, Texture *tex)
|
|
||||||
{
|
|
||||||
assert(obj != NULL);
|
|
||||||
obj->texture = tex;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* object_does_intersect --
|
|
||||||
*
|
|
||||||
* Determine if a ray intersects with the object.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
object_does_intersect(Object *obj, Ray ray, float **t)
|
|
||||||
{
|
|
||||||
assert(obj != NULL && obj->does_intersect != NULL);
|
|
||||||
return obj->does_intersect(obj, ray, t);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Sphere functions
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* object_sphere_get_radius --
|
|
||||||
* object_sphere_set_radius --
|
|
||||||
*
|
|
||||||
* Get and set the radius of a Sphere object.
|
|
||||||
*/
|
|
||||||
float
|
|
||||||
object_sphere_get_radius(Object *obj)
|
|
||||||
{
|
|
||||||
assert(obj != NULL && obj->type == ObjectTypeSphere);
|
|
||||||
return ((Sphere *)obj->shape)->radius;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
object_sphere_set_radius(Object *obj, float r)
|
|
||||||
{
|
|
||||||
assert(obj != NULL && obj->type == ObjectTypeSphere);
|
|
||||||
((Sphere *)obj->shape)->radius = r;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* sphere_does_intersect --
|
* sphere_does_intersect --
|
||||||
*
|
*
|
43
src/object.h
43
src/object.h
|
@ -1,38 +1,37 @@
|
||||||
/* object.h
|
/* object.h
|
||||||
*
|
*
|
||||||
* Declaration of scene Objects.
|
* Declaration of abstract, top-level scene objects. The Object class is the top of this hierarchy. All other scene
|
||||||
|
* objects are based on it. The Shape class defines a visible shape in the scene.
|
||||||
*
|
*
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef __OBJECT_H__
|
||||||
#ifndef __OBJECT_H
|
#define __OBJECT_H__
|
||||||
#define __OBJECT_H
|
|
||||||
|
|
||||||
#include "basics.h"
|
#include "basics.h"
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
|
|
||||||
|
|
||||||
typedef enum {
|
class Object
|
||||||
ObjectTypeSphere = 1,
|
{
|
||||||
} ObjectType;
|
public:
|
||||||
|
Object();
|
||||||
|
|
||||||
typedef struct _Object Object;
|
Vector3 get_origin();
|
||||||
|
void set_origin(Vector3 v);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector3 origin;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Object *object_init(ObjectType type);
|
class Shape
|
||||||
void object_destroy(Object *obj);
|
: public Object
|
||||||
|
{
|
||||||
Vector3 object_get_location(Object *obj);
|
public:
|
||||||
void object_set_location(Object *obj, Vector3 location);
|
virtual int does_intersect(const Ray &ray, float **t) = 0;
|
||||||
Texture *object_get_texture(Object *obj);
|
virtual Vector3 compute_normal(const Vector3 &p) = 0;
|
||||||
void object_set_texture(Object *obj, Texture *tex);
|
};
|
||||||
|
|
||||||
int object_does_intersect(Object *obj, Ray ray, float **t);
|
|
||||||
|
|
||||||
// Sphere methods
|
|
||||||
float object_sphere_get_radius(Object *obj);
|
|
||||||
void object_sphere_set_radius(Object *obj, float r);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue