1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 12:53:51 +00:00
openmw-tes3mp/terrain/cpp_baseland.cpp
nkorslund f739bf90f1 Terrain code 90% done.
git-svn-id: https://openmw.svn.sourceforge.net/svnroot/openmw/trunk@123 ea6a568a-9f4f-0410-981a-c910a81bb256
2009-06-08 12:59:00 +00:00

99 lines
2.2 KiB
C++

class BaseLand
{
public:
BaseLand()
{
createMesh();
}
~BaseLand()
{
destroyMesh();
}
// Repositions the mesh based on camera location
void update()
{
Ogre::Real vd = mCamera->getFarClipDistance();
// Recreate the mesh if the view distance has increased
if ( vd > mMeshDistance )
{
destroyMesh();
createMesh();
}
Ogre::Vector3 p = mCamera->getDerivedPosition();
p.x -= ((int)p.x % CELL_WIDTH);
p.z -= ((int)p.z % CELL_WIDTH);
float h = (p.y + 2048)*2.0/CELL_WIDTH;
h *= h;
mNode->setPosition(p.x, -p.z, -32 - h);
}
private:
void createMesh()
{
float vd = mCamera->getFarClipDistance();
mMeshDistance = vd;
vd = vd/CELL_WIDTH * 32;
mMat = Ogre::MaterialManager::getSingleton().
create("BaseLandMat",
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Ogre::TextureUnitState* us = mMat->getTechnique(0)->getPass(0)->createTextureUnitState("_land_default.dds");
us->setTextureScale(1.0f/vd,1.0f/vd);
mMat->getTechnique(0)->getPass(0)->setDepthBias(-1);
mObject = mSceneMgr->createManualObject("BaseLand");
mObject->begin("BaseLandMat", Ogre::RenderOperation::OT_TRIANGLE_LIST);
vd = mMeshDistance;
mObject->position(-vd,vd,-2048);
mObject->textureCoord(0, 1);
mObject->position(-vd,-vd,-2048);
mObject->textureCoord(0, 0);
mObject->position(vd,-vd,-2048);
mObject->textureCoord(1, 0);
mObject->position(vd,vd,-2048);
mObject->textureCoord(1, 1);
mObject->quad(0,1,2,3);
mObject->end();
mNode = g_rootTerrainNode->createChildSceneNode();
mNode->attachObject(mObject);
}
void destroyMesh()
{
mNode->detachAllObjects();
mSceneMgr->destroyManualObject(mObject);
mNode->getParentSceneNode()->removeAndDestroyChild(mNode->getName());
mMat->getCreator()->remove(mMat->getHandle());
mMat = Ogre::MaterialPtr();
}
///the created mesh
Ogre::ManualObject* mObject;
///The material for the mesh
Ogre::MaterialPtr mMat;
///scene node for the mesh
Ogre::SceneNode* mNode;
///In essence, the farViewDistance of the camera last frame
Ogre::Real mMeshDistance;
};