# HG changeset patch # User John Tsiombikas # Date 1398746320 -10800 # Node ID 93894c232d650171727e6cfd655a4e53dd776e1b # Parent a932848de6521cefc94574b026c7b7a437e8133a more changes across the board diff -r a932848de652 -r 93894c232d65 .clang_complete --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.clang_complete Tue Apr 29 07:38:40 2014 +0300 @@ -0,0 +1,2 @@ +-std=c++11 +-Iliberebus/src diff -r a932848de652 -r 93894c232d65 .hgignore --- a/.hgignore Mon Apr 28 15:44:59 2014 +0300 +++ b/.hgignore Tue Apr 29 07:38:40 2014 +0300 @@ -2,8 +2,10 @@ \.d$ \.swp$ \.a$ +\.so$ \.so\. -\.dylib +\.dylib$ \.suo$ Debug/ Release/ +^erebus$ diff -r a932848de652 -r 93894c232d65 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Tue Apr 29 07:38:40 2014 +0300 @@ -0,0 +1,28 @@ +csrc = $(wildcard src/*.c) +ccsrc = $(wildcard src/*.cc) +obj = $(csrc:.c=.o) $(ccsrc:.cc=.o) +bin = erebus + +CFLAGS = -pedantic -Wall -g -Iliberebus/src +CXXFLAGS = -std=c++11 $(CFLAGS) +LDFLAGS = -Lliberebus -Wl,-rpath=liberebus $(libgl_$(sys)) -lm -lerebus -lvmath + +libgl_unix = -lGL -lGLU -lglut -lGLEW +libgl_mac = -framework OpenGL -framework GLUT -lGLEW +libgl_win = -lopengl32 -lglu32 -lglut32 -lglew32 + +$(bin): $(obj) liberebus/liberebus.so + $(CXX) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) + +uname = $(shell uname -s) +ifeq ($(uname), Darwin) + sys = mac +else ifeq ($(findstring MINGW, $(uname)), MINGW) + sys = win +else + sys = unix +endif diff -r a932848de652 -r 93894c232d65 liberebus/Makefile --- a/liberebus/Makefile Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/Makefile Tue Apr 29 07:38:40 2014 +0300 @@ -27,6 +27,9 @@ $(lib_so): $(obj) $(CXX) -o $@ $(shared) $(obj) $(LDFLAGS) + [ -n "$(devlink)" ] && \ + rm -f $(soname) $(devlink) && \ + ln -s $@ $(soname) && ln -s $(soname) $(devlink) || true $(lib_a): $(obj) $(AR) rcs $@ $(obj) @@ -38,4 +41,4 @@ .PHONY: clean clean: - rm -f $(obj) $(lib_so) $(lib_a) + rm -f $(obj) $(lib_so) $(lib_a) $(soname) $(devlink) diff -r a932848de652 -r 93894c232d65 liberebus/src/brdf.cc --- a/liberebus/src/brdf.cc Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/brdf.cc Tue Apr 29 07:38:40 2014 +0300 @@ -1,108 +1,9 @@ #include "brdf.h" #include "erebus_impl.h" -ReflAttrib::ReflAttrib() - : value(1), color(1, 1, 1), map(0) -{ -} - -ReflAttrib::ReflAttrib(const Color &col, Texture *tex) -{ - set_color(col); - map = tex; -} - -void ReflAttrib::set_value(float val) -{ - value = val; - color = Color{val, val, val}; -} - -void ReflAttrib::set_color(const Color &col) -{ - color = col; - value = color_luminance(col); -} - -void ReflAttrib::set_map(Texture *tex) -{ - map = tex; -} - -Texture *ReflAttrib::get_map() const -{ - return map; -} - -float ReflAttrib::get_value() const -{ - return value; -} - -float ReflAttrib::get_value(float u, float v) const -{ - return map ? value * color_luminance(map->lookup(u, v)) : value; -} - -const Color &ReflAttrib::get_color() const -{ - return color; -} - -Color ReflAttrib::get_color(float u, float v) const -{ - return map ? color * map->lookup(u, v) : color; -} - // ---- class Reflectance ---- -ReflAttrib Reflectance::def_attrib; - Reflectance::Reflectance() { - set_default_attribs(); -} - -void Reflectance::set_attrib(const char *name, const Color &color, Texture *tex) -{ - attrib[name] = ReflAttrib{color, tex}; -} - -ReflAttrib &Reflectance::get_attrib(const char *name) -{ - auto it = attrib.find(name); - if(it == attrib.end()) { - return def_attrib; - } - return it->second; -} - -const ReflAttrib &Reflectance::get_attrib(const char *name) const -{ - auto it = attrib.find(name); - if(it == attrib.end()) { - return def_attrib; - } - return it->second; -} - -float Reflectance::get_attrib_value(const char *name) const -{ - return get_attrib(name).get_value(); -} - -float Reflectance::get_attrib_value(const char *name, float u, float v) const -{ - return get_attrib(name).get_value(u, v); -} - -Color Reflectance::get_attrib_color(const char *name) const -{ - return get_attrib(name).get_color(); -} - -Color Reflectance::get_attrib_color(const char *name, float u, float v) const -{ - return get_attrib(name).get_color(u, v); } float Reflectance::sample(const Vector3 &norm, const Vector3 &outdir, Vector3 *indir) const @@ -112,12 +13,6 @@ } // --- class LambertRefl --- - -void LambertRefl::set_default_attribs() -{ - set_attrib("color", Color(1, 1, 1)); -} - Vector3 LambertRefl::sample_dir(const Vector3 &norm, const Vector3 &outdir) const { Vector3 dir = Vector3{randf(-1, 1), randf(-1, 1), randf(-1, 1)}.normalized(); diff -r a932848de652 -r 93894c232d65 liberebus/src/brdf.h --- a/liberebus/src/brdf.h Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/brdf.h Tue Apr 29 07:38:40 2014 +0300 @@ -1,64 +1,19 @@ #ifndef BRDF_H_ #define BRDF_H_ -#include -#include -#include "color.h" #include "texture.h" -class ReflAttrib { -private: - float value; - Color color; - Texture *map; - -public: - - ReflAttrib(); - explicit ReflAttrib(const Color &color, Texture *tex = 0); - - void set_value(float val); - void set_color(const Color &col); - - void set_map(Texture *tex); - Texture *get_map() const; - - float get_value() const; - float get_value(float u, float v) const; - const Color &get_color() const; - Color get_color(float u, float v) const; -}; - - class Reflectance { -private: - static ReflAttrib def_attrib; - std::map attrib; - - virtual void set_default_attribs(); - public: Reflectance(); virtual ~Reflectance() = default; - virtual void set_attrib(const char *name, const Color &color, Texture *tex = 0); - virtual ReflAttrib &get_attrib(const char *name); - virtual const ReflAttrib &get_attrib(const char *name) const; - - virtual float get_attrib_value(const char *name) const; - virtual float get_attrib_value(const char *name, float u, float v) const; - virtual Color get_attrib_color(const char *name) const; - virtual Color get_attrib_color(const char *name, float u, float v) const; - virtual Vector3 sample_dir(const Vector3 &norm, const Vector3 &outdir) const = 0; virtual float sample(const Vector3 &norm, const Vector3 &outdir, Vector3 *indir) const; virtual float eval(const Vector3 &norm, const Vector3 &outdir, const Vector3 &indir) const = 0; }; class LambertRefl : public Reflectance { -private: - void set_default_attribs() override; - public: Vector3 sample_dir(const Vector3 &norm, const Vector3 &outdir) const override; float eval(const Vector3 &norm, const Vector3 &outdir, const Vector3 &indir) const override; diff -r a932848de652 -r 93894c232d65 liberebus/src/erebus.cc --- a/liberebus/src/erebus.cc Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/erebus.cc Tue Apr 29 07:38:40 2014 +0300 @@ -5,6 +5,8 @@ #include "erebus.h" #include "vmath/vector.h" #include "image.h" +#include "scene.h" +#include "geomobj.h" using namespace std::chrono; @@ -18,6 +20,8 @@ #define INVALID_RECT Rect{0, 0, 0, 0} struct erebus { + Scene *scn; + Image fbimg; Vector4 options[ERB_NUM_OPTIONS]; @@ -43,6 +47,7 @@ return 0; } + ctx->scn = 0; ctx->cur_time = 0; ctx->cur_rect = INVALID_RECT; return ctx; @@ -89,6 +94,11 @@ void erb_begin_frame(struct erebus *ctx, long ms) { ctx->cur_time = ms; + + int xsz = ctx->options[ERB_OPT_WIDTH].x; + int ysz = ctx->options[ERB_OPT_HEIGHT].x; + + ctx->fbimg.create(xsz, ysz); } int erb_render(struct erebus *ctx, long timeout) @@ -107,6 +117,8 @@ ctx->cur_pixel_y = y; } + ctx->scn->update(); + if(timeout > 0) { auto start_time = steady_clock::now(); while(duration_cast(steady_clock::now() - start_time).count() < timeout) { @@ -137,10 +149,20 @@ int erb_load_scene(struct erebus *ctx, const char *fname) { - //delete ctx->scene; - //ctx->scene = new Scene; + delete ctx->scn; + ctx->scn = new Scene; - return false; // TODO + // XXX for now just create a test scene here + Sphere *sph = new Sphere; + SceneNode *sph_node = new SceneNode(sph); + ctx->scn->add_object(sph); + ctx->scn->add_node(sph_node); + + TargetCamera *cam = new TargetCamera(Vector3(0, 0, -10), Vector3(0, 0, 0)); + //ctx->scn->add_object(cam); + ctx->scn->use_camera(cam); + + return 0; } } // extern "C" diff -r a932848de652 -r 93894c232d65 liberebus/src/geomobj.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liberebus/src/geomobj.cc Tue Apr 29 07:38:40 2014 +0300 @@ -0,0 +1,87 @@ +#include +#include "geomobj.h" + +static LambertRefl lambert; + +GeomObject::GeomObject() +{ + brdf = &lambert; +} + +ObjType GeomObject::get_type() const +{ + return ObjType::geom; +} + +bool GeomObject::intersect(const Ray &ray, RayHit *hit) const +{ + return false; +} + +// --- class Sphere --- + +bool Sphere::intersect(const Ray &ray, RayHit *hit) const +{ + // assumes center is the origin and radius is 1 + float a = dot_product(ray.dir, ray.dir); + float b = 2.0 * dot_product(ray.dir, ray.origin); + float c = dot_product(ray.origin, ray.origin) - 1.0; + + float d = b * b - 4.0 * a * c; + if(d < 1e-4) return false; + + float sqrt_d = sqrt(d); + float t0 = (-b + sqrt_d) / (2.0 * a); + float t1 = (-b - sqrt_d) / (2.0 * a); + + if(t0 < 1e-4) t0 = t1; + if(t1 < 1e-4) t1 = t0; + float t = t0 < t1 ? t0 : t1; + if(t < 1e-4) return false; + + if(hit) { + hit->dist = t; + hit->obj = this; + hit->subobj = 0; + } + return true; +} + +// --- class Box --- + +bool Box::intersect(const Ray &ray, RayHit *hit) const +{ + return false; +} + +// --- class Triangle --- + +bool Triangle::intersect(const Ray &ray, RayHit *hit) const +{ + return false; +} + +// --- class Mesh --- + +bool Mesh::intersect(const Ray &ray, RayHit *hit) const +{ + RayHit nearest; + nearest.dist = FLT_MAX; + + for(size_t i=0; idist < nearest.dist) { + nearest = *hit; + } + } + } + + if(nearest.dist < FLT_MAX) { + *hit = nearest; + hit->subobj = hit->obj; + hit->obj = this; + return true; + } + return false; +} diff -r a932848de652 -r 93894c232d65 liberebus/src/geomobj.h --- a/liberebus/src/geomobj.h Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/geomobj.h Tue Apr 29 07:38:40 2014 +0300 @@ -3,12 +3,16 @@ #include #include "object.h" +#include "material.h" #include "brdf.h" class GeomObject : public Object { public: + Material mtl; Reflectance *brdf; + GeomObject(); + ObjType get_type() const override; bool intersect(const Ray &ray, RayHit *hit = 0) const override; diff -r a932848de652 -r 93894c232d65 liberebus/src/material.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liberebus/src/material.cc Tue Apr 29 07:38:40 2014 +0300 @@ -0,0 +1,107 @@ +#include "material.h" + +MatAttrib::MatAttrib() + : value(1), color(1, 1, 1), map(0) +{ +} + +MatAttrib::MatAttrib(const Color &col, Texture *tex) +{ + set_color(col); + map = tex; +} + +void MatAttrib::set_value(float val) +{ + value = val; + color = Color{val, val, val}; +} + +void MatAttrib::set_color(const Color &col) +{ + color = col; + value = color_luminance(col); +} + +void MatAttrib::set_map(Texture *tex) +{ + map = tex; +} + +Texture *MatAttrib::get_map() const +{ + return map; +} + +float MatAttrib::get_value() const +{ + return value; +} + +float MatAttrib::get_value(float u, float v) const +{ + return map ? value * color_luminance(map->lookup(u, v)) : value; +} + +const Color &MatAttrib::get_color() const +{ + return color; +} + +Color MatAttrib::get_color(float u, float v) const +{ + return map ? color * map->lookup(u, v) : color; +} + + +// --- class Material --- + +MatAttrib Material::def_attrib; + +Material::Material() +{ +} + +void Material::set_attrib(const char *name, const Color &color, Texture *tex) +{ + attrib[name] = MatAttrib{color, tex}; +} + +MatAttrib &Material::get_attrib(const char *name) +{ + auto it = attrib.find(name); + if(it == attrib.end()) { + return def_attrib; + } + return it->second; +} + +const MatAttrib &Material::get_attrib(const char *name) const +{ + auto it = attrib.find(name); + if(it == attrib.end()) { + return def_attrib; + } + return it->second; +} + +float Material::get_attrib_value(const char *name) const +{ + return get_attrib(name).get_value(); +} + +float Material::get_attrib_value(const char *name, float u, float v) const +{ + return get_attrib(name).get_value(u, v); +} + +Color Material::get_attrib_color(const char *name) const +{ + return get_attrib(name).get_color(); +} + +Color Material::get_attrib_color(const char *name, float u, float v) const +{ + return get_attrib(name).get_color(u, v); +} + diff -r a932848de652 -r 93894c232d65 liberebus/src/material.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liberebus/src/material.h Tue Apr 29 07:38:40 2014 +0300 @@ -0,0 +1,50 @@ +#ifndef MATERIAL_H_ +#define MATERIAL_H_ + +#include +#include +#include "color.h" +#include "texture.h" + +class MatAttrib { +private: + float value; + Color color; + Texture *map; + +public: + MatAttrib(); + explicit MatAttrib(const Color &color, Texture *tex = 0); + + void set_value(float val); + void set_color(const Color &col); + + void set_map(Texture *tex); + Texture *get_map() const; + + float get_value() const; + float get_value(float u, float v) const; + const Color &get_color() const; + Color get_color(float u, float v) const; +}; + +class Material { +private: + static MatAttrib def_attrib; + std::map attrib; + +public: + Material(); + + void set_attrib(const char *name, const Color &color, Texture *tex = 0); + MatAttrib &get_attrib(const char *name); + const MatAttrib &get_attrib(const char *name) const; + + float get_attrib_value(const char *name) const; + float get_attrib_value(const char *name, float u, float v) const; + Color get_attrib_color(const char *name) const; + Color get_attrib_color(const char *name, float u, float v) const; +}; + + +#endif // MATERIAL_H_ diff -r a932848de652 -r 93894c232d65 liberebus/src/object.h --- a/liberebus/src/object.h Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/object.h Tue Apr 29 07:38:40 2014 +0300 @@ -8,7 +8,7 @@ struct RayHit { float dist; - const Ray world_ray, local_ray; + Ray world_ray, local_ray; const Object *obj, *subobj; }; diff -r a932848de652 -r 93894c232d65 liberebus/src/scene.cc --- a/liberebus/src/scene.cc Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/scene.cc Tue Apr 29 07:38:40 2014 +0300 @@ -1,51 +1,77 @@ -#include "scene.h" - -Scene::Scene() -{ - active_cam = 0; -} - -Scene::~Scene() -{ - for(auto obj : objects) { - delete obj; - } - for(auto node : nodes) { - delete node; - } -} - -void Scene::add_object(Object *obj) -{ - objects.push_back(obj); -} - -int Scene::get_object_count() const -{ - return (int)objects.size(); -} - -Object *Scene::get_object(int idx) const -{ - return objects[idx]; -} - -void Scene::add_node(SceneNode *node) -{ - nodes.push_back(node); -} - -int Scene::get_node_count() const -{ - return (int)nodes.size(); -} - -SceneNode *Scene::get_node(int idx) const -{ - return nodes[idx]; -} - -bool Scene::intersect(const Ray &ray, RayHit *hit) const -{ - -} \ No newline at end of file +#include "scene.h" + +Scene::Scene() +{ + active_cam = 0; + root = new SceneNode; +} + +Scene::~Scene() +{ + for(auto obj : objects) { + delete obj; + } + for(auto node : nodes) { + delete node; + } + delete root; +} + +void Scene::add_object(Object *obj) +{ + objects.push_back(obj); +} + +int Scene::get_object_count() const +{ + return (int)objects.size(); +} + +Object *Scene::get_object(int idx) const +{ + return objects[idx]; +} + +void Scene::add_node(SceneNode *node) +{ + nodes.push_back(node); + + if(!node->get_parent()) { + root->add_child(node); + } +} + +int Scene::get_node_count() const +{ + return (int)nodes.size(); +} + +SceneNode *Scene::get_node(int idx) const +{ + return nodes[idx]; +} + +void Scene::use_camera(Camera *cam) +{ + active_cam = cam; +} + +Camera *Scene::get_active_camera() const +{ + return active_cam; +} + +void Scene::update(long msec) +{ + root->update(msec); +} + +bool Scene::intersect(const Ray &ray, RayHit *hit) const +{ + return root->intersect(ray, hit); +} + +bool Scene::load(const char *fname) +{ + return false; // TODO +} diff -r a932848de652 -r 93894c232d65 liberebus/src/scene.h --- a/liberebus/src/scene.h Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/scene.h Tue Apr 29 07:38:40 2014 +0300 @@ -10,6 +10,8 @@ std::vector objects; std::vector nodes; + SceneNode *root; + Camera *active_cam; public: @@ -24,7 +26,14 @@ int get_node_count() const; SceneNode *get_node(int idx) const; + void use_camera(Camera *cam); + Camera *get_active_camera() const; + + void update(long msec = 0); + bool intersect(const Ray &ray, RayHit *hit) const; + + bool load(const char *fname); }; #endif // SCENE_H_ diff -r a932848de652 -r 93894c232d65 liberebus/src/snode.cc --- a/liberebus/src/snode.cc Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/snode.cc Tue Apr 29 07:38:40 2014 +0300 @@ -1,111 +1,168 @@ -#include -#include -#include "snode.h" - -void SceneNode::add_child(SceneNode *node) -{ - if(node->parent) { - if(node->parent == this) { - return; - } - node->parent->remove_child(node); - } - - children.push_back(node); - node->parent = this; -} - -bool SceneNode::remove_child(SceneNode *node) -{ - auto it = std::find(children.begin(), children.end(), node); - if(it != children.end()) { - assert(node->parent == this); - node->parent = 0; - } -} - -int SceneNode::get_num_children() const -{ - return (int)children.size(); -} - -SceneNode *SceneNode::get_child(int idx) const -{ - return children[idx]; -} - -void SceneNode::set_position(const Vector3 &pos) -{ - this->pos = pos; -} - -void SceneNode::set_rotation(const Quaternion &rot) -{ - this->rot = rot; -} - -void SceneNode::set_scaling(const Vector3 &scale) -{ - this->scale = scale; -} - - -const Vector3 &SceneNode::get_node_position() const -{ - return pos; -} - -const Quaternion &SceneNode::get_node_rotation() const -{ - return rot; -} - -const Vector3 &SceneNode::get_node_scaling() const -{ - return scale; -} - - -Vector3 SceneNode::get_position() const -{ - return Vector3{0, 0, 0}.transformed(xform); -} - -Quaternion SceneNode::get_rotation() const -{ - return rot; // TODO -} - -Vector3 SceneNode::get_scaling() const -{ - return scale; // TODO -} - - -void SceneNode::update_node(long msec) -{ - xform.reset_identity(); - xform.translate(pos); - xform.rotate(rot); - xform.scale(scale); - - if(parent) { - xform = parent->xform * xform; - } - inv_xform = xform.inverse(); -} - -void SceneNode::update(long msec) -{ - update_node(msec); - - for(size_t i=0; iupdate(msec); - } -} - - -bool SceneNode::intersect(const Ray &ray, RayHit *hit) const -{ - Ray local_ray = ray.transformed(inv_xform); -} \ No newline at end of file +#include +#include +#include +#include "snode.h" + +SceneNode::SceneNode() + : scale(1, 1, 1) +{ + parent = 0; +} + +SceneNode::SceneNode(Object *obj) + : scale(1, 1, 1) +{ + parent = 0; + add_object(obj); +} + +void SceneNode::add_child(SceneNode *node) +{ + if(node->parent) { + if(node->parent == this) { + return; + } + node->parent->remove_child(node); + } + + children.push_back(node); + node->parent = this; +} + +bool SceneNode::remove_child(SceneNode *node) +{ + auto it = std::find(children.begin(), children.end(), node); + if(it != children.end()) { + assert(node->parent == this); + node->parent = 0; + return true; + } + return false; +} + +int SceneNode::get_num_children() const +{ + return (int)children.size(); +} + +SceneNode *SceneNode::get_child(int idx) const +{ + return children[idx]; +} + +SceneNode *SceneNode::get_parent() const +{ + return parent; +} + +void SceneNode::add_object(Object *obj) +{ + if(std::find(this->obj.begin(), this->obj.end(), obj) == this->obj.end()) { + this->obj.push_back(obj); + } +} + +int SceneNode::get_num_objects() const +{ + return (int)obj.size(); +} + +Object *SceneNode::get_object(int idx) const +{ + return obj[idx]; +} + +void SceneNode::set_position(const Vector3 &pos) +{ + this->pos = pos; +} + +void SceneNode::set_rotation(const Quaternion &rot) +{ + this->rot = rot; +} + +void SceneNode::set_scaling(const Vector3 &scale) +{ + this->scale = scale; +} + + +const Vector3 &SceneNode::get_node_position() const +{ + return pos; +} + +const Quaternion &SceneNode::get_node_rotation() const +{ + return rot; +} + +const Vector3 &SceneNode::get_node_scaling() const +{ + return scale; +} + + +Vector3 SceneNode::get_position() const +{ + return Vector3{0, 0, 0}.transformed(xform); +} + +Quaternion SceneNode::get_rotation() const +{ + return rot; // TODO +} + +Vector3 SceneNode::get_scaling() const +{ + return scale; // TODO +} + + +void SceneNode::update_node(long msec) +{ + xform.reset_identity(); + xform.translate(pos); + xform.rotate(rot); + xform.scale(scale); + + if(parent) { + xform = parent->xform * xform; + } + inv_xform = xform.inverse(); +} + +void SceneNode::update(long msec) +{ + update_node(msec); + + for(size_t i=0; iupdate(msec); + } +} + + +bool SceneNode::intersect(const Ray &ray, RayHit *hit) const +{ + Ray local_ray = ray.transformed(inv_xform); + + RayHit nearest; + nearest.dist = FLT_MAX; + for(size_t i=0; iintersect(local_ray, hit)) { + if(!hit) return true; + if(hit->dist < nearest.dist) { + nearest = *hit; + } + } + } + + if(nearest.dist < FLT_MAX) { + *hit = nearest; + hit->local_ray = local_ray; + hit->world_ray = ray; + return true; + } + return false; +} diff -r a932848de652 -r 93894c232d65 liberebus/src/snode.h --- a/liberebus/src/snode.h Mon Apr 28 15:44:59 2014 +0300 +++ b/liberebus/src/snode.h Tue Apr 29 07:38:40 2014 +0300 @@ -20,12 +20,21 @@ Matrix4x4 inv_xform; public: + SceneNode(); + explicit SceneNode(Object *obj); + void add_child(SceneNode *node); bool remove_child(SceneNode *node); int get_num_children() const; SceneNode *get_child(int idx) const; + SceneNode *get_parent() const; + + void add_object(Object *obj); + int get_num_objects() const; + Object *get_object(int idx) const; + void set_position(const Vector3 &pos); void set_rotation(const Quaternion &rot); void set_scaling(const Vector3 &scale); diff -r a932848de652 -r 93894c232d65 src/main.cc --- a/src/main.cc Mon Apr 28 15:44:59 2014 +0300 +++ b/src/main.cc Tue Apr 29 07:38:40 2014 +0300 @@ -2,11 +2,13 @@ #include #include #include "opengl.h" +#include "erebus.h" static bool init(); static void cleanup(); static void resize_rtarget(int xsz, int ysz); static void update_rect(int x, int y, int xsz, int ysz, float *pixels); +static void idle(); static void display(); static void reshape(int x, int y); static void keyb(unsigned char key, int x, int y); @@ -16,6 +18,10 @@ static int width, height, rtex_width, rtex_height; static unsigned int rtex; +static erebus *erb; +static bool render_pending; + + int main(int argc, char **argv) { glutInitWindowSize(1024, 600); @@ -38,11 +44,27 @@ static bool init() { + if(!(erb = erb_init())) { + return false; + } + erb_setopti(erb, ERB_OPT_WIDTH, width); + erb_setopti(erb, ERB_OPT_HEIGHT, height); + + if(erb_load_scene(erb, "scene") == -1) { + return false; + } + + printf("begin rendering\n"); + render_pending = true; + glutIdleFunc(idle); + erb_begin_frame(erb, 0); + return true; } static void cleanup() { + erb_destroy(erb); } static void resize_rtarget(int xsz, int ysz) @@ -92,8 +114,21 @@ glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels); } +static void idle() +{ + glutPostRedisplay(); +} + static void display() { + if(render_pending) { + if(erb_render(erb, 128) == 0) { + render_pending = false; + glutIdleFunc(0); + } + update_rect(0, 0, width, height, erb_get_framebuffer(erb)); + } + glBindTexture(GL_TEXTURE_2D, rtex); glEnable(GL_TEXTURE_2D); @@ -117,6 +152,9 @@ { glViewport(0, 0, x, y); resize_rtarget(x, y); + + erb_setopti(erb, ERB_OPT_WIDTH, width); + erb_setopti(erb, ERB_OPT_HEIGHT, height); } static void keyb(unsigned char key, int x, int y) @@ -124,6 +162,13 @@ switch(key) { case 27: exit(0); + + case ' ': + printf("begin rendering\n"); + render_pending = true; + glutIdleFunc(idle); + erb_begin_frame(erb, 0); + break; } }