Ogre::Technique* GBufferSchemeHandler::handleSchemeNotFound(unsigned short schemeIndex, const Ogre::String& schemeName, Ogre::Material* originalMaterial, unsigned short lodIndex, const Ogre::Renderable* rend) { Ogre::MaterialManager& matMgr = Ogre::MaterialManager::getSingleton(); Ogre::String curSchemeName = matMgr.getActiveScheme(); matMgr.setActiveScheme(Ogre::MaterialManager::DEFAULT_SCHEME_NAME); Ogre::Technique* originalTechnique = originalMaterial->getBestTechnique(lodIndex, rend); matMgr.setActiveScheme(curSchemeName); Ogre::Technique* gBufferTech = originalMaterial->createTechnique(); gBufferTech->removeAllPasses(); gBufferTech->setSchemeName(schemeName); Ogre::Technique* noGBufferTech = originalMaterial->createTechnique(); noGBufferTech->removeAllPasses(); noGBufferTech->setSchemeName("NoGBuffer"); for (unsigned short i=0; i<originalTechnique->getNumPasses(); i++) { Ogre::Pass* originalPass = originalTechnique->getPass(i); PassProperties props = inspectPass(originalPass, lodIndex, rend); if (!props.isDeferred) { //Just copy the technique so it gets rendered regularly Ogre::Pass* clonePass = noGBufferTech->createPass(); *clonePass = *originalPass; continue; } Ogre::Pass* newPass = gBufferTech->createPass(); MaterialGenerator::Perm perm = getPermutation(props); const Ogre::MaterialPtr& templateMat = mMaterialGenerator.getMaterial(perm); //We assume that the GBuffer technique contains only one pass. But its true. *newPass = *(templateMat->getTechnique(0)->getPass(0)); fillPass(newPass, originalPass, props); } return gBufferTech; }
Ogre::MaterialPtr MaterialGenerator::create(bool renderCompositeMap, bool displayCompositeMap) { assert(!renderCompositeMap || !displayCompositeMap); static int count = 0; std::stringstream name; name << "terrain/mat" << count++; if (!mShaders) { Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create(name.str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Ogre::Technique* technique = mat->getTechnique(0); technique->removeAllPasses(); if (displayCompositeMap) { Ogre::Pass* pass = technique->createPass(); pass->setVertexColourTracking(Ogre::TVC_AMBIENT|Ogre::TVC_DIFFUSE); pass->createTextureUnitState(mCompositeMap)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); } else { assert(mLayerList.size() == mBlendmapList.size()+1); std::vector<Ogre::TexturePtr>::iterator blend = mBlendmapList.begin(); for (std::vector<LayerInfo>::iterator layer = mLayerList.begin(); layer != mLayerList.end(); ++layer) { Ogre::Pass* pass = technique->createPass(); pass->setLightingEnabled(false); pass->setVertexColourTracking(Ogre::TVC_NONE); // TODO: How to handle fog? pass->setFog(true, Ogre::FOG_NONE); bool first = (layer == mLayerList.begin()); Ogre::TextureUnitState* tus; if (!first) { pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA); pass->setDepthFunction(Ogre::CMPF_EQUAL); tus = pass->createTextureUnitState((*blend)->getName()); tus->setAlphaOperation(Ogre::LBX_BLEND_TEXTURE_ALPHA, Ogre::LBS_TEXTURE, Ogre::LBS_TEXTURE); tus->setColourOperationEx(Ogre::LBX_BLEND_DIFFUSE_ALPHA, Ogre::LBS_TEXTURE, Ogre::LBS_TEXTURE); tus->setIsAlpha(true); tus->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); float scale = (16/(16.f+1.f)); tus->setTextureScale(1.f/scale,1.f/scale); } // Add the actual layer texture on top of the alpha map. tus = pass->createTextureUnitState(layer->mDiffuseMap); if (!first) tus->setColourOperationEx(Ogre::LBX_BLEND_DIFFUSE_ALPHA, Ogre::LBS_TEXTURE, Ogre::LBS_CURRENT); tus->setTextureScale(1/16.f,1/16.f); if (!first) ++blend; } if (!renderCompositeMap) { Ogre::Pass* lightingPass = technique->createPass(); lightingPass->setSceneBlending(Ogre::SBT_MODULATE); lightingPass->setVertexColourTracking(Ogre::TVC_AMBIENT|Ogre::TVC_DIFFUSE); lightingPass->setFog(true, Ogre::FOG_NONE); } } return mat; } #if TERRAIN_USE_SHADER else { sh::MaterialInstance* material = sh::Factory::getInstance().createMaterialInstance (name.str()); material->setProperty ("allow_fixed_function", sh::makeProperty<sh::BooleanValue>(new sh::BooleanValue(false))); if (displayCompositeMap) { sh::MaterialInstancePass* p = material->createPass (); p->setProperty ("vertex_program", sh::makeProperty<sh::StringValue>(new sh::StringValue("terrain_vertex"))); p->setProperty ("fragment_program", sh::makeProperty<sh::StringValue>(new sh::StringValue("terrain_fragment"))); p->mShaderProperties.setProperty ("is_first_pass", sh::makeProperty(new sh::BooleanValue(true))); p->mShaderProperties.setProperty ("render_composite_map", sh::makeProperty(new sh::BooleanValue(false))); p->mShaderProperties.setProperty ("display_composite_map", sh::makeProperty(new sh::BooleanValue(true))); p->mShaderProperties.setProperty ("num_layers", sh::makeProperty (new sh::StringValue("0"))); p->mShaderProperties.setProperty ("num_blendmaps", sh::makeProperty (new sh::StringValue("0"))); p->mShaderProperties.setProperty ("normal_map_enabled", sh::makeProperty (new sh::BooleanValue(false))); p->mShaderProperties.setProperty ("parallax_enabled", sh::makeProperty (new sh::BooleanValue(false))); p->mShaderProperties.setProperty ("normal_maps", sh::makeProperty (new sh::IntValue(0))); sh::MaterialInstanceTextureUnit* tex = p->createTextureUnit ("compositeMap"); tex->setProperty ("direct_texture", sh::makeProperty (new sh::StringValue(mCompositeMap))); tex->setProperty ("tex_address_mode", sh::makeProperty (new sh::StringValue("clamp"))); // shadow. TODO: repeated, put in function if (mShadows) { for (int i = 0; i < (mSplitShadows ? 3 : 1); ++i) { sh::MaterialInstanceTextureUnit* shadowTex = p->createTextureUnit ("shadowMap" + Ogre::StringConverter::toString(i)); shadowTex->setProperty ("content_type", sh::makeProperty<sh::StringValue> (new sh::StringValue("shadow"))); } } p->mShaderProperties.setProperty ("shadowtexture_offset", sh::makeProperty (new sh::StringValue( Ogre::StringConverter::toString(1)))); p->mShaderProperties.setProperty ("pass_index", sh::makeProperty(new sh::IntValue(0))); } else { bool shadows = mShadows && !renderCompositeMap; int layerOffset = 0; while (layerOffset < (int)mLayerList.size()) { int blendmapOffset = (layerOffset == 0) ? 1 : 0; // the first layer of the first pass is the base layer and does not need a blend map // Check how many layers we can fit in this pass int numLayersInThisPass = 0; int numBlendTextures = 0; std::vector<std::string> blendTextures; int remainingTextureUnits = OGRE_MAX_TEXTURE_LAYERS; if (shadows) remainingTextureUnits -= (mSplitShadows ? 3 : 1); while (remainingTextureUnits && layerOffset + numLayersInThisPass < (int)mLayerList.size()) { int layerIndex = numLayersInThisPass + layerOffset; int neededTextureUnits=0; int neededBlendTextures=0; if (layerIndex != 0) { std::string blendTextureName = mBlendmapList[getBlendmapIndexForLayer(layerIndex)]->getName(); if (std::find(blendTextures.begin(), blendTextures.end(), blendTextureName) == blendTextures.end()) { blendTextures.push_back(blendTextureName); ++neededBlendTextures; ++neededTextureUnits; // blend texture } } ++neededTextureUnits; // layer texture // Check if this layer has a normal map if (mNormalMapping && !mLayerList[layerIndex].mNormalMap.empty() && !renderCompositeMap) ++neededTextureUnits; // normal map if (neededTextureUnits <= remainingTextureUnits) { // We can fit another! remainingTextureUnits -= neededTextureUnits; numBlendTextures += neededBlendTextures; ++numLayersInThisPass; } else break; // We're full } sh::MaterialInstancePass* p = material->createPass (); p->setProperty ("vertex_program", sh::makeProperty<sh::StringValue>(new sh::StringValue("terrain_vertex"))); p->setProperty ("fragment_program", sh::makeProperty<sh::StringValue>(new sh::StringValue("terrain_fragment"))); if (layerOffset != 0) { p->setProperty ("scene_blend", sh::makeProperty(new sh::StringValue("alpha_blend"))); // Only write if depth is equal to the depth value written by the previous pass. p->setProperty ("depth_func", sh::makeProperty(new sh::StringValue("equal"))); } p->mShaderProperties.setProperty ("render_composite_map", sh::makeProperty(new sh::BooleanValue(renderCompositeMap))); p->mShaderProperties.setProperty ("display_composite_map", sh::makeProperty(new sh::BooleanValue(displayCompositeMap))); p->mShaderProperties.setProperty ("num_layers", sh::makeProperty (new sh::StringValue(Ogre::StringConverter::toString(numLayersInThisPass)))); p->mShaderProperties.setProperty ("num_blendmaps", sh::makeProperty (new sh::StringValue(Ogre::StringConverter::toString(numBlendTextures)))); p->mShaderProperties.setProperty ("normal_map_enabled", sh::makeProperty (new sh::BooleanValue(false))); // blend maps // the index of the first blend map used in this pass int blendmapStart; if (mLayerList.size() == 1) // special case. if there's only one layer, we don't need blend maps at all blendmapStart = 0; else blendmapStart = getBlendmapIndexForLayer(layerOffset+blendmapOffset); for (int i = 0; i < numBlendTextures; ++i) { sh::MaterialInstanceTextureUnit* blendTex = p->createTextureUnit ("blendMap" + Ogre::StringConverter::toString(i)); blendTex->setProperty ("direct_texture", sh::makeProperty (new sh::StringValue(mBlendmapList[blendmapStart+i]->getName()))); blendTex->setProperty ("tex_address_mode", sh::makeProperty (new sh::StringValue("clamp"))); } // layer maps bool anyNormalMaps = false; bool anyParallax = false; size_t normalMaps = 0; for (int i = 0; i < numLayersInThisPass; ++i) { const LayerInfo& layer = mLayerList[layerOffset+i]; // diffuse map sh::MaterialInstanceTextureUnit* diffuseTex = p->createTextureUnit ("diffuseMap" + Ogre::StringConverter::toString(i)); diffuseTex->setProperty ("direct_texture", sh::makeProperty (new sh::StringValue(layer.mDiffuseMap))); // normal map (optional) bool useNormalMap = mNormalMapping && !mLayerList[layerOffset+i].mNormalMap.empty() && !renderCompositeMap; bool useParallax = useNormalMap && mParallaxMapping && layer.mParallax; bool useSpecular = layer.mSpecular; if (useNormalMap) { anyNormalMaps = true; anyParallax = anyParallax || useParallax; sh::MaterialInstanceTextureUnit* normalTex = p->createTextureUnit ("normalMap" + Ogre::StringConverter::toString(i)); normalTex->setProperty ("direct_texture", sh::makeProperty (new sh::StringValue(layer.mNormalMap))); } p->mShaderProperties.setProperty ("use_normal_map_" + Ogre::StringConverter::toString(i), sh::makeProperty (new sh::BooleanValue(useNormalMap))); p->mShaderProperties.setProperty ("use_parallax_" + Ogre::StringConverter::toString(i), sh::makeProperty (new sh::BooleanValue(useParallax))); p->mShaderProperties.setProperty ("use_specular_" + Ogre::StringConverter::toString(i), sh::makeProperty (new sh::BooleanValue(useSpecular))); boost::hash_combine(normalMaps, useNormalMap); boost::hash_combine(normalMaps, useNormalMap && layer.mParallax); boost::hash_combine(normalMaps, useSpecular); if (i+layerOffset > 0) { int blendTextureIndex = getBlendmapIndexForLayer(layerOffset+i); std::string blendTextureComponent = getBlendmapComponentForLayer(layerOffset+i); p->mShaderProperties.setProperty ("blendmap_component_" + Ogre::StringConverter::toString(i), sh::makeProperty (new sh::StringValue(Ogre::StringConverter::toString(blendTextureIndex-blendmapStart) + "." + blendTextureComponent))); } else { // just to make it shut up about blendmap_component_0 not existing in the first pass. // it might be retrieved, but will never survive the preprocessing step. p->mShaderProperties.setProperty ("blendmap_component_" + Ogre::StringConverter::toString(i), sh::makeProperty (new sh::StringValue(""))); } } p->mShaderProperties.setProperty ("normal_map_enabled", sh::makeProperty (new sh::BooleanValue(anyNormalMaps))); p->mShaderProperties.setProperty ("parallax_enabled", sh::makeProperty (new sh::BooleanValue(anyParallax))); // Since the permutation handler can't handle dynamic property names, // combine normal map settings for all layers into one value p->mShaderProperties.setProperty ("normal_maps", sh::makeProperty (new sh::IntValue(normalMaps))); // shadow if (shadows) { for (int i = 0; i < (mSplitShadows ? 3 : 1); ++i) { sh::MaterialInstanceTextureUnit* shadowTex = p->createTextureUnit ("shadowMap" + Ogre::StringConverter::toString(i)); shadowTex->setProperty ("content_type", sh::makeProperty<sh::StringValue> (new sh::StringValue("shadow"))); } } p->mShaderProperties.setProperty ("shadowtexture_offset", sh::makeProperty (new sh::StringValue( Ogre::StringConverter::toString(numBlendTextures + numLayersInThisPass)))); // Make sure the pass index is fed to the permutation handler, because blendmap components may be different p->mShaderProperties.setProperty ("pass_index", sh::makeProperty(new sh::IntValue(layerOffset))); assert ((int)p->mTexUnits.size() == OGRE_MAX_TEXTURE_LAYERS - remainingTextureUnits); layerOffset += numLayersInThisPass; } } } #endif return Ogre::MaterialManager::getSingleton().getByName(name.str()); }
/**************************************************************************** ** ** Copyright (C) 2014 ** ** This file is generated by the Magus toolkit ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ****************************************************************************/ // Include #include "mainwindow.h" #include "Editor_dockwidget.h" #include "constants.h" #include "sme_asset_material.h" #include "sme_node_material.h" #include "sme_asset_technique.h" #include "sme_node_technique.h" #include "sme_asset_pass.h" #include "sme_node_pass.h" #include "sme_asset_texture_unit.h" #include "sme_node_texture_unit.h" //****************************************************************************/ EditorDockWidget::EditorDockWidget(QString title, MainWindow* parent, Qt::WindowFlags flags) : QDockWidget (title, parent, flags), mParent(parent) { mInnerMain = new QMainWindow(); setWidget(mInnerMain); // Perform standard functions createActions(); createMenus(); createToolBars(); // Create the node editor widget. mNodeEditor = new Magus::QtNodeEditor(this); mNodeEditor->setMenuSelectionToCompoundEnabled(false); // Enabling this makes it a bit more complicated mNodeEditor->setMenuExpandCompoundsEnabled(false); // No compounds are used connect(mNodeEditor, SIGNAL(nodeRemoved(QtNode*)), this, SLOT(nodeDeleted())); connect(mNodeEditor, SIGNAL(nodeSelected(QtNode*)), this, SLOT(nodeSelected(QtNode*))); mInnerMain->setCentralWidget(mNodeEditor); mMaterialNode = 0; } //****************************************************************************/ EditorDockWidget::~EditorDockWidget(void) { } //****************************************************************************/ void EditorDockWidget::createActions(void) { mMaterialHToolbarAction = new QAction(QIcon(ICON_MATERIAL), QString("Add a material node to the editor"), this); connect(mMaterialHToolbarAction, SIGNAL(triggered()), this, SLOT(doMaterialHToolbarAction())); mTechniqueHToolbarAction = new QAction(QIcon(ICON_TECHNIQUE), QString("Add a technique node to the editor"), this); connect(mTechniqueHToolbarAction, SIGNAL(triggered()), this, SLOT(doTechniqueHToolbarAction())); mPassHToolbarAction = new QAction(QIcon(ICON_PASS), QString("Add a pass node to the editor"), this); connect(mPassHToolbarAction, SIGNAL(triggered()), this, SLOT(doPassHToolbarAction())); mTextureHToolbarAction = new QAction(QIcon(ICON_TEXTURE), QString("Add a texture unit node to the editor"), this); connect(mTextureHToolbarAction, SIGNAL(triggered()), this, SLOT(doTextureHToolbarAction())); mCogHToolbarAction = new QAction(QIcon(ICON_COG), QString("Generate material"), this); connect(mCogHToolbarAction, SIGNAL(triggered()), this, SLOT(doCogHToolbarAction())); } //****************************************************************************/ void EditorDockWidget::createMenus(void) { } //****************************************************************************/ void EditorDockWidget::createToolBars(void) { mHToolBar = new QToolBar(); mInnerMain->addToolBar(Qt::TopToolBarArea, mHToolBar); mHToolBar->setMinimumHeight(TB_ICON_AND_SPACING); mHToolBar->setMinimumWidth(1200); mHToolBar->addAction(mMaterialHToolbarAction); mHToolBar->addAction(mTechniqueHToolbarAction); mHToolBar->addAction(mPassHToolbarAction); mHToolBar->addAction(mTextureHToolbarAction); mHToolBar->addAction(mCogHToolbarAction); } //****************************************************************************/ void EditorDockWidget::doMaterialHToolbarAction(void) { // Add a material node; only 1 is allowed if (!mMaterialNode) { mMaterialNode = new Magus::QtNodeMaterial(NODE_TITLE_MATERIAL); mNodeEditor->addNode(mMaterialNode); } } //****************************************************************************/ void EditorDockWidget::doTechniqueHToolbarAction(void) { // Add a technique node Magus::QtNodeTechnique* techniqueNode = new Magus::QtNodeTechnique(NODE_TITLE_TECHNIQUE); mNodeEditor->addNode(techniqueNode); } //****************************************************************************/ void EditorDockWidget::doPassHToolbarAction(void) { // Add a pass node Magus::QtNodePass* passNode = new Magus::QtNodePass(NODE_TITLE_PASS); mNodeEditor->addNode(passNode); } //****************************************************************************/ void EditorDockWidget::doTextureHToolbarAction(void) { // Add a texture unit node Magus::QtNodeTextureUnit* textureUnitNode = new Magus::QtNodeTextureUnit(NODE_TITLE_TEXTURE_UNIT); mNodeEditor->addNode(textureUnitNode); } //****************************************************************************/ void EditorDockWidget::doCogHToolbarAction(void) { if (!mMaterialNode) return; if (mMaterialNode->getMaterialName().isEmpty()) return; // ---------------------------------------- Create a material ---------------------------------------- Ogre::LogManager* logManager = Ogre::LogManager::getSingletonPtr(); Ogre::MaterialManager* materialManager = Ogre::MaterialManager::getSingletonPtr(); Ogre::String materialName = mMaterialNode->getMaterialName().toStdString(); // Convert to std format logManager->logMessage("SME: create Ogre material: " + materialName); Ogre::MaterialPtr material = materialManager->create(materialName, "General"); // Remark: Sceneblending is done for each pass individually, although it is defined on material level // ---------------------------------------- Add the technique ---------------------------------------- Magus::QtNode* node = mMaterialNode->getNodeConnectedToPort(PORT_TECHNIQUE_OUT); if (!node) { logManager->logMessage("SME: No technique node available"); return; } Magus::QtNodeTechnique* techniqueNode = static_cast<Magus::QtNodeTechnique*>(node); material->removeAllTechniques(); Ogre::Technique* technique = material->createTechnique(); technique->removeAllPasses(); logManager->logMessage("SME: Technique created" + Ogre::StringConverter::toString(material->getNumTechniques())); // ---------------------------------------- Add the passes ---------------------------------------- Magus::QtNodePass* passNode; Magus::QtNodeTextureUnit* textureUnitNode; Ogre::Pass* pass; Ogre::TextureUnitState* textureUnit; for (unsigned int i = 1; i < 5; ++i) { node = techniqueNode->getNodeConnectedToPort(PORT_PASS_OUT, i); // node with the same name if (node) { passNode = static_cast<Magus::QtNodePass*>(node); pass = technique->createPass(); pass->removeAllTextureUnitStates(); logManager->logMessage("SME: Pass on port nr. " + Ogre::StringConverter::toString(i) + " created"); propagatePassNodeData(passNode, pass); // ---------------------------------------- Add the texture units ---------------------------------------- for (unsigned int j = 1; j < 9; ++j) { node = passNode->getNodeConnectedToPort(PORT_TEXTURE_OUT, j); if (node) { logManager->logMessage("SME: Texture unit on port nr. " + Ogre::StringConverter::toString(j) + " of Pass port nr. " + Ogre::StringConverter::toString(i) + " created"); textureUnitNode = static_cast<Magus::QtNodeTextureUnit*>(node); textureUnit = pass->createTextureUnitState(); propagateTextureUnitNodeData(textureUnitNode, textureUnit); } } } } // Assign the material to the ogrehead material->compile(); material->load(); mParent->getOgreManager()->getOgreWidget(1)->mEntity->setMaterial(material); }