Wouldnt the use of dynamic_cast remove some of the point of designing my primitive class as I have done?
I am steeling the idea of how to do this primitive/ scene class from a tutorial on how to write a raytracer, and in that tutorial they do it as I do, just without the erros.'
Code:// ----------------------------------------------------------- // Primitive class definition // ----------------------------------------------------------- class Primitive { public: enum { SPHERE = 1, PLANE }; Primitive() : m_Name( 0 ), m_Light( false ) {}; Material* GetMaterial() { return &m_Material; } void SetMaterial( Material& a_Mat ) { m_Material = a_Mat; } virtual int GetType() = 0; virtual int Intersect( Ray& a_Ray, float& a_Dist ) = 0; virtual vector3 GetNormal( vector3& a_Pos ) = 0; virtual Color GetColor( vector3& ) { return m_Material.GetColor(); } virtual void Light( bool a_Light ) { m_Light = a_Light; } bool IsLight() { return m_Light; } void SetName( char* a_Name ); char* GetName() { return m_Name; } protected: Material m_Material; char* m_Name; bool m_Light; }; // ----------------------------------------------------------- // Sphere primitive class definition // ----------------------------------------------------------- class Sphere : public Primitive { public: int GetType() { return SPHERE; } Sphere( vector3& a_Centre, float a_Radius ) : m_Centre( a_Centre ), m_SqRadius( a_Radius * a_Radius ), m_Radius( a_Radius ), m_RRadius( 1.0f / a_Radius ) {}; vector3& GetCentre() { return m_Centre; } float GetSqRadius() { return m_SqRadius; } int Intersect( Ray& a_Ray, float& a_Dist ); vector3 GetNormal( vector3& a_Pos ) { return (a_Pos - m_Centre) * m_RRadius; } private: vector3 m_Centre; float m_SqRadius, m_Radius, m_RRadius; };As you see, here all object can be treathed the same, which makes all much more easy.Code:m_Primitive = new Primitive*[100]; // ground plane m_Primitive[0] = new PlanePrim( vector3( 0, 1, 0 ), 4.4f ); m_Primitive[0]->SetName( "plane" ); m_Primitive[0]->GetMaterial()->SetReflection( 0 ); m_Primitive[0]->GetMaterial()->SetDiffuse( 1.0f ); m_Primitive[0]->GetMaterial()->SetColor( Color( 0.4f, 0.3f, 0.3f ) ); // big sphere m_Primitive[1] = new Sphere( vector3( 1, -0.8f, 3 ), 2.5f ); m_Primitive[1]->SetName( "big sphere" ); m_Primitive[1]->GetMaterial()->SetReflection( 0.6f ); m_Primitive[1]->GetMaterial()->SetColor( Color( 0.7f, 0.7f, 0.7f ) );
Also, with this code I can call primitve[0].intersection(), so that I dont have to think about what kind of object I am testing. So, what am I doint wrong as my code dont work, but this does?
Thanks



LinkBack URL
About LinkBacks



CornedBee