Panagiotis Christopoulos Charitos 15 роки тому
батько
коміт
6f0816f8f9

+ 4 - 2
src/Core/Messaging.h

@@ -1,14 +1,16 @@
 #ifndef MESSAGING_H
 #define MESSAGING_H
 
+#include <string>
 #include "App.h"
 #include "MessageHandler.h"
 
+
 #define INFO(x) \
-	app->getMessageHandler().write(std::string("Info: ") + x)
+	app->getMessageHandler().write(__FILE__, __LINE__, __func__, std::string("Info: ") + x)
 
 #define WARNING(x) \
-	app->getMessageHandler().write(std::string("Warning: ") + x)
+	app->getMessageHandler().write(__FILE__, __LINE__, __func__, std::string("Warning: ") + x)
 
 
 #endif

+ 4 - 6
src/Main.cpp

@@ -13,7 +13,6 @@
 #include "Light.h"
 #include "PointLight.h"
 #include "SpotLight.h"
-#include "collision.h"
 #include "Material.h"
 #include "Resource.h"
 #include "Scene.h"
@@ -39,6 +38,7 @@
 #include "RigidBody.h"
 #include "ScriptingEngine.h"
 #include "StdinListener.h"
+#include "Messaging.h"
 
 
 App* app = NULL; ///< The only global var. App constructor sets it
@@ -239,7 +239,7 @@ void init()
 
 	initPhysics();
 
-	INFO("Engine initialization ends (" << App::getTicks()-ticks << ")");
+	INFO("Engine initialization ends (" + boost::lexical_cast<std::string>(App::getTicks() - ticks) + ")");
 }
 
 
@@ -351,7 +351,7 @@ void mainLoop()
 
 		// std stuff follow
 		app->swapBuffers();
-		GL_OK();
+		//ON_GL_FAIL_THROW_EXCEPTION();
 		if(1)
 		{
 			//if(app->getMainRenderer().getFramesNum() == 100) app->getMainRenderer().takeScreenshot("gfx/screenshot.tga");
@@ -361,11 +361,9 @@ void mainLoop()
 			if(app->getMainRenderer().getFramesNum() == 5000) break;
 	}while(true);
 
-	INFO("Exiting main loop (" << App::getTicks()-ticks << ")");
+	INFO("Exiting main loop (" + boost::lexical_cast<std::string>(App::getTicks() - ticks) + ")");
 }
 
-#include "Exception.h"
-
 
 //======================================================================================================================
 // main                                                                                                                =

+ 0 - 1982
src/Misc/collision.cpp

@@ -1,1982 +0,0 @@
-#include <float.h>
-#include "collision.h"
-#include "MainRenderer.h"
-#include "App.h"
-
-
-static int render_seperation_lock = 0;
-#define LOCK_RENDER_SEPERATION ++render_seperation_lock;
-#define UNLOCK_RENDER_SEPERATION --render_seperation_lock;
-#define RENDER_SEPERATION_TEST if(render_seperation_lock==0) RenderSeparationData(normal, impact_point, depth);
-
-
-/*
-=======================================================================================================================================
-misc                                                                                                                   =
-=======================================================================================================================================
-*/
-static void RenderSeparationData(const Vec3& normal, const Vec3& impact_point, float depth)
-{
-	float con = 0.5f;
-	const Vec3& i = impact_point;
-
-	//glLineWidth(2.0);
-	glBegin(GL_LINES);
-		glColor3fv(&Vec3(1.0, 0.0, 0.0)[0]);
-		glVertex3f(i.x-con, i.y, i.z);
-		glVertex3f(i.x+con, i.y, i.z);
-		glColor3fv(&Vec3(0.0, 1.0, 0.0)[0]);
-		glVertex3f(i.x, i.y-con, i.z);
-		glVertex3f(i.x, i.y+con, i.z);
-		glColor3fv(&Vec3(0.0, 0.0, 1.0)[0]);
-		glVertex3f(i.x, i.y, i.z-con);
-		glVertex3f(i.x, i.y, i.z+con);
-	glEnd();
-
-	//glLineWidth(6.0);
-	glBegin(GL_LINES);
-		glColor3fv(&Vec3(1.0, 1.0, 1.0)[0]);
-		glVertex3fv(&((Vec3&)impact_point)[0]);
-		glVertex3fv(&(impact_point+ normal*depth)[0]);
-	glEnd();
-}
-
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-=======================================================================================================================================
-*/
-bool bvolume_t::Intersects(const bvolume_t& bv) const
-{
-	switch(type)
-	{
-		case LINE_SEG:
-			return Intersects((const lineseg_t&)bv);
-		case RAY:
-			return Intersects((const ray_t&)bv);
-		case PLANE:
-			return PlaneTest((const plane_t&)bv) == 0.0;
-		case BSPHERE:
-			return Intersects((const bsphere_t&)bv);
-		case AABB:
-			return Intersects((const aabb_t&)bv);
-		case OBB:
-			return Intersects((const obb_t&)bv);
-		default:
-			FATAL("Incorect type");
-	}
-	return false;
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-=======================================================================================================================================
-*/
-bool bvolume_t::SeperationTest(const bvolume_t& bv, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	switch(type)
-	{
-		case BSPHERE:
-			return SeperationTest((const bsphere_t&)bv, normal, impact_point, depth);
-		case AABB:
-			return SeperationTest((const aabb_t&)bv, normal, impact_point, depth);
-		case OBB:
-			return SeperationTest((const obb_t&)bv, normal, impact_point, depth);
-		default:
-			FATAL("Trying to seperate incompatible volumes");
-	}
-	return false;
-}
-
-
-/*
-=======================================================================================================================================
-getDistanceSquared                                                                                                        =
-seg - seg                                                                                                              =
-=======================================================================================================================================
-*/
-float lineseg_t::DistanceSquared(const lineseg_t& ls, float& s_c, float& t_c) const
-{
-	// compute intermediate parameters
-	Vec3 w0 = origin - ls.origin;
-	float a = dir.dot(dir);
-	float b = dir.dot(ls.dir);
-	float c = ls.dir.dot(ls.dir);
-	float d = dir.dot(w0);
-	float e = ls.dir.dot(w0);
-
-	float denom = a*c - b*b;
-	// parameters to compute s_c, t_c
-	float sn, sd, tn, td;
-
-	// if denom is zero, try finding closest point on sl to origin0
-	if(isZero(denom))
-	{
-		// clamp s_c to 0
-		sd = td = c;
-		sn = 0.0f;
-		tn = e;
-	}
-	else
-	{
-		// clamp s_c within [0, 1]
-		sd = td = denom;
-		sn = b*e - c*d;
-		tn = a*e - b*d;
-
-		// clamp s_c to 0
-		if (sn < 0.0f)
-		{
-			sn = 0.0f;
-			tn = e;
-			td = c;
-		}
-		// clamp s_c to 1
-		else if (sn > sd)
-		{
-			sn = sd;
-			tn = e + b;
-			td = c;
-		}
-	}
-
-	// clamp t_c within [0, 1]
-	// clamp t_c to 0
-	if(tn < 0.0f)
-	{
-		t_c = 0.0f;
-		// clamp s_c to 0
-		if (-d < 0.0f)
-			s_c = 0.0f;
-		// clamp s_c to 1
-		else if (-d > a)
-			s_c = 1.0f;
-		else
-			s_c = -d/a;
-	}
-	// clamp t_c to 1
-	else if(tn > td)
-	{
-		t_c = 1.0f;
-		// clamp s_c to 0
-		if((-d+b) < 0.0f)
-			s_c = 0.0f;
-		// clamp s_c to 1
-		else if((-d+b) > a)
-			s_c = 1.0f;
-		else
-			s_c = (-d+b)/a;
-	}
-	else
-	{
-			t_c = tn/td;
-			s_c = sn/sd;
-	}
-
-	// compute difference vector and distance squared
-	Vec3 wc = w0 + dir*s_c - ls.dir*t_c;
-	return wc.dot(wc);
-}
-
-
-/*
-=======================================================================================================================================
-getDistanceSquared                                                                                                        =
-seg - ray                                                                                                              =
-=======================================================================================================================================
-*/
-float lineseg_t::DistanceSquared(const ray_t& ray, float& s_c, float& t_c) const
-{
-	// compute intermediate parameters
-	Vec3 w0 = origin - ray.origin;
-	float a = dir.dot(dir);
-	float b = dir.dot(ray.dir);
-	float c = ray.dir.dot(ray.dir);
-	float d = dir.dot(w0);
-	float e = ray.dir.dot(w0);
-
-	float denom = a*c - b*b;
-	// parameters to compute s_c, t_c
-	float sn, sd, tn, td;
-
-	// if denom is zero, try finding closest point on ME1 to origin0
-	if(isZero(denom))
-	{
-		// clamp s_c to 0
-		sd = td = c;
-		sn = 0.0f;
-		tn = e;
-	}
-	else
-	{
-		// clamp s_c within [0, 1]
-		sd = td = denom;
-		sn = b*e - c*d;
-		tn = a*e - b*d;
-
-		// clamp s_c to 0
-		if (sn < 0.0f)
-		{
-			sn = 0.0f;
-			tn = e;
-			td = c;
-		}
-		// clamp s_c to 1
-		else if (sn > sd)
-		{
-			sn = sd;
-			tn = e + b;
-			td = c;
-		}
-	}
-
-	// clamp t_c within [0,+inf]
-	// clamp t_c to 0
-	if (tn < 0.0f)
-	{
-		t_c = 0.0f;
-		// clamp s_c to 0
-		if (-d < 0.0f)
-			s_c = 0.0f;
-		// clamp s_c to 1
-		else if (-d > a)
-			s_c = 1.0f;
-		else
-			s_c = -d/a;
-	}
-	else
-	{
-		t_c = tn/td;
-		s_c = sn/sd;
-	}
-
-	// compute difference vector and distance squared
-	Vec3 wc = w0 + dir*s_c - ray.dir*t_c;
-	return wc.dot(wc);
-}
-
-
-/*
-=======================================================================================================================================
-getDistanceSquared                                                                                                        =
-seg - point                                                                                                            =
-=======================================================================================================================================
-*/
-float lineseg_t::DistanceSquared(const Vec3& point, float& t_c) const
-{
-	Vec3 w = point - origin;
-	float proj = w.dot(dir);
-	// endpoint 0 is closest point
-	if (proj <= 0)
-	{
-		t_c = 0.0f;
-		return w.dot(w);
-	}
-	else
-	{
-		float vsq = dir.dot(dir);
-		// endpoint 1 is closest point
-		if (proj >= vsq)
-		{
-			t_c = 1.0f;
-			return w.dot(w) - 2.0f*proj + vsq;
-		}
-		// otherwise somewhere else in segment
-		else
-		{
-			t_c = proj/vsq;
-			return w.dot(w) - t_c*proj;
-		}
-	}
-}
-
-
-/*
-=======================================================================================================================================
-ClosestPoints                                                                                                          =
-seg - seg                                                                                                              =
-=======================================================================================================================================
-*/
-void lineseg_t::ClosestPoints(const lineseg_t& segment1, Vec3& point0, Vec3& point1) const
-{
-  // compute intermediate parameters
-  Vec3 w0 = origin - segment1.origin;
-  float a = dir.dot(dir);
-  float b = dir.dot(segment1.dir);
-  float c = segment1.dir.dot(segment1.dir);
-  float d = dir.dot(w0);
-  float e = segment1.dir.dot(w0);
-
-  float denom = a*c - b*b;
-  // parameters to compute s_c, t_c
-  float s_c, t_c;
-  float sn, sd, tn, td;
-
-  // if denom is zero, try finding closest point on segment1 to origin0
-  if(isZero(denom))
-  {
-		// clamp s_c to 0
-		sd = td = c;
-		sn = 0.0f;
-		tn = e;
-  }
-  else
-  {
-		// clamp s_c within [0, 1]
-		sd = td = denom;
-		sn = b*e - c*d;
-		tn = a*e - b*d;
-
-		// clamp s_c to 0
-		if(sn < 0.0f)
-		{
-			sn = 0.0f;
-			tn = e;
-			td = c;
-		}
-		// clamp s_c to 1
-		else if(sn > sd)
-		{
-			sn = sd;
-			tn = e + b;
-			td = c;
-		}
-  }
-
-  // clamp t_c within [0, 1]
-  // clamp t_c to 0
-  if(tn < 0.0f)
-  {
-		t_c = 0.0f;
-		// clamp s_c to 0
-		if(-d < 0.0f)
-			s_c = 0.0f;
-		// clamp s_c to 1
-		else if(-d > a)
-			s_c = 1.0f;
-		else
-			s_c = -d/a;
-  }
-  // clamp t_c to 1
-  else if(tn > td)
-  {
-		t_c = 1.0f;
-		// clamp s_c to 0
-		if((-d+b) < 0.0f)
-			s_c = 0.0f;
-		// clamp s_c to 1
-		else if((-d+b) > a)
-			s_c = 1.0f;
-		else
-			s_c = (-d+b)/a;
-  }
-  else
-  {
-     t_c = tn/td;
-     s_c = sn/sd;
-  }
-
-  // compute closest points
-  point0 = origin + dir*s_c;
-  point1 = segment1.origin + segment1.dir*t_c;
-
-}
-
-/*
-=======================================================================================================================================
-ClosestPoints                                                                                                          =
-seg - ray                                                                                                              =
-=======================================================================================================================================
-*/
-void lineseg_t::ClosestPoints(const ray_t& ray, Vec3& point0, Vec3& point1) const
-{
-	// compute intermediate parameters
-	Vec3 w0 = origin - ray.origin;
-	float a = dir.dot(dir);
-	float b = dir.dot(ray.dir);
-	float c = ray.dir.dot(ray.dir);
-	float d = dir.dot(w0);
-	float e = ray.dir.dot(w0);
-
-	float denom = a*c - b*b;
-	// parameters to compute s_c, t_c
-	float s_c, t_c;
-	float sn, sd, tn, td;
-
-	// if denom is zero, try finding closest point on 1 to origin0
-	if(isZero(denom))
-	{
-		// clamp s_c to 0
-		sd = td = c;
-		sn = 0.0f;
-		tn = e;
-	}
-	else
-	{
-		// clamp s_c within [0, 1]
-		sd = td = denom;
-		sn = b*e - c*d;
-		tn = a*e - b*d;
-
-		// clamp s_c to 0
-		if(sn < 0.0f)
-		{
-			sn = 0.0f;
-			tn = e;
-			td = c;
-		}
-		// clamp s_c to 1
-		else if(sn > sd)
-		{
-			sn = sd;
-			tn = e + b;
-			td = c;
-		}
-	}
-
-	// clamp t_c within [0,+inf]
-	// clamp t_c to 0
-	if(tn < 0.0f)
-	{
-		t_c = 0.0f;
-		// clamp s_c to 0
-		if(-d < 0.0f)
-		{
-			s_c = 0.0f;
-		}
-		// clamp s_c to 1
-		else if(-d > a)
-		{
-			s_c = 1.0f;
-		}
-		else
-		{
-			s_c = -d/a;
-		}
-	}
-	else
-	{
-		t_c = tn/td;
-		s_c = sn/sd;
-	}
-
-	// compute closest points
-	point0 = origin + dir*s_c;
-	point1 = ray.origin + ray.dir*t_c;
-
-}
-
-
-/*
-=======================================================================================================================================
-ClosestPoints                                                                                                          =
-seg - point                                                                                                            =
-=======================================================================================================================================
-*/
-Vec3 lineseg_t::ClosestPoints(const Vec3& point) const
-{
-    Vec3 w = point - origin;
-    float proj = w.dot(dir);
-    // endpoint 0 is closest point
-    if(proj <= 0.0f)
-			return origin;
-    else
-    {
-			float vsq = dir.dot(dir);
-			// endpoint 1 is closest point
-			if(proj >= vsq)
-				return origin + dir;
-			// else somewhere else in segment
-			else
-				return origin + dir*(proj/vsq);
-    }
-}
-
-
-/*
-=======================================================================================================================================
-Transformed                                                                                                            =
-=======================================================================================================================================
-*/
-lineseg_t lineseg_t::Transformed(const Vec3& translate, const Mat3& rotate, float scale) const
-{
-	lineseg_t seg;
-
-	seg.origin = origin.getTransformed(translate, rotate, scale);
-	seg.dir = rotate * (dir * scale);
-
-	return seg;
-}
-
-
-/*
-=======================================================================================================================================
-render                                                                                                                 =
-=======================================================================================================================================
-*/
-void lineseg_t::Render()
-{
-	Vec3 P1 = origin+dir;
-
-	glColor3fv(&Vec3(1.0f, 1.0f, 1.0f)[0]);
-	glBegin(GL_LINES);
-		glVertex3fv(&origin[0]);
-		glVertex3fv(&P1[0]);
-	glEnd();
-
-
-	//glPointSize(4.0f);
-	glBegin(GL_POINTS);
-		glColor3fv(&Vec3(1.0, 0.0, 0.0)[0]);
-		glVertex3fv(&origin[0]);
-		glColor3fv(&Vec3(0.0, 1.0, 0.0)[0]);
-		glVertex3fv(&P1[0]);
-	glEnd();
-
-	glDisable(GL_DEPTH_TEST);
-	glColor3fv(&Vec3(1.0, 1.0, 1.0)[0]);
-	glBegin(GL_LINES);
-		glVertex3fv(&origin[0]);
-		glVertex3fv(&(P1)[0]);
-	glEnd();
-}
-
-
-/*
-=======================================================================================================================================
-PlaneTest                                                                                                              =
-=======================================================================================================================================
-*/
-float lineseg_t::PlaneTest(const plane_t& plane) const
-{
-	const Vec3& P0 = origin;
-	Vec3 P1 = origin+dir;
-
-	float dist0 = plane.Test(P0);
-	float dist1 = plane.Test(P1);
-
-	if(dist0 > 0.0)
-	{
-		if(dist1 < 0.0)
-			return 0.0;
-		else
-			return min(dist0, -dist1);
-	}
-	else
-	{
-		if(dist1 > 0.0)
-			return 0.0;
-		else
-			return min(-dist0, dist1);
-	}
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-line - sphere                                                                                                          =
-=======================================================================================================================================
-*/
-bool lineseg_t::Intersects(const bsphere_t& sphere) const
-{
-	return sphere.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-line - aabb                                                                                                            =
-=======================================================================================================================================
-*/
-bool lineseg_t::Intersects(const aabb_t& box) const
-{
-	return box.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-line - obb                                                                                                             =
-=======================================================================================================================================
-*/
-bool lineseg_t::Intersects(const obb_t& obb) const
-{
-	return obb.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-Transformed                                                                                                            =
-=======================================================================================================================================
-*/
-ray_t ray_t::Transformed(const Vec3& translate, const Mat3& rotate, float scale) const
-{
-	/*ray_t ray;
-
-	Mat4 semi_transf(rotate*scale);
-
-	ray.dir = semi_transf * dir;
-	ray.dir.normalize();
-
-	ray.origin = semi_transf * origin;
-	ray.origin += translate;
-
-	return ray;*/
-	ray_t new_ray;
-
-	new_ray.origin = origin.getTransformed(translate, rotate, scale);
-	new_ray.dir = rotate * dir;
-
-	return new_ray;
-}
-
-
-/*
-=======================================================================================================================================
-render                                                                                                                 =
-=======================================================================================================================================
-*/
-void ray_t::Render()
-{
-	const float dist = 100.0f;
-	glColor3fv(&Vec3(1.0f, 1.0f, 0.0f)[0]);
-
-	// render a dotted without depth
-	glDisable(GL_DEPTH_TEST);
-
-	glBegin(GL_LINES);
-		glVertex3fv(&origin[0]);
-		glVertex3fv(&(dir*dist+origin)[0]);
-	glEnd();
-
-	//glPointSize(4.0f);
-	glBegin(GL_POINTS);
-		glVertex3fv(&origin[0]);
-	glEnd();
-
-	// render with depth
-	glBegin(GL_LINES);
-		glVertex3fv(&origin[0]);
-		glVertex3fv(&(dir*dist+origin)[0]);
-	glEnd();
-
-}
-
-
-/*
-=======================================================================================================================================
-PlaneTest                                                                                                              =
-=======================================================================================================================================
-*/
-float ray_t::PlaneTest(const plane_t& plane) const
-{
-	float dist = plane.Test(origin);
-	float cos_ = plane.normal.dot(dir);
-
-	if(cos_ > 0.0) // the ray points to the same half-space as the plane
-	{
-		if(dist < 0.0) // the ray's origin is behind the plane
-			return 0.0;
-		else
-			return dist;
-	}
-	else
-	{
-		if(dist > 0.0)
-			return 0.0;
-		else
-			return dist;
-	}
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-ray - sphere                                                                                                           =
-=======================================================================================================================================
-*/
-bool ray_t::Intersects(const bsphere_t& sphere) const
-{
-	return sphere.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-ray - aabb                                                                                                             =
-=======================================================================================================================================
-*/
-bool ray_t::Intersects(const aabb_t& box) const
-{
-	return box.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-ray - obb                                                                                                              =
-=======================================================================================================================================
-*/
-bool ray_t::Intersects(const obb_t& obb) const
-{
-	return obb.Intersects(*this);
-}
-
-
-
-/*
-=======================================================================================================================================
-Set                                                                                                                    =
-from plane eqtuation                                                                                                   =
-=======================================================================================================================================
-*/
-void plane_t::Set(float a, float b, float c, float d)
-{
-	// normalize for cheap distance checks
-	float lensq = a*a + b*b + c*c;
-	// length of normal had better not be zero
-	DEBUG_ERR(isZero(lensq));
-
-	// recover gracefully
-	if (isZero(lensq))
-	{
-		normal = Vec3(1.0, 0.0, 0.0);
-		offset = 0.0f;
-	}
-	else
-	{
-		float recip = invSqrt(lensq);
-		normal = Vec3(a*recip, b*recip, c*recip);
-		offset = d*recip;
-	}
-}
-
-
-/*
-=======================================================================================================================================
-Set                                                                                                                    =
-from 3 points                                                                                                          =
-=======================================================================================================================================
-*/
-void plane_t::Set(const Vec3& p0, const Vec3& p1, const Vec3& p2)
-{
-	// get plane vectors
-	Vec3 u = p1 - p0;
-	Vec3 v = p2 - p0;
-
-	normal = u.cross(v);
-
-	// length of normal had better not be zero
-	DEBUG_ERR(isZero(normal.getLengthSquared()));
-
-	normal.normalize();
-	offset = normal.dot(p0); // ToDo: correct??
-
-}
-
-
-/*
-=======================================================================================================================================
-Transformed                                                                                                            =
-=======================================================================================================================================
-*/
-plane_t plane_t::Transformed(const Vec3& translate, const Mat3& rotate, float scale) const
-{
-	plane_t plane;
-
-	// the normal
-	plane.normal = rotate*normal;
-
-	// the offset
-	Vec3 new_trans = rotate.getTransposed() * translate;
-	plane.offset = offset*scale + new_trans.dot(normal);
-
-	return plane;
-}
-
-
-/*
-=======================================================================================================================================
-render                                                                                                                 =
-=======================================================================================================================================
-*/
-void plane_t::Render()
-{
-	/*glPushMatrix();
-
-	Vec3 translate(normal*offset);
-	Quat q;
-	q.setFrom2Vec3(Vec3(0.0, 0.0, 1.0), normal);
-	Mat3 rotate(q);
-	Mat4 transform(translate, rotate);
-	app->getMainRenderer()->multMatrix(transform);
-
-	glColor4fv(&Vec4(1.0f, 1.0f, 1.0f, 0.5f)[0]);
-
-	const float size = 10.0f;
-
-	glBegin(GL_QUADS);
-		glVertex3fv(&Vec3(size, size, 0.0f)[0]);
-		glVertex3fv(&Vec3(-size, size, 0.0f)[0]);
-		glVertex3fv(&Vec3(-size, -size, 0.0f)[0]);
-		glVertex3fv(&Vec3(size, -size, 0.0f)[0]);
-	glEnd();
-
-	glColor4fv(&Vec4(1.0f, 1.0f, 1.0f, 0.2f)[0]);
-	glBegin(GL_QUADS);
-		glVertex3fv(&Vec3(size, -size, 0.0f)[0]);
-		glVertex3fv(&Vec3(-size, -size, 0.0f)[0]);
-		glVertex3fv(&Vec3(-size, size, 0.0f)[0]);
-		glVertex3fv(&Vec3(size, size, 0.0f)[0]);
-	glEnd();
-
-	glPopMatrix();
-
-	glDisable(GL_DEPTH_TEST);
-	glColor3fv(&Vec3(1, 1, 0)[0]);
-	glBegin(GL_LINES);
-		glVertex3fv(&(normal*offset)[0]);
-		glVertex3fv(&(normal*(offset+1))[0]);
-	glEnd();*/
-}
-
-
-
-/*
-=======================================================================================================================================
-Set                                                                                                                    =
-from a vec3 array                                                                                                      =
-=======================================================================================================================================
-*/
-void bsphere_t::Set(const void* pointer, uint stride, int count)
-{
-	void* tmp_pointer = (void*)pointer;
-	Vec3 min(*(Vec3*)tmp_pointer),
-	       max(*(Vec3*)tmp_pointer);
-
-	// for all the vec3 calc the max and min
-	for(int i=1; i<count; i++)
-	{
-		tmp_pointer = (char*)tmp_pointer + stride;
-
-		const Vec3& tmp = *((Vec3*)tmp_pointer);
-
-		for(int j=0; j<3; j++)
-		{
-			if(tmp[j] > max[j])
-				max[j] = tmp[j];
-			else if(tmp[j] < min[j])
-				min[j] = tmp[j];
-		}
-	}
-
-	center = (min+max) * 0.5; // average
-
-	tmp_pointer = (void*)pointer;
-	float max_dist = (*((Vec3*)tmp_pointer) - center).getLengthSquared(); // max distance between center and the vec3 arr
-	for(int i=1; i<count; i++)
-	{
-		tmp_pointer = (char*)tmp_pointer + stride;
-
-		const Vec3& vertco = *((Vec3*)tmp_pointer);
-		float dist = (vertco - center).getLengthSquared();
-		if(dist > max_dist)
-			max_dist = dist;
-	}
-
-	radius = M::sqrt(max_dist);
-}
-
-
-/*
-=======================================================================================================================================
-render                                                                                                                 =
-=======================================================================================================================================
-*/
-void bsphere_t::Render()
-{
-	/// @todo
-	/*glPushMatrix();
-
-	glTranslatef(center.x, center.y, center.z);
-
-	glColor4fv(&Vec4(1.0, 1.0, 1.0, 0.2)[0]);
-
-	app->getMainRenderer()->dbg.drawSphere(radius, 24, Vec3(1.0));
-
-	glPopMatrix();*/
-}
-
-
-/*
-=======================================================================================================================================
-Transformed                                                                                                            =
-=======================================================================================================================================
-*/
-bsphere_t bsphere_t::Transformed(const Vec3& translate, const Mat3& rotate, float scale) const
-{
-	bsphere_t new_sphere;
-
-	new_sphere.center = center.getTransformed(translate, rotate, scale);
-	new_sphere.radius = radius * scale;
-	return new_sphere;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-sphere - sphere                                                                                                        =
-=======================================================================================================================================
-*/
-bool bsphere_t::Intersects(const bsphere_t& other) const
-{
-	float tmp = radius + other.radius;
-	return (center-other.center).getLengthSquared() <= tmp*tmp ;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-sphere - aabb                                                                                                          =
-=======================================================================================================================================
-*/
-bool bsphere_t::Intersects(const aabb_t& box) const
-{
-	return box.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-sphere - ray                                                                                                           =
-=======================================================================================================================================
-*/
-bool bsphere_t::Intersects(const ray_t& ray) const
-{
-	Vec3 w(center - ray.origin);
-	const Vec3& v = ray.dir;
-	float proj = v.dot(w);
-	float wsq = w.getLengthSquared();
-	float rsq = radius*radius;
-
-	if(proj < 0.0 && wsq > rsq)
-		return false;
-
-	float vsq = v.getLengthSquared();
-
-	return (vsq*wsq - proj*proj <= vsq*rsq);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-sphere - segment                                                                                                       =
-=======================================================================================================================================
-*/
-bool bsphere_t::Intersects(const lineseg_t& segment) const
-{
-	const Vec3& v = segment.dir;
-	Vec3 w0 = center - segment.origin;
-	float w0dv = w0.dot(v);
-	float rsq = radius * radius;
-
-	if(w0dv < 0.0f) // if the ang is >90
-		return w0.getLengthSquared() <= rsq;
-
-	Vec3 w1 = w0 - v; // aka center - P1, where P1 = seg.origin + seg.dir
-	float w1dv = w1.dot(v);
-
-	if(w1dv > 0.0f) // if the ang is <90
-		return w1.getLengthSquared() <= rsq;
-
-	Vec3 tmp = w0 - (v * (w0.dot(v) / v.getLengthSquared())); // the big parenthesis is the projection of w0 to v
-	return tmp.getLengthSquared() <= rsq;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-sphere - obb                                                                                                           =
-=======================================================================================================================================
-*/
-bool bsphere_t::Intersects(const obb_t& obb) const
-{
-	return obb.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-PlaneTest                                                                                                              =
-=======================================================================================================================================
-*/
-float bsphere_t::PlaneTest(const plane_t& plane) const
-{
-	float dist = plane.Test(center);
-
-	if(dist > radius)
-		return dist-radius;
-	else if(-dist > radius)
-		return dist+radius;
-	else
-		return 0.0f;
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-sphere - sphere                                                                                                        =
-=======================================================================================================================================
-*/
-bool bsphere_t::SeperationTest(const bsphere_t& sphere, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	normal = sphere.center - center;
-	float rsum = radius + sphere.radius;
-	float distsq = normal.getLengthSquared();
-
-	if(distsq <= rsum*rsum)
-	{
-		// calc the depth
-		float dist = M::sqrt(distsq);
-		depth = rsum - dist;
-
-		normal.normalize();
-
-		impact_point = ((center + normal*radius) + (sphere.center - normal*sphere.radius)) * 0.5f;
-
-		RENDER_SEPERATION_TEST
-
-		return true;
-	}
-
-	return false;
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-sphere - aabb                                                                                                          =
-=======================================================================================================================================
-*/
-bool bsphere_t::SeperationTest(const aabb_t& box, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	UNLOCK_RENDER_SEPERATION
-	bool test = box.SeperationTest(*this, normal, impact_point, depth);
-	LOCK_RENDER_SEPERATION
-	impact_point = impact_point + (normal*depth);
-	normal = -normal;
-
-	if(test) RENDER_SEPERATION_TEST;
-
-	return test;
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-sphere - obb                                                                                                           =
-=======================================================================================================================================
-*/
-bool bsphere_t::SeperationTest(const obb_t& obb, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	UNLOCK_RENDER_SEPERATION
-	bool test = obb.SeperationTest(*this, normal, impact_point, depth);
-	LOCK_RENDER_SEPERATION
-
-	if(!test) return false;
-
-	impact_point = impact_point + (normal*depth);
-	normal = -normal;
-
-	RENDER_SEPERATION_TEST;
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-Set                                                                                                                    =
-Calc origin and radius from a vec3 array                                                                               =
-=======================================================================================================================================
-*/
-void aabb_t::Set(const void* pointer, uint stride, int count)
-{
-	void* tmp_pointer = (void*)pointer;
-	min = *(Vec3*)tmp_pointer;
-	max = *(Vec3*)tmp_pointer;
-
-	// for all the vec3 calc the max and min
-	for(int i=1; i<count; i++)
-	{
-		tmp_pointer = (char*)tmp_pointer + stride;
-
-		Vec3 tmp(*(Vec3*)tmp_pointer);
-
-		for(int j=0; j<3; j++)
-		{
-			if(tmp[j] > max[j])
-				max[j] = tmp[j];
-			else if(tmp[j] < min[j])
-				min[j] = tmp[j];
-		}
-	}
-}
-
-
-/*
-=======================================================================================================================================
-Transformed                                                                                                            =
-=======================================================================================================================================
-*/
-aabb_t aabb_t::Transformed(const Vec3& translate, const Mat3& rotate, float scale) const
-{
-	/*aabb_t aabb;
-	aabb.min = min * scale;
-	aabb.max = max * scale;
-
-	aabb.min += translate;
-	aabb.max += translate;
-	return aabb;*/
-	aabb_t new_aabb;
-
-	// if there is no rotation our job is easy
-	if(rotate == Mat3::getIdentity())
-	{
-		new_aabb.min = (min * scale) + translate;
-		new_aabb.max = (max * scale) + translate;
-	}
-	// if not then we are fucked
-	else
-	{
-		Vec3 points [8] = { max, Vec3(min.x,max.y,max.z), Vec3(min.x,min.y,max.z), Vec3(max.x,min.y,max.z),
-		                      Vec3(max.x,max.y,min.z), Vec3(min.x,max.y,min.z), min, Vec3(max.x,min.y,min.z) };
-
-		for(int i=0; i<8; i++)
-			points[i].transform(translate, rotate, scale);
-
-		new_aabb.Set(points, 0, 8);
-	}
-
-	return new_aabb;
-}
-
-
-/*
-=======================================================================================================================================
-render                                                                                                                 =
-=======================================================================================================================================
-*/
-void aabb_t::Render()
-{
-	/// @todo
-	/*glPushMatrix();
-
-	Vec3 sub(max-min);
-	Vec3 center((max+min)*0.5);
-
-	glTranslatef(center.x, center.y, center.z);
-	glScalef(sub.x, sub.y, sub.z);
-
-	glColor3fv(&Vec3(1.0, 1.0, 1.0)[0]);
-
-	app->getMainRenderer()->dbg.renderCube();
-
-	glPopMatrix();
-
-	glBegin(GL_POINTS);
-		glColor3fv(&Vec3(1.0, 0.0, 0.0)[0]);
-		glVertex3fv(&min[0]);
-		glColor3fv(&Vec3(0.0, 1.0, 0.0)[0]);
-		glVertex3fv(&max[0]);
-	glEnd();*/
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-aabb - aabb                                                                                                            =
-=======================================================================================================================================
-*/
-bool aabb_t::Intersects(const aabb_t& other) const
-{
-	// if separated in x direction
-	if(min.x > other.max.x || other.min.x > max.x)
-		return false;
-
-	// if separated in y direction
-	if(min.y > other.max.y || other.min.y > max.y)
-		return false;
-
-	// if separated in z direction
-	if(min.z > other.max.z || other.min.z > max.z)
-		return false;
-
-	// no separation, must be intersecting
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-aabb - sphere                                                                                                          =
-=======================================================================================================================================
-*/
-bool aabb_t::Intersects(const bsphere_t& sphere) const
-{
-	const Vec3& c = sphere.center;
-
-	// find the box's closest point to the sphere
-	Vec3 cp; // Closest Point
-	for(uint i=0; i<3; i++)
-	{
-		if(c[i] > max[i]) // if the center is greater than the max then the closest point is the max
-			cp[i] = max[i];
-		else if(c[i] < min[i]) // relative to the above
-			cp[i] = min[i];
-		else           // the c lies between min and max
-			cp[i] = c[i];
-	}
-
-	float rsq = sphere.radius * sphere.radius;
-	Vec3 sub = c - cp; // if the c lies totaly inside the box then the sub is the zero,
-	                     //this means that the length is also zero and thus its always smaller than rsq
-
-	if(sub.getLengthSquared() <= rsq) return true;
-
-	return false;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-aabb - ray                                                                                                             =
-=======================================================================================================================================
-*/
-bool aabb_t::Intersects(const ray_t& ray) const
-{
-	float maxS = -FLT_MAX;
-	float minT = FLT_MAX;
-
-	// do tests against three sets of planes
-	for (int i = 0; i < 3; ++i)
-	{
-		// ray is parallel to plane
-		if (isZero(ray.dir[i]))
-		{
-			// ray passes by box
-			if (ray.origin[i] < min[i] || ray.origin[i] > max[i])
-				return false;
-		}
-		else
-		{
-			// compute intersection parameters and sort
-			float s = (min[i] - ray.origin[i])/ray.dir[i];
-			float t = (max[i] - ray.origin[i])/ray.dir[i];
-			if (s > t)
-			{
-				float temp = s;
-				s = t;
-				t = temp;
-			}
-
-			// adjust min and max values
-			if (s > maxS)
-				maxS = s;
-			if (t < minT)
-				minT = t;
-			// check for intersection failure
-			if (minT < 0.0f || maxS > minT)
-				return false;
-		}
-	}
-
-	// done, have intersection
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-aabb - segment                                                                                                         =
-=======================================================================================================================================
-*/
-bool aabb_t::Intersects(const lineseg_t& segment) const
-{
-	float maxS = -FLT_MAX;
-	float minT = FLT_MAX;
-
-	// do tests against three sets of planes
-	for(int i = 0; i < 3; ++i)
-	{
-		// segment is parallel to plane
-		if(isZero(segment.dir[i]))
-		{
-			// segment passes by box
-			if((segment.origin)[i] < min[i] || (segment.origin)[i] > max[i])
-				return false;
-		}
-		else
-		{
-			// compute intersection parameters and sort
-			float s = (min[i] - segment.origin[i])/segment.dir[i];
-			float t = (max[i] - segment.origin[i])/segment.dir[i];
-			if(s > t)
-			{
-				float temp = s;
-				s = t;
-				t = temp;
-			}
-
-			// adjust min and max values
-			if(s > maxS)
-				maxS = s;
-			if(t < minT)
-				minT = t;
-			// check for intersection failure
-			if(minT < 0.0f || maxS > 1.0f || maxS > minT)
-				return false;
-		}
-	}
-
-	// done, have intersection
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-aabb - obb                                                                                                             =
-=======================================================================================================================================
-*/
-bool aabb_t::Intersects(const obb_t& obb) const
-{
-	return obb.Intersects(*this);
-}
-
-
-/*
-=======================================================================================================================================
-PlaneTest                                                                                                              =
-=======================================================================================================================================
-*/
-float aabb_t::PlaneTest(const plane_t& plane) const
-{
-	Vec3 diag_min, diag_max;
-	// set min/max values for x,y,z direction
-	for(int i=0; i<3; i++)
-	{
-		if(plane.normal[i] >= 0.0f)
-		{
-			diag_min[i] = min[i];
-			diag_max[i] = max[i];
-		}
-		else
-		{
-			diag_min[i] = max[i];
-			diag_max[i] = min[i];
-		}
-	}
-
-	// minimum on positive side of plane, box on positive side
-	float test = plane.Test(diag_min);
-	if (test > 0.0f)
-		return test;
-
-	test = plane.Test(diag_max);
-	// min on non-positive side, max on non-negative side, intersection
-	if (test >= 0.0f)
-		return 0.0f;
-	// max on negative side, box on negative side
-	else
-		return test;
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-aabb - aabb                                                                                                            =
-=======================================================================================================================================
-*/
-bool aabb_t::SeperationTest(const aabb_t& other, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	// calculete the closest points
-	for(uint i=0; i<3; i++) // for 3 axis
-	{
-		if(min[i] > other.max[i] || other.min[i] > max[i])
-			return false;
-
-		const float& Am = min[i], AM = max[i], Bm = other.min[i], BM = other.max[i];
-
-		if(Bm < Am)
-		{
-			if(BM < Am) // B is left and outside A
-				return false;
-			else
-				if(BM < AM) // left
-				{
-					normal[i] = Am - BM;
-					impact_point[i] = (Am+BM) * 0.5f;
-				}
-				else // B overlaps A
-				{
-					float t0 = AM-Bm, t1 = BM-Am;
-					if(t0 < t1)
-						normal[i] = t0;
-					else
-						normal[i] = -t1;
-
-					impact_point[i] = (Am+AM) * 0.5f;
-				}
-		}
-		else
-		{
-			if(Bm > AM) // B is right and outside A
-				return false;
-			else
-				if(BM < AM) // B totaly inside A
-				{
-					float t0 = BM-Am, t1 = AM-Bm;
-					if(t0 < t1)
-						normal[i] = -t0;
-					else
-						normal[i] = t1;
-
-					impact_point[i] = (Bm+BM) * 0.5f;
-				}
-				else // right
-				{
-					normal[i] = AM - Bm;
-					impact_point[i] = (AM + Bm) * 0.5f;
-				}
-		}
-
-	}
-
-	Vec3 dist(fabs(normal.x), fabs(normal.y), fabs(normal.z));
-	if(dist.x < dist.y && dist.x < dist.z)
-		normal = Vec3(normal.x, 0.0f, 0.0f);
-	else if(dist.y < dist.z)
-		normal = Vec3(0.0f, normal.y, 0.0f);
-	else
-		normal = Vec3(0.0f, 0.0f, normal.z);
-
-	depth = normal.getLength();
-
-	normal *= 1.0f/depth; // aka normal.normalize()
-
-	RENDER_SEPERATION_TEST
-
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-aabb - obb                                                                                                            =
-=======================================================================================================================================
-*/
-bool aabb_t::SeperationTest(const obb_t& obb, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	UNLOCK_RENDER_SEPERATION
-	bool test = obb.SeperationTest(*this, normal, impact_point, depth);
-	LOCK_RENDER_SEPERATION
-
-	if(!test) return false;
-
-	impact_point = impact_point + (normal*depth);
-	normal = -normal;
-
-	RENDER_SEPERATION_TEST;
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-aabb - sphere                                                                                                          =
-=======================================================================================================================================
-*/
-bool aabb_t::SeperationTest(const bsphere_t& sphere, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	const Vec3& c = sphere.center;
-	const float r = sphere.radius;
-	Vec3 cp; // closest point of box that its closer to the sphere's center
-
-	for(int i=0; i<3; i++)
-	{
-		if(c[i] >= max[i]) // if the center is greater than the max then the closest point is the max
-			cp[i] = max[i];
-		else if(c[i] <= min[i]) // relative to the above
-			cp[i] = min[i];
-		else           // the c lies between min and max
-			cp[i] = c[i];
-	}
-
-	Vec3 sub = c - cp; // if the c lies totaly inside the box then the sub is the zero,
-	                     // this means that the length is also zero and thus its always smaller than rsq
-
-	float sublsq = sub.getLengthSquared();
-	if(sublsq > r*r) return false; // if no collision leave before its too late
-
-	if(isZero(sublsq)) // this means that the closest point is coincide with the center so the center is totaly inside tha box. We have to revise the calcs
-	{
-		int n_axis = 0; // the axis that the normal will be
-		float min_d = FLT_MAX; // in the end of "for" the min_d holds the min projection dist of c to every cube's facet
-		float coord = 0.0;
-		for(int i=0; i<3; i++)
-		{
-			// dist between c and max/min in the i axis
-			float dist_c_max = max[i]-c[i];
-			float dist_c_min = c[i]-min[i];
-
-			if(dist_c_max < min_d && dist_c_max < dist_c_min)
-			{
-				min_d = dist_c_max;
-				n_axis = i;
-				coord = max[i];
-			}
-			else if(dist_c_min < min_d)
-			{
-				min_d = dist_c_min;
-				n_axis = i;
-				coord = min[i];
-			}
-		}
-
-		float dif = coord - c[n_axis];
-
-		normal = Vec3(0.0, 0.0, 0.0);
-		normal[n_axis] = dif / min_d; // aka ... = (dif<0.0f) ? -1.0f : 1.0f;
-
-		depth = r + min_d;
-
-		impact_point = c-(normal*r);
-	}
-	// the c is outside the box
-	else
-	{
-		normal = c - cp;
-
-		depth = r - normal.getLength();
-
-		normal.normalize();
-
-		impact_point = c-(normal*r);
-	}
-
-	RENDER_SEPERATION_TEST
-
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-Set                                                                                                                    =
-calc from a vec3 array                                                                                                 =
-=======================================================================================================================================
-*/
-void obb_t::Set(const void* pointer, uint stride, int count)
-{
-	void* tmp_pointer = (void*)pointer;
-	Vec3 min = *(Vec3*)tmp_pointer;
-	Vec3 max = *(Vec3*)tmp_pointer;
-
-	// for all the vec3 calc the max and min
-	for(int i=1; i<count; i++)
-	{
-		tmp_pointer = (char*)tmp_pointer + stride;
-
-		Vec3 tmp(*(Vec3*)tmp_pointer);
-
-		for(int j=0; j<3; j++) // for x y z
-		{
-			if(tmp[j] > max[j])
-				max[j] = tmp[j];
-			else if(tmp[j] < min[j])
-				min[j] = tmp[j];
-		}
-	}
-
-	// set the locals
-	center = (max+min)*0.5f;
-	rotation = Mat3::getIdentity();
-	extends = max-center;
-}
-
-
-/*
-=======================================================================================================================================
-render                                                                                                                 =
-=======================================================================================================================================
-*/
-void obb_t::Render()
-{
-	/// @todo
-	/*glPushMatrix();
-
-	glTranslatef(center.x, center.y, center.z); // translate
-	app->getMainRenderer()->multMatrix(Mat4(rotation)); // rotate
-	glScalef(extends.x, extends.y, extends.z); // scale
-
-	glColor3fv(&Vec3(1.0f, 1.0f, 1.0f)[0]);
-
-	app->getMainRenderer()->dbg.renderCube(false, 2.0);
-
-	app->getMainRenderer()->color3(Vec3(0.0, 1.0, 0.0));
-	glBegin(GL_POINTS);
-		glVertex3fv(&Vec3(1.0, 1.0, 1.0)[0]);
-	glEnd();
-
-	glPopMatrix();*/
-}
-
-
-/*
-=======================================================================================================================================
-Transformed                                                                                                            =
-=======================================================================================================================================
-*/
-obb_t obb_t::Transformed(const Vec3& translate, const Mat3& rotate, float scale) const
-{
-	obb_t res;
-
-	res.extends = extends * scale;
-	res.center = center.getTransformed(translate, rotate, scale);
-	res.rotation = rotate * rotation;
-
-	return res;
-}
-
-
-/*
-=======================================================================================================================================
-PlaneTest                                                                                                              =
-=======================================================================================================================================
-*/
-float obb_t::PlaneTest(const plane_t& plane) const
-{
-	Vec3 x_normal = rotation.getTransposed() * plane.normal;
-	// maximum extent in direction of plane normal
-	float r = fabs(extends.x*x_normal.x)
-					+ fabs(extends.y*x_normal.y)
-					+ fabs(extends.z*x_normal.z);
-	// signed distance between box center and plane
-	float d = plane.Test(center);
-
-	// return signed distance
-	if(fabs(d) < r)
-		return 0.0f;
-	else if(d < 0.0f)
-		return d + r;
-	else
-		return d - r;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-obb - obb                                                                                                              =
-=======================================================================================================================================
-*/
-bool obb_t::Intersects(const obb_t& other) const
-{
-	// extent vectors
-	const Vec3& a = extends;
-	const Vec3& b = other.extends;
-
-	// test factors
-	float cTest, aTest, bTest;
-	bool parallelAxes = false;
-
-	// transpose of rotation of B relative to A, i.e. (R_b^T * R_a)^T
-	Mat3 Rt = rotation.getTransposed() * other.rotation;
-
-	// absolute value of relative rotation matrix
-	Mat3 Rabs;
-	for(uint i = 0; i < 3; ++i)
-	{
-		for(uint j = 0; j < 3; ++j)
-		{
-			Rabs(i,j) = fabs(Rt(i,j));
-			// if magnitude of dot product between axes is close to one
-			if (Rabs(i,j) + EPSILON >= 1.0f)
-			{
-				// then box A and box B have near-parallel axes
-				parallelAxes = true;
-			}
-		}
-	}
-
-	// relative translation (in A's frame)
-	Vec3 c = rotation.getTransposed()*(other.center - center);
-
-	// separating axis A0
-	cTest = fabs(c.x);
-	aTest = a.x;
-	bTest = b.x*Rabs(0, 0)+b.y*Rabs(0, 1)+b.z*Rabs(0, 2);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A1
-	cTest = fabs(c.y);
-	aTest = a.y;
-	bTest = b.x*Rabs(1, 0)+b.y*Rabs(1, 1)+b.z*Rabs(1, 2);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A2
-	cTest = fabs(c.z);
-	aTest = a.z;
-	bTest = b.x*Rabs(2, 0)+b.y*Rabs(2, 1)+b.z*Rabs(2, 2);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis B0
-	cTest = fabs(c.x*Rt(0, 0) + c.y*Rt(1, 0) + c.z*Rt(2, 0));
-	aTest = a.x*Rabs(0, 0)+a.y*Rabs(1, 0)+a.z*Rabs(2, 0);
-	bTest = b.x;
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis B1
-	cTest = fabs(c.x*Rt(0, 1) + c.y*Rt(1, 1) + c.z*Rt(2, 1));
-	aTest = a.x*Rabs(0, 1)+a.y*Rabs(1, 1)+a.z*Rabs(2, 1);
-	bTest = b.y;
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis B2
-	cTest = fabs(c.x*Rt(0, 2) + c.y*Rt(1, 2) + c.z*Rt(2, 2));
-	aTest = a.x*Rabs(0, 2)+a.y*Rabs(1, 2)+a.z*Rabs(2, 2);
-	bTest = b.z;
-	if (cTest > aTest + bTest) return false;
-
-	// if the two boxes have parallel axes, we're done, intersection
-	if (parallelAxes) return true;
-
-	// separating axis A0 x B0
-	cTest = fabs(c.z*Rt(1, 0)-c.y*Rt(2, 0));
-	aTest = a.y*Rabs(2, 0) + a.z*Rabs(1, 0);
-	bTest = b.y*Rabs(0, 2) + b.z*Rabs(0, 1);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A0 x B1
-	cTest = fabs(c.z*Rt(1, 1)-c.y*Rt(2, 1));
-	aTest = a.y*Rabs(2, 1) + a.z*Rabs(1, 1);
-	bTest = b.x*Rabs(0, 2) + b.z*Rabs(0, 0);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A0 x B2
-	cTest = fabs(c.z*Rt(1, 2)-c.y*Rt(2, 2));
-	aTest = a.y*Rabs(2, 2) + a.z*Rabs(1, 2);
-	bTest = b.x*Rabs(0, 1) + b.y*Rabs(0, 0);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A1 x B0
-	cTest = fabs(c.x*Rt(2, 0)-c.z*Rt(0, 0));
-	aTest = a.x*Rabs(2, 0) + a.z*Rabs(0, 0);
-	bTest = b.y*Rabs(1, 2) + b.z*Rabs(1, 1);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A1 x B1
-	cTest = fabs(c.x*Rt(2, 1)-c.z*Rt(0, 1));
-	aTest = a.x*Rabs(2, 1) + a.z*Rabs(0, 1);
-	bTest = b.x*Rabs(1, 2) + b.z*Rabs(1, 0);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A1 x B2
-	cTest = fabs(c.x*Rt(2, 2)-c.z*Rt(0, 2));
-	aTest = a.x*Rabs(2, 2) + a.z*Rabs(0, 2);
-	bTest = b.x*Rabs(1, 1) + b.y*Rabs(1, 0);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A2 x B0
-	cTest = fabs(c.y*Rt(0, 0)-c.x*Rt(1, 0));
-	aTest = a.x*Rabs(1, 0) + a.y*Rabs(0, 0);
-	bTest = b.y*Rabs(2, 2) + b.z*Rabs(2, 1);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A2 x B1
-	cTest = fabs(c.y*Rt(0, 1)-c.x*Rt(1, 1));
-	aTest = a.x*Rabs(1, 1) + a.y*Rabs(0, 1);
-	bTest = b.x*Rabs(2, 2) + b.z*Rabs(2, 0);
-	if (cTest > aTest + bTest) return false;
-
-	// separating axis A2 x B2
-	cTest = fabs(c.y*Rt(0, 2)-c.x*Rt(1, 2));
-	aTest = a.x*Rabs(1, 2) + a.y*Rabs(0, 2);
-	bTest = b.x*Rabs(2, 1) + b.y*Rabs(2, 0);
-	if (cTest > aTest + bTest) return false;
-
-	// all tests failed, have intersection
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-obb - ray                                                                                                              =
-=======================================================================================================================================
-*/
-bool obb_t::Intersects(const ray_t& ray) const
-{
-	aabb_t aabb_(-extends, extends);
-	ray_t newray;
-	Mat3 rottrans = rotation.getTransposed();
-
-	newray.origin = rottrans * (ray.origin - center);
-	newray.dir = rottrans * ray.dir;
-
-	return aabb_.Intersects(newray);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-obb - segment                                                                                                          =
-ToDo: not working good                                                                                                 =
-=======================================================================================================================================
-*/
-bool obb_t::Intersects(const lineseg_t& segment) const
-{
-	float maxS = -FLT_MAX;
-	float minT = FLT_MAX;
-
-	// compute difference vector
-	Vec3 diff = center - segment.origin;
-
-	// for each axis do
-	for(int i = 0; i < 3; ++i)
-	{
-		// get axis i
-		Vec3 axis = rotation.getColumn(i);
-
-		// project relative vector onto axis
-		float e = axis.dot(diff);
-		float f = segment.dir.dot(axis);
-
-		// ray is parallel to plane
-		if(isZero(f))
-		{
-			// ray passes by box
-			if(-e - extends[i] > 0.0f || -e + extends[i] > 0.0f)
-				return false;
-			continue;
-		}
-
-		float s = (e - extends[i])/f;
-		float t = (e + extends[i])/f;
-
-		// fix order
-		if(s > t)
-		{
-			float temp = s;
-			s = t;
-			t = temp;
-		}
-
-		// adjust min and max values
-		if(s > maxS)
-			maxS = s;
-		if(t < minT)
-			minT = t;
-
-		// check for intersection failure
-		if(minT < 0.0f || maxS > 1.0f || maxS > minT)
-			return false;
-	}
-
-	// done, have intersection
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-obb - sphere                                                                                                           =
-=======================================================================================================================================
-*/
-bool obb_t::Intersects(const bsphere_t& sphere) const
-{
-	aabb_t aabb_(-extends, extends); // aabb_ is in "this" frame
-	Vec3 new_center = rotation.getTransposed() * (sphere.center - center);
-	bsphere_t sphere_(new_center, sphere.radius); // sphere1 to "this" fame
-
-	return aabb_.Intersects(sphere_);
-}
-
-
-/*
-=======================================================================================================================================
-Intersects                                                                                                             =
-obb - aabb                                                                                                             =
-=======================================================================================================================================
-*/
-bool obb_t::Intersects(const aabb_t& aabb) const
-{
-	Vec3 center_ = (aabb.max + aabb.min) * 0.5f;
-	Vec3 extends_ = (aabb.max - aabb.min) * 0.5f;
-	obb_t obb_(center_, Mat3::getIdentity(), extends_);
-
-	return Intersects(obb_);
-}
-
-
-/*
-=======================================================================================================================================
-SeperationTest                                                                                                         =
-obb - sphere                                                                                                           =
-=======================================================================================================================================
-*/
-bool obb_t::SeperationTest(const bsphere_t& sphere, Vec3& normal, Vec3& impact_point, float& depth) const
-{
-	aabb_t aabb_(-extends, extends); // aabb_ is in "this" frame
-	Vec3 new_center = rotation.getTransposed() * (sphere.center - center); // sphere's new center
-	bsphere_t sphere_(new_center, sphere.radius); // sphere_ to "this" frame
-
-	UNLOCK_RENDER_SEPERATION
-	bool test = aabb_.SeperationTest(sphere_, normal, impact_point, depth);
-	LOCK_RENDER_SEPERATION
-
-	if(!test) return false;
-
-	impact_point = (rotation*impact_point) + center;
-	normal = rotation * normal;
-
-	RENDER_SEPERATION_TEST
-
-	return true;
-}
-
-
-
-
-
-

+ 0 - 335
src/Misc/collision.h

@@ -1,335 +0,0 @@
-#ifndef COLLISION_H
-#define COLLISION_H
-
-/*
-Info on how seperation tests work.
-We have: bvolume1.SeperationTest(bvolume2, normal, impact_point, depth);
-This expresion returns the normal, impact_point and depth of the collision between bvolume1 and bvolume2.
-The normal shows the direction we have to move bvolume2 in order to seperate the 2 volumes. The depth shows the distance we have to
-move bvolume2 for the seperation. The impact_point is a point between inside the collision area.
-*/
-
-#include "Common.h"
-#include "Math.h"
-
-class lineseg_t;
-class ray_t;
-class plane_t;
-class bsphere_t;
-class aabb_t;
-class obb_t;
-
-
-/*
-=======================================================================================================================================
-bvolume_t (A)                                                                                                          =
-=======================================================================================================================================
-*/
-class bvolume_t
-{
-	public:
-		enum type_e
-		{
-			LINE_SEG,
-			RAY,
-			PLANE,
-			BSPHERE,
-			AABB,
-			OBB,
-			BVOLUMES_NUM
-		};
-
-		type_e type;
-
-		bvolume_t(type_e type_): type(type_) {}
-
-		virtual void Render() = 0;
-
-		/**
-		 * If the bounding volume intersects with the plane then the func returns 0, else it returns the distance. If the distance is <0 then
-		 * the b.v. lies behind the plane and if >0 then in front of it
-		 */
-		virtual float PlaneTest(const plane_t&) const { ERROR("N.A."); return 0.0; }
-
-		// Intersections
-		virtual bool Intersects(const lineseg_t&)      const { ERROR("N.A."); return false; }
-		virtual bool Intersects(const ray_t&)          const { ERROR("N.A."); return false; }
-		virtual bool Intersects(const bsphere_t&)      const { ERROR("N.A."); return false; }
-		virtual bool Intersects(const aabb_t&)         const { ERROR("N.A."); return false; }
-		virtual bool Intersects(const obb_t&)          const { ERROR("N.A."); return false; }
-		        bool Intersects(const bvolume_t& bv)   const;        ///< Abstract intersection test
-
-		// SeperationTests
-		virtual bool SeperationTest(const bsphere_t&, Vec3&, Vec3&, float&) const { ERROR("N.A."); return false; }
-		virtual bool SeperationTest(const aabb_t&, Vec3&, Vec3&, float&)    const { ERROR("N.A."); return false; }
-		virtual bool SeperationTest(const obb_t&, Vec3&, Vec3&, float&)     const { ERROR("N.A."); return false; }
-		        bool SeperationTest(const bvolume_t& bv, Vec3& normal, Vec3& impact_point, float& depth) const; ///< Abstract seperation test
-};
-
-
-/*
-=======================================================================================================================================
-line segment                                                                                                           =
-=======================================================================================================================================
-*/
-class lineseg_t: public bvolume_t
-{
-	public:
-		// data members
-		Vec3 origin; // P0
-		Vec3 dir;    // P1 = origin+dir so dir = P1-origin
-
-		// constructors & distructors
-		lineseg_t(): bvolume_t(LINE_SEG) {}
-		lineseg_t(const lineseg_t& b): bvolume_t(LINE_SEG) { (*this)=b; }
-		lineseg_t(const Vec3& origin_, const Vec3& dir_): bvolume_t(LINE_SEG) { origin=origin_; dir=dir_; }
-
-		// operators
-		lineseg_t& operator =(const lineseg_t& b) { origin=b.origin; dir=b.dir; return (*this); }
-
-		// std funcs
-		lineseg_t Transformed(const Vec3& translate, const Mat3& rotate, float scale) const;
-		void Render();
-		float PlaneTest(const plane_t&) const;
-		bool Intersects(const bsphere_t& sphere) const;
-		bool Intersects(const aabb_t& aabb) const;
-		bool Intersects(const obb_t& obb) const;
-
-		// other funcs
-		/*
-		1) If t_c<0 then outside the line segment and close to origin. If t_c>1 again outside the line segment and closer to dir.
-		If >0 or <1 then inside the segment
-		2) When we talk about distances we calc the distance between the point|line|ray etc and the P0 OR P1. For example the dist
-		between a point and P0 or P1 depending of t_c
-		*/
-		float  LengthSquared() const { return dir.getLengthSquared(); }
-		float  Length() const { return dir.getLength(); }
-		float  DistanceSquared(const lineseg_t& seg, float& s_c, float& t_c) const;   ///< Dist with another segment
-		float  DistanceSquared(const ray_t& ray, float& s_c, float& t_c) const;       ///< Dist with a ray
-		float  DistanceSquared(const Vec3& point, float& t_c) const;                ///< Dist with a point.
-		void   ClosestPoints(const lineseg_t& seg, Vec3& point0, Vec3& point1) const; ///< Closest points between this and another seg
-		void   ClosestPoints(const ray_t& ray, Vec3& point0, Vec3& point1) const;     ///< Closest points between this and a ray
-		Vec3 ClosestPoints(const Vec3& point) const;                                  ///< Closest points between this and a poin
-};
-
-
-/*
-=======================================================================================================================================
-ray                                                                                                                    =
-=======================================================================================================================================
-*/
-class ray_t: bvolume_t
-{
-	public:
-		// data members
-		Vec3 origin;
-		Vec3 dir; ///< Normalized Vec3
-
-		// constructors & distructors
-		ray_t(): bvolume_t(RAY) {}
-		ray_t(const ray_t& b): bvolume_t(RAY) { (*this)=b; }
-		ray_t(const Vec3& origin_, const Vec3& dir_): bvolume_t(RAY), origin(origin_), dir(dir_) {}
-
-		// operators
-		ray_t& operator =(const ray_t& b) { origin=b.origin; dir=b.dir; return (*this); }
-
-		// std funcs
-		ray_t Transformed(const Vec3& translate, const Mat3& rotate, float scale) const;
-		void Render();
-		float PlaneTest(const plane_t&) const;
-		bool Intersects(const bsphere_t& sphere) const;
-		bool Intersects(const aabb_t& aabb) const;
-		bool Intersects(const obb_t& obb) const;
-
-		// other funcs
-		float  DistanceSquared(const ray_t& ray, float& s_c, float& t_c) const;   // this and another ray
-		void   ClosestPoints(const ray_t& ray, Vec3& point0, Vec3& point1) const;   // this and another ray
-		Vec3 ClosestPoint(const Vec3& point) const;                                 // this and point
-};
-
-
-/*
-=======================================================================================================================================
-plane                                                                                                                  =
-=======================================================================================================================================
-*/
-class plane_t: public bvolume_t
-{
-	public:
-		// data members
-		Vec3 normal;
-		float  offset;
-
-		// constructors & distructors
-		plane_t(): bvolume_t(PLANE) {}
-		plane_t(const plane_t& b): bvolume_t(PLANE) { (*this)=b; }
-		plane_t(const Vec3& normal_, float offset_): bvolume_t(PLANE), normal(normal_), offset(offset_) {}
-		plane_t(const Vec3& p0, const Vec3& p1, const Vec3& p2): bvolume_t(PLANE) { Set(p0,p1,p2); }
-		plane_t(float a, float b, float c, float d): bvolume_t(PLANE) { Set(a,b,c,d); }
-
-		// operators
-		plane_t& operator =(const plane_t& b) { normal=b.normal; offset=b.offset; return (*this); }
-
-		// std funcs
-		plane_t Transformed(const Vec3& translate, const Mat3& rotate, float scale) const;
-		void Render();
-
-		// other funcs
-		void Set(const Vec3& p0, const Vec3& p1, const Vec3& p2); ///< Set the plane from 3 vectors
-		void Set(float a, float b, float c, float d); ///< Set from plane where plane equation is ax+by+cz+d
-
-		/**
-		 * It gives the distance between a point and a plane. if returns >0 then the point lies in front of the plane, if <0 then it
-		 * is behind and if =0 then it is co-planar
-		 */
-		float Test(const Vec3& point) const { return normal.dot(point) - offset; }
-
-		float Distance(const Vec3& point) const { return fabs(Test(point)); }
-
-		/**
-		 * Returns the perpedicular point of a given point in this plane. Plane's normal and returned-point are perpedicular
-		 */
-		Vec3 ClosestPoint(const Vec3& point) const { return point - normal*Test(point); };
-};
-
-
-/*
-=======================================================================================================================================
-bounding sphere                                                                                                        =
-=======================================================================================================================================
-*/
-class bsphere_t: public bvolume_t
-{
-	public:
-		// data members
-		Vec3 center;
-		float radius;
-
-		// constructors & distractor
-		bsphere_t(): bvolume_t(BSPHERE) {}
-		bsphere_t(const bsphere_t& other): bvolume_t(BSPHERE) { (*this) = other; }
-		bsphere_t(const Vec3& center_, float radius_): bvolume_t(BSPHERE), center(center_), radius(radius_) {}
-
-		// operators
-		bsphere_t& operator =(const bsphere_t& other) { center=other.center; radius=other.radius; return (*this); }
-
-		// std funcs
-		bsphere_t Transformed(const Vec3& translate, const Mat3& rotate, float scale) const;
-		void Render();
-		float PlaneTest(const plane_t& plane) const;
-		bool Intersects(const ray_t& ray) const;
-		bool Intersects(const lineseg_t& segment) const;
-		bool Intersects(const bsphere_t& sphere) const;
-		bool Intersects(const aabb_t& aabb) const;
-		bool Intersects(const obb_t& obb) const;
-		bool SeperationTest(const bsphere_t& sphere, Vec3& normal, Vec3& impact_point, float& depth) const;
-		bool SeperationTest(const aabb_t& aabb, Vec3& normal, Vec3& impact_point, float& depth) const;
-		bool SeperationTest(const obb_t& obb, Vec3& normal, Vec3& impact_point, float& depth) const;
-
-		// other funcs
-		void Set(const void* pointer, uint stride, int count); ///< Set from vec3 array
-};
-
-
-/*
-=======================================================================================================================================
-axis aligned bounding box                                                                                              =
-=======================================================================================================================================
-*/
-class aabb_t: public bvolume_t
-{
-	public:
-		// data members
-		Vec3 min;
-		Vec3 max;
-
-		// constructors & destractor
-		aabb_t(): bvolume_t(AABB) {}
-		aabb_t(const aabb_t& other): bvolume_t(AABB) { (*this) = other; }
-		aabb_t(const Vec3& min_, const Vec3& max_): bvolume_t(AABB), min(min_), max(max_) { DEBUG_ERR(max.x<min.x || max.y<min.y || max.z<min.z); }
-
-		// operators
-		aabb_t& operator =(const aabb_t& other) { min=other.min; max=other.max; return (*this); }
-
-		// std funcs
-		aabb_t Transformed(const Vec3& translate, const Mat3& rotate, float scale) const;
-		void Render();
-		float PlaneTest(const plane_t& plane) const;
-		bool Intersects(const ray_t& ray) const;
-		bool Intersects(const lineseg_t& segment) const;
-		bool Intersects(const bsphere_t& sphere) const;
-		bool Intersects(const aabb_t& aabb) const;
-		bool Intersects(const obb_t& obb) const;
-		bool SeperationTest(const bsphere_t& sphere, Vec3& normal, Vec3& impact_point, float& depth) const;
-		bool SeperationTest(const aabb_t& aabb, Vec3& normal, Vec3& impact_point, float& depth) const;
-		bool SeperationTest(const obb_t& oob, Vec3& normal, Vec3& impact_point, float& depth) const;
-
-		// other funcs
-		void Set(const void* pointer, uint stride, int count); ///< Set from vec3 array
-};
-
-
-/*
-=======================================================================================================================================
-object oriented bounding box                                                                                           =
-=======================================================================================================================================
-*/
-class obb_t: public bvolume_t
-{
-	public:
-		// data members
-		Vec3 center;
-		Mat3 rotation;
-		Vec3 extends;
-
-		// constructors & destractor
-		obb_t(): bvolume_t(OBB) {}
-		obb_t(const obb_t& other): bvolume_t(OBB) { (*this)=other; }
-		obb_t(const Vec3& c_, const Mat3& r_, const Vec3& e_): bvolume_t(OBB) { center=c_; rotation=r_; extends=e_; }
-
-		// operators
-		obb_t& operator =(const obb_t& other) { center=other.center; rotation=other.rotation; extends=other.extends; return (*this); }
-
-		// std funcs
-		obb_t Transformed(const Vec3& translate, const Mat3& rotate, float scale) const;
-		void  Render();
-		float PlaneTest(const plane_t& plane) const;
-		bool Intersects(const ray_t& ray) const;
-		bool Intersects(const lineseg_t& segment) const;
-		bool Intersects(const bsphere_t& sphere) const;
-		bool Intersects(const aabb_t& aabb) const;
-		bool Intersects(const obb_t& obb) const;
-		bool SeperationTest(const bvolume_t& bv, Vec3& normal, Vec3& impact_point, float& depth) const;
-		bool SeperationTest(const bsphere_t& sphere, Vec3& normal, Vec3& impact_point, float& depth) const;
-		bool SeperationTest(const aabb_t& /*aabb*/, Vec3& /*normal*/, Vec3& /*impact_point*/, float& /*depth*/) const { ERROR("ToDo"); return false; }
-		bool SeperationTest(const obb_t& /*obb*/, Vec3& /*normal*/, Vec3& /*impact_point*/, float& /*depth*/) const { ERROR("ToDo"); return false; }
-
-		// other funcs
-		void Set(const void* pointer, uint stride, int count); ///< Set from vec3 array
-};
-
-
-/*
-=======================================================================================================================================
-collision triangle                                                                                                     =
-=======================================================================================================================================
-*/
-/*class cTriangle: public bvolume_t
-{
-	public:
-		// data members
-		Vec3 a, b, c;
-
-		// constructors & destractor
-		cTriangle() {}
-		cTriangle(const Vec3& a_, const Vec3& b_, const Vec3& c_): a(a_), b(b_), c(c_) {}
-		cTriangle(const)
-
-		// operators
-		cTriangle& operator =(const cTriangle& other) { a=other.a; b=other.b; c=other.c; return (*this); }
-
-		// std funcs
-};*/
-
-
-#endif

+ 0 - 1013
src/Misc/memory.cpp

@@ -1,1013 +0,0 @@
-#include "memory.h"
-#include <SDL/SDL.h>
-
-#ifdef _USE_MEM_MANAGER_
-
-namespace mem {
-
-#ifdef malloc
-#undef malloc
-#endif
-
-#ifdef realloc
-#undef realloc
-#endif
-
-#ifdef calloc
-#undef calloc
-#endif
-
-#ifdef free
-#undef free
-#endif
-
-#ifdef new
-#undef new
-#endif
-
-#ifdef delete
-#undef delete
-#endif
-
-
-#if DEBUG_ENABLED == 1
-	#define SANITY_CHECKS SanityChecks();
-	#define PRINT_CALL_INFO(x) { if(mem::print_call_info) INFO(x) }
-#else
-	#define SANITY_CHECKS
-	#define PRINT_CALL_INFO(x)
-#endif
-
-#define MERROR(x) {++errors_num; if(print_errors) ERROR(x)}
-
-/*
-=======================================================================================================================================
-variables and types                                                                                                    =
-=======================================================================================================================================
-*/
-
-// owner info for the block
-struct mblock_owner_t
-{
-	const char* file;
-	int         line;
-	const char* func;
-};
-
-// used as a list node
-struct mem_block_t
-{
-	void*          addr;
-	size_t         size;     // aka offset
-	bool           free_space;
-	bool           active;   // if true then the block/node is an active node of the list
-	uint   id;      // the id in the mem_blocks array
-	mem_block_t*   prev;
-	mem_block_t*   next;
-	mblock_owner_t owner;  // for leak tracking
-};
-
-// the buffer
-const size_t buffer_size = 30*MEGABYTE;
-char prealloced_buff [buffer_size];
-void* buffer = prealloced_buff;
-size_t free_size = buffer_size;
-
-// block stuff
-const int MAX_MEM_BLOCKS = 20*KILOBYTE;
-mem_block_t mem_blocks[MAX_MEM_BLOCKS]; // this is actualy a list
-uint active_mem_blocks_num = 3;
-mem_block_t& head_node = mem_blocks[0];
-mem_block_t& tail_node = mem_blocks[1];
-
-// dummy
-static void DummyFunc() {}
-
-// Used so we can save a check in NewBlock
-static void init();
-void (*p_Init)(void) = init;
-
-// threads
-void (*p_Lock)(void) = DummyFunc;
-void (*p_Unlock)(void) = DummyFunc;
-SDL_sem* semaphore = NULL;
-
-// unknown owner
-mblock_owner_t unknown_owner = {"??", 0, "??"};
-
-// times we called each
-uint malloc_called_num = 0;
-uint calloc_called_num = 0;
-uint realloc_called_num = 0;
-uint free_called_num = 0;
-uint new_called_num = 0;
-uint delete_called_num = 0;
-
-// errors & other
-bool print_errors = false;
-uint errors_num = 0;
-bool print_call_info = false; // works only in debug
-
-/*
-=======================================================================================================================================
-FreeBlocksNum                                                                                                          =
-=======================================================================================================================================
-*/
-static int FreeBlocksNum()
-{
-	mem_block_t* mb = head_node.next;
-	int num = 0;
-	do
-	{
-		if(mb->free_space)
-			++num;
-		mb = mb->next;
-	} while(mb != &tail_node);
-	return num;
-}
-
-/*
-=======================================================================================================================================
-SetOwner                                                                                                               =
-set the file,func,line to the given block                                                                              =
-=======================================================================================================================================
-*/
-static inline void SetOwner(mem_block_t* mb, mblock_owner_t* owner)
-{
-	DEBUG_ERR(mb == &head_node || mb == &tail_node); // shouldn't change the head_node or tail node
-	mb->owner.file = owner->file;
-	mb->owner.line = owner->line;
-	mb->owner.func = owner->func;
-}
-
-
-/*
-=======================================================================================================================================
-SanityChecks                                                                                                           =
-=======================================================================================================================================
-*/
-static bool SanityChecks()
-{
-	// the head_node
-	if(!(head_node.addr == NULL || head_node.size == 0 || head_node.prev == NULL || head_node.id == 0 ||
-	    head_node.active == true || head_node.free_space == false))
-		MERROR("In head_node");
-
-	// check the list
-	uint num = 0;
-	for(int i=0; i<MAX_MEM_BLOCKS; i++)
-		if(mem_blocks[i].active) ++num;
-
-	if(active_mem_blocks_num != num) MERROR("In mem_blocks list");
-
-	// check the size
-	size_t size = 0;
-	mem_block_t* mb = head_node.next;
-	do
-	{
-		if(!mb->free_space)
-			size += mb->size;
-
-		// the prev's next has to be ME and the next's prev has to show me also
-		if(mb->prev->next!=mb || mb->next->prev!=mb)
-			MERROR("Chain is broken");
-
-		if(mb->next!=&tail_node && ((char*)mb->addr)+mb->size!=mb->next->addr)
-			MERROR("In crnt and next sizes cohisency");
-
-		if(mb->next == NULL || mb->prev==NULL)
-			MERROR("Prev or next are NULL");
-
-		mb = mb->next;
-	} while(mb!=&tail_node);
-
-	if(size != buffer_size-free_size) MERROR("In size");
-
-	return true;
-}
-
-
-/*
-=======================================================================================================================================
-BytesStr                                                                                                               =
-=======================================================================================================================================
-*/
-static char* BytesStr(size_t size)
-{
-	static char str[10];
-
-	if(size > MEGABYTE)
-		sprintf(str, "%dMB", (uint)(size/MEGABYTE));
-	else if(size > KILOBYTE)
-		sprintf(str, "%dKB", (uint)(size/KILOBYTE));
-	else
-		sprintf(str, "%dB ", (uint)(size));
-	return str;
-}
-
-
-/*
-=======================================================================================================================================
-printBlockInfo                                                                                                         =
-=======================================================================================================================================
-*/
-static void printBlockInfo(const mem_block_t* mb)
-{
-	const char cond = (mb->free_space) ? 'F' : 'U';
-	cout << setw(4) << setfill(' ') << mb->id << setw(0) << ' ' << cond << ' ' << setw(6) <<  BytesStr(mb->size) << setw(0) << hex <<
-	     " 0x" << mb->addr << dec;
-
-	if(cond=='U') cout << " " << mb->owner.file << ' ' << mb->owner.line << ' ' << mb->owner.func;
-
-	cout << endl;
-}
-
-
-/*
-=======================================================================================================================================
-printInfo                                                                                                              =
-=======================================================================================================================================
-*/
-void printInfo(uint flags)
-{
-	cout << "\n=========================== MEM REPORT =========================" << endl;
-
-	// header
-	if((flags & PRINT_ALL)==PRINT_ALL || (flags & PRINT_HEADER)==PRINT_HEADER)
-	{
-		cout << "Used space: " << BytesStr(buffer_size-free_size) << "(" << buffer_size-free_size << ")";
-		cout << ", free: " << BytesStr(free_size) << " (" << free_size << ")";
-		cout << ", total: " << BytesStr(buffer_size) << " (" << buffer_size << ")" << endl;
-
-		int num = FreeBlocksNum();
-		cout << "Active blocks: " << active_mem_blocks_num << "(free space: " << num << ", used space: " << active_mem_blocks_num-num <<
-						"), total: " << MAX_MEM_BLOCKS << endl;
-
-		// get the block with the max free space
-		mem_block_t* tmp = head_node.next;
-		mem_block_t* mb = &head_node;
-		do
-		{
-			if(tmp->free_space && tmp->size > mb->size)
-				mb = tmp;
-			tmp = tmp->next;
-		} while(tmp!=&tail_node);
-
-		cout << "Block with max free space: " << mb->id << ", size: " << BytesStr(mb->size) << " (" << mb->size << ")" << endl;
-
-		// print how many times malloc,realloc etc have been called
-		cout << "Func calls: malloc:" << malloc_called_num << ", calloc:" << calloc_called_num << ", realloc:" << realloc_called_num <<
-		     ", free:" << free_called_num << ", new:" << new_called_num << ", delete:" << delete_called_num << endl;
-
-		cout << "Errors count:" << errors_num << endl;
-	}
-
-	// blocks
-	if((flags & PRINT_ALL)==PRINT_ALL || (flags & PRINT_BLOCKS)==PRINT_BLOCKS)
-	{
-		cout << "Block table (id, type, size [, file, line, func]):" << endl;
-
-		mem_block_t* mb = head_node.next;
-		do
-		{
-			printBlockInfo(mb);
-			mb = mb->next;
-		} while(mb!=&tail_node);
-	}
-
-	cout << "================================================================\n" << endl;
-}
-
-
-/*
-=======================================================================================================================================
-init                                                                                                                   =
-=======================================================================================================================================
-*/
-static void init()
-{
-#if DEBUG_ENABLED == 1
-	memset(buffer, (char)0xCC, buffer_size);
-#endif
-
-	// mem block stuff
-
-	// set the head block. Its the head of the list
-	head_node.addr = NULL;
-	head_node.size = 0;
-	head_node.prev = NULL;
-	head_node.next = &mem_blocks[2];
-	head_node.id = 0;
-	head_node.active = true;
-	head_node.free_space = false;
-
-	// set the head block. Its the head of the list
-	tail_node.addr = NULL;
-	tail_node.size = 0;
-	tail_node.prev = &mem_blocks[2];
-	tail_node.next = NULL;
-	tail_node.id = 1;
-	tail_node.active = true;
-	tail_node.free_space = false;
-
-	// set the first block
-	mem_blocks[2].addr = buffer;
-	mem_blocks[2].size = buffer_size;
-	mem_blocks[2].prev = &head_node;
-	mem_blocks[2].next = &tail_node;
-	mem_blocks[2].id = 2;
-	mem_blocks[2].active = true;
-	mem_blocks[2].free_space = true;
-
-	// set the rest
-	memset(&mem_blocks[3], 0, sizeof(mem_block_t)*(MAX_MEM_BLOCKS-3));
-	for(int i=3; i<MAX_MEM_BLOCKS; i++)
-	{
-		mem_blocks[i].id = i;
-	}
-
-	p_Init = DummyFunc;
-
-	semaphore = SDL_CreateSemaphore(1);
-}
-
-
-/*
-=======================================================================================================================================
-thread stuff                                                                                                           =
-=======================================================================================================================================
-*/
-static void Lock()
-{
-	if(SDL_SemWait(semaphore)==-1)
-		MERROR("Cant lock semaphore");
-}
-
-static void Unlock()
-{
-	if(SDL_SemPost(semaphore)==-1)
-		MERROR("Cant unlock semaphore");
-}
-
-
-/*
-=======================================================================================================================================
-Enable                                                                                                                 =
-=======================================================================================================================================
-*/
-void Enable(uint flags)
-{
-	if((flags & THREADS)==THREADS)
-	{
-		p_Lock = Lock;
-		p_Unlock = Unlock;
-	}
-
-	if((flags & PRINT_ERRORS)==PRINT_ERRORS)
-		print_errors = true;
-
-
-	if((flags & PRINT_CALL_INFO)==PRINT_CALL_INFO)
-		print_call_info = true;
-}
-
-
-/*
-=======================================================================================================================================
-Disable                                                                                                                =
-=======================================================================================================================================
-*/
-void Disable(uint flags)
-{
-	if((flags & THREADS)==THREADS)
-	{
-		p_Lock = DummyFunc;
-		p_Unlock = DummyFunc;
-	}
-
-	if((flags & PRINT_ERRORS)==PRINT_ERRORS)
-		print_errors = false;
-
-	if((flags & PRINT_CALL_INFO)==PRINT_CALL_INFO)
-		print_call_info = false;
-}
-
-
-/*
-=======================================================================================================================================
-GetBlock                                                                                                               =
-find the active block who has for addr the given ptr param. Func used by free and realloc                              =
-=======================================================================================================================================
-*/
-static mem_block_t* GetBlock(void* ptr)
-{
-	//if(ptr<buffer || ptr>((char*)buffer+buffer_size)) return &head_node;
-
-	mem_block_t* mb = tail_node.prev;
-	do
-	{
-		if(mb->addr==ptr)
-			return mb;
-		mb = mb->prev;
-	} while(mb!=&head_node);
-
-	return NULL;
-
-//
-//	int a = 1;
-//	int b = active_mem_blocks_num-2;
-//	mem_block_t* mb = head_node.next;
-//	int pos = 1;
-//
-//	for(;;)
-//	{
-//		int tmp = (a+b)/2;
-//
-//		// move the mb to crnt_pos
-//		if(pos < tmp)
-//			for(int i=0; i<tmp-pos; i++)
-//				mb = mb->next;
-//		else
-//			for(int i=0; i<pos-tmp; i++)
-//				mb = mb->prev;
-//		pos = tmp;
-//
-//		if(ptr < mb->addr)
-//			b = pos;
-//		else if(ptr > mb->addr)
-//			a = pos;
-//		else
-//			return mb;
-//		if(b-a < 2) break;
-//	}
-//
-//	return NULL;
-}
-
-
-/*
-=======================================================================================================================================
-GetInactiveBlock                                                                                                       =
-get an inactive node/block                                                                                             =
-=======================================================================================================================================
-*/
-static mem_block_t* GetInactiveBlock()
-{
-	for(int i=2; i<MAX_MEM_BLOCKS; i++)
-	{
-		if(!mem_blocks[i].active)
-			return &mem_blocks[i];
-	}
-
-	FATAL("Cannot find an inactive node. Inc the mem_blocks arr");
-	return NULL;
-}
-
-
-/*
-=======================================================================================================================================
-WorstFit                                                                                                               =
-"worst fit" algorithm. It returns the block with the biger free space                                                  =
-=======================================================================================================================================
-*/
-static mem_block_t* WorstFit(size_t size)
-{
-	mem_block_t* tmp = tail_node.prev;
-	mem_block_t* candidate = &head_node;
-	do
-	{
-		if(tmp->size > candidate->size && tmp->free_space)
-			candidate = tmp;
-		tmp = tmp->prev;
-	} while(tmp!=&head_node);
-
-	return candidate;
-}
-
-
-/*
-=======================================================================================================================================
-BestFit                                                                                                                =
-=======================================================================================================================================
-*/
-static mem_block_t* BestFit(size_t size)
-{
-	mem_block_t* tmp = tail_node.prev;
-	mem_block_t* candidate = &head_node;
-
-	// find a free block firstly
-	do
-	{
-		if(tmp->free_space)
-		{
-			candidate = tmp;
-			break;
-		}
-		tmp = tmp->prev;
-	} while(tmp!=&head_node);
-
-	if(candidate == &head_node) return candidate; // we failed to find free node
-
-	// now run the real deal
-	do
-	{
-		if(tmp->free_space)
-		{
-			if((tmp->size < candidate->size) && (tmp->size > size))
-				candidate = tmp;
-			else if(tmp->size == size)
-				return tmp;
-		}
-		tmp = tmp->prev;
-	} while(tmp!=&head_node);
-
-	return candidate;
-}
-
-
-/*
-=======================================================================================================================================
-BadFit                                                                                                                 =
-=======================================================================================================================================
-*/
-static mem_block_t* BadFit(size_t size)
-{
-	mem_block_t* tmp = tail_node.prev;
-	do
-	{
-		if(tmp->size >= size && tmp->free_space)
-			return tmp;
-		tmp = tmp->prev;
-	} while(tmp!=&head_node);
-
-	return &head_node;
-}
-
-
-/*
-=======================================================================================================================================
-NewBlock                                                                                                               =
-just free the given block                                                                                              =
-=======================================================================================================================================
-*/
-static mem_block_t* NewBlock(size_t size)
-{
-	p_Init();
-
-	// a simple check
-	if(size < 1)
-	{
-		MERROR("Size is < 1");
-		return &head_node;
-	}
-
-	// get an inactive block
-	mem_block_t* newmb = GetInactiveBlock();
-
-	// use an algorithm to find the best candidate
-	mem_block_t* candidate = BestFit(size);
-	if(candidate==&head_node)
-	{
-		FATAL("There are no free blocks");
-		return &head_node;
-	}
-
-	// case 0: we have found a big enought free block
-	if(candidate->size > size)
-	{
-		// reorganize the prev and the next of the 3 involved blocks
-		DEBUG_ERR(candidate->prev==NULL);
-		candidate->prev->next = newmb;
-		newmb->prev = candidate->prev;
-		newmb->next = candidate;
-		candidate->prev = newmb;
-
-		// do the rest of the changes
-		newmb->addr = candidate->addr;
-		newmb->size = size;
-
-		candidate->addr = ((char*)candidate->addr) + size;
-		candidate->size -= size;
-
-		newmb->active = true;
-		newmb->free_space = false;
-		++active_mem_blocks_num;
-	}
-	// case 1: we have found a block with the exchact space
-	else if(candidate->size == size)
-	{
-		newmb = candidate;
-		newmb->free_space = false;
-	}
-	// case 2: we cannot find a block!!!
-	else // if(max_free_bytes < bytes)
-	{
-		FATAL("Cant find block with " << size << " free space. Inc buffer");
-		return &head_node;
-	}
-
-	free_size -= size;
-	return newmb;
-}
-
-
-/*
-=======================================================================================================================================
-FreeBlock                                                                                                              =
-=======================================================================================================================================
-*/
-static void FreeBlock(mem_block_t* crnt)
-{
-	DEBUG_ERR(crnt->free_space || !crnt->active || crnt==&head_node || crnt==&tail_node); // self explanatory
-
-	free_size += crnt->size;
-
-#if DEBUG_ENABLED == 1
-	memset(crnt->addr, (char)0xCC, crnt->size);
-#endif
-
-	// rearange the blocks
-	mem_block_t* prev = crnt->prev;
-	mem_block_t* next = crnt->next;
-	// if we have a prev block with free space we resize the prev and then we remove the current one
-	if(prev != &head_node && prev->free_space)
-	{
-		prev->size += crnt->size;
-		prev->next = next;
-		next->prev = prev;
-
-		// remove the crnt block from the list
-		crnt->active = false;
-		--active_mem_blocks_num;
-
-		// rearange the blocks for the next check
-		crnt = prev;
-		prev = crnt->prev;
-	}
-
-	// if we have a next block with free space we resize the next and then we remove the crnt one
-	if(next != &tail_node && next->free_space)
-	{
-		next->addr = crnt->addr;
-		next->size += crnt->size;
-		next->prev = prev;
-		prev->next = next;
-
-		// remove the next block from the list
-		crnt->active = false;
-		--active_mem_blocks_num;
-	}
-
-	crnt->free_space = true;
-}
-
-
-/*
-=======================================================================================================================================
-ReallocBlock                                                                                                           =
-it gets the block we want to realloc and returns the reallocated (either the same or a new)                            =
-=======================================================================================================================================
-*/
-static mem_block_t* ReallocBlock(mem_block_t* crnt, size_t size)
-{
-	DEBUG_ERR(crnt->free_space || !crnt->active || crnt==&head_node || crnt==&tail_node); // self explanatory
-
-
-	// case 0: If size is 0 and p points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL...
-	// ...pointer is returned.(ISO behaviour)
-	if(size==0)
-	{
-		FreeBlock(crnt);
-		crnt = &head_node;
-	}
-	// case 1: we want more space
-	else if(size > crnt->size)
-	{
-		mem_block_t* next = crnt->next;
-		// case 1.0: the next block has enough space. Then we eat from the next
-		if(next!=&tail_node && next->free_space && next->size >= size)
-		{
-			free_size -= size - crnt->size;
-			next->addr = ((char*)next->addr) + (size - crnt->size); // shift right the addr
-			next->size -= size - crnt->size;
-			crnt->size = size;
-		}
-		// case 1.1: We cannot eat from the next. Create new block and move the crnt's data there
-		else
-		{
-			mem_block_t* mb = NewBlock(size);
-			memcpy(mb->addr, crnt->addr, crnt->size);
-			FreeBlock(crnt);
-			crnt = mb;
-		}
-	}
-	// case 2: we want less space
-	else if(size < crnt->size)
-	{
-		mem_block_t* next = crnt->next;
-		// case 2.0: we have next
-		if(next!=&tail_node)
-		{
-			// case 2.0.0: the next block is free space...
-			// ...resize next and crnt
-			if(next->free_space)
-			{
-				free_size -= size - crnt->size;
-				next->addr = ((char*)next->addr) - (crnt->size - size); // shl
-				next->size += crnt->size - size;
-				crnt->size = size;
-			}
-			// case 2.0.1: the next block is used space. Create new free block
-			else
-			{
-				free_size -= size - crnt->size;
-				mem_block_t* newmb = GetInactiveBlock();
-				newmb->active = true;
-				newmb->free_space = true;
-				newmb->prev = crnt;
-				newmb->next = next;
-				newmb->addr = ((char*)crnt->addr) + size;
-				newmb->size = crnt->size - size;
-
-				next->prev = newmb;
-
-				crnt->size = size;
-				crnt->next = newmb;
-			}
-		}
-		// case 2.1: We DONT have next. Create a new node
-		else
-		{
-			free_size -= size - crnt->size;
-			mem_block_t* newmb = GetInactiveBlock();
-			newmb->active = true;
-			newmb->free_space = true;
-			newmb->prev = crnt;
-			newmb->next = next;
-			newmb->addr = ((char*)crnt->addr) + size;
-			newmb->size = crnt->size - size;
-
-			crnt->size = size;
-			crnt->next = newmb;
-		}
-	}
-
-	return crnt;
-}
-
-
-/*
-=======================================================================================================================================
-Malloc                                                                                                                 =
-=======================================================================================================================================
-*/
-static void* Malloc(size_t size, mblock_owner_t* owner=&unknown_owner)
-{
-	p_Lock();
-
-	PRINT_CALL_INFO("caller: \"" << owner->file << ':' << owner->line << "\", size: " << size);
-
-	mem_block_t* mb = NewBlock(size);
-	SetOwner(mb, owner);
-	SANITY_CHECKS
-
-	p_Unlock();
-	return mb->addr;
-}
-
-
-/*
-=======================================================================================================================================
-Calloc                                                                                                                 =
-=======================================================================================================================================
-*/
-static void* Calloc(size_t num, size_t size, mblock_owner_t* owner=&unknown_owner)
-{
-	p_Lock();
-
-
-	PRINT_CALL_INFO("caller: \"" << owner->file << ':' << owner->line << "size: " << size);
-
-	mem_block_t* mb = NewBlock(num*size);
-	SetOwner(mb, owner);
-	memset(mb->addr, 0x00000000, num*size);
-	SANITY_CHECKS
-
-	p_Unlock();
-	return mb->addr;
-}
-
-
-/*
-=======================================================================================================================================
-Realloc                                                                                                                =
-=======================================================================================================================================
-*/
-static void* Realloc(void* ptr, size_t size, mblock_owner_t* owner=&unknown_owner)
-{
-	p_Lock();
-
-	// ISO beheviur
-	if(ptr==NULL)
-	{
-		p_Unlock();
-		return Malloc(size, owner);
-	}
-
-	// find the block we want to realloc
-	mem_block_t* mb = GetBlock(ptr);
-
-	PRINT_CALL_INFO("caller: \"" << owner->file << ':' << owner->line << "\", user: \"" << mb->owner.file << ':' << mb->owner.line <<
-		"\", new size: " << size);
-
-	if(mb==NULL)
-	{
-		MERROR("Addr 0x" << hex << ptr << dec << " not found");
-		p_Unlock();
-		return NULL;
-	}
-	if(mb->free_space)
-	{
-		MERROR("Addr 0x" << hex << ptr << dec << " is free space");
-		p_Unlock();
-		return NULL;
-	}
-
-	mem_block_t* crnt = ReallocBlock(mb, size);
-	SetOwner(crnt, owner);
-	SANITY_CHECKS
-
-	p_Unlock();
-	return crnt->addr;
-}
-
-
-/*
-=======================================================================================================================================
-Free                                                                                                                   =
-=======================================================================================================================================
-*/
-static void Free(void* ptr, mblock_owner_t* owner=&unknown_owner)
-{
-	p_Lock();
-
-	// find the block we want to delete
-	mem_block_t* mb = GetBlock(ptr);
-	if(mb==NULL)
-	{
-		MERROR("Addr 0x" << hex << ptr << dec << " not found");
-		p_Unlock();
-		return;
-	}
-	if(mb->free_space)
-	{
-		MERROR("Addr 0x" << hex << ptr << dec << " is free space");
-		p_Unlock();
-		return;
-	}
-
-	PRINT_CALL_INFO("caller: \"" << owner->file << ':' << owner->line << "\", user: \"" << mb->owner.file << ':' << mb->owner.line
-		<< "\", mb size: " << mb->size);
-
-	FreeBlock(mb);
-	SANITY_CHECKS
-
-	p_Unlock();
-}
-
-} // end namespace
-
-/**
-=======================================================================================================================================
-overloaded stuff                                                                                                       =
-=======================================================================================================================================
-*/
-
-// malloc
-void* malloc(size_t size) throw()
-{
-	++mem::malloc_called_num;
-	return mem::Malloc(size);
-}
-
-// realloc
-void* realloc(void* p, size_t size) throw()
-{
-	++mem::realloc_called_num;
-	return mem::Realloc(p, size);
-}
-
-// calloc
-void* calloc(size_t num, size_t size) throw()
-{
-	++mem::calloc_called_num;
-	return mem::Calloc(num, size);
-}
-
-// free
-void free(void* p) throw()
-{
-	++mem::free_called_num;
-	mem::Free(p);
-}
-
-// new
-void* operator new(size_t size) throw(std::bad_alloc)
-{
-	++mem::new_called_num;
-	return mem::Malloc(size);
-}
-
-// new[]
-void* operator new[](size_t size) throw(std::bad_alloc)
-{
-	++mem::new_called_num;
-	return mem::Malloc(size);
-}
-
-// delete
-void operator delete(void* p) throw()
-{
-	++mem::delete_called_num;
-	mem::Free(p);
-}
-
-// delete []
-void operator delete[](void* p) throw()
-{
-	++mem::delete_called_num;
-	mem::Free(p);
-}
-
-
-/**
-=======================================================================================================================================
-overloaded stuff with owner                                                                                            =
-=======================================================================================================================================
-*/
-
-// malloc
-void* malloc(size_t size, const char* file, int line, const char* func)
-{
-	++mem::malloc_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	return mem::Malloc(size, &owner);
-}
-
-// realloc
-void* realloc(void* p, size_t size, const char* file, int line, const char* func)
-{
-	++mem::realloc_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	return mem::Realloc(p, size, &owner);
-}
-
-// calloc
-void* calloc(size_t num, size_t size, const char* file, int line, const char* func)
-{
-	++mem::calloc_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	return mem::Calloc(num, size, &owner);
-}
-
-// free
-void free(void* p, const char* file, int line, const char* func)
-{
-	++mem::free_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	mem::Free(p, &owner);
-}
-
-// new
-void* operator new(size_t size, const char* file, int line, const char* func)
-{
-	++mem::new_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	return mem::Malloc(size, &owner);
-}
-
-// new[]
-void* operator new[](size_t size, const char* file, int line, const char* func)
-{
-	++mem::new_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	return mem::Malloc(size, &owner);
-}
-
-// delete
-void operator delete(void* p, const char* file, int line, const char* func)
-{
-	++mem::delete_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	mem::Free(p, &owner);
-}
-
-// delete []
-void operator delete[](void* p, const char* file, int line, const char* func)
-{
-	++mem::delete_called_num;
-	mem::mblock_owner_t owner = {file, line, func};
-	mem::Free(p, &owner);
-}
-
-#endif // _USE_MEM_MANAGER_

+ 0 - 58
src/Misc/memory.h

@@ -1,58 +0,0 @@
-#ifndef _MEMORY_H_
-#define _MEMORY_H_
-
-#include "Common.h"
-
-#define MEGABYTE 1048576
-#define KILOBYTE 1024
-
-namespace mem {
-
-// for printInfo
-const uint PRINT_ALL    = 0x0001;
-const uint PRINT_HEADER = 0x0002;
-const uint PRINT_BLOCKS = 0x0004;
-
-// for Enable
-const uint THREADS         = 0x0001;
-const uint PRINT_ERRORS    = 0x0002;
-const uint PRINT_CALL_INFO = 0x0004;
-
-
-extern uint malloc_called_num, calloc_called_num, realloc_called_num, free_called_num, new_called_num, delete_called_num;
-extern size_t free_size;
-extern const size_t buffer_size;
-extern uint errors_num;
-
-extern void printInfo(uint flags=PRINT_ALL);
-extern void Enable(uint flags);
-extern void Disable(uint flags);
-
-} // end namespace
-
-
-//#define _USE_MEM_MANAGER_ // comment this line if you dont want to use mem manager
-
-// MACROS
-#ifdef _USE_MEM_MANAGER_
-
-extern void* malloc(size_t size, const char* file, int line, const char* func);
-extern void* realloc(void* p, size_t size, const char* file, int line, const char* func);
-extern void* calloc(size_t num, size_t size, const char* file, int line, const char* func);
-extern void  free(void* p, const char* file, int line, const char* func);
-extern void* operator new(size_t size, const char* file, int line, const char* func);
-extern void* operator new[](size_t size, const char* file, int line, const char* func);
-extern void  operator delete(void* p, const char* file, int line, const char* func);
-extern void  operator delete[](void* p, const char* file, int line, const char* func);
-
-#define new new(__FILENAME__, __LINE__, __FUNCTION__)
-#define malloc(x) malloc(x, __FILENAME__, __LINE__, __FUNCTION__)
-#define realloc(x, y) realloc(x, y, __FILENAME__, __LINE__, __FUNCTION__)
-#define calloc(x, y) calloc(x, y, __FILENAME__, __LINE__, __FUNCTION__)
-#define free(x) free(x, __FILENAME__, __LINE__, __FUNCTION__)
-
-
-#endif
-
-
-#endif

+ 1 - 1
src/Physics/PhyCharacter.cpp

@@ -21,7 +21,7 @@ PhyCharacter::PhyCharacter(Physics& physics_, const Initializer& init, Object* p
 	motionState = new MotionState(init.startTrf, init.sceneNode, this);
 
 	btAxisSweep3* sweepBp = dynamic_cast<btAxisSweep3*>(physics.broadphase);
-	DEBUG_ERR(sweepBp == NULL);
+	RASSERT_THROW_EXCEPTION(sweepBp == NULL);
 
 	ghostPairCallback = new btGhostPairCallback();
 	sweepBp->getOverlappingPairCache()->setInternalGhostPairCallback(ghostPairCallback);

+ 1 - 1
src/Physics/RigidBody.cpp

@@ -12,7 +12,7 @@ RigidBody::RigidBody(Physics& physics_, const Initializer& init, Object* parent)
   Object(parent),
   physics(physics_)
 {
-	DEBUG_ERR(init.shape==NULL || init.shape->getShapeType()==INVALID_SHAPE_PROXYTYPE);
+	RASSERT_THROW_EXCEPTION(init.shape==NULL || init.shape->getShapeType()==INVALID_SHAPE_PROXYTYPE);
 
 	bool isDynamic = (init.mass != 0.0);
 

+ 1 - 0
src/Renderer/BufferObjects/Fbo.h

@@ -4,6 +4,7 @@
 #include <GL/glew.h>
 #include "Exception.h"
 #include "Properties.h"
+#include "StdTypes.h"
 
 
 /// The class is actually a wrapper to avoid common mistakes

+ 2 - 2
src/Renderer/Hdr.cpp

@@ -70,8 +70,8 @@ void Hdr::init(const RendererInitializer& initializer)
 
 	const char* SHADER_FILENAME = "shaders/GaussianBlurGeneric.glsl";
 
-	string pps = "#define HPASS\n#define COL_RGB\n";
-	string prefix = "HorizontalRgb";
+	std::string pps = "#define HPASS\n#define COL_RGB\n";
+	std::string prefix = "HorizontalRgb";
 	hblurSProg.loadRsrc(ShaderProg::createSrcCodeToCache(SHADER_FILENAME, pps.c_str(), prefix.c_str()).c_str());
 
 	pps = "#define VPASS\n#define COL_RGB\n";

+ 5 - 4
src/Renderer/Is.cpp

@@ -9,6 +9,7 @@
 #include "App.h"
 #include "Scene.h"
 #include "LightData.h"
+#include "Collision.h"
 
 
 //======================================================================================================================
@@ -122,9 +123,9 @@ void Is::init(const RendererInitializer& initializer)
 	                                                                 "SpotNoShadow").c_str());
 
 	// spot light w/t shadow
-	string pps = string("\n#define SPOT_LIGHT_ENABLED\n#define SHADOW_ENABLED\n") +
-	                    "#define SHADOWMAP_SIZE " + lexical_cast<string>(sm.getResolution()) + "\n";
-	string prefix = "SpotShadowSmSize" + lexical_cast<string>(sm.getResolution());
+	std::string pps = std::string("\n#define SPOT_LIGHT_ENABLED\n#define SHADOW_ENABLED\n") +
+	                              "#define SHADOWMAP_SIZE " + boost::lexical_cast<std::string>(sm.getResolution()) + "\n";
+	std::string prefix = "SpotShadowSmSize" + boost::lexical_cast<std::string>(sm.getResolution());
 	if(sm.isPcfEnabled())
 	{
 		pps += "#define PCF_ENABLED\n";
@@ -165,7 +166,7 @@ void Is::pointLightPass(const PointLight& light)
 	const Camera& cam = r.getCamera();
 
 	// frustum test
-	bsphere_t sphere(light.getWorldTransform().origin, light.getRadius());
+	Sphere sphere(light.getWorldTransform().origin, light.getRadius());
 	if(!cam.insideFrustum(sphere))
 	{
 		return;

+ 8 - 12
src/Renderer/MainRenderer.cpp

@@ -14,8 +14,6 @@
 //======================================================================================================================
 void MainRenderer::init(const RendererInitializer& initializer_)
 {
-	INFO("Main renderer initializing...");
-
 	initGl();
 
 	sProg.loadRsrc("shaders/final.glsl");
@@ -29,8 +27,6 @@ void MainRenderer::init(const RendererInitializer& initializer_)
 	initializer.height = app->getWindowHeight() * renderingQuality;
 	Renderer::init(initializer);
 	dbg.init(initializer);
-
-	INFO("Main renderer initialization ends");
 }
 
 
@@ -46,12 +42,12 @@ void MainRenderer::initGl()
 	}
 
 	// print GL info
-	INFO("OpenGL info: OGL " << glGetString(GL_VERSION) << ", GLSL " << glGetString(GL_SHADING_LANGUAGE_VERSION));
+	//INFO("OpenGL info: OGL " << glGetString(GL_VERSION) << ", GLSL " << glGetString(GL_SHADING_LANGUAGE_VERSION));
 
-	if(!glewIsSupported("GL_VERSION_3_1"))
+	/*if(!glewIsSupported("GL_VERSION_3_1"))
 	{
 		WARNING("OpenGL ver 3.1 not supported. The application may crash (and burn)");
-	}
+	}*/
 
 	// get max texture units
 	glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &Texture::textureUnitsNum);
@@ -105,8 +101,8 @@ void MainRenderer::render(Camera& cam_)
 void MainRenderer::takeScreenshotTga(const char* filename)
 {
 	// open file and check
-	fstream fs;
-	fs.open(filename, ios::out|ios::binary);
+	std::fstream fs;
+	fs.open(filename, std::ios::out | std::ios::binary);
 	if(!fs.good())
 	{
 		throw EXCEPTION("Cannot create screenshot. File \"" + filename + "\"");
@@ -193,8 +189,8 @@ void MainRenderer::takeScreenshotJpeg(const char* filename)
 //======================================================================================================================
 void MainRenderer::takeScreenshot(const char* filename)
 {
-	std::string ext = filesystem::path(filename).extension();
-	to_lower(ext);
+	std::string ext = boost::filesystem::path(filename).extension();
+	boost::to_lower(ext);
 
 	// exec from this extension
 	if(ext == ".tga")
@@ -209,6 +205,6 @@ void MainRenderer::takeScreenshot(const char* filename)
 	{
 		throw EXCEPTION("File \"" + filename + "\": Unsupported extension");
 	}
-	INFO("Screenshot \"" << filename << "\" saved");
+	//INFO("Screenshot \"" << filename << "\" saved");
 }
 

+ 36 - 35
src/Renderer/Ms.cpp

@@ -11,47 +11,45 @@
 //======================================================================================================================
 void Ms::init(const RendererInitializer& initializer)
 {
-	// create FBO
-	fbo.create();
-	fbo.bind();
-
-	// inform in what buffers we draw
-	fbo.setNumOfColorAttachements(3);
-
-	// create the FAIs
 	try
 	{
-		normalFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RG16F, GL_RG, GL_FLOAT);
-	  diffuseFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGB16F, GL_RGB, GL_FLOAT);
-	  specularFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGBA16F, GL_RGBA, GL_FLOAT);
-	  depthFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
-	}
-	catch(Exception& e)
-	{
-		FATAL("Failed to create one MS FAI. See prev error");
-	}
+		// create FBO
+		fbo.create();
+		fbo.bind();
+
+		// inform in what buffers we draw
+		fbo.setNumOfColorAttachements(3);
 
-	normalFai.setRepeat(false);
-	diffuseFai.setRepeat(false);
-	specularFai.setRepeat(false);
-	depthFai.setRepeat(false);
+		// create the FAIs
+		normalFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RG16F, GL_RG, GL_FLOAT);
+		diffuseFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGB16F, GL_RGB, GL_FLOAT);
+		specularFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGBA16F, GL_RGBA, GL_FLOAT);
+		depthFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
 
+		normalFai.setRepeat(false);
+		diffuseFai.setRepeat(false);
+		specularFai.setRepeat(false);
+		depthFai.setRepeat(false);
 
-	// attach the buffers to the FBO
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, normalFai.getGlId(), 0);
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, diffuseFai.getGlId(), 0);
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, specularFai.getGlId(), 0);
+		// attach the buffers to the FBO
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, normalFai.getGlId(), 0);
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, diffuseFai.getGlId(), 0);
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, specularFai.getGlId(), 0);
 
-	//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
-	//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
+		//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
+		//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
 
-	// test if success
-	if(!fbo.isGood())
-		FATAL("Cannot create deferred shading material stage FBO");
+		// test if success
+		fbo.checkIfGood();
 
-	// unbind
-	fbo.unbind();
+		// unbind
+		fbo.unbind();
+	}
+	catch(std::exception& e)
+	{
+		throw EXCEPTION("Cannot create deferred shading material stage FBO: " + e.what());
+	}
 
 	ez.init(initializer);
 }
@@ -96,10 +94,13 @@ void Ms::run()
 		MeshNode* meshNode = (*it);
 		if(meshNode->mesh->material.get() == NULL)
 		{
-			ERROR("Mesh \"" << meshNode->mesh->getRsrcName() << "\" doesnt have material" );
+			throw EXCEPTION("Mesh \"" + meshNode->mesh->getRsrcName() + "\" doesnt have material");
+		}
+
+		if(meshNode->mesh->material->blends)
+		{
 			continue;
 		}
-		if(meshNode->mesh->material->blends) continue;
 
 		r.setupMaterial(*meshNode->mesh->material, *meshNode, cam);
 		meshNode->render();

+ 27 - 9
src/Renderer/Pps.cpp

@@ -5,7 +5,7 @@
 //======================================================================================================================
 // initPassFbo                                                                                                         =
 //======================================================================================================================
-void Pps::initPassFbo(Fbo& fbo, Texture& fai, const char* msg)
+void Pps::initPassFbo(Fbo& fbo, Texture& fai)
 {
 	fbo.create();
 	fbo.bind();
@@ -17,8 +17,7 @@ void Pps::initPassFbo(Fbo& fbo, Texture& fai, const char* msg)
 
 	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fai.getGlId(), 0);
 
-	if(!fbo.isGood())
-		FATAL(msg);
+	fbo.checkIfGood();
 
 	fbo.unbind();
 }
@@ -29,8 +28,8 @@ void Pps::initPassFbo(Fbo& fbo, Texture& fai, const char* msg)
 //======================================================================================================================
 void Pps::initPrePassSProg()
 {
-	string pps = "";
-	string prefix = "";
+	std::string pps = "";
+	std::string prefix = "";
 
 	if(ssao.isEnabled())
 	{
@@ -49,8 +48,8 @@ void Pps::initPrePassSProg()
 //======================================================================================================================
 void Pps::initPostPassSProg()
 {
-	string pps = "";
-	string prefix = "";
+	std::string pps = "";
+	std::string prefix = "";
 
 	if(hdr.isEnabled())
 	{
@@ -72,8 +71,23 @@ void Pps::init(const RendererInitializer& initializer)
 	ssao.init(initializer);
 	hdr.init(initializer);
 
-	initPassFbo(prePassFbo, prePassFai, "Cannot create pre-pass post-processing stage FBO");
-	initPassFbo(postPassFbo, postPassFai, "Cannot create post-pass post-processing stage FBO");
+	try
+	{
+		initPassFbo(prePassFbo, prePassFai);
+	}
+	catch(std::exception& e)
+	{
+		throw EXCEPTION("Cannot create pre-pass post-processing stage FBO: " + e.what());
+	}
+
+	try
+	{
+		initPassFbo(postPassFbo, postPassFai);
+	}
+	catch(std::exception& e)
+	{
+		throw EXCEPTION("Cannot create post-pass post-processing stage FBO: " + e.what());
+	}
 
 	initPrePassSProg();
 	initPostPassSProg();
@@ -86,7 +100,9 @@ void Pps::init(const RendererInitializer& initializer)
 void Pps::runPrePass()
 {
 	if(ssao.isEnabled())
+	{
 		ssao.run();
+	}
 
 	prePassFbo.bind();
 
@@ -114,7 +130,9 @@ void Pps::runPrePass()
 void Pps::runPostPass()
 {
 	if(hdr.isEnabled())
+	{
 		hdr.run();
+	}
 
 	postPassFbo.bind();
 

+ 6 - 12
src/Renderer/Pps.h

@@ -8,11 +8,9 @@
 #include "Ssao.h"
 
 
-/**
- * Post-processing stage
- *
- * This stage is divided into 2 two parts. The first happens before blending stage and the second after.
- */
+/// Post-processing stage.
+///
+/// This stage is divided into 2 two parts. The first happens before blending stage and the second after.
 class Pps: private RenderingStage
 {
 	public:
@@ -32,16 +30,12 @@ class Pps: private RenderingStage
 		RsrcPtr<ShaderProg> prePassSProg;
 		RsrcPtr<ShaderProg> postPassSProg;
 
-		void initPassFbo(Fbo& fbo, Texture& fai, const char* msg);
+		void initPassFbo(Fbo& fbo, Texture& fai);
 
-		/**
-		 * before BS pass
-		 */
+		/// Before BS pass
 		void initPrePassSProg();
 
-		/**
-		 * after BS pass
-		 */
+		/// After BS pass
 		void initPostPassSProg();
 };
 

+ 44 - 33
src/Renderer/Sm.cpp

@@ -22,41 +22,47 @@ void Sm::init(const RendererInitializer& initializer)
 	bilinearEnabled = initializer.is.sm.bilinearEnabled;
 	resolution = initializer.is.sm.resolution;
 
-	// create FBO
-	fbo.create();
-	fbo.bind();
-
-	// texture
-	shadowMap.createEmpty2D(resolution, resolution, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT);
-	if(bilinearEnabled)
+	try
 	{
-		shadowMap.setFiltering(Texture::TFT_LINEAR);
+		// create FBO
+		fbo.create();
+		fbo.bind();
+
+		// texture
+		shadowMap.createEmpty2D(resolution, resolution, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT);
+		if(bilinearEnabled)
+		{
+			shadowMap.setFiltering(Texture::TFT_LINEAR);
+		}
+		else
+		{
+			shadowMap.setFiltering(Texture::TFT_NEAREST);
+		}
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
+		/*
+		 * If you dont want to use the FFP for comparing the shadowmap (the above two lines) then you can make the comparison
+		 * inside the glsl shader. The GL_LEQUAL means that: shadow = (R <= Dt) ? 1.0 : 0.0; . The R is given by:
+		 * R = _tex_coord2.z/_tex_coord2.w; and the Dt = shadow2D(shadow_depth_map, _shadow_uv).r (see lp_generic.frag).
+		 * Hardware filters like GL_LINEAR cannot be applied.
+		 */
+
+		// inform the we wont write to color buffers
+		fbo.setNumOfColorAttachements(0);
+
+		// attach the texture
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowMap.getGlId(), 0);
+
+		// test if success
+		fbo.checkIfGood();
+
+		// unbind
+		fbo.unbind();
 	}
-	else
+	catch(std::exception& e)
 	{
-		shadowMap.setFiltering(Texture::TFT_NEAREST);
+		throw EXCEPTION("Cannot create shadowmapping FBO: " + e.what());
 	}
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
-	/*
-	 * If you dont want to use the FFP for comparing the shadowmap (the above two lines) then you can make the comparison
-	 * inside the glsl shader. The GL_LEQUAL means that: shadow = (R <= Dt) ? 1.0 : 0.0; . The R is given by:
-	 * R = _tex_coord2.z/_tex_coord2.w; and the Dt = shadow2D(shadow_depth_map, _shadow_uv).r (see lp_generic.frag).
-	 * Hardware filters like GL_LINEAR cannot be applied.
-	 */
-
-	// inform the we wont write to color buffers
-	fbo.setNumOfColorAttachements(0);
-
-	// attach the texture
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowMap.getGlId(), 0);
-
-	// test if success
-	if(!fbo.isGood())
-		FATAL("Cannot create shadowmapping FBO");
-
-	// unbind
-	fbo.unbind();
 }
 
 
@@ -65,7 +71,10 @@ void Sm::init(const RendererInitializer& initializer)
 //======================================================================================================================
 void Sm::run(const Camera& cam)
 {
-	DEBUG_ERR(!enabled);
+	if(!enabled)
+	{
+		return;
+	}
 
 	// FBO
 	fbo.bind();
@@ -88,9 +97,11 @@ void Sm::run(const Camera& cam)
 	{
 		MeshNode* meshNode = (*it);
 		if(meshNode->mesh->material->blends)
+		{
 			continue;
+		}
 
-		DEBUG_ERR(meshNode->mesh->material->dpMtl.get() == NULL);
+		RASSERT_THROW_EXCEPTION(meshNode->mesh->material->dpMtl.get() == NULL);
 
 		r.setupMaterial(*meshNode->mesh->material->dpMtl, *meshNode, cam);
 		meshNode->renderDepth();

+ 27 - 23
src/Renderer/Ssao.cpp

@@ -10,31 +10,37 @@
 //======================================================================================================================
 void Ssao::createFbo(Fbo& fbo, Texture& fai)
 {
-	int width = renderingQuality * r.getWidth();
-	int height = renderingQuality * r.getHeight();
+	try
+	{
+		int width = renderingQuality * r.getWidth();
+		int height = renderingQuality * r.getHeight();
 
-	// create
-	fbo.create();
-	fbo.bind();
+		// create
+		fbo.create();
+		fbo.bind();
 
-	// inform in what buffers we draw
-	fbo.setNumOfColorAttachements(1);
+		// inform in what buffers we draw
+		fbo.setNumOfColorAttachements(1);
 
-	// create the texes
-	fai.createEmpty2D(width, height, GL_RED, GL_RED, GL_FLOAT);
-	fai.setRepeat(false);
-	fai.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-	fai.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+		// create the texes
+		fai.createEmpty2D(width, height, GL_RED, GL_RED, GL_FLOAT);
+		fai.setRepeat(false);
+		fai.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+		fai.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
-	// attach
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fai.getGlId(), 0);
+		// attach
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fai.getGlId(), 0);
 
-	// test if success
-	if(!fbo.isGood())
-		FATAL("Cannot create deferred shading post-processing stage SSAO blur FBO");
+		// test if success
+		fbo.checkIfGood();
 
-	// unbind
-	fbo.unbind();
+		// unbind
+		fbo.unbind();
+	}
+	catch(std::exception& e)
+	{
+		throw EXCEPTION("Cannot create deferred shading post-processing stage SSAO blur FBO");
+	}
 }
 
 
@@ -66,8 +72,8 @@ void Ssao::init(const RendererInitializer& initializer)
 	// blurring progs
 	const char* SHADER_FILENAME = "shaders/GaussianBlurGeneric.glsl";
 
-	string pps = "#define HPASS\n#define COL_R\n";
-	string prefix = "HorizontalR";
+	std::string pps = "#define HPASS\n#define COL_R\n";
+	std::string prefix = "HorizontalR";
 	hblurSProg.loadRsrc(ShaderProg::createSrcCodeToCache(SHADER_FILENAME, pps.c_str(), prefix.c_str()).c_str());
 
 	pps = "#define VPASS\n#define COL_R\n";
@@ -80,7 +86,6 @@ void Ssao::init(const RendererInitializer& initializer)
 
 	/// @todo fix this crap
 	// load noise map and disable temporally the texture compression and enable mipmapping
-	GL_OK();
 	/*bool texCompr = Texture::compressionEnabled;
 	bool mipmaping = Texture::mipmappingEnabled;
 	Texture::compressionEnabled = false;
@@ -92,7 +97,6 @@ void Ssao::init(const RendererInitializer& initializer)
 	//noise_map->setTexParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 	Texture::compressionEnabled = texCompr;
 	Texture::mipmappingEnabled = mipmaping;*/
-	GL_OK();
 }
 
 

+ 1 - 1
src/Scene/Controllers/Controller.cpp

@@ -6,7 +6,7 @@
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-Controller::Controller(Type type_): 
+Controller::Controller(ControllerType type_):
 	type(type_) 
 {
 	app->getScene().registerController(this);

+ 6 - 6
src/Scene/Controllers/Controller.h

@@ -1,14 +1,14 @@
-#ifndef _CONTROLLER_H_
-#define _CONTROLLER_H_
+#ifndef CONTROLLER_H
+#define CONTROLLER_H
 
-#include "Common.h"
+#include "Properties.h"
 
 
 /// Scenegraph node controller (A)
 class Controller
 {
 	public:
-		enum Type
+		enum ControllerType
 		{ 
 			CT_SKEL_ANIM, 
 			CT_SKEL,
@@ -18,10 +18,10 @@ class Controller
 			CT_LIGHT
 		};
 	
-	PROPERTY_R(Type, type, getType) ///< Once the type is set nothing can change it
+	PROPERTY_R(ControllerType, type, getType) ///< Once the type is set nothing can change it
 
 	public:
-		Controller(Type type_);
+		Controller(ControllerType type_);
 		virtual ~Controller();
 		virtual void update(float time) = 0;
 };

+ 3 - 4
src/Scene/Controllers/LightPropsScriptCtrl.h

@@ -1,7 +1,6 @@
-#ifndef _LIGHT_SCRIPT_CTRL_H_
-#define _LIGHT_SCRIPT_CTRL_H_
+#ifndef LIGHT_SCRIPT_CTRL_H
+#define LIGHT_SCRIPT_CTRL_H
 
-#include "Common.h"
 #include "Controller.h"
 
 
@@ -15,6 +14,6 @@ class LightPropsScriptCtrl: public Controller
 		
 		LightPropsScriptCtrl(Light* light_): controller(CT_LIGHT), light(light_) {}
 		void update(float) { /* ToDo */ }
-}
+};
 
 #endif

+ 6 - 11
src/Scene/Controllers/MeshSkelNodeCtrl.h

@@ -1,7 +1,6 @@
-#ifndef _MESH_SKEL_CTRL_H_
-#define _MESH_SKEL_CTRL_H_
+#ifndef MESH_SKEL_CTRL_H
+#define MESH_SKEL_CTRL_H
 
-#include "Common.h"
 #include "Controller.h"
 #include "Vbo.h"
 
@@ -11,10 +10,8 @@ class SkelNode;
 class Mesh;
 
 
-/**
- * Skeleton controller
- * It controls a mesh node using a skeleton node and the skeleton node's controllers
- */
+/// Skeleton controller
+/// It controls a mesh node using a skeleton node and the skeleton node's controllers
 class MeshSkelNodeCtrl: public Controller
 {
 	public:
@@ -35,10 +32,8 @@ class MeshSkelNodeCtrl: public Controller
 			meshNode(meshNode_)
 		{}
 
-		/**
-		 * Do nothing! We use HW skinning so its not necessary to update anything in the meshNode. 
-		 * The skelNode's controllers provide us with sufficient data to do the trick.
-		 */
+		/// Do nothing! We use HW skinning so its not necessary to update anything in the meshNode.
+		/// The skelNode's controllers provide us with sufficient data to do the trick.
 		void update(float) {}
 };
 

+ 2 - 2
src/Scene/Controllers/SkelAnimCtrl.cpp

@@ -20,7 +20,7 @@ SkelAnimCtrl::SkelAnimCtrl(SkelNode& skelNode_):
 //======================================================================================================================
 void SkelAnimCtrl::interpolate(const SkelAnim& animation, float frame)
 {
-	DEBUG_ERR(frame >= animation.framesNum);
+	RASSERT_THROW_EXCEPTION(frame >= animation.framesNum);
 
 	// calculate the t (used in slerp and lerp) using the keyframs and the frame and
 	// calc the lPose and rPose witch indicate the pose ids in witch the frame lies between
@@ -49,7 +49,7 @@ void SkelAnimCtrl::interpolate(const SkelAnim& animation, float frame)
 	Vec<Mat3>& boneRotations = skelNode.boneRotations;
 	Vec<Vec3>& boneTranslations = skelNode.boneTranslations;
 
-	DEBUG_ERR(boneRotations.size()<1);
+	RASSERT_THROW_EXCEPTION(boneRotations.size()<1);
 	for(uint i=0; i<boneRotations.size(); i++)
 	{
 		const SkelAnim::BoneAnim& banim = animation.bones[i];

+ 3 - 6
src/Scene/Controllers/SkelAnimCtrl.h

@@ -1,7 +1,6 @@
 #ifndef SKEL_ANIM_CTRL_H
 #define SKEL_ANIM_CTRL_H
 
-#include "Common.h"
 #include "Vec.h"
 #include "Controller.h"
 #include "Math.h"
@@ -26,14 +25,12 @@ class SkelAnimCtrl: public Controller
 	private:
 		SkelNode& skelNode;
 
-		/**
-		 * @name The 3 steps of skeletal animation in 3 funcs
-		 */
-		/**@{*/
+		/// @name The 3 steps of skeletal animation in 3 funcs
+		/// @{
 		void interpolate(const SkelAnim& animation, float frame);
 		void updateBoneTransforms();
 		void deform();  ///< Now with HW skinning it deforms only the debug skeleton
-		/**@}*/
+		/// @}
 };
 
 

+ 2 - 3
src/Scene/Controllers/TrfScriptCtrl.h

@@ -1,7 +1,6 @@
-#ifndef _TRF_SCRIPT_CTRL_H_
-#define _TRF_SCRIPT_CTRL_H_
+#ifndef TRF_SCRIPT_CTRL_H
+#define TRF_SCRIPT_CTRL_H
 
-#include "Common.h"
 #include "Controller.h"
 
 

+ 0 - 1
src/Scene/Light.cpp

@@ -1,5 +1,4 @@
 #include "Light.h"
-#include "collision.h"
 #include "LightData.h"
 #include "App.h"
 #include "MainRenderer.h"

+ 0 - 1
src/Scene/Light.h

@@ -17,7 +17,6 @@ Specular intensity of material: Sm
 #ifndef LIGHT_H
 #define LIGHT_H
 
-#include "Common.h"
 #include "Texture.h"
 #include "SceneNode.h"
 #include "Camera.h"

+ 4 - 1
src/Scene/MeshNode.cpp

@@ -10,6 +10,9 @@
 #include "MainRenderer.h"
 
 
+#define BUFFER_OFFSET(i) ((char *)NULL + (i))
+
+
 //======================================================================================================================
 // init                                                                                                                =
 //======================================================================================================================
@@ -31,7 +34,7 @@ void MeshNode::render(Material* mtl) const
 	// if we have skeleton controller
 	if(meshSkelCtrl)
 	{
-		DEBUG_ERR(!mtl->hasHWSkinning()); // it has skel controller but no skinning
+		RASSERT_THROW_EXCEPTION(!mtl->hasHWSkinning()); // it has skel controller but no skinning
 
 		// first the uniforms
 		mtl->stdUniVars[Material::SUV_SKINNING_ROTATIONS]->setMat3(&meshSkelCtrl->skelNode->boneRotations[0],

+ 2 - 3
src/Scene/MeshNode.h

@@ -1,7 +1,6 @@
-#ifndef _MESH_NODE_H_
-#define _MESH_NODE_H_
+#ifndef MESH_NODE_H
+#define MESH_NODE_H
 
-#include "Common.h"
 #include "SceneNode.h"
 #include "Material.h"
 #include "RsrcPtr.h"

+ 1 - 2
src/Scene/PointLight.h

@@ -1,7 +1,6 @@
 #ifndef POINT_LIGHT_H
 #define POINT_LIGHT_H
 
-#include "Common.h"
 #include "Light.h"
 
 
@@ -21,7 +20,7 @@ inline void PointLight::init(const char* filename)
 	Light::init(filename);
 	if(lightData->getType() != LightData::LT_POINT)
 	{
-		ERROR("Light data is wrong type");
+		throw EXCEPTION("Light data is wrong type");
 		return;
 	}
 	radius = lightData->getRadius();

+ 2 - 6
src/Scene/SceneNode.cpp

@@ -1,7 +1,6 @@
 #include <algorithm>
 #include "SceneNode.h"
 #include "Renderer.h"
-#include "collision.h"
 #include "Controller.h"
 #include "Scene.h"
 #include "App.h"
@@ -14,7 +13,6 @@ void SceneNode::commonConstructorCode()
 {
 	parent = NULL;
 	isCompound = false;
-	bvolumeLspace = NULL;
 	getWorldTransform().setIdentity();
 	getLocalTransform().setIdentity();
 
@@ -111,8 +109,7 @@ void SceneNode::addChild(SceneNode* node)
 {
 	if(node->parent != NULL)
 	{
-		ERROR("Node already has parent");
-		return;
+		throw EXCEPTION("Node already has parent");
 	}
 
 	node->parent = this;
@@ -128,8 +125,7 @@ void SceneNode::removeChild(SceneNode* child)
 	Vec<SceneNode*>::iterator it = std::find(childs.begin(), childs.end(), child);
 	if(it == childs.end())
 	{
-		ERROR("Child not found");
-		return;
+		throw EXCEPTION("Child not found");
 	}
 
 	child->parent = NULL;

+ 6 - 27
src/Scene/SceneNode.h

@@ -2,13 +2,12 @@
 #define SCENE_NODE_H
 
 #include <memory>
-#include "Common.h"
 #include "Vec.h"
 #include "Math.h"
 #include "Object.h"
+#include "Properties.h"
 
 
-class bvolume_t;
 class Material;
 class Controller;
 
@@ -30,15 +29,13 @@ class SceneNode: public Object
 			SNT_PARTICLE_EMITTER
 		};
 
-	PROPERTY_RW(Transform, localTransform, setLocalTransform, getLocalTransform); ///< The transformation in local space
-	PROPERTY_RW(Transform, worldTransform, setWorldTransform, getWorldTransform); ///< The transformation in world space (local combined with parent transformation)
+	PROPERTY_RW(Transform, localTransform, setLocalTransform, getLocalTransform) ///< The transformation in local space
+	PROPERTY_RW(Transform, worldTransform, setWorldTransform, getWorldTransform) ///< The transformation in world space (local combined with parent transformation)
 
 	public:
 		SceneNode* parent;
 		Vec<SceneNode*> childs;
 		SceneNodeType type;
-		bvolume_t* bvolumeLspace;
-		bvolume_t* bvolumeWspace;
 		bool isCompound;
 		
 		SceneNode(SceneNodeType type_, SceneNode* parent = NULL);
@@ -56,9 +53,9 @@ class SceneNode: public Object
 
 		/// @name Mess with the local transform
 		/// @{
-		void rotateLocalX(float angDegrees);
-		void rotateLocalY(float angDegrees);
-		void rotateLocalZ(float angDegrees);
+		void rotateLocalX(float angDegrees) {localTransform.rotation.rotateXAxis(angDegrees);}
+		void rotateLocalY(float angDegrees) {localTransform.rotation.rotateYAxis(angDegrees);}
+		void rotateLocalZ(float angDegrees) {localTransform.rotation.rotateZAxis(angDegrees);}
 		void moveLocalX(float distance);
 		void moveLocalY(float distance);
 		void moveLocalZ(float distance);
@@ -85,22 +82,4 @@ inline SceneNode::SceneNode(SceneNodeType type_, SceneNode* parent):
 }
 
 
-inline void SceneNode::rotateLocalX(float angDegrees)
-{
-	localTransform.rotation.rotateXAxis(angDegrees);
-}
-
-
-inline void SceneNode::rotateLocalY(float angDegrees)
-{
-	localTransform.rotation.rotateYAxis(angDegrees);
-}
-
-
-inline void SceneNode::rotateLocalZ(float angDegrees)
-{
-	localTransform.rotation.rotateZAxis(angDegrees);
-}
-
-
 #endif

+ 1 - 2
src/Scene/SpotLight.cpp

@@ -9,8 +9,7 @@ void SpotLight::init(const char* filename)
 	Light::init(filename);
 	if(lightData->getType() != LightData::LT_SPOT)
 	{
-		ERROR("Light data is wrong type");
-		return;
+		throw EXCEPTION("Light data is wrong type");
 	}
 	camera = new Camera(this);
 	camera->setAll(lightData->getFovX(), lightData->getFovY(), 0.02, lightData->getDistance());

+ 0 - 1
src/Scene/SpotLight.h

@@ -1,7 +1,6 @@
 #ifndef SPOT_LIGHT_H
 #define SPOT_LIGHT_H
 
-#include "Common.h"
 #include "Light.h"
 
 

+ 1 - 1
src/Scripting/BoostPythonInterfaces.cpp

@@ -1,4 +1,3 @@
-#include "Common.h"
 #include <boost/python.hpp>
 #include "Math.h"
 #include "Scene.h"
@@ -6,6 +5,7 @@
 #include "App.h"
 
 
+using namespace boost;
 using namespace boost::python;
 using namespace M;