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