FixedFunctionTechnique::FixedFunctionTechnique(const std::vector<TextureLayer>& layers, const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize) { bool firstLayer = true; int i=0; for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it) { osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); if (!firstLayer) { 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); } int texunit = 0; if(!firstLayer) { osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. osg::Matrixf texMat; float scale = (blendmapScale/(static_cast<float>(blendmapScale)+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)); 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); ++texunit; } // Add the actual layer texture multiplied by the alpha map. osg::ref_ptr<osg::Texture2D> tex = it->mDiffuseMap; stateset->setTextureAttributeAndModes(texunit, tex.get()); osg::ref_ptr<osg::TexMat> texMat (new osg::TexMat); texMat->setMatrix(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f))); stateset->setTextureAttributeAndModes(texunit, texMat, osg::StateAttribute::ON); firstLayer = false; addPass(stateset); } }
ShaderTechnique::ShaderTechnique(Shader::ShaderManager& shaderManager, bool forcePerPixelLighting, bool clampLighting, const std::vector<TextureLayer>& layers, const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize) { bool firstLayer = true; int i=0; for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it) { osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); if (!firstLayer) { 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); } int texunit = 0; stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap); osg::ref_ptr<osg::TexMat> texMat (new osg::TexMat); texMat->setMatrix(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f))); stateset->setTextureAttributeAndModes(texunit, texMat, osg::StateAttribute::ON); stateset->addUniform(new osg::Uniform("diffuseMap", texunit)); if(!firstLayer) { ++texunit; osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. osg::Matrixf texMat; float scale = (blendmapScale/(static_cast<float>(blendmapScale)+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)); stateset->setTextureAttributeAndModes(texunit, new osg::TexMat(texMat)); stateset->addUniform(new osg::Uniform("blendMap", texunit)); } if (it->mNormalMap) { ++texunit; stateset->setTextureAttributeAndModes(texunit, it->mNormalMap); stateset->addUniform(new osg::Uniform("normalMap", texunit)); } Shader::ShaderManager::DefineMap defineMap; defineMap["forcePPL"] = forcePerPixelLighting ? "1" : "0"; defineMap["clamp"] = clampLighting ? "1" : "0"; defineMap["normalMap"] = (it->mNormalMap) ? "1" : "0"; defineMap["blendMap"] = !firstLayer ? "1" : "0"; defineMap["colorMode"] = "2"; defineMap["specularMap"] = it->mSpecular ? "1" : "0"; osg::ref_ptr<osg::Shader> vertexShader = shaderManager.getShader("terrain_vertex.glsl", defineMap, osg::Shader::VERTEX); osg::ref_ptr<osg::Shader> fragmentShader = shaderManager.getShader("terrain_fragment.glsl", defineMap, osg::Shader::FRAGMENT); if (!vertexShader || !fragmentShader) throw std::runtime_error("Unable to create shader"); stateset->setAttributeAndModes(shaderManager.getProgram(vertexShader, fragmentShader)); firstLayer = false; addPass(stateset); } }
void ProcessedShaderMaterial::_setTextureTransforms(const U32 pass) { PROFILE_SCOPE( ProcessedShaderMaterial_SetTextureTransforms ); ShaderConstHandles* handles = _getShaderConstHandles(pass); if (handles->mTexMatSC->isValid()) { MatrixF texMat( true ); mMaterial->updateTimeBasedParams(); F32 waveOffset = _getWaveOffset( pass ); // offset is between 0.0 and 1.0 // handle scroll anim type if( mMaterial->mAnimFlags[pass] & Material::Scroll ) { if( mMaterial->mAnimFlags[pass] & Material::Wave ) { Point3F scrollOffset; scrollOffset.x = mMaterial->mScrollDir[pass].x * waveOffset; scrollOffset.y = mMaterial->mScrollDir[pass].y * waveOffset; scrollOffset.z = 1.0; texMat.setColumn( 3, scrollOffset ); } else { Point3F offset( mMaterial->mScrollOffset[pass].x, mMaterial->mScrollOffset[pass].y, 1.0 ); texMat.setColumn( 3, offset ); } } // handle rotation if( mMaterial->mAnimFlags[pass] & Material::Rotate ) { if( mMaterial->mAnimFlags[pass] & Material::Wave ) { F32 rotPos = waveOffset * M_2PI; texMat.set( EulerF( 0.0, 0.0, rotPos ) ); texMat.setColumn( 3, Point3F( 0.5, 0.5, 0.0 ) ); MatrixF test( true ); test.setColumn( 3, Point3F( mMaterial->mRotPivotOffset[pass].x, mMaterial->mRotPivotOffset[pass].y, 0.0 ) ); texMat.mul( test ); } else { texMat.set( EulerF( 0.0, 0.0, mMaterial->mRotPos[pass] ) ); texMat.setColumn( 3, Point3F( 0.5, 0.5, 0.0 ) ); MatrixF test( true ); test.setColumn( 3, Point3F( mMaterial->mRotPivotOffset[pass].x, mMaterial->mRotPivotOffset[pass].y, 0.0 ) ); texMat.mul( test ); } } // Handle scale + wave offset if( mMaterial->mAnimFlags[pass] & Material::Scale && mMaterial->mAnimFlags[pass] & Material::Wave ) { F32 wOffset = fabs( waveOffset ); texMat.setColumn( 3, Point3F( 0.5, 0.5, 0.0 ) ); MatrixF temp( true ); temp.setRow( 0, Point3F( wOffset, 0.0, 0.0 ) ); temp.setRow( 1, Point3F( 0.0, wOffset, 0.0 ) ); temp.setRow( 2, Point3F( 0.0, 0.0, wOffset ) ); temp.setColumn( 3, Point3F( -wOffset * 0.5, -wOffset * 0.5, 0.0 ) ); texMat.mul( temp ); } // handle sequence if( mMaterial->mAnimFlags[pass] & Material::Sequence ) { U32 frameNum = (U32)(MATMGR->getTotalTime() * mMaterial->mSeqFramePerSec[pass]); F32 offset = frameNum * mMaterial->mSeqSegSize[pass]; if ( mMaterial->mAnimFlags[pass] & Material::Scale ) texMat.scale( Point3F( mMaterial->mSeqSegSize[pass], 1.0f, 1.0f ) ); Point3F texOffset = texMat.getPosition(); texOffset.x += offset; texMat.setPosition( texOffset ); } GFXShaderConstBuffer* shaderConsts = _getShaderConstBuffer(pass); shaderConsts->setSafe(handles->mTexMatSC, texMat); } }