[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 | |
---|
[8569] | 19 | #define USE_VBO |
---|
| 20 | #ifdef USE_VBO |
---|
| 21 | #include "buffer_broker.h" |
---|
| 22 | #endif |
---|
[9147] | 23 | |
---|
| 24 | #include "vector.h" |
---|
[8319] | 25 | #include "terrain_page.h" |
---|
| 26 | #include "terrain_quad.h" |
---|
| 27 | #include <cstdlib> |
---|
| 28 | #include <string> |
---|
[8744] | 29 | #include <vector> |
---|
[8319] | 30 | #include "texture.h" |
---|
| 31 | #include "frustum.h" |
---|
| 32 | |
---|
[9147] | 33 | class Texture; |
---|
[8744] | 34 | class Terrain; |
---|
| 35 | typedef Terrain *pTerrain; |
---|
| 36 | |
---|
[8319] | 37 | class Terrain { |
---|
| 38 | public: |
---|
[8745] | 39 | const static unsigned int PAGE_SIZE = 17; |
---|
| 40 | const static unsigned int MAX_VERTICES = PAGE_SIZE*PAGE_SIZE; |
---|
| 41 | const static unsigned int MAX_INDICES = 2*( PAGE_SIZE*PAGE_SIZE + PAGE_SIZE -2 ); |
---|
[8319] | 42 | /** |
---|
| 43 | * The amount of geometry rendered is largely depending on this constant. So chose a |
---|
| 44 | * reasonable value... |
---|
| 45 | */ |
---|
[8741] | 46 | inline float getDetail() { return 0.02f; } |
---|
[8319] | 47 | |
---|
| 48 | |
---|
| 49 | inline bool debug() |
---|
| 50 | { |
---|
| 51 | return false; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | inline pTerrainPage getActiveList() { return activePages; } |
---|
| 55 | |
---|
| 56 | |
---|
[9147] | 57 | inline void setCameraPosition( const Vector& _cameraPosition ) |
---|
[8319] | 58 | { |
---|
| 59 | cameraPosition = _cameraPosition; |
---|
| 60 | } |
---|
| 61 | |
---|
[9147] | 62 | inline const Vector& getCameraPosition() const |
---|
[8319] | 63 | { |
---|
| 64 | return cameraPosition; |
---|
| 65 | } |
---|
| 66 | |
---|
[9147] | 67 | void getAltitude( Vector& _alt, Vector& _normal ); |
---|
[8328] | 68 | |
---|
[8744] | 69 | inline void setLightmap( Texture *_lightmap ) |
---|
| 70 | { |
---|
| 71 | lightmap = _lightmap; |
---|
| 72 | } |
---|
[8641] | 73 | //What a bad name: Need to rename that :( |
---|
| 74 | void addLevelFourPage( int _numVertices, Vertex *_vertices, |
---|
| 75 | int _numIndices, unsigned short *_indices ); |
---|
[8319] | 76 | void showPages( int _x0, int _z0, int _width, int _height ); |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Create a new terrain. |
---|
| 80 | */ |
---|
| 81 | Terrain( ) |
---|
| 82 | { |
---|
| 83 | pageSize = 17; |
---|
[8770] | 84 | pagesX = pagesZ = 0; |
---|
| 85 | lightmap = NULL; |
---|
| 86 | heightmapSource = ""; |
---|
| 87 | root = NULL; |
---|
| 88 | current = 0; |
---|
| 89 | activePages = NULL; |
---|
[9147] | 90 | scale = Vector( 1.0f, 3.0f, 1.0f ); |
---|
[8319] | 91 | frustum = new Frustum(); |
---|
| 92 | activatedCount = deactivatedCount = 0; |
---|
[8641] | 93 | vertices = new Vertex[MAX_VERTICES]; |
---|
| 94 | indices = new unsigned short[MAX_INDICES]; |
---|
[8569] | 95 | #ifdef USE_VBO |
---|
[9071] | 96 | broker = new BufferBroker( 300, MAX_VERTICES*sizeof( Vertex ), |
---|
[8641] | 97 | MAX_INDICES*sizeof( short ) ); |
---|
[8569] | 98 | #endif |
---|
[8319] | 99 | } |
---|
| 100 | |
---|
[8775] | 101 | ~Terrain( ) |
---|
| 102 | { |
---|
| 103 | //The pages are deleted through the SAVE_DELETE( root ) call. |
---|
| 104 | SAVE_DELETE_ARRAY( pages ); |
---|
| 105 | SAVE_DELETE_ARRAY( vertices ); |
---|
| 106 | SAVE_DELETE_ARRAY( indices ); |
---|
| 107 | SAVE_DELETE( lightmap ); |
---|
| 108 | SAVE_DELETE( root ); |
---|
| 109 | #ifdef USE_VBO |
---|
| 110 | SAVE_DELETE( broker ); |
---|
| 111 | #endif |
---|
| 112 | } |
---|
[8319] | 113 | /** |
---|
| 114 | * Draws the terrain. |
---|
| 115 | */ |
---|
| 116 | void draw( ); |
---|
| 117 | |
---|
[9147] | 118 | void setPageSize( const Vector& _page ) { } |
---|
[8319] | 119 | |
---|
| 120 | void build(); |
---|
[8929] | 121 | void tick( float _dt ); |
---|
[8319] | 122 | inline void setActiveList( pTerrainPage _page ) |
---|
| 123 | { |
---|
| 124 | activePages = _page; |
---|
| 125 | } |
---|
| 126 | inline pFrustum getViewingFrustum() |
---|
| 127 | { |
---|
| 128 | return frustum; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Returns the normalized ( range from [0..1] ) altitude from the heightmap for the given |
---|
| 133 | * coordinates. |
---|
| 134 | */ |
---|
| 135 | inline float getAltitude( int _x, int _z ) const; |
---|
| 136 | |
---|
[8744] | 137 | inline void addMaterialLayer( LayerInfo *_layer ) |
---|
| 138 | { layers.push_back( _layer ); } |
---|
[8548] | 139 | |
---|
[8328] | 140 | inline void setHeightmap( const std::string &_heightmap ) |
---|
[8319] | 141 | { |
---|
| 142 | heightmapSource = _heightmap; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | inline void setPageSize( int _pageSize ) |
---|
| 146 | { |
---|
| 147 | pageSize = _pageSize; |
---|
| 148 | } |
---|
| 149 | |
---|
[9147] | 150 | inline const Vector getScale() const { return scale; } |
---|
[8548] | 151 | |
---|
[8319] | 152 | inline int getPageSize() const |
---|
| 153 | { |
---|
| 154 | return pageSize; |
---|
| 155 | } |
---|
[8548] | 156 | |
---|
[8319] | 157 | inline void getCoord( int _x, int _z, TexCoord& _coord) const |
---|
| 158 | { |
---|
| 159 | _coord.u = ( ( float )_x )/( heightfield.width-1 ); |
---|
| 160 | _coord.v = ( ( float )_z )/( heightfield.height-1 ); |
---|
| 161 | } |
---|
| 162 | |
---|
[9147] | 163 | inline void setScale( const Vector& _scale ) |
---|
[8775] | 164 | { scale = _scale; } |
---|
[8569] | 165 | #ifdef USE_VBO |
---|
| 166 | inline pBufferBroker getBufferBroker() { return broker; } |
---|
| 167 | #endif |
---|
[8319] | 168 | inline TerrainPage *getPage( int _x, int _z ) |
---|
| 169 | { |
---|
| 170 | return pages[pagesX*_z+_x]; |
---|
| 171 | } |
---|
| 172 | protected: |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * convenience method to create a new terrain page at position offset |
---|
| 176 | * p = ( _xOffset, _zOffset ). |
---|
| 177 | */ |
---|
| 178 | pTerrainPage createPage( int _xOffset, int _zOffset ) const; |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Creates the quad tree structure for fast culling of the terrain pages. |
---|
| 182 | */ |
---|
[8748] | 183 | pTerrainQuad createQuadTree( int _x0, int _z0, |
---|
| 184 | int _x1, int _z1, int _depth = 0 ); |
---|
[8319] | 185 | |
---|
| 186 | /** |
---|
| 187 | * Walks the quad tree to determine which pages are visible, e.g. are in the viewing |
---|
| 188 | * frustum of the camera. |
---|
| 189 | */ |
---|
[8745] | 190 | void determineVisiblePages( pTerrainQuad _node ); |
---|
| 191 | void determineLayerVisibility( int _layer ); |
---|
[8319] | 192 | |
---|
[8569] | 193 | #ifdef USE_VBO |
---|
[8641] | 194 | pBufferBroker broker; |
---|
[8569] | 195 | #endif |
---|
[8641] | 196 | pTerrainQuad root; // The quad-tree root node. |
---|
| 197 | pTerrainPage *pages; // the references to all pages |
---|
| 198 | std::string heightmapSource; |
---|
[8744] | 199 | Texture *lightmap; |
---|
[8641] | 200 | Heightfield heightfield; |
---|
[9147] | 201 | Vector scale; |
---|
[8641] | 202 | int pagesX, |
---|
| 203 | pagesZ; |
---|
| 204 | int pageSize; |
---|
| 205 | int cullCount; |
---|
[8319] | 206 | |
---|
[8641] | 207 | int activatedCount, |
---|
| 208 | deactivatedCount; // For debugging and statistics |
---|
| 209 | |
---|
| 210 | //The next five members are for the level 4 pages. |
---|
| 211 | Vertex *vertices; |
---|
| 212 | unsigned short *indices; |
---|
| 213 | int current; |
---|
| 214 | |
---|
| 215 | std::vector<BufferInfo> buffers; |
---|
| 216 | |
---|
[9147] | 217 | Vector cameraPosition; |
---|
[8641] | 218 | pFrustum frustum; |
---|
| 219 | pTerrainPage activePages; |
---|
[8744] | 220 | std::vector<LayerInfo*> layers; |
---|
[8319] | 221 | }; |
---|
| 222 | |
---|
| 223 | inline float Terrain::getAltitude( int _x, int _z ) const |
---|
| 224 | { |
---|
[8770] | 225 | assert( heightfield.data ); |
---|
[8748] | 226 | if ( _x < 0 || _x >= heightfield.width || _z < 0 || _z >= heightfield.height ) { |
---|
[8747] | 227 | return 0.0f; |
---|
[8748] | 228 | printf( "outside!\n" ); |
---|
| 229 | } |
---|
[8319] | 230 | return heightfield.data[heightfield.pitch*_z+_x]/255.0f; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | #endif |
---|