|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
#include "terraingrid.hpp"
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <osg/io_utils>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include <components/resource/resourcesystem.hpp>
|
|
|
|
|
#include <components/resource/texturemanager.hpp>
|
|
|
|
@ -47,8 +49,10 @@ namespace Terrain
|
|
|
|
|
TerrainGrid::TerrainGrid(osg::Group* parent, Resource::ResourceSystem* resourceSystem, osgUtil::IncrementalCompileOperation* ico,
|
|
|
|
|
Storage* storage, int nodeMask)
|
|
|
|
|
: Terrain::World(parent, resourceSystem, ico, storage, nodeMask)
|
|
|
|
|
, mNumSplits(4)
|
|
|
|
|
, mKdTreeBuilder(new osg::KdTreeBuilder)
|
|
|
|
|
{
|
|
|
|
|
mCache = BufferCache((storage->getCellVertices()-1)/static_cast<float>(mNumSplits) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TerrainGrid::~TerrainGrid()
|
|
|
|
@ -62,108 +66,149 @@ TerrainGrid::~TerrainGrid()
|
|
|
|
|
class GridElement
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
osg::ref_ptr<osg::PositionAttitudeTransform> mNode;
|
|
|
|
|
osg::ref_ptr<osg::Node> mNode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void TerrainGrid::loadCell(int x, int y)
|
|
|
|
|
osg::ref_ptr<osg::Node> TerrainGrid::buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter)
|
|
|
|
|
{
|
|
|
|
|
if (mGrid.find(std::make_pair(x, y)) != mGrid.end())
|
|
|
|
|
return; // already loaded
|
|
|
|
|
if (chunkSize * mNumSplits > 1.f)
|
|
|
|
|
{
|
|
|
|
|
// keep splitting
|
|
|
|
|
osg::ref_ptr<osg::Group> group (new osg::Group);
|
|
|
|
|
if (parent)
|
|
|
|
|
parent->addChild(group);
|
|
|
|
|
std::cout << "splitting " << chunkSize << " " << std::endl;
|
|
|
|
|
float newChunkSize = chunkSize/2.f;
|
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(newChunkSize/2.f, newChunkSize/2.f));
|
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(newChunkSize/2.f, -newChunkSize/2.f));
|
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(-newChunkSize/2.f, newChunkSize/2.f));
|
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(-newChunkSize/2.f, -newChunkSize/2.f));
|
|
|
|
|
return group;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
float minH, maxH;
|
|
|
|
|
if (!mStorage->getMinMaxHeights(chunkSize, chunkCenter, minH, maxH))
|
|
|
|
|
return NULL; // no terrain defined
|
|
|
|
|
|
|
|
|
|
osg::Vec2f center(x+0.5f, y+0.5f);
|
|
|
|
|
float minH, maxH;
|
|
|
|
|
if (!mStorage->getMinMaxHeights(1, center, minH, maxH))
|
|
|
|
|
return; // no terrain defined
|
|
|
|
|
std::cout << "creating " << chunkSize << " " << chunkCenter << std::endl;
|
|
|
|
|
|
|
|
|
|
std::auto_ptr<GridElement> element (new GridElement);
|
|
|
|
|
osg::Vec2f worldCenter = chunkCenter*mStorage->getCellWorldSize();
|
|
|
|
|
osg::ref_ptr<osg::PositionAttitudeTransform> transform (new osg::PositionAttitudeTransform);
|
|
|
|
|
transform->setPosition(osg::Vec3f(worldCenter.x(), worldCenter.y(), 0.f));
|
|
|
|
|
|
|
|
|
|
osg::Vec2f worldCenter = center*mStorage->getCellWorldSize();
|
|
|
|
|
element->mNode = new osg::PositionAttitudeTransform;
|
|
|
|
|
element->mNode->setPosition(osg::Vec3f(worldCenter.x(), worldCenter.y(), 0.f));
|
|
|
|
|
mTerrainRoot->addChild(element->mNode);
|
|
|
|
|
if (parent)
|
|
|
|
|
parent->addChild(transform);
|
|
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Vec3Array> positions (new osg::Vec3Array);
|
|
|
|
|
osg::ref_ptr<osg::Vec3Array> normals (new osg::Vec3Array);
|
|
|
|
|
osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array);
|
|
|
|
|
osg::ref_ptr<osg::Vec3Array> positions (new osg::Vec3Array);
|
|
|
|
|
osg::ref_ptr<osg::Vec3Array> normals (new osg::Vec3Array);
|
|
|
|
|
osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array);
|
|
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::VertexBufferObject> vbo (new osg::VertexBufferObject);
|
|
|
|
|
positions->setVertexBufferObject(vbo);
|
|
|
|
|
normals->setVertexBufferObject(vbo);
|
|
|
|
|
colors->setVertexBufferObject(vbo);
|
|
|
|
|
osg::ref_ptr<osg::VertexBufferObject> vbo (new osg::VertexBufferObject);
|
|
|
|
|
positions->setVertexBufferObject(vbo);
|
|
|
|
|
normals->setVertexBufferObject(vbo);
|
|
|
|
|
colors->setVertexBufferObject(vbo);
|
|
|
|
|
|
|
|
|
|
mStorage->fillVertexBuffers(0, 1, center, positions, normals, colors);
|
|
|
|
|
mStorage->fillVertexBuffers(0, chunkSize, chunkCenter, positions, normals, colors);
|
|
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry);
|
|
|
|
|
geometry->setVertexArray(positions);
|
|
|
|
|
geometry->setNormalArray(normals, osg::Array::BIND_PER_VERTEX);
|
|
|
|
|
geometry->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
|
|
|
|
|
geometry->setUseDisplayList(false);
|
|
|
|
|
geometry->setUseVertexBufferObjects(true);
|
|
|
|
|
osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry);
|
|
|
|
|
geometry->setVertexArray(positions);
|
|
|
|
|
geometry->setNormalArray(normals, osg::Array::BIND_PER_VERTEX);
|
|
|
|
|
geometry->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
|
|
|
|
|
geometry->setUseDisplayList(false);
|
|
|
|
|
geometry->setUseVertexBufferObjects(true);
|
|
|
|
|
|
|
|
|
|
geometry->addPrimitiveSet(mCache.getIndexBuffer(0));
|
|
|
|
|
geometry->addPrimitiveSet(mCache.getIndexBuffer(0));
|
|
|
|
|
|
|
|
|
|
// we already know the bounding box, so no need to let OSG compute it.
|
|
|
|
|
osg::Vec3f min(-0.5f*mStorage->getCellWorldSize(),
|
|
|
|
|
-0.5f*mStorage->getCellWorldSize(),
|
|
|
|
|
minH);
|
|
|
|
|
osg::Vec3f max (0.5f*mStorage->getCellWorldSize(),
|
|
|
|
|
0.5f*mStorage->getCellWorldSize(),
|
|
|
|
|
maxH);
|
|
|
|
|
osg::BoundingBox bounds(min, max);
|
|
|
|
|
geometry->setComputeBoundingBoxCallback(new StaticBoundingBoxCallback(bounds));
|
|
|
|
|
// we already know the bounding box, so no need to let OSG compute it.
|
|
|
|
|
osg::Vec3f min(-0.5f*mStorage->getCellWorldSize()*chunkSize,
|
|
|
|
|
-0.5f*mStorage->getCellWorldSize()*chunkSize,
|
|
|
|
|
minH);
|
|
|
|
|
osg::Vec3f max (0.5f*mStorage->getCellWorldSize()*chunkSize,
|
|
|
|
|
0.5f*mStorage->getCellWorldSize()*chunkSize,
|
|
|
|
|
maxH);
|
|
|
|
|
osg::BoundingBox bounds(min, max);
|
|
|
|
|
geometry->setComputeBoundingBoxCallback(new StaticBoundingBoxCallback(bounds));
|
|
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Geode> geode (new osg::Geode);
|
|
|
|
|
geode->addDrawable(geometry);
|
|
|
|
|
osg::ref_ptr<osg::Geode> geode (new osg::Geode);
|
|
|
|
|
geode->addDrawable(geometry);
|
|
|
|
|
|
|
|
|
|
// build a kdtree to speed up intersection tests with the terrain
|
|
|
|
|
// Note, the build could be optimized using a custom kdtree builder, since we know that the terrain can be represented by a quadtree
|
|
|
|
|
geode->accept(*mKdTreeBuilder);
|
|
|
|
|
|
|
|
|
|
std::vector<LayerInfo> layerList;
|
|
|
|
|
std::vector<osg::ref_ptr<osg::Image> > blendmaps;
|
|
|
|
|
mStorage->getBlendmaps(1.f, center, false, blendmaps, layerList);
|
|
|
|
|
std::vector<LayerInfo> layerList;
|
|
|
|
|
std::vector<osg::ref_ptr<osg::Image> > blendmaps;
|
|
|
|
|
mStorage->getBlendmaps(chunkSize, chunkCenter, false, blendmaps, layerList);
|
|
|
|
|
|
|
|
|
|
// For compiling textures, I don't think the osgFX::Effect does it correctly
|
|
|
|
|
osg::ref_ptr<osg::Node> textureCompileDummy (new osg::Node);
|
|
|
|
|
// For compiling textures, I don't think the osgFX::Effect does it correctly
|
|
|
|
|
osg::ref_ptr<osg::Node> textureCompileDummy (new osg::Node);
|
|
|
|
|
|
|
|
|
|
std::vector<osg::ref_ptr<osg::Texture2D> > layerTextures;
|
|
|
|
|
for (std::vector<LayerInfo>::const_iterator it = layerList.begin(); it != layerList.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
layerTextures.push_back(mResourceSystem->getTextureManager()->getTexture2D(it->mDiffuseMap, osg::Texture::REPEAT, osg::Texture::REPEAT));
|
|
|
|
|
textureCompileDummy->getOrCreateStateSet()->setTextureAttributeAndModes(0, layerTextures.back());
|
|
|
|
|
}
|
|
|
|
|
std::vector<osg::ref_ptr<osg::Texture2D> > layerTextures;
|
|
|
|
|
for (std::vector<LayerInfo>::const_iterator it = layerList.begin(); it != layerList.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
layerTextures.push_back(mResourceSystem->getTextureManager()->getTexture2D(it->mDiffuseMap, osg::Texture::REPEAT, osg::Texture::REPEAT));
|
|
|
|
|
textureCompileDummy->getOrCreateStateSet()->setTextureAttributeAndModes(0, layerTextures.back());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<osg::ref_ptr<osg::Texture2D> > blendmapTextures;
|
|
|
|
|
for (std::vector<osg::ref_ptr<osg::Image> >::const_iterator it = blendmaps.begin(); it != blendmaps.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D);
|
|
|
|
|
texture->setImage(*it);
|
|
|
|
|
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
|
|
|
|
|
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
|
|
|
|
|
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
|
|
|
|
|
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
|
|
|
|
|
texture->setResizeNonPowerOfTwoHint(false);
|
|
|
|
|
blendmapTextures.push_back(texture);
|
|
|
|
|
|
|
|
|
|
textureCompileDummy->getOrCreateStateSet()->setTextureAttributeAndModes(0, layerTextures.back());
|
|
|
|
|
std::vector<osg::ref_ptr<osg::Texture2D> > blendmapTextures;
|
|
|
|
|
for (std::vector<osg::ref_ptr<osg::Image> >::const_iterator it = blendmaps.begin(); it != blendmaps.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D);
|
|
|
|
|
texture->setImage(*it);
|
|
|
|
|
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
|
|
|
|
|
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
|
|
|
|
|
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
|
|
|
|
|
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
|
|
|
|
|
texture->setResizeNonPowerOfTwoHint(false);
|
|
|
|
|
blendmapTextures.push_back(texture);
|
|
|
|
|
|
|
|
|
|
textureCompileDummy->getOrCreateStateSet()->setTextureAttributeAndModes(0, layerTextures.back());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use texture coordinates for both texture units, the layer texture and blend texture
|
|
|
|
|
for (unsigned int i=0; i<2; ++i)
|
|
|
|
|
geometry->setTexCoordArray(i, mCache.getUVBuffer());
|
|
|
|
|
|
|
|
|
|
float blendmapScale = ESM::Land::LAND_TEXTURE_SIZE*chunkSize;
|
|
|
|
|
osg::ref_ptr<osgFX::Effect> effect (new Terrain::Effect(layerTextures, blendmapTextures, blendmapScale, blendmapScale));
|
|
|
|
|
|
|
|
|
|
effect->addCullCallback(new SceneUtil::LightListCallback);
|
|
|
|
|
|
|
|
|
|
transform->addChild(effect);
|
|
|
|
|
effect->addChild(geode);
|
|
|
|
|
|
|
|
|
|
return transform;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TerrainGrid::loadCell(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
if (mGrid.find(std::make_pair(x, y)) != mGrid.end())
|
|
|
|
|
return; // already loaded
|
|
|
|
|
|
|
|
|
|
// use texture coordinates for both texture units, the layer texture and blend texture
|
|
|
|
|
for (unsigned int i=0; i<2; ++i)
|
|
|
|
|
geometry->setTexCoordArray(i, mCache.getUVBuffer());
|
|
|
|
|
osg::Vec2f center(x+0.5f, y+0.5f);
|
|
|
|
|
|
|
|
|
|
osg::ref_ptr<osgFX::Effect> effect (new Terrain::Effect(layerTextures, blendmapTextures, ESM::Land::LAND_TEXTURE_SIZE, ESM::Land::LAND_TEXTURE_SIZE));
|
|
|
|
|
osg::ref_ptr<osg::Node> terrainNode = buildTerrain(NULL, 1.f, center);
|
|
|
|
|
if (!terrainNode)
|
|
|
|
|
return; // no terrain defined
|
|
|
|
|
|
|
|
|
|
std::auto_ptr<GridElement> element (new GridElement);
|
|
|
|
|
element->mNode = terrainNode;
|
|
|
|
|
mTerrainRoot->addChild(element->mNode);
|
|
|
|
|
|
|
|
|
|
effect->addCullCallback(new SceneUtil::LightListCallback);
|
|
|
|
|
/*
|
|
|
|
|
// build a kdtree to speed up intersection tests with the terrain
|
|
|
|
|
// Note, the build could be optimized using a custom kdtree builder, since we know that the terrain can be represented by a quadtree
|
|
|
|
|
geode->accept(*mKdTreeBuilder);
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
effect->addChild(geode);
|
|
|
|
|
element->mNode->addChild(effect);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
if (mIncrementalCompileOperation)
|
|
|
|
|
{
|
|
|
|
|
mIncrementalCompileOperation->add(geode);
|
|
|
|
|
mIncrementalCompileOperation->add(textureCompileDummy);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mGrid[std::make_pair(x,y)] = element.release();
|
|
|
|
|
}
|
|
|
|
|