2012-04-03 20:44:46 +02:00
|
|
|
/* Copyright (C) 2012 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-09-23 06:00:56 +02:00
|
|
|
#include "precompiled.h"
|
2012-04-08 17:55:06 +02:00
|
|
|
|
|
|
|
#include "MaterialManager.h"
|
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/ogl.h"
|
2012-04-08 17:55:06 +02:00
|
|
|
#include "maths/Vector4D.h"
|
2010-07-04 12:15:53 +02:00
|
|
|
#include "ps/Filesystem.h"
|
2012-04-10 23:03:22 +02:00
|
|
|
#include "ps/PreprocessorWrapper.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2012-04-10 23:03:22 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2012-04-03 20:44:46 +02:00
|
|
|
CMaterial CMaterialManager::LoadMaterial(const VfsPath& pathname)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2012-04-03 20:44:46 +02:00
|
|
|
if (pathname.empty())
|
|
|
|
return CMaterial();
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2012-04-03 20:44:46 +02:00
|
|
|
std::map<VfsPath, CMaterial>::iterator iter = m_Materials.find(pathname);
|
|
|
|
if (iter != m_Materials.end())
|
|
|
|
return iter->second;
|
2004-09-23 07:10:03 +02:00
|
|
|
|
2004-09-23 06:00:56 +02:00
|
|
|
CXeromyces xeroFile;
|
2012-04-03 20:44:46 +02:00
|
|
|
if (xeroFile.Load(g_VFS, pathname) != PSRETURN_OK)
|
|
|
|
return CMaterial();
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
#define EL(x) int el_##x = xeroFile.GetElementID(#x)
|
|
|
|
#define AT(x) int at_##x = xeroFile.GetAttributeID(#x)
|
2012-04-03 20:44:46 +02:00
|
|
|
EL(alpha_blending);
|
2012-04-10 23:03:22 +02:00
|
|
|
EL(alternative);
|
2012-04-03 20:44:46 +02:00
|
|
|
EL(define);
|
|
|
|
EL(shader);
|
2012-04-08 17:55:06 +02:00
|
|
|
EL(uniform);
|
2012-04-03 20:44:46 +02:00
|
|
|
AT(effect);
|
2012-04-10 23:03:22 +02:00
|
|
|
AT(if);
|
|
|
|
AT(material);
|
2012-04-03 20:44:46 +02:00
|
|
|
AT(name);
|
|
|
|
AT(value);
|
2004-09-23 06:00:56 +02:00
|
|
|
#undef AT
|
|
|
|
#undef EL
|
|
|
|
|
2012-04-03 20:44:46 +02:00
|
|
|
CMaterial material;
|
2004-09-23 06:00:56 +02:00
|
|
|
|
2012-04-03 20:44:46 +02:00
|
|
|
XMBElement root = xeroFile.GetRoot();
|
|
|
|
XMBElementList childNodes = root.GetChildNodes();
|
|
|
|
|
2012-04-10 23:03:22 +02:00
|
|
|
CPreprocessorWrapper preprocessor;
|
|
|
|
preprocessor.AddDefine("CFG_FORCE_ALPHATEST", g_Renderer.m_Options.m_ForceAlphaTest ? "1" : "0");
|
|
|
|
|
2012-04-03 20:44:46 +02:00
|
|
|
XERO_ITER_EL(root, node)
|
|
|
|
{
|
|
|
|
int token = node.GetNodeName();
|
|
|
|
XMBAttributeList attrs = node.GetAttributes();
|
2012-04-10 23:03:22 +02:00
|
|
|
if (token == el_alternative)
|
|
|
|
{
|
|
|
|
if (preprocessor.TestConditional(attrs.GetNamedItem(at_if)))
|
|
|
|
{
|
|
|
|
material = LoadMaterial(VfsPath("art/materials") / attrs.GetNamedItem(at_material).FromUTF8());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (token == el_alpha_blending)
|
2004-09-23 06:00:56 +02:00
|
|
|
{
|
2012-04-03 20:44:46 +02:00
|
|
|
material.SetUsesAlphaBlending(true);
|
|
|
|
}
|
|
|
|
else if (token == el_shader)
|
|
|
|
{
|
|
|
|
material.SetShaderEffect(attrs.GetNamedItem(at_effect));
|
|
|
|
}
|
|
|
|
else if (token == el_define)
|
|
|
|
{
|
|
|
|
material.AddShaderDefine(attrs.GetNamedItem(at_name).c_str(), attrs.GetNamedItem(at_value).c_str());
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
2012-04-08 17:55:06 +02:00
|
|
|
else if (token == el_uniform)
|
|
|
|
{
|
|
|
|
std::stringstream str(attrs.GetNamedItem(at_value));
|
|
|
|
CVector4D vec;
|
|
|
|
str >> vec.X >> vec.Y >> vec.Z >> vec.W;
|
|
|
|
material.AddStaticUniform(attrs.GetNamedItem(at_name).c_str(), vec);
|
|
|
|
}
|
2004-09-23 06:00:56 +02:00
|
|
|
}
|
|
|
|
|
2012-04-03 20:44:46 +02:00
|
|
|
m_Materials[pathname] = material;
|
|
|
|
return material;
|
2004-09-26 23:21:34 +02:00
|
|
|
}
|