Utility: Add MTL exporting
Former-commit-id: b524c2d445f4c5cdadedabc3a9c38307dbfecb9b [formerly d65160f4e3f2fa2c7c9ddd151c73990c6712b4c1] Former-commit-id: 7ccdf043ccf793d3d9a5c9d93c65919ac015b52c
This commit is contained in:
@@ -19,6 +19,22 @@ namespace Nz
|
||||
class NAZARA_UTILITY_API MTLParser
|
||||
{
|
||||
public:
|
||||
struct Material;
|
||||
|
||||
MTLParser() = default;
|
||||
~MTLParser() = default;
|
||||
|
||||
inline Material* AddMaterial(const String& matName);
|
||||
|
||||
inline void Clear();
|
||||
|
||||
inline const Material* GetMaterial(const String& materialName) const;
|
||||
inline const std::unordered_map<String, Material>& GetMaterials() const;
|
||||
|
||||
bool Parse(Stream& stream);
|
||||
|
||||
bool Save(Stream& stream) const;
|
||||
|
||||
struct Material
|
||||
{
|
||||
Color ambient = Color::White;
|
||||
@@ -39,27 +55,26 @@ namespace Nz
|
||||
unsigned int illumModel = 0;
|
||||
};
|
||||
|
||||
MTLParser(Stream& stream$);
|
||||
~MTLParser();
|
||||
|
||||
const Material* GetMaterial(const String& materialName) const;
|
||||
const std::unordered_map<String, Material>& GetMaterials() const;
|
||||
|
||||
bool Parse();
|
||||
|
||||
private:
|
||||
bool Advance(bool required = true);
|
||||
void Error(const String& message);
|
||||
void Warning(const String& message);
|
||||
void UnrecognizedLine(bool error = false);
|
||||
template<typename T> void Emit(const T& text) const;
|
||||
inline void EmitLine() const;
|
||||
template<typename T> void EmitLine(const T& line) const;
|
||||
inline void Error(const String& message);
|
||||
inline void Flush() const;
|
||||
inline void Warning(const String& message);
|
||||
inline void UnrecognizedLine(bool error = false);
|
||||
|
||||
std::unordered_map<String, Material> m_materials;
|
||||
Stream& m_stream;
|
||||
mutable Stream* m_currentStream;
|
||||
String m_currentLine;
|
||||
mutable StringStream m_outputStream;
|
||||
bool m_keepLastLine;
|
||||
unsigned int m_lineCount;
|
||||
unsigned int m_streamFlags;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Utility/Formats/MTLParser.inl>
|
||||
|
||||
#endif // NAZARA_FORMATS_MTLPARSER_HPP
|
||||
|
||||
83
include/Nazara/Utility/Formats/MTLParser.inl
Normal file
83
include/Nazara/Utility/Formats/MTLParser.inl
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright (C) 2016 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Formats/MTLParser.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline MTLParser::Material* MTLParser::AddMaterial(const String& matName)
|
||||
{
|
||||
return &m_materials[matName];
|
||||
}
|
||||
|
||||
inline void MTLParser::Clear()
|
||||
{
|
||||
m_materials.clear();
|
||||
}
|
||||
|
||||
inline const MTLParser::Material* MTLParser::GetMaterial(const String& materialName) const
|
||||
{
|
||||
auto it = m_materials.find(materialName);
|
||||
if (it != m_materials.end())
|
||||
return &it->second;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline const std::unordered_map<String, MTLParser::Material>& MTLParser::GetMaterials() const
|
||||
{
|
||||
return m_materials;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void MTLParser::Emit(const T& text) const
|
||||
{
|
||||
m_outputStream << text;
|
||||
if (m_outputStream.GetBufferSize() > 1024 * 1024)
|
||||
Flush();
|
||||
}
|
||||
|
||||
inline void MTLParser::EmitLine() const
|
||||
{
|
||||
Emit('\n');
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void MTLParser::EmitLine(const T& line) const
|
||||
{
|
||||
Emit(line);
|
||||
Emit('\n');
|
||||
}
|
||||
|
||||
inline void MTLParser::Error(const String& message)
|
||||
{
|
||||
NazaraError(message + " at line #" + String::Number(m_lineCount));
|
||||
}
|
||||
|
||||
inline void MTLParser::Flush() const
|
||||
{
|
||||
m_currentStream->Write(m_outputStream);
|
||||
m_outputStream.Clear();
|
||||
}
|
||||
|
||||
inline void MTLParser::Warning(const String& message)
|
||||
{
|
||||
NazaraWarning(message + " at line #" + String::Number(m_lineCount));
|
||||
}
|
||||
|
||||
inline void MTLParser::UnrecognizedLine(bool error)
|
||||
{
|
||||
String message = "Unrecognized \"" + m_currentLine + '"';
|
||||
|
||||
if (error)
|
||||
Error(message);
|
||||
else
|
||||
Warning(message);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Utility/DebugOff.hpp>
|
||||
#include "MTLParser.hpp"
|
||||
@@ -52,6 +52,7 @@ namespace Nz
|
||||
|
||||
inline String* SetMaterialCount(std::size_t materialCount);
|
||||
inline Mesh* SetMeshCount(std::size_t meshCount);
|
||||
inline void SetMtlLib(const String& mtlLib);
|
||||
inline Vector3f* SetNormalCount(std::size_t normalCount);
|
||||
inline Vector4f* SetPositionCount(std::size_t positionCount);
|
||||
inline Vector3f* SetTexCoordCount(std::size_t texCoordCount);
|
||||
|
||||
@@ -109,6 +109,11 @@ namespace Nz
|
||||
return m_meshes.data();
|
||||
}
|
||||
|
||||
inline void OBJParser::SetMtlLib(const String& mtlLib)
|
||||
{
|
||||
m_mtlLib = mtlLib;
|
||||
}
|
||||
|
||||
inline Vector3f* OBJParser::SetNormalCount(std::size_t normalCount)
|
||||
{
|
||||
m_normals.resize(normalCount);
|
||||
|
||||
Reference in New Issue
Block a user