You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw-tes3mp/components/terrain/material.cpp

89 lines
3.2 KiB
C++

#include "material.hpp"
10 years ago
#include <osg/Depth>
#include <osg/TexEnvCombine>
#include <osg/Texture2D>
#include <osg/TexMat>
#include <osg/Material>
#include <osg/TexEnvCombine>
namespace Terrain
{
10 years ago
FixedFunctionTechnique::FixedFunctionTechnique(const std::vector<osg::ref_ptr<osg::Texture2D> >& layers,
const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps)
{
10 years ago
bool firstLayer = true;
int i=0;
for (std::vector<osg::ref_ptr<osg::Texture2D> >::const_iterator it = layers.begin(); it != layers.end(); ++it)
{
10 years ago
osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet);
10 years ago
if (!firstLayer)
{
10 years ago
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);
}
10 years ago
int texunit = 0;
if(!firstLayer)
{
10 years ago
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++);
10 years ago
stateset->setTextureAttributeAndModes(texunit, blendmap.get());
10 years ago
// This is to map corner vertices directly to the center of a blendmap texel.
osg::Matrixf texMat;
float scale = (16/(16.f+1.f));
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));
10 years ago
stateset->setTextureAttributeAndModes(texunit, new osg::TexMat(texMat));
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);
10 years ago
++texunit;
}
10 years ago
// Add the actual layer texture multiplied by the alpha map.
osg::ref_ptr<osg::Texture2D> tex = *it;
stateset->setTextureAttributeAndModes(texunit, tex.get());
10 years ago
osg::ref_ptr<osg::TexMat> texMat (new osg::TexMat);
float scale = 16.f;
texMat->setMatrix(osg::Matrix::scale(osg::Vec3f(scale,scale,1.f)));
stateset->setTextureAttributeAndModes(texunit, texMat, osg::StateAttribute::ON);
10 years ago
firstLayer = false;
10 years ago
addPass(stateset);
}
}
10 years ago
Effect::Effect(const std::vector<osg::ref_ptr<osg::Texture2D> > &layers, const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps)
: mLayers(layers)
, mBlendmaps(blendmaps)
{
osg::ref_ptr<osg::Material> material (new osg::Material);
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON);
10 years ago
selectTechnique(0);
}
10 years ago
bool Effect::define_techniques()
{
addTechnique(new FixedFunctionTechnique(mLayers, mBlendmaps));
11 years ago
10 years ago
return true;
}
}