tsLastDetail.h

Engine/source/ts/tsLastDetail.h

More...

Classes:

class

This neat little class renders the object to a texture so that when the object is far away, it can be drawn as a billboard instead of a mesh.

Public Functions

GFXDeclareVertexFormat(ImposterState )

The imposter state vertex format.

Detailed Description

Public Functions

GFXDeclareVertexFormat(ImposterState )

The imposter state vertex format.

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TSLASTDETAIL_H_
 25#define _TSLASTDETAIL_H_
 26
 27#ifndef _MATHTYPES_H_
 28#include "math/mathTypes.h"
 29#endif
 30#ifndef _MPOINT3_H_
 31#include "math/mPoint3.h"
 32#endif
 33#ifndef _MMATRIX_H_
 34#include "math/mMatrix.h"
 35#endif
 36#ifndef _TVECTOR_H_
 37#include "core/util/tVector.h"
 38#endif
 39#ifndef __RESOURCE_H__
 40#include "core/resource.h"
 41#endif
 42#ifndef _TSRENDERDATA_H_
 43#include "ts/tsRenderState.h"
 44#endif
 45#ifndef _GFXVERTEXFORMAT_H_
 46#include "gfx/gfxVertexFormat.h"
 47#endif
 48#ifndef _SIM_H_
 49#include "console/simObject.h"
 50#endif
 51
 52
 53class TSShape;
 54class TSRenderState;
 55class SceneRenderState;
 56class Material;
 57class BaseMatInstance;
 58
 59
 60/// The imposter state vertex format.
 61GFXDeclareVertexFormat( ImposterState )
 62{
 63   /// .xyz = imposter center
 64   /// .w = billboard corner... damn SM 2.0
 65   Point3F center;
 66   F32 corner;
 67
 68   /// .x = scaled half size
 69   /// .y = alpha fade out
 70   F32 halfSize;
 71   F32 alpha;
 72
 73   /// The rotation encoded as the up
 74   /// and right vectors... cross FTW.
 75   Point3F upVec;
 76   Point3F rightVec;
 77};
 78
 79
 80/// This neat little class renders the object to a texture so that when the object
 81/// is far away, it can be drawn as a billboard instead of a mesh.  This happens
 82/// when the model is first loaded as to keep the realtime render as fast as possible.
 83/// It also renders the model from a few different perspectives so that it would actually
 84/// pass as a model instead of a silly old billboard.  In other words, this is an imposter.
 85class TSLastDetail
 86{
 87protected:
 88
 89   /// The shape which we're impostering.
 90   TSShape *mShape;
 91
 92   /// This is the path of the object, which is
 93   /// where we'll be storing our cache for rendered imposters.
 94   String mCachePath;
 95
 96   /// The shape detail level to capture into
 97   /// the imposters.
 98   S32 mDl;
 99
100   /// The bounding radius of the shape 
101   /// used to size the billboard.
102   F32 mRadius;
103
104   /// The center offset for the bounding
105   /// sphere used to render the imposter.
106   Point3F mCenter;
107
108   /// The square dimensions of each 
109   /// captured imposter image.
110   S32 mDim;
111
112   /// The number steps around the equator of
113   /// the globe at which we capture an imposter.
114   U32 mNumEquatorSteps;
115
116   /// The number of steps to go from equator to
117   /// each polar region (0 means equator only) at
118   /// which we capture an imposter.
119   U32 mNumPolarSteps;   
120
121   /// The angle in radians of sub-polar regions.
122   F32 mPolarAngle;
123
124   /// If true we captures polar images in the 
125   /// imposter texture.
126   bool mIncludePoles;
127
128   /// The combined imposter state and corner data vertex
129   /// format used for rendering with multiple streams. 
130   GFXVertexFormat mImposterVertDecl;
131
132   /// The material for this imposter.
133   SimObjectPtr<Material> mMaterial;
134
135   /// The material instance used to render this imposter.
136   BaseMatInstance *mMatInstance;
137
138   /// This is a global list of all the TSLastDetail
139   /// objects in the system.
140   static Vector<TSLastDetail*> smLastDetails;
141
142   /// The maximum texture size for a billboard texture.
143   static const U32 smMaxTexSize = 2048;
144
145   /// This update actually regenerates the imposter images.
146   void _update();
147
148   ///
149   void _validateDim();
150
151   /// Helper which returns the imposter diffuse map path.
152   String _getDiffuseMapPath() const { return mCachePath + ".imposter.dds"; }
153
154   /// Helper which returns the imposter normal map path.
155   String _getNormalMapPath() const { return mCachePath + ".imposter_normals.dds"; }
156
157public:
158
159   TSLastDetail(  TSShape *shape, 
160                  const String &cachePath,
161                  U32 numEquatorSteps, 
162                  U32 numPolarSteps, 
163                  F32 polarAngle, 
164                  bool includePoles, 
165                  S32 dl, 
166                  S32 dim );
167
168   ~TSLastDetail();
169
170   /// Global preference for rendering imposters to shadows.
171   static bool smCanShadow;
172
173   /// Calls update on all TSLastDetail objects in the system.
174   /// @see update()
175   static void updateImposterImages( bool forceUpdate = false );
176
177   /// Loads the imposter images by reading them from the disk
178   /// or generating them if the TSShape is more recient than the
179   /// cached imposter textures.
180   ///
181   /// This should not be called from within any rendering code.
182   ///
183   /// @param forceUpdate  If true the disk cache is invalidated and
184   ///                     new imposter images are rendered.
185   ///
186   void update( bool forceUpdate = false );
187
188
189   /// Internal function called from TSShapeInstance to 
190   /// submit an imposter render instance.
191   void render( const TSRenderState &rdata, F32 alpha );
192
193   /// Returns the material instance used to render this imposter.
194   BaseMatInstance* getMatInstance() const { return mMatInstance; }
195
196   /// Helper function which deletes the cached imposter 
197   /// texture files from disk.
198   void deleteImposterCacheTextures();
199
200   /// Returns the radius.
201   /// @see mRadius
202   F32 getRadius() const { return mRadius; }
203};
204
205
206#endif // _TSLASTDETAIL_H_
207
208
209