From ce6d530aa8d4a44ae4312bcbf2f1e074781301c5 Mon Sep 17 00:00:00 2001 From: milek7 Date: Sun, 6 Mar 2022 18:18:07 +0100 Subject: [PATCH] changes for standalone e3d export --- CMakeLists.txt | 2 + EU07.cpp | 19 ++++- Globals.cpp | 4 +- Globals.h | 1 + Logs.cpp | 4 +- Model3d.cpp | 2 +- export_e3d_standalone.cpp | 17 +++++ nullrenderer.cpp | 9 +++ nullrenderer.h | 156 ++++++++++++++++++++++++++++++++++++++ parser.cpp | 5 ++ 10 files changed, 210 insertions(+), 9 deletions(-) create mode 100644 export_e3d_standalone.cpp create mode 100644 nullrenderer.cpp create mode 100644 nullrenderer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e76e6c7b..357b274b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ set(SOURCES "dumb3d.cpp" "DynObj.cpp" "EU07.cpp" +"export_e3d_standalone.cpp" "Event.cpp" "EvLaunch.cpp" "Float3d.cpp" @@ -87,6 +88,7 @@ set(SOURCES "Model3d.cpp" "mtable.cpp" "parser.cpp" +"nullrenderer.cpp" "renderer.cpp" "PyInt.cpp" "ResourceManager.cpp" diff --git a/EU07.cpp b/EU07.cpp index 00b6f8fe..f5878ae2 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -22,13 +22,24 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others #include "Logs.h" #include -#ifdef _MSC_VER -#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup") -#endif +#ifdef _MSC_VER +#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup") +#endif +void export_e3d_standalone(std::string in, std::string out, int flags, bool dynamic); -int main( int argc, char *argv[] ) +int main(int argc, char *argv[]) { + // quick short-circuit for standalone e3d export + if (argc == 6 && std::string(argv[1]) == "-e3d") { + std::string in(argv[2]); + std::string out(argv[3]); + int flags = std::stoi(std::string(argv[4])); + int dynamic = std::stoi(std::string(argv[5])); + export_e3d_standalone(in, out, flags, dynamic); + std::_Exit(0); + } + try { auto result { Application.init( argc, argv ) }; diff --git a/Globals.cpp b/Globals.cpp index 31dc2c01..c3bddd01 100644 --- a/Globals.cpp +++ b/Globals.cpp @@ -487,8 +487,6 @@ global_settings::ConfigParse(cParser &Parser) { // tworzenie plików binarnych Parser.getTokens(1, false); Parser >> iConvertModels; - // temporary override, to prevent generation of .e3d not compatible with old exe - iConvertModels = (iConvertModels > 128 ? iConvertModels - 128 : 0); } else if (token == "file.binary.terrain") { @@ -1266,7 +1264,7 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "timespeed", fTimeSpeed ); export_as_text( Output, "multisampling", iMultisampling ); export_as_text( Output, "latitude", fLatitudeDeg ); - export_as_text( Output, "convertmodels", iConvertModels + ( iConvertModels > 0 ? 128 : 0 ) ); + export_as_text( Output, "convertmodels", iConvertModels ); export_as_text( Output, "file.binary.terrain", file_binary_terrain ); export_as_text( Output, "inactivepause", bInactivePause ); export_as_text( Output, "slowmotion", iSlowMotionMask ); diff --git a/Globals.h b/Globals.h index af2afb17..5431795c 100644 --- a/Globals.h +++ b/Globals.h @@ -75,6 +75,7 @@ struct global_settings { int iWriteLogEnabled{ 3 }; // maska bitowa: 1-zapis do pliku, 2-okienko, 4-nazwy torów bool MultipleLogs{ false }; unsigned int DisabledLogTypes{ 0 }; + bool ParserLogIncludes{ false }; // simulation bool RealisticControlMode{ false }; // controls ability to steer the vehicle from outside views bool bEnableTraction{ true }; diff --git a/Logs.cpp b/Logs.cpp index 225d0a6a..c3ffef77 100644 --- a/Logs.cpp +++ b/Logs.cpp @@ -103,12 +103,14 @@ void WriteLog( const char *str, logtype const Type ) { } } -// Ra: bezwarunkowa rejestracja poważnych błędów void ErrorLog( const char *str, logtype const Type ) { if( str == nullptr ) { return; } if( true == TestFlag( Global.DisabledLogTypes, static_cast( Type ) ) ) { return; } + if (!(Global.iWriteLogEnabled & 1)) + return; + if (!errors.is_open()) { std::string const filename = diff --git a/Model3d.cpp b/Model3d.cpp index 78c869f7..5a8f401f 100644 --- a/Model3d.cpp +++ b/Model3d.cpp @@ -2277,7 +2277,7 @@ void TModel3d::Init() Root->m_boundingradius = std::max( Root->m_boundingradius, root->m_boundingradius ); } - if( ( Global.iConvertModels > 0 ) + if( ( Global.iConvertModels & 1 ) && ( false == asBinary.empty() ) ) { SaveToBinFile( asBinary ); asBinary = ""; // zablokowanie powtórnego zapisu diff --git a/export_e3d_standalone.cpp b/export_e3d_standalone.cpp new file mode 100644 index 00000000..1451877c --- /dev/null +++ b/export_e3d_standalone.cpp @@ -0,0 +1,17 @@ +#include "stdafx.h" + +#include "Model3d.h" +#include "Globals.h" +#include "renderer.h" + +void export_e3d_standalone(std::string in, std::string out, int flags, bool dynamic) +{ + Global.iConvertModels = flags; + Global.iWriteLogEnabled = 2; + Global.ParserLogIncludes = true; + GfxRenderer = gfx_renderer_factory::get_instance()->create("null"); + TModel3d model; + model.LoadFromTextFile(in, dynamic); + model.Init(); + model.SaveToBinFile(out); +} \ No newline at end of file diff --git a/nullrenderer.cpp b/nullrenderer.cpp new file mode 100644 index 00000000..452ce971 --- /dev/null +++ b/nullrenderer.cpp @@ -0,0 +1,9 @@ +#include "stdafx.h" +#include "nullrenderer.h" + +std::unique_ptr null_renderer::create_func() +{ + return std::unique_ptr(new null_renderer()); +} + +bool null_renderer::renderer_register = gfx_renderer_factory::get_instance()->register_backend("null", null_renderer::create_func); diff --git a/nullrenderer.h b/nullrenderer.h new file mode 100644 index 00000000..71f76eb1 --- /dev/null +++ b/nullrenderer.h @@ -0,0 +1,156 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#pragma once + +#include "Texture.h" +#include "geometrybank.h" +#include "material.h" +#include "renderer.h" +#include "stdafx.h" +#include +#include + +class null_geometrybank : public gfx::geometry_bank { +public: +// constructors: + null_geometrybank() = default; +// destructor + ~null_geometrybank() {}; + +private: +// methods: + // create() subclass details + void + create_( gfx::geometry_handle const &Geometry ) override {} + // replace() subclass details + void + replace_( gfx::geometry_handle const &Geometry ) override {} + // draw() subclass details + auto + draw_( gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, unsigned int const Streams ) -> std::size_t override { return 0; } + // release() subclass details + void + release_() override {} +}; + +// bare-bones render controller, in lack of anything better yet +class null_renderer : public gfx_renderer { + +public: +// types +// constructors + null_renderer() = default; +// destructor + ~null_renderer() { } +// methods + bool + Init( GLFWwindow *Window ) override { return true; } + // main draw call. returns false on error + bool + Render() override { return true; } + void + SwapBuffers() override {} + inline + float + Framerate() override { return 10.0f; } + + bool AddViewport(const global_settings::extraviewport_config &conf) override { return false; } + bool Debug_Ui_State(std::optional) override { return false; } + void Shutdown() override {} + + // geometry methods + // NOTE: hands-on geometry management is exposed as a temporary measure; ultimately all visualization data should be generated/handled automatically by the renderer itself + // creates a new geometry bank. returns: handle to the bank or NULL + gfx::geometrybank_handle + Create_Bank() override { return m_geometry.register_bank(std::make_unique()); } + // creates a new indexed geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL + gfx::geometry_handle + Insert( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::geometrybank_handle const &Geometry, int const Type ) override { return m_geometry.create_chunk( Indices, Vertices, Geometry, Type ); } + // creates a new geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL + gfx::geometry_handle + Insert( gfx::vertex_array &Vertices, gfx::geometrybank_handle const &Geometry, int const Type ) override { + gfx::calculate_tangents(Vertices, gfx::index_array(), Type); + + return m_geometry.create_chunk(Vertices, Geometry, Type); + } + // replaces data of specified chunk with the supplied vertex data, starting from specified offset + bool + Replace( gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type, std::size_t const Offset = 0 ) override { + gfx::calculate_tangents(Vertices, gfx::index_array(), Type); + + return m_geometry.replace(Vertices, Geometry, Offset); + } + // adds supplied vertex data at the end of specified chunk + bool + Append( gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type ) override { + gfx::calculate_tangents(Vertices, gfx::index_array(), Type); + + return m_geometry.append(Vertices, Geometry); + } + // provides direct access to index data of specfied chunk + gfx::index_array const & + Indices( gfx::geometry_handle const &Geometry ) const override { return m_geometry.indices(Geometry); } + // provides direct access to vertex data of specfied chunk + gfx::vertex_array const & + Vertices( gfx::geometry_handle const &Geometry ) const override { return m_geometry.vertices(Geometry); } + // material methods + material_handle + Fetch_Material( std::string const &Filename, bool const Loadnow = true ) override { + m_materials.push_back(std::make_shared()); + m_materials.back()->name = Filename; + return m_materials.size(); + } + void + Bind_Material( material_handle const Material, TSubModel const *sm = nullptr, lighting_data const *lighting = nullptr ) override {} + opengl_material const & + Material( material_handle const Material ) const override { return *m_materials.at(Material - 1); } + // shader methods + auto Fetch_Shader( std::string const &name ) -> std::shared_ptr override { throw std::runtime_error("not impl"); } + // texture methods + texture_handle + Fetch_Texture( std::string const &Filename, bool const Loadnow = true, GLint format_hint = GL_SRGB_ALPHA ) override { throw std::runtime_error("not impl"); } + void + Bind_Texture( texture_handle const Texture ) override {} + void + Bind_Texture( std::size_t const Unit, texture_handle const Texture ) override {} + opengl_texture & + Texture( texture_handle const Texture ) override { throw std::runtime_error("not impl"); } + opengl_texture const & + Texture( texture_handle const Texture ) const override { throw std::runtime_error("not impl"); } + // utility methods + void + Pick_Control_Callback( std::function Callback ) override {} + void + Pick_Node_Callback( std::function Callback ) override {} + TSubModel const * + Pick_Control() const override { return nullptr; } + scene::basic_node const * + Pick_Node() const override { return nullptr; } + glm::dvec3 + Mouse_Position() const override { return glm::dvec3(); } + // maintenance methods + void + Update( double const Deltatime ) override {} + void + Update_Pick_Control() override {} + void + Update_Pick_Node() override {} + glm::dvec3 + Update_Mouse_Position() override { return glm::dvec3(); } + // debug methods + std::string const & + info_times() const override { return empty_str; } + std::string const & + info_stats() const override { return empty_str; } + + static std::unique_ptr create_func(); + + static bool renderer_register; + +private: + std::string empty_str; + gfx::geometrybank_manager m_geometry; + std::vector> m_materials; +}; diff --git a/parser.cpp b/parser.cpp index 40522490..e4b7d957 100644 --- a/parser.cpp +++ b/parser.cpp @@ -7,6 +7,7 @@ obtain one at http://mozilla.org/MPL/2.0/. */ +#include "Globals.h" #include "stdafx.h" #include "parser.h" #include "utilities.h" @@ -250,6 +251,8 @@ std::string cParser::readToken( bool ToLower, const char *Break ) { if( ( true == LoadTraction ) || ( ( false == contains( includefile, "tr/" ) ) && ( false == contains( includefile, "tra/" ) ) ) ) { + if (Global.ParserLogIncludes) + WriteLog("including: " + includefile); mIncludeParser = std::make_shared( includefile, buffer_FILE, mPath, LoadTraction, readParameters( *this ) ); mIncludeParser->autoclear( m_autoclear ); if( mIncludeParser->mSize <= 0 ) { @@ -270,6 +273,8 @@ std::string cParser::readToken( bool ToLower, const char *Break ) { if( ( true == LoadTraction ) || ( ( false == contains( includefile, "tr/" ) ) && ( false == contains( includefile, "tra/" ) ) ) ) { + if (Global.ParserLogIncludes) + WriteLog("including: " + includefile); mIncludeParser = std::make_shared( includefile, buffer_FILE, mPath, LoadTraction, readParameters( includeparser ) ); mIncludeParser->autoclear( m_autoclear ); if( mIncludeParser->mSize <= 0 ) {