irrlicht/source/Irrlicht/CTerrainSceneNode.cpp

1504 lines
43 KiB
C++
Raw Normal View History

// Copyright (C) 2002-2009 Nikolaus Gebhardt
// 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
// distributed under this licence. I only modified some parts. A lot of thanks
// go to him.
#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"
#include "ITextSceneNode.h"
#include "IAnimatedMesh.h"
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,
const core::vector3df& rotation,
const core::vector3df& scale)
: ITerrainSceneNode(parent, mgr, id, position, rotation, scale),
TerrainData(patchSize, maxLOD, position, rotation, scale), RenderBuffer(0),
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)),
OldCameraUp(core::vector3df(-99999.9f, -99999.9f, -99999.9f)),
CameraMovementDelta(10.0f), CameraRotationDelta(1.0f),CameraFOVDelta(0.1f),
TCoordScale1(1.0f), TCoordScale2(1.0f), FileSystem(fs)
{
#ifdef _DEBUG
setDebugName("CTerrainSceneNode");
#endif
RenderBuffer = new CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
RenderBuffer->setHardwareMappingHint(scene::EHM_STATIC, scene::EBT_VERTEX);
RenderBuffer->setHardwareMappingHint(scene::EHM_DYNAMIC, scene::EBT_INDEX);
if (FileSystem)
FileSystem->grab();
setAutomaticCulling(scene::EAC_OFF);
}
//! destructor
CTerrainSceneNode::~CTerrainSceneNode()
{
delete [] TerrainData.Patches;
if (FileSystem)
FileSystem->drop();
if (RenderBuffer)
RenderBuffer->drop();
}
//! Initializes the terrain data. Loads the vertices from the heightMapFile
bool CTerrainSceneNode::loadHeightMap(io::IReadFile* file, video::SColor vertexColor,
s32 smoothFactor)
{
if (!file)
return false;
Mesh.MeshBuffers.clear();
const u32 startTime = os::Timer::getRealTime();
video::IImage* heightMap = SceneManager->getVideoDriver()->createImageFromFile(file);
if (!heightMap)
{
os::Printer::log("Unable to load heightmap.");
return false;
}
HeightmapFile = file->getFileName();
// Get the dimension of the heightmap data
TerrainData.Size = heightMap->getDimension().Width;
switch (TerrainData.PatchSize)
{
case ETPS_9:
if (TerrainData.MaxLOD > 3)
{
TerrainData.MaxLOD = 3;
}
break;
case ETPS_17:
if (TerrainData.MaxLOD > 4)
{
TerrainData.MaxLOD = 4;
}
break;
case ETPS_33:
if (TerrainData.MaxLOD > 5)
{
TerrainData.MaxLOD = 5;
}
break;
case ETPS_65:
if (TerrainData.MaxLOD > 6)
{
TerrainData.MaxLOD = 6;
}
break;
case ETPS_129:
if (TerrainData.MaxLOD > 7)
{
TerrainData.MaxLOD = 7;
}
break;
}
// --- Generate vertex data from heightmap ----
// resize the vertex array for the mesh buffer one time (makes loading faster)
//SMeshBufferLightMap* mb = new SMeshBufferLightMap();
scene::CDynamicMeshBuffer *mb=0;
const u32 numVertices = TerrainData.Size * TerrainData.Size;
if (numVertices <= 65536)
{
//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(numVertices);
// 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;
float fx=0.f;
float fx2=0.f;
for (s32 x = 0; x < TerrainData.Size; ++x)
{
float fz=0.f;
float fz2=0.f;
for (s32 z = 0; z < TerrainData.Size; ++z)
{
video::S3DVertex2TCoords& vertex= static_cast<video::S3DVertex2TCoords*>(mb->getVertexBuffer().pointer())[index++];
vertex.Normal.set(0.0f, 1.0f, 0.0f);
vertex.Color = vertexColor;
vertex.Pos.X = fx;
vertex.Pos.Y = (f32) heightMap->getPixel(TerrainData.Size-x-1,z).getLuminance();
vertex.Pos.Z = fz;
vertex.TCoords.X = vertex.TCoords2.X = 1.f-fx2;
vertex.TCoords.Y = vertex.TCoords2.Y = fz2;
++fz;
fz2 += tdSize;
}
++fx;
fx2 += tdSize;
}
// drop heightMap, no longer needed
heightMap->drop();
smoothTerrain(mb, smoothFactor);
// calculate smooth normals for the vertices
calculateNormals(mb);
// add the MeshBuffer to the mesh
Mesh.addMeshBuffer(mb);
// We copy the data to the renderBuffer, after the normals have been calculated.
RenderBuffer->getVertexBuffer().set_used(numVertices);
for (u32 i = 0; i < numVertices; ++i)
{
RenderBuffer->getVertexBuffer()[i] = mb->getVertexBuffer()[i];
RenderBuffer->getVertexBuffer()[i].Pos *= TerrainData.Scale;
RenderBuffer->getVertexBuffer()[i].Pos += TerrainData.Position;
}
// We no longer need the mb
mb->drop();
// 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;
// Rotate the vertices of the terrain by the rotation
// specified. Must be done after calculating the terrain data,
// so we know what the current center of the terrain is.
setRotation(TerrainData.Rotation);
// Pre-allocate memory for indices
RenderBuffer->getIndexBuffer().set_used(
TerrainData.PatchCount * TerrainData.PatchCount *
TerrainData.CalcPatchSize * TerrainData.CalcPatchSize * 6);
RenderBuffer->setDirty();
const u32 endTime = os::Timer::getRealTime();
c8 tmp[255];
snprintf(tmp, 255, "Generated terrain data (%dx%d) in %.4f seconds",
TerrainData.Size, TerrainData.Size, (endTime - startTime) / 1000.0f );
os::Printer::log(tmp);
return true;
}
//! Initializes the terrain data. Loads the vertices from the heightMapFile
bool CTerrainSceneNode::loadHeightMapRAW(io::IReadFile* file,
s32 bitsPerPixel, bool signedData, bool floatVals,
s32 width, video::SColor vertexColor, s32 smoothFactor)
{
if (!file)
return false;
if (floatVals && bitsPerPixel != 32)
return false;
// start reading
const u32 startTime = os::Timer::getTime();
Mesh.MeshBuffers.clear();
const s32 bytesPerPixel = bitsPerPixel / 8;
// Get the dimension of the heightmap data
const s32 filesize = file->getSize();
if (!width)
TerrainData.Size = core::floor32(sqrtf((f32)(filesize / bytesPerPixel)));
else
{
if ((filesize-file->getPos())/bytesPerPixel>width*width)
{
os::Printer::log("Error reading heightmap RAW file", "File is too small.");
return false;
}
TerrainData.Size = width;
}
switch (TerrainData.PatchSize)
{
case ETPS_9:
if (TerrainData.MaxLOD > 3)
{
TerrainData.MaxLOD = 3;
}
break;
case ETPS_17:
if (TerrainData.MaxLOD > 4)
{
TerrainData.MaxLOD = 4;
}
break;
case ETPS_33:
if (TerrainData.MaxLOD > 5)
{
TerrainData.MaxLOD = 5;
}
break;
case ETPS_65:
if (TerrainData.MaxLOD > 6)
{
TerrainData.MaxLOD = 6;
}
break;
case ETPS_129:
if (TerrainData.MaxLOD > 7)
{
TerrainData.MaxLOD = 7;
}
break;
}
// --- Generate vertex data from heightmap ----
// resize the vertex array for the mesh buffer one time (makes loading faster)
scene::CDynamicMeshBuffer *mb=0;
const u32 numVertices = TerrainData.Size * TerrainData.Size;
if (numVertices <= 65536)
{
//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().reallocate(numVertices);
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);
float fx=0.f;
float fx2=0.f;
for (s32 x = 0; x < TerrainData.Size; ++x)
{
float fz=0.f;
float fz2=0.f;
for (s32 z = 0; z < TerrainData.Size; ++z)
{
bool failure=false;
vertex.Pos.X = fx;
if (floatVals)
{
if (file->read(&vertex.Pos.Y, bytesPerPixel) != bytesPerPixel)
failure=true;
}
else if (signedData)
{
switch (bytesPerPixel)
{
case 1:
{
s8 val;
if (file->read(&val, bytesPerPixel) != bytesPerPixel)
failure=true;
vertex.Pos.Y=val;
}
break;
case 2:
{
s16 val;
if (file->read(&val, bytesPerPixel) != bytesPerPixel)
failure=true;
vertex.Pos.Y=val/256.f;
}
break;
case 4:
{
s32 val;
if (file->read(&val, bytesPerPixel) != bytesPerPixel)
failure=true;
vertex.Pos.Y=val/16777216.f;
}
break;
}
}
else
{
switch (bytesPerPixel)
{
case 1:
{
u8 val;
if (file->read(&val, bytesPerPixel) != bytesPerPixel)
failure=true;
vertex.Pos.Y=val;
}
break;
case 2:
{
u16 val;
if (file->read(&val, bytesPerPixel) != bytesPerPixel)
failure=true;
vertex.Pos.Y=val/256.f;
}
break;
case 4:
{
u32 val;
if (file->read(&val, bytesPerPixel) != bytesPerPixel)
failure=true;
vertex.Pos.Y=val/16777216.f;
}
break;
}
}
if (failure)
{
os::Printer::log("Error reading heightmap RAW file.");
mb->drop();
return false;
}
vertex.Pos.Z = fz;
vertex.TCoords.X = vertex.TCoords2.X = 1.f-fx2;
vertex.TCoords.Y = vertex.TCoords2.Y = fz2;
mb->getVertexBuffer().push_back(vertex);
++fz;
fz2 += tdSize;
}
++fx;
fx2 += tdSize;
}
smoothTerrain(mb, smoothFactor);
// calculate smooth normals for the vertices
calculateNormals(mb);
// add the MeshBuffer to the mesh
Mesh.addMeshBuffer(mb);
const u32 vertexCount = mb->getVertexCount();
// We copy the data to the renderBuffer, after the normals have been calculated.
RenderBuffer->getVertexBuffer().set_used(vertexCount);
for (u32 i = 0; i < vertexCount; i++)
{
RenderBuffer->getVertexBuffer()[i] = mb->getVertexBuffer()[i];
RenderBuffer->getVertexBuffer()[i].Pos *= TerrainData.Scale;
RenderBuffer->getVertexBuffer()[i].Pos += TerrainData.Position;
}
// We no longer need the mb
mb->drop();
// 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;
// Rotate the vertices of the terrain by the rotation specified. Must be done
// after calculating the terrain data, so we know what the current center of the
// terrain is.
setRotation(TerrainData.Rotation);
// Pre-allocate memory for indices
RenderBuffer->getIndexBuffer().set_used(
TerrainData.PatchCount*TerrainData.PatchCount*
TerrainData.CalcPatchSize*TerrainData.CalcPatchSize*6);
const u32 endTime = os::Timer::getTime();
c8 tmp[255];
snprintf(tmp, 255, "Generated terrain data (%dx%d) in %.4f seconds",
TerrainData.Size, TerrainData.Size, (endTime - startTime) / 1000.0f);
os::Printer::log(tmp);
return true;
}
//! 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;
}
//! Sets the pivot point for rotation of this node. This is useful for the TiledTerrainManager to
//! 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;
s32 vtxCount = Mesh.getMeshBuffer(0)->getVertexCount();
core::matrix4 rotMatrix;
rotMatrix.setRotationDegrees(TerrainData.Rotation);
for (s32 i = 0; i < vtxCount; ++i)
{
RenderBuffer->getVertexBuffer()[i].Pos = Mesh.getMeshBuffer(0)->getPosition(i) * TerrainData.Scale + TerrainData.Position;
RenderBuffer->getVertexBuffer()[i].Pos -= TerrainData.RotationPivot;
rotMatrix.inverseRotateVect(RenderBuffer->getVertexBuffer()[i].Pos);
RenderBuffer->getVertexBuffer()[i].Pos += TerrainData.RotationPivot;
}
calculateDistanceThresholds(true);
calculatePatchData();
RenderBuffer->setDirty(EBT_VERTEX);
}
//! Updates the scene nodes indices if the camera has moved or rotated by a certain
//! threshold, which can be changed using the SetCameraMovementDeltaThreshold and
//! SetCameraRotationDeltaThreshold functions. This also determines if a given patch
//! 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()
{
scene::ICameraSceneNode * camera = SceneManager->getActiveCamera();
if(!camera)
return;
SceneManager->registerNodeForRendering(this);
// Do Not call ISceneNode::OnRegisterSceneNode(), this node should have no children
// Determine the camera rotation, based on the camera direction.
const core::vector3df cameraPosition = camera->getAbsolutePosition();
const core::vector3df cameraRotation = core::line3d<f32>(cameraPosition, camera->getTarget()).getVector().getHorizontalAngle();
core::vector3df cameraUp = camera->getUpVector();
cameraUp.normalize();
const f32 CameraFOV = SceneManager->getActiveCamera()->getFOV();
// Only check on the Camera's Y Rotation
if (!ForceRecalculation)
{
if ((fabsf(cameraRotation.X - OldCameraRotation.X) < CameraRotationDelta) &&
(fabsf(cameraRotation.Y - OldCameraRotation.Y) < CameraRotationDelta))
{
if ((fabs(cameraPosition.X - OldCameraPosition.X) < CameraMovementDelta) &&
(fabs(cameraPosition.Y - OldCameraPosition.Y) < CameraMovementDelta) &&
(fabs(cameraPosition.Z - OldCameraPosition.Z) < CameraMovementDelta))
{
if (fabs(CameraFOV-OldCameraFOV) < CameraFOVDelta &&
cameraUp.dotProduct(OldCameraUp) > (1.f - (cos(core::DEGTORAD * CameraRotationDelta))))
{
return;
}
}
}
}
OldCameraPosition = cameraPosition;
OldCameraRotation = cameraRotation;
OldCameraUp = cameraUp;
OldCameraFOV = CameraFOV;
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).
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
for (s32 j = 0; j < count; ++j)
{
if (frustum->getBoundingBox().intersectsWithBox(TerrainData.Patches[j].BoundingBox))
{
const f32 distance = (cameraPosition.X - TerrainData.Patches[j].Center.X) * (cameraPosition.X - TerrainData.Patches[j].Center.X) +
(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
// too close, we need to turn it back on, at the highest LOD. The if above doesn't catch this.
TerrainData.Patches[j].CurrentLOD = 0;
}
}
}
else
{
TerrainData.Patches[j].CurrentLOD = -1;
}
}
}
void CTerrainSceneNode::preRenderIndicesCalculations()
{
scene::IIndexBuffer& indexBuffer = RenderBuffer->getIndexBuffer();
IndicesToRender = 0;
indexBuffer.set_used(0);
s32 index = 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)
{
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
const s32 step = 1 << TerrainData.Patches[index].CurrentLOD;
// Loop through patch and generate indices
while (z < TerrainData.CalcPatchSize)
{
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);
indexBuffer.push_back(index12);
indexBuffer.push_back(index11);
indexBuffer.push_back(index22);
indexBuffer.push_back(index22);
indexBuffer.push_back(index11);
indexBuffer.push_back(index21);
IndicesToRender+=6;
// increment index position horizontally
x += step;
// we've hit an edge
if (x >= TerrainData.CalcPatchSize)
{
x = 0;
z += step;
}
}
}
++index;
}
}
RenderBuffer->setDirty(EBT_INDEX);
if (DynamicSelectorUpdate && TriangleSelector)
{
CTerrainTriangleSelector* selector = (CTerrainTriangleSelector*)TriangleSelector;
selector->setTriangleData(this, -1);
}
}
//! Render the scene node
void CTerrainSceneNode::render()
{
if (!IsVisible || !SceneManager->getActiveCamera())
return;
if (!Mesh.getMeshBufferCount())
return;
video::IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setTransform (video::ETS_WORLD, core::IdentityMatrix);
driver->setMaterial(Mesh.getMeshBuffer(0)->getMaterial());
RenderBuffer->getIndexBuffer().set_used(IndicesToRender);
// For use with geomorphing
driver->drawMeshBuffer(RenderBuffer);
RenderBuffer->getIndexBuffer().set_used(RenderBuffer->getIndexBuffer().allocated_size());
// for debug purposes only:
if (DebugDataVisible)
{
video::SMaterial m;
m.Lighting = false;
driver->setMaterial(m);
if (DebugDataVisible & scene::EDS_BBOX)
driver->draw3DBox(TerrainData.BoundingBox, video::SColor(255,255,255,255));
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
s32 visible = 0;
if (DebugDataVisible & scene::EDS_BBOX_BUFFERS)
{
for (s32 j = 0; j < count; ++j)
{
driver->draw3DBox(TerrainData.Patches[j].BoundingBox, video::SColor(255,255,0,0));
visible += (TerrainData.Patches[j].CurrentLOD >= 0);
}
}
if (DebugDataVisible & scene::EDS_NORMALS)
{
IAnimatedMesh * arrow = SceneManager->addArrowMesh(
"__debugnormal", 0xFFECEC00,
0xFF999900, 4, 8, 1.f, 0.6f, 0.05f,
0.3f);
if (0 == arrow)
{
Changes in version 1.6, TA - FileSystem 2.0 SUPER MASTER MAJOR API CHANGE !!! The FileSystem is know build internally like for e.q the texture-, and the meshloaders. There exists a known list of ArchiveLoader, which know how to produce a Archive. The Loaders and the Archive can be attached/detached on runtime. The FileNames are now stored as core::string<c16>. where c16 is toggled between char/wchar with the #define flag _IRR_WCHAR_FILESYSTEM, to supported unicode backends (default:off) I replaced all (const c8* filename) to string references. Basically the FileSystem is divided into two regions. Native and Virtual. Native means using the backend OS. Virtual means only use currently attach IArchives. Browsing each FileSystem has it's own workdirectory and it's own methods to - create a FileTree - add/remove files & directory ( to be done ) Hint: store a savegame in a zip archive... basic browsing for all archives is implemented. Example 21. Quake3Explorer shows this TODO: - a file filter should be implemented. - The IArchive should have a function to create a filetree for now CFileList is used. Class Hiarchy: IArchiveLoader: is able to produce a IFileArchive - ZipLoader - PakLoader - MountPointReader ( formaly known as CUnzipReader ) IFileArchive: -ZipArchive -PakArchive -MountPoint (known as FolderFile) IFileSystem - addArchiveLoader - changed implementation of isALoadableFileExtension in all loaders to have consistent behavior - added a parameter to IFileList * createFileList setFileListSystem allows to query files in any of the game archives standard behavior listtype = SYSTEM ( default) - CLimitReadFile added multiple file random-access support. solved problems with mixed compressed & uncompressed files in a zip TODO: - Big Big Testing!! - Linux Version ( minor ) - remove all double loader interfaces where only the filename differs (IReadFile/const char *filename). This blows up the the interface - many loaders use their own private filesearching we should rework this - there are a lot of helper function ( getAbsolutePath, getFileDir ) which should be adapted to the virtual filesystem - IrrlichtDevice added: virtual bool setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast ) = 0; virtual bool getGammaRamp( f32 &red, f32 &green, f32 &blue ) = 0; and calculating methods to DeviceStub. implemented in Win32, TODO: other Devices - irrlicht.h changed exported irrlicht.dll routines createDevice, createDeviceEx, IdentityMatrix to extern "C" name mangling. for easier dynamically loading the irrlicht library and different versions - ParticleSystem removed the private (old?,wrong?) interface from the ParticleEffectors to match the parent class irr::io::IAttributeExchangingObject::deserializeAttributes TODO: please test if the serialization works! - Generic - vector3d<T>& normalize() #if 0 f32 length = (f32)(X*X + Y*Y + Z*Z); if (core::equals(length, 0.f)) return *this; length = core::reciprocal_squareroot ( (f32)length ); #else const T length = core::reciprocal_squareroot ( (X*X + Y*Y + Z*Z) ); #endif Weak checking on zero?!?! just to avoid a sqrt?. mhm, maybe not;-) added reciprocal_squareroot for f64 - dimension2d added operator dimension2d<T>& operator=(const dimension2d<U>& other) to cast between different types - vector2d bugfix: vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Width; return *this; } to vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; } - C3DMeshLoader renamed chunks const u16 to a enum removing "variable declared but never used warning" - added a global const identity Material changed all references *((video::SMaterial*)0) to point to IdentityMaterial removed warning: "a NULL reference is not allowed" - modified IRRLICHT_MATH to not support reciprocal stuff but to use faster float-to-int conversion. gcc troubles may they are. i'm using intel-compiler..;-) - core::matrix4 USE_MATRIX_TEST i tried to optimize the identity-check ( in means of performance) i didn't succeed so well, so i made a define for the matrix isIdentity -check for now it's sometimes faster to always calculate versus identity-check but if there are a lot of scenenodes/ particles one can profit from the fast_inverse matrix, when no scaling is used. further approvement could be done on inverse for just tranlastion! ( many static scenenodes are not rotated, they are just placed somewhere in the world) one thing to take in account is that sizeof(matrix) is 64 byte and with the additional bool/u32 makes it 66 byte which is not really cache-friendly.. - added buildRotateFromTo Builds a matrix that rotates from one vector to another - irr::array. changed allocating routine in push_back okt, 2008. it's only allowed to alloc one element, if default constructor has to be called. removes existing crashes. ( MD3 Mesh ) and possible others ones. A new list template should be made. one with constructor/destructor calls ( safe_array ) and one without. like the array since the beginning of irrlicht. currently the array/string is extremly slow.. also a hint for the user has to be done, so that a struct T of array<T> must have a copy constructor of type T ( const T&other ). i needed hours to track that down... added a new method setAllocStrategy, safe ( used + 1 ), double ( used * 2 + 1) better default strategies will be implemented - removed binary_search_const i added it quite a long time ago, but it doesnt make real sense a call to a sort method should happen always. i just wanted to safe a few cycles.. - added binary_search_multi searches for a multi-set ( more than 1 entry in the sorted array) returns start and end-index - changed some identity matrix settings to use core::IdentityMatrix - added deletePathFromFilename to generic string functions in coreutil.h and removed from CZipReader and CPakReader - s32 deserializeAttributes used instead of virtual void deserializeAttributes in ParticleSystem ( wrong virtual was used) - strings & Locale - started to add locale support - added verify to string - added some helper functions - XBOX i have access to a XBOX development machine now. I started to compile for the XBOX. Question: Who did the previous implementation?. There is no XBOX-Device inhere. maybe it's forbidden because of using the offical Microsoft XDK. I will implement a native or sdl device based on opendk. irrlicht compiles without errors on the xbox but can't be used. TODO: - native XBOX Device - Windows Mobile reworked a little. added the mobile example to the windows solution for cross development. added maximal 128x128 texture size for windows mobile ( memory issues ) - Collision Speed Up The Collision Speed Up greatly improves with many small static child-nodes - added COctTreeTriangleSelector::getTriangles for 3dline from user Piraaate - modified createOctTreeTriangleSelector and createTriangleSelector to allow node == 0, to be added to a meta selector - CSceneNodeAnimatorCollisionResponse has the same problem as CSceneNodeAnimatorFPS on first update: Problem. you start setting the map. (setWorld). First update cames 4000 ms later. The Animator applies the missing force... big problem... changed to react on first update like camera. - add Variable FirstUpdate. if set to true ( on all changes ) then position, lasttime, and falling are initialized -added #define OCTTREE_USE_HARDWARE in Octree.h if defined octtree uses internally a derived scene::MeshBuffer which has the possibility to use the Hardware Vertex Buffer for static vertices and dirty indices;-) if defined OCTTREE_USE_HARDWARE octree uses internally a derived scene::CMeshBuffer so it's not just a replacement inside the octree. It also in the OctTreeSceneNode. #if defined (OCTTREE_USE_HARDWARE) driver->drawMeshBuffer ( &LightMapMeshes[i] ); #else driver->drawIndexedTriangleList( &LightMapMeshes[i].Vertices[0], LightMapMeshes[i].Vertices.size(), d[i].Indices, d[i].CurrentSize / 3); #endif #define OCTTREE_PARENTTEST is also used. It's skip testing on fully outside and takes everything on fully inside - virtual void ISceneNode::updateAbsolutePosition() - changed inline CMatrix4<T> CMatrix4<T>::operator*(const CMatrix4<T>& m2) const all two matrices have to be checked by isIdentity() to let the isIdentity work always -changed inline bool CMatrix4<T>::isIdentity() const on full identityCheck-> to look first on Translation, because this is the most challenging element which will likely not to be identity.. - virtual core::matrix4 getRelativeTransformation() const Hiarchy on Identity-Check 1) ->getRelativeTransform -> 9 floating point checks to be passed as Identity 2) ->isIdentity () -> 16 floating point checks to be passed as Identity - inline void CMatrix4<T>::transformBoxEx(core::aabbox3d<f32>& box) const added isIdentity() check - changed CSceneNodeAnimatorCollisionResponse - added CSceneNodeAnimatorCollisionResponse::setGravity needed to set the differents Forces for the Animator. for eq. water.. - added CSceneNodeAnimatorCollisionResponse::setAnimateTarget - added CSceneNodeAnimatorCollisionResponse::getAnimateTarget - changed CSceneNodeAnimatorCollisionResponse::animateNode to react on FirstUpdate - changad Gravity to - TODO: set Gravity to Physically frame independent values.. current response uses an frame depdended acceleration vector. ~9.81 m/s^2 was achieved at around 50 fps with a setting of -0.03 may effect existing application.. - SceneNodes - CSkyDomeSceneNode moved radius ( default 1000 ) to constructor added Normals added DebugInfo added Material.ZBuffer, added SceneMaanager - CVolumeLightSceneNode: changed default blending OneTextureBlendgl_src_color gl_src_alpha to EMT_TRANSPARENT_ADD_COLOR ( gl_src_color gl_one ) which gives the same effect on non-transparent-materials. Following the unspoken guide-line, lowest effect as default - added LensFlareSceneNode (from forum user gammaray, modified to work ) showing in example special fx - changed SceneNode Skydome f64 to f32, - AnimatedMesh -Debug Data: mesh normals didn't rotate with the scenenode fixed ( matrix-multiplication order) - Camera SceneNode setPosition Camera now finally allow to change position and target and updates all effected animators.. a call to OnAnimate ( ) lastime < time or OnAnimate ( 0 ) will reset the camera and fr. the collision animator to a new position - Device: added the current mousebutton state to the Mouse Event so i need to get the current mouse state from the OS -a dded to CIrrDeviceWin32 TODO: - Linux and SDL Device - GUI - CGUIFont: - added virtual void setInvisibleCharacters( const wchar_t *s ) = 0; define which characters should not be drawn ( send to driver) by the font. for example " " would not draw any space which is usually blank in most fonts and saves rendering of ususally full blank alpha-sprites. This saves a lot of rendering... default: setInvisibleCharacters ( L" " ); - added MultiLine rendering should avoid to us CStaticText breaking text in future - CGUIListBox - changed Scrollbar LargeStepSize to ItemHeight which easy enables to scroll line by line - CGUIScrollBar bug: Create a Window and inside a listbox with a scrollbar or a windowed irrlicht application Click & hold Scrollbar Slider. move outside it's region. Release Mouse. Go Back to Scrollbar.. it's moving always... it's generally missing the event PRESSED_MOVED, which leads to problem when an element is dragging, has a focus, or position loose and gets focus back again. ( think of a drunken mouse sliding left&right during tracking ) so added the mouse Input Buttonstates on every mouse event IrrDeviceWin32: added event.MouseInput.ButtonStates = wParam & ( MK_LBUTTON | MK_RBUTTON | MK_MBUTTON ); TODO: Linux & SDL so now i can do this case irr::EMIE_MOUSE_MOVED: if ( !event.MouseInput.isLeftPressed () ) { Dragging = false; } - bug: Scrollbar notifyListBox notify when the scrollbar is clicked. - changed timed event in draw to OnPostRender Why the hell is a gui element firing a timed event in a draw routine!!!!!. This should be corrected for all gui-elements. - added GUI Image List from Reinhard Ostermeier, modified to work added GUI Tree View from Reinhard Ostermeier, modified to work shown in the Quake3MapShader Example TODO: Spritebanks - FileOpenDialog changed the static text for the filename to an edit box. - changed the interface for addEditBox to match with addStaticText - changed the interface for addSpinBox to match with addEditBox - added MouseWheel to Spinbox - changed CGUITable CLICK_AREA from 3 to 12 to enable clicking on the visible marker - CGUISpritebank removed some crashes with empty Sprite banks - IGUIScrollBar added SetMin before min was always 0 changed ScrollWheel Direction on horizontal to move right on wheel up, left on wheel down - IComboBox -added ItemData - removed IsVisbile check in IGUIElement::draw - Image Loaders - added TGA file type 2 ( grayscale uncompressed ) - added TGA file type (1) 8 Bit indexed color uncompressed ColorConverter: - added convert_B8G8R8toA8R8G8B8 - added convert_B8G8R8A8toA8R8G8B8 - Media Files - added missing shaders and textures to map-20kdm2. Taken from free implementation - ball.wav. adjusted DC-Offset, amplified to -4dB, trim cross-zero - impact.wav clip-restoration, trim cross-zero - added gun.md2, gun.pcx to media-files copyright issues!. i don't know from where this file came from... i hope this is not from original quake2.. - added new irrlicht logo irrlicht3.png i've taken the new layout. i should ask niko to use it. - added Skydome picture to media files (skydome2.jpg) half/sphere - OctTree -added #define OCTTREE_PARENTTEST ( default: disabled ) used to leave-out children test if the parent passed a complete frustum. plus: leaves out children test minus: all edges have to be checked - added MesBuffer Hardware Hint Vertex to octtree - CQuake3ShaderSceneNode: - removed function releaseMesh Shader doesn't copy the original mesh anymore ( saving memory ) so therefore this (for others often misleading ) function was removed - changed constructor to take a (shared) destination meshbuffer for rendering reducing vertex-memory to a half - don't copy the original vertices anymore - added deformvertexes autosprite - added deformvertexes move - added support for RTCW and Raven BSPs ( qmap2 ) - added polygonoffset (TODO: not perfect) - added added nomipmaps - added rgbgen const - added alphagen - added MesBuffer Hardware Hint Vertex/Index to Quake3: static geometry, dynamic indices - added Quake3Explorer examples - added wave noise - added tcmod transform - added whiteimage - added collision to Quake3Explorer - renamed SMD3QuaterionTag* to SMD3QuaternionTag* ( typo ) - updated quake3:blendfunc - added crouch to Quake3Explorer (modifying the ellipsiodRadius of the camera animator ) added crouch to CSceneNodeAnimatorCameraFPS still problems with stand up and collision - Quake3MapLoader modified memory allocation for faster loading - Quake3LoadParam added Parameter to the Mesh-Loader - added The still existing missing caulking of curved surfaces. using round in the coordinates doesn't solve the problem. but for the demo bsp mesh it solves the problem... (luck) so for now it's switchable. TJUNCTION_SOLVER_ROUND default:off - BurningVideo - pushed BurningsVideo to 0.40 - added blendfunc gl_one_minus_dst_alpha gl_one - added blendfunc gl_dst_color gl_zero - added blendfunc gl_dst_color src_alpha - modified AlphaChannel_Ref renderer to support alpha test lessequal - addded 32 Bit Index Buffer - added sourceRect/destRect check to 2D-Blitter ( slower, but resolves crash ) - added setTextureCreationFlag video::ETCF_ALLOW_NON_POWER_2 Burning checks this flag and when set, it bypasses the power2 size check, which is necessary on 3D but can be avoided on 2D. used on fonts automatically. - added Support for Destination Alpha - OpenGL - Fixed a bug in COpenGLExtensenionHandler where a glint was downcasted to u8!!!!!! MaxTextureSize=static_cast<u32>(num); - TODO: COpenGLMaterialRenderer_ONETEXTURE_BLEND to work as expected - Direct3D8 - compile and links again - added 32 Bit Index Buffer - D3DSAMP_MIPMAPLODBIAS doesnt compile!. it is d3d9 i think. - compile for XBOX - Direc3D9 - fixed crash on RTT Textures DepthBuffer freed twice. added deleteAllTextures to destuctor - NullDriver - removeallTextures. added setMaterial ( SMaterial() ) to clean pointers for freed textures git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2147 dfc29bdd-3216-0410-991c-e03cc46cb475
2009-01-27 07:53:53 -08:00
arrow = SceneManager->getMesh( "__debugnormal");
}
IMesh *mesh = arrow->getMesh(0);
// find a good scaling factor
core::matrix4 m2;
// draw normals
driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
for (u32 i=0; i != RenderBuffer->getVertexCount(); ++i)
{
const core::vector3df& v = RenderBuffer->getNormal(i);
// align to v->Normal
if (core::vector3df(0,-1,0)==v)
{
m2.makeIdentity();
m2[5]=-m2[5];
}
else
{
core::quaternion quatRot;
m2=quatRot.rotationFromTo(v,core::vector3df(0,1,0)).getMatrix();
}
m2.setTranslation(RenderBuffer->getPosition(i));
m2=AbsoluteTransformation*m2;
driver->setTransform(video::ETS_WORLD, m2 );
for (u32 a = 0; a != mesh->getMeshBufferCount(); ++a)
driver->drawMeshBuffer(mesh->getMeshBuffer(a));
}
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
}
static u32 lastTime = 0;
const u32 now = os::Timer::getRealTime();
if (now - lastTime > 1000)
{
char buf[64];
snprintf(buf, 64, "Count: %d, Visible: %d", count, visible);
os::Printer::log(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.
void CTerrainSceneNode::getMeshBufferForLOD(IDynamicMeshBuffer& mb, s32 LOD ) const
{
if (!Mesh.getMeshBufferCount())
return;
LOD = core::clamp(LOD, 0, TerrainData.MaxLOD - 1);
const u32 numVertices = Mesh.getMeshBuffer(0)->getVertexCount();
mb.getVertexBuffer().reallocate(numVertices);
video::S3DVertex2TCoords* vertices = (video::S3DVertex2TCoords*)Mesh.getMeshBuffer(0)->getVertices();
for (u32 n=0; n<numVertices; ++n)
mb.getVertexBuffer().push_back(vertices[n]);
mb.getIndexBuffer().setType(RenderBuffer->getIndexBuffer().getType());
// calculate the step we take for all patches, since LOD is the same
const s32 step = 1 << LOD;
// Generate the indices for all patches at the specified LOD
s32 index = 0;
for (s32 i=0; i<TerrainData.PatchCount; ++i)
{
for (s32 j=0; j<TerrainData.PatchCount; ++j)
{
s32 x = 0;
s32 z = 0;
// Loop through patch and generate indices
while (z < TerrainData.CalcPatchSize)
{
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);
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);
// increment index position horizontally
x += step;
if (x >= TerrainData.CalcPatchSize) // we've hit an edge
{
x = 0;
z += step;
}
}
++index;
}
}
}
//! 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.
//! \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,
//! then it will retrieve the triangles at the highest LOD (0).
//! \return: Number if indices put into the buffer.
s32 CTerrainSceneNode::getIndicesForPatch(core::array<u32>& indices, s32 patchX, s32 patchZ, s32 LOD)
{
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
const s32 step = 1 << LOD;
// Generate the indices for the specified patch at the specified LOD
const s32 index = patchX * TerrainData.PatchCount + patchZ;
s32 x = 0;
s32 z = 0;
indices.set_used(TerrainData.PatchSize * TerrainData.PatchSize * 6);
// Loop through patch and generate indices
s32 rv=0;
while (z<TerrainData.CalcPatchSize)
{
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);
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)
setCurrentLODOfPatches(cLODs);
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
s32 CTerrainSceneNode::getCurrentLODOfPatches(core::array<s32>& LODs) const
{
s32 numLODs;
LODs.clear();
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
for (numLODs = 0; numLODs < count; numLODs++)
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)
{
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.f;
f32 x2val = 0.f;
for (s32 x=0; x<TerrainData.Size; ++x)
{
f32 zval=0.f;
f32 z2val=0.f;
for (s32 z=0; z<TerrainData.Size; ++z)
{
RenderBuffer->getVertexBuffer()[index].TCoords.X = 1.f-xval;
RenderBuffer->getVertexBuffer()[index].TCoords.Y = zval;
if (RenderBuffer->getVertexType()==video::EVT_2TCOORDS)
{
if (resolution2 == 0)
{
((video::S3DVertex2TCoords&)RenderBuffer->getVertexBuffer()[index]).TCoords2 = RenderBuffer->getVertexBuffer()[index].TCoords;
}
else
{
((video::S3DVertex2TCoords&)RenderBuffer->getVertexBuffer()[index]).TCoords2.X = 1.f-x2val;
((video::S3DVertex2TCoords&)RenderBuffer->getVertexBuffer()[index]).TCoords2.Y = z2val;
}
}
++index;
zval += resBySize;
z2val += res2BySize;
}
xval += resBySize;
x2val += res2BySize;
}
RenderBuffer->setDirty(EBT_VERTEX);
}
//! used to get the indices when generating index data for patches at varying levels of detail.
u32 CTerrainSceneNode::getIndex(const s32 PatchX, const s32 PatchZ,
const s32 PatchIndex, u32 vX, u32 vZ) const
{
// 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 )
{
vX -= vX % (1 << TerrainData.Patches[PatchIndex].Top->CurrentLOD);
}
}
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)
{
vX -= vX % (1 << TerrainData.Patches[PatchIndex].Bottom->CurrentLOD);
}
}
// 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)
{
vZ -= vZ % (1 << TerrainData.Patches[PatchIndex].Left->CurrentLOD);
}
}
else
if (vX == (u32)TerrainData.CalcPatchSize) // right border
{
if (TerrainData.Patches[PatchIndex].Right &&
TerrainData.Patches[PatchIndex].CurrentLOD < TerrainData.Patches[PatchIndex].Right->CurrentLOD &&
(vZ % (1 << TerrainData.Patches[PatchIndex].Right->CurrentLOD)) != 0)
{
vZ -= vZ % (1 << TerrainData.Patches[PatchIndex].Right->CurrentLOD);
}
}
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));
}
//! smooth the terrain
void CTerrainSceneNode::smoothTerrain(CDynamicMeshBuffer* mb, s32 smoothFactor)
{
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)
{
mb->getVertexBuffer()[x + yd].Pos.Y =
(mb->getVertexBuffer()[x-1 + yd].Pos.Y + //left
mb->getVertexBuffer()[x+1 + yd].Pos.Y + //right
mb->getVertexBuffer()[x + yd - TerrainData.Size].Pos.Y + //above
mb->getVertexBuffer()[x + yd + TerrainData.Size].Pos.Y) * 0.25f; //below
}
yd += TerrainData.Size;
}
}
}
//! calculate smooth normals
void CTerrainSceneNode::calculateNormals(CDynamicMeshBuffer* mb)
{
s32 count;
core::vector3df a, b, c, t;
for (s32 x=0; x<TerrainData.Size; ++x)
{
for (s32 z=0; z<TerrainData.Size; ++z)
{
count = 0;
core::vector3df normal;
// top left
if (x>0 && z>0)
{
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;
b -= a;
c -= a;
t = b.crossProduct(c);
t.normalize();
normal += t;
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;
b -= a;
c -= a;
t = b.crossProduct(c);
t.normalize();
normal += t;
count += 2;
}
// top right
if (x>0 && z<TerrainData.Size-1)
{
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;
b -= a;
c -= a;
t = b.crossProduct(c);
t.normalize();
normal += t;
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;
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)
{
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;
b -= a;
c -= a;
t = b.crossProduct(c);
t.normalize();
normal += t;
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;
b -= a;
c -= a;
t = b.crossProduct(c);
t.normalize();
normal += t;
count += 2;
}
// bottom left
if (x<TerrainData.Size-1 && z>0)
{
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;
b -= a;
c -= a;
t = b.crossProduct(c);
t.normalize();
normal += t;
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;
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);
}
mb->getVertexBuffer()[x * TerrainData.Size + z].Normal = normal;
}
}
}
//! 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)
{
const s32 index = x * TerrainData.PatchCount + z;
TerrainData.Patches[index].CurrentLOD = 0;
// For each patch, calculate the bounding box (mins and maxes)
TerrainData.Patches[index].BoundingBox = core::aabbox3df(999999.9f, 999999.9f, 999999.9f,
-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)
TerrainData.Patches[index].BoundingBox.addInternalPoint(RenderBuffer->getVertexBuffer()[xx * TerrainData.Size + zz].Pos);
// 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)
{
TerrainData.LODDistanceThreshold.set_used(0);
// Determine new distance threshold for determining what LOD to draw patches at
TerrainData.LODDistanceThreshold.reallocate(TerrainData.MaxLOD);
const f64 size = TerrainData.PatchSize * TerrainData.PatchSize *
TerrainData.Scale.X * TerrainData.Scale.Z;
for (s32 i=0; i<TerrainData.MaxLOD; ++i)
{
TerrainData.LODDistanceThreshold.push_back(size * ((i+1+ i / 2) * (i+1+ i / 2)));
}
}
}
void CTerrainSceneNode::setCurrentLODOfPatches(s32 lod)
{
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
for (s32 i=0; i< count; ++i)
TerrainData.Patches[i].CurrentLOD = lod;
}
void CTerrainSceneNode::setCurrentLODOfPatches(const core::array<s32>& lodarray)
{
const s32 count = TerrainData.PatchCount * TerrainData.PatchCount;
for (s32 i=0; i<count; ++i)
TerrainData.Patches[i].CurrentLOD = lodarray[i];
}
//! Gets the height
f32 CTerrainSceneNode::getHeight(f32 x, f32 z) const
{
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));
if (X >= 0 && X < TerrainData.Size-1 &&
Z >= 0 && Z < TerrainData.Size-1)
{
const video::S3DVertex2TCoords* Vertices = (const video::S3DVertex2TCoords*)Mesh.getMeshBuffer(0)->getVertices();
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.
void CTerrainSceneNode::serializeAttributes(io::IAttributes* out,
io::SAttributeReadWriteOptions* options) const
{
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,
io::SAttributeReadWriteOptions* options)
{
io::path newHeightmap = in->getAttributeAsString("Heightmap");
f32 tcoordScale1 = in->getAttributeAsFloat("TextureScale1");
f32 tcoordScale2 = in->getAttributeAsFloat("TextureScale2");
// set possible new heightmap
if (newHeightmap.size() != 0 && newHeightmap != HeightmapFile)
{
io::IReadFile* file = FileSystem->createAndOpenFile(newHeightmap.c_str());
if (file)
{
loadHeightMap(file, video::SColor(255,255,255,255), 0);
file->drop();
}
else
os::Printer::log("could not open heightmap", newHeightmap.c_str());
}
// set possible new scale
if (core::equals(tcoordScale1, 0.f))
tcoordScale1 = 1.0f;
if (core::equals(tcoordScale2, 0.f))
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)
{
if (!newParent)
newParent = Parent;
if (!newManager)
newManager = SceneManager;
CTerrainSceneNode* nb = new CTerrainSceneNode(
newParent, newManager, FileSystem, ID,
4, ETPS_17, getPosition(), getRotation(), getScale());
nb->cloneMembers(this, newManager);
// 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();
}
// 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))
{
nb->Mesh.getMeshBuffer(m)->getMaterial() =
Mesh.getMeshBuffer(m)->getMaterial();
}
}
nb->RenderBuffer->Material = RenderBuffer->Material;
// finish
nb->drop();
return nb;
}
} // end namespace scene
} // end namespace irr