Simplified model I/O, uses filename extensions to figure out format
parent
920a4c3fc8
commit
aa3efd5841
|
@ -129,7 +129,6 @@ add_library(colobotbase STATIC
|
||||||
graphics/model/model.cpp
|
graphics/model/model.cpp
|
||||||
graphics/model/model.h
|
graphics/model/model.h
|
||||||
graphics/model/model_crash_sphere.h
|
graphics/model/model_crash_sphere.h
|
||||||
graphics/model/model_format.h
|
|
||||||
graphics/model/model_input.cpp
|
graphics/model/model_input.cpp
|
||||||
graphics/model/model_input.h
|
graphics/model/model_input.h
|
||||||
graphics/model/model_io_exception.h
|
graphics/model/model_io_exception.h
|
||||||
|
|
|
@ -52,23 +52,9 @@ bool COldModelManager::LoadModel(const std::string& fileName, bool mirrored, int
|
||||||
CModel model;
|
CModel model;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CInputStream stream;
|
std::filesystem::path path = "models/" + fileName;
|
||||||
stream.open("models/" + fileName);
|
|
||||||
if (!stream.is_open())
|
|
||||||
throw CModelIOException(std::string("Could not open file '") + fileName + "'");
|
|
||||||
|
|
||||||
std::string::size_type extension_index = fileName.find_last_of('.');
|
ModelInput::Read(model, path);
|
||||||
if (extension_index == std::string::npos)
|
|
||||||
throw CModelIOException(std::string("Filename '") + fileName + "' has no extension");
|
|
||||||
|
|
||||||
std::string extension = fileName.substr(extension_index + 1);
|
|
||||||
|
|
||||||
if (extension == "mod")
|
|
||||||
model = ModelInput::Read(stream, ModelFormat::Old);
|
|
||||||
else if (extension == "txt")
|
|
||||||
model = ModelInput::Read(stream, ModelFormat::Text);
|
|
||||||
else
|
|
||||||
throw CModelIOException(std::string("Filename '") + fileName + "' has unknown extension");
|
|
||||||
}
|
}
|
||||||
catch (const CModelIOException& e)
|
catch (const CModelIOException& e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of the Colobot: Gold Edition source code
|
|
||||||
* Copyright (C) 2001-2021, Daniel Roux, EPSITEC SA & TerranovaTeam
|
|
||||||
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
|
||||||
*
|
|
||||||
* This program 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 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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 this program. If not, see http://gnu.org/licenses
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
namespace Gfx
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \enum ModelFormat
|
|
||||||
* \brief Describes model format to use
|
|
||||||
*/
|
|
||||||
enum class ModelFormat
|
|
||||||
{
|
|
||||||
Text, //!< new text format
|
|
||||||
Old //!< old binary format, deprecated
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Gfx
|
|
|
@ -27,37 +27,22 @@
|
||||||
namespace Gfx
|
namespace Gfx
|
||||||
{
|
{
|
||||||
|
|
||||||
CModel ModelInput::Read(std::istream &stream, ModelFormat format)
|
void ModelInput::Read(CModel& model, const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
stream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
auto extension = path.extension();
|
||||||
|
|
||||||
CModel model;
|
if (extension == ".mod")
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
switch (format)
|
ModelIO::ReadOldModel(model, path);
|
||||||
|
}
|
||||||
|
else if (extension == ".txt")
|
||||||
{
|
{
|
||||||
case ModelFormat::Text:
|
ModelIO::ReadTextModel(model, path);
|
||||||
ModelIO::ReadTextModel(model, stream);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ModelFormat::Old:
|
|
||||||
ModelIO::ReadOldModel(model, stream);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
catch (const CModelIOException& e)
|
|
||||||
{
|
{
|
||||||
throw;
|
throw CModelIOException(std::string("Unknown model format: ") + extension.string());
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
|
||||||
{
|
|
||||||
throw CModelIOException(std::string("Other error while reading model data: ") + e.what());
|
|
||||||
}
|
|
||||||
|
|
||||||
return model;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace Gfx
|
} // namespace Gfx
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "graphics/model/model.h"
|
#include "graphics/model/model.h"
|
||||||
#include "graphics/model/model_format.h"
|
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
|
||||||
namespace Gfx
|
namespace Gfx
|
||||||
|
@ -33,11 +33,7 @@ namespace Gfx
|
||||||
*/
|
*/
|
||||||
namespace ModelInput
|
namespace ModelInput
|
||||||
{
|
{
|
||||||
//! Reads model from \a stream in given \a format
|
void Read(CModel& model, const std::filesystem::path& path);
|
||||||
/**
|
|
||||||
* @throws CModelIOException on read/write error
|
|
||||||
*/
|
|
||||||
CModel Read(std::istream &stream, ModelFormat format);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Gfx
|
} // namespace Gfx
|
||||||
|
|
|
@ -35,16 +35,12 @@ CModel& CModelManager::GetModel(const std::string& modelName)
|
||||||
if (it != m_models.end())
|
if (it != m_models.end())
|
||||||
return it->second;
|
return it->second;
|
||||||
|
|
||||||
std::string modelFile = "models-new/" + modelName + ".txt";
|
std::filesystem::path modelFile = "models-new/" + modelName + ".txt";
|
||||||
|
|
||||||
GetLogger()->Debug("Loading new model: %s\n", modelFile.c_str());
|
GetLogger()->Debug("Loading new model: %s\n", modelFile.c_str());
|
||||||
|
|
||||||
CInputStream stream;
|
CModel model;
|
||||||
stream.open(modelFile.c_str());
|
ModelInput::Read(model, modelFile);
|
||||||
if (!stream.is_open())
|
|
||||||
throw CModelIOException(std::string("Could not open file '") + modelName + "'");
|
|
||||||
|
|
||||||
CModel model = ModelInput::Read(stream, ModelFormat::Text);
|
|
||||||
m_models[modelName] = model;
|
m_models[modelName] = model;
|
||||||
|
|
||||||
return m_models[modelName];
|
return m_models[modelName];
|
||||||
|
|
|
@ -47,8 +47,10 @@ void ConvertOldTex1Name(ModelTriangle& triangle, const char* tex1Name);
|
||||||
void ConvertFromOldRenderState(ModelTriangle& triangle, int state);
|
void ConvertFromOldRenderState(ModelTriangle& triangle, int state);
|
||||||
ModelLODLevel MinMaxToLodLevel(float min, float max);
|
ModelLODLevel MinMaxToLodLevel(float min, float max);
|
||||||
|
|
||||||
void ReadOldModel(CModel& model, std::istream& stream)
|
void ReadOldModel(CModel& model, const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
|
CInputStream stream(path);
|
||||||
|
|
||||||
OldModelHeader header;
|
OldModelHeader header;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "graphics/model/model.h"
|
#include "graphics/model/model.h"
|
||||||
#include "graphics/model/model_format.h"
|
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,6 +31,6 @@
|
||||||
namespace Gfx::ModelIO
|
namespace Gfx::ModelIO
|
||||||
{
|
{
|
||||||
|
|
||||||
void ReadOldModel(CModel& model, std::istream& stream);
|
void ReadOldModel(CModel& model, const std::filesystem::path& path);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,19 +19,12 @@
|
||||||
|
|
||||||
#include "graphics/model/model_output.h"
|
#include "graphics/model/model_output.h"
|
||||||
|
|
||||||
#include "common/ioutils.h"
|
|
||||||
|
|
||||||
#include "graphics/model/model.h"
|
|
||||||
#include "graphics/model/model_io_exception.h"
|
|
||||||
#include "graphics/model/model_io_structs.h"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
namespace Gfx
|
namespace Gfx
|
||||||
{
|
{
|
||||||
|
|
||||||
void ModelOutput::Write(const CModel& model, std::ostream &stream, ModelFormat format)
|
void ModelOutput::Write(const CModel& model, const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
throw std::logic_error("Not implemented");
|
throw std::logic_error("Not implemented");
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,9 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "graphics/model/model_format.h"
|
#include "graphics/model/model.h"
|
||||||
|
|
||||||
#include <ostream>
|
#include <filesystem>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace Gfx
|
namespace Gfx
|
||||||
{
|
{
|
||||||
|
@ -35,7 +34,7 @@ namespace ModelOutput
|
||||||
/**
|
/**
|
||||||
* @throws CModelIOException on read/write error
|
* @throws CModelIOException on read/write error
|
||||||
*/
|
*/
|
||||||
void Write(const CModel& model, std::ostream& stream, ModelFormat format);
|
void Write(const CModel& model, const std::filesystem::path& path);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Gfx
|
} // namespace Gfx
|
||||||
|
|
|
@ -51,8 +51,10 @@ std::string ParseSpecialMark(const std::string& text);
|
||||||
|
|
||||||
void ConvertFromOldRenderState(ModelTriangle& triangle, int state);
|
void ConvertFromOldRenderState(ModelTriangle& triangle, int state);
|
||||||
|
|
||||||
void ReadTextModel(CModel& model, std::istream& stream)
|
void ReadTextModel(CModel& model, const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
|
CInputStream stream(path);
|
||||||
|
|
||||||
int version = 0;
|
int version = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "graphics/model/model.h"
|
#include "graphics/model/model.h"
|
||||||
#include "graphics/model/model_format.h"
|
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,6 +31,6 @@
|
||||||
namespace Gfx::ModelIO
|
namespace Gfx::ModelIO
|
||||||
{
|
{
|
||||||
|
|
||||||
void ReadTextModel(CModel& model, std::istream& stream);
|
void ReadTextModel(CModel& model, const std::filesystem::path& path);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue