From 1529d37d585ec38aee475519e444d2718a81fda5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 13 Apr 2023 02:10:44 +0100 Subject: Add TextureAtlas class as an extension of Texture --- gfx/models/texture.cpp | 32 ++++++++++++++++++++++++++++++++ gfx/models/texture.h | 19 +++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index efc76e1..a500fed 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -85,3 +85,35 @@ Texture::saveNormal(const glTexture & texture, const glm::ivec2 & size, const ch { save(texture, GL_BGR, GL_BYTE, size, 3, path, 2); } + +TextureAtlas::TextureAtlas(GLsizei width, GLsizei height, GLuint count) : Texture(width, height, nullptr, {}) +{ + glBindTexture(GL_TEXTURE_RECTANGLE, m_atlas); + + glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA16UI, 2, static_cast(count), 0, GL_RGBA_INTEGER, + GL_UNSIGNED_BYTE, nullptr); +} + +void +TextureAtlas::bind(GLenum unit) const +{ + Texture::bind(unit); + glActiveTexture(unit + 1); + glBindTexture(GL_TEXTURE_RECTANGLE, m_atlas); +} + +GLuint +TextureAtlas::add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions) +{ + glTextureSubImage2D(m_texture, 0, position.x, position.y, size.x, size.y, GL_RGBA, GL_UNSIGNED_BYTE, data); + struct Material { + glm::vec<2, uint16_t> position, size, unused1 {}, unused2 {}; + } material {position, size}; + glTextureSubImage2D(m_atlas, 0, 0, static_cast(used), 2, 1, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, &material); + return ++used; +} diff --git a/gfx/models/texture.h b/gfx/models/texture.h index ffc9a4a..7900f17 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -16,6 +16,9 @@ struct TextureOptions { class Texture { public: + virtual ~Texture() = default; + DEFAULT_MOVE_NO_COPY(Texture); + explicit Texture(const std::filesystem::path & fileName, TextureOptions = {}); explicit Texture(const Image & image, TextureOptions = {}); explicit Texture(GLsizei width, GLsizei height, TextureOptions = {}); @@ -23,17 +26,29 @@ public: static Cache cachedTexture; - void bind(GLenum unit = GL_TEXTURE0) const; + virtual void bind(GLenum unit = GL_TEXTURE0) const; void save(const glm::ivec2 & size, const char * path) const; static void save(const glTexture &, const glm::ivec2 & size, const char * path); static void saveDepth(const glTexture &, const glm::ivec2 & size, const char * path); static void saveNormal(const glTexture &, const glm::ivec2 & size, const char * path); -private: +protected: static void save(const glTexture &, GLenum, GLenum, const glm::ivec2 & size, unsigned short channels, const char * path, short tgaFormat); glTexture m_texture; GLenum type; }; + +class TextureAtlas : public Texture { +public: + TextureAtlas(GLsizei width, GLsizei height, GLuint count); + + void bind(GLenum unit = GL_TEXTURE0) const override; + GLuint add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions = {}); + +private: + glTexture m_atlas; + GLuint used {}; +}; -- cgit v1.2.3 From 259444e842ac4ec546948a8e828b62bec734f659 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 13 Apr 2023 02:13:20 +0100 Subject: Add material field to vertex and configure it in mesh --- gfx/models/mesh.cpp | 3 ++- gfx/models/vertex.hpp | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index 3db1ad5..2719211 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -7,7 +7,8 @@ Mesh::Mesh(const std::span vertices, const std::span indices, GLenum m) : m_numIndices {static_cast(indices.size())}, mode {m} { - VertexArrayObject::configure<&Vertex::pos, &Vertex::texCoord, &Vertex::normal, &Vertex::colour>( + VertexArrayObject::configure<&Vertex::pos, &Vertex::texCoord, &Vertex::normal, &Vertex::colour, + &Vertex::material>( m_vertexArrayObject, m_vertexArrayBuffers[0], m_vertexArrayBuffers[1], vertices, indices); } diff --git a/gfx/models/vertex.hpp b/gfx/models/vertex.hpp index 64ec3d0..181e7e7 100644 --- a/gfx/models/vertex.hpp +++ b/gfx/models/vertex.hpp @@ -1,12 +1,14 @@ #pragma once +#include #include class Vertex { public: #ifndef __cpp_aggregate_paren_init - constexpr Vertex(glm::vec3 pos, glm::vec2 texCoord, glm::vec3 normal, glm::vec4 colour = {}) : - pos {std::move(pos)}, texCoord {std::move(texCoord)}, normal {std::move(normal)}, colour {std::move(colour)} + constexpr Vertex(glm::vec3 pos, glm::vec2 texCoord, glm::vec3 normal, glm::vec4 colour = {}, GLuint material = 0) : + pos {std::move(pos)}, texCoord {std::move(texCoord)}, normal {std::move(normal)}, colour {std::move(colour)}, + material {material} { } #endif @@ -16,5 +18,6 @@ public: glm::vec3 pos; glm::vec2 texCoord; glm::vec3 normal; - glm::vec4 colour; + glm::vec4 colour {}; + GLuint material {}; }; -- cgit v1.2.3 From 0638aaaf2a60efae95a1b404d7eee29e894047bc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 01:54:19 +0100 Subject: No need to pass size around, we can get it back from the texture --- gfx/models/texture.cpp | 30 ++++++++++++++++++++---------- gfx/models/texture.h | 12 ++++++------ test/test-assetFactory.cpp | 2 +- test/test-render.cpp | 6 +++--- 4 files changed, 30 insertions(+), 20 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index a500fed..ab256d5 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -44,12 +44,22 @@ Texture::bind(GLenum unit) const glBindTexture(type, m_texture); } +glm::ivec2 +Texture::getSize(const glTexture & texture) +{ + glm::ivec2 size; + glGetTextureLevelParameteriv(texture, 0, GL_TEXTURE_WIDTH, &size.x); + glGetTextureLevelParameteriv(texture, 0, GL_TEXTURE_HEIGHT, &size.y); + return size; +} + void -Texture::save(const glTexture & texture, GLenum format, GLenum type, const glm::ivec2 & size, unsigned short channels, - const char * path, short tgaFormat) +Texture::save(const glTexture & texture, GLenum format, GLenum type, unsigned short channels, const char * path, + short tgaFormat) { using TGAHead = std::array; + const auto size = getSize(texture); const size_t dataSize = (static_cast(size.x * size.y * channels)); const size_t fileSize = dataSize + sizeof(TGAHead); @@ -63,27 +73,27 @@ Texture::save(const glTexture & texture, GLenum format, GLenum type, const glm:: } void -Texture::save(const glm::ivec2 & size, const char * path) const +Texture::save(const char * path) const { - save(m_texture, GL_BGR, GL_UNSIGNED_BYTE, size, 3, path, 2); + save(m_texture, GL_BGR, GL_UNSIGNED_BYTE, 3, path, 2); } void -Texture::save(const glTexture & texture, const glm::ivec2 & size, const char * path) +Texture::save(const glTexture & texture, const char * path) { - save(texture, GL_BGR, GL_UNSIGNED_BYTE, size, 3, path, 2); + save(texture, GL_BGR, GL_UNSIGNED_BYTE, 3, path, 2); } void -Texture::saveDepth(const glTexture & texture, const glm::ivec2 & size, const char * path) +Texture::saveDepth(const glTexture & texture, const char * path) { - save(texture, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, size, 1, path, 3); + save(texture, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 1, path, 3); } void -Texture::saveNormal(const glTexture & texture, const glm::ivec2 & size, const char * path) +Texture::saveNormal(const glTexture & texture, const char * path) { - save(texture, GL_BGR, GL_BYTE, size, 3, path, 2); + save(texture, GL_BGR, GL_BYTE, 3, path, 2); } TextureAtlas::TextureAtlas(GLsizei width, GLsizei height, GLuint count) : Texture(width, height, nullptr, {}) diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 7900f17..f4e1476 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -28,14 +28,14 @@ public: virtual void bind(GLenum unit = GL_TEXTURE0) const; - void save(const glm::ivec2 & size, const char * path) const; - static void save(const glTexture &, const glm::ivec2 & size, const char * path); - static void saveDepth(const glTexture &, const glm::ivec2 & size, const char * path); - static void saveNormal(const glTexture &, const glm::ivec2 & size, const char * path); + void save(const char * path) const; + static void save(const glTexture &, const char * path); + static void saveDepth(const glTexture &, const char * path); + static void saveNormal(const glTexture &, const char * path); protected: - static void save(const glTexture &, GLenum, GLenum, const glm::ivec2 & size, unsigned short channels, - const char * path, short tgaFormat); + static void save(const glTexture &, GLenum, GLenum, unsigned short channels, const char * path, short tgaFormat); + static glm::ivec2 getSize(const glTexture &); glTexture m_texture; GLenum type; diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index 4fde5ed..59dc40d 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -32,7 +32,7 @@ public: glDisable(GL_DEBUG_OUTPUT); auto outpath = (TMP / boost::unit_test::framework::current_test_case().full_name()).replace_extension(".tga"); std::filesystem::create_directories(outpath.parent_path()); - Texture::save(outImage, size, outpath.c_str()); + Texture::save(outImage, outpath.c_str()); } void content(const SceneShader & shader) const override diff --git a/test/test-render.cpp b/test/test-render.cpp index 7771760..8c6b31c 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(basic) const TestScene scene; ss.render(scene); glDisable(GL_DEBUG_OUTPUT); - Texture::save(outImage, size, "/tmp/basic.tga"); + Texture::save(outImage, "/tmp/basic.tga"); } BOOST_AUTO_TEST_CASE(pointlight) @@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(pointlight) const PointLightScene scene; ss.render(scene); glDisable(GL_DEBUG_OUTPUT); - Texture::save(outImage, size, "/tmp/pointlight.tga"); + Texture::save(outImage, "/tmp/pointlight.tga"); } BOOST_AUTO_TEST_CASE(spotlight) @@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE(spotlight) const PointLightScene scene; ss.render(scene); glDisable(GL_DEBUG_OUTPUT); - Texture::save(outImage, size, "/tmp/spotlight.tga"); + Texture::save(outImage, "/tmp/spotlight.tga"); } BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3 From c44e7537d027cba66abe564b247549040426ebfe Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 02:56:33 +0100 Subject: Externalise a neater definition of TGAHead --- gfx/models/texture.cpp | 14 ++++++++------ gfx/models/texture.h | 2 +- gfx/models/tga.h | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 gfx/models/tga.h (limited to 'gfx/models') diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index ab256d5..3b27e77 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -1,5 +1,6 @@ #include "texture.h" #include "glArrays.h" +#include "tga.h" #include #include #include @@ -54,11 +55,9 @@ Texture::getSize(const glTexture & texture) } void -Texture::save(const glTexture & texture, GLenum format, GLenum type, unsigned short channels, const char * path, - short tgaFormat) +Texture::save( + const glTexture & texture, GLenum format, GLenum type, uint8_t channels, const char * path, uint8_t tgaFormat) { - using TGAHead = std::array; - const auto size = getSize(texture); const size_t dataSize = (static_cast(size.x * size.y * channels)); const size_t fileSize = dataSize + sizeof(TGAHead); @@ -66,8 +65,11 @@ Texture::save(const glTexture & texture, GLenum format, GLenum type, unsigned sh filesystem::fh out {path, O_RDWR | O_CREAT, 0660}; out.truncate(fileSize); auto tga = out.mmap(fileSize, 0, PROT_WRITE, MAP_SHARED); - *tga.get() = {0, tgaFormat, 0, 0, 0, 0, static_cast(size.x), static_cast(size.y), - static_cast(8 * channels)}; + *tga.get() = { + .format = tgaFormat, + .size = size, + .pixelDepth = static_cast(8 * channels), + }; glGetTextureImage(texture, 0, format, type, static_cast(dataSize), tga.get() + 1); tga.msync(MS_ASYNC); } diff --git a/gfx/models/texture.h b/gfx/models/texture.h index f4e1476..68ec649 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -34,7 +34,7 @@ public: static void saveNormal(const glTexture &, const char * path); protected: - static void save(const glTexture &, GLenum, GLenum, unsigned short channels, const char * path, short tgaFormat); + static void save(const glTexture &, GLenum, GLenum, uint8_t channels, const char * path, uint8_t tgaFormat); static glm::ivec2 getSize(const glTexture &); glTexture m_texture; diff --git a/gfx/models/tga.h b/gfx/models/tga.h new file mode 100644 index 0000000..1f400ef --- /dev/null +++ b/gfx/models/tga.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +struct TGAHead { + uint8_t idLength {}, colorMapType {}, format {}; + uint16_t __attribute__((packed)) colorMapFirst {}, colorMapLength {}; + uint8_t colorMapEntrySize {}; + glm::vec<2, uint16_t> origin {}, size {}; + uint8_t pixelDepth {}; + uint8_t descriptor {}; +}; +static_assert(sizeof(TGAHead) == 18); -- cgit v1.2.3 From 59733c3c3de7aa6dee5fde8e2a60b80b4c706583 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 15:21:36 +0100 Subject: Set GL_PACK_ALIGNMENT before saving texture to fit buffer correctly --- gfx/models/texture.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'gfx/models') diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 3b27e77..6319eb8 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -70,6 +70,7 @@ Texture::save( .size = size, .pixelDepth = static_cast(8 * channels), }; + glPixelStorei(GL_PACK_ALIGNMENT, 1); glGetTextureImage(texture, 0, format, type, static_cast(dataSize), tga.get() + 1); tga.msync(MS_ASYNC); } -- cgit v1.2.3 From b4ede2fd89d0effb44ebae15862427de9a147476 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 16:34:56 +0100 Subject: Define our own enum for texture mapmode --- gfx/models/texture.cpp | 19 +++++++++++++++++-- gfx/models/texture.h | 9 ++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 6319eb8..e29fd6c 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -13,6 +13,21 @@ Cache Texture::cachedTexture; +GLint +TextureOptions::glMapMode(TextureOptions::MapMode mm) +{ + switch (mm) { + case MapMode::Repeat: + return GL_REPEAT; + case MapMode::Clamp: + return GL_CLAMP_TO_EDGE; + case MapMode::Mirror: + return GL_MIRRORED_REPEAT; + default: + throw std::domain_error("Invalid MapMode value"); + } +} + Texture::Texture(const std::filesystem::path & fileName, TextureOptions to) : Texture {Image {Resource::mapPath(fileName).c_str(), STBI_rgb_alpha}, to} { @@ -30,8 +45,8 @@ Texture::Texture(GLsizei width, GLsizei height, const void * data, TextureOption glBindTexture(type, m_texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexParameteri(type, GL_TEXTURE_WRAP_S, to.wrap); - glTexParameteri(type, GL_TEXTURE_WRAP_T, to.wrap); + glTexParameteri(type, GL_TEXTURE_WRAP_S, TextureOptions::glMapMode(to.wrapU)); + glTexParameteri(type, GL_TEXTURE_WRAP_T, TextureOptions::glMapMode(to.wrapV)); glTexParameteri(type, GL_TEXTURE_MIN_FILTER, to.minFilter); glTexParameteri(type, GL_TEXTURE_MAG_FILTER, to.magFilter); diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 68ec649..86e76a0 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -9,9 +9,16 @@ class Image; struct TextureOptions { - GLint wrap {GL_REPEAT}; + enum class MapMode { + Repeat, + Clamp, + Mirror, + Decal, + }; + MapMode wrapU {MapMode::Repeat}, wrapV {MapMode::Repeat}; GLint minFilter {GL_LINEAR}, magFilter {GL_LINEAR}; GLenum type {GL_TEXTURE_2D}; + static GLint glMapMode(MapMode); }; class Texture { -- cgit v1.2.3 From be9a796e68a25f7567b7c16e00d9b42f5e0d9528 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 16:39:27 +0100 Subject: Write applicable Texture Options into texture atlas texture data --- gfx/models/texture.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index e29fd6c..e014f80 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -136,12 +136,15 @@ TextureAtlas::bind(GLenum unit) const } GLuint -TextureAtlas::add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions) +TextureAtlas::add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions to) { glTextureSubImage2D(m_texture, 0, position.x, position.y, size.x, size.y, GL_RGBA, GL_UNSIGNED_BYTE, data); struct Material { - glm::vec<2, uint16_t> position, size, unused1 {}, unused2 {}; - } material {position, size}; + glm::vec<2, uint16_t> position, size; + TextureOptions::MapMode wrapU; + TextureOptions::MapMode wrapV; + } material {position, size, to.wrapU, to.wrapV}; + static_assert(sizeof(Material) <= 32); glTextureSubImage2D(m_atlas, 0, 0, static_cast(used), 2, 1, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, &material); return ++used; } -- cgit v1.2.3 From 66da4f83c1b5bb6f3ceda880820e01c2f8e23e43 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 17:45:31 +0100 Subject: Remove the old .obj load, assets and supporting stuff --- Jamroot.jam | 1 - game/vehicles/railVehicleClass.cpp | 53 -------- game/vehicles/railVehicleClass.h | 9 -- gfx/models/obj.h | 38 ------ gfx/models/obj.impl.cpp | 77 ----------- gfx/models/obj.ll | 116 ----------------- res/brush47.mtl | 13 -- res/brush47.obj | 260 ------------------------------------- res/brush47.png | Bin 111263 -> 0 bytes test/Jamfile.jam | 1 - test/test-obj.cpp | 39 ------ 11 files changed, 607 deletions(-) delete mode 100644 gfx/models/obj.h delete mode 100644 gfx/models/obj.impl.cpp delete mode 100644 gfx/models/obj.ll delete mode 100644 res/brush47.mtl delete mode 100644 res/brush47.obj delete mode 100644 res/brush47.png delete mode 100644 test/test-obj.cpp (limited to 'gfx/models') diff --git a/Jamroot.jam b/Jamroot.jam index 2275dd6..35b48ea 100644 --- a/Jamroot.jam +++ b/Jamroot.jam @@ -22,7 +22,6 @@ project : requirements profile:on coverage:on tidy:all - tidy:bin/link-static/gfx/models/obj.cpp tidy:bin/link-static/lib/jsonParse.cpp tidy:boost-* tidy:bugprone-* diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 4e9263c..b7a3b1e 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -2,7 +2,6 @@ #include "gfx/gl/sceneShader.h" #include "gfx/gl/shadowMapper.h" #include "gfx/models/mesh.h" -#include "gfx/models/obj.h" #include "gfx/models/texture.h" #include #include @@ -22,25 +21,6 @@ #include #include -RailVehicleClass::RailVehicleClass(const std::string & name) : - RailVehicleClass {std::make_unique(Resource::mapPath(name + ".obj")), - Texture::cachedTexture.get(Resource::mapPath(name + ".png"))} -{ -} - -RailVehicleClass::RailVehicleClass(std::unique_ptr o, std::shared_ptr t) : - texture {std::move(t)}, maxSpeed(95._mph) -{ - wheelBase = bogieOffset(*o); - length = objectLength(*o); - const auto m = o->createMeshes(); - bodyMesh = m.at("Body"); - bogies[0] = m.at("Bogie1"); - bogies[1] = m.at("Bogie2"); -} - -RailVehicleClass::RailVehicleClass() { } - bool RailVehicleClass::persist(Persistence::PersistenceStore & store) { @@ -80,36 +60,3 @@ RailVehicleClass::shadows( bogies[b]->Draw(); } } - -float -RailVehicleClass::bogieOffset(ObjParser & o) -{ - float wheelBase {0}; - // offset bogie positions so they can be set directly - for (auto & object : o.objects) { // bogie object index - if (!object.first.starts_with("Bogie")) { - continue; - } - std::set> vertexIds; - for (const auto & face : object.second) { - for (const auto & faceElement : face) { - vertexIds.emplace(o.vertices[faceElement.x - 1].y, faceElement.x - 1); - } - } - const auto offset = (vertexIds.begin()->first + vertexIds.rbegin()->first) / 2; - for (const auto & v : vertexIds) { - o.vertices[v.second].y -= offset; - } - wheelBase += std::abs(offset); - } - return wheelBase; -} - -float -RailVehicleClass::objectLength(ObjParser & o) -{ - const auto mme = std::minmax_element(o.vertices.begin(), o.vertices.end(), [](const auto & v1, const auto & v2) { - return v1.y < v2.y; - }); - return mme.second->y - mme.first->y; -} diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h index 61ec4ec..1a52e2b 100644 --- a/game/vehicles/railVehicleClass.h +++ b/game/vehicles/railVehicleClass.h @@ -9,14 +9,10 @@ class SceneShader; class ShadowMapper; class Texture; -class ObjParser; class Location; class RailVehicleClass : public Asset { public: - explicit RailVehicleClass(const std::string & name); - RailVehicleClass(); - void render(const SceneShader &, const Location &, const std::array &) const; void shadows(const ShadowMapper &, const Location &, const std::array &) const; @@ -31,10 +27,5 @@ protected: friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; void postLoad() override; - -private: - RailVehicleClass(std::unique_ptr obj, std::shared_ptr); - static float bogieOffset(ObjParser & o); - static float objectLength(ObjParser & o); }; using RailVehicleClassPtr = std::shared_ptr; diff --git a/gfx/models/obj.h b/gfx/models/obj.h deleted file mode 100644 index e28f7de..0000000 --- a/gfx/models/obj.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#ifndef yyFlexLexer -# define yyFlexLexer objbaseFlexLexer -# include -#endif -#include -#include -#include -#include -#include -#include - -class Mesh; -class Vertex; - -class ObjParser : yyFlexLexer { -public: - explicit ObjParser(const std::filesystem::path & fileName); - explicit ObjParser(std::unique_ptr in); - - int yylex() override; - - std::vector vertices; - std::vector texCoords; - std::vector normals; - using FaceElement = glm::vec<3, unsigned int>; - using Face = std::vector; - using Faces = std::vector; - using Object = std::pair; - std::vector objects; - glm::length_t axis {0}; - - using NamedMeshesData = std::map, std::vector>>; - [[nodiscard]] NamedMeshesData createMeshData() const; - using NamedMeshes = std::map>; - [[nodiscard]] NamedMeshes createMeshes() const; -}; diff --git a/gfx/models/obj.impl.cpp b/gfx/models/obj.impl.cpp deleted file mode 100644 index 205abc8..0000000 --- a/gfx/models/obj.impl.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "obj.h" -#include -#include -#include -#include -#include // IWYU pragma: keep -#include -#include -#include -#include -#include -#include -#include - -ObjParser::ObjParser(const std::filesystem::path & fileName) : ObjParser {std::make_unique(fileName)} { } - -ObjParser::ObjParser(std::unique_ptr in) : yyFlexLexer(in.get()) -{ - assert(in); - ObjParser::yylex(); - assert(in->good()); - constexpr const glm::mat4 obj2ilt { - -1, 0, 0, 0, // x - 0, 0, 1, 0, // y - 0, 1, 0, 0, // z - 0, 0, 0, 0, // w - }; - std::for_each(vertices.begin(), vertices.end(), [&obj2ilt](auto & v) { - v = v * obj2ilt; - }); - std::for_each(normals.begin(), normals.end(), [&obj2ilt](auto & v) { - v = glm::vec4 {v, 0} * obj2ilt; - }); -} - -ObjParser::NamedMeshes -ObjParser::createMeshes() const -{ - NamedMeshes out; - const auto data {createMeshData()}; - std::transform(data.begin(), data.end(), std::inserter(out, out.end()), [](auto && obj) { - return std::make_pair(obj.first, std::make_shared(obj.second.first, obj.second.second)); - }); - return out; -} - -ObjParser::NamedMeshesData -ObjParser::createMeshData() const -{ - NamedMeshesData out; - std::transform(objects.begin(), objects.end(), std::inserter(out, out.end()), [this](auto && obj) { - std::vector overtices; - std::vector vertexOrder; - std::vector indices; - for (const auto & face : obj.second) { - for (auto idx = 2U; idx < face.size(); idx += 1) { - auto f = [&](auto idx) { - const auto & fe {face[idx]}; - if (const auto existing = std::find(vertexOrder.begin(), vertexOrder.end(), fe); - existing != vertexOrder.end()) { - indices.push_back(static_cast(std::distance(vertexOrder.begin(), existing))); - } - else { - indices.push_back(static_cast(overtices.size())); - overtices.emplace_back(vertices[fe.x - 1], texCoords[fe.y - 1], -normals[fe.z - 1]); - vertexOrder.emplace_back(fe); - } - }; - f(0U); - f(idx); - f(idx - 1); - } - } - return std::make_pair(obj.first, std::make_pair(overtices, indices)); - }); - return out; -} diff --git a/gfx/models/obj.ll b/gfx/models/obj.ll deleted file mode 100644 index 84884fe..0000000 --- a/gfx/models/obj.ll +++ /dev/null @@ -1,116 +0,0 @@ -%option batch -%option c++ -%option noyywrap -%option 8bit -%option stack -%option yyclass="ObjParser" -%option prefix="objbase" - -%{ -#include -#include -#include -#include -#include -class objbaseFlexLexer; -%} - -comment #.* -float -?[0-9.]+ -index -?[0-9]+ -linestring [^\r\n]* -truthy (1|on) -falsey (0|off) - -%x FACE -%x MTLLIB -%x OBJECT -%x SMOOTH -%x USEMTL -%x VERTEX -%x NORMAL -%x TEXCOORD - -%% - -{comment} { - // fprintf(stderr, "COMMENT %s\n", YYText()); -} -"f " { - BEGIN(FACE); - objects.back().second.emplace_back(); - objects.back().second.back().emplace_back(); - axis = 0; -} -"mtllib " { - BEGIN(MTLLIB); -} -"o " { - BEGIN(OBJECT); -} -"s " { - BEGIN(SMOOTH); -} -"usemtl " { - BEGIN(USEMTL); -} -"v " { - BEGIN(VERTEX); - vertices.emplace_back(0, 0, 0, 1); - axis = 0; -} -"vn " { - BEGIN(NORMAL); - normals.emplace_back(0, 0, 0); - axis = 0; -} -"vt " { - BEGIN(TEXCOORD); - texCoords.emplace_back(0, 1, 1); - axis = 0; -} -{linestring} { - // fprintf(stderr, "USEMTL <%s>\n", YYText()); -} -{linestring} { - // fprintf(stderr, "MTLLIB <%s>\n", YYText()); -} -{linestring} { - objects.emplace_back(YYText(), Faces{}); -} -{truthy} { - // fprintf(stderr, "Set smooth\n"); -} -{falsey} { - // fprintf(stderr, "Set flat\n"); -} -{float} { - vertices.back()[axis++] = std::stof(YYText()); -} -{float} { - normals.back()[axis++] = std::stof(YYText()); -} -{float} { - texCoords.back()[axis++] = std::stof(YYText()); -} -{index} { - objects.back().second.back().back()[axis] = std::stoi(YYText()); -} -\/ { - axis++; -} -[ \t] { - objects.back().second.back().emplace_back(); - axis = 0; -} - -<*>[ \t] { -} -<*>[\r\n\f] { - BEGIN(INITIAL); -} - -%% - -// Make iwyu think unistd.h is required -[[maybe_unused]]static auto x=getpid; diff --git a/res/brush47.mtl b/res/brush47.mtl deleted file mode 100644 index a4ba512..0000000 --- a/res/brush47.mtl +++ /dev/null @@ -1,13 +0,0 @@ -# Blender MTL File: 'brush47.blend' -# Material Count: 1 - -newmtl Brush47 -Ns 225.000000 -Ka 1.000000 1.000000 1.000000 -Kd 0.800000 0.800000 0.800000 -Ks 0.500000 0.500000 0.500000 -Ke 0.000000 0.000000 0.000000 -Ni 1.450000 -d 1.000000 -illum 2 -map_Kd brush47.png diff --git a/res/brush47.obj b/res/brush47.obj deleted file mode 100644 index 8fcb2b1..0000000 --- a/res/brush47.obj +++ /dev/null @@ -1,260 +0,0 @@ -# Blender v2.91.0 OBJ File: 'brush47.blend' -# www.blender.org -mtllib brush47.mtl -o Body -v -1.345000 1.200000 -9.690000 -v -1.345000 1.200000 9.690000 -v 1.345000 1.200000 -9.690000 -v 1.345000 1.200000 9.690000 -v -0.991447 3.900000 -8.536446 -v -1.345000 3.546447 -9.440000 -v -0.991447 3.900000 8.536446 -v -1.345000 3.546447 9.440000 -v 0.991447 3.900000 -8.536446 -v 1.345000 3.546447 -9.440000 -v 0.991447 3.900000 8.536446 -v 1.345000 3.546447 9.440000 -v -1.345000 2.723224 9.690000 -v 1.345000 2.723224 9.690000 -v 1.345000 2.723224 -9.690000 -v -1.345000 2.723224 -9.690000 -v 1.340000 0.500000 9.600000 -v -1.340000 0.500000 9.600000 -v -1.340000 1.200000 9.690000 -v -1.340000 0.500000 -9.600000 -v 1.340000 0.500000 -9.600000 -v -1.300000 0.200000 -2.250000 -v -1.300000 1.200000 -2.250000 -v -1.300000 0.200000 2.250000 -v -1.300000 1.200000 2.250000 -v 1.300000 0.200000 -2.250000 -v 1.300000 1.200000 -2.250000 -v 1.300000 0.200000 2.250000 -v 1.300000 1.200000 2.250000 -v 1.345000 1.200000 8.236500 -v 1.345000 1.200000 -8.236500 -v -1.345000 1.200000 -8.236500 -v -1.345000 1.200000 8.236500 -vt 0.025174 0.662500 -vt 0.025174 0.831250 -vt 0.037326 0.925000 -vt 0.957465 0.927083 -vt 0.971354 0.833333 -vt 0.971354 0.662500 -vt 0.900391 0.662500 -vt 0.096137 0.662500 -vt 0.025174 0.662500 -vt 0.025174 0.831250 -vt 0.037326 0.925000 -vt 0.957465 0.927083 -vt 0.971354 0.833333 -vt 0.971354 0.662500 -vt 0.900391 0.662500 -vt 0.096137 0.662500 -vt 0.605035 0.456250 -vt 0.605035 0.581250 -vt 0.359375 0.581250 -vt 0.359375 0.456250 -vt 0.605035 0.581250 -vt 0.605035 0.456250 -vt 0.359375 0.581250 -vt 0.359375 0.456250 -vt 0.026042 0.593750 -vt 0.971354 0.593750 -vt 0.026042 0.593750 -vt 0.971354 0.593750 -vt 0.088542 0.089583 -vt 0.088542 0.206250 -vt 0.918403 0.206250 -vt 0.918403 0.089583 -vt 0.926215 0.527083 -vt 0.926215 0.612500 -vt 0.834201 0.612500 -vt 0.834201 0.527083 -vt 0.083333 0.989583 -vt 0.913194 0.989583 -vt 0.037326 0.927083 -vt 0.152778 0.364583 -vt 0.197049 0.364583 -vt 0.204861 0.310417 -vt 0.144965 0.310417 -vt 0.083333 0.989583 -vt 0.913194 0.989583 -vt 0.037326 0.927083 -vt 0.152778 0.364583 -vt 0.197049 0.364583 -vt 0.204861 0.310417 -vt 0.144965 0.310417 -vt 0.834201 0.527083 -vt 0.834201 0.612500 -vt 0.927083 0.612500 -vt 0.927083 0.527083 -vt 0.114583 0.293750 -vt 0.114583 0.414583 -vt 0.010417 0.414583 -vt 0.010417 0.293750 -vt 0.010417 0.293750 -vt 0.010417 0.414583 -vt 0.114583 0.414583 -vt 0.114583 0.293750 -vt 0.043403 0.014583 -vt 0.002604 0.014583 -vt 0.002604 0.081250 -vt 0.043403 0.081250 -vt 0.043403 0.014583 -vt 0.002604 0.014583 -vt 0.002604 0.081250 -vt 0.043403 0.081250 -vn -1.0000 0.0000 0.0000 -vn 1.0000 0.0000 0.0000 -vn 0.0000 0.0000 -1.0000 -vn 0.0000 0.0000 1.0000 -vn -1.0000 0.0071 0.0000 -vn 1.0000 0.0071 -0.0000 -vn -0.3335 -0.9344 -0.1252 -vn 0.3335 -0.9344 -0.1252 -vn 0.3335 -0.9344 0.1252 -vn -0.3335 -0.9344 0.1252 -vn 0.6755 -0.1083 -0.7294 -vn 0.6276 -0.5509 -0.5500 -vn -0.6276 -0.5509 -0.5500 -vn -0.6755 -0.1083 -0.7294 -vn 0.6276 -0.5509 0.5500 -vn -0.6276 -0.5509 0.5500 -vn -0.6755 -0.1083 0.7294 -vn 0.6755 -0.1083 0.7294 -vn 0.8867 0.0030 -0.4622 -vn -0.6937 0.0482 -0.7186 -vn -0.6937 0.0482 0.7186 -vn 0.6937 0.0482 0.7186 -vn -0.6128 0.1051 -0.7832 -vn 0.6145 0.1049 -0.7819 -vn 0.0000 0.1275 -0.9918 -vn 0.6128 0.1051 0.7832 -vn -0.6128 0.1051 0.7832 -usemtl Brush47 -s off -f 4/1/1 14/2/1 12/3/1 10/4/1 15/5/1 3/6/1 31/7/1 30/8/1 -f 1/9/2 16/10/2 6/11/2 8/12/2 13/13/2 2/14/2 33/15/2 32/16/2 -f 22/17/2 23/18/2 25/19/2 24/20/2 -f 24/20/3 25/19/3 29/21/3 28/22/3 -f 28/22/1 29/21/1 27/23/1 26/24/1 -f 26/24/4 27/23/4 23/18/4 22/17/4 -f 4/1/5 30/8/5 17/25/5 -f 31/7/5 3/6/5 21/26/5 -f 1/9/6 32/16/6 20/27/6 -f 33/15/6 2/14/6 18/28/6 -s 1 -f 11/29/7 7/30/8 5/31/9 9/32/10 -f 13/33/11 8/34/12 12/35/13 14/36/14 -f 5/37/9 7/38/8 8/12/12 6/39/15 -f 7/40/8 11/41/7 12/42/13 8/43/12 -f 11/44/7 9/45/10 10/4/16 12/46/13 -f 9/47/10 5/48/9 6/49/15 10/50/16 -f 15/51/17 10/52/16 6/53/15 16/54/18 -f 2/55/19 13/56/11 14/57/14 4/58/20 -f 3/59/21 15/60/17 16/61/18 1/62/22 -f 17/63/23 18/64/24 19/65/25 4/66/20 -f 20/67/26 21/68/27 3/69/21 1/70/22 -o Bogie2 -v -0.717500 0.000000 -2.700000 -v -0.717500 0.000000 -9.300000 -v -0.717500 1.200000 -2.700000 -v 0.717500 1.200000 -2.700000 -v 0.717500 0.000000 -2.700000 -v 0.717500 0.000000 -9.300000 -v 1.017500 1.200000 -2.700000 -v 1.017500 0.000000 -2.700000 -v 1.017500 0.000000 -9.300000 -v -1.017500 0.000000 -9.300000 -v -1.017501 0.000000 -2.700000 -v -1.017501 1.200000 -2.700000 -v -0.717500 0.654015 -9.300000 -v -0.717500 1.200000 -8.236500 -v -0.717500 0.661560 -9.285304 -v 0.717500 1.200000 -8.236500 -v 0.717500 0.654015 -9.300000 -v 1.017500 1.200000 -8.236500 -v 1.017500 0.654015 -9.300000 -v -1.017500 0.654015 -9.300000 -v -1.017500 1.200000 -8.236500 -vt 0.342014 0.591667 -vt 0.342014 0.435417 -vt 0.006076 0.435417 -vt 0.006076 0.520575 -vt 0.006824 0.521557 -vt 0.060208 0.591667 -vt 0.006076 0.435417 -vt 0.342014 0.435417 -vt 0.342014 0.591667 -vt 0.060208 0.591667 -vt 0.006076 0.520575 -vt 0.217014 0.270833 -vt 0.552951 0.270833 -vt 0.552951 0.427083 -vt 0.271146 0.427083 -vt 0.217014 0.355992 -vt 0.552083 0.429167 -vt 0.552083 0.272917 -vt 0.216146 0.272917 -vt 0.216146 0.358075 -vt 0.270278 0.429167 -vn 1.0000 0.0000 -0.0000 -vn -1.0000 0.0000 0.0000 -usemtl Brush47 -s off -f 36/71/28 34/72/28 35/73/28 46/74/28 48/75/28 47/76/28 -f 39/77/29 38/78/29 37/79/29 49/80/29 50/81/29 -f 42/82/29 41/83/29 40/84/29 51/85/29 52/86/29 -f 45/87/28 44/88/28 43/89/28 53/90/28 54/91/28 -o Bogie1 -v 0.717500 0.000000 2.700000 -v 0.717500 1.200000 2.700000 -v 0.717500 0.000000 9.300000 -v -0.717500 0.000000 2.700000 -v -0.717500 1.200000 2.700000 -v -0.717500 0.000000 9.300000 -v 1.017500 0.000000 2.700001 -v 1.017500 1.200000 2.700001 -v 1.017500 0.000000 9.300000 -v -1.017500 0.000000 2.700000 -v -1.017500 1.200000 2.700000 -v -1.017500 0.000000 9.300000 -v 0.717500 0.654015 9.300000 -v 0.717500 1.200000 8.236500 -v 1.017500 0.654015 9.300000 -v 1.017500 1.200000 8.236500 -v -0.717500 1.200000 8.236500 -v -0.717500 0.661372 9.285670 -v -0.717500 0.654015 9.300000 -v -1.017500 1.200000 8.236500 -v -1.017500 0.654015 9.300000 -vt 0.342014 0.435417 -vt 0.006076 0.435417 -vt 0.006076 0.520575 -vt 0.060208 0.591667 -vt 0.342014 0.591667 -vt 0.006076 0.520575 -vt 0.006076 0.435417 -vt 0.342014 0.435417 -vt 0.342014 0.591667 -vt 0.060208 0.591667 -vt 0.006806 0.521533 -vt 0.552083 0.270833 -vt 0.216146 0.270833 -vt 0.216146 0.355992 -vt 0.270278 0.427083 -vt 0.552083 0.427083 -vt 0.215278 0.355992 -vt 0.215278 0.270833 -vt 0.552083 0.270833 -vt 0.552083 0.427083 -vt 0.269549 0.427083 -vn -1.0000 0.0000 -0.0000 -vn 1.0000 -0.0000 0.0000 -usemtl Brush47 -s off -f 55/92/30 57/93/30 67/94/30 68/95/30 56/96/30 -f 73/97/31 60/98/31 58/99/31 59/100/31 71/101/31 72/102/31 -f 61/103/30 63/104/30 69/105/30 70/106/30 62/107/30 -f 75/108/31 66/109/31 64/110/31 65/111/31 74/112/31 diff --git a/res/brush47.png b/res/brush47.png deleted file mode 100644 index 3d435d6..0000000 Binary files a/res/brush47.png and /dev/null differ diff --git a/test/Jamfile.jam b/test/Jamfile.jam index d91af1d..b0eed5e 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -44,7 +44,6 @@ project : requirements lib test : [ glob *.cpp : test-*.cpp perf-*.cpp ] ; run test-collection.cpp ; -run test-obj.cpp ; run test-maths.cpp ; run test-lib.cpp ; run test-geo.cpp ; diff --git a/test/test-obj.cpp b/test/test-obj.cpp deleted file mode 100644 index e6e725d..0000000 --- a/test/test-obj.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#define BOOST_TEST_MODULE test_obj - -#include - -#include -#include -#include -#include -#include -#include -#include - -BOOST_AUTO_TEST_CASE(objparse) -{ - const ObjParser op {RESDIR "/brush47.obj"}; - BOOST_CHECK_EQUAL(75, op.vertices.size()); - BOOST_CHECK_EQUAL(112, op.texCoords.size()); - BOOST_CHECK_EQUAL(31, op.normals.size()); - BOOST_CHECK_EQUAL(3, op.objects.size()); - const auto & object {op.objects.front()}; - BOOST_CHECK_EQUAL("Body", object.first); - BOOST_CHECK_EQUAL(21, object.second.size()); - BOOST_CHECK_EQUAL(8, object.second[0].size()); - BOOST_CHECK_EQUAL(8, object.second[1].size()); - BOOST_CHECK_EQUAL(4, object.second[12].size()); -} - -BOOST_AUTO_TEST_CASE(create_meshes) -{ - const ObjParser op {RESDIR "/brush47.obj"}; - const auto ms = op.createMeshData(); - BOOST_REQUIRE_EQUAL(3, ms.size()); - BOOST_REQUIRE_EQUAL("Body", ms.begin()->first); - const auto & o = ms.at("Body"); - BOOST_REQUIRE_EQUAL(88, o.first.size()); - const auto & v = o.first.front(); - BOOST_REQUIRE_CLOSE(-1.345, v.pos.x, 1); - BOOST_REQUIRE_EQUAL(138, o.second.size()); -} -- cgit v1.2.3