📄 ettile.cpp
字号:
/*
EDITABLE TERRAIN MANAGER for Ogre
Copyright (C) 2007 Holger Frydrych <h.frydrych@gmx.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#include "Impl/ETTerrainImpl.h"
#include "Impl/ETTile.h"
#include "Impl/ETIndexHandler.h"
#include "ETTerrainInfo.h"
#include <OgreColourValue.h>
#include <OgreHardwareBufferManager.h>
using namespace Ogre;
namespace ET
{
namespace Impl
{
const unsigned short MAIN_BINDING = 0;
const unsigned short DELTA_BINDING = 1;
const unsigned int MORPH_CUSTOM_PARAM_ID = 77;
Tile::Tile(const String& name, SceneManager* sm, TerrainImpl* tm,
IndexHandler* indexHandler, TerrainInfo& info, Options opts, size_t startx, size_t startz)
: MovableObject(name), mSceneMgr(sm), mTerrainMgr(tm),
mIndexHandler(indexHandler), mInfo(info), mOpt(opts),
mStartX(startx), mStartZ(startz),
mLOD(0),
mLODChangeMinDistSqr(opts.maxMipMapLevel),
mLightListDirty(true),
mTerrain(0),
mLastNextLevel(0)
{
// disable LOD morphing if max LOD is 1
if (mOpt.maxMipMapLevel <= 1)
mOpt.useLODMorph = false;
mCastShadows = false;
mTileX = startx / (mOpt.tileSize-1);
mTileZ = startz / (mOpt.tileSize-1);
createVertexData(startx, startz);
calculateMinLevelDist2();
}
Tile::~Tile()
{
}
const String& Tile::getMovableType() const
{
static const String type = "EditableTerrainTile";
return type;
}
const MaterialPtr& Tile::getMaterial() const
{
return mTerrainMgr->getMaterial();
}
const AxisAlignedBox& Tile::getBoundingBox() const
{
return mBounds;
}
Real Tile::getBoundingRadius() const
{
return mBoundingRadius;
}
void Tile::getWorldTransforms(Matrix4* m) const
{
*m = mParentNode->_getFullTransform();
}
const Quaternion& Tile::getWorldOrientation() const
{
return mParentNode->_getDerivedOrientation();
}
const Vector3& Tile::getWorldPosition() const
{
return mParentNode->_getDerivedPosition();
}
Real Tile::getSquaredViewDepth(const Camera* cam) const
{
Vector3 diff = mCenter - cam->getDerivedPosition();
return diff.squaredLength();
}
const LightList& Tile::getLights() const
{
if (mLightListDirty)
{
mSceneMgr->_populateLightList(mCenter, getBoundingRadius(), mLightList);
mLightListDirty = false;
}
return mLightList;
}
uint32 Tile::getTypeFlags() const
{
return SceneManager::WORLD_GEOMETRY_TYPE_MASK;
}
void Tile::emptyBuffer(HardwareVertexBufferSharedPtr buf)
{
void* pVoid = buf->lock(HardwareBuffer::HBL_DISCARD);
memset(pVoid, 0, mOpt.tileSize*mOpt.tileSize*sizeof(float));
buf->unlock();
}
void Tile::createVertexData(size_t startx, size_t startz)
{
mTerrain = new VertexData;
mTerrain->vertexStart = 0;
mTerrain->vertexCount = mOpt.tileSize * mOpt.tileSize;
VertexDeclaration* decl = mTerrain->vertexDeclaration;
VertexBufferBinding* bind = mTerrain->vertexBufferBinding;
// first we need to declare the contents of our vertex buffer
size_t offset = 0;
decl->addElement(MAIN_BINDING, offset, VET_FLOAT3, VES_POSITION);
offset += VertexElement::getTypeSize(VET_FLOAT3);
if (mOpt.vertexNormals)
{
decl->addElement(MAIN_BINDING, offset, VET_FLOAT3, VES_NORMAL);
offset += VertexElement::getTypeSize(VET_FLOAT3);
}
decl->addElement(MAIN_BINDING, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
offset += VertexElement::getTypeSize(VET_FLOAT2);
mMainBuffer = HardwareBufferManager::getSingleton().createVertexBuffer(
decl->getVertexSize(MAIN_BINDING), mTerrain->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
// bind the buffer
bind->setBinding(MAIN_BINDING, mMainBuffer);
// declare delta buffers, if requested
if (mOpt.useLODMorph)
{
decl->addElement(DELTA_BINDING, 0, VET_FLOAT1, VES_BLEND_WEIGHTS);
}
// now construct the vertex buffer from the heightmap data
size_t endx = startx + mOpt.tileSize;
size_t endz = startz + mOpt.tileSize;
Real minHeight = mInfo.getOffset().y + mInfo.getScaling().y, maxHeight = mInfo.getOffset().y;
const VertexElement* posElem = decl->findElementBySemantic(VES_POSITION);
const VertexElement* texElem0 = decl->findElementBySemantic(VES_TEXTURE_COORDINATES, 0);
unsigned char* pBase = static_cast<unsigned char*>(mMainBuffer->lock(HardwareBuffer::HBL_DISCARD));
for (size_t j = startz; j < endz; ++j)
{
for (size_t i = startx; i < endx; ++i)
{
float* pPos, * pTex0;//, * pTex1;
posElem->baseVertexPointerToElement(pBase, &pPos);
texElem0->baseVertexPointerToElement(pBase, &pTex0);
Real height = mInfo.getOffset().y + mInfo.at(i, j) * mInfo.getScaling().y;
*pPos++ = mInfo.getOffset().x + mInfo.getScaling().x * i;
*pPos++ = height;
*pPos++ = mInfo.getOffset().z + mInfo.getScaling().z * j;
*pTex0++ = float(i) / (mInfo.getWidth() - 1);
*pTex0++ = float(j) / (mInfo.getHeight() - 1);
if (height < minHeight)
minHeight = height;
if (height > maxHeight)
maxHeight = height;
pBase += mMainBuffer->getVertexSize();
}
}
mMainBuffer->unlock();
// set the extents of this terrain tile
mBounds.setExtents(mInfo.getOffset().x + mInfo.getScaling().x * startx, minHeight,
mInfo.getOffset().z + mInfo.getScaling().z * startz,
mInfo.getOffset().x + mInfo.getScaling().x * endx, maxHeight,
mInfo.getOffset().z + mInfo.getScaling().z * (endz));
mCenter = mBounds.getCenter();
mBoundingRadius = (mBounds.getMaximum() - mCenter).length();
// if using LOD morphing, create the delta buffers now (they'll be filled in _calculateMinLevelDist2)
if (mOpt.useLODMorph)
{
for (unsigned int i = 0; i < mOpt.maxMipMapLevel-1; ++i)
{
HardwareVertexBufferSharedPtr buf = HardwareBufferManager::getSingleton().createVertexBuffer(
VertexElement::getTypeSize(VET_FLOAT1), mOpt.tileSize*mOpt.tileSize,
HardwareBuffer::HBU_STATIC_WRITE_ONLY);
emptyBuffer(buf);
mDeltaBuffers.push_back(buf);
}
}
// calc vertex normals, if necessary
if (mOpt.vertexNormals)
calculateVertexNormals();
}
void Tile::calculateVertexNormals()
{
// set the vertex normals of the tile
size_t startx = mStartX;
size_t startz = mStartZ;
size_t endx = startx + mOpt.tileSize;
size_t endz = startz + mOpt.tileSize;
Real minHeight = mInfo.getOffset().y + mInfo.getScaling().y, maxHeight = mInfo.getOffset().y;
const VertexElement* normElem = mTerrain->vertexDeclaration->findElementBySemantic(VES_NORMAL);
unsigned char* pBase = static_cast<unsigned char*>(mMainBuffer->lock(HardwareBuffer::HBL_NORMAL));
for (size_t j = startz; j < endz; ++j)
{
for (size_t i = startx; i < endx; ++i)
{
float* pNorm;
normElem->baseVertexPointerToElement(pBase, &pNorm);
Vector3 normal = mInfo.getNormalAt(mInfo.vertexToPosX((int)i), mInfo.vertexToPosZ((int)j));
*pNorm++ = normal.x;
*pNorm++ = normal.y;
*pNorm++ = normal.z;
pBase += mMainBuffer->getVertexSize();
}
}
mMainBuffer->unlock();
}
IndexData* Tile::getIndexData()
{
IndexData* data = mIndexHandler->requestIndexData(mLOD,
mTerrainMgr->getNeighbourState(mTileX, mTileZ));
return data;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -