tavli

changeset 3:94aff2ff1934

too much?
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 22 Jun 2015 21:46:57 +0300
parents 893192aea099
children b41ceead1708
files src/geom.h src/object.cc src/object.h src/snode.cc src/snode.h
diffstat 5 files changed, 259 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/src/geom.h	Mon Jun 22 05:15:39 2015 +0300
     1.2 +++ b/src/geom.h	Mon Jun 22 21:46:57 2015 +0300
     1.3 @@ -4,12 +4,15 @@
     1.4  #include "vmath/vmath.h"
     1.5  
     1.6  class GeomObject;
     1.7 +class SceneNode;
     1.8  
     1.9  struct HitPoint {
    1.10  	float dist;				//< parametric distance along the ray
    1.11  	Vector3 pos;			//< position of intersection (orig + dir * dist)
    1.12  	Vector3 normal;			//< normal at the point of intersection
    1.13  	const void *obj;		//< pointer to the intersected object
    1.14 +	const SceneNode *node;
    1.15 +	Ray ray;
    1.16  };
    1.17  
    1.18  class GeomObject {
     2.1 --- a/src/object.cc	Mon Jun 22 05:15:39 2015 +0300
     2.2 +++ b/src/object.cc	Mon Jun 22 21:46:57 2015 +0300
     2.3 @@ -43,3 +43,8 @@
     2.4  
     2.5  	glPopMatrix();
     2.6  }
     2.7 +
     2.8 +bool Object::intersect(const Ray &ray, HitPoint *hit) const
     2.9 +{
    2.10 +	return false;	// TODO
    2.11 +}
     3.1 --- a/src/object.h	Mon Jun 22 05:15:39 2015 +0300
     3.2 +++ b/src/object.h	Mon Jun 22 21:46:57 2015 +0300
     3.3 @@ -2,6 +2,7 @@
     3.4  #define OBJECT_H_
     3.5  
     3.6  #include "mesh.h"
     3.7 +#include "geom.h"
     3.8  
     3.9  class Object {
    3.10  private:
    3.11 @@ -19,6 +20,8 @@
    3.12  	Mesh *get_mesh() const;
    3.13  
    3.14  	void draw() const;
    3.15 +
    3.16 +	bool intersect(const Ray &ray, HitPoint *hit) const;
    3.17  };
    3.18  
    3.19  #endif	// OBJECT_H_
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/snode.cc	Mon Jun 22 21:46:57 2015 +0300
     4.3 @@ -0,0 +1,188 @@
     4.4 +#include <float.h>
     4.5 +#include <assert.h>
     4.6 +#include <algorithm>
     4.7 +#include "snode.h"
     4.8 +
     4.9 +SceneNode::SceneNode()
    4.10 +	: scale(1, 1, 1)
    4.11 +{
    4.12 +	parent = 0;
    4.13 +}
    4.14 +
    4.15 +SceneNode::SceneNode(Object *obj)
    4.16 +	: scale(1, 1, 1)
    4.17 +{
    4.18 +	parent = 0;
    4.19 +	add_object(obj);
    4.20 +}
    4.21 +
    4.22 +void SceneNode::add_child(SceneNode *node)
    4.23 +{
    4.24 +	if(node->parent) {
    4.25 +		if(node->parent == this) {
    4.26 +			return;
    4.27 +		}
    4.28 +		node->parent->remove_child(node);
    4.29 +	}
    4.30 +
    4.31 +	children.push_back(node);
    4.32 +	node->parent = this;
    4.33 +}
    4.34 +
    4.35 +bool SceneNode::remove_child(SceneNode *node)
    4.36 +{
    4.37 +	for(size_t i=0; i<children.size(); i++) {
    4.38 +		if(children[i] == node) {
    4.39 +			assert(node->parent == this);
    4.40 +			node->parent = 0;
    4.41 +			return true;
    4.42 +		}
    4.43 +	}
    4.44 +	return false;
    4.45 +}
    4.46 +
    4.47 +int SceneNode::get_num_children() const
    4.48 +{
    4.49 +	return (int)children.size();
    4.50 +}
    4.51 +
    4.52 +SceneNode *SceneNode::get_child(int idx) const
    4.53 +{
    4.54 +	return children[idx];
    4.55 +}
    4.56 +
    4.57 +SceneNode *SceneNode::get_parent() const
    4.58 +{
    4.59 +	return parent;
    4.60 +}
    4.61 +
    4.62 +void SceneNode::add_object(Object *obj)
    4.63 +{
    4.64 +	if(std::find(this->obj.begin(), this->obj.end(), obj) == this->obj.end()) {
    4.65 +		this->obj.push_back(obj);
    4.66 +	}
    4.67 +}
    4.68 +
    4.69 +int SceneNode::get_num_objects() const
    4.70 +{
    4.71 +	return (int)obj.size();
    4.72 +}
    4.73 +
    4.74 +Object *SceneNode::get_object(int idx) const
    4.75 +{
    4.76 +	return obj[idx];
    4.77 +}
    4.78 +
    4.79 +void SceneNode::set_position(const Vector3 &pos)
    4.80 +{
    4.81 +	this->pos = pos;
    4.82 +}
    4.83 +
    4.84 +void SceneNode::set_rotation(const Quaternion &rot)
    4.85 +{
    4.86 +	this->rot = rot;
    4.87 +}
    4.88 +
    4.89 +void SceneNode::set_scaling(const Vector3 &scale)
    4.90 +{
    4.91 +	this->scale = scale;
    4.92 +}
    4.93 +
    4.94 +
    4.95 +const Vector3 &SceneNode::get_node_position() const
    4.96 +{
    4.97 +	return pos;
    4.98 +}
    4.99 +
   4.100 +const Quaternion &SceneNode::get_node_rotation() const
   4.101 +{
   4.102 +	return rot;
   4.103 +}
   4.104 +
   4.105 +const Vector3 &SceneNode::get_node_scaling() const
   4.106 +{
   4.107 +	return scale;
   4.108 +}
   4.109 +
   4.110 +
   4.111 +Vector3 SceneNode::get_position() const
   4.112 +{
   4.113 +	return Vector3(0, 0, 0).transformed(xform);
   4.114 +}
   4.115 +
   4.116 +Quaternion SceneNode::get_rotation() const
   4.117 +{
   4.118 +	return rot;	// TODO
   4.119 +}
   4.120 +
   4.121 +Vector3 SceneNode::get_scaling() const
   4.122 +{
   4.123 +	return scale;	// TODO
   4.124 +}
   4.125 +
   4.126 +const Matrix4x4 &SceneNode::get_matrix() const
   4.127 +{
   4.128 +	return xform;
   4.129 +}
   4.130 +
   4.131 +const Matrix4x4 &SceneNode::get_inv_matrix() const
   4.132 +{
   4.133 +	return inv_xform;
   4.134 +}
   4.135 +
   4.136 +
   4.137 +void SceneNode::update_node(long msec)
   4.138 +{
   4.139 +	xform.reset_identity();
   4.140 +	xform.translate(pos);
   4.141 +	xform.rotate(rot);
   4.142 +	xform.scale(scale);
   4.143 +
   4.144 +	if(parent) {
   4.145 +		xform = parent->xform * xform;
   4.146 +	}
   4.147 +	inv_xform = xform.inverse();
   4.148 +}
   4.149 +
   4.150 +void SceneNode::update(long msec)
   4.151 +{
   4.152 +	update_node(msec);
   4.153 +
   4.154 +	for(size_t i=0; i<children.size(); i++) {
   4.155 +		children[i]->update(msec);
   4.156 +	}
   4.157 +}
   4.158 +
   4.159 +
   4.160 +bool SceneNode::intersect(const Ray &ray, HitPoint *hit) const
   4.161 +{
   4.162 +	Ray local_ray = ray.transformed(inv_xform);
   4.163 +
   4.164 +	HitPoint nearest;
   4.165 +	nearest.dist = FLT_MAX;
   4.166 +	for(size_t i=0; i<obj.size(); i++) {
   4.167 +		if(obj[i]->intersect(local_ray, hit)) {
   4.168 +			if(!hit) return true;
   4.169 +			if(hit->dist < nearest.dist) {
   4.170 +				nearest = *hit;
   4.171 +				nearest.node = this;
   4.172 +			}
   4.173 +		}
   4.174 +	}
   4.175 +
   4.176 +	for(size_t i=0; i<children.size(); i++) {
   4.177 +		if(children[i]->intersect(ray, hit)) {
   4.178 +			if(!hit) return true;
   4.179 +			if(hit->dist < nearest.dist) {
   4.180 +				nearest = *hit;
   4.181 +			}
   4.182 +		}
   4.183 +	}
   4.184 +
   4.185 +	if(nearest.dist < FLT_MAX) {
   4.186 +		*hit = nearest;
   4.187 +		hit->ray = ray;
   4.188 +		return true;
   4.189 +	}
   4.190 +	return false;
   4.191 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/snode.h	Mon Jun 22 21:46:57 2015 +0300
     5.3 @@ -0,0 +1,60 @@
     5.4 +#ifndef SNODE_H_
     5.5 +#define SNODE_H_
     5.6 +
     5.7 +#include <vector>
     5.8 +#include "object.h"
     5.9 +#include "vmath/vmath.h"
    5.10 +#include "geom.h"
    5.11 +
    5.12 +class SceneNode {
    5.13 +private:
    5.14 +	Vector3 pos;
    5.15 +	Quaternion rot;
    5.16 +	Vector3 scale;
    5.17 +
    5.18 +	std::vector<Object*> obj;
    5.19 +
    5.20 +	SceneNode *parent;
    5.21 +	std::vector<SceneNode*> children;
    5.22 +
    5.23 +	Matrix4x4 xform;
    5.24 +	Matrix4x4 inv_xform;
    5.25 +
    5.26 +public:
    5.27 +	SceneNode();
    5.28 +	explicit SceneNode(Object *obj);
    5.29 +
    5.30 +	void add_child(SceneNode *node);
    5.31 +	bool remove_child(SceneNode *node);
    5.32 +
    5.33 +	int get_num_children() const;
    5.34 +	SceneNode *get_child(int idx) const;
    5.35 +
    5.36 +	SceneNode *get_parent() const;
    5.37 +
    5.38 +	void add_object(Object *obj);
    5.39 +	int get_num_objects() const;
    5.40 +	Object *get_object(int idx) const;
    5.41 +
    5.42 +	void set_position(const Vector3 &pos);
    5.43 +	void set_rotation(const Quaternion &rot);
    5.44 +	void set_scaling(const Vector3 &scale);
    5.45 +
    5.46 +	const Vector3 &get_node_position() const;
    5.47 +	const Quaternion &get_node_rotation() const;
    5.48 +	const Vector3 &get_node_scaling() const;
    5.49 +
    5.50 +	Vector3 get_position() const;
    5.51 +	Quaternion get_rotation() const;
    5.52 +	Vector3 get_scaling() const;
    5.53 +
    5.54 +	const Matrix4x4 &get_matrix() const;
    5.55 +	const Matrix4x4 &get_inv_matrix() const;
    5.56 +
    5.57 +	void update_node(long msec = 0);
    5.58 +	void update(long msec = 0);
    5.59 +
    5.60 +	bool intersect(const Ray &ray, HitPoint *hit) const;
    5.61 +};
    5.62 +
    5.63 +#endif	// SNODE_H_