1 //------------------------------------------------------------------------------
2 // File : SkyRenderableInstance.hpp
3 //------------------------------------------------------------------------------
4 // SkyWorks : Copyright 2002 Mark J. Harris and
5 // The University of North Carolina at Chapel Hill
6 //------------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software and its
8 // documentation for any purpose is hereby granted without fee, provided that
9 // the above copyright notice appear in all copies and that both that copyright
10 // notice and this permission notice appear in supporting documentation.
11 // Binaries may be compiled with this software without any royalties or
14 // The author(s) and The University of North Carolina at Chapel Hill make no
15 // representations about the suitability of this software for any purpose.
16 // It is provided "as is" without express or
19 * @file SkyRenderableInstance.hpp
21 * Interface definition for SkyRenderableInstance, an instance of a renderable object.
23 #ifndef __SKYRENDERABLEINSTANCE_HPP__
24 #define __SKYRENDERABLEINSTANCE_HPP__
29 #include "SkyUtil.hpp"
31 // forward to reduce unnecessary dependencies
35 // forward so we can make the following typedefs easily visible in the header.
36 // rather than buried under the class definition.
37 class SkyRenderableInstance;
39 //! A dynamic array of SkyRenderableInstance pointers.
40 typedef std::vector<SkyRenderableInstance*> InstanceArray;
41 //! An instance array iterator.
42 typedef InstanceArray::iterator InstanceIterator;
44 //------------------------------------------------------------------------------
46 * @class SkyRenderableInstance
47 * @brief An instance of a SkyRenderable object.
49 * An instance contains a pointer to a SkyRenderable object. The
50 * instance contains attributes such as position, orientation,
51 * scale, etc. that vary between instances.
53 class SkyRenderableInstance
57 SkyRenderableInstance()
58 : _bCulled(false), _bAlive(true), _vecPosition(0, 0, 0), _rScale(1), _rSquareSortDistance(0)
60 _matRotation.Identity(); _matInvRotation.Identity();
64 SkyRenderableInstance(const Vec3f &position,
65 const Mat33f &rotation,
67 : _bCulled(false), _bAlive(true), _vecPosition(position),
68 _matRotation(rotation), _rScale(scale), _rSquareSortDistance(0)
70 _matInvRotation = _matRotation;
71 _matInvRotation.Transpose();
75 virtual ~SkyRenderableInstance() {}
78 //! Set the world space position of the instance.
79 virtual void SetPosition(const Vec3f &position) { _vecPosition = position; }
80 //! Set the world space rotation of the instance.
81 virtual void SetRotation(const Mat33f &rotation) { _matRotation = rotation;
82 _matInvRotation = rotation;
83 _matInvRotation.Transpose(); }
84 //! Set the world space scale of the instance.
85 virtual void SetScale( const float &scale) { _rScale = scale; }
87 //! Returns the world space position of the instance.
88 virtual const Vec3f& GetPosition() const { return _vecPosition; }
89 //! Returns the world space rotation matrix of the instance.
90 virtual const Mat33f& GetRotation() const { return _matRotation; }
91 //! Returns the inverse of the world space rotation matrix of the instance.
92 virtual const Mat33f& GetInverseRotation() const { return _matInvRotation; }
93 //! Returns the world space scale of the instance.
94 virtual float GetScale() const { return _rScale; }
96 //! Update the instance based on the given camera, @a cam.
97 virtual SKYRESULT Update(const Camera &cam) { return SKYRESULT_OK; }
98 //! Render the instance.
99 virtual SKYRESULT Display() { return SKYRESULT_OK; }
101 //! Returns the transform matrix from model space to world space.
102 inline virtual void GetModelToWorldTransform(Mat44f &mat) const;
104 //! Returns the transform matrix from world space to model space.
106 inline virtual void GetWorldToModelTransform(Mat44f &mat) const;
108 //! Returns the object-space bounding volume for this instance, or NULL if none is available.
109 virtual SkyMinMaxBox* GetBoundingVolume() const { return NULL; }
111 //! Returns true if and only if the bounding volume of this instance lies entirely outside @a cam.
112 virtual bool ViewFrustumCull(const Camera &cam) { return false; }
113 //! Returns true if the instance was culled.
114 virtual bool IsCulled() { return _bCulled; }
115 //! Sets the culled state of the instance.
116 virtual void SetCulled(bool bCulled) { _bCulled = bCulled; }
118 //! Returns true if the instance is currently active.
119 virtual bool IsAlive() { return _bAlive; }
120 //! Activates or deactivates the instance.
121 virtual void SetIsAlive(bool bAlive) { _bAlive = bAlive; }
123 //! Sets the distance of this object from the sort position. Used to sort instances.
124 virtual void SetSquareSortDistance(float rSqrDist) { _rSquareSortDistance = rSqrDist; }
125 //! Returns the distance of this object from the sort position. (Set with SetSquareSortDistance())
126 virtual float GetSquareSortDistace() const { return _rSquareSortDistance; }
128 //! This operator is used to sort instance arrays.
129 bool operator<(const SkyRenderableInstance& instance) const
131 return (_rSquareSortDistance > instance._rSquareSortDistance);
135 bool _bCulled; // Culled flag
136 bool _bAlive; // Alive object flag
138 Vec3f _vecPosition; // Position
139 Mat33f _matRotation; // Rotation
140 Mat33f _matInvRotation; // inverse rotation
141 float _rScale; // Scale
143 // for sorting particles during shading
144 float _rSquareSortDistance;
147 //------------------------------------------------------------------------------
148 // Function : SkyRenderableInstance::GetModelToWorldTransform
150 //------------------------------------------------------------------------------
152 * @fn SkyRenderableInstance::GetModelToWorldTransform(Mat44f &mat) const
153 * @brief Returns the 4x4 transformation matrix from world to model space.
155 inline void SkyRenderableInstance::GetModelToWorldTransform(Mat44f &mat) const
157 mat[0] = _matRotation.M[0]; mat[4] = _matRotation.M[3];
158 mat[8] = _matRotation.M[6]; mat[12] = 0;
159 mat[1] = _matRotation.M[1]; mat[5] = _matRotation.M[4];
160 mat[9] = _matRotation.M[7]; mat[13] = 0;
161 mat[2] = _matRotation.M[2]; mat[6] = _matRotation.M[5];
162 mat[10] = _matRotation.M[8]; mat[14] = 0;
163 mat[3] = 0; mat[7] = 0; mat[11] = 0; mat[15] = 0;
165 // Scale the matrix (we don't want to scale translation or mat[15] which is 1)
169 // Set the translation and w coordinate after the potential scaling
170 mat[12] = _vecPosition.x; mat[13] = _vecPosition.y; mat[14] = _vecPosition.z;
175 //------------------------------------------------------------------------------
176 // Function : Mat44f& SkyRenderableInstance::GetWorldToModelTransform
178 //------------------------------------------------------------------------------
180 * @fn SkyRenderableInstance::GetWorldToModelTransform(Mat44f &mat) const
181 * @brief Returns the 4x4 transformation matrix from world to model space.
183 inline void SkyRenderableInstance::GetWorldToModelTransform(Mat44f &mat) const
185 mat[0] = _matRotation.M[0]; mat[4] = _matRotation.M[1];
186 mat[8] = _matRotation.M[2]; mat[12] = 0;
187 mat[1] = _matRotation.M[3]; mat[5] = _matRotation.M[4];
188 mat[9] = _matRotation.M[5]; mat[13] = 0;
189 mat[2] = _matRotation.M[6]; mat[6] = _matRotation.M[7];
190 mat[10] = _matRotation.M[8]; mat[14] = 0;
191 mat[3] = 0; mat[7] = 0; mat[11] = 0; mat[15] = 0;
193 // Scale the matrix (we don't want to scale translation or mat[15] which is 1)
195 mat *= (1 / _rScale);
197 // Set the translation and w coordinate after the potential scaling
198 mat[12] = -_vecPosition.x; mat[13] = -_vecPosition.y; mat[14] = -_vecPosition.z;
202 #endif //__SKYRENDERABLEINSTANCE_HPP__