[8319] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2006 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main programmer: Marco Biasini |
---|
| 13 | |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #ifndef _TERRAIN_H |
---|
| 17 | #define _TERRAIN_H |
---|
| 18 | |
---|
| 19 | #include "types.h" |
---|
| 20 | #include "terrain_page.h" |
---|
| 21 | #include "terrain_quad.h" |
---|
| 22 | #include <cstdlib> |
---|
| 23 | #include <string> |
---|
[8548] | 24 | #include <list> |
---|
| 25 | #include "material.h" |
---|
[8319] | 26 | #include "texture.h" |
---|
| 27 | #include "frustum.h" |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | typedef struct { |
---|
| 31 | int width; |
---|
| 32 | int height; |
---|
| 33 | int pitch; |
---|
| 34 | UByte *data; |
---|
| 35 | } Heightfield, *pHeightfield; |
---|
| 36 | |
---|
| 37 | class Terrain; |
---|
[8548] | 38 | |
---|
[8319] | 39 | typedef Terrain *pTerrain; |
---|
| 40 | |
---|
| 41 | class TerrainPage; |
---|
| 42 | |
---|
| 43 | class Terrain { |
---|
| 44 | public: |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * The amount of geometry rendered is largely depending on this constant. So chose a |
---|
| 48 | * reasonable value... |
---|
| 49 | */ |
---|
| 50 | inline float getDetail() { return 0.05f; } |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | inline bool debug() |
---|
| 54 | { |
---|
| 55 | return false; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | inline pTerrainPage getActiveList() { return activePages; } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | inline void setCameraPosition( const Triple& _cameraPosition ) |
---|
| 62 | { |
---|
| 63 | cameraPosition = _cameraPosition; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | inline const Triple& getCameraPosition() const |
---|
| 67 | { |
---|
| 68 | return cameraPosition; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void getAltitude( Triple& _alt, Triple& _normal ); |
---|
[8328] | 72 | |
---|
[8319] | 73 | void showPages( int _x0, int _z0, int _width, int _height ); |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Create a new terrain. |
---|
| 77 | */ |
---|
| 78 | Terrain( ) |
---|
| 79 | { |
---|
| 80 | pageSize = 17; |
---|
| 81 | scale = Triple( 1.0f, 3.0f, 1.0f ); |
---|
| 82 | frustum = new Frustum(); |
---|
| 83 | activatedCount = deactivatedCount = 0; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Draws the terrain. |
---|
| 88 | */ |
---|
| 89 | void draw( ); |
---|
| 90 | |
---|
| 91 | void setPageSize( const Triple& _page ) { } |
---|
| 92 | |
---|
| 93 | void build(); |
---|
| 94 | |
---|
| 95 | inline void setActiveList( pTerrainPage _page ) |
---|
| 96 | { |
---|
| 97 | activePages = _page; |
---|
| 98 | } |
---|
| 99 | inline pFrustum getViewingFrustum() |
---|
| 100 | { |
---|
| 101 | return frustum; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * Returns the normalized ( range from [0..1] ) altitude from the heightmap for the given |
---|
| 106 | * coordinates. |
---|
| 107 | */ |
---|
| 108 | inline void incActivated() { activatedCount++; } |
---|
| 109 | inline void incDeactivated() { deactivatedCount++; } |
---|
| 110 | inline float getAltitude( int _x, int _z ) const; |
---|
| 111 | |
---|
[8548] | 112 | inline void addMaterial( Material *_material ) |
---|
[8319] | 113 | { |
---|
[8548] | 114 | materials.push_back( _material ); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /*inline void setLightmap( const std::string &_lightmap ) |
---|
| 118 | { |
---|
[8319] | 119 | lightmapSource = _lightmap; |
---|
[8548] | 120 | }*/ |
---|
[8328] | 121 | inline void setHeightmap( const std::string &_heightmap ) |
---|
[8319] | 122 | { |
---|
| 123 | heightmapSource = _heightmap; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | inline void setPageSize( int _pageSize ) |
---|
| 127 | { |
---|
| 128 | pageSize = _pageSize; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | inline const Triple getScale() const { return scale; } |
---|
[8548] | 132 | |
---|
[8319] | 133 | inline int getPageSize() const |
---|
| 134 | { |
---|
| 135 | return pageSize; |
---|
| 136 | } |
---|
[8548] | 137 | |
---|
[8319] | 138 | inline void getCoord( int _x, int _z, TexCoord& _coord) const |
---|
| 139 | { |
---|
| 140 | _coord.u = ( ( float )_x )/( heightfield.width-1 ); |
---|
| 141 | _coord.v = ( ( float )_z )/( heightfield.height-1 ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | inline void setScale( const Triple& _scale ) |
---|
| 145 | { |
---|
| 146 | scale = _scale; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | inline TerrainPage *getPage( int _x, int _z ) |
---|
| 150 | { |
---|
| 151 | return pages[pagesX*_z+_x]; |
---|
| 152 | } |
---|
| 153 | protected: |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * convenience method to create a new terrain page at position offset |
---|
| 157 | * p = ( _xOffset, _zOffset ). |
---|
| 158 | */ |
---|
| 159 | pTerrainPage createPage( int _xOffset, int _zOffset ) const; |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * Creates the quad tree structure for fast culling of the terrain pages. |
---|
| 163 | */ |
---|
| 164 | pTerrainQuad createQuadTree( int _x0, int _z0, int _x1, int _z1, int _depth = 0 ); |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Walks the quad tree to determine which pages are visible, e.g. are in the viewing |
---|
| 168 | * frustum of the camera. |
---|
| 169 | */ |
---|
| 170 | void determineVisiblePages( pTerrainQuad _node ); |
---|
| 171 | |
---|
[8548] | 172 | pTerrainQuad root; // The quad-tree root node. |
---|
| 173 | pTerrainPage *pages; // the references to all pages |
---|
| 174 | std::string heightmapSource; |
---|
| 175 | std::string lightmapSource; |
---|
| 176 | Heightfield heightfield; |
---|
| 177 | Triple scale; |
---|
| 178 | int pagesX, |
---|
| 179 | pagesZ; |
---|
| 180 | int pageSize; |
---|
| 181 | int cullCount; |
---|
[8319] | 182 | |
---|
[8548] | 183 | int activatedCount, |
---|
| 184 | deactivatedCount; // For debugging and statistics |
---|
| 185 | Texture *tex; |
---|
| 186 | Triple cameraPosition; |
---|
| 187 | pFrustum frustum; |
---|
| 188 | pTerrainPage activePages; |
---|
| 189 | std::vector<Material*> materials; |
---|
[8319] | 190 | }; |
---|
| 191 | |
---|
| 192 | inline float Terrain::getAltitude( int _x, int _z ) const |
---|
| 193 | { |
---|
| 194 | return heightfield.data[heightfield.pitch*_z+_x]/255.0f; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | #endif |
---|