Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages

terrain.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                      terrain.h  -  Classes for terrain
00003                              -------------------
00004     begin                : Mon Sep 8 2003
00005     copyright            : (C) 2003 by Cody Russell
00006     email                : cody `at' jhu.edu
00007  ***************************************************************************
00008 
00009  The contents of this file are subject to the Mozilla Public License Version
00010  1.1 (the "License"); you may not use this file except in compliance with
00011  the License. You may obtain a copy of the License at 
00012  http://www.mozilla.org/MPL/
00013 
00014  Software distributed under the License is distributed on an "AS IS" basis,
00015  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  for the specific language governing rights and limitations under the
00017  License.
00018 
00019  The Original Code is the NeoEngine, terrain.h
00020 
00021  The Initial Developer of the Original Code is Cody Russell.
00022  Portions created by Cody Russell are Copyright (C) 2003
00023  Cody Russell. All Rights Reserved.
00024 
00025  ***************************************************************************/
00026 
00027 #ifndef __NETERRAIN_H
00028 #define __NETERRAIN_H
00029 
00030 #include "base.h"
00031 #include "program.h"
00032 #include "render.h"
00033 #include "scenenode.h"
00034 #include "sceneentity.h"
00035 #include "texture.h"
00036 
00037 
00051 namespace NeoEngine
00052 {
00053 
00054 
00064 class NEOENGINE_API TerrainHeightmap
00065 {
00066     friend class TerrainPage;
00067 
00068         protected:
00069 
00071             float                                         *m_pfData;
00072 
00074         int                                            m_iSize;
00075 
00077         Vector3d                                      *m_pkNormalData;
00078 
00080         float                                          m_fHScale;
00081 
00083         float                                          m_fVScale;
00084 
00086         float                                          m_fMaxHeight;
00087 
00089         float                                          m_fMinHeight;
00090 
00091         public:
00092 
00098                                                            TerrainHeightmap( float fHScale = 10.0f, float fVScale = 5.0f );
00099 
00102                 virtual                                       ~TerrainHeightmap();
00103 
00104         void                                           PostLoadInit();
00105 
00110         void                                           Load( ImageData *pkImage );
00111 
00117         void                                           Load( const std::string &rstrFilename, int iSize );
00118 
00125         int                                            GetIndex( int x, int y ) const;
00126 
00133         float                                          GetHeight( int x, int y ) const;
00134 
00141         const Vector3d&                                GetNormal( int x, int y ) const;
00142 
00149         float                                          GetWorldHeight( float x, float y );
00150 
00151         float                                          GetWorldHeight( int iIndex );
00152 
00157         float                                          GetMaxHeight() { return m_fMaxHeight; }
00158 
00163         float                                          GetMinHeight() { return m_fMinHeight; }
00164 
00169         inline const int                               GetSize() const { return m_iSize; }
00170 
00175         const float                                    GetHScale() const { return m_fHScale; }
00176 
00181         const float                                    GetVScale() const { return m_fVScale; }
00182 };
00183 
00184 
00185 class NEOENGINE_API TerrainLightmap
00186 {
00187 
00188         protected:
00189 
00191             int                                            m_iSize;
00192 
00193             float                                         *m_pfShadeMap;
00194 
00195         public:
00196 
00197                                                            TerrainLightmap( int x, int y, TerrainHeightmap *pkHeightmap, float fMaxBright = 0.8f, float fMinBright = 0.2f, float fLightSoftness = 25.0f );
00198 
00199     virtual                                               ~TerrainLightmap() { delete [] m_pfShadeMap; }
00200 
00201     inline float                                           GetLighting( int x, int y ) { return m_pfShadeMap[ y * m_iSize + x ]; }
00202 };
00203 
00204 
00205 class NEOENGINE_API TerrainTextureComponent
00206 {
00207         protected:
00208  
00209                 float m_fMin;
00210                 float m_fOptimal;
00211                 float m_fMax;
00212  
00213         public:
00214                                                                                 
00215                 void Set( float fMin, float fOpt, float fMax );
00216                                                                                 
00217                 unsigned char CalculateBlendFactor( float f, TerrainHeightmap *pkHeightap );
00218 };
00219 
00220 
00221 class NEOENGINE_API TerrainShader
00222 {
00223         public:
00224 
00225             enum LIGHTINGTYPE
00226         {
00227             LIGHT_SOFTWARE,
00228             LIGHT_HARDWARE
00229         };
00230 
00231         enum TEXTURINGTYPE
00232         {
00233             TEXTURE_SOFTWARE,
00234             TEXTURE_HARDWARE
00235         };
00236 
00237                 enum RANGECOMPONENT
00238                 {
00239                         LOWER  = 0,
00240                         MIDDLE = 1,
00241                         UPPER  = 2
00242                 };
00243 
00244         protected:
00245 
00246         LIGHTINGTYPE                                   m_eLightingType;
00247 
00248         TEXTURINGTYPE                                  m_eTexturingType;
00249 
00250         TerrainTextureComponent                        m_akRanges[4];
00251 
00253         ProgramParam                                  *m_pkTransformParam;
00254 
00256         ProgramParam                                  *m_pkLightParam;
00257 
00259         ProgramParam                                  *m_pkAmbientParam;
00260 
00262         Vector3d                                       m_kAmbient;
00263 
00265         Vector3d                                       m_kLight;
00266 
00268                 ProgramParam                                  *m_pkBlendMapParam;
00269  
00271                 ProgramParam                                  *m_pkTexture1Param;
00272  
00274                 ProgramParam                                  *m_pkTexture2Param;
00275  
00277                 ProgramParam                                  *m_pkTexture3Param;
00278 
00280         ProgramParam                                  *m_pkTexture4Param;
00281 
00282         std::string m_rstrFile1;
00283         std::string m_rstrFile2;
00284         std::string m_rstrFile3;
00285         std::string m_rstrFile4;
00286 
00287         std::string m_strBlendmap;
00288 
00289         bool m_bCalculateBlendmap;
00290 
00291         public:
00292 
00293         MaterialPtr                                    m_pkMaterial;
00294 
00295         TerrainShader( MaterialPtr pkMaterial, LIGHTINGTYPE eLightType = LIGHT_HARDWARE, TEXTURINGTYPE = TEXTURE_HARDWARE );
00296 
00297         virtual ~TerrainShader() {}
00298 
00299         TexturePtr TerrainShader::CalculateBlendmap( TerrainPage *pkPage, int iSize );
00300 
00301         void SetTextures( const std::string &rstrFile1, const std::string &rstrFile2, const std::string &rstrFile3, const std::string &rstrFile4 );
00302 
00303         void SetAmbient( const Vector3d &rkAmbient );
00304 
00305         void SetBlendmap( const std::string &rstrFile );
00306 
00307             void GenerateMaterial( TerrainPage *pkPage );
00308 
00309         LIGHTINGTYPE GetLighting() { return m_eLightingType; }
00310 };
00311 
00312 
00313 class NEOENGINE_API TerrainBlock : public SceneEntity
00314 {
00315         protected:
00316 
00318             int                                            m_iOffsetX;
00319 
00321         int                                            m_iOffsetY;
00322 
00324         int                                            m_iSize;
00325 
00327         int                                            m_iBlockIndex;
00328 
00330         //PolygonBufferPtr                               m_pkPolygons;
00331 
00333         bool                                           m_bNeedUpdate;
00334 
00338         virtual void                                   UpdateData() { }
00339 
00340         public:
00341 
00342                                                       TerrainBlock( int iOffsetX, int iOffsetY, int iSize, int iIndex );
00343 
00344         virtual                                      ~TerrainBlock() {}
00345 
00346         int                                           GetOffsetX() { return m_iOffsetX; }
00347 
00348         int                                           GetOffsetY() { return m_iOffsetY; }
00349 
00350         virtual VertexBufferPtr                       &GetVertexBuffer() = 0;
00351 
00352         virtual PolygonBufferPtr                      &GetPolygonBuffer() = 0;
00353 
00354         virtual bool                                   Render( Frustum *pkFrustum, bool bForce = false ) = 0;
00355 
00356         virtual void                                   GenerateAABB() = 0;
00357 };
00358 
00359 #ifdef WIN32
00360 
00361 #ifndef __HAVE_VECTOR_NETERRAINBLOCK
00362      UDTVectorEXPIMP( TerrainBlock* );
00363 #    define __HAVE_VECTOR_NETERRAINBLOCK
00364 #  endif
00365 
00366 #endif // WIN32
00367 
00368 class NEOENGINE_API TerrainPage : public SceneNode
00369 {
00370     friend class TerrainShader;
00371 
00372         public:
00373 
00374             DefineVisitable();
00375 
00376         protected:
00377 
00379             TerrainHeightmap                              *m_pkHeightmap;
00380 
00382         TerrainLightmap                               *m_pkLightmap;
00383 
00385         std::vector< TerrainBlock* >                   m_vpkBlocks;
00386 
00387         int                                            m_iBlockSize;
00388 
00390         TerrainShader                                 *m_pkShader;
00391 
00392         public:
00393 
00394                                                            TerrainPage( TerrainHeightmap *pkHeightmap, TerrainShader *pkShader );
00395 
00396         virtual                                       ~TerrainPage() { delete m_pkHeightmap; }
00397 
00398         TerrainHeightmap                              *GetHeightmap() { return m_pkHeightmap; }
00399 
00400         TerrainBlock                                  *GetBlock( int iIndex ) { return m_vpkBlocks[ iIndex ]; }
00401 
00402         TerrainBlock                                  *GetBlock( int x, int y ) { return GetBlock( y * GetNumBlocksOnSide() + x ); }
00403 
00405         int                                            GetTotalBlocks() { return m_vpkBlocks.size(); }
00406 
00408         int                                            GetNumBlocksOnSide() { return m_pkHeightmap->GetSize() / m_iBlockSize; }
00409 
00410         int                                            GetBlockSize() { return m_iBlockSize; }
00411 
00412         int                                            GetSize() { return m_pkHeightmap->GetSize(); }
00413 
00414         void                                           GenerateAABB();
00415 
00421         virtual bool                                   Render( Frustum *pkFrustum, bool bForce = false ) = 0;
00422 };
00423 
00424 #ifdef WIN32
00425 
00426 #ifndef __HAVE_VECTOR_NETERRAINPAGE
00427      UDTVectorEXPIMP( TerrainPage* );
00428 #    define __HAVE_VECTOR_NETERRAINPAGE
00429 #  endif
00430 
00431 #endif // WIN32
00432 
00438 class NEOENGINE_API TerrainManager
00439 {
00440 
00441         protected:
00442 
00444             std::vector< ModulePtr >                       m_vpkModules;
00445 
00447         std::vector< TerrainPage* >                    m_vpkPages;
00448 
00449  public:
00450 
00453                                                        TerrainManager();
00454 
00457             virtual                                       ~TerrainManager();
00458 
00464         ModulePtr                                      LoadModule( const HashString &rstrName );
00465 
00471         virtual TerrainPage                           *CreateTerrain( TerrainHeightmap *pkHeightmap, TerrainShader *pkShader ) = 0;
00472 
00473         virtual void                                   ResetFrame() { }
00474 };
00475 
00476 
00477 }; // namespace NeoEngine
00478 
00479 
00480 #endif // __NETERRAIN_H

Generated on Wed Jan 21 14:21:07 2004 for NeoEngine by doxygen 1.3.5