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