# HG changeset patch # User John Tsiombikas # Date 1376745026 -10800 # Node ID 2918358f5e6dda0f30601b4e4ba86de16501ebd3 initial commit diff -r 000000000000 -r 2918358f5e6d .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,6 @@ +\.o$ +\.d$ +\.swp$ +\.a$ +\.so\. +\.dylib$ diff -r 000000000000 -r 2918358f5e6d Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,51 @@ +# ----- options ----- +PREFIX = /usr/local +dbg = -g +opt = -O0 +# ------------------- + +src = $(wildcard src/*.cc) +obj = $(src:.cc=.o) +dep = $(obj:.o=.d) + +name = goat3d +so_major = 0 +so_minor = 1 + +lib_a = lib$(name).a + +ifeq ($(shell uname -s), Darwin) + lib_so = lib$(name).dylib + shared = -dynamiclib +else + devlink = lib$(name).so + soname = lib$(name).so.$(so_major) + lib_so = lib$(name).so.$(so_major).$(so_minor) + + shared = -shared -Wl,-soname=$(soname) + pic = -fPIC +endif + +CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic) + +.PHONY: all +all: $(lib_so) $(lib_a) + +$(lib_so): $(obj) + $(CXX) -o $@ $(shared) $(obj) $(LDFLAGS) + +$(lib_a): $(obj) + $(AR) rcs $@ $(obj) + +-include $(dep) + +%.d: %.cc + @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(obj) $(lib_a) $(lib_so) + +.PHONY: cleandep +cleandep: + rm -f $(dep) diff -r 000000000000 -r 2918358f5e6d src/camera.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/camera.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,8 @@ +#ifndef CAMERA_H_ +#define CAMERA_H_ + +class Camera { + // TODO +}; + +#endif // CAMERA_H_ diff -r 000000000000 -r 2918358f5e6d src/goat3d.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/goat3d.cc Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,62 @@ +#include "goat3d.h" +#include "goat3d_impl.h" + +static void delete_node_tree(Node *n); + +Scene::Scene() + : name("unnamed"), ambient(0.05, 0.05, 0.05) +{ +} + +Scene::~Scene() +{ + clear(); +} + +void Scene::clear() +{ + for(size_t i=0; iget_num_children(); i++) { + delete_node_tree(n->get_child(i)); + } + delete n; +} + +void Scene::set_name(const char *name) +{ + this->name = name; +} + +const char *Scene::get_name() const +{ + return name.c_str(); +} diff -r 000000000000 -r 2918358f5e6d src/goat3d.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/goat3d.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,54 @@ +#ifndef GOAT3D_H_ +#define GOAT3D_H_ + +#include +#include + +struct goat3d; + +struct goat3d_io { + void *cls; /* closure data */ + + size_t (*read)(void *buf, size_t bytes, void *uptr); + size_t (*write)(void *buf, size_t bytes, void *uptr); + long (*seek)(long offs, int whence, void *uptr); +}; + +struct goat3d_vec3 { float x, y, z; }; +struct goat3d_vec4 { float x, y, z, w; }; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* construction/destruction */ +struct goat3d *goat3d_create(); +void goat3d_free(struct goat3d *g); + +/* load/save */ +int goat3d_load(struct goat3d *g, const char *fname); +int goat3d_save(const struct goat3d *g, const char *fname); + +int goat3d_load_file(struct goat3d *g, FILE *fp); +int goat3d_save_file(const struct goat3d *g, FILE *fp); + +int goat3d_load_io(struct goat3d *g, struct goat3d_io *io); +int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io); + +/* misc scene properties */ +int goat3d_set_name(struct goat3d *g, const char *name); +const char *goat3d_get_name(const struct goat3d *g); + +void goat3d_set_ambient(struct goat3d *g, float x, float y, float z); + +/* helpers */ +struct goat3d_vec3 goat3d_vec3(float x, float y, float z); +struct goat3d_vec4 goat3d_vec4(float x, float y, float z, float w); + + +#ifdef __cplusplus +} +#endif + +#endif /* GOAT3D_H_ */ diff -r 000000000000 -r 2918358f5e6d src/goat3d_impl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/goat3d_impl.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,61 @@ +#ifndef GOAT3D_IMPL_H_ +#define GOAT3D_IMPL_H_ + +#include +#include "goat3d.h" +#include "vmath.h" +#include "mesh.h" +#include "light.h" +#include "camera.h" +#include "material.h" +#include "node.h" + +class Scene { +private: + std::string name; + Vector3 ambient; + + std::vector materials; + std::vector meshes; + std::vector lights; + std::vector cameras; + std::vector nodes; + +public: + Scene(); + ~Scene(); + + void clear(); + + void set_name(const char *name); + const char *get_name() const; + + void set_ambient(const Vector3 &amb); + const Vector3 &get_ambient() const; + + void add_material(Material *mat); + Material *get_material(int idx) const; + Material *get_material(const char *name) const; + + void add_mesh(Mesh *mesh); + Mesh *get_mesh(int idx) const; + Mesh *get_mesh(const char *name) const; + + void add_light(Light *light); + Light *get_light(int idx) const; + Light *get_light(const char *name) const; + + void add_camera(Camera *cam); + Camera *get_camera(int idx) const; + Camera *get_camera(const char *name) const; + + void add_node(Node *node); + Node *get_node(int idx) const; + Node *get_node(const char *name) const; + + bool load(goat3d_io *io); + bool save(goat3d_io *io) const; +}; + + +#endif // GOAT3D_IMPL_H_ diff -r 000000000000 -r 2918358f5e6d src/light.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/light.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,8 @@ +#ifndef LIGHT_H_ +#define LIGHT_H_ + +class Light { + // TODO +}; + +#endif // LIGHT_H_ diff -r 000000000000 -r 2918358f5e6d src/material.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/material.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,8 @@ +#ifndef MATERIAL_H_ +#define MATERIAL_H_ + +class Material { + // TODO +}; + +#endif // MATERIAL_H_ diff -r 000000000000 -r 2918358f5e6d src/mesh.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mesh.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,8 @@ +#ifndef MESH_H_ +#define MESH_H_ + +class Mesh { + // TODO +}; + +#endif // MESH_H_ diff -r 000000000000 -r 2918358f5e6d src/node.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/node.cc Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,95 @@ +#include +#include +#include "node.h" + +Node::Node() +{ + parent = 0; +} + +Node::~Node() +{ +} + +void Node::set_name(const char *name) +{ + this->name = name; +} + +const char *Node::get_name() const +{ + return name.c_str(); +} + +void Node::add_child(Node *c) +{ + // make sure we don't add it twice + if(std::find(children.begin(), children.end(), c) != children.end()) { + return; + } + children.push_back(c); + c->parent = this; +} + +int Node::get_num_children() const +{ + return (int)children.size(); +} + +Node *Node::get_child(int idx) const +{ + if(idx < 0 || idx >= get_num_children()) { + return 0; + } + return children[idx]; +} + +Node *Node::get_child(const char *name) const +{ + for(size_t i=0; iget_name(), name) == 0) { + return children[i]; + } + } + return 0; +} + +Node *Node::get_descendant(const char *name) const +{ + Node *c = get_child(name); + if(c) { + return c; + } + + // depth first search might not be ideal in this case, but it's the simplest + for(size_t i=0; iget_descendant(name))) { + return c; + } + } + return 0; +} + +Node *Node::get_parent() const +{ + return parent; +} + +Node *Node::get_ancestor(const char *name) const +{ + Node *n = (Node*)this; + + if(!name) { + // just return the root + while(n->parent) { + n = n->parent; + } + return n; + } + + // otherwise we're looking for a specific ancestor + while(n && strcmp(n->get_name(), name) != 0) { + n = n->parent; + } + return n; +} diff -r 000000000000 -r 2918358f5e6d src/node.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/node.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,32 @@ +#ifndef NODE_H_ +#define NODE_H_ + +#include +#include + +class Node { +private: + std::string name; + Node *parent; + std::vector children; + +public: + Node(); + virtual ~Node(); + + virtual void set_name(const char *name); + virtual const char *get_name() const; + + virtual void add_child(Node *c); + virtual int get_num_children() const; + + virtual Node *get_child(int idx) const; + virtual Node *get_child(const char *name) const; + virtual Node *get_descendant(const char *name) const; + + virtual Node *get_parent() const; + // passing 0 will return the root + virtual Node *get_ancestor(const char *name) const; +}; + +#endif // NODE_H_ diff -r 000000000000 -r 2918358f5e6d src/object.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.cc Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,27 @@ +#include "object.h" + +Object::Object() +{ + material = 0; + mesh = 0; +} + +void Object::set_mesh(Mesh *mesh) +{ + this->mesh = mesh; +} + +Mesh *Object::get_mesh() const +{ + return mesh; +} + +void Object::set_material(Material *mtl) +{ + material = mtl; +} + +Material *Object::get_material() const +{ + return material; +} diff -r 000000000000 -r 2918358f5e6d src/object.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,23 @@ +#ifndef OBJECT_H_ +#define OBJECT_H_ + +#include "node.h" +#include "mesh.h" +#include "material.h" + +class Object : public Node { +private: + Material *material; + Mesh *mesh; + +public: + Object(); + + void set_mesh(Mesh *mesh); + Mesh *get_mesh() const; + + void set_material(Material *mtl); + Material *get_material() const; +}; + +#endif // OBJECT_H_ diff -r 000000000000 -r 2918358f5e6d src/vmath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vmath.h Sat Aug 17 16:10:26 2013 +0300 @@ -0,0 +1,29 @@ +#ifndef VMATH_H_ +#define VMATH_H_ + +class Vector3 { +public: + float x, y, z; + + Vector3() : x(0), y(0), z(0) {} + Vector3(float x, float y, float z) { + this->x = x; + this->y = y; + this->z = z; + } +}; + +class Vector4 { +public: + float x, y, z, w; + + Vector4() : x(0), y(0), z(0), w(1) {} + Vector4(float x, float y, float z, float w) { + this->x = x; + this->y = y; + this->z = z; + this->w = w; + } +}; + +#endif // VMATH_H_