2008-05-22 04:51:37 -07:00
|
|
|
// Copyright (C) 2002-2008 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
// The code for the TerrainSceneNode is based on the GeoMipMapSceneNode
|
|
|
|
// developed by Spintz. He made it available for Irrlicht and allowed it to be
|
2007-09-24 15:41:52 -07:00
|
|
|
// distributed under this licence. I only modified some parts. A lot of thanks
|
|
|
|
// go to him.
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
#include "CTerrainSceneNode.h"
|
|
|
|
#include "CTerrainTriangleSelector.h"
|
|
|
|
#include "IVideoDriver.h"
|
|
|
|
#include "ISceneManager.h"
|
|
|
|
#include "ICameraSceneNode.h"
|
|
|
|
#include "SMeshBufferLightMap.h"
|
|
|
|
#include "SViewFrustum.h"
|
|
|
|
#include "irrMath.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "IGUIFont.h"
|
|
|
|
#include "IFileSystem.h"
|
|
|
|
#include "IReadFile.h"
|
2008-03-04 16:02:00 -08:00
|
|
|
#include "ITextSceneNode.h"
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
//! constructor
|
|
|
|
CTerrainSceneNode::CTerrainSceneNode(ISceneNode* parent, ISceneManager* mgr,
|
|
|
|
io::IFileSystem* fs, s32 id, s32 maxLOD, E_TERRAIN_PATCH_SIZE patchSize,
|
|
|
|
const core::vector3df& position,
|
2008-01-06 14:56:36 -08:00
|
|
|
const core::vector3df& rotation,
|
2007-05-20 11:03:49 -07:00
|
|
|
const core::vector3df& scale)
|
|
|
|
: ITerrainSceneNode(parent, mgr, id, position, rotation, scale),
|
2008-08-06 07:45:22 -07:00
|
|
|
TerrainData(patchSize, maxLOD, position, rotation, scale), RenderBuffer(0),
|
2007-05-20 11:03:49 -07:00
|
|
|
VerticesToRender(0), IndicesToRender(0), DynamicSelectorUpdate(false),
|
|
|
|
OverrideDistanceThreshold(false), UseDefaultRotationPivot(true), ForceRecalculation(false),
|
|
|
|
OldCameraPosition(core::vector3df(-99999.9f, -99999.9f, -99999.9f)),
|
|
|
|
OldCameraRotation(core::vector3df(-99999.9f, -99999.9f, -99999.9f)),
|
2008-10-16 22:11:14 -07:00
|
|
|
CameraMovementDelta(10.0f), CameraRotationDelta(1.0f),CameraFOVDelta(0.1f),
|
2007-05-20 11:03:49 -07:00
|
|
|
TCoordScale1(1.0f), TCoordScale2(1.0f), FileSystem(fs)
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("CTerrainSceneNode");
|
|
|
|
#endif
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer = new CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
|
2008-08-06 07:45:22 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
if (FileSystem)
|
|
|
|
FileSystem->grab();
|
|
|
|
|
|
|
|
setAutomaticCulling( scene::EAC_OFF );
|
|
|
|
}
|
|
|
|
|
2007-09-24 15:41:52 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! destructor
|
|
|
|
CTerrainSceneNode::~CTerrainSceneNode ( )
|
|
|
|
{
|
2007-12-23 09:04:40 -08:00
|
|
|
delete [] TerrainData.Patches;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
if (FileSystem)
|
|
|
|
FileSystem->drop();
|
2008-08-06 07:45:22 -07:00
|
|
|
|
|
|
|
if (RenderBuffer)
|
|
|
|
RenderBuffer->drop();
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
2007-09-24 15:41:52 -07:00
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
//! Initializes the terrain data. Loads the vertices from the heightMapFile
|
2007-05-20 11:03:49 -07:00
|
|
|
bool CTerrainSceneNode::loadHeightMap( io::IReadFile* file, video::SColor vertexColor, s32 smoothFactor )
|
|
|
|
{
|
|
|
|
if( !file )
|
|
|
|
return false;
|
|
|
|
|
2008-11-20 16:02:58 -08:00
|
|
|
Mesh.MeshBuffers.clear();
|
2007-05-20 11:03:49 -07:00
|
|
|
u32 startTime = os::Timer::getRealTime();
|
|
|
|
video::IImage* heightMap = SceneManager->getVideoDriver()->createImageFromFile( file );
|
|
|
|
|
|
|
|
if( !heightMap )
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
os::Printer::print( "Unable to load heightmap." );
|
2007-05-20 11:03:49 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeightmapFile = file->getFileName();
|
|
|
|
|
|
|
|
// Get the dimension of the heightmap data
|
|
|
|
TerrainData.Size = heightMap->getDimension().Width;
|
|
|
|
|
|
|
|
switch( TerrainData.PatchSize )
|
|
|
|
{
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_9:
|
|
|
|
if( TerrainData.MaxLOD > 3 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 3;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_17:
|
|
|
|
if( TerrainData.MaxLOD > 4 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 4;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_33:
|
|
|
|
if( TerrainData.MaxLOD > 5 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 5;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_65:
|
|
|
|
if( TerrainData.MaxLOD > 6 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 6;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_129:
|
|
|
|
if( TerrainData.MaxLOD > 7 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 7;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Generate vertex data from heightmap ----
|
|
|
|
// resize the vertex array for the mesh buffer one time ( makes loading faster )
|
2008-08-27 21:00:22 -07:00
|
|
|
//SMeshBufferLightMap* mb = new SMeshBufferLightMap();
|
|
|
|
|
|
|
|
scene::CDynamicMeshBuffer *mb=0;
|
|
|
|
|
|
|
|
if ((heightMap->getDimension().Width*heightMap->getDimension().Height) <65535)
|
|
|
|
{
|
|
|
|
//small enough for 16bit buffers
|
|
|
|
mb=new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
|
|
|
|
RenderBuffer->getIndexBuffer().setType(video::EIT_16BIT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//we need 32bit buffers
|
|
|
|
mb=new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_32BIT);
|
|
|
|
RenderBuffer->getIndexBuffer().setType(video::EIT_32BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
mb->getVertexBuffer().set_used( TerrainData.Size * TerrainData.Size );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
video::S3DVertex2TCoords vertex;
|
|
|
|
vertex.Normal.set( 0.0f, 1.0f, 0.0f );
|
|
|
|
vertex.Color = vertexColor;
|
|
|
|
|
|
|
|
// Read the heightmap to get the vertex data
|
|
|
|
// Apply positions changes, scaling changes
|
|
|
|
const f32 tdSize = 1.0f/(f32)(TerrainData.Size-1);
|
|
|
|
s32 index = 0;
|
|
|
|
for( s32 x = 0; x < TerrainData.Size; ++x )
|
|
|
|
{
|
|
|
|
for( s32 z = 0; z < TerrainData.Size; ++z )
|
|
|
|
{
|
|
|
|
vertex.Pos.X = (f32)x;
|
|
|
|
video::SColor pixelColor(heightMap->getPixel(x,z));
|
|
|
|
vertex.Pos.Y = (f32) pixelColor.getLuminance();
|
|
|
|
vertex.Pos.Z = (f32)z;
|
|
|
|
|
|
|
|
vertex.TCoords.X = vertex.TCoords2.X = x * tdSize;
|
|
|
|
vertex.TCoords.Y = vertex.TCoords2.Y = z * tdSize;
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
mb->getVertexBuffer()[index] = vertex;
|
2007-05-20 11:03:49 -07:00
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop heightMap, no longer needed
|
|
|
|
heightMap->drop();
|
|
|
|
|
2008-01-06 14:56:36 -08:00
|
|
|
smoothTerrain(mb, smoothFactor);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// calculate smooth normals for the vertices
|
2008-01-06 14:56:36 -08:00
|
|
|
calculateNormals( mb );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// add the MeshBuffer to the mesh
|
2008-01-06 14:56:36 -08:00
|
|
|
Mesh.addMeshBuffer( mb );
|
|
|
|
const u32 vertexCount = mb->getVertexCount();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// We copy the data to the renderBuffer, after the normals have been calculated.
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getVertexBuffer().set_used( vertexCount );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2007-09-24 15:41:52 -07:00
|
|
|
for( u32 i = 0; i < vertexCount; ++i )
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getVertexBuffer()[i] = mb->getVertexBuffer()[i];
|
|
|
|
RenderBuffer->getVertexBuffer()[i].Pos *= TerrainData.Scale;
|
|
|
|
RenderBuffer->getVertexBuffer()[i].Pos += TerrainData.Position;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
2008-01-06 14:56:36 -08:00
|
|
|
// We no longer need the mb
|
|
|
|
mb->drop();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// calculate all the necessary data for the patches and the terrain
|
|
|
|
calculateDistanceThresholds();
|
|
|
|
createPatches();
|
|
|
|
calculatePatchData();
|
|
|
|
|
|
|
|
// set the default rotation pivot point to the terrain nodes center
|
|
|
|
TerrainData.RotationPivot = TerrainData.Center;
|
|
|
|
|
2007-09-24 15:41:52 -07:00
|
|
|
// Rotate the vertices of the terrain by the rotation
|
2008-11-20 15:50:43 -08:00
|
|
|
// specified. Must be done after calculating the terrain data,
|
2007-09-24 15:41:52 -07:00
|
|
|
// so we know what the current center of the terrain is.
|
2007-05-20 11:03:49 -07:00
|
|
|
setRotation( TerrainData.Rotation );
|
|
|
|
|
|
|
|
// Pre-allocate memory for indices
|
2008-08-27 21:00:22 -07:00
|
|
|
|
|
|
|
RenderBuffer->getIndexBuffer().set_used( TerrainData.PatchCount * TerrainData.PatchCount *
|
2007-05-20 11:03:49 -07:00
|
|
|
TerrainData.CalcPatchSize * TerrainData.CalcPatchSize * 6 );
|
2008-06-13 07:01:17 -07:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
RenderBuffer->setDirty();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2007-09-24 15:41:52 -07:00
|
|
|
const u32 endTime = os::Timer::getRealTime();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
c8 tmp[255];
|
|
|
|
sprintf(tmp, "Generated terrain data (%dx%d) in %.4f seconds",
|
|
|
|
TerrainData.Size, TerrainData.Size, ( endTime - startTime ) / 1000.0f );
|
|
|
|
os::Printer::log( tmp );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-25 09:06:06 -07:00
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
//! Initializes the terrain data. Loads the vertices from the heightMapFile
|
2007-05-20 11:03:49 -07:00
|
|
|
bool CTerrainSceneNode::loadHeightMapRAW( io::IReadFile* file, s32 bitsPerPixel, video::SColor vertexColor, s32 smoothFactor )
|
|
|
|
{
|
|
|
|
if( !file )
|
|
|
|
return false;
|
|
|
|
|
2008-11-20 16:02:58 -08:00
|
|
|
Mesh.MeshBuffers.clear();
|
2007-05-20 11:03:49 -07:00
|
|
|
// start reading
|
|
|
|
u32 startTime = os::Timer::getTime();
|
|
|
|
|
|
|
|
// get file size
|
2007-09-14 02:07:11 -07:00
|
|
|
const long fileSize = file->getSize();
|
2008-11-20 15:50:43 -08:00
|
|
|
// TODO: Currently no floats are supported
|
2007-09-14 02:07:11 -07:00
|
|
|
const s32 bytesPerPixel = bitsPerPixel / 8;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// Get the dimension of the heightmap data
|
2007-09-14 02:07:11 -07:00
|
|
|
TerrainData.Size = core::floor32(sqrtf( (f32)( fileSize / bytesPerPixel ) ));
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
switch( TerrainData.PatchSize )
|
|
|
|
{
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_9:
|
|
|
|
if( TerrainData.MaxLOD > 3 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 3;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_17:
|
|
|
|
if( TerrainData.MaxLOD > 4 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 4;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_33:
|
|
|
|
if( TerrainData.MaxLOD > 5 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 5;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_65:
|
|
|
|
if( TerrainData.MaxLOD > 6 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 6;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
2007-09-24 15:41:52 -07:00
|
|
|
case ETPS_129:
|
|
|
|
if( TerrainData.MaxLOD > 7 )
|
|
|
|
{
|
|
|
|
TerrainData.MaxLOD = 7;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Generate vertex data from heightmap ----
|
|
|
|
// resize the vertex array for the mesh buffer one time ( makes loading faster )
|
2008-08-27 21:00:22 -07:00
|
|
|
scene::CDynamicMeshBuffer *mb=new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
|
|
|
|
|
|
|
|
mb->getVertexBuffer().reallocate( TerrainData.Size * TerrainData.Size );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
video::S3DVertex2TCoords vertex;
|
|
|
|
vertex.Normal.set( 0.0f, 1.0f, 0.0f );
|
|
|
|
vertex.Color = vertexColor;
|
|
|
|
|
|
|
|
// Read the heightmap to get the vertex data
|
|
|
|
// Apply positions changes, scaling changes
|
|
|
|
const f32 tdSize = 1.0f/(f32)(TerrainData.Size-1);
|
|
|
|
for( s32 x = 0; x < TerrainData.Size; ++x )
|
|
|
|
{
|
|
|
|
for( s32 z = 0; z < TerrainData.Size; ++z )
|
|
|
|
{
|
|
|
|
vertex.Pos.X = (f32)x;
|
|
|
|
|
|
|
|
if( file->read( &vertex.Pos.Y, bytesPerPixel ) != bytesPerPixel )
|
|
|
|
{
|
|
|
|
os::Printer::print("Error reading heightmap RAW file.");
|
2008-01-06 14:56:36 -08:00
|
|
|
mb->drop();
|
2007-05-20 11:03:49 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertex.Pos.Z = (f32)z;
|
|
|
|
|
|
|
|
vertex.TCoords.X = vertex.TCoords2.X = x * tdSize;
|
|
|
|
vertex.TCoords.Y = vertex.TCoords2.Y = z * tdSize;
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
mb->getVertexBuffer().push_back( vertex );
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-06 14:56:36 -08:00
|
|
|
smoothTerrain(mb, smoothFactor);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// calculate smooth normals for the vertices
|
2008-01-06 14:56:36 -08:00
|
|
|
calculateNormals( mb );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// add the MeshBuffer to the mesh
|
2008-01-06 14:56:36 -08:00
|
|
|
Mesh.addMeshBuffer( mb );
|
|
|
|
const u32 vertexCount = mb->getVertexCount();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// We copy the data to the renderBuffer, after the normals have been calculated.
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getVertexBuffer().set_used( vertexCount );
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2007-09-24 15:41:52 -07:00
|
|
|
for( u32 i = 0; i < vertexCount; i++ )
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getVertexBuffer()[i] = mb->getVertexBuffer()[i];
|
|
|
|
RenderBuffer->getVertexBuffer()[i].Pos *= TerrainData.Scale;
|
|
|
|
RenderBuffer->getVertexBuffer()[i].Pos += TerrainData.Position;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
2008-01-06 14:56:36 -08:00
|
|
|
// We no longer need the mb
|
|
|
|
mb->drop();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// calculate all the necessary data for the patches and the terrain
|
|
|
|
calculateDistanceThresholds();
|
|
|
|
createPatches();
|
|
|
|
calculatePatchData();
|
|
|
|
|
|
|
|
// set the default rotation pivot point to the terrain nodes center
|
|
|
|
TerrainData.RotationPivot = TerrainData.Center;
|
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
// Rotate the vertices of the terrain by the rotation specified. Must be done
|
2007-05-20 11:03:49 -07:00
|
|
|
// after calculating the terrain data, so we know what the current center of the
|
|
|
|
// terrain is.
|
|
|
|
setRotation( TerrainData.Rotation );
|
|
|
|
|
|
|
|
// Pre-allocate memory for indices
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getIndexBuffer().set_used( TerrainData.PatchCount * TerrainData.PatchCount *
|
2007-05-20 11:03:49 -07:00
|
|
|
TerrainData.CalcPatchSize * TerrainData.CalcPatchSize * 6 );
|
|
|
|
|
|
|
|
u32 endTime = os::Timer::getTime();
|
|
|
|
|
|
|
|
c8 tmp[255];
|
|
|
|
sprintf( tmp, "Generated terrain data (%dx%d) in %.4f seconds",
|
|
|
|
TerrainData.Size, TerrainData.Size, (endTime - startTime) / 1000.0f );
|
|
|
|
os::Printer::print( tmp );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-25 09:06:06 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Sets the scale of the scene node.
|
|
|
|
//! \param scale: New scale of the node
|
|
|
|
void CTerrainSceneNode::setScale(const core::vector3df& scale)
|
|
|
|
{
|
|
|
|
TerrainData.Scale = scale;
|
|
|
|
applyTransformation();
|
|
|
|
ForceRecalculation = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Sets the rotation of the node. This only modifies
|
|
|
|
//! the relative rotation of the node.
|
|
|
|
//! \param rotation: New rotation of the node in degrees.
|
|
|
|
void CTerrainSceneNode::setRotation(const core::vector3df& rotation)
|
|
|
|
{
|
|
|
|
TerrainData.Rotation = rotation;
|
|
|
|
applyTransformation();
|
|
|
|
ForceRecalculation = true;
|
|
|
|
}
|
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
//! Sets the pivot point for rotation of this node. This is useful for the TiledTerrainManager to
|
2007-05-20 11:03:49 -07:00
|
|
|
//! rotate all terrain tiles around a global world point.
|
|
|
|
//! NOTE: The default for the RotationPivot will be the center of the individual tile.
|
|
|
|
void CTerrainSceneNode::setRotationPivot( const core::vector3df& pivot )
|
|
|
|
{
|
|
|
|
UseDefaultRotationPivot = false;
|
|
|
|
TerrainData.RotationPivot = pivot;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Sets the position of the node.
|
|
|
|
//! \param newpos: New postition of the scene node.
|
|
|
|
void CTerrainSceneNode::setPosition ( const core::vector3df& newpos )
|
|
|
|
{
|
|
|
|
TerrainData.Position = newpos;
|
|
|
|
applyTransformation();
|
|
|
|
ForceRecalculation = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Apply transformation changes( scale, position, rotation )
|
|
|
|
void CTerrainSceneNode::applyTransformation()
|
|
|
|
{
|
|
|
|
if( !Mesh.getMeshBufferCount() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
TerrainData.Position = TerrainData.Position;
|
|
|
|
video::S3DVertex2TCoords* meshVertices = (video::S3DVertex2TCoords*)Mesh.getMeshBuffer( 0 )->getVertices();
|
|
|
|
s32 vtxCount = Mesh.getMeshBuffer( 0 )->getVertexCount();
|
|
|
|
core::matrix4 rotMatrix;
|
|
|
|
rotMatrix.setRotationDegrees( TerrainData.Rotation );
|
|
|
|
|
|
|
|
for( s32 i = 0; i < vtxCount; ++i )
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getVertexBuffer()[i].Pos = meshVertices[i].Pos * TerrainData.Scale + TerrainData.Position;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getVertexBuffer()[i].Pos -= TerrainData.RotationPivot;
|
|
|
|
rotMatrix.inverseRotateVect( RenderBuffer->getVertexBuffer()[i].Pos );
|
|
|
|
RenderBuffer->getVertexBuffer()[i].Pos += TerrainData.RotationPivot;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
calculateDistanceThresholds( true );
|
2008-06-13 07:01:17 -07:00
|
|
|
calculatePatchData();
|
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
RenderBuffer->setDirty(EBT_VERTEX);
|
2008-02-27 06:07:27 -08:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Updates the scene nodes indices if the camera has moved or rotated by a certain
|
|
|
|
//! threshold, which can be changed using the SetCameraMovementDeltaThreshold and
|
2008-11-20 15:50:43 -08:00
|
|
|
//! SetCameraRotationDeltaThreshold functions. This also determines if a given patch
|
2007-05-20 11:03:49 -07:00
|
|
|
//! for the scene node is within the view frustum and if it's not the indices are not
|
|
|
|
//! generated for that patch.
|
|
|
|
void CTerrainSceneNode::OnRegisterSceneNode()
|
|
|
|
{
|
|
|
|
if (!IsVisible || !SceneManager->getActiveCamera())
|
|
|
|
return;
|
|
|
|
|
|
|
|
preRenderLODCalculations();
|
|
|
|
preRenderIndicesCalculations();
|
|
|
|
ISceneNode::OnRegisterSceneNode();
|
|
|
|
ForceRecalculation = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerrainSceneNode::preRenderLODCalculations()
|
|
|
|
{
|
|
|
|
SceneManager->registerNodeForRendering( this );
|
|
|
|
// Do Not call ISceneNode::OnRegisterSceneNode ( ), this node should have no children
|
|
|
|
|
|
|
|
// Determine the camera rotation, based on the camera direction.
|
2008-01-13 16:23:02 -08:00
|
|
|
const core::vector3df cameraPosition = SceneManager->getActiveCamera()->getAbsolutePosition();
|
|
|
|
const core::vector3df cameraRotation = core::line3d<f32>(cameraPosition, SceneManager->getActiveCamera()->getTarget()).getVector().getHorizontalAngle();
|
2008-10-16 22:11:14 -07:00
|
|
|
const f32 CameraFOV = SceneManager->getActiveCamera()->getFOV();
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// Only check on the Camera's Y Rotation
|
|
|
|
if (!ForceRecalculation)
|
|
|
|
{
|
|
|
|
if (( fabs(cameraRotation.X - OldCameraRotation.X) < CameraRotationDelta) &&
|
|
|
|
( fabs(cameraRotation.Y - OldCameraRotation.Y) < CameraRotationDelta))
|
|
|
|
{
|
|
|
|
if ((fabs(cameraPosition.X - OldCameraPosition.X) < CameraMovementDelta) &&
|
|
|
|
(fabs(cameraPosition.Y - OldCameraPosition.Y) < CameraMovementDelta) &&
|
|
|
|
(fabs(cameraPosition.Z - OldCameraPosition.Z) < CameraMovementDelta))
|
|
|
|
{
|
2008-10-16 22:11:14 -07:00
|
|
|
if (fabs(CameraFOV-OldCameraFOV) < CameraFOVDelta)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OldCameraPosition = cameraPosition;
|
|
|
|
OldCameraRotation = cameraRotation;
|
2008-10-16 22:11:14 -07:00
|
|
|
OldCameraFOV = CameraFOV;
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
const SViewFrustum* frustum = SceneManager->getActiveCamera()->getViewFrustum();
|
|
|
|
|
|
|
|
// Determine each patches LOD based on distance from camera ( and whether or not they are in
|
|
|
|
// the view frustum ).
|
2008-08-06 07:45:22 -07:00
|
|
|
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
|
|
|
|
for( s32 j = 0; j < count; ++j )
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
if( frustum->getBoundingBox().intersectsWithBox( TerrainData.Patches[j].BoundingBox ) )
|
|
|
|
{
|
2008-01-13 16:23:02 -08:00
|
|
|
const f32 distance = (cameraPosition.X - TerrainData.Patches[j].Center.X) * (cameraPosition.X - TerrainData.Patches[j].Center.X) +
|
2007-05-20 11:03:49 -07:00
|
|
|
(cameraPosition.Y - TerrainData.Patches[j].Center.Y) * (cameraPosition.Y - TerrainData.Patches[j].Center.Y) +
|
|
|
|
(cameraPosition.Z - TerrainData.Patches[j].Center.Z) * (cameraPosition.Z - TerrainData.Patches[j].Center.Z);
|
|
|
|
|
|
|
|
for( s32 i = TerrainData.MaxLOD - 1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
if( distance >= TerrainData.LODDistanceThreshold[i] )
|
|
|
|
{
|
|
|
|
TerrainData.Patches[j].CurrentLOD = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//else if( i == 0 )
|
|
|
|
{
|
|
|
|
// If we've turned off a patch from viewing, because of the frustum, and now we turn around and it's
|
2008-11-20 15:50:43 -08:00
|
|
|
// too close, we need to turn it back on, at the highest LOD. The if above doesn't catch this.
|
2007-05-20 11:03:49 -07:00
|
|
|
TerrainData.Patches[j].CurrentLOD = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TerrainData.Patches[j].CurrentLOD = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
void CTerrainSceneNode::preRenderIndicesCalculations()
|
2008-09-18 02:07:07 -07:00
|
|
|
{
|
|
|
|
switch (RenderBuffer->getIndexBuffer().getType())
|
|
|
|
{
|
|
|
|
case video::EIT_16BIT:
|
|
|
|
{
|
|
|
|
preRenderIndicesCalculationsDirect<u16>((u16*)RenderBuffer->getIndexBuffer().pointer());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case video::EIT_32BIT:
|
|
|
|
{
|
|
|
|
preRenderIndicesCalculationsDirect<u32>((u32*)RenderBuffer->getIndexBuffer().pointer());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class INDEX_TYPE>
|
|
|
|
void CTerrainSceneNode::preRenderIndicesCalculationsDirect(INDEX_TYPE* IndexBuffer)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
IndicesToRender = 0;
|
|
|
|
|
|
|
|
// Then generate the indices for all patches that are visible.
|
|
|
|
for( s32 i = 0; i < TerrainData.PatchCount; ++i )
|
|
|
|
{
|
|
|
|
for( s32 j = 0; j < TerrainData.PatchCount; ++j )
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 index = i * TerrainData.PatchCount + j;
|
2007-05-20 11:03:49 -07:00
|
|
|
if( TerrainData.Patches[index].CurrentLOD >= 0 )
|
|
|
|
{
|
|
|
|
s32 x = 0;
|
|
|
|
s32 z = 0;
|
|
|
|
|
|
|
|
// calculate the step we take this patch, based on the patches current LOD
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 step = 1 << TerrainData.Patches[index].CurrentLOD;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// Loop through patch and generate indices
|
|
|
|
while( z < TerrainData.CalcPatchSize )
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 index11 = getIndex( j, i, index, x, z );
|
|
|
|
const s32 index21 = getIndex( j, i, index, x + step, z );
|
|
|
|
const s32 index12 = getIndex( j, i, index, x, z + step );
|
|
|
|
const s32 index22 = getIndex( j, i, index, x + step, z + step );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-11-21 08:41:30 -08:00
|
|
|
IndexBuffer[IndicesToRender++]= static_cast<INDEX_TYPE>(index12);
|
|
|
|
IndexBuffer[IndicesToRender++]= static_cast<INDEX_TYPE>(index11);
|
|
|
|
IndexBuffer[IndicesToRender++]= static_cast<INDEX_TYPE>(index22);
|
|
|
|
IndexBuffer[IndicesToRender++]= static_cast<INDEX_TYPE>(index22);
|
|
|
|
IndexBuffer[IndicesToRender++]= static_cast<INDEX_TYPE>(index11);
|
|
|
|
IndexBuffer[IndicesToRender++]= static_cast<INDEX_TYPE>(index21);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// increment index position horizontally
|
|
|
|
x += step;
|
|
|
|
|
|
|
|
if ( x >= TerrainData.CalcPatchSize ) // we've hit an edge
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
z += step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-13 07:01:17 -07:00
|
|
|
}
|
2008-02-27 06:07:27 -08:00
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
RenderBuffer->setDirty(EBT_INDEX);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
if ( DynamicSelectorUpdate && TriangleSelector )
|
|
|
|
{
|
|
|
|
CTerrainTriangleSelector* selector = (CTerrainTriangleSelector*)TriangleSelector;
|
|
|
|
selector->setTriangleData ( this, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 02:07:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Render the scene node
|
|
|
|
void CTerrainSceneNode::render()
|
|
|
|
{
|
|
|
|
if (!IsVisible || !SceneManager->getActiveCamera())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Mesh.getMeshBufferCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
video::IVideoDriver* driver = SceneManager->getVideoDriver();
|
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
driver->setTransform (video::ETS_WORLD, core::IdentityMatrix);
|
2008-06-13 07:01:17 -07:00
|
|
|
driver->setMaterial(Mesh.getMeshBuffer(0)->getMaterial());
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getIndexBuffer().set_used(IndicesToRender);
|
2008-06-13 07:01:17 -07:00
|
|
|
|
|
|
|
// For use with geomorphing
|
2008-08-06 07:45:22 -07:00
|
|
|
driver->drawMeshBuffer(RenderBuffer);
|
2008-06-13 07:01:17 -07:00
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getIndexBuffer().set_used( RenderBuffer->getIndexBuffer().allocated_size() );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// for debug purposes only:
|
|
|
|
if (DebugDataVisible)
|
|
|
|
{
|
|
|
|
video::SMaterial m;
|
|
|
|
m.Lighting = false;
|
|
|
|
driver->setMaterial(m);
|
|
|
|
if ( DebugDataVisible & scene::EDS_BBOX )
|
2008-11-20 15:50:43 -08:00
|
|
|
driver->draw3DBox( TerrainData.BoundingBox, video::SColor(255,255,255,255));
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
|
|
|
|
s32 visible = 0;
|
|
|
|
if ( DebugDataVisible & scene::EDS_BBOX_BUFFERS )
|
2008-11-20 15:50:43 -08:00
|
|
|
{
|
2007-05-20 11:03:49 -07:00
|
|
|
for( s32 j = 0; j < count; ++j )
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
driver->draw3DBox( TerrainData.Patches[j].BoundingBox, video::SColor(255,255,0,0));
|
2007-05-20 11:03:49 -07:00
|
|
|
visible += ( TerrainData.Patches[j].CurrentLOD >= 0 );
|
|
|
|
}
|
2008-11-20 15:50:43 -08:00
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
static u32 lastTime = 0;
|
|
|
|
|
|
|
|
const u32 now = os::Timer::getRealTime();
|
|
|
|
if ( now - lastTime > 1000 )
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
sprintf ( buf, "Count: %d, Visible: %d", count, visible );
|
|
|
|
os::Printer::print ( buf );
|
|
|
|
|
|
|
|
lastTime = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Return the bounding box of the entire terrain.
|
|
|
|
const core::aabbox3d<f32>& CTerrainSceneNode::getBoundingBox() const
|
|
|
|
{
|
|
|
|
return TerrainData.BoundingBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Return the bounding box of a patch
|
|
|
|
const core::aabbox3d<f32>& CTerrainSceneNode::getBoundingBox( s32 patchX, s32 patchZ ) const
|
|
|
|
{
|
|
|
|
return TerrainData.Patches[patchX * TerrainData.PatchCount + patchZ].BoundingBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Gets the meshbuffer data based on a specified Level of Detail.
|
|
|
|
//! \param mb: A reference to an SMeshBuffer object
|
|
|
|
//! \param LOD: The Level Of Detail you want the indices from.
|
2008-08-27 21:00:22 -07:00
|
|
|
void CTerrainSceneNode::getMeshBufferForLOD(IDynamicMeshBuffer& mb, s32 LOD ) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
if (!Mesh.getMeshBufferCount())
|
|
|
|
return;
|
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
LOD = core::clamp(LOD, 0, TerrainData.MaxLOD - 1);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
const u32 numVertices = Mesh.getMeshBuffer(0)->getVertexCount();
|
|
|
|
mb.getVertexBuffer().reallocate(numVertices);
|
|
|
|
video::S3DVertex2TCoords* vertices = (video::S3DVertex2TCoords*)Mesh.getMeshBuffer(0)->getVertices();
|
2008-08-27 21:00:22 -07:00
|
|
|
|
|
|
|
for (u32 n=0; n<numVertices; ++n)
|
|
|
|
mb.getVertexBuffer().push_back(vertices[n]);
|
|
|
|
|
|
|
|
mb.getIndexBuffer().setType( RenderBuffer->getIndexBuffer().getType() );
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
// calculate the step we take for all patches, since LOD is the same
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 step = 1 << LOD;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// Generate the indices for all patches at the specified LOD
|
2008-11-20 16:02:58 -08:00
|
|
|
s32 index = 0;
|
2008-08-27 21:00:22 -07:00
|
|
|
for (s32 i=0; i<TerrainData.PatchCount; ++i)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
for (s32 j=0; j<TerrainData.PatchCount; ++j)
|
|
|
|
{
|
|
|
|
s32 x = 0;
|
|
|
|
s32 z = 0;
|
|
|
|
|
|
|
|
// Loop through patch and generate indices
|
|
|
|
while (z < TerrainData.CalcPatchSize)
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 index11 = getIndex( j, i, index, x, z );
|
|
|
|
const s32 index21 = getIndex( j, i, index, x + step, z );
|
|
|
|
const s32 index12 = getIndex( j, i, index, x, z + step );
|
|
|
|
const s32 index22 = getIndex( j, i, index, x + step, z + step );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
mb.getIndexBuffer().push_back( index12 );
|
|
|
|
mb.getIndexBuffer().push_back( index11 );
|
|
|
|
mb.getIndexBuffer().push_back( index22 );
|
|
|
|
mb.getIndexBuffer().push_back( index22 );
|
|
|
|
mb.getIndexBuffer().push_back( index11 );
|
|
|
|
mb.getIndexBuffer().push_back( index21 );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// increment index position horizontally
|
|
|
|
x += step;
|
|
|
|
|
|
|
|
if (x >= TerrainData.CalcPatchSize) // we've hit an edge
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
z += step;
|
|
|
|
}
|
|
|
|
}
|
2008-11-20 16:02:58 -08:00
|
|
|
++index;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Gets the indices for a specified patch at a specified Level of Detail.
|
|
|
|
//! \param mb: A reference to an array of u32 indices.
|
|
|
|
//! \param patchX: Patch x coordinate.
|
|
|
|
//! \param patchZ: Patch z coordinate.
|
2008-11-20 15:50:43 -08:00
|
|
|
//! \param LOD: The level of detail to get for that patch. If -1, then get
|
|
|
|
//! the CurrentLOD. If the CurrentLOD is set to -1, meaning it's not shown,
|
2007-05-20 11:03:49 -07:00
|
|
|
//! then it will retrieve the triangles at the highest LOD ( 0 ).
|
|
|
|
//! \return: Number if indices put into the buffer.
|
2007-09-17 09:09:50 -07:00
|
|
|
s32 CTerrainSceneNode::getIndicesForPatch(core::array<u32>& indices, s32 patchX, s32 patchZ, s32 LOD)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
if ( patchX < 0 || patchX > TerrainData.PatchCount - 1 || patchZ < 0 || patchZ > TerrainData.PatchCount - 1 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ( LOD < -1 || LOD > TerrainData.MaxLOD - 1 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
core::array<s32> cLODs;
|
|
|
|
bool setLODs = false;
|
|
|
|
|
|
|
|
// If LOD of -1 was passed in, use the CurrentLOD of the patch specified
|
|
|
|
if ( LOD == -1 )
|
|
|
|
{
|
|
|
|
LOD = TerrainData.Patches[patchX * TerrainData.PatchCount + patchZ].CurrentLOD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
getCurrentLODOfPatches(cLODs);
|
|
|
|
setCurrentLODOfPatches(LOD);
|
|
|
|
setLODs = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( LOD < 0 )
|
|
|
|
return -2; // Patch not visible, don't generate indices.
|
|
|
|
|
|
|
|
// calculate the step we take for this LOD
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 step = 1 << LOD;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// Generate the indices for the specified patch at the specified LOD
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 index = patchX * TerrainData.PatchCount + patchZ;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
s32 x = 0;
|
|
|
|
s32 z = 0;
|
|
|
|
|
|
|
|
indices.set_used ( TerrainData.PatchSize * TerrainData.PatchSize * 6 );
|
|
|
|
|
|
|
|
// Loop through patch and generate indices
|
2008-11-20 15:50:43 -08:00
|
|
|
s32 rv=0;
|
2007-05-20 11:03:49 -07:00
|
|
|
while (z<TerrainData.CalcPatchSize)
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 index11 = getIndex( patchZ, patchX, index, x, z );
|
|
|
|
const s32 index21 = getIndex( patchZ, patchX, index, x + step, z );
|
|
|
|
const s32 index12 = getIndex( patchZ, patchX, index, x, z + step );
|
|
|
|
const s32 index22 = getIndex( patchZ, patchX, index, x + step, z + step );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
indices[rv++] = index12;
|
|
|
|
indices[rv++] = index11;
|
|
|
|
indices[rv++] = index22;
|
|
|
|
indices[rv++] = index22;
|
|
|
|
indices[rv++] = index11;
|
|
|
|
indices[rv++] = index21;
|
|
|
|
|
|
|
|
// increment index position horizontally
|
|
|
|
x += step;
|
|
|
|
|
|
|
|
if (x >= TerrainData.CalcPatchSize) // we've hit an edge
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
z += step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( setLODs )
|
2008-11-20 16:02:58 -08:00
|
|
|
setCurrentLODOfPatches(cLODs);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Populates an array with the CurrentLOD of each patch.
|
|
|
|
//! \param LODs: A reference to a core::array<s32> to hold the values
|
|
|
|
//! \return Returns the number of elements in the array
|
2007-09-17 09:09:50 -07:00
|
|
|
s32 CTerrainSceneNode::getCurrentLODOfPatches(core::array<s32>& LODs) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
s32 numLODs;
|
|
|
|
LODs.clear ( );
|
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
|
|
|
|
for ( numLODs = 0; numLODs < count; numLODs++ )
|
2007-05-20 11:03:49 -07:00
|
|
|
LODs.push_back ( TerrainData.Patches[numLODs].CurrentLOD );
|
|
|
|
|
|
|
|
return LODs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Manually sets the LOD of a patch
|
|
|
|
//! \param patchX: Patch x coordinate.
|
|
|
|
//! \param patchZ: Patch z coordinate.
|
|
|
|
//! \param LOD: The level of detail to set the patch to.
|
|
|
|
void CTerrainSceneNode::setLODOfPatch( s32 patchX, s32 patchZ, s32 LOD )
|
|
|
|
{
|
|
|
|
TerrainData.Patches[patchX * TerrainData.PatchCount + patchZ].CurrentLOD = LOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Override the default generation of distance thresholds for determining the LOD a patch
|
|
|
|
//! is rendered at.
|
|
|
|
bool CTerrainSceneNode::overrideLODDistance(s32 LOD, f64 newDistance)
|
|
|
|
{
|
|
|
|
OverrideDistanceThreshold = true;
|
|
|
|
|
|
|
|
if ( LOD < 0 || LOD > TerrainData.MaxLOD - 1 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TerrainData.LODDistanceThreshold[LOD] = newDistance * newDistance;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Creates a planar texture mapping on the terrain
|
|
|
|
//! \param resolution: resolution of the planar mapping. This is the value
|
|
|
|
//! specifying the relation between world space and texture coordinate space.
|
|
|
|
void CTerrainSceneNode::scaleTexture(f32 resolution, f32 resolution2)
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
TCoordScale1 = resolution;
|
|
|
|
TCoordScale2 = resolution2;
|
|
|
|
|
|
|
|
const f32 resBySize = resolution / (f32)(TerrainData.Size-1);
|
|
|
|
const f32 res2BySize = resolution2 / (f32)(TerrainData.Size-1);
|
|
|
|
u32 index = 0;
|
|
|
|
f32 xval = 0, zval;
|
|
|
|
f32 x2val = 0, z2val=0;
|
|
|
|
for (s32 x=0; x<TerrainData.Size; ++x)
|
|
|
|
{
|
|
|
|
zval=z2val=0;
|
|
|
|
for (s32 z=0; z<TerrainData.Size; ++z)
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
RenderBuffer->getVertexBuffer()[index].TCoords.X = xval;
|
|
|
|
RenderBuffer->getVertexBuffer()[index].TCoords.Y = zval;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
if (RenderBuffer->getVertexType()==video::EVT_2TCOORDS)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
if ( resolution2 == 0 )
|
|
|
|
{
|
|
|
|
((video::S3DVertex2TCoords&)RenderBuffer->getVertexBuffer()[index]).TCoords2 = RenderBuffer->getVertexBuffer()[index].TCoords;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
((video::S3DVertex2TCoords&)RenderBuffer->getVertexBuffer()[index]).TCoords2.X = x2val;
|
|
|
|
((video::S3DVertex2TCoords&)RenderBuffer->getVertexBuffer()[index]).TCoords2.Y = z2val;
|
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
2008-08-27 21:00:22 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
++index;
|
|
|
|
zval += resBySize;
|
|
|
|
z2val += res2BySize;
|
|
|
|
}
|
|
|
|
xval += resBySize;
|
|
|
|
x2val += res2BySize;
|
2008-06-13 07:01:17 -07:00
|
|
|
}
|
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
RenderBuffer->setDirty(EBT_VERTEX);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//! used to get the indices when generating index data for patches at varying levels of detail.
|
2007-12-23 09:04:40 -08:00
|
|
|
u32 CTerrainSceneNode::getIndex(const s32 PatchX, const s32 PatchZ,
|
|
|
|
const s32 PatchIndex, u32 vX, u32 vZ) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
// top border
|
|
|
|
if (vZ == 0)
|
|
|
|
{
|
|
|
|
if (TerrainData.Patches[PatchIndex].Top &&
|
|
|
|
TerrainData.Patches[PatchIndex].CurrentLOD < TerrainData.Patches[PatchIndex].Top->CurrentLOD &&
|
|
|
|
(vX % ( 1 << TerrainData.Patches[PatchIndex].Top->CurrentLOD)) != 0 )
|
|
|
|
{
|
2007-12-23 09:04:40 -08:00
|
|
|
vX -= vX % ( 1 << TerrainData.Patches[PatchIndex].Top->CurrentLOD );
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( vZ == (u32)TerrainData.CalcPatchSize ) // bottom border
|
|
|
|
{
|
|
|
|
if (TerrainData.Patches[PatchIndex].Bottom &&
|
|
|
|
TerrainData.Patches[PatchIndex].CurrentLOD < TerrainData.Patches[PatchIndex].Bottom->CurrentLOD &&
|
|
|
|
(vX % ( 1 << TerrainData.Patches[PatchIndex].Bottom->CurrentLOD)) != 0)
|
|
|
|
{
|
2007-12-23 09:04:40 -08:00
|
|
|
vX -= vX % ( 1 << TerrainData.Patches[PatchIndex].Bottom->CurrentLOD );
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// left border
|
|
|
|
if ( vX == 0 )
|
|
|
|
{
|
|
|
|
if (TerrainData.Patches[PatchIndex].Left &&
|
|
|
|
TerrainData.Patches[PatchIndex].CurrentLOD < TerrainData.Patches[PatchIndex].Left->CurrentLOD &&
|
|
|
|
( vZ % ( 1 << TerrainData.Patches[PatchIndex].Left->CurrentLOD ) ) != 0)
|
|
|
|
{
|
2007-12-23 09:04:40 -08:00
|
|
|
vZ -= vZ % ( 1 << TerrainData.Patches[PatchIndex].Left->CurrentLOD );
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2008-11-20 15:50:43 -08:00
|
|
|
if ( vX == (u32)TerrainData.CalcPatchSize ) // right border
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
if (TerrainData.Patches[PatchIndex].Right &&
|
|
|
|
TerrainData.Patches[PatchIndex].CurrentLOD < TerrainData.Patches[PatchIndex].Right->CurrentLOD &&
|
2008-11-20 15:50:43 -08:00
|
|
|
( vZ % ( 1 << TerrainData.Patches[PatchIndex].Right->CurrentLOD ) ) != 0)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2007-12-23 09:04:40 -08:00
|
|
|
vZ -= vZ % ( 1 << TerrainData.Patches[PatchIndex].Right->CurrentLOD );
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( vZ >= (u32)TerrainData.PatchSize )
|
|
|
|
vZ = TerrainData.CalcPatchSize;
|
|
|
|
|
|
|
|
if ( vX >= (u32)TerrainData.PatchSize )
|
|
|
|
vX = TerrainData.CalcPatchSize;
|
|
|
|
|
|
|
|
return (vZ + ((TerrainData.CalcPatchSize) * PatchZ)) * TerrainData.Size +
|
|
|
|
(vX + ((TerrainData.CalcPatchSize) * PatchX));
|
|
|
|
}
|
|
|
|
|
2008-01-06 14:56:36 -08:00
|
|
|
//! smooth the terrain
|
2008-08-27 21:00:22 -07:00
|
|
|
void CTerrainSceneNode::smoothTerrain(CDynamicMeshBuffer* mb, s32 smoothFactor)
|
2008-01-06 14:56:36 -08:00
|
|
|
{
|
|
|
|
for (s32 run = 0; run < smoothFactor; ++run)
|
|
|
|
{
|
|
|
|
s32 yd = TerrainData.Size;
|
|
|
|
for (s32 y = 1; y < TerrainData.Size - 1; ++y)
|
|
|
|
{
|
|
|
|
for (s32 x = 1; x < TerrainData.Size - 1; ++x)
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
mb->getVertexBuffer()[x + yd].Pos.Y =
|
|
|
|
(mb->getVertexBuffer()[x-1 + yd].Pos.Y +
|
|
|
|
mb->getVertexBuffer()[x+1 + yd].Pos.Y +
|
|
|
|
mb->getVertexBuffer()[x + yd - TerrainData.Size].Pos.Y +
|
|
|
|
mb->getVertexBuffer()[x + yd - TerrainData.Size].Pos.Y) * 0.25f;
|
2008-01-06 14:56:36 -08:00
|
|
|
}
|
|
|
|
yd += TerrainData.Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! calculate smooth normals
|
2008-11-20 15:50:43 -08:00
|
|
|
void CTerrainSceneNode::calculateNormals( CDynamicMeshBuffer* mb )
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
s32 count;
|
|
|
|
core::vector3df a, b, c, t;
|
|
|
|
|
|
|
|
for (s32 x=0; x<TerrainData.Size; ++x)
|
2007-09-24 15:41:52 -07:00
|
|
|
{
|
2007-05-20 11:03:49 -07:00
|
|
|
for (s32 z=0; z<TerrainData.Size; ++z)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
core::vector3df normal;
|
|
|
|
|
|
|
|
// top left
|
|
|
|
if (x>0 && z>0)
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[(x-1)*TerrainData.Size+z-1].Pos;
|
|
|
|
b = mb->getVertexBuffer()[(x-1)*TerrainData.Size+z].Pos;
|
|
|
|
c = mb->getVertexBuffer()[x*TerrainData.Size+z].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[(x-1)*TerrainData.Size+z-1].Pos;
|
|
|
|
b = mb->getVertexBuffer()[x*TerrainData.Size+z-1].Pos;
|
|
|
|
c = mb->getVertexBuffer()[x*TerrainData.Size+z].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
|
|
|
count += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// top right
|
|
|
|
if (x>0 && z<TerrainData.Size-1)
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[(x-1)*TerrainData.Size+z].Pos;
|
|
|
|
b = mb->getVertexBuffer()[(x-1)*TerrainData.Size+z+1].Pos;
|
|
|
|
c = mb->getVertexBuffer()[x*TerrainData.Size+z+1].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[(x-1)*TerrainData.Size+z].Pos;
|
|
|
|
b = mb->getVertexBuffer()[x*TerrainData.Size+z+1].Pos;
|
|
|
|
c = mb->getVertexBuffer()[x*TerrainData.Size+z].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
|
|
|
count += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bottom right
|
|
|
|
if (x<TerrainData.Size-1 && z<TerrainData.Size-1)
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[x*TerrainData.Size+z+1].Pos;
|
|
|
|
b = mb->getVertexBuffer()[x*TerrainData.Size+z].Pos;
|
|
|
|
c = mb->getVertexBuffer()[(x+1)*TerrainData.Size+z+1].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[x*TerrainData.Size+z+1].Pos;
|
|
|
|
b = mb->getVertexBuffer()[(x+1)*TerrainData.Size+z+1].Pos;
|
|
|
|
c = mb->getVertexBuffer()[(x+1)*TerrainData.Size+z].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
|
|
|
count += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bottom left
|
|
|
|
if (x<TerrainData.Size-1 && z>0)
|
|
|
|
{
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[x*TerrainData.Size+z-1].Pos;
|
|
|
|
b = mb->getVertexBuffer()[x*TerrainData.Size+z].Pos;
|
|
|
|
c = mb->getVertexBuffer()[(x+1)*TerrainData.Size+z].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
a = mb->getVertexBuffer()[x*TerrainData.Size+z-1].Pos;
|
|
|
|
b = mb->getVertexBuffer()[(x+1)*TerrainData.Size+z].Pos;
|
|
|
|
c = mb->getVertexBuffer()[(x+1)*TerrainData.Size+z-1].Pos;
|
2007-05-20 11:03:49 -07:00
|
|
|
b -= a;
|
|
|
|
c -= a;
|
|
|
|
t = b.crossProduct ( c );
|
|
|
|
t.normalize ( );
|
|
|
|
normal += t;
|
|
|
|
|
|
|
|
count += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count != 0 )
|
|
|
|
{
|
|
|
|
normal.normalize ( );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
normal.set( 0.0f, 1.0f, 0.0f );
|
|
|
|
}
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
mb->getVertexBuffer()[x * TerrainData.Size + z].Normal = normal;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
2007-09-24 15:41:52 -07:00
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//! create patches, stuff that needs to be done only once for patches goes here.
|
|
|
|
void CTerrainSceneNode::createPatches()
|
|
|
|
{
|
|
|
|
TerrainData.PatchCount = (TerrainData.Size - 1) / ( TerrainData.CalcPatchSize );
|
|
|
|
|
|
|
|
if (TerrainData.Patches)
|
|
|
|
delete [] TerrainData.Patches;
|
|
|
|
|
|
|
|
TerrainData.Patches = new SPatch[TerrainData.PatchCount * TerrainData.PatchCount];
|
|
|
|
}
|
|
|
|
|
|
|
|
//! used to calculate the internal STerrainData structure both at creation and after scaling/position calls.
|
|
|
|
void CTerrainSceneNode::calculatePatchData()
|
|
|
|
{
|
|
|
|
// Reset the Terrains Bounding Box for re-calculation
|
|
|
|
TerrainData.BoundingBox = core::aabbox3df ( 999999.9f, 999999.9f, 999999.9f, -999999.9f, -999999.9f, -999999.9f );
|
|
|
|
|
|
|
|
for( s32 x = 0; x < TerrainData.PatchCount; ++x )
|
|
|
|
{
|
|
|
|
for( s32 z = 0; z < TerrainData.PatchCount; ++z )
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
const s32 index = x * TerrainData.PatchCount + z;
|
2007-05-20 11:03:49 -07:00
|
|
|
TerrainData.Patches[index].CurrentLOD = 0;
|
|
|
|
|
|
|
|
// For each patch, calculate the bounding box ( mins and maxes )
|
2008-11-20 15:50:43 -08:00
|
|
|
TerrainData.Patches[index].BoundingBox = core::aabbox3df (999999.9f, 999999.9f, 999999.9f,
|
2007-05-20 11:03:49 -07:00
|
|
|
-999999.9f, -999999.9f, -999999.9f );
|
|
|
|
|
|
|
|
for( s32 xx = x*(TerrainData.CalcPatchSize); xx <= ( x + 1 ) * TerrainData.CalcPatchSize; ++xx )
|
|
|
|
for( s32 zz = z*(TerrainData.CalcPatchSize); zz <= ( z + 1 ) * TerrainData.CalcPatchSize; ++zz )
|
2008-08-27 21:00:22 -07:00
|
|
|
TerrainData.Patches[index].BoundingBox.addInternalPoint( RenderBuffer->getVertexBuffer()[xx * TerrainData.Size + zz].Pos );
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// Reconfigure the bounding box of the terrain as a whole
|
|
|
|
TerrainData.BoundingBox.addInternalBox( TerrainData.Patches[index].BoundingBox );
|
|
|
|
|
|
|
|
// get center of Patch
|
|
|
|
TerrainData.Patches[index].Center = TerrainData.Patches[index].BoundingBox.getCenter();
|
|
|
|
|
|
|
|
// Assign Neighbours
|
|
|
|
// Top
|
|
|
|
if( x > 0 )
|
|
|
|
TerrainData.Patches[index].Top = &TerrainData.Patches[(x-1) * TerrainData.PatchCount + z];
|
|
|
|
else
|
|
|
|
TerrainData.Patches[index].Top = 0;
|
|
|
|
|
|
|
|
// Bottom
|
|
|
|
if( x < TerrainData.PatchCount - 1 )
|
|
|
|
TerrainData.Patches[index].Bottom = &TerrainData.Patches[(x+1) * TerrainData.PatchCount + z];
|
|
|
|
else
|
|
|
|
TerrainData.Patches[index].Bottom = 0;
|
|
|
|
|
|
|
|
// Left
|
|
|
|
if( z > 0 )
|
|
|
|
TerrainData.Patches[index].Left = &TerrainData.Patches[x * TerrainData.PatchCount + z - 1];
|
|
|
|
else
|
|
|
|
TerrainData.Patches[index].Left = 0;
|
|
|
|
|
|
|
|
// Right
|
|
|
|
if( z < TerrainData.PatchCount - 1 )
|
|
|
|
TerrainData.Patches[index].Right = &TerrainData.Patches[x * TerrainData.PatchCount + z + 1];
|
|
|
|
else
|
|
|
|
TerrainData.Patches[index].Right = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get center of Terrain
|
|
|
|
TerrainData.Center = TerrainData.BoundingBox.getCenter();
|
|
|
|
|
|
|
|
// if the default rotation pivot is still being used, update it.
|
|
|
|
if( UseDefaultRotationPivot )
|
|
|
|
{
|
|
|
|
TerrainData.RotationPivot = TerrainData.Center;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! used to calculate or recalculate the distance thresholds
|
|
|
|
void CTerrainSceneNode::calculateDistanceThresholds(bool scalechanged)
|
|
|
|
{
|
|
|
|
// Only update the LODDistanceThreshold if it's not manually changed
|
|
|
|
if (!OverrideDistanceThreshold)
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
TerrainData.LODDistanceThreshold.set_used(0);
|
2007-05-20 11:03:49 -07:00
|
|
|
// Determine new distance threshold for determining what LOD to draw patches at
|
2008-11-20 15:50:43 -08:00
|
|
|
TerrainData.LODDistanceThreshold.reallocate(TerrainData.MaxLOD);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
const f64 size = TerrainData.PatchSize * TerrainData.PatchSize *
|
|
|
|
TerrainData.Scale.X * TerrainData.Scale.Z;
|
2007-05-20 11:03:49 -07:00
|
|
|
for (s32 i=0; i<TerrainData.MaxLOD; ++i)
|
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
TerrainData.LODDistanceThreshold.push_back(size * ((i+1+ i / 2) * (i+1+ i / 2)));
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerrainSceneNode::setCurrentLODOfPatches(s32 lod)
|
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
|
|
|
|
for (s32 i=0; i< count; ++i)
|
2007-05-20 11:03:49 -07:00
|
|
|
TerrainData.Patches[i].CurrentLOD = lod;
|
|
|
|
}
|
|
|
|
|
2007-12-23 09:04:40 -08:00
|
|
|
void CTerrainSceneNode::setCurrentLODOfPatches(const core::array<s32>& lodarray)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-08-06 07:45:22 -07:00
|
|
|
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
|
|
|
|
for (s32 i=0; i<count; ++i)
|
2007-05-20 11:03:49 -07:00
|
|
|
TerrainData.Patches[i].CurrentLOD = lodarray[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Gets the height
|
2007-09-17 09:09:50 -07:00
|
|
|
f32 CTerrainSceneNode::getHeight( f32 x, f32 z ) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
if (!Mesh.getMeshBufferCount())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
f32 height = -999999.9f;
|
|
|
|
|
|
|
|
core::matrix4 rotMatrix;
|
|
|
|
rotMatrix.setRotationDegrees( TerrainData.Rotation );
|
|
|
|
core::vector3df pos( x, 0.0f, z );
|
|
|
|
rotMatrix.rotateVect( pos );
|
|
|
|
pos -= TerrainData.Position;
|
|
|
|
pos /= TerrainData.Scale;
|
|
|
|
|
|
|
|
s32 X(core::floor32( pos.X ));
|
|
|
|
s32 Z(core::floor32( pos.Z ));
|
|
|
|
|
2008-01-29 16:07:52 -08:00
|
|
|
if( X >= 0 && X < TerrainData.Size && Z >= 0 && Z < TerrainData.Size )
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-11-20 15:50:43 -08:00
|
|
|
const video::S3DVertex2TCoords* Vertices = (const video::S3DVertex2TCoords*)Mesh.getMeshBuffer(0)->getVertices();
|
2007-05-20 11:03:49 -07:00
|
|
|
const core::vector3df& a = Vertices[ X * TerrainData.Size + Z ].Pos;
|
|
|
|
const core::vector3df& b = Vertices[ (X + 1) * TerrainData.Size + Z ].Pos;
|
|
|
|
const core::vector3df& c = Vertices[ X * TerrainData.Size + ( Z + 1 ) ].Pos;
|
|
|
|
const core::vector3df& d = Vertices[ (X + 1) * TerrainData.Size + ( Z + 1 ) ].Pos;
|
|
|
|
|
|
|
|
// offset from integer position
|
|
|
|
const f32 dx = pos.X - X;
|
|
|
|
const f32 dz = pos.Z - Z;
|
|
|
|
|
|
|
|
if( dx > dz )
|
|
|
|
height = a.Y + (d.Y - b.Y)*dz + (b.Y - a.Y)*dx;
|
|
|
|
else
|
|
|
|
height = a.Y + (d.Y - c.Y)*dx + (c.Y - a.Y)*dz;
|
|
|
|
|
|
|
|
height *= TerrainData.Scale.Y;
|
|
|
|
height += TerrainData.Position.Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Writes attributes of the scene node.
|
2007-09-14 15:25:59 -07:00
|
|
|
void CTerrainSceneNode::serializeAttributes(io::IAttributes* out,
|
|
|
|
io::SAttributeReadWriteOptions* options) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
ISceneNode::serializeAttributes(out, options);
|
|
|
|
|
|
|
|
out->addString("Heightmap", HeightmapFile.c_str());
|
|
|
|
out->addFloat("TextureScale1", TCoordScale1);
|
|
|
|
out->addFloat("TextureScale2", TCoordScale2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Reads attributes of the scene node.
|
|
|
|
void CTerrainSceneNode::deserializeAttributes(io::IAttributes* in,
|
2008-11-20 15:50:43 -08:00
|
|
|
io::SAttributeReadWriteOptions* options)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
core::stringc newHeightmap = in->getAttributeAsString("Heightmap");
|
|
|
|
f32 tcoordScale1 = in->getAttributeAsFloat("TextureScale1");
|
|
|
|
f32 tcoordScale2 = in->getAttributeAsFloat("TextureScale2");
|
|
|
|
|
|
|
|
// set possible new heightmap
|
|
|
|
|
2008-11-20 15:50:43 -08:00
|
|
|
if (newHeightmap.size() != 0 && newHeightmap != HeightmapFile)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
io::IReadFile* file = FileSystem->createAndOpenFile(newHeightmap.c_str());
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
loadHeightMap(file, video::SColor(255,255,255,255), 0);
|
|
|
|
file->drop();
|
2008-02-27 06:07:27 -08:00
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
else
|
|
|
|
os::Printer::log("could not open heightmap", newHeightmap.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// set possible new scale
|
|
|
|
|
2007-09-12 10:45:29 -07:00
|
|
|
if (core::equals(tcoordScale1, 0.f))
|
2007-05-20 11:03:49 -07:00
|
|
|
tcoordScale1 = 1.0f;
|
|
|
|
|
2007-09-12 10:45:29 -07:00
|
|
|
if (core::equals(tcoordScale2, 0.f))
|
2007-05-20 11:03:49 -07:00
|
|
|
tcoordScale2 = 1.0f;
|
|
|
|
|
|
|
|
if (!core::equals(tcoordScale1, TCoordScale1) ||
|
|
|
|
!core::equals(tcoordScale2, TCoordScale2))
|
|
|
|
{
|
|
|
|
scaleTexture(tcoordScale1, tcoordScale2);
|
|
|
|
}
|
|
|
|
|
|
|
|
ISceneNode::deserializeAttributes(in, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a clone of this scene node and its children.
|
|
|
|
ISceneNode* CTerrainSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
|
|
|
|
{
|
2007-09-24 15:41:52 -07:00
|
|
|
if (!newParent)
|
|
|
|
newParent = Parent;
|
|
|
|
if (!newManager)
|
|
|
|
newManager = SceneManager;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
CTerrainSceneNode* nb = new CTerrainSceneNode(
|
2008-01-06 14:56:36 -08:00
|
|
|
newParent, newManager, FileSystem, ID,
|
2007-09-24 15:41:52 -07:00
|
|
|
4, ETPS_17, getPosition(), getRotation(), getScale());
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
nb->cloneMembers(this, newManager);
|
2008-02-27 06:07:27 -08:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
// instead of cloning the data structures, recreate the terrain.
|
|
|
|
// (temporary solution)
|
|
|
|
|
|
|
|
// load file
|
|
|
|
|
|
|
|
io::IReadFile* file = FileSystem->createAndOpenFile(HeightmapFile.c_str());
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
nb->loadHeightMap(file, video::SColor(255,255,255,255), 0);
|
|
|
|
file->drop();
|
2008-02-27 06:07:27 -08:00
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// scale textures
|
|
|
|
|
|
|
|
nb->scaleTexture(TCoordScale1, TCoordScale2);
|
|
|
|
|
|
|
|
// copy materials
|
|
|
|
|
|
|
|
for (unsigned int m = 0; m<Mesh.getMeshBufferCount(); ++m)
|
|
|
|
{
|
|
|
|
if (nb->Mesh.getMeshBufferCount()>m &&
|
|
|
|
nb->Mesh.getMeshBuffer(m) &&
|
|
|
|
Mesh.getMeshBuffer(m))
|
|
|
|
{
|
2008-01-06 14:56:36 -08:00
|
|
|
nb->Mesh.getMeshBuffer(m)->getMaterial() =
|
2007-05-20 11:03:49 -07:00
|
|
|
Mesh.getMeshBuffer(m)->getMaterial();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-06 07:45:22 -07:00
|
|
|
nb->RenderBuffer->Material = RenderBuffer->Material;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// finish
|
|
|
|
|
|
|
|
nb->drop();
|
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|
|
|
|
|
2008-08-27 21:00:22 -07:00
|
|
|
|