#include "material.hpp" #include #include #include #include #include #include #include #include #include namespace Terrain { osg::ref_ptr getBlendmapTexMat(int blendmapScale) { static std::map > texMatMap; osg::ref_ptr texMat = texMatMap[blendmapScale]; if (!texMat) { osg::Matrixf matrix; float scale = (blendmapScale/(static_cast(blendmapScale)+1.f)); matrix.preMultTranslate(osg::Vec3f(0.5f, 0.5f, 0.f)); matrix.preMultScale(osg::Vec3f(scale, scale, 1.f)); matrix.preMultTranslate(osg::Vec3f(-0.5f, -0.5f, 0.f)); // We need to nudge the blendmap to look like vanilla. // This causes visible seams unless the blendmap's resolution is doubled, but Vanilla also doubles the blendmap, apparently. matrix.preMultTranslate(osg::Vec3f(1.0f/blendmapScale/4.0f, 1.0f/blendmapScale/4.0f, 0.f)); texMat = new osg::TexMat(matrix); texMatMap[blendmapScale] = texMat; } return texMat; } osg::ref_ptr getLayerTexMat(float layerTileSize) { static std::map > texMatMap; osg::ref_ptr texMat = texMatMap[layerTileSize]; if (!texMat) { texMat = new osg::TexMat(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f))); texMatMap[layerTileSize] = texMat; } return texMat; } osg::ref_ptr getEqualDepth() { static osg::ref_ptr depth; if (!depth) { depth = new osg::Depth; depth->setFunction(osg::Depth::EQUAL); } return depth; } osg::ref_ptr getLequalDepth() { static osg::ref_ptr depth; if (!depth) { depth = new osg::Depth; depth->setFunction(osg::Depth::LEQUAL); } return depth; } std::vector > createPasses(bool useShaders, bool forcePerPixelLighting, bool clampLighting, Shader::ShaderManager* shaderManager, const std::vector &layers, const std::vector > &blendmaps, int blendmapScale, float layerTileSize) { std::vector > passes; unsigned int blendmapIndex = 0; unsigned int passIndex = 0; for (std::vector::const_iterator it = layers.begin(); it != layers.end(); ++it) { bool firstLayer = (it == layers.begin()); osg::ref_ptr stateset (new osg::StateSet); if (!firstLayer) { static osg::ref_ptr blendFunc; if (!blendFunc) { blendFunc= new osg::BlendFunc(); blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE); } stateset->setMode(GL_BLEND, osg::StateAttribute::ON); stateset->setAttributeAndModes(blendFunc, osg::StateAttribute::ON); stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON); } // disable fog if we're the first layer of several - supposed to be completely black if (firstLayer && blendmaps.size() > 0) { osg::ref_ptr fog (new osg::Fog); fog->setStart(10000000); fog->setEnd(10000000); stateset->setAttributeAndModes(fog, osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); stateset->setAttributeAndModes(getLequalDepth(), osg::StateAttribute::ON); } int texunit = 0; if (useShaders) { stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap); if (layerTileSize != 1.f) stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); stateset->addUniform(new osg::Uniform("diffuseMap", texunit)); if(!firstLayer) { ++texunit; osg::ref_ptr blendmap = blendmaps.at(blendmapIndex++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); 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"; defineMap["parallax"] = (it->mNormalMap && it->mParallax) ? "1" : "0"; osg::ref_ptr vertexShader = shaderManager->getShader("terrain_vertex.glsl", defineMap, osg::Shader::VERTEX); osg::ref_ptr fragmentShader = shaderManager->getShader("terrain_fragment.glsl", defineMap, osg::Shader::FRAGMENT); if (!vertexShader || !fragmentShader) { // Try again without shader. Error already logged by above return createPasses(false, forcePerPixelLighting, clampLighting, shaderManager, layers, blendmaps, blendmapScale, layerTileSize); } stateset->setAttributeAndModes(shaderManager->getProgram(vertexShader, fragmentShader)); } else { if(!firstLayer) { osg::ref_ptr blendmap = blendmaps.at(blendmapIndex++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); static osg::ref_ptr texEnvCombine; if (!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 tex = it->mDiffuseMap; stateset->setTextureAttributeAndModes(texunit, tex.get()); if (layerTileSize != 1.f) stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); } stateset->setRenderBinDetails(passIndex++, "RenderBin"); passes.push_back(stateset); } return passes; } }