bool Technique::BeginLoad(Deserializer& source) { passes_.Clear(); SetMemoryUse(sizeof(Technique)); SharedPtr<XMLFile> xml(new XMLFile(context_)); if (!xml->Load(source)) return false; XMLElement rootElem = xml->GetRoot(); if (rootElem.HasAttribute("desktop")) isDesktop_ = rootElem.GetBool("desktop"); String globalVS = rootElem.GetAttribute("vs"); String globalPS = rootElem.GetAttribute("ps"); String globalVSDefines = rootElem.GetAttribute("vsdefines"); String globalPSDefines = rootElem.GetAttribute("psdefines"); // End with space so that the pass-specific defines can be appended if (!globalVSDefines.Empty()) globalVSDefines += ' '; if (!globalPSDefines.Empty()) globalPSDefines += ' '; bool globalAlphaMask = false; if (rootElem.HasAttribute("alphamask")) globalAlphaMask = rootElem.GetBool("alphamask"); XMLElement passElem = rootElem.GetChild("pass"); while (passElem) { if (passElem.HasAttribute("name")) { Pass* newPass = CreatePass(passElem.GetAttribute("name")); if (passElem.HasAttribute("desktop")) newPass->SetIsDesktop(passElem.GetBool("desktop")); // Append global defines only when pass does not redefine the shader if (passElem.HasAttribute("vs")) { newPass->SetVertexShader(passElem.GetAttribute("vs")); newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines")); } else { newPass->SetVertexShader(globalVS); newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines")); } if (passElem.HasAttribute("ps")) { newPass->SetPixelShader(passElem.GetAttribute("ps")); newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines")); } else { newPass->SetPixelShader(globalPS); newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines")); } if (passElem.HasAttribute("lighting")) { String lighting = passElem.GetAttributeLower("lighting"); newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames, LIGHTING_UNLIT)); } if (passElem.HasAttribute("blend")) { String blend = passElem.GetAttributeLower("blend"); newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE)); } if (passElem.HasAttribute("depthtest")) { String depthTest = passElem.GetAttributeLower("depthtest"); if (depthTest == "false") newPass->SetDepthTestMode(CMP_ALWAYS); else newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS)); } if (passElem.HasAttribute("depthwrite")) newPass->SetDepthWrite(passElem.GetBool("depthwrite")); if (passElem.HasAttribute("alphamask")) newPass->SetAlphaMask(passElem.GetBool("alphamask")); else newPass->SetAlphaMask(globalAlphaMask); } else LOGERROR("Missing pass name"); passElem = passElem.GetNext("pass"); } return true; }
void RenderPathCommand::Load(const XMLElement& element) { type_ = (RenderCommandType)GetStringListIndex(element.GetAttributeLower("type").CString(), commandTypeNames, CMD_NONE); tag_ = element.GetAttribute("tag"); if (element.HasAttribute("enabled")) enabled_ = element.GetBool("enabled"); if (element.HasAttribute("metadata")) metadata_ = element.GetAttribute("metadata"); switch (type_) { case CMD_CLEAR: if (element.HasAttribute("color")) { clearFlags_ |= CLEAR_COLOR; if (element.GetAttributeLower("color") == "fog") useFogColor_ = true; else clearColor_ = element.GetColor("color"); } if (element.HasAttribute("depth")) { clearFlags_ |= CLEAR_DEPTH; clearDepth_ = element.GetFloat("depth"); } if (element.HasAttribute("stencil")) { clearFlags_ |= CLEAR_STENCIL; clearStencil_ = (unsigned)element.GetInt("stencil"); } break; case CMD_SCENEPASS: pass_ = element.GetAttribute("pass"); sortMode_ = (RenderCommandSortMode)GetStringListIndex(element.GetAttributeLower("sort").CString(), sortModeNames, SORT_FRONTTOBACK); if (element.HasAttribute("marktostencil")) markToStencil_ = element.GetBool("marktostencil"); if (element.HasAttribute("vertexlights")) vertexLights_ = element.GetBool("vertexlights"); break; case CMD_FORWARDLIGHTS: pass_ = element.GetAttribute("pass"); if (element.HasAttribute("uselitbase")) useLitBase_ = element.GetBool("uselitbase"); break; case CMD_LIGHTVOLUMES: case CMD_QUAD: vertexShaderName_ = element.GetAttribute("vs"); pixelShaderName_ = element.GetAttribute("ps"); if (type_ == CMD_QUAD && element.HasAttribute("blend")) { String blend = element.GetAttributeLower("blend"); blendMode_ = ((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE)); } break; case CMD_SENDEVENT: eventName_ = element.GetAttribute("name"); break; default: break; } // By default use 1 output, which is the viewport outputs_.Resize(1); outputs_[0] = MakePair(String("viewport"), FACE_POSITIVE_X); if (element.HasAttribute("output")) outputs_[0].first_ = element.GetAttribute("output"); if (element.HasAttribute("face")) outputs_[0].second_ = (CubeMapFace)element.GetInt("face"); if (element.HasAttribute("depthstencil")) depthStencilName_ = element.GetAttribute("depthstencil"); // Check for defining multiple outputs XMLElement outputElem = element.GetChild("output"); while (outputElem) { unsigned index = (unsigned)outputElem.GetInt("index"); if (index < MAX_RENDERTARGETS) { if (index >= outputs_.Size()) outputs_.Resize(index + 1); outputs_[index].first_ = outputElem.GetAttribute("name"); outputs_[index].second_ = outputElem.HasAttribute("face") ? (CubeMapFace)outputElem.GetInt("face") : FACE_POSITIVE_X; } outputElem = outputElem.GetNext("output"); } // Shader compile flags & parameters vertexShaderDefines_ = element.GetAttribute("vsdefines"); pixelShaderDefines_ = element.GetAttribute("psdefines"); XMLElement parameterElem = element.GetChild("parameter"); while (parameterElem) { String name = parameterElem.GetAttribute("name"); shaderParameters_[name] = Material::ParseShaderParameterValue(parameterElem.GetAttribute("value")); parameterElem = parameterElem.GetNext("parameter"); } // Texture bindings XMLElement textureElem = element.GetChild("texture"); while (textureElem) { TextureUnit unit = TU_DIFFUSE; if (textureElem.HasAttribute("unit")) unit = ParseTextureUnitName(textureElem.GetAttribute("unit")); if (unit < MAX_TEXTURE_UNITS) { String name = textureElem.GetAttribute("name"); textureNames_[unit] = name; } textureElem = textureElem.GetNext("texture"); } }
std::shared_ptr<XMLElement> JointElement::childElementForName(const std::string& name) { // As an alternative for simple elements, instead of creating other classes, // I implement the simple functions of the generic element if (name == "origin") { return std::make_shared<OriginElement>(m_jointFrame); } else if (name == "parent" || name == "child") { std::string& ref = (name == "parent") ? m_parentLink : m_childLink; XMLElement* element = new XMLElement(name); element->setAttributeCallback([&ref](const std::unordered_map<std::string, std::shared_ptr<XMLAttribute>>& attributes) { auto linkName = attributes.find("link"); if (linkName != attributes.end()) { ref = linkName->second->value(); } return true; }); return std::shared_ptr<XMLElement>(element); } else if (name == "axis") { XMLElement* element = new XMLElement(name); element->setAttributeCallback([this](const std::unordered_map<std::string, std::shared_ptr<XMLAttribute>>& attributes) { auto xyz = attributes.find("xyz"); if (xyz != attributes.end()) { Vector3 direction; if (vector3FromString(xyz->second->value(), direction)) { m_axis = Axis(Direction(direction(0), direction(1), direction(2)), Position(0.0, 0.0, 0.0)); } } return true; }); return std::shared_ptr<XMLElement>(element); } else if (name == "limit") { m_limits = std::make_shared<Limits>(); m_limits->positionLower = .0; m_limits->positionUpper = .0; // TODO: check how the defaults/required works XMLElement* element = new XMLElement(name); element->setAttributeCallback([this](const std::unordered_map<std::string, std::shared_ptr<XMLAttribute>>& attributes) { auto found = attributes.find("lower"); if (found != attributes.end()) { double value = 0; if (stringToDoubleWithClassicLocale(found->second->value(), value)) { m_limits->positionLower = value; } } found = attributes.find("upper"); if (found != attributes.end()) { double value = 0; if (stringToDoubleWithClassicLocale(found->second->value(), value)) { m_limits->positionUpper = value; } } found = attributes.find("effort"); if (found != attributes.end()) { double value = 0; if (stringToDoubleWithClassicLocale(found->second->value(), value)) { m_limits->effort = value; } } found = attributes.find("velocity"); if (found != attributes.end()) { double value = 0; if (stringToDoubleWithClassicLocale(found->second->value(), value)) { m_limits->velocity = value; } } // TODO: check if we need to impose restrictions on the joint type return true; }); return std::shared_ptr<XMLElement>(element); } return std::make_shared<XMLElement>(name); }