goat3d

changeset 0:2918358f5e6d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 17 Aug 2013 16:10:26 +0300
parents
children e46529a5d057
files .hgignore Makefile src/camera.h src/goat3d.cc src/goat3d.h src/goat3d_impl.h src/light.h src/material.h src/mesh.h src/node.cc src/node.h src/object.cc src/object.h src/vmath.h
diffstat 14 files changed, 472 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Sat Aug 17 16:10:26 2013 +0300
     1.3 @@ -0,0 +1,6 @@
     1.4 +\.o$
     1.5 +\.d$
     1.6 +\.swp$
     1.7 +\.a$
     1.8 +\.so\.
     1.9 +\.dylib$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Sat Aug 17 16:10:26 2013 +0300
     2.3 @@ -0,0 +1,51 @@
     2.4 +# ----- options -----
     2.5 +PREFIX = /usr/local
     2.6 +dbg = -g
     2.7 +opt = -O0
     2.8 +# -------------------
     2.9 +
    2.10 +src = $(wildcard src/*.cc)
    2.11 +obj = $(src:.cc=.o)
    2.12 +dep = $(obj:.o=.d)
    2.13 +
    2.14 +name = goat3d
    2.15 +so_major = 0
    2.16 +so_minor = 1
    2.17 +
    2.18 +lib_a = lib$(name).a
    2.19 +
    2.20 +ifeq ($(shell uname -s), Darwin)
    2.21 +	lib_so = lib$(name).dylib
    2.22 +	shared = -dynamiclib
    2.23 +else
    2.24 +	devlink = lib$(name).so
    2.25 +	soname = lib$(name).so.$(so_major)
    2.26 +	lib_so = lib$(name).so.$(so_major).$(so_minor)
    2.27 +
    2.28 +	shared = -shared -Wl,-soname=$(soname)
    2.29 +	pic = -fPIC
    2.30 +endif
    2.31 +
    2.32 +CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic)
    2.33 +
    2.34 +.PHONY: all
    2.35 +all: $(lib_so) $(lib_a)
    2.36 +
    2.37 +$(lib_so): $(obj)
    2.38 +	$(CXX) -o $@ $(shared) $(obj) $(LDFLAGS)
    2.39 +
    2.40 +$(lib_a): $(obj)
    2.41 +	$(AR) rcs $@ $(obj)
    2.42 +
    2.43 +-include $(dep)
    2.44 +
    2.45 +%.d: %.cc
    2.46 +	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.47 +
    2.48 +.PHONY: clean
    2.49 +clean:
    2.50 +	rm -f $(obj) $(lib_a) $(lib_so)
    2.51 +
    2.52 +.PHONY: cleandep
    2.53 +cleandep:
    2.54 +	rm -f $(dep)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/camera.h	Sat Aug 17 16:10:26 2013 +0300
     3.3 @@ -0,0 +1,8 @@
     3.4 +#ifndef CAMERA_H_
     3.5 +#define CAMERA_H_
     3.6 +
     3.7 +class Camera {
     3.8 +	// TODO
     3.9 +};
    3.10 +
    3.11 +#endif	// CAMERA_H_
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/goat3d.cc	Sat Aug 17 16:10:26 2013 +0300
     4.3 @@ -0,0 +1,62 @@
     4.4 +#include "goat3d.h"
     4.5 +#include "goat3d_impl.h"
     4.6 +
     4.7 +static void delete_node_tree(Node *n);
     4.8 +
     4.9 +Scene::Scene()
    4.10 +	: name("unnamed"), ambient(0.05, 0.05, 0.05)
    4.11 +{
    4.12 +}
    4.13 +
    4.14 +Scene::~Scene()
    4.15 +{
    4.16 +	clear();
    4.17 +}
    4.18 +
    4.19 +void Scene::clear()
    4.20 +{
    4.21 +	for(size_t i=0; i<materials.size(); i++) {
    4.22 +		delete materials[i];
    4.23 +	}
    4.24 +	materials.clear();
    4.25 +
    4.26 +	for(size_t i=0; i<meshes.size(); i++) {
    4.27 +		delete meshes[i];
    4.28 +	}
    4.29 +	meshes.clear();
    4.30 +
    4.31 +	for(size_t i=0; i<lights.size(); i++) {
    4.32 +		delete lights[i];
    4.33 +	}
    4.34 +	lights.clear();
    4.35 +
    4.36 +	for(size_t i=0; i<cameras.size(); i++) {
    4.37 +		delete cameras[i];
    4.38 +	}
    4.39 +	cameras.clear();
    4.40 +
    4.41 +	for(size_t i=0; i<nodes.size(); i++) {
    4.42 +		delete_node_tree(nodes[i]);
    4.43 +	}
    4.44 +	nodes.clear();
    4.45 +
    4.46 +	name = "unnamed";
    4.47 +}
    4.48 +
    4.49 +static void delete_node_tree(Node *n)
    4.50 +{
    4.51 +	for(int i=0; i<n->get_num_children(); i++) {
    4.52 +		delete_node_tree(n->get_child(i));
    4.53 +	}
    4.54 +	delete n;
    4.55 +}
    4.56 +
    4.57 +void Scene::set_name(const char *name)
    4.58 +{
    4.59 +	this->name = name;
    4.60 +}
    4.61 +
    4.62 +const char *Scene::get_name() const
    4.63 +{
    4.64 +	return name.c_str();
    4.65 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/goat3d.h	Sat Aug 17 16:10:26 2013 +0300
     5.3 @@ -0,0 +1,54 @@
     5.4 +#ifndef GOAT3D_H_
     5.5 +#define GOAT3D_H_
     5.6 +
     5.7 +#include <stdio.h>
     5.8 +#include <stdlib.h>
     5.9 +
    5.10 +struct goat3d;
    5.11 +
    5.12 +struct goat3d_io {
    5.13 +	void *cls;	/* closure data */
    5.14 +
    5.15 +	size_t (*read)(void *buf, size_t bytes, void *uptr);
    5.16 +	size_t (*write)(void *buf, size_t bytes, void *uptr);
    5.17 +	long (*seek)(long offs, int whence, void *uptr);
    5.18 +};
    5.19 +
    5.20 +struct goat3d_vec3 { float x, y, z; };
    5.21 +struct goat3d_vec4 { float x, y, z, w; };
    5.22 +
    5.23 +
    5.24 +#ifdef __cplusplus
    5.25 +extern "C" {
    5.26 +#endif
    5.27 +
    5.28 +/* construction/destruction */
    5.29 +struct goat3d *goat3d_create();
    5.30 +void goat3d_free(struct goat3d *g);
    5.31 +
    5.32 +/* load/save */
    5.33 +int goat3d_load(struct goat3d *g, const char *fname);
    5.34 +int goat3d_save(const struct goat3d *g, const char *fname);
    5.35 +
    5.36 +int goat3d_load_file(struct goat3d *g, FILE *fp);
    5.37 +int goat3d_save_file(const struct goat3d *g, FILE *fp);
    5.38 +
    5.39 +int goat3d_load_io(struct goat3d *g, struct goat3d_io *io);
    5.40 +int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io);
    5.41 +
    5.42 +/* misc scene properties */
    5.43 +int goat3d_set_name(struct goat3d *g, const char *name);
    5.44 +const char *goat3d_get_name(const struct goat3d *g);
    5.45 +
    5.46 +void goat3d_set_ambient(struct goat3d *g, float x, float y, float z);
    5.47 +
    5.48 +/* helpers */
    5.49 +struct goat3d_vec3 goat3d_vec3(float x, float y, float z);
    5.50 +struct goat3d_vec4 goat3d_vec4(float x, float y, float z, float w);
    5.51 +
    5.52 +
    5.53 +#ifdef __cplusplus
    5.54 +}
    5.55 +#endif
    5.56 +
    5.57 +#endif	/* GOAT3D_H_ */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/goat3d_impl.h	Sat Aug 17 16:10:26 2013 +0300
     6.3 @@ -0,0 +1,61 @@
     6.4 +#ifndef GOAT3D_IMPL_H_
     6.5 +#define GOAT3D_IMPL_H_
     6.6 +
     6.7 +#include <string>
     6.8 +#include "goat3d.h"
     6.9 +#include "vmath.h"
    6.10 +#include "mesh.h"
    6.11 +#include "light.h"
    6.12 +#include "camera.h"
    6.13 +#include "material.h"
    6.14 +#include "node.h"
    6.15 +
    6.16 +class Scene {
    6.17 +private:
    6.18 +	std::string name;
    6.19 +	Vector3 ambient;
    6.20 +
    6.21 +	std::vector<Material*> materials;
    6.22 +	std::vector<Mesh*> meshes;
    6.23 +	std::vector<Light*> lights;
    6.24 +	std::vector<Camera*> cameras;
    6.25 +	std::vector<Node*> nodes;
    6.26 +
    6.27 +public:
    6.28 +	Scene();
    6.29 +	~Scene();
    6.30 +
    6.31 +	void clear();
    6.32 +
    6.33 +	void set_name(const char *name);
    6.34 +	const char *get_name() const;
    6.35 +
    6.36 +	void set_ambient(const Vector3 &amb);
    6.37 +	const Vector3 &get_ambient() const;
    6.38 +
    6.39 +	void add_material(Material *mat);
    6.40 +	Material *get_material(int idx) const;
    6.41 +	Material *get_material(const char *name) const;
    6.42 +
    6.43 +	void add_mesh(Mesh *mesh);
    6.44 +	Mesh *get_mesh(int idx) const;
    6.45 +	Mesh *get_mesh(const char *name) const;
    6.46 +
    6.47 +	void add_light(Light *light);
    6.48 +	Light *get_light(int idx) const;
    6.49 +	Light *get_light(const char *name) const;
    6.50 +
    6.51 +	void add_camera(Camera *cam);
    6.52 +	Camera *get_camera(int idx) const;
    6.53 +	Camera *get_camera(const char *name) const;
    6.54 +
    6.55 +	void add_node(Node *node);
    6.56 +	Node *get_node(int idx) const;
    6.57 +	Node *get_node(const char *name) const;
    6.58 +
    6.59 +	bool load(goat3d_io *io);
    6.60 +	bool save(goat3d_io *io) const;
    6.61 +};
    6.62 +
    6.63 +
    6.64 +#endif	// GOAT3D_IMPL_H_
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/light.h	Sat Aug 17 16:10:26 2013 +0300
     7.3 @@ -0,0 +1,8 @@
     7.4 +#ifndef LIGHT_H_
     7.5 +#define LIGHT_H_
     7.6 +
     7.7 +class Light {
     7.8 +	// TODO
     7.9 +};
    7.10 +
    7.11 +#endif	// LIGHT_H_
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/material.h	Sat Aug 17 16:10:26 2013 +0300
     8.3 @@ -0,0 +1,8 @@
     8.4 +#ifndef MATERIAL_H_
     8.5 +#define MATERIAL_H_
     8.6 +
     8.7 +class Material {
     8.8 +	// TODO
     8.9 +};
    8.10 +
    8.11 +#endif	// MATERIAL_H_
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/mesh.h	Sat Aug 17 16:10:26 2013 +0300
     9.3 @@ -0,0 +1,8 @@
     9.4 +#ifndef MESH_H_
     9.5 +#define MESH_H_
     9.6 +
     9.7 +class Mesh {
     9.8 +	// TODO
     9.9 +};
    9.10 +
    9.11 +#endif	// MESH_H_
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/node.cc	Sat Aug 17 16:10:26 2013 +0300
    10.3 @@ -0,0 +1,95 @@
    10.4 +#include <algorithm>
    10.5 +#include <string.h>
    10.6 +#include "node.h"
    10.7 +
    10.8 +Node::Node()
    10.9 +{
   10.10 +	parent = 0;
   10.11 +}
   10.12 +
   10.13 +Node::~Node()
   10.14 +{
   10.15 +}
   10.16 +
   10.17 +void Node::set_name(const char *name)
   10.18 +{
   10.19 +	this->name = name;
   10.20 +}
   10.21 +
   10.22 +const char *Node::get_name() const
   10.23 +{
   10.24 +	return name.c_str();
   10.25 +}
   10.26 +
   10.27 +void Node::add_child(Node *c)
   10.28 +{
   10.29 +	// make sure we don't add it twice
   10.30 +	if(std::find(children.begin(), children.end(), c) != children.end()) {
   10.31 +		return;
   10.32 +	}
   10.33 +	children.push_back(c);
   10.34 +	c->parent = this;
   10.35 +}
   10.36 +
   10.37 +int Node::get_num_children() const
   10.38 +{
   10.39 +	return (int)children.size();
   10.40 +}
   10.41 +
   10.42 +Node *Node::get_child(int idx) const
   10.43 +{
   10.44 +	if(idx < 0 || idx >= get_num_children()) {
   10.45 +		return 0;
   10.46 +	}
   10.47 +	return children[idx];
   10.48 +}
   10.49 +
   10.50 +Node *Node::get_child(const char *name) const
   10.51 +{
   10.52 +	for(size_t i=0; i<children.size(); i++) {
   10.53 +		if(strcmp(children[i]->get_name(), name) == 0) {
   10.54 +			return children[i];
   10.55 +		}
   10.56 +	}
   10.57 +	return 0;
   10.58 +}
   10.59 +
   10.60 +Node *Node::get_descendant(const char *name) const
   10.61 +{
   10.62 +	Node *c = get_child(name);
   10.63 +	if(c) {
   10.64 +		return c;
   10.65 +	}
   10.66 +
   10.67 +	// depth first search might not be ideal in this case, but it's the simplest
   10.68 +	for(size_t i=0; i<children.size(); i++) {
   10.69 +		if((c = children[i]->get_descendant(name))) {
   10.70 +			return c;
   10.71 +		}
   10.72 +	}
   10.73 +	return 0;
   10.74 +}
   10.75 +
   10.76 +Node *Node::get_parent() const
   10.77 +{
   10.78 +	return parent;
   10.79 +}
   10.80 +
   10.81 +Node *Node::get_ancestor(const char *name) const
   10.82 +{
   10.83 +	Node *n = (Node*)this;
   10.84 +
   10.85 +	if(!name) {
   10.86 +		// just return the root
   10.87 +		while(n->parent) {
   10.88 +			n = n->parent;
   10.89 +		}
   10.90 +		return n;
   10.91 +	}
   10.92 +
   10.93 +	// otherwise we're looking for a specific ancestor
   10.94 +	while(n && strcmp(n->get_name(), name) != 0) {
   10.95 +		n = n->parent;
   10.96 +	}
   10.97 +	return n;
   10.98 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/node.h	Sat Aug 17 16:10:26 2013 +0300
    11.3 @@ -0,0 +1,32 @@
    11.4 +#ifndef NODE_H_
    11.5 +#define NODE_H_
    11.6 +
    11.7 +#include <string>
    11.8 +#include <vector>
    11.9 +
   11.10 +class Node {
   11.11 +private:
   11.12 +	std::string name;
   11.13 +	Node *parent;
   11.14 +	std::vector<Node*> children;
   11.15 +
   11.16 +public:
   11.17 +	Node();
   11.18 +	virtual ~Node();
   11.19 +
   11.20 +	virtual void set_name(const char *name);
   11.21 +	virtual const char *get_name() const;
   11.22 +
   11.23 +	virtual void add_child(Node *c);
   11.24 +	virtual int get_num_children() const;
   11.25 +
   11.26 +	virtual Node *get_child(int idx) const;
   11.27 +	virtual Node *get_child(const char *name) const;
   11.28 +	virtual Node *get_descendant(const char *name) const;
   11.29 +
   11.30 +	virtual Node *get_parent() const;
   11.31 +	// passing 0 will return the root
   11.32 +	virtual Node *get_ancestor(const char *name) const;
   11.33 +};
   11.34 +
   11.35 +#endif	// NODE_H_
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/object.cc	Sat Aug 17 16:10:26 2013 +0300
    12.3 @@ -0,0 +1,27 @@
    12.4 +#include "object.h"
    12.5 +
    12.6 +Object::Object()
    12.7 +{
    12.8 +	material = 0;
    12.9 +	mesh = 0;
   12.10 +}
   12.11 +
   12.12 +void Object::set_mesh(Mesh *mesh)
   12.13 +{
   12.14 +	this->mesh = mesh;
   12.15 +}
   12.16 +
   12.17 +Mesh *Object::get_mesh() const
   12.18 +{
   12.19 +	return mesh;
   12.20 +}
   12.21 +
   12.22 +void Object::set_material(Material *mtl)
   12.23 +{
   12.24 +	material = mtl;
   12.25 +}
   12.26 +
   12.27 +Material *Object::get_material() const
   12.28 +{
   12.29 +	return material;
   12.30 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/object.h	Sat Aug 17 16:10:26 2013 +0300
    13.3 @@ -0,0 +1,23 @@
    13.4 +#ifndef OBJECT_H_
    13.5 +#define OBJECT_H_
    13.6 +
    13.7 +#include "node.h"
    13.8 +#include "mesh.h"
    13.9 +#include "material.h"
   13.10 +
   13.11 +class Object : public Node {
   13.12 +private:
   13.13 +	Material *material;
   13.14 +	Mesh *mesh;
   13.15 +
   13.16 +public:
   13.17 +	Object();
   13.18 +
   13.19 +	void set_mesh(Mesh *mesh);
   13.20 +	Mesh *get_mesh() const;
   13.21 +
   13.22 +	void set_material(Material *mtl);
   13.23 +	Material *get_material() const;
   13.24 +};
   13.25 +
   13.26 +#endif	// OBJECT_H_
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/vmath.h	Sat Aug 17 16:10:26 2013 +0300
    14.3 @@ -0,0 +1,29 @@
    14.4 +#ifndef VMATH_H_
    14.5 +#define VMATH_H_
    14.6 +
    14.7 +class Vector3 {
    14.8 +public:
    14.9 +	float x, y, z;
   14.10 +
   14.11 +	Vector3() : x(0), y(0), z(0) {}
   14.12 +	Vector3(float x, float y, float z) {
   14.13 +		this->x = x;
   14.14 +		this->y = y;
   14.15 +		this->z = z;
   14.16 +	}
   14.17 +};
   14.18 +
   14.19 +class Vector4 {
   14.20 +public:
   14.21 +	float x, y, z, w;
   14.22 +
   14.23 +	Vector4() : x(0), y(0), z(0), w(1) {}
   14.24 +	Vector4(float x, float y, float z, float w) {
   14.25 +		this->x = x;
   14.26 +		this->y = y;
   14.27 +		this->z = z;
   14.28 +		this->w = w;
   14.29 +	}
   14.30 +};
   14.31 +
   14.32 +#endif	// VMATH_H_