mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-07-04 02:01:51 +00:00
Implement writer for raw materials
This commit is contained in:
30
src/ObjWriting/Dumping/AbstractRawDumper.cpp
Normal file
30
src/ObjWriting/Dumping/AbstractRawDumper.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include "AbstractRawDumper.h"
|
||||
|
||||
AbstractRawDumper::AbstractRawDumper(std::ostream& stream)
|
||||
: m_stream(stream),
|
||||
m_current_offset(0u)
|
||||
{
|
||||
}
|
||||
|
||||
size_t AbstractRawDumper::OffsetAdd(const size_t len)
|
||||
{
|
||||
const auto lastOffset = m_current_offset;
|
||||
m_current_offset += len;
|
||||
|
||||
return lastOffset;
|
||||
}
|
||||
|
||||
size_t AbstractRawDumper::OffsetAdd(const std::string& str)
|
||||
{
|
||||
return OffsetAdd(str.size() + 1);
|
||||
}
|
||||
|
||||
void AbstractRawDumper::Write(const void* ptr, const size_t size) const
|
||||
{
|
||||
m_stream.write(static_cast<const char*>(ptr), size);
|
||||
}
|
||||
|
||||
void AbstractRawDumper::Write(const std::string& str) const
|
||||
{
|
||||
m_stream.write(str.c_str(), str.size() + 1);
|
||||
}
|
25
src/ObjWriting/Dumping/AbstractRawDumper.h
Normal file
25
src/ObjWriting/Dumping/AbstractRawDumper.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
class AbstractRawDumper
|
||||
{
|
||||
protected:
|
||||
explicit AbstractRawDumper(std::ostream& stream);
|
||||
|
||||
public:
|
||||
virtual ~AbstractRawDumper() = default;
|
||||
AbstractRawDumper(const AbstractRawDumper& other) = default;
|
||||
AbstractRawDumper(AbstractRawDumper&& other) noexcept = default;
|
||||
AbstractRawDumper& operator=(const AbstractRawDumper& other) = delete;
|
||||
AbstractRawDumper& operator=(AbstractRawDumper&& other) noexcept = delete;
|
||||
|
||||
protected:
|
||||
size_t OffsetAdd(size_t len);
|
||||
size_t OffsetAdd(const std::string& str);
|
||||
void Write(const void* ptr, size_t size) const;
|
||||
void Write(const std::string& str) const;
|
||||
|
||||
std::ostream& m_stream;
|
||||
size_t m_current_offset;
|
||||
};
|
101
src/ObjWriting/Material/Raw/MaterialRawOatWriter.cpp
Normal file
101
src/ObjWriting/Material/Raw/MaterialRawOatWriter.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
#include "MaterialRawOatWriter.h"
|
||||
|
||||
#include "Material/Raw/MaterialRawOat.h"
|
||||
|
||||
using namespace material;
|
||||
|
||||
MaterialRawOatWriter::MaterialRawOatWriter(std::ostream& stream)
|
||||
: AbstractRawDumper(stream)
|
||||
{
|
||||
}
|
||||
|
||||
void MaterialRawOatWriter::WriteMaterial(const CommonMaterial& material)
|
||||
{
|
||||
constexpr MaterialRawOatHeader header{
|
||||
{'M', 'T', 'L'},
|
||||
1u
|
||||
};
|
||||
OffsetAdd(sizeof(header));
|
||||
|
||||
MaterialRaw materialRaw{};
|
||||
OffsetAdd(sizeof(materialRaw));
|
||||
|
||||
materialRaw.info.nameOffset = OffsetAdd(material.m_name);
|
||||
materialRaw.info.gameFlags = material.m_game_flags;
|
||||
materialRaw.info.sortKey = material.m_sort_key;
|
||||
materialRaw.info.textureAtlasRowCount = material.m_texture_atlas_row_count;
|
||||
materialRaw.info.textureAtlasColumnCount = material.m_texture_atlas_column_count;
|
||||
materialRaw.info.surfaceFlags = material.m_surface_flags;
|
||||
materialRaw.info.contents = material.m_contents;
|
||||
materialRaw.refStateBits[0] = material.m_ref_state_bits[0];
|
||||
materialRaw.refStateBits[1] = material.m_ref_state_bits[1];
|
||||
materialRaw.textureCount = static_cast<uint16_t>(material.m_textures.size());
|
||||
materialRaw.constantCount = static_cast<uint16_t>(material.m_textures.size());
|
||||
materialRaw.techSetNameOffset = OffsetAdd(material.m_techset_name);
|
||||
materialRaw.textureTableOffset = OffsetAdd(sizeof(MaterialTextureDefRaw) * material.m_textures.size());
|
||||
materialRaw.constantTableOffset = OffsetAdd(sizeof(MaterialConstantDefRaw) * material.m_textures.size());
|
||||
|
||||
const auto textureCount = material.m_textures.size();
|
||||
std::vector<MaterialTextureDefRaw> textureDefs(textureCount);
|
||||
for (auto i = 0u; i < textureCount; i++)
|
||||
{
|
||||
const auto& texture = material.m_textures[i];
|
||||
auto& textureDef = textureDefs[i];
|
||||
|
||||
textureDef.nameOffset = OffsetAdd(texture.m_name);
|
||||
textureDef.samplerState = texture.m_sampler_state;
|
||||
textureDef.semantic = texture.m_semantic;
|
||||
textureDef.imageNameOffset = OffsetAdd(texture.m_image_name);
|
||||
textureDef.waterDefOffset = texture.m_water_def ? OffsetAdd(sizeof(MaterialWaterDefRaw)) : 0u;
|
||||
}
|
||||
|
||||
const auto constantCount = material.m_constants.size();
|
||||
std::vector<MaterialConstantDefRaw> constantDefs(constantCount);
|
||||
for (auto i = 0u; i < constantCount; i++)
|
||||
{
|
||||
const auto& constant = material.m_constants[i];
|
||||
auto& constantDef = constantDefs[i];
|
||||
|
||||
constantDef.nameOffset = OffsetAdd(constant.m_name);
|
||||
constantDef.literal[0] = constant.m_literal[0];
|
||||
constantDef.literal[1] = constant.m_literal[1];
|
||||
constantDef.literal[2] = constant.m_literal[2];
|
||||
constantDef.literal[3] = constant.m_literal[3];
|
||||
}
|
||||
|
||||
Write(&header, sizeof(header));
|
||||
Write(&materialRaw, sizeof(materialRaw));
|
||||
Write(material.m_name);
|
||||
Write(material.m_techset_name);
|
||||
Write(textureDefs.data(), sizeof(MaterialTextureDefRaw) * textureCount);
|
||||
Write(constantDefs.data(), sizeof(MaterialConstantDefRaw) * constantCount);
|
||||
|
||||
for (auto i = 0u; i < textureCount; i++)
|
||||
{
|
||||
const auto& texture = material.m_textures[i];
|
||||
|
||||
Write(texture.m_name);
|
||||
Write(texture.m_image_name);
|
||||
|
||||
if (texture.m_water_def)
|
||||
{
|
||||
const auto& water = *texture.m_water_def;
|
||||
MaterialWaterDefRaw waterDef{};
|
||||
waterDef.textureWidth = water.m_texture_width;
|
||||
waterDef.horizontalWorldLength = water.m_horizontal_world_length;
|
||||
waterDef.verticalWorldLength = water.m_vertical_world_length;
|
||||
waterDef.amplitude = water.m_amplitude;
|
||||
waterDef.windSpeed = water.m_wind_speed;
|
||||
waterDef.windDirection[0] = water.m_wind_direction[0];
|
||||
waterDef.windDirection[1] = water.m_wind_direction[1];
|
||||
|
||||
Write(&waterDef, sizeof(MaterialWaterDefRaw));
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = 0u; i < constantCount; i++)
|
||||
{
|
||||
const auto& constant = material.m_constants[i];
|
||||
Write(constant.m_name);
|
||||
}
|
||||
}
|
18
src/ObjWriting/Material/Raw/MaterialRawOatWriter.h
Normal file
18
src/ObjWriting/Material/Raw/MaterialRawOatWriter.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
|
||||
#include "Dumping/AbstractRawDumper.h"
|
||||
#include "Material/MaterialCommon.h"
|
||||
|
||||
namespace material
|
||||
{
|
||||
class MaterialRawOatWriter final : AbstractRawDumper
|
||||
{
|
||||
public:
|
||||
explicit MaterialRawOatWriter(std::ostream& stream);
|
||||
|
||||
void WriteMaterial(const CommonMaterial& material);
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user