Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagecpp
titleAnalytical Sphere
firstline1
linenumberstrue
#include <math.h>
#include "extensionmanager.h"
#include "geometryextension.h"
#include "maxwell.h"
#ifndef DEG2RAD
#define DEG2RAD(d) ((d) * 3.14159265358979323846 / 180.0)
#endif

class SphereRenderExtension : public CgeometryProceduralExtension
{
	DECLARE_EXTENSION_METHODS( "SphereRenderExample", SphereRenderExtension, 1 )
	double radius;
public:
	SphereRenderExtension()
	{
		getExtensionData()->createDouble( "Radius", 1.00, 0, 1000000 );
	}
	~SphereRenderExtension()
	{
	}
	bool initialize()
	{
		getExtensionData()->getDouble( "Radius", radius );
		return true;
	}
	bool intersect( Cmaxwell::Cobject* object, const Cpoint& origin, const Cvector& dir, 
			real time, const dword subVolumeIndex, Cvector* pNormal, Cvector* pLocalImpact, 
			CfVector &data )
	{
		real dist, a,b, disc, t0, t1, sqr;
		dist = -1.0;
		//Assume sphere centered in 0,0,0
		a = 2*( dir.x * origin.x + dir.y * origin.y + dir.z * origin.z );
		b = origin.x*origin.x + origin.y*origin.y + origin.z*origin.z - radius*radius;
		disc = a*a - 4*b;
		if ( disc < 0.0 )
		{
			return false;
		}
		sqr = sqrt ( disc );
		t0 = ( -a - sqr ) * 0.5;
		t1 = ( -a + sqr ) * 0.5;
		if ( t0 <= 0.0 )
		{
			if ( t1 > 0.0 )
			{
				dist = t1;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if ( t0 < t1 ) dist = t0;
			else dist = t1;
		}
		Cvector p;
		p.assign( origin + dir * dist );//Point of intersection in local coordinates
		pLocalImpact->assign( p );
		Cvector normal;
		normal.assign( p );
		pNormal->assign( normal );//No need to normalize. Done later on demand.
		return true;
	}
	void getBoundingBox( Cvector& bmin, Cvector& bmax, float time )
	{
		bmin.assign( -radius, -radius, -radius );
		bmax.assign( radius, radius, radius );
	}
};
EXPORT_GEOMETRY_PROCEDURAL_EXTENSION( SphereRenderExtension )

Now that we know who you are, I know who I am. I'm not a mistake! It all makes sense! In a comic, you know how you can tell who the arch-villain's going to be? He's the exact opposite of the hero. And most times they're friends, like you and me! I should've known way back when... You know why, David? Because of the kids. They called me Mr Glass.