openmw-tes3coop/components/terrain/material.cpp

91 lines
3.4 KiB
C++
Raw Normal View History

#include "material.hpp"
2015-06-02 23:18:36 +00:00
#include <osg/Depth>
#include <osg/TexEnvCombine>
#include <osg/Texture2D>
#include <osg/TexMat>
#include <osg/Material>
2015-06-03 00:21:41 +00:00
#include <osg/TexEnvCombine>
namespace Terrain
{
2015-06-02 23:18:36 +00:00
FixedFunctionTechnique::FixedFunctionTechnique(const std::vector<osg::ref_ptr<osg::Texture2D> >& layers,
const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapSize, float layerTileSize)
{
2015-06-02 23:18:36 +00:00
bool firstLayer = true;
int i=0;
for (std::vector<osg::ref_ptr<osg::Texture2D> >::const_iterator it = layers.begin(); it != layers.end(); ++it)
{
2015-06-02 23:18:36 +00:00
osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet);
2015-06-02 23:18:36 +00:00
if (!firstLayer)
{
2015-06-02 23:18:36 +00:00
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
osg::ref_ptr<osg::Depth> depth (new osg::Depth);
depth->setFunction(osg::Depth::EQUAL);
stateset->setAttributeAndModes(depth, osg::StateAttribute::ON);
}
2015-06-02 23:18:36 +00:00
int texunit = 0;
if(!firstLayer)
{
2015-06-02 23:18:36 +00:00
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++);
2015-06-02 23:18:36 +00:00
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
2015-06-02 23:18:36 +00:00
// This is to map corner vertices directly to the center of a blendmap texel.
osg::Matrixf texMat;
float scale = (blendmapSize/(static_cast<float>(blendmapSize)+1.f));
2015-06-02 23:18:36 +00:00
texMat.preMultTranslate(osg::Vec3f(0.5f, 0.5f, 0.f));
texMat.preMultScale(osg::Vec3f(scale, scale, 1.f));
texMat.preMultTranslate(osg::Vec3f(-0.5f, -0.5f, 0.f));
2015-06-02 23:18:36 +00:00
stateset->setTextureAttributeAndModes(texunit, new osg::TexMat(texMat));
2015-06-03 00:21:41 +00:00
osg::ref_ptr<osg::TexEnvCombine> texEnvCombine (new osg::TexEnvCombine);
texEnvCombine->setCombine_RGB(osg::TexEnvCombine::REPLACE);
texEnvCombine->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
stateset->setTextureAttributeAndModes(texunit, texEnvCombine, osg::StateAttribute::ON);
2015-06-02 23:18:36 +00:00
++texunit;
}
2015-06-02 23:18:36 +00:00
// Add the actual layer texture multiplied by the alpha map.
osg::ref_ptr<osg::Texture2D> tex = *it;
stateset->setTextureAttributeAndModes(texunit, tex.get());
2015-06-02 23:18:36 +00:00
osg::ref_ptr<osg::TexMat> texMat (new osg::TexMat);
texMat->setMatrix(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f)));
2015-06-02 23:18:36 +00:00
stateset->setTextureAttributeAndModes(texunit, texMat, osg::StateAttribute::ON);
2015-06-02 23:18:36 +00:00
firstLayer = false;
2015-06-02 23:18:36 +00:00
addPass(stateset);
}
}
Effect::Effect(const std::vector<osg::ref_ptr<osg::Texture2D> > &layers, const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps,
int blendmapSize, float layerTileSize)
2015-06-02 23:18:36 +00:00
: mLayers(layers)
, mBlendmaps(blendmaps)
, mBlendmapSize(blendmapSize)
, mLayerTileSize(layerTileSize)
2015-06-02 23:18:36 +00:00
{
osg::ref_ptr<osg::Material> material (new osg::Material);
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON);
2015-06-02 23:18:36 +00:00
selectTechnique(0);
}
2015-06-02 23:18:36 +00:00
bool Effect::define_techniques()
{
addTechnique(new FixedFunctionTechnique(mLayers, mBlendmaps, mBlendmapSize, mLayerTileSize));
2013-12-19 00:41:36 +00:00
2015-06-02 23:18:36 +00:00
return true;
}
}