mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
* Enhance Windows build matrix with AVX2 variants * fix(lang): the two Polish lines of the download area written with \n, so they match the tooltip and the hint * fix(files): data named in another case is found on Linux, folders included The parser and the loaders lowercase every name they read, while the data made on Windows has files and folders named in any case (scenery/Braniewo, textures/siec_trakcyjna_ph/.../STS_*.tga, sounds/sip/Gorki.wav). FileCased matched the file name only, so a folder in another case wasn't found. - FileCased walks every part of the path on Linux, the folder listings kept by their lowercase and read again once the folder changed; Windows as it was - FileExists finds the file in any case on Linux, and the variant with names and extensions gives back the name as it's on the disk, so textures, materials, sounds, station announcements and scripts open that name - models, sound captions, timetables and nodebank previews open the file as it's named on the disk; the texture format is told by the extension in any case * fix(files): the banks keep the names asked for, only the opening takes the case of the disk The textures, materials and sounds are kept by the names they're asked for. Given back as named on the disk, a file in another case missed the bank and was loaded again on every request. - FileExists with names and extensions finds the file in any case but gives back the name asked for; FileOnDisk gives the file to open, used where textures, sounds and scripts are opened - FileExists of a single name is a plain check again, the places checking it and opening the same name stay as they were - the texture format is told by the extension asked for again - a folder changed shortly before it was read is read again for a name missing from it, the time of a change being as coarse as the clock of the file system; a listing cut short isn't kept * fix(files): a folder changed in the future isn't read again on every name missing from it * perf(files): a file missing costs a single check, as before Measured in a case sensitive folder (3000 textures, every 20th in mixed case, looked up the way Texture.cpp does), the walk over every part of the path made a texture lookup 4-5 times slower than master: each name tried and missing checked every folder of its path. - the folders found are kept by the name they're given with, the missing ones for a second, so a missing file checks its folder alone - a folder named as it is on the disk is taken without its listing - the time a folder was changed is looked at once a second at most The lookup is now as fast as master's (about 180 us per texture on NTFS in both) while finding the files named in another case too. * fix(files): terrain files named in another case are found on Linux The terrain directive names .txtf/.btf files in lowercase, as the parser gives every token. The name is now taken as it is on disk, from the text file first, so the binary file is made next to the text and the editor's references and layers keep one name. * fix(files): models named in another case are found on Linux The manager of the models looked for the file with FileExists of a single name, a plain check again since the banks keep the names asked for, so a model named in another case (models/przejazdy/plyty3_L, dynamic/pkp/st44_v2/main/2M62-0685-A) was reported missing before TModel3d::LoadFromFile() could open it as it's named on the disk. find_on_disk() finds the file in any case and gives back the name asked for, the key of the bank. * refactor(files): named functions instead of the lambdas of the folder cache (Sonar S2259, S3608, S6004) Sonar took the folder captured by the lambda reading it for a reference to null, failing the quality gate on reliability. The folder is read and the path kept by functions in the anonymous namespace, the age of the listing declared in its if. * fix: the simulator opens more than 512 files at once, and a shader it can't open says why With "physicslog yes" every driver keeps its .dat open, and a large scenery (Tomaszewo: some 500 vehicles, cars of the roads included) took all 512 streams the MSVC runtime gives: the next fopen failed and the simulator stopped at "cannot read shader: shaders/map.vert" on opening the map. The limit is raised to 8192 at the start (the soft limit of open files up to the hard one on Linux), and a shader file that doesn't open is told as such, with the system's reason, rather than as one that can't be read. * feat(blackbox): the black box on its own, writing the tapes of the user's vehicles at the end "blackbox yes" in eu07.ini records the black box with the physics logs on or off, "blackbox no" never; with no entry it goes with "physicslog yes" as before. With the physics logs off, the recorder keeps the line of the physics log of the driver the user is with, every 0.05 s, and writes it to physicslog/<vehicle>.dat when the recording closes: the speed recorder's tape of the vehicles the user was in, without every vehicle keeping a log open and written twenty times a second. The line and the header of the log are made by one function of the driver, which its own log writes too, and the file is opened the same way, so the two are the same to the byte: the readers of these logs (the old Hasler tape viewer, the starter) read them as before. * feat(physicslog): the logs written every two seconds by a thread of their own, no file kept open; the black box's tapes as it goes "physicslog yes" kept the .dat of every vehicle with a driver open for the whole run and flushed it twenty times a second from the simulation's thread: some 580 open files on Tomaszewo. The lines are kept now and a thread of their own writes them every two seconds, each file opened only for the moment it is written to. The files, their lines and their text mode stay as they were. - The black box, with the physics logs off, writes the tapes of the user's vehicles through the same writer as it goes, not at the end. A log begins when the user is first in the vehicle and goes on while the user looks around outside, until another vehicle is taken; back in it, the log goes on after the gap. The vehicle's own driver keeps the log only if it is the primary one, as a passive one's clock stands still; otherwise the driver of its train does. - What is kept is written when the simulator closes, after the black box, at the end of a test run, and when the test watchdog ends a run. A crash loses the last two seconds. - On Tomaszewo with the black box alone, the one tape grows every two seconds. With "physicslog yes", the 581 logs grow and the simulator holds as many handles as without them; writing them takes the thread about 1.9 s, which the closing waits for once. * feat(blackbox): the cab record holds the speed of the wheels, apart from the vehicle's when they slip The speed of the cab record is the vehicle's. The speedometer, and a speed recorder's tape with it, shows the speed of the wheels as they turn, which runs away from it while they slip and drops to 0 while they are locked in a skid. It is added at the end of the record (194 bytes), where the readers skip what they don't know; the .dat tapes have it already, in their second column. * fix(ci): build OpenAL Soft with the PipeWire, PulseAudio and ALSA backends, so the Linux game has sound The Linux job builds OpenAL Soft 1.24.2 from source, but the runner had none of the headers of its audio backends, and OpenAL Soft's CMake leaves out every backend it can't find. The bundled libopenal.so.1 had only OSS, Null, Wave and Loopback. Modern distributions and the Steam Linux Runtime have no /dev/dsp, so the game logged "Failed to obtain audio device" and played nothing, with any Steam Linux Runtime and without one. - install libasound2-dev, libpulse-dev and libpipewire-0.3-dev before building OpenAL Soft - configure it with ALSOFT_REQUIRE_PIPEWIRE/PULSEAUDIO/ALSA, so a missing header fails the build instead of shipping a library without sound The backends are loaded with dlopen at run time (ALSOFT_DLOPEN is on by default), so the bundled library gains no new dependency. Tested by putting an OpenAL Soft 1.24.2 with these backends next to the game from the Steam depot: under Steam Linux Runtime 4.0 the sound works. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(editor): the certificates of https looked for where the distribution keeps them The Linux game bundles the libcurl of Ubuntu, which knows only Ubuntu's /etc/ssl/certs/ca-certificates.crt. Fedora, RHEL and openSUSE keep the certificates elsewhere, so every download of the editor (orthophoto, terrain model, topo map) failed there with a certificate error. The file is taken from SSL_CERT_FILE or CURL_CA_BUNDLE when set, else the first of the files the distributions keep, and passed as CURLOPT_CAINFO. With none found libcurl keeps its own. * fix(editor): an https proxy checked with the same certificates CURLOPT_PROXY_CAINFO gets the file found for CURLOPT_CAINFO, so a download through an https:// proxy on Fedora or RHEL doesn't fail on the proxy's certificate. * fix(ci): the no-AVX2 Windows build has its own concurrency group and its own public download After master was merged the two Windows variants shared one concurrency group with cancel-in-progress, so one cancelled the other, and the publish job looked for an artifact name the matrix no longer produces. Each variant now has its own group, the publish job downloads both, and the release carries maszyna-windows-x64.zip (AVX2, the old name, so the links already handed out still work) and maszyna-windows-x64-noavx2.zip. The AVX2 variant passes -DENABLE_AVX2=ON explicitly instead of an empty flag. * build: a build without AVX2 has _noavx2 at the end of its executable name With ENABLE_AVX2=OFF the executable is eu07-x64_<date>_<hash>_noavx2.exe (eu07-x64_noavx2.exe without ADD_VERSION_SUFFIX), so a starter can tell the two builds apart by the name alone and pick this one on a CPU without AVX2. The AVX2 build keeps its name. * ci: Linux is built without AVX2 too, published as maszyna-linux-x64-noavx2.tar.gz The Linux job becomes the same two-variant matrix as Windows (ENABLE_AVX2=ON/OFF), each variant with a concurrency group of its own. The publish job downloads both Linux artifacts, restores the executable bits in both and releases maszyna-linux-x64.tar.gz (AVX2, the old name) next to maszyna-linux-x64-noavx2.tar.gz. The no-AVX2 executable is eu07_<date>_<hash>_noavx2. * fix(renderer): the legacy renderer clears the arrays the scene left before it draws the ui The legacy renderer drew the ui with the client arrays of the last scene draw still enabled: the texture coordinates of the normal map unit and the normals, pointing into the vertex buffer of whatever was drawn last, 48 bytes a vertex. The ImGui backend sets up the vertex, colour and texture coordinates of the active unit only, so the driver read the stale arrays with the indices of the ui. Once the ui had more vertices than that buffer, the NVIDIA driver read past its end and the game crashed; a crash dump of the editor (an area selected, a track being laid) shows the read of vertex 15113 at a 48-byte stride in ImGui_ImplOpenGL2_RenderDrawData. The arrays are now disabled on every texture unit the geometry uses, with the normal and colour arrays, inside the glPushClientAttrib already there, so the scene gets its state back after the ui. The default and the experimental renderers don't go through this code. * fix(app): a renderer this build doesn't have starts the full one, not the legacy one The starter writes "gfxrenderer vulkan" for the Vulkan renderer of the builds that have it. This exe doesn't know the word, and every word other than default/experimental fell to the legacy renderer: a player who picked Vulkan got the slowest renderer, the one whose ui crashes on NVIDIA (the arrays left by the scene). "experimental" in a build without the NVRHI renderer ended with "no renderer found!" and no game at all. After the settings are read, a gfxrenderer word this build has no renderer for becomes "default", with a line in the log saying so. The words it knows stay as they were: full/default, legacy, simple, null, the older starters' simpleshader and off (legacy as before), and experimental where it is built. * fix(settings): the gfxrenderer word read in any case, as the starter reads it A hand-written "gfxrenderer Legacy" started the legacy renderer only because every unknown word did; with unknown words going to the full renderer it would have changed. The word is lowercased when read, as StarterNG's RenderEngineOf does, so Legacy, Simple or Full mean what they say. * fix(physicslog): a log whose file can't be written keeps its lines and header, and the writer is stopped once on every way out A failed write put its lines back, ahead of those since, so a file held by another program for a while gets its header and every line once it can be written; up to 4 MB kept per log. Stopping is joined once, and not from the writer's own thread. main stops the logs after an error it caught too, and the test watchdog flushes with a time limit, so a disk that doesn't answer doesn't keep the run from ending. * fix(blackbox): the wheels of the powered vehicle, as the speedometer has them, and the logs described as the drivers' they are In a unit driven from an unpowered car, the speed of the wheels was of the car the user is in, whose wheels never slip; now of the motor car, signed as the occupied vehicle's speed. The doc tells of a log per driver, and of the two seconds of real time a crash loses. * fix(physicslog): failed logs kept within 64 MB in all, cut outside the lock, never written twice; the writer knows itself when stopped The lines kept while files can't be written are cut to their share of 64 MB before the lock the simulation waits on. A log written in part is cut back to its length, so the next try doesn't write its start twice. A stop from the writer itself is told by its id even after another thread took it to join, and the watchdog's flush waits at most its time, the writing on a thread of its own. * feat(physicslog): the logs of the trains only, physicslog.trains "physicslog.trains yes" in eu07.ini writes the .dat log of each driver who drives rail vehicles, not of the passengers nor of the road vehicles that make "physicslog yes" hundreds of files on a big scenery: the tape of every train. A driver made primary later begins its log then, and isn't counted while it doesn't log. The black box goes with it when the ini says nothing of its own, and doesn't write a second log of a driver writing its own. The doc tells the doors' sides are the vehicle's, the driver's swapped in the rear cab. * fix(physicslog): a train's log begun at its first line, so passive crews get no file Every crew is primary in the constructor and the drivers but one of a train with crews in more vehicles are made passive only before it starts, so a log begun there left files with only the header. With physicslog on all logs begin at once as before; one of a train waits for update_logs. * fix(editor): the texture pickers let go of the images scrolled past, the memory no longer runs out The picker of the textures of a track offers every texture of the textures folder, and each tile that came into sight read its texture full size, into the graphics card and the memory, and kept it there for good. Scrolling through the list for a trackbed texture took about 2.5 MB a tile: some 900 tiles made 3.5 GB, the whole folder 28 GB, and the game went down once the memory ran out. An image out of sight for 3 seconds is now taken out of both again (texture_manager::unload, Unload_Texture of the renderers) and read anew when it's shown once more, a few a frame as before. Only a texture the scene has never drawn is let go; those of the scene stay to the resource sweep. The texture browser lets go of its images on Refresh too. The picker no longer lists textures/previews: the 4100 previews of the node bank are images of models, not textures of anything. Checked on td.scn by a scripted run: 8000 images of the picker read one batch a few frames after another held the game at 2-3 GB, where it went to 28 GB and fell before; images shown again after they were let go come back; a trackbed texture picked from the folder is drawn on the track after its image was let go. * fix(physicslog): the writer's destructor throws nothing, its wait a plain condition variable woken to stop; a driver of no vehicle controls nothing * fix(editor): the images of the materials let go of with all their textures, the browser's Refresh a frame later An image of a material read the material with all its textures, the normal and specular maps and the variants, and only the first one was let go of: those stayed in the memory for every material scrolled past. They are let go of together now (Unload_Material). texture_manager::unload also takes the textures read and not made yet, waiting for a worker still reading one, and deletes the gl texture itself: release() would read it back from the card with gfx.resource.move only for it to be dropped. The Refresh of the texture browser let go of the images in the middle of the frame, while panels drawn before it could still refer to them; it now marks them, and they go at the start of the next frame. Checked on td.scn by a scripted run: 8040 images of the picker read a batch every few frames end at 2.0 GB, against 2.3-2.6 GB before this and 1.1 GB at the start; a trackbed material picked from the folder is drawn on the track after its image was let go. * fix(physicslog): every stop asked wakes the writer, the one from itself too; no psyche for a driver of no vehicle * fix(editor): the image of a material with no texture in its first slot is let go of too Its normal and specular maps were read with it and stayed in the memory: the trim passed over every image with no texture of its own, the material along with it. * fix(physicslog): nothing that goes wrong in the writing ends the simulator; a name in 1250 makes a path on a Windows set to UTF-8 The writer's thread called flush() with nothing around it, so an exception leaving write() ended the process with std::terminate, with no message. One that happens: the file of a log was made a std::filesystem::path of the vehicle's name, which MSVC converts in the code page of the system and throws for bytes not of it; on a Windows with "Use Unicode UTF-8 for worldwide language support" a vehicle named in 1250, as the sceneries have them (0xB3 for ł), isn't UTF-8. - the file of a log is made of the name with MultiByteToWideChar without MB_ERR_INVALID_CHARS: the same file as before for every name of the code page, U+FFFD for the bytes not of it instead of a throw. Elsewhere the bytes are the name of the file as they were - a log whose writing throws for any other reason (memory) is kept for the next try as one whose file can't be written, and the other logs are written still - the thread, flush_for() and stop() write through flush_safely(), which lets nothing out: what was taken for a writing failing as a whole is lost and told in the log, the next writing goes on - the messages name the file by the bytes of the name, not by path::string(), which converts back Tests: a log which can't be written doesn't keep the others from theirs; a vehicle named in 1250 has its log, read back by physicslog_writer::file(). Reported in the review of #385. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(blackbox): the tapes go on after the editor, with the physics logs off The tapes of the user's vehicles were written by update() after the check for the writer of the recording, which close() drops on entering the editor (F11). With "physicslog no", the speed recorder's tape ended at F11 while the drivers' own logs of "physicslog yes" went on. The tapes are files of their own: they are kept from the moment the recording was to be made, the file made or not, closed or not, until the simulator closes, after the gap of the time in the editor. Only the recording itself stops at F11, as before. Reported in the review of #385. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(physicslog): what no file name may have on Windows is replaced in the name of a log, on every system A spawned vehicle is named <spawn>:<number>, and its log went to physicslog/<spawn>:<number>.dat. On NTFS that isn't even an error: it is the stream <number>.dat of the file <spawn>, which nothing lists, so the log was written and no one could find it. On Linux the file was there, under a name Windows can't open. The name of the file of a log has < > : " / \ | ? * and the control characters replaced by _, on every system, so the logs are named alike everywhere (spawn_ezt:12 is spawn_ezt_12.dat), and a _ before a name Windows keeps for a device (con, nul, com1..., with an extension too). The bytes of 1250 stay. The black box tells the log it begins by the same name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(renderer): a material's textures let go of in one place, for every backend Unload_Material walked the textures of a material and of its variants the same way in both opengl renderers, and Sonar counted its declarations, set into the long run of overrides the two headers share, as duplicated: 5.2% of the new code, 3% allowed. The walk is gfx_renderer's now, over IMaterial::GetAllTextures and the backend's Unload_Texture. Materials that don't tell their textures, the Vulkan renderer's, give none, and are left alone as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(physicslog): road vehicles keep no log, the lines are made into text by the writer, and a slow disk gets them less often On a big scenery "physicslog yes" wrote a log for every driver, the hundreds of cars of the roads too: Tomaszewo gives over a thousand. A player with an i7-3770 and a hard disk had 1 fps there and 90 fps paused; with the physics log off the frame rate came back. The disk was busy with the logs all the time, and the simulation formatted 26 values a line, twenty lines a second, for each of them. - "physicslog yes" writes the logs of all drivers but those of road vehicles. Nobody reads those, and the starter's tapes are of rail vehicles. The black box writes the tape of a car the user drives, as it does with the physics logs off. On Tomaszewo a 120 s test run gives 20 logs instead of 579. - The simulation hands the writer the values of a line; the writer's thread makes them into text, with the same stream as before. The logs of a fixed-seed run of Tomaszewo come out byte for byte the same as before. - The writer's thread does its disk work at background priority (Windows THREAD_MODE_BACKGROUND_BEGIN, Linux the lowest best-effort I/O priority), so the simulation's own reading doesn't wait behind the logs. - A writing that takes over a third of the 2 s interval makes the next wait three times as long as it took: the disk is busy with the logs a quarter of the time at most. The log of the simulator says so once. - A log is written with one write, the header of a log begun with its lines, and a log added to is opened at its end rather than asked for its length first. What changes for the data: with "physicslog yes" no .dat files of road vehicles appear in physicslog/. Nothing in the SVN needs to change; the comment of physicslog in the shipped eu07.ini ("for all vehicles manned by AI or a human") is now a little wide of it. * fix(physicslog): the wait after a slow writing is ten seconds at most, a log whose lines can't be made into text is kept - The wait after a slow writing grew with what it left to the next one, with no end once the formatting and the bytes took a quarter of the time: it is five intervals at most now, so a crash loses ten seconds at most. - The lines given as values are made into text within the try of each log, aside and added at once: out of memory there keeps that log for the next try and the others are written, as the comment said. - The Windows background mode is described as it is: very low I/O priority, the memory pages let go of first. - The black box doc says a car the user drives has no log with the black box off. * fix(physicslog): the closing waits for the logs a second at most, the rest is dropped stop() gives the writing one second (a constructor parameter, ClosingWait). The writer thread's batch under way and the final writing check the time before each log and drop the logs past it, telling how many in the log. The final writing runs on a thread of its own, left behind when it isn't done in time, so a disk that doesn't answer in the middle of a file doesn't hold the closing either; the simulator ends with _Exit, and a writer object that goes waits for those writings in its destructor. At the closing the writer thread's disk priority is put back to normal, so that second goes to the logs. A flush() after the stop is not cut short. * fix(physicslog): a second stop keeps the time of a closing under way, and a stop lets out no exception The simulator stops the logs twice on a normal exit (Application.exit() and main). A stop while an earlier closing is still writing now keeps that closing's time instead of giving it another second, and doesn't tell it again. The thread of the closing is begun in a noexcept helper: with no memory or no thread to be had the rest is written on the calling thread, the logs past the time dropped still, so stop() doesn't throw on the way out of an out-of-memory error. * fix(physicslog): a closing whose thread is begun isn't written again by the stop when its log line fails std::format of the timeout line can throw (out of memory) after the closing's thread was started; close_aside returned false then, and stop() wrote on the calling thread too, waiting on the files the closing holds with no time limit. A thread begun counts now. * fix(physicslog): a failed log keeps its values in order, and the user's log goes first at the closing A log whose file couldn't be written kept the values the writing failed to make into text (out of memory) only until the next line came: keeping it put the values given since in their place. They're made into text again now, and the values given since go after them, added at once. Values which still can't be made into text are dropped when text was given after them, as the order can't be kept otherwise. What a writing needs before it takes the lines (the names put first, the order) is had before it takes them, so without the memory for it the lines are left for the next writing. The closing, which waits a second for the writing, dropped the logs past it by the order of their names: the user's train could lose its last seconds while a log nobody reads was written. Now: - the logs put first (put_first) are written before the others at each writing: the one of the driver the black box follows, of the vehicle the user is in (the black box goes with the physics logs unless "blackbox no"), and those of the drivers of the tested trains (test.vehicle); - a stop ends a writing of the thread under way at the log at hand, and the thread begins none after it. The logs it hasn't come to are left to the closing's writing, which has them with the lines given since, those put first first. Before, the thread went on through them and the closing's writing, coming after, had no time left for the newest lines. They go back as they were taken: their values are made into text by the closing's writing, not on the way to it unless text was given since, and they don't count toward the 64 MB kept of the logs whose file can't be written. The closing drops the logs past its time as before, the user's too: none is begun once the time is over, only the one at hand may still be written while the process ends. The thread no longer raises its disk priority at a closing, as it no longer writes there. --------- Co-authored-by: maj00r <maj00r@host> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Hirek193 <polomarket73@gmail.com>
2954 lines
94 KiB
C++
2954 lines
94 KiB
C++
/*
|
|
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/.
|
|
*/
|
|
/*
|
|
MaSzyna EU07 locomotive simulator
|
|
Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others
|
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "model/Model3d.h"
|
|
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/Logs.h"
|
|
#include "utilities/utilities.h"
|
|
#include "rendering/renderer.h"
|
|
#include "utilities/Timer.h"
|
|
#include "simulation/simulation.h"
|
|
#include "simulation/simulationtime.h"
|
|
#include "world/mtable.h"
|
|
#include "scene/sn_utils.h"
|
|
|
|
#include <bit>
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
using namespace Mtable;
|
|
|
|
float TSubModel::fSquareDist = 0.f;
|
|
std::uintptr_t TSubModel::iInstance; // numer renderowanego egzemplarza obiektu
|
|
std::uint32_t TSubModel::iVariantSeed = 0; // ziarno losowania wariantów tekstur renderowanego egzemplarza
|
|
texture_handle const *TSubModel::ReplacableSkinId = nullptr;
|
|
int TSubModel::iAlpha = 0x30300030; // maska do testowania flag tekstur wymiennych
|
|
TModel3d *TSubModel::pRoot; // Ra: tymczasowo wskaźnik na model widoczny z submodelu
|
|
std::string *TSubModel::pasText;
|
|
// przykłady dla TSubModel::iAlpha:
|
|
// 0x30300030 - wszystkie bez kanału alfa
|
|
// 0x31310031 - tekstura -1 używana w danym cyklu, pozostałe nie
|
|
// 0x32320032 - tekstura -2 używana w danym cyklu, pozostałe nie
|
|
// 0x34340034 - tekstura -3 używana w danym cyklu, pozostałe nie
|
|
// 0x38380038 - tekstura -4 używana w danym cyklu, pozostałe nie
|
|
// 0x3F3F003F - wszystkie wymienne tekstury używane w danym cyklu
|
|
// Ale w TModel3d okerśla przezroczystość tekstur wymiennych!
|
|
|
|
TSubModel::~TSubModel()
|
|
{
|
|
|
|
if (iFlags & 0x0200)
|
|
{ // wczytany z pliku tekstowego musi sam posprzątać
|
|
SafeDelete(Next);
|
|
SafeDelete(Child);
|
|
delete fMatrix; // własny transform trzeba usunąć (zawsze jeden)
|
|
}
|
|
};
|
|
|
|
void TSubModel::Name_Material(std::string const &Name)
|
|
{ // ustawienie nazwy submodelu, o
|
|
// ile nie jest wczytany z E3D
|
|
if (iFlags & 0x0200)
|
|
{ // tylko jeżeli submodel zosta utworzony przez new
|
|
m_materialname = Name;
|
|
}
|
|
};
|
|
|
|
void TSubModel::Name(std::string const &Name)
|
|
{ // ustawienie nazwy submodelu, o ile
|
|
// nie jest wczytany z E3D
|
|
if (iFlags & 0x0200)
|
|
pName = Name;
|
|
};
|
|
|
|
// sets rgb components of diffuse color override to specified value
|
|
void TSubModel::SetDiffuseOverride(glm::vec3 const &Color, bool const Includechildren, bool const Includesiblings)
|
|
{
|
|
|
|
if (eType == TP_FREESPOTLIGHT)
|
|
{
|
|
DiffuseOverride = Color;
|
|
}
|
|
if (true == Includesiblings)
|
|
{
|
|
auto sibling{this};
|
|
while ((sibling = sibling->Next) != nullptr)
|
|
{
|
|
sibling->SetDiffuseOverride(Color, Includechildren, false); // no need for all siblings to duplicate the work
|
|
}
|
|
}
|
|
if (true == Includechildren && Child != nullptr)
|
|
{
|
|
Child->SetDiffuseOverride(Color, Includechildren, true); // node's children include child's siblings and children
|
|
}
|
|
}
|
|
|
|
std::optional<glm::vec3> TSubModel::GetDiffuse(float Includesiblings)
|
|
{
|
|
if (eType == TP_FREESPOTLIGHT)
|
|
{
|
|
if (DiffuseOverride.x >= 0.0f)
|
|
return DiffuseOverride;
|
|
else
|
|
return glm::vec3(f4Diffuse);
|
|
}
|
|
|
|
if (Includesiblings)
|
|
{
|
|
auto sibling = this;
|
|
while ((sibling = sibling->Next))
|
|
{
|
|
auto result = sibling->GetDiffuse(true);
|
|
if (result)
|
|
return result;
|
|
}
|
|
}
|
|
|
|
if (Child)
|
|
return Child->GetDiffuse(true);
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
// sets visibility level (alpha component) to specified value
|
|
void TSubModel::SetVisibilityLevel(float const Level, bool const Includechildren, bool const Includesiblings)
|
|
{
|
|
|
|
fVisible = Level;
|
|
if (true == Includesiblings)
|
|
{
|
|
auto sibling{this};
|
|
while ((sibling = sibling->Next) != nullptr)
|
|
{
|
|
sibling->SetVisibilityLevel(Level, Includechildren, false); // no need for all siblings to duplicate the work
|
|
}
|
|
}
|
|
if (true == Includechildren && Child != nullptr)
|
|
{
|
|
Child->SetVisibilityLevel(Level, Includechildren, true); // node's children include child's siblings and children
|
|
}
|
|
}
|
|
|
|
// sets light level (alpha component of illumination color) to specified value
|
|
void TSubModel::SetLightLevel(glm::vec4 const &Level, bool const Includechildren, bool const Includesiblings)
|
|
{
|
|
/*
|
|
f4Emision = Level;
|
|
*/
|
|
f4Diffuse = {Level.r, Level.g, Level.b, f4Diffuse.a};
|
|
f4Emision.a = Level.a;
|
|
if (true == Includesiblings)
|
|
{
|
|
auto sibling{this};
|
|
while ((sibling = sibling->Next) != nullptr)
|
|
{
|
|
sibling->SetLightLevel(Level, Includechildren, false); // no need for all siblings to duplicate the work
|
|
}
|
|
}
|
|
if (true == Includechildren && Child != nullptr)
|
|
{
|
|
Child->SetLightLevel(Level, Includechildren, true); // node's children include child's siblings and children
|
|
}
|
|
}
|
|
|
|
// sets activation threshold of self-illumination to specitied value
|
|
void TSubModel::SetSelfIllum(float const Threshold, bool const Includechildren, bool const Includesiblings)
|
|
{
|
|
|
|
fLight = Threshold;
|
|
if (true == Includesiblings)
|
|
{
|
|
auto sibling{this};
|
|
while ((sibling = sibling->Next) != nullptr)
|
|
{
|
|
sibling->SetSelfIllum(Threshold, Includechildren, false); // no need for all siblings to duplicate the work
|
|
}
|
|
}
|
|
if (true == Includechildren && Child != nullptr)
|
|
{
|
|
Child->SetSelfIllum(Threshold, Includechildren, true); // node's children include child's siblings and children
|
|
}
|
|
}
|
|
|
|
int TSubModel::SeekFaceNormal(std::vector<unsigned int> const &Masks, int const Startface, unsigned int const Mask, glm::vec3 const &Position, gfx::vertex_array const &Vertices)
|
|
{ // szukanie punktu stycznego do (pt), zwraca numer wierzchołka, a nie trójkąta
|
|
int facecount = m_geometry.vertex_count / 3;
|
|
for (int faceidx = Startface; faceidx < facecount; ++faceidx)
|
|
{
|
|
// pętla po trójkątach, od trójkąta (f)
|
|
if (Masks[faceidx] & Mask)
|
|
{
|
|
// jeśli wspólna maska powierzchni
|
|
for (int vertexidx = 0; vertexidx < 3; ++vertexidx)
|
|
{
|
|
if (Vertices[3 * faceidx + vertexidx].position == Position)
|
|
{
|
|
return 3 * faceidx + vertexidx;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return -1; // nie znaleziono stycznego wierzchołka
|
|
}
|
|
|
|
float emm1[] = {1, 1, 1, 0};
|
|
float emm2[] = {0, 0, 0, 1};
|
|
|
|
inline void readColor(cParser &parser, glm::vec4 &color)
|
|
{
|
|
int discard;
|
|
parser.getTokens(4, false);
|
|
parser >> discard >> color.r >> color.g >> color.b;
|
|
color /= 255.0f;
|
|
};
|
|
|
|
inline void readMatrix(cParser &parser, float4x4 &matrix)
|
|
{ // Ra: wczytanie transforma
|
|
parser.getTokens(16, false);
|
|
for (int x = 0; x <= 3; ++x) // wiersze
|
|
for (int y = 0; y <= 3; ++y) // kolumny
|
|
parser >> matrix(x)[y];
|
|
};
|
|
|
|
template <typename T> void UserdataParse(cParser &parser, gfx::vertex_userdata &vertex)
|
|
{
|
|
static_assert(sizeof(T) == sizeof(float), "userdata is stored as raw 32-bit values");
|
|
parser.getTokens(4);
|
|
for (int i = 0; i < 4; ++i)
|
|
{
|
|
T value{};
|
|
parser >> value;
|
|
vertex.data[i] = std::bit_cast<float>(value);
|
|
}
|
|
}
|
|
|
|
std::pair<int, int> TSubModel::Load(cParser &parser, bool dynamic)
|
|
{ // Ra: VBO tworzone na poziomie modelu, a nie submodeli
|
|
auto token{parser.getToken<std::string>()};
|
|
if (token != "type:")
|
|
{
|
|
std::string errormessage{"Bad model: expected submodel type definition not found while loading model \"" + parser.Name() + "\"" + "\ncurrent model data stream content: \""};
|
|
auto count{10};
|
|
while (true == parser.getTokens() && false == (token = parser.peek()).empty() && token != "parent:")
|
|
{
|
|
// skip data until next submodel, dump first few tokens in the error message
|
|
if (--count > 0)
|
|
{
|
|
errormessage += token + " ";
|
|
}
|
|
}
|
|
errormessage += "(...)\"";
|
|
ErrorLog(errormessage);
|
|
return {0, 0};
|
|
}
|
|
{
|
|
auto const type{parser.getToken<std::string>()};
|
|
if (type == "mesh")
|
|
eType = GL_TRIANGLES; // submodel - trójkaty
|
|
else if (type == "point")
|
|
eType = GL_POINTS; // co to niby jest?
|
|
else if (type == "freespotlight")
|
|
eType = TP_FREESPOTLIGHT; // światełko
|
|
else if (type == "text")
|
|
eType = TP_TEXT; // wyświetlacz tekstowy (generator napisów)
|
|
else if (type == "stars")
|
|
eType = TP_STARS; // wiele punktów świetlnych
|
|
};
|
|
parser.ignoreToken();
|
|
parser.getTokens(1, false); // nazwa submodelu bez zmieny na małe
|
|
parser >> token;
|
|
Name(token);
|
|
if (dynamic)
|
|
{
|
|
// dla pojazdu, blokujemy załączone submodele, które mogą być nieobsługiwane
|
|
if (token.size() >= 3 && token.find("_on") + 3 == token.length())
|
|
{
|
|
// jeśli nazwa kończy się na "_on" to domyślnie wyłączyć, żeby się nie nakładało z obiektem "_off"
|
|
iVisible = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// dla pozostałych modeli blokujemy zapalone światła, które mogą być nieobsługiwane
|
|
if (token.compare(0, 8, "Light_On") == 0)
|
|
{
|
|
// jeśli nazwa zaczyna się od "Light_On" to domyślnie wyłączyć, żeby się nie nakładało z obiektem "Light_Off"
|
|
iVisible = 0;
|
|
}
|
|
}
|
|
|
|
if (parser.expectToken("anim:")) // Ra: ta informacja by się przydała!
|
|
{ // rodzaj animacji
|
|
std::string type = parser.getToken<std::string>();
|
|
if (type != "false")
|
|
{
|
|
iFlags |= 0x4000; // jak animacja, to trzeba przechowywać macierz zawsze
|
|
if (type == "seconds_jump")
|
|
b_Anim = b_aAnim = TAnimType::at_SecondsJump; // sekundy z przeskokiem
|
|
else if (type == "minutes_jump")
|
|
b_Anim = b_aAnim = TAnimType::at_MinutesJump; // minuty z przeskokiem
|
|
else if (type == "hours_jump")
|
|
b_Anim = b_aAnim = TAnimType::at_HoursJump; // godziny z przeskokiem
|
|
else if (type == "hours24_jump")
|
|
b_Anim = b_aAnim = TAnimType::at_Hours24Jump; // godziny z przeskokiem
|
|
else if (type == "seconds")
|
|
b_Anim = b_aAnim = TAnimType::at_Seconds; // minuty płynnie
|
|
else if (type == "minutes")
|
|
b_Anim = b_aAnim = TAnimType::at_Minutes; // minuty płynnie
|
|
else if (type == "hours")
|
|
b_Anim = b_aAnim = TAnimType::at_Hours; // godziny płynnie
|
|
else if (type == "hours24")
|
|
b_Anim = b_aAnim = TAnimType::at_Hours24; // godziny płynnie
|
|
else if (type == "billboard")
|
|
b_Anim = b_aAnim = TAnimType::at_Billboard; // obrót w pionie do kamery
|
|
else if (type == "wind")
|
|
b_Anim = b_aAnim = TAnimType::at_Wind; // ruch pod wpływem wiatru
|
|
else if (type == "sky")
|
|
b_Anim = b_aAnim = TAnimType::at_Sky; // aniamacja nieba
|
|
else if (type == "digital")
|
|
b_Anim = b_aAnim = TAnimType::at_Digital; // licznik mechaniczny
|
|
else if (type == "digiclk")
|
|
b_Anim = b_aAnim = TAnimType::at_DigiClk; // zegar cyfrowy
|
|
else
|
|
b_Anim = b_aAnim = TAnimType::at_Undefined; // nieznana forma animacji
|
|
}
|
|
}
|
|
if (eType < TP_ROTATOR)
|
|
readColor(parser, f4Ambient); // ignoruje token przed
|
|
readColor(parser, f4Diffuse);
|
|
if (eType < TP_ROTATOR)
|
|
{
|
|
readColor(parser, f4Specular);
|
|
if (pName == "cien")
|
|
{
|
|
// crude workaround to kill specular on shadow geometry of legacy models
|
|
f4Specular = glm::vec4{0.0f, 0.0f, 0.0f, 1.0f};
|
|
}
|
|
}
|
|
parser.ignoreToken(); // zignorowanie nazwy "SelfIllum:"
|
|
{
|
|
std::string light = parser.getToken<std::string>();
|
|
if (light == "true")
|
|
fLight = 2.0; // zawsze świeci
|
|
else if (light == "false")
|
|
fLight = -1.0; // zawsze ciemy
|
|
else
|
|
fLight = std::stod(light);
|
|
};
|
|
if (eType == TP_FREESPOTLIGHT)
|
|
{
|
|
if (!parser.expectToken("nearattenstart:"))
|
|
{
|
|
Error("Model light parse failure!");
|
|
}
|
|
std::string discard;
|
|
discard.reserve(64);
|
|
parser.getTokens(13, false);
|
|
parser >> fNearAttenStart >> discard >> fNearAttenEnd >> discard >> bUseNearAtten >> discard >> iFarAttenDecay >> discard >> fFarDecayRadius >> discard >>
|
|
fCosFalloffAngle // kąt liczony dla średnicy, a nie promienia
|
|
>> discard >> fCosHotspotAngle; // kąt liczony dla średnicy, a nie promienia
|
|
// trzeba pobrac do kolejki 2 tokeny
|
|
parser.getTokens(2);
|
|
if (parser.peek() == "hotspotpower:")
|
|
{
|
|
float multiplier;
|
|
parser >> discard >> multiplier;
|
|
WriteLog("Found hotspot power!");
|
|
// recaluclate multiplier
|
|
diffuseMultiplier = multiplier / 100.f;
|
|
|
|
// apply color only on nvrenderer
|
|
if (Global.NvRenderer)
|
|
f4Diffuse *= diffuseMultiplier;
|
|
}
|
|
else
|
|
parser.autoclear(false);
|
|
|
|
// convert conve parameters if specified in degrees
|
|
if (fCosFalloffAngle > 1.0)
|
|
{
|
|
fCosFalloffAngle = std::cos(glm::radians(0.5f * fCosFalloffAngle));
|
|
}
|
|
if (fCosHotspotAngle > 1.0)
|
|
{
|
|
fCosHotspotAngle = std::cos(glm::radians(0.5f * fCosHotspotAngle));
|
|
}
|
|
m_geometry.vertex_count = 1;
|
|
iFlags |= 0x4030; // drawn both in solid (light point) and transparent (light glare) phases
|
|
}
|
|
else if (eType < TP_ROTATOR)
|
|
{
|
|
std::string discard;
|
|
parser.getTokens(6, false);
|
|
parser >> discard >> bWire >> discard >> fWireSize >> discard >> Opacity;
|
|
// wymagane jest 0 dla szyb, 100 idzie w nieprzezroczyste
|
|
if (Opacity > 1.f)
|
|
{
|
|
Opacity = std::min(1.f, Opacity * 0.01f);
|
|
}
|
|
if (Opacity < -1.f)
|
|
{
|
|
Opacity = std::max(-1.f, Opacity * 0.01f);
|
|
}
|
|
|
|
if (!parser.expectToken("map:"))
|
|
Error("Model map parse failure!");
|
|
std::string material = parser.getToken<std::string>();
|
|
std::replace(material.begin(), material.end(), '\\', '/');
|
|
if (material == "none")
|
|
{ // rysowanie podanym kolorem
|
|
Name_Material("colored");
|
|
m_material = GfxRenderer->Fetch_Material(m_materialname);
|
|
iFlags |= 0x10; // rysowane w cyklu nieprzezroczystych
|
|
}
|
|
else if (material.find("replacableskin") != material.npos)
|
|
{ // McZapkie-060702: zmienialne skory modelu
|
|
m_material = -1;
|
|
iFlags |= Opacity < 0.999 ? 1 : 0x10; // zmienna tekstura 1
|
|
}
|
|
else if (material == "-1")
|
|
{
|
|
m_material = -1;
|
|
iFlags |= Opacity < 0.999 ? 1 : 0x10; // zmienna tekstura 1
|
|
}
|
|
else if (material == "-2")
|
|
{
|
|
m_material = -2;
|
|
iFlags |= Opacity < 0.999 ? 2 : 0x10; // zmienna tekstura 2
|
|
}
|
|
else if (material == "-3")
|
|
{
|
|
m_material = -3;
|
|
iFlags |= Opacity < 0.999 ? 4 : 0x10; // zmienna tekstura 3
|
|
}
|
|
else if (material == "-4")
|
|
{
|
|
m_material = -4;
|
|
iFlags |= Opacity < 0.999 ? 8 : 0x10; // zmienna tekstura 4
|
|
}
|
|
else
|
|
{
|
|
Name_Material(material);
|
|
/*
|
|
if( material.find_first_of( "/" ) == material.npos ) {
|
|
// jeśli tylko nazwa pliku, to dawać bieżącą ścieżkę do tekstur
|
|
material.insert( 0, Global.asCurrentTexturePath );
|
|
}
|
|
*/
|
|
m_material = GfxRenderer->Fetch_Material(material);
|
|
// renderowanie w cyklu przezroczystych tylko jeśli:
|
|
// 1. Opacity=0 (przejściowo <1, czy tam <100)
|
|
iFlags |= Opacity < 0.999f ? 0x20 : 0x10; // 0x20-przezroczysta, 0x10-nieprzezroczysta
|
|
};
|
|
}
|
|
else if (eType == TP_STARS)
|
|
{
|
|
m_material = GfxRenderer->Fetch_Material("stars");
|
|
iFlags |= 0x10;
|
|
}
|
|
else
|
|
{
|
|
iFlags |= 0x10;
|
|
}
|
|
|
|
if (m_material > 0)
|
|
{
|
|
const IMaterial *mat = GfxRenderer->Material(m_material);
|
|
/*
|
|
// if material does have opacity set, replace submodel opacity with it
|
|
if (mat.opacity)
|
|
{
|
|
iFlags &= ~0x30;
|
|
if (*mat.opacity == 0.0f)
|
|
iFlags |= 0x20; // translucent
|
|
else
|
|
iFlags |= 0x10; // opaque
|
|
}
|
|
*/
|
|
// and same thing with selfillum
|
|
if (mat->GetSelfillum())
|
|
fLight = *mat->GetSelfillum();
|
|
}
|
|
|
|
// visibility range
|
|
std::string discard;
|
|
parser.getTokens(5, false);
|
|
parser.autoclear(true);
|
|
parser >> discard >> fSquareMaxDist >> discard >> fSquareMinDist >> discard;
|
|
|
|
if (fSquareMaxDist <= 0.0)
|
|
{
|
|
// 15km to więcej, niż się obecnie wyświetla
|
|
fSquareMaxDist = 15000.0;
|
|
}
|
|
fSquareMaxDist *= fSquareMaxDist;
|
|
fSquareMinDist *= fSquareMinDist;
|
|
|
|
// transformation matrix
|
|
fMatrix = new float4x4();
|
|
readMatrix(parser, *fMatrix); // wczytanie transform
|
|
if (Parent != nullptr)
|
|
transformscalestack = Parent->transformscalestack;
|
|
if (!fMatrix->IdentityIs())
|
|
{
|
|
iFlags |= 0x8000; // transform niejedynkowy - trzeba go przechować
|
|
// check the scaling
|
|
auto const matrix = glm::make_mat4(fMatrix->readArray());
|
|
glm::vec3 const scale{glm::length(glm::vec3(glm::column(matrix, 0))), glm::length(glm::vec3(glm::column(matrix, 1))), glm::length(glm::vec3(glm::column(matrix, 2)))};
|
|
if (std::abs(scale.x - 1.0f) > 0.01 || std::abs(scale.y - 1.0f) > 0.01 || std::abs(scale.z - 1.0f) > 0.01)
|
|
{
|
|
ErrorLog("Bad model: transformation matrix for sub-model \"" + pName + "\" imposes geometry scaling (factors: " + to_string(scale) + ")", logtype::model);
|
|
m_normalizenormals = std::abs(scale.x - scale.y) < 0.01f && std::abs(scale.y - scale.z) < 0.01f ? rescale : normalize;
|
|
}
|
|
transformscalestack *= (scale.x + scale.y + scale.z) / 3.0f;
|
|
}
|
|
if (eType < TP_ROTATOR)
|
|
{ // wczytywanie wierzchołków
|
|
token = parser.getToken<std::string>();
|
|
bool has_userdata = false;
|
|
if (token == "userdata:")
|
|
{
|
|
has_userdata = parser.getToken<bool>();
|
|
|
|
token = parser.getToken<std::string>();
|
|
}
|
|
if (token == "numindices:") // optional block, potentially preceeding vertex list
|
|
{
|
|
m_geometry.index_count = parser.getToken<int>(false);
|
|
Indices.resize(m_geometry.index_count);
|
|
for (auto idx = 0; idx < m_geometry.index_count; ++idx)
|
|
{
|
|
Indices[idx] = parser.getToken<unsigned int>(false);
|
|
}
|
|
token = parser.getToken<std::string>();
|
|
}
|
|
if (token == "numverts:" || token == "numverts")
|
|
{ // normalna lista wierzchołków
|
|
/*
|
|
// Ra 15-01: to wczytać jako tekst - jeśli pierwszy znak zawiera "*", to
|
|
// dalej będzie nazwa wcześniejszego submodelu, z którego należy wziąć wierzchołki
|
|
// zapewni to jakąś zgodność wstecz, bo zamiast liczby będzie ciąg, którego
|
|
// wartość powinna być uznana jako zerowa
|
|
token = parser.getToken<std::string>();
|
|
if( token.front() == '*' ) {
|
|
// jeśli pierwszy znak jest gwiazdką, poszukać submodelu o nazwie bez tej gwiazdki i wziąć z niego wierzchołki
|
|
Error( "Vertices reference not yet supported!" );
|
|
}
|
|
*/
|
|
m_geometry.vertex_count = parser.getToken<int>(false);
|
|
if (m_geometry.index_count <= 0 && m_geometry.vertex_count % 3 != 0)
|
|
{
|
|
m_geometry.vertex_count = 0;
|
|
Error("Bad model: incomplete triangle encountered in submodel \"" + pName + "\"");
|
|
return {0, 0};
|
|
}
|
|
if (m_geometry.vertex_count)
|
|
{
|
|
Vertices.resize(m_geometry.vertex_count);
|
|
if (m_geometry.index_count)
|
|
{
|
|
// indexed geometry chunks are expected to come with pre-generated normals and tangents, to avoid hassle required to generate them manually
|
|
auto vertices{std::begin(Vertices)};
|
|
for (auto idx = 0; idx < m_geometry.vertex_count; ++idx)
|
|
{
|
|
auto vertex{vertices + idx};
|
|
parser.getTokens(3 + 3 + 2 + 4, false);
|
|
parser >> vertex->position.x >> vertex->position.y >> vertex->position.z >> vertex->normal.x >> vertex->normal.y >> vertex->normal.z >> vertex->texture.s >>
|
|
vertex->texture.t >> vertex->tangent.x >> vertex->tangent.y >> vertex->tangent.z >> vertex->tangent.w;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// legacy geometry, more or less incomplete
|
|
// to stay on the safe side we'll potentially need to convert indexed geometry to raw triangle form, cal
|
|
auto vertices{std::begin(Vertices)};
|
|
int facecount = m_geometry.vertex_count / 3;
|
|
std::vector<unsigned int> sg;
|
|
sg.resize(facecount); // maski przynależności trójkątów do powierzchni
|
|
std::vector<int> wsp;
|
|
wsp.resize(m_geometry.vertex_count); // z którego wierzchołka kopiować wektor normalny
|
|
int maska = 0;
|
|
int vertexidx = 0; // used to keep track of vertex indices in source file
|
|
for (auto idx = 0; idx < m_geometry.vertex_count; ++idx)
|
|
{
|
|
++vertexidx;
|
|
// Ra: z konwersją na układ scenerii - będzie wydajniejsze wyświetlanie
|
|
wsp[idx] = -1; // wektory normalne nie są policzone dla tego wierzchołka
|
|
if (idx % 3 == 0)
|
|
{
|
|
// jeśli będzie maska -1, to dalej będą wierzchołki z wektorami normalnymi, podanymi jawnie
|
|
maska = parser.getToken<int>(false); // maska powierzchni trójkąta
|
|
// dla maski -1 będzie 0, czyli nie ma wspólnych wektorów normalnych
|
|
sg[idx / 3] = maska == -1 ? 0 : maska;
|
|
}
|
|
auto vertex{vertices + idx};
|
|
parser.getTokens(3, false);
|
|
parser >> vertex->position.x >> vertex->position.y >> vertex->position.z;
|
|
if (maska == -1)
|
|
{ // jeśli wektory normalne podane jawnie
|
|
parser.getTokens(3, false);
|
|
parser >> vertex->normal.x >> vertex->normal.y >> vertex->normal.z;
|
|
if (glm::length2(vertex->normal) > 0.0f)
|
|
{
|
|
vertex->normal = glm::normalize(vertex->normal);
|
|
}
|
|
else
|
|
{
|
|
WriteLog("Bad model: zero length normal vector specified in submodel \"" + pName + "\", vertex " + std::to_string(idx), logtype::model);
|
|
}
|
|
wsp[idx] = idx; // wektory normalne "są już policzone"
|
|
}
|
|
parser.getTokens(2, false);
|
|
parser >> vertex->texture.s >> vertex->texture.t;
|
|
|
|
if (idx % 3 == 2)
|
|
{
|
|
// jeżeli wczytano 3 punkty
|
|
if (true == degenerate(vertex->position, (vertex - 1)->position, (vertex - 2)->position))
|
|
{
|
|
// jeżeli punkty się nakładają na siebie
|
|
--facecount; // o jeden trójkąt mniej
|
|
m_geometry.vertex_count -= 3; // czyli o 3 wierzchołki
|
|
idx -= 3; // wczytanie kolejnego w to miejsce
|
|
WriteLog("Bad model: degenerated triangle ignored in: \"" + pName + "\", vertices " + std::to_string(vertexidx - 2) + "-" + std::to_string(vertexidx), logtype::model);
|
|
}
|
|
if (idx > 0)
|
|
{
|
|
// jeśli pierwszy trójkąt będzie zdegenerowany, to zostanie usunięty i nie ma co sprawdzać
|
|
// length2 is better than length for comparing because it does not require sqrt function
|
|
if (glm::length2(vertex->position - (vertex - 1)->position) > sq(1000.0) || glm::length2((vertex - 1)->position - (vertex - 2)->position) > sq(1000.0) ||
|
|
glm::length2((vertex - 2)->position - vertex->position) > sq(1000.0))
|
|
{
|
|
// jeżeli są dalej niż 2km od siebie //Ra 15-01:
|
|
// obiekt wstawiany nie powinien być większy niż 300m (trójkąty terenu w E3D mogą mieć 1.5km)
|
|
--facecount; // o jeden trójkąt mniej
|
|
m_geometry.vertex_count -= 3; // czyli o 3 wierzchołki
|
|
idx -= 3; // wczytanie kolejnego w to miejsce
|
|
WriteLog("Bad model: too large triangle ignored in: \"" + pName + "\"", logtype::model);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<glm::vec3> facenormals;
|
|
facenormals.reserve(facecount);
|
|
for (int i = 0; i < facecount; ++i)
|
|
{
|
|
// pętla po trójkątach - będzie szybciej, jak wstępnie przeliczymy normalne trójkątów
|
|
auto const vertex{vertices + i * 3};
|
|
auto facenormal = glm::cross(vertex->position - (vertex + 1)->position, vertex->position - (vertex + 2)->position);
|
|
facenormals.emplace_back(glm::length2(facenormal) > 0.0f ? glm::normalize(facenormal) : glm::vec3());
|
|
}
|
|
glm::vec3 vertexnormal; // roboczy wektor normalny
|
|
for (int vertexidx = 0; vertexidx < m_geometry.vertex_count; ++vertexidx)
|
|
{
|
|
// pętla po wierzchołkach trójkątów
|
|
auto vertex{vertices + vertexidx};
|
|
if (wsp[vertexidx] >= 0)
|
|
{
|
|
// jeśli już był liczony wektor normalny z użyciem tego wierzchołka to wystarczy skopiować policzony wcześniej
|
|
vertex->normal = (vertices + wsp[vertexidx])->normal;
|
|
}
|
|
else
|
|
{
|
|
// inaczej musimy dopiero policzyć
|
|
auto const faceidx = vertexidx / 3; // numer trójkąta
|
|
vertexnormal = glm::vec3(); // liczenie zaczynamy od zera
|
|
auto adjacenvertextidx = vertexidx; // zaczynamy dodawanie wektorów normalnych od własnego
|
|
while (adjacenvertextidx >= 0)
|
|
{
|
|
// sumowanie z wektorem normalnym sąsiada (włącznie ze sobą)
|
|
if (glm::dot(vertexnormal, facenormals[adjacenvertextidx / 3]) > -0.99f)
|
|
{
|
|
wsp[adjacenvertextidx] = vertexidx; // informacja, że w tym wierzchołku jest już policzony wektor normalny
|
|
vertexnormal += facenormals[adjacenvertextidx / 3];
|
|
}
|
|
/*
|
|
else {
|
|
ErrorLog( "Bad model: opposite normals in the same smoothing group, check sub-model \"" + pName + "\" for two-sided faces and/or scaling", logtype::model );
|
|
}
|
|
*/
|
|
// i szukanie od kolejnego trójkąta
|
|
adjacenvertextidx = SeekFaceNormal(sg, adjacenvertextidx / 3 + 1, sg[faceidx], vertex->position, Vertices);
|
|
}
|
|
// Ra 15-01: należało by jeszcze uwzględnić skalowanie wprowadzane przez transformy, aby normalne po przeskalowaniu były jednostkowe
|
|
if (glm::length2(vertexnormal) == 0.0f)
|
|
{
|
|
WriteLog("Bad model: zero length normal vector generated for sub-model \"" + pName + "\"", logtype::model);
|
|
}
|
|
vertex->normal = glm::length2(vertexnormal) > 0.0f ? glm::normalize(vertexnormal) : facenormals[vertexidx / 3]; // przepisanie do wierzchołka trójkąta
|
|
}
|
|
}
|
|
Vertices.resize(m_geometry.vertex_count); // in case we had some degenerate triangles along the way
|
|
gfx::calculate_indices(Indices, Vertices, Userdata, transformscalestack);
|
|
gfx::calculate_tangents(Vertices, Indices, GL_TRIANGLES);
|
|
// update values potentially changed by indexing
|
|
m_geometry.index_count = Indices.size();
|
|
m_geometry.vertex_count = Vertices.size();
|
|
}
|
|
if (has_userdata)
|
|
{
|
|
Userdata.resize(m_geometry.vertex_count);
|
|
auto userdata{std::begin(Userdata)};
|
|
for (auto idx = 0; idx < m_geometry.vertex_count; ++idx)
|
|
{
|
|
auto vertex{userdata + idx};
|
|
UserdataParse<float>(parser, *vertex);
|
|
}
|
|
}
|
|
}
|
|
else // gdy brak wierzchołków
|
|
{
|
|
eType = TP_ROTATOR; // submodel pomocniczy, ma tylko macierz przekształcenia
|
|
// dla formalności
|
|
m_geometry.vertex_offset = 0;
|
|
m_geometry.vertex_count = 0;
|
|
}
|
|
} // obsługa submodelu z własną listą wierzchołków
|
|
}
|
|
else if (eType == TP_STARS)
|
|
{ // punkty świecące dookólnie - składnia jak
|
|
// dla smt_Mesh
|
|
std::string discard;
|
|
parser.getTokens(2, false);
|
|
parser >> discard >> m_geometry.vertex_count;
|
|
Vertices.resize(m_geometry.vertex_count);
|
|
int idx;
|
|
unsigned int color;
|
|
auto vertices{std::begin(Vertices)};
|
|
for (idx = 0; idx < m_geometry.vertex_count; ++idx)
|
|
{
|
|
if (idx % 3 == 0)
|
|
{
|
|
parser.ignoreToken(); // maska powierzchni trójkąta
|
|
}
|
|
auto vertex{vertices + idx};
|
|
parser.getTokens(5, false);
|
|
parser >> vertex->position.x >> vertex->position.y >> vertex->position.z >> color // zakodowany kolor
|
|
>> discard;
|
|
vertex->normal = {(color & 0xff) / 255.0f, // R
|
|
(color >> 8 & 0xff) / 255.0f, // G
|
|
(color >> 16 & 0xff) / 255.0f}; // B
|
|
}
|
|
}
|
|
else if (eType == TP_FREESPOTLIGHT)
|
|
{
|
|
// single light points only have single data point, duh
|
|
Vertices.emplace_back();
|
|
m_geometry.vertex_count = 1;
|
|
}
|
|
// Visible=true; //się potem wyłączy w razie potrzeby
|
|
// iFlags|=0x0200; //wczytano z pliku tekstowego (jest właścicielem tablic)
|
|
if (m_geometry.vertex_count < 1)
|
|
iFlags &= ~0x3F; // cykl renderowania uzależniony od potomnych
|
|
|
|
return {m_geometry.index_count, m_geometry.vertex_count}; // do określenia wielkości VBO
|
|
};
|
|
|
|
/*
|
|
int TSubModel::TriangleAdd(TModel3d *m, material_handle tex, int tri)
|
|
{ // dodanie trójkątów do submodelu, używane przy tworzeniu E3D terenu
|
|
TSubModel *s = this;
|
|
while (s ? (s->m_material != tex) : false)
|
|
{ // szukanie submodelu o danej teksturze
|
|
if (s == this)
|
|
s = Child;
|
|
else
|
|
s = s->Next;
|
|
}
|
|
if (!s)
|
|
{
|
|
if (m_material <= 0)
|
|
s = this; // użycie głównego
|
|
else
|
|
{ // dodanie nowego submodelu do listy potomnych
|
|
s = new TSubModel();
|
|
m->AddTo(this, s);
|
|
}
|
|
s->Name_Material(GfxRenderer->Material(tex).name);
|
|
s->m_material = tex;
|
|
s->eType = GL_TRIANGLES;
|
|
}
|
|
if (s->iNumVerts < 0)
|
|
s->iNumVerts = tri; // bo na początku jest -1, czyli że nie wiadomo
|
|
else
|
|
s->iNumVerts += tri; // aktualizacja ilości wierzchołków
|
|
return s->iNumVerts - tri; // zwraca pozycję tych trójkątów w submodelu
|
|
};
|
|
*/
|
|
/*
|
|
basic_vertex *TSubModel::TrianglePtr(int tex, int pos, glm::vec3 const &Ambient, glm::vec3 const &Diffuse, glm::vec3 const &Specular )
|
|
{ // zwraca wskaźnik do wypełnienia tabeli wierzchołków, używane przy tworzeniu E3D terenu
|
|
|
|
TSubModel *s = this;
|
|
while (s ? s->TextureID != tex : false)
|
|
{ // szukanie submodelu o danej teksturze
|
|
if (s == this)
|
|
s = Child;
|
|
else
|
|
s = s->Next;
|
|
}
|
|
if (!s)
|
|
return NULL; // coś nie tak poszło
|
|
if (!s->Vertices)
|
|
{ // utworznie tabeli trójkątów
|
|
s->Vertices = new basic_vertex[s->iNumVerts];
|
|
s->iVboPtr = iInstance; // pozycja submodelu w tabeli wierzchołków
|
|
iInstance += s->iNumVerts; // pozycja dla następnego
|
|
}
|
|
s->ColorsSet(Ambient, Diffuse, Specular); // ustawienie kolorów świateł
|
|
return s->Vertices + pos; // wskaźnik na wolne miejsce w tabeli wierzchołków
|
|
};
|
|
*/
|
|
|
|
void TSubModel::InitialRotate(bool doit)
|
|
{ // konwersja układu współrzędnych na zgodny ze scenerią
|
|
if (iFlags & 0xC000) // jeśli jest animacja albo niejednostkowy transform
|
|
{ // niejednostkowy transform jest mnożony i wystarczy zabawy
|
|
if (doit)
|
|
{
|
|
// obrót lewostronny
|
|
if (!fMatrix)
|
|
{
|
|
// macierzy może nie być w dodanym "bananie"
|
|
fMatrix = new float4x4(); // tworzy macierz o przypadkowej zawartości
|
|
fMatrix->Identity(); // a zaczynamy obracanie od jednostkowej
|
|
}
|
|
iFlags |= 0x8000; // po obróceniu będzie raczej niejedynkowy matrix
|
|
fMatrix->InitialRotate(); // zmiana znaku X oraz zamiana Y i Z
|
|
if (fMatrix->IdentityIs())
|
|
iFlags &= ~0x8000; // jednak jednostkowa po obróceniu
|
|
}
|
|
if (Child)
|
|
{
|
|
// potomnych nie obracamy już, tylko ewentualnie optymalizujemy
|
|
Child->InitialRotate(false);
|
|
}
|
|
else if (Global.iConvertModels & 2)
|
|
{
|
|
// optymalizacja jest opcjonalna
|
|
if ((iFlags & 0xC000) == 0x8000 // o ile nie ma animacji
|
|
&& false == is_emitter()) // don't optimize smoke emitter attachment points
|
|
{ // jak nie ma potomnych, można wymnożyć przez transform i wyjedynkować go
|
|
float4x4 *mat = GetMatrix(); // transform submodelu
|
|
if (false == Vertices.empty())
|
|
{
|
|
for (auto &vertex : Vertices)
|
|
{
|
|
vertex.position = *mat * vertex.position;
|
|
}
|
|
// zerujemy przesunięcie przed obracaniem normalnych
|
|
(*mat)(3)[0] = (*mat)(3)[1] = (*mat)(3)[2] = 0.0;
|
|
if (eType != TP_STARS)
|
|
{
|
|
// gwiazdki mają kolory zamiast normalnych, to ich wtedy nie ruszamy
|
|
for (auto &vertex : Vertices)
|
|
{
|
|
vertex.normal = *mat * vertex.normal;
|
|
vertex.tangent.xyz = *mat * vertex.tangent.xyz;
|
|
}
|
|
}
|
|
}
|
|
mat->Identity(); // jedynkowanie transformu po przeliczeniu wierzchołków
|
|
iFlags &= ~0x8000; // transform jedynkowy
|
|
}
|
|
}
|
|
}
|
|
else // jak jest jednostkowy i nie ma animacji
|
|
if (doit)
|
|
{ // jeśli jest jednostkowy transform, to przeliczamy
|
|
// wierzchołki, a mnożenie podajemy dalej
|
|
for (auto &vertex : Vertices)
|
|
{
|
|
glm::mat4 vertexTransform{{-1.f, 0.f, 0.f, 0.f}, {0.f, 0.f, 1.f, 0.f}, {0.f, 1.f, 0.f, 0.f}, {0.f, 0.f, 0.f, 1.f}};
|
|
vertex.position = vertexTransform * glm::vec4(vertex.position, 1.f);
|
|
// wektory normalne również trzeba przekształcić, bo się źle oświetlają
|
|
if (eType != TP_STARS)
|
|
{
|
|
glm::mat3 normalTransform{{-1.f, 0.f, 0.f}, {0.f, 0.f, 1.f}, {0.f, 1.f, 0.f}};
|
|
// gwiazdki mają kolory zamiast normalnych, to // ich wtedy nie ruszamy
|
|
vertex.normal = normalTransform * vertex.normal;
|
|
vertex.tangent.xyz = normalTransform * vertex.tangent.xyz;
|
|
}
|
|
}
|
|
if (Child)
|
|
Child->InitialRotate(doit); // potomne ewentualnie obrócimy
|
|
}
|
|
if (Next)
|
|
Next->InitialRotate(doit);
|
|
};
|
|
|
|
// text display (TP_TEXT): returns sub-models of the characters to draw for provided text, in drawing order.
|
|
// a character is matched with the first character of a child sub-model name. both the text and the names can be
|
|
// encoded either in utf-8 or in windows-1250, so the legacy (ansi) scenery and model files work along the utf-8 ones
|
|
std::vector<TSubModel *> const &TSubModel::text_letters(std::string const &Text)
|
|
{
|
|
// the sequence is prepared once for each text the display shows. the texts of the model instances don't change,
|
|
// so drawing a display afterwards costs a single lookup, however many instances and passes take turns
|
|
if (auto const prepared{m_textlayouts.find(Text)}; prepared != m_textlayouts.end())
|
|
{
|
|
return prepared->second;
|
|
}
|
|
|
|
std::u32string characters;
|
|
if (false == m_lettersready)
|
|
{
|
|
for (auto *letter = Child; letter != nullptr; letter = letter->Next)
|
|
{
|
|
if (letter->pName.empty())
|
|
{
|
|
continue;
|
|
}
|
|
if (false == utf8_to_utf32(letter->pName, characters))
|
|
{
|
|
win1250_to_utf32(letter->pName, characters);
|
|
}
|
|
if (characters.front() != 0)
|
|
{
|
|
m_letters[characters.front()] = letter;
|
|
}
|
|
}
|
|
m_lettersready = true;
|
|
}
|
|
|
|
// the text is taken for utf-8 if it's valid as such, and for windows-1250 otherwise. a short ansi text can pass
|
|
// for utf-8 by accident (two upper case accented letters in a row are enough for this to happen),
|
|
// so if both readings are possible the one which finds more of its characters in the model wins
|
|
auto const known = [this](std::u32string const &Characters) {
|
|
return std::ranges::count_if(Characters, [this](char32_t const Character) { return m_letters.contains(Character); });
|
|
};
|
|
std::u32string ansi;
|
|
win1250_to_utf32(Text, ansi);
|
|
if (false == utf8_to_utf32(Text, characters) || known(ansi) > known(characters))
|
|
{
|
|
characters = ansi;
|
|
}
|
|
|
|
auto &textletters{m_textlayouts[Text]};
|
|
// NOTE: the first character of the text isn't displayed. the displays always worked this way, and the existing
|
|
// content relies on it
|
|
for (std::size_t idx = 1; idx < characters.size(); ++idx)
|
|
{
|
|
auto const lookup{m_letters.find(characters[idx])};
|
|
if (lookup != m_letters.end())
|
|
{
|
|
textletters.emplace_back(lookup->second);
|
|
}
|
|
}
|
|
return textletters;
|
|
}
|
|
|
|
void TSubModel::ChildAdd(TSubModel *SubModel)
|
|
{ // dodanie submodelu potemnego (uzależnionego)
|
|
// Ra: zmiana kolejności, żeby kolejne móc renderować po aktualnym (było
|
|
// przed)
|
|
if (SubModel)
|
|
SubModel->NextAdd(Child); // Ra: zmiana kolejności renderowania
|
|
Child = SubModel;
|
|
};
|
|
|
|
void TSubModel::NextAdd(TSubModel *SubModel)
|
|
{ // dodanie submodelu kolejnego (wspólny przodek)
|
|
if (Next)
|
|
Next->NextAdd(SubModel);
|
|
else
|
|
Next = SubModel;
|
|
};
|
|
|
|
int TSubModel::count_siblings()
|
|
{
|
|
|
|
auto siblingcount{0};
|
|
auto *sibling{Next};
|
|
while (sibling != nullptr)
|
|
{
|
|
++siblingcount;
|
|
sibling = sibling->Next;
|
|
}
|
|
return siblingcount;
|
|
}
|
|
|
|
int TSubModel::count_children()
|
|
{
|
|
|
|
return Child == nullptr ? 0 : 1 + Child->count_siblings();
|
|
}
|
|
|
|
// locates submodel mapped with replacable -4
|
|
std::tuple<TSubModel *, bool> TSubModel::find_replacable4()
|
|
{
|
|
|
|
if (m_material == -4)
|
|
{
|
|
return std::make_tuple(this, fLight != -1.0);
|
|
}
|
|
|
|
if (Next != nullptr)
|
|
{
|
|
auto lookup{Next->find_replacable4()};
|
|
if (std::get<TSubModel *>(lookup) != nullptr)
|
|
{
|
|
return lookup;
|
|
}
|
|
}
|
|
|
|
if (Child != nullptr)
|
|
{
|
|
auto lookup{Child->find_replacable4()};
|
|
if (std::get<TSubModel *>(lookup) != nullptr)
|
|
{
|
|
return lookup;
|
|
}
|
|
}
|
|
|
|
return std::make_tuple(nullptr, false);
|
|
}
|
|
|
|
// locates particle emitter submodels and adds them to provided list
|
|
void TSubModel::find_smoke_sources(nameoffset_sequence &Sourcelist) const
|
|
{
|
|
|
|
auto const name{ToLower(pName)};
|
|
|
|
if (eType == TP_ROTATOR && pName.find("smokesource_") == 0)
|
|
{
|
|
Sourcelist.emplace_back(pName, offset());
|
|
}
|
|
|
|
if (Next != nullptr)
|
|
{
|
|
Next->find_smoke_sources(Sourcelist);
|
|
}
|
|
|
|
if (Child != nullptr)
|
|
{
|
|
Child->find_smoke_sources(Sourcelist);
|
|
}
|
|
}
|
|
|
|
uint32_t TSubModel::FlagsCheck()
|
|
{ // analiza koniecznych zmian pomiędzy submodelami
|
|
// samo pomijanie glBindTexture() nie poprawi wydajności
|
|
// ale można sprawdzić, czy można w ogóle pominąć kod do tekstur (sprawdzanie
|
|
// replaceskin)
|
|
m_rotation_init_done = true;
|
|
|
|
uint32_t i = 0;
|
|
if (Child)
|
|
{ // Child jest renderowany po danym submodelu
|
|
if (Child->m_material) // o ile ma teksturę
|
|
if (Child->m_material != m_material) // i jest ona inna niż rodzica
|
|
Child->iFlags |= 0x80; // to trzeba sprawdzać, jak z teksturami jest
|
|
i = Child->FlagsCheck();
|
|
iFlags |= 0x00FF0000 & (i << 16 | i | i >> 8); // potomny, rodzeństwo i dzieci
|
|
if (eType == TP_TEXT)
|
|
{ // wyłączenie renderowania Next dla znaków
|
|
// wyświetlacza tekstowego
|
|
TSubModel *p = Child;
|
|
while (p)
|
|
{
|
|
p->iFlags &= 0xC0FFFFFF;
|
|
p = p->Next;
|
|
}
|
|
}
|
|
}
|
|
if (Next)
|
|
{ // Next jest renderowany po danym submodelu (kolejność odwrócona
|
|
// po wczytaniu T3D)
|
|
if (m_material) // o ile dany ma teksturę
|
|
if (m_material != Next->m_material || i & 0x00800000) // a ma inną albo dzieci zmieniają
|
|
iFlags |= 0x80; // to dany submodel musi sobie ją ustawiać
|
|
i = Next->FlagsCheck();
|
|
iFlags |= 0xFF000000 & (i << 24 | i << 8 | i); // następny, kolejne i ich dzieci
|
|
// tekstury nie ustawiamy tylko wtedy, gdy jest taka sama jak Next i jego
|
|
// dzieci nie zmieniają
|
|
}
|
|
return iFlags;
|
|
};
|
|
|
|
void TSubModel::SetRotate(float3 vNewRotateAxis, float fNewAngle)
|
|
{ // obrócenie submodelu wg podanej
|
|
// osi (np. wskazówki w kabinie)
|
|
v_RotateAxis = vNewRotateAxis;
|
|
f_Angle = fNewAngle;
|
|
if (fNewAngle != 0.0)
|
|
{
|
|
b_Anim = TAnimType::at_Rotate;
|
|
b_aAnim = TAnimType::at_Rotate;
|
|
}
|
|
iAnimOwner = iInstance; // zapamiętanie czyja jest animacja
|
|
}
|
|
|
|
void TSubModel::SetRotateXYZ(float3 vNewAngles)
|
|
{ // obrócenie submodelu o
|
|
// podane kąty wokół osi
|
|
// lokalnego układu
|
|
v_Angles = vNewAngles;
|
|
b_Anim = TAnimType::at_RotateXYZ;
|
|
b_aAnim = TAnimType::at_RotateXYZ;
|
|
iAnimOwner = iInstance; // zapamiętanie czyja jest animacja
|
|
}
|
|
|
|
void TSubModel::SetRotateXYZ(glm::vec3 vNewAngles)
|
|
{ // obrócenie submodelu o
|
|
// podane kąty wokół osi
|
|
// lokalnego układu
|
|
v_Angles.x = vNewAngles.x;
|
|
v_Angles.y = vNewAngles.y;
|
|
v_Angles.z = vNewAngles.z;
|
|
b_Anim = TAnimType::at_RotateXYZ;
|
|
b_aAnim = TAnimType::at_RotateXYZ;
|
|
iAnimOwner = iInstance; // zapamiętanie czyja jest animacja
|
|
}
|
|
|
|
void TSubModel::SetTranslate(float3 vNewTransVector)
|
|
{ // przesunięcie submodelu (np. w kabinie)
|
|
v_TransVector = vNewTransVector;
|
|
b_Anim = TAnimType::at_Translate;
|
|
b_aAnim = TAnimType::at_Translate;
|
|
iAnimOwner = iInstance; // zapamiętanie czyja jest animacja
|
|
}
|
|
|
|
void TSubModel::SetTranslate(glm::vec3 vNewTransVector)
|
|
{ // przesunięcie submodelu (np. w kabinie)
|
|
v_TransVector.x = vNewTransVector.x;
|
|
v_TransVector.y = vNewTransVector.y;
|
|
v_TransVector.z = vNewTransVector.z;
|
|
b_Anim = TAnimType::at_Translate;
|
|
b_aAnim = TAnimType::at_Translate;
|
|
iAnimOwner = iInstance; // zapamiętanie czyja jest animacja
|
|
}
|
|
|
|
void TSubModel::SetRotateIK1(float3 vNewAngles)
|
|
{ // obrócenie submodelu o
|
|
// podane kąty wokół osi
|
|
// lokalnego układu
|
|
v_Angles = vNewAngles;
|
|
iAnimOwner = iInstance; // zapamiętanie czyja jest animacja
|
|
}
|
|
|
|
struct ToLower
|
|
{
|
|
char operator()(char input)
|
|
{
|
|
return tolower(input);
|
|
}
|
|
};
|
|
|
|
TSubModel *TSubModel::GetFromName(std::string const &search, bool i)
|
|
{
|
|
TSubModel *result;
|
|
// std::transform(search.begin(),search.end(),search.begin(),ToLower());
|
|
// search=search.LowerCase();
|
|
// AnsiString name=AnsiString();
|
|
std::string search_lc = search;
|
|
if (i)
|
|
std::transform(search_lc.begin(), search_lc.end(), search_lc.begin(), ::tolower);
|
|
std::string pName_lc = pName;
|
|
if (i)
|
|
std::transform(pName_lc.begin(), pName_lc.end(), pName_lc.begin(), ::tolower);
|
|
if (pName.size() && search.size())
|
|
if (pName_lc == search_lc)
|
|
return this;
|
|
if (Next)
|
|
{
|
|
result = Next->GetFromName(search);
|
|
if (result)
|
|
return result;
|
|
}
|
|
if (Child)
|
|
{
|
|
result = Child->GetFromName(search);
|
|
if (result)
|
|
return result;
|
|
}
|
|
return nullptr;
|
|
};
|
|
|
|
// WORD hbIndices[18]={3,0,1,5,4,2,1,0,4,1,5,3,2,3,5,2,4,0};
|
|
|
|
void TSubModel::RaAnimation(TAnimType a)
|
|
{
|
|
glm::mat4 m = OpenGLMatrices.data(GL_MODELVIEW);
|
|
RaAnimation(m, a);
|
|
glLoadMatrixf(glm::value_ptr(m));
|
|
}
|
|
|
|
void TSubModel::RaAnimation(glm::mat4 &m, TAnimType a)
|
|
{ // wykonanie animacji niezależnie od renderowania
|
|
switch (a)
|
|
{ // korekcja położenia, jeśli submodel jest animowany
|
|
case TAnimType::at_Translate: // Ra: było "true"
|
|
if (iAnimOwner != iInstance)
|
|
break; // cudza animacja
|
|
m = glm::translate(m, glm::vec3(v_TransVector.x, v_TransVector.y, v_TransVector.z));
|
|
break;
|
|
case TAnimType::at_Rotate: // Ra: było "true"
|
|
if (iAnimOwner != iInstance)
|
|
break; // cudza animacja
|
|
m = glm::rotate(m, glm::radians(f_Angle), glm::vec3(v_RotateAxis.x, v_RotateAxis.y, v_RotateAxis.z));
|
|
break;
|
|
case TAnimType::at_RotateXYZ:
|
|
if (iAnimOwner != iInstance)
|
|
break; // cudza animacja
|
|
m = glm::translate(m, glm::vec3(v_TransVector.x, v_TransVector.y, v_TransVector.z));
|
|
m = glm::rotate(m, glm::radians(v_Angles.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
m = glm::rotate(m, glm::radians(v_Angles.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
m = glm::rotate(m, glm::radians(v_Angles.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
|
break;
|
|
case TAnimType::at_SecondsJump: // sekundy z przeskokiem
|
|
m = glm::rotate(m, glm::radians(simulation::Time.data().wSecond * 6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_MinutesJump: // minuty z przeskokiem
|
|
m = glm::rotate(m, glm::radians(simulation::Time.data().wMinute * 6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_HoursJump: // godziny skokowo 12h/360°
|
|
m = glm::rotate(m, glm::radians(simulation::Time.data().wHour * 30.0f * 0.5f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_Hours24Jump: // godziny skokowo 24h/360°
|
|
m = glm::rotate(m, glm::radians(simulation::Time.data().wHour * 15.0f * 0.25f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_Seconds: // sekundy płynnie
|
|
m = glm::rotate(m, glm::radians((float)simulation::Time.second() * 6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_Minutes: // minuty płynnie
|
|
m = glm::rotate(m, glm::radians(simulation::Time.data().wMinute * 6.0f + (float)simulation::Time.second() * 0.1f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_Hours: // godziny płynnie 12h/360°
|
|
// glRotatef(GlobalTime->hh*30.0+GlobalTime->mm*0.5+GlobalTime->mr/120.0,0.0,1.0,0.0);
|
|
m = glm::rotate(m, glm::radians(2.0f * (float)Global.fTimeAngleDeg), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_Hours24: // godziny płynnie 24h/360°
|
|
// glRotatef(GlobalTime->hh*15.0+GlobalTime->mm*0.25+GlobalTime->mr/240.0,0.0,1.0,0.0);
|
|
m = glm::rotate(m, glm::radians((float)Global.fTimeAngleDeg), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_Billboard: // obrót w pionie do kamery
|
|
{
|
|
glm::mat4 mat = glm::make_mat4(OpenGLMatrices.data_array(GL_MODELVIEW));
|
|
float3 gdzie = float3(mat[3][0], mat[3][1], mat[3][2]); // początek układu współrzędnych submodelu względem kamery
|
|
m = glm::mat4(1.0f);
|
|
m = glm::translate(m, glm::vec3(gdzie.x, gdzie.y, gdzie.z)); // początek układu zostaje bez zmian
|
|
m = glm::rotate(m, (float)atan2(gdzie.x, gdzie.z), glm::vec3(0.0f, 1.0f, 0.0f)); // jedynie obracamy w pionie o kąt
|
|
}
|
|
break;
|
|
case TAnimType::at_Wind: // ruch pod wpływem wiatru (wiatr będziemy liczyć potem...)
|
|
m = glm::rotate(m, glm::radians(1.5f * (float)sin(M_PI * simulation::Time.second() / 6.0)), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_Sky: // animacja nieba
|
|
m = glm::rotate(m, glm::radians((float)Global.fLatitudeDeg), glm::vec3(0.0f, 1.0f, 0.0f)); // ustawienie osi OY na północ
|
|
m = glm::rotate(m, glm::radians((float)-fmod(Global.fTimeAngleDeg, 360.0)), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
break;
|
|
case TAnimType::at_DigiClk: // animacja zegara cyfrowego
|
|
{ // ustawienie animacji w submodelach potomnych
|
|
TSubModel *sm = ChildGet();
|
|
do
|
|
{ // pętla po submodelach potomnych i obracanie ich o kąt zależy od czasu
|
|
if (sm->pName.size())
|
|
{
|
|
// musi mieć niepustą nazwę
|
|
if (sm->pName[0] >= '0' && sm->pName[0] <= '5')
|
|
{
|
|
// zegarek ma 6 cyfr maksymalnie
|
|
sm->SetRotate(float3(0, 1, 0), -Global.fClockAngleDeg[sm->pName[0] - '0']);
|
|
}
|
|
}
|
|
sm = sm->NextGet();
|
|
} while (sm);
|
|
}
|
|
break;
|
|
}
|
|
if (mAnimMatrix) // można by to dać np. do at_Translate
|
|
{
|
|
m *= glm::make_mat4(mAnimMatrix->e);
|
|
mAnimMatrix.reset(); // jak animator będzie potrzebował, to ustawi ponownie
|
|
}
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
void TSubModel::serialize_geometry(std::ostream &Output, bool const Packed, bool const Indexed, bool const UserData) const
|
|
{
|
|
|
|
if (Child)
|
|
{
|
|
Child->serialize_geometry(Output, Packed, Indexed, UserData);
|
|
}
|
|
if (m_geometry.handle != null_handle)
|
|
{
|
|
if (Packed)
|
|
{
|
|
auto vertices = GfxRenderer->Vertices(m_geometry.handle);
|
|
auto userdatas = GfxRenderer->UserData(m_geometry.handle);
|
|
bool has_userdata = !userdatas.empty();
|
|
for (int i = 0; i < vertices.size(); ++i)
|
|
{
|
|
vertices[i].serialize_packed(Output, Indexed);
|
|
if (UserData)
|
|
{
|
|
if (has_userdata)
|
|
userdatas[i].serialize_packed(Output);
|
|
else
|
|
gfx::vertex_userdata{}.serialize_packed(Output);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
auto vertices = GfxRenderer->Vertices(m_geometry.handle);
|
|
auto userdatas = GfxRenderer->UserData(m_geometry.handle);
|
|
bool has_userdata = !userdatas.empty();
|
|
for (int i = 0; i < vertices.size(); ++i)
|
|
{
|
|
vertices[i].serialize(Output, Indexed);
|
|
if (UserData)
|
|
{
|
|
if (has_userdata)
|
|
userdatas[i].serialize(Output);
|
|
else
|
|
gfx::vertex_userdata{}.serialize(Output);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (Next)
|
|
{
|
|
Next->serialize_geometry(Output, Packed, Indexed, UserData);
|
|
}
|
|
};
|
|
|
|
int TSubModel::index_size() const
|
|
{
|
|
|
|
int size{1};
|
|
if (Child)
|
|
{
|
|
size = std::max(size, Child->index_size());
|
|
}
|
|
if (size < 4 && m_geometry.handle != null_handle)
|
|
{
|
|
auto const indexcount{GfxRenderer->Indices(m_geometry.handle).size()};
|
|
size = std::max(size, indexcount >= 1 << 16 ? 4 : indexcount >= 1 << 8 ? 2 : 1);
|
|
}
|
|
if (size < 4 && Next)
|
|
{
|
|
size = std::max(size, Next->index_size());
|
|
}
|
|
return size;
|
|
}
|
|
|
|
void TSubModel::serialize_indices(std::ostream &Output, int const Size) const
|
|
{
|
|
|
|
if (Child)
|
|
{
|
|
Child->serialize_indices(Output, Size);
|
|
}
|
|
if (m_geometry.handle != null_handle)
|
|
{
|
|
switch (Size)
|
|
{
|
|
case 1:
|
|
{
|
|
for (auto const &index : GfxRenderer->Indices(m_geometry.handle))
|
|
{
|
|
sn_utils::s_uint8(Output, index);
|
|
}
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
for (auto const &index : GfxRenderer->Indices(m_geometry.handle))
|
|
{
|
|
sn_utils::ls_uint16(Output, index);
|
|
}
|
|
break;
|
|
}
|
|
case 4:
|
|
{
|
|
for (auto const &index : GfxRenderer->Indices(m_geometry.handle))
|
|
{
|
|
sn_utils::ls_uint32(Output, index);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (Next)
|
|
{
|
|
Next->serialize_indices(Output, Size);
|
|
}
|
|
};
|
|
|
|
void TSubModel::create_geometry(std::size_t &Indexoffset, std::size_t &Vertexoffset, gfx::geometrybank_handle const &Bank)
|
|
{
|
|
|
|
// data offset is used to determine data offset of each submodel into single shared geometry bank
|
|
// (the offsets are part of legacy system which we now need to work around for backward compatibility)
|
|
if (Child)
|
|
{
|
|
Child->create_geometry(Indexoffset, Vertexoffset, Bank);
|
|
}
|
|
|
|
if (false == Vertices.empty())
|
|
{
|
|
|
|
m_geometry.index_offset = static_cast<int>(Indexoffset);
|
|
Indexoffset += Indices.size();
|
|
m_geometry.vertex_offset = static_cast<int>(Vertexoffset);
|
|
Vertexoffset += Vertices.size();
|
|
// conveniently all relevant custom node types use GL_POINTS, or we'd have to determine the type on individual basis
|
|
auto type = eType < TP_ROTATOR ? eType : GL_POINTS;
|
|
m_geometry.handle = GfxRenderer->Insert(Indices, Vertices, Userdata, Bank, type);
|
|
}
|
|
|
|
if (m_geometry.handle != 0)
|
|
{
|
|
// calculate bounding radius while we're at it
|
|
float squaredradius{};
|
|
// if this happens to be root node it may already have non-squared radius of the largest child
|
|
// since we're comparing squared radii, we need to square it back for correct results
|
|
m_boundingradius *= m_boundingradius;
|
|
auto const submodeloffset{offset(std::numeric_limits<float>::max())};
|
|
for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle))
|
|
{
|
|
squaredradius = glm::length2(submodeloffset + vertex.position);
|
|
if (squaredradius > m_boundingradius)
|
|
{
|
|
m_boundingradius = squaredradius;
|
|
}
|
|
}
|
|
if (m_boundingradius > 0.f)
|
|
{
|
|
m_boundingradius = std::sqrt(m_boundingradius);
|
|
}
|
|
// adjust overall radius if needed
|
|
// NOTE: the method to access root submodel is... less than ideal
|
|
auto *root{this};
|
|
while (root->Parent != nullptr)
|
|
{
|
|
root = root->Parent;
|
|
}
|
|
root->m_boundingradius = std::max(root->m_boundingradius, m_boundingradius);
|
|
}
|
|
|
|
if (Next)
|
|
{
|
|
Next->create_geometry(Indexoffset, Vertexoffset, Bank);
|
|
}
|
|
}
|
|
|
|
void TSubModel::ColorsSet(glm::vec3 const &Ambient, glm::vec3 const &Diffuse, glm::vec3 const &Specular)
|
|
{ // ustawienie kolorów dla modelu terenu
|
|
f4Ambient = glm::vec4(Ambient, 1.0f);
|
|
f4Diffuse = glm::vec4(Diffuse, 1.0f);
|
|
f4Specular = glm::vec4(Specular, 1.0f);
|
|
/*
|
|
int i;
|
|
if (a)
|
|
for (i = 0; i < 4; ++i)
|
|
f4Ambient[i] = a[i] / 255.0;
|
|
if (d)
|
|
for (i = 0; i < 4; ++i)
|
|
f4Diffuse[i] = d[i] / 255.0;
|
|
if (s)
|
|
for (i = 0; i < 4; ++i)
|
|
f4Specular[i] = s[i] / 255.0;
|
|
*/
|
|
};
|
|
|
|
bool TSubModel::is_emitter() const
|
|
{
|
|
|
|
return eType == TP_ROTATOR && ToLower(pName).find("smokesource_") == 0;
|
|
}
|
|
|
|
// pobranie transformacji względem wstawienia modelu
|
|
void TSubModel::ParentMatrix(float4x4 *m) const
|
|
{
|
|
|
|
m->Identity();
|
|
|
|
float4x4 submodelmatrix;
|
|
auto *submodel = this;
|
|
do
|
|
{
|
|
// for given step in hierarchy there can be custom transformation matrix, or no transformation
|
|
// retrieve it...
|
|
submodelmatrix.Identity();
|
|
if (submodel->GetMatrix())
|
|
{
|
|
submodelmatrix = float4x4(*submodel->GetMatrix());
|
|
}
|
|
// ...potentially adjust transformations of the root matrix if the model wasn't yet initialized...
|
|
if (submodel->Parent == nullptr && false == submodel->m_rotation_init_done)
|
|
{
|
|
// dla ostatniego może być potrzebny dodatkowy obrót, jeśli wczytano z T3D, a nie obrócono jeszcze
|
|
submodelmatrix.InitialRotate();
|
|
}
|
|
// ...combine the transformations...
|
|
*m = submodelmatrix * *m;
|
|
// ...and move up the transformation chain for the iteration...
|
|
submodel = submodel->Parent;
|
|
// ... until we hit the root
|
|
} while (submodel != nullptr);
|
|
/*
|
|
if( fMatrix != nullptr ) {
|
|
// skopiowanie, bo będziemy mnożyć
|
|
*m = float4x4( *fMatrix );
|
|
}
|
|
else {
|
|
m->Identity();
|
|
}
|
|
auto *sm = this;
|
|
while (sm->Parent)
|
|
{ // przenieść tę funkcję do modelu
|
|
if (sm->Parent->GetMatrix())
|
|
*m = *sm->Parent->GetMatrix() * *m;
|
|
sm = sm->Parent;
|
|
}
|
|
*/
|
|
};
|
|
|
|
void TSubModel::ReplaceMatrix(const glm::mat4 &mat)
|
|
{
|
|
for (int column = 0; column < 4; ++column)
|
|
for (int row = 0; row < 4; ++row)
|
|
fMatrix->e[column * 4 + row] = mat[column][row];
|
|
}
|
|
|
|
void TSubModel::ReplaceMaterial(const std::string &name)
|
|
{
|
|
m_material = GfxRenderer->Fetch_Material(name);
|
|
}
|
|
|
|
// obliczenie maksymalnej wysokości, na początek ślizgu w pantografie
|
|
float TSubModel::MaxY(float4x4 const &m)
|
|
{
|
|
// tylko dla trójkątów liczymy
|
|
if (eType != GL_TRIANGLES)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
auto maxy{0.0f};
|
|
// binary and text models invoke this function at different stages, either after or before geometry data was sent to the geometry manager
|
|
if (m_geometry.handle != null_handle)
|
|
{
|
|
|
|
for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle))
|
|
{
|
|
maxy = std::max(maxy, m[0][1] * vertex.position.x + m[1][1] * vertex.position.y + m[2][1] * vertex.position.z + m[3][1]);
|
|
}
|
|
}
|
|
else if (false == Vertices.empty())
|
|
{
|
|
|
|
for (auto const &vertex : Vertices)
|
|
{
|
|
maxy = std::max(maxy, m[0][1] * vertex.position.x + m[1][1] * vertex.position.y + m[2][1] * vertex.position.z + m[3][1]);
|
|
}
|
|
}
|
|
|
|
return maxy;
|
|
};
|
|
//---------------------------------------------------------------------------
|
|
|
|
TModel3d::~TModel3d()
|
|
{
|
|
|
|
if (iFlags & 0x0200)
|
|
{
|
|
// wczytany z pliku tekstowego, submodele sprzątają same
|
|
SafeDelete(Root);
|
|
}
|
|
else
|
|
{
|
|
// wczytano z pliku binarnego (jest właścicielem tablic)
|
|
SafeDeleteArray(Root); // submodele się usuną rekurencyjnie
|
|
}
|
|
};
|
|
|
|
TSubModel *TModel3d::AddToNamed(const char *Name, TSubModel *SubModel)
|
|
{
|
|
|
|
TSubModel *sm = Name ? GetFromName(Name) : nullptr;
|
|
if (sm == nullptr && Name != nullptr && std::strcmp(Name, "none") != 0)
|
|
{
|
|
ErrorLog("Bad model: parent for sub-model \"" + SubModel->pName + "\" doesn't exist or is located later in the model data", logtype::model);
|
|
}
|
|
AddTo(sm, SubModel); // szukanie nadrzędnego
|
|
return sm; // zwracamy wskaźnik do nadrzędnego submodelu
|
|
};
|
|
|
|
// jedyny poprawny sposób dodawania submodeli, inaczej mogą zginąć przy zapisie E3D
|
|
void TModel3d::AddTo(TSubModel *tmp, TSubModel *SubModel)
|
|
{
|
|
|
|
if (tmp)
|
|
{
|
|
// jeśli znaleziony, podłączamy mu jako potomny
|
|
tmp->ChildAdd(SubModel);
|
|
}
|
|
else
|
|
{ // jeśli nie znaleziony, podczepiamy do łańcucha głównego
|
|
SubModel->NextAdd(Root); // Ra: zmiana kolejności renderowania wymusza zmianę tu
|
|
Root = SubModel;
|
|
}
|
|
++iSubModelsCount; // teraz jest o 1 submodel więcej
|
|
iFlags |= 0x0200; // submodele są oddzielne
|
|
};
|
|
|
|
TSubModel *TModel3d::GetFromName(std::string const &Name) const
|
|
{ // wyszukanie submodelu po nazwie
|
|
if (Name.empty())
|
|
return Root; // potrzebne do terenu z E3D
|
|
// both text and binary models are searched recursively
|
|
return Root ? Root->GetFromName(Name) : nullptr;
|
|
};
|
|
|
|
// returns submodels which scenery instances of the model refer to by name, locating them on first use
|
|
TModel3d::instance_submodels const &TModel3d::instance_parts()
|
|
{
|
|
if (false == m_instancesubmodelslocated)
|
|
{
|
|
for (std::size_t idx = 0; idx < m_instancesubmodels.lights_on.size(); ++idx)
|
|
{
|
|
auto const index{(idx < 10 ? "0" : "") + std::to_string(idx)};
|
|
m_instancesubmodels.lights_on[idx] = GetFromName("Light_On" + index);
|
|
m_instancesubmodels.lights_off[idx] = GetFromName("Light_Off" + index);
|
|
}
|
|
m_instancesubmodels.variants = {GetFromName("winter_variant"), GetFromName("spring_variant"), GetFromName("summer_variant"), GetFromName("autumn_variant")};
|
|
m_instancesubmodelslocated = true;
|
|
}
|
|
return m_instancesubmodels;
|
|
}
|
|
|
|
// locates particle source submodels and stores them on internal list
|
|
nameoffset_sequence const &TModel3d::find_smoke_sources()
|
|
{
|
|
|
|
m_smokesources.clear();
|
|
if (Root != nullptr)
|
|
{
|
|
Root->find_smoke_sources(m_smokesources);
|
|
}
|
|
|
|
return smoke_sources();
|
|
}
|
|
|
|
// returns offset vector from root
|
|
glm::vec3 TSubModel::offset(float const Geometrytestoffsetthreshold) const
|
|
{
|
|
|
|
float4x4 parentmatrix;
|
|
ParentMatrix(&parentmatrix);
|
|
|
|
auto offset{glm::vec3{glm::make_mat4(parentmatrix.readArray()) * glm::vec4{0, 0, 0, 1}}};
|
|
|
|
if (glm::length(offset) < Geometrytestoffsetthreshold)
|
|
{
|
|
// offset of zero generally means the submodel has optimized identity matrix
|
|
// for such cases we resort to an estimate from submodel geometry
|
|
// TODO: do proper bounding area calculation for submodel when loading mesh and grab the centre point from it here
|
|
auto const &vertices{(m_geometry.handle != null_handle ? GfxRenderer->Vertices(m_geometry.handle) : Vertices)};
|
|
if (false == vertices.empty())
|
|
{
|
|
// transformation matrix for the submodel can still contain rotation and/or scaling,
|
|
// so we pass the vertex positions through it rather than just grab them directly
|
|
offset = glm::vec3();
|
|
auto const vertexfactor{1.f / vertices.size()};
|
|
auto const transformationmatrix{glm::make_mat4(parentmatrix.readArray())};
|
|
for (auto const &vertex : vertices)
|
|
{
|
|
offset += glm::vec3{transformationmatrix * glm::vec4{vertex.position, 1}} * vertexfactor;
|
|
}
|
|
}
|
|
}
|
|
|
|
return offset;
|
|
}
|
|
|
|
bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic)
|
|
{
|
|
// wczytanie modelu z pliku
|
|
/*
|
|
// NOTE: disabled, this work is now done by the model manager
|
|
std::string name = ToLower(FileName);
|
|
// trim extension if needed
|
|
if( name.rfind( '.' ) != std::string::npos )
|
|
{
|
|
name.erase(name.rfind('.'));
|
|
}
|
|
*/
|
|
auto const name{FileName};
|
|
// cache the file name, in case someone wants it later
|
|
m_filename = name;
|
|
|
|
asBinary = name + ".e3d";
|
|
// the files as they're named on the disk, the name given is in lowercase
|
|
auto const textfile{FileCased(name + ".t3d")};
|
|
auto const binaryfile{FileCased(asBinary)};
|
|
|
|
// Hirek: Jesli mamy ustawione priorityLoadText3D na yes to wpierw ladujemy t3d
|
|
if (Global.priorityLoadText3D && (false == textfile.empty()))
|
|
{
|
|
WriteLog("Forced loading text model \"" + textfile + "\"");
|
|
LoadFromTextFile(textfile, dynamic);
|
|
if (!dynamic)
|
|
Init();
|
|
}
|
|
else if (false == binaryfile.empty())
|
|
{
|
|
LoadFromBinFile(binaryfile, dynamic);
|
|
asBinary = "";
|
|
Init();
|
|
}
|
|
else if (false == textfile.empty())
|
|
{
|
|
LoadFromTextFile(textfile, dynamic);
|
|
if (!dynamic)
|
|
Init();
|
|
}
|
|
|
|
bool const result = Root ? iSubModelsCount > 0 : false; // brak pliku albo problem z wczytaniem
|
|
if (false == result)
|
|
{
|
|
ErrorLog("Bad model: failed to load 3d model \"" + name + "\"");
|
|
}
|
|
return result;
|
|
};
|
|
|
|
// E3D serialization
|
|
// http://rainsted.com/pl/Format_binarny_modeli_-_E3D
|
|
|
|
// m7todo: wymyślić lepszą nazwę
|
|
template <typename L, typename T> size_t get_container_pos(L &list, T o)
|
|
{
|
|
auto i = std::find(list.begin(), list.end(), o);
|
|
if (i == list.end())
|
|
{
|
|
list.push_back(o);
|
|
return list.size() - 1;
|
|
}
|
|
else
|
|
{
|
|
return std::distance(list.begin(), i);
|
|
}
|
|
}
|
|
|
|
// m7todo: za dużo argumentów, może przenieść do osobnej
|
|
// klasy serializera mającej własny stan, albo zrobić
|
|
// strukturę TModel3d::SerializerContext?
|
|
void TSubModel::serialize(std::ostream &s, std::vector<TSubModel *> &models, std::vector<std::string> &names, std::vector<std::string> &textures, std::vector<float4x4> &transforms)
|
|
{
|
|
size_t end = (size_t)s.tellp() + 256;
|
|
|
|
if (!Next)
|
|
sn_utils::ls_int32(s, -1);
|
|
else
|
|
sn_utils::ls_int32(s, (int32_t)get_container_pos(models, Next));
|
|
if (!Child)
|
|
sn_utils::ls_int32(s, -1);
|
|
else
|
|
sn_utils::ls_int32(s, (int32_t)get_container_pos(models, Child));
|
|
|
|
sn_utils::ls_int32(s, eType);
|
|
if (pName.size() == 0)
|
|
sn_utils::ls_int32(s, -1);
|
|
else
|
|
sn_utils::ls_int32(s, (int32_t)get_container_pos(names, pName));
|
|
sn_utils::ls_int32(s, (int)b_Anim);
|
|
|
|
uint32_t flags = iFlags;
|
|
if (m_material > 0 && Global.iConvertModels & 16)
|
|
flags &= ~0x30; // don't save phase information, will be guessed on binary load from material
|
|
sn_utils::ls_uint32(s, flags);
|
|
sn_utils::ls_int32(s, (int32_t)get_container_pos(transforms, *fMatrix));
|
|
|
|
sn_utils::ls_int32(s, m_geometry.vertex_count);
|
|
sn_utils::ls_int32(s, m_geometry.vertex_offset);
|
|
|
|
if (m_material <= 0)
|
|
sn_utils::ls_int32(s, m_material);
|
|
else
|
|
sn_utils::ls_int32(s, (int32_t)get_container_pos(textures, m_materialname));
|
|
|
|
sn_utils::ls_float32(s, 1.f); // fVisible
|
|
sn_utils::ls_float32(s, fLight);
|
|
|
|
sn_utils::s_vec4(s, f4Ambient);
|
|
sn_utils::s_vec4(s, f4Diffuse);
|
|
sn_utils::s_vec4(s, f4Specular);
|
|
sn_utils::s_vec4(s, f4Emision);
|
|
|
|
sn_utils::ls_float32(s, fWireSize);
|
|
sn_utils::ls_float32(s, fSquareMaxDist);
|
|
sn_utils::ls_float32(s, fSquareMinDist);
|
|
|
|
sn_utils::ls_float32(s, fNearAttenStart);
|
|
sn_utils::ls_float32(s, fNearAttenEnd);
|
|
sn_utils::ls_uint32(s, bUseNearAtten ? 1 : 0);
|
|
|
|
sn_utils::ls_int32(s, iFarAttenDecay);
|
|
sn_utils::ls_float32(s, fFarDecayRadius);
|
|
sn_utils::ls_float32(s, fCosFalloffAngle);
|
|
sn_utils::ls_float32(s, fCosHotspotAngle);
|
|
sn_utils::ls_float32(s, fCosViewAngle);
|
|
|
|
sn_utils::ls_int32(s, m_geometry.index_count);
|
|
sn_utils::ls_int32(s, m_geometry.index_offset);
|
|
|
|
// external data
|
|
sn_utils::ls_float32(s, diffuseMultiplier);
|
|
|
|
// fill empty space
|
|
size_t fill = end - s.tellp();
|
|
for (size_t i = 0; i < fill; i++)
|
|
s.put(0);
|
|
}
|
|
|
|
void TModel3d::SaveToBinFile(std::string const &FileName)
|
|
{
|
|
WriteLog("saving e3d model..");
|
|
|
|
// m7todo: można by zoptymalizować robiąc unordered_map
|
|
// na wyszukiwanie numerów już dodanych stringów i osobno
|
|
// vector na wskaźniki do stringów w kolejności numeracji
|
|
// tylko czy potrzeba?
|
|
std::vector<TSubModel *> models;
|
|
models.push_back(Root);
|
|
std::vector<std::string> names;
|
|
std::vector<std::string> textures;
|
|
textures.push_back("");
|
|
std::vector<float4x4> transforms;
|
|
|
|
std::ofstream s(FileName, std::ios::binary);
|
|
|
|
sn_utils::ls_uint32(s, MAKE_ID4('E', '3', 'D', '0'));
|
|
auto const e3d_spos = s.tellp();
|
|
sn_utils::ls_uint32(s, 0);
|
|
bool has_any_userdata = Root->HasAnyVertexUserData();
|
|
|
|
{
|
|
sn_utils::ls_uint32(s, MAKE_ID4('S', 'U', 'B', '0'));
|
|
auto const sub_spos = s.tellp();
|
|
sn_utils::ls_uint32(s, 0);
|
|
for (size_t i = 0; i < models.size(); i++)
|
|
models[i]->serialize(s, models, names, textures, transforms);
|
|
auto const pos = s.tellp();
|
|
s.seekp(sub_spos);
|
|
sn_utils::ls_uint32(s, (uint32_t)(4 + pos - sub_spos));
|
|
s.seekp(pos);
|
|
}
|
|
|
|
sn_utils::ls_uint32(s, MAKE_ID4('T', 'R', 'A', '0'));
|
|
sn_utils::ls_uint32(s, 8 + (uint32_t)transforms.size() * 64);
|
|
for (size_t i = 0; i < transforms.size(); i++)
|
|
transforms[i].serialize_float32(s);
|
|
|
|
auto const isindexed{m_indexcount > 0};
|
|
if (isindexed)
|
|
{
|
|
auto const indexsize{Root->index_size()};
|
|
sn_utils::ls_uint32(s, MAKE_ID4('I', 'D', 'X', '0' + indexsize));
|
|
sn_utils::ls_uint32(s, 8 + m_indexcount * indexsize);
|
|
Root->serialize_indices(s, indexsize);
|
|
|
|
if (!(Global.iConvertModels & 8))
|
|
{
|
|
int modeltype = 1;
|
|
int vertexsize = 20;
|
|
if (has_any_userdata)
|
|
{
|
|
modeltype |= 4;
|
|
vertexsize += 8;
|
|
}
|
|
sn_utils::ls_uint32(s, MAKE_ID4('V', 'N', 'T', '0' + modeltype));
|
|
sn_utils::ls_uint32(s, 8 + m_vertexcount * vertexsize);
|
|
Root->serialize_geometry(s, true, true, has_any_userdata);
|
|
}
|
|
else
|
|
{
|
|
int modeltype = 2;
|
|
int vertexsize = 48;
|
|
if (has_any_userdata)
|
|
{
|
|
modeltype |= 4;
|
|
vertexsize += 16;
|
|
}
|
|
sn_utils::ls_uint32(s, MAKE_ID4('V', 'N', 'T', '0' + modeltype));
|
|
sn_utils::ls_uint32(s, 8 + m_vertexcount * vertexsize);
|
|
Root->serialize_geometry(s, false, true, has_any_userdata);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int modeltype = 0;
|
|
int vertexsize = 32;
|
|
if (has_any_userdata)
|
|
{
|
|
modeltype |= 4;
|
|
vertexsize += 16;
|
|
}
|
|
sn_utils::ls_uint32(s, MAKE_ID4('V', 'N', 'T', '0' + modeltype));
|
|
sn_utils::ls_uint32(s, 8 + m_vertexcount * vertexsize);
|
|
Root->serialize_geometry(s, false, false, has_any_userdata);
|
|
}
|
|
|
|
if (textures.size())
|
|
{
|
|
sn_utils::ls_uint32(s, MAKE_ID4('T', 'E', 'X', '0'));
|
|
auto const tex_spos = s.tellp();
|
|
sn_utils::ls_uint32(s, 0);
|
|
for (size_t i = 0; i < textures.size(); i++)
|
|
sn_utils::s_str(s, textures[i]);
|
|
auto const pos = s.tellp();
|
|
s.seekp(tex_spos);
|
|
sn_utils::ls_uint32(s, (uint32_t)(4 + pos - tex_spos));
|
|
s.seekp(pos);
|
|
}
|
|
|
|
if (names.size())
|
|
{
|
|
sn_utils::ls_uint32(s, MAKE_ID4('N', 'A', 'M', '0'));
|
|
auto const nam_spos = s.tellp();
|
|
sn_utils::ls_uint32(s, 0);
|
|
for (size_t i = 0; i < names.size(); i++)
|
|
sn_utils::s_str(s, names[i]);
|
|
auto const pos = s.tellp();
|
|
s.seekp(nam_spos);
|
|
sn_utils::ls_uint32(s, (uint32_t)(4 + pos - nam_spos));
|
|
s.seekp(pos);
|
|
}
|
|
|
|
auto const end = s.tellp();
|
|
s.seekp(e3d_spos);
|
|
sn_utils::ls_uint32(s, (uint32_t)(4 + end - e3d_spos));
|
|
s.close();
|
|
|
|
WriteLog("..done.");
|
|
}
|
|
|
|
void TSubModel::deserialize(std::istream &s)
|
|
{
|
|
iNext = sn_utils::ld_int32(s);
|
|
iChild = sn_utils::ld_int32(s);
|
|
|
|
eType = sn_utils::ld_int32(s);
|
|
iName = sn_utils::ld_int32(s);
|
|
|
|
b_Anim = (TAnimType)sn_utils::ld_int32(s);
|
|
|
|
iFlags = sn_utils::ld_uint32(s);
|
|
iMatrix = sn_utils::ld_int32(s);
|
|
|
|
m_geometry.vertex_count = sn_utils::ld_int32(s);
|
|
m_geometry.vertex_offset = sn_utils::ld_int32(s);
|
|
iTexture = sn_utils::ld_int32(s);
|
|
|
|
auto discard = sn_utils::ld_float32(s); // fVisible
|
|
fLight = sn_utils::ld_float32(s);
|
|
|
|
f4Ambient = sn_utils::d_vec4(s);
|
|
f4Diffuse = sn_utils::d_vec4(s);
|
|
f4Specular = sn_utils::d_vec4(s);
|
|
f4Emision = sn_utils::d_vec4(s);
|
|
|
|
fWireSize = sn_utils::ld_float32(s);
|
|
fSquareMaxDist = sn_utils::ld_float32(s);
|
|
fSquareMinDist = sn_utils::ld_float32(s);
|
|
|
|
fNearAttenStart = sn_utils::ld_float32(s);
|
|
fNearAttenEnd = sn_utils::ld_float32(s);
|
|
bUseNearAtten = sn_utils::ld_uint32(s) != 0;
|
|
iFarAttenDecay = sn_utils::ld_int32(s);
|
|
fFarDecayRadius = sn_utils::ld_float32(s);
|
|
fCosFalloffAngle = sn_utils::ld_float32(s);
|
|
fCosHotspotAngle = sn_utils::ld_float32(s);
|
|
fCosViewAngle = sn_utils::ld_float32(s);
|
|
// HACK: the values will be 0 also when reading legacy chunk
|
|
m_geometry.index_count = sn_utils::ld_int32(s);
|
|
m_geometry.index_offset = sn_utils::ld_int32(s);
|
|
|
|
// extra data block
|
|
diffuseMultiplier = sn_utils::ld_float32(s);
|
|
// only multiply diffuse on experimental renderer
|
|
if (!Global.NvRenderer)
|
|
f4Diffuse /= diffuseMultiplier <= 0.0 ? 1.0 : diffuseMultiplier;
|
|
|
|
// necessary rotations were already done during t3d->e3d conversion
|
|
m_rotation_init_done = true;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
// read only stream buffer over the content of a whole e3d file kept in memory.
|
|
// reading the geometry of a model value by value from a file stream costs several times more than the read of the file itself
|
|
class e3d_memorybuffer : public std::streambuf
|
|
{
|
|
public:
|
|
e3d_memorybuffer(char *Begin, char *End)
|
|
{
|
|
setg(Begin, Begin, End);
|
|
}
|
|
// data from the current read position on
|
|
unsigned char const *cursor() const
|
|
{
|
|
return reinterpret_cast<unsigned char const *>(gptr());
|
|
}
|
|
std::size_t available() const
|
|
{
|
|
return static_cast<std::size_t>(egptr() - gptr());
|
|
}
|
|
|
|
protected:
|
|
pos_type seekoff(off_type Offset, std::ios_base::seekdir Direction, std::ios_base::openmode) override
|
|
{
|
|
off_type const size{egptr() - eback()};
|
|
off_type const origin{Direction == std::ios_base::beg ? 0 : Direction == std::ios_base::cur ? (gptr() - eback()) + m_beyondend : size};
|
|
off_type const position{origin + Offset};
|
|
if (position < 0)
|
|
{
|
|
return pos_type(off_type(-1));
|
|
}
|
|
// like a file stream it can be placed past the end of the data, where nothing more can be read
|
|
m_beyondend = std::max<off_type>(position - size, 0);
|
|
setg(eback(), eback() + std::min(position, size), egptr());
|
|
return pos_type(position);
|
|
}
|
|
pos_type seekpos(pos_type Position, std::ios_base::openmode Mode) override
|
|
{
|
|
return seekoff(off_type(Position), std::ios_base::beg, Mode);
|
|
}
|
|
|
|
private:
|
|
off_type m_beyondend{0};
|
|
};
|
|
|
|
// little endian values taken from memory the same way the sn_utils functions read them from a stream
|
|
std::uint16_t e3d_uint16(unsigned char const *&Data)
|
|
{
|
|
std::uint16_t const value = static_cast<std::uint16_t>(Data[1] << 8 | Data[0]);
|
|
Data += 2;
|
|
return value;
|
|
}
|
|
std::uint32_t e3d_uint32(unsigned char const *&Data)
|
|
{
|
|
std::uint32_t const value = std::uint32_t{Data[3]} << 24 | std::uint32_t{Data[2]} << 16 | std::uint32_t{Data[1]} << 8 | std::uint32_t{Data[0]};
|
|
Data += 4;
|
|
return value;
|
|
}
|
|
std::uint64_t e3d_uint64(unsigned char const *&Data)
|
|
{
|
|
std::uint64_t const low{e3d_uint32(Data)};
|
|
std::uint64_t const high{e3d_uint32(Data)};
|
|
return high << 32 | low;
|
|
}
|
|
float e3d_float32(unsigned char const *&Data)
|
|
{
|
|
return std::bit_cast<float>(e3d_uint32(Data));
|
|
}
|
|
glm::vec3 e3d_vec3(unsigned char const *&Data)
|
|
{
|
|
glm::vec3 value;
|
|
value.x = e3d_float32(Data);
|
|
value.y = e3d_float32(Data);
|
|
value.z = e3d_float32(Data);
|
|
return value;
|
|
}
|
|
glm::vec4 e3d_vec4(unsigned char const *&Data)
|
|
{
|
|
glm::vec4 value;
|
|
value.x = e3d_float32(Data);
|
|
value.y = e3d_float32(Data);
|
|
value.z = e3d_float32(Data);
|
|
value.w = e3d_float32(Data);
|
|
return value;
|
|
}
|
|
// counterparts of basic_vertex::deserialize() and basic_vertex::deserialize_packed()
|
|
void e3d_vertex(unsigned char const *&Data, gfx::basic_vertex &Vertex, bool const Tangent)
|
|
{
|
|
Vertex.position = e3d_vec3(Data);
|
|
Vertex.normal = e3d_vec3(Data);
|
|
Vertex.texture.x = e3d_float32(Data);
|
|
Vertex.texture.y = e3d_float32(Data);
|
|
if (Tangent)
|
|
{
|
|
Vertex.tangent = e3d_vec4(Data);
|
|
}
|
|
}
|
|
void e3d_vertex_packed(unsigned char const *&Data, gfx::basic_vertex &Vertex, bool const Tangent)
|
|
{
|
|
Vertex.position = glm::unpackHalf4x16(e3d_uint64(Data));
|
|
Vertex.normal = glm::unpackSnorm3x10_1x2(e3d_uint32(Data));
|
|
Vertex.texture.x = glm::unpackHalf1x16(e3d_uint16(Data));
|
|
Vertex.texture.y = glm::unpackHalf1x16(e3d_uint16(Data));
|
|
if (Tangent)
|
|
{
|
|
Vertex.tangent = glm::unpackSnorm3x10_1x2(e3d_uint32(Data));
|
|
}
|
|
}
|
|
} // namespace
|
|
|
|
void TModel3d::deserialize(std::istream &s, size_t size, bool dynamic)
|
|
{
|
|
// a model loaded by LoadFromBinFile() comes from memory, its geometry is taken straight from there
|
|
auto *const memory{dynamic_cast<e3d_memorybuffer *>(s.rdbuf())};
|
|
|
|
Root = nullptr;
|
|
if (m_geometrybank == null_handle)
|
|
{
|
|
m_geometrybank = GfxRenderer->Create_Bank();
|
|
}
|
|
|
|
std::streampos end = s.tellg() + (std::streampos)size;
|
|
bool hastangents{false};
|
|
bool hasuserdata{false};
|
|
|
|
while (s.tellg() < end)
|
|
{
|
|
uint32_t type = sn_utils::ld_uint32(s);
|
|
uint32_t size = sn_utils::ld_uint32(s) - 8;
|
|
size_t pos = s.tellg();
|
|
std::streampos end = pos + (std::streampos)size;
|
|
|
|
if ((type & 0x00FFFFFF) == MAKE_ID4('S', 'U', 'B', 0)) // submodels
|
|
{
|
|
if (Root != nullptr)
|
|
throw std::runtime_error("e3d: duplicated SUB chunk");
|
|
|
|
size_t sm_size = 256 + 64 * (((type & 0xFF000000) >> 24) - '0');
|
|
size_t sm_cnt = size / sm_size;
|
|
iSubModelsCount = (int)sm_cnt;
|
|
Root = new TSubModel[sm_cnt];
|
|
for (size_t i = 0; i < sm_cnt; ++i)
|
|
{
|
|
s.seekg(pos + sm_size * i);
|
|
Root[i].deserialize(s);
|
|
}
|
|
}
|
|
else if ((type & 0x00FFFFFF) == MAKE_ID4('V', 'N', 'T', 0)) // geometry vertices
|
|
{
|
|
// we rely on the SUB chunk coming before the vertex data, and on the overall vertex count matching the size of data in the chunk.
|
|
// geometry associated with chunks isn't stored in the same order as the chunks themselves, so we need to sort that out first
|
|
if (Root == nullptr)
|
|
throw std::runtime_error("e3d: VNT chunk encountered before SUB chunk");
|
|
std::vector<std::pair<int, int>> submodeloffsets; // vertex data offset, submodel index
|
|
submodeloffsets.reserve(iSubModelsCount);
|
|
for (auto submodelindex = 0; submodelindex < iSubModelsCount; ++submodelindex)
|
|
{
|
|
auto const &submodelgeometry{Root[submodelindex].m_geometry};
|
|
if (submodelgeometry.vertex_count <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
submodeloffsets.emplace_back(submodelgeometry.vertex_offset, submodelindex);
|
|
}
|
|
std::sort(std::begin(submodeloffsets), std::end(submodeloffsets), [](std::pair<int, int> const &Left, std::pair<int, int> const &Right) { return Left.first < Right.first; });
|
|
// once sorted we can grab geometry as it comes, and assign it to the chunks it belongs to
|
|
size_t const vertextype{(((type & 0xFF000000) >> 24) - '0')};
|
|
hastangents = (vertextype & 3) > 0;
|
|
hasuserdata = vertextype & 4;
|
|
size_t vertex_size = 0;
|
|
switch (vertextype & 3)
|
|
{
|
|
case 0:
|
|
case 2:
|
|
{
|
|
vertex_size = 8;
|
|
if (hastangents)
|
|
vertex_size += 4;
|
|
if (hasuserdata)
|
|
vertex_size += 4;
|
|
break;
|
|
}
|
|
case 1:
|
|
{
|
|
vertex_size = 4;
|
|
if (hastangents)
|
|
vertex_size += 1;
|
|
if (hasuserdata)
|
|
vertex_size += 2;
|
|
break;
|
|
}
|
|
}
|
|
for (auto const &submodeloffset : submodeloffsets)
|
|
{
|
|
s.seekg(pos + submodeloffset.first * vertex_size * sizeof(float)); // Ensure that we start reading from the correct offset even if geometry is not encoded contiguously
|
|
auto &submodel{Root[submodeloffset.second]};
|
|
auto const &submodelgeometry{submodel.m_geometry};
|
|
submodel.Vertices.resize(submodelgeometry.vertex_count);
|
|
if (hasuserdata)
|
|
submodel.Userdata.resize(submodelgeometry.vertex_count);
|
|
m_vertexcount += submodelgeometry.vertex_count;
|
|
if (memory != nullptr)
|
|
{
|
|
// geometry past the end of the file is left as it was made
|
|
auto const datasize{vertex_size * sizeof(float)};
|
|
auto const vertexcount{std::min<std::size_t>(submodel.Vertices.size(), (datasize > 0 ? memory->available() / datasize : 0))};
|
|
if ((datasize > 0) && (vertexcount < submodel.Vertices.size()))
|
|
{
|
|
ErrorLog("Bad model: vertex data of sub-model " + std::to_string(submodeloffset.second) + " lies past the end of the file", logtype::model);
|
|
}
|
|
auto *data{memory->cursor()};
|
|
switch (vertextype & 3)
|
|
{
|
|
case 0:
|
|
{
|
|
// legacy vnt0 format
|
|
for (std::size_t i = 0; i < vertexcount; ++i)
|
|
{
|
|
e3d_vertex(data, submodel.Vertices[i], hastangents);
|
|
if (hasuserdata)
|
|
submodel.Userdata[i].data = e3d_vec4(data);
|
|
if (submodel.eType < TP_ROTATOR)
|
|
{
|
|
// normal vectors debug routine
|
|
if (false == submodel.m_normalizenormals && std::abs(glm::length2(submodel.Vertices[i].normal) - 1.0f) > 0.01f)
|
|
{
|
|
submodel.m_normalizenormals = TSubModel::normalize; // we don't know if uniform scaling would suffice
|
|
WriteLog("Bad model: non-unit normal vector(s) encountered during sub-model geometry deserialization", logtype::model);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case 1:
|
|
{
|
|
// expanded chunk formats
|
|
for (std::size_t i = 0; i < vertexcount; ++i)
|
|
{
|
|
e3d_vertex_packed(data, submodel.Vertices[i], hastangents);
|
|
if (hasuserdata)
|
|
submodel.Userdata[i].data = glm::unpackHalf4x16(e3d_uint64(data));
|
|
}
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
// expanded chunk formats
|
|
for (std::size_t i = 0; i < vertexcount; ++i)
|
|
{
|
|
e3d_vertex(data, submodel.Vertices[i], hastangents);
|
|
if (hasuserdata)
|
|
submodel.Userdata[i].data = e3d_vec4(data);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
switch (vertextype & 3)
|
|
{
|
|
case 0:
|
|
{
|
|
// legacy vnt0 format
|
|
for (int i = 0; i < submodel.Vertices.size(); ++i)
|
|
{
|
|
submodel.Vertices[i].deserialize(s, hastangents);
|
|
if (hasuserdata)
|
|
submodel.Userdata[i].deserialize(s);
|
|
if (submodel.eType < TP_ROTATOR)
|
|
{
|
|
// normal vectors debug routine
|
|
if (false == submodel.m_normalizenormals && std::abs(glm::length2(submodel.Vertices[i].normal) - 1.0f) > 0.01f)
|
|
{
|
|
submodel.m_normalizenormals = TSubModel::normalize; // we don't know if uniform scaling would suffice
|
|
WriteLog("Bad model: non-unit normal vector(s) encountered during sub-model geometry deserialization", logtype::model);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case 1:
|
|
{
|
|
// expanded chunk formats
|
|
for (int i = 0; i < submodel.Vertices.size(); ++i)
|
|
{
|
|
submodel.Vertices[i].deserialize_packed(s, hastangents);
|
|
if (hasuserdata)
|
|
submodel.Userdata[i].deserialize_packed(s);
|
|
}
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
// expanded chunk formats
|
|
for (int i = 0; i < submodel.Vertices.size(); ++i)
|
|
{
|
|
submodel.Vertices[i].deserialize(s, hastangents);
|
|
if (hasuserdata)
|
|
submodel.Userdata[i].deserialize(s);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
// TBD, TODO: throw error here?
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if ((type & 0x00FFFFFF) == MAKE_ID4('I', 'D', 'X', 0)) // geometry indices
|
|
{
|
|
// handled similarly to vertex data chunk
|
|
// we rely on the SUB chunk coming before the vertex data, and on the overall vertex count matching the size of data in the chunk.
|
|
// geometry associated with chunks isn't stored in the same order as the chunks themselves, so we need to sort that out first
|
|
if (Root == nullptr)
|
|
throw std::runtime_error("e3d: IDX chunk encountered before SUB chunk");
|
|
std::vector<std::pair<int, int>> submodeloffsets; // index data offset, submodel index
|
|
submodeloffsets.reserve(iSubModelsCount);
|
|
for (auto submodelindex = 0; submodelindex < iSubModelsCount; ++submodelindex)
|
|
{
|
|
auto const &submodelgeometry{Root[submodelindex].m_geometry};
|
|
if (submodelgeometry.index_count <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
submodeloffsets.emplace_back(submodelgeometry.index_offset, submodelindex);
|
|
}
|
|
std::sort(std::begin(submodeloffsets), std::end(submodeloffsets), [](std::pair<int, int> const &Left, std::pair<int, int> const &Right) { return Left.first < Right.first; });
|
|
// once sorted we can grab indices in a continuous read, and assign them to the chunks they belong to
|
|
size_t const indexsize{(((type & 0xFF000000) >> 24) - '0')};
|
|
for (auto const &submodeloffset : submodeloffsets)
|
|
{
|
|
s.seekg(pos + submodeloffset.first * indexsize);
|
|
auto &submodel{Root[submodeloffset.second]};
|
|
auto const &submodelgeometry{submodel.m_geometry};
|
|
submodel.Indices.resize(submodelgeometry.index_count);
|
|
m_indexcount += submodelgeometry.index_count;
|
|
if (memory != nullptr)
|
|
{
|
|
// indices past the end of the file are left as zeroes
|
|
auto const indexcount{std::min<std::size_t>(submodel.Indices.size(), (indexsize > 0 ? memory->available() / indexsize : 0))};
|
|
if ((indexsize > 0) && (indexcount < submodel.Indices.size()))
|
|
{
|
|
ErrorLog("Bad model: index data of sub-model " + std::to_string(submodeloffset.second) + " lies past the end of the file", logtype::model);
|
|
}
|
|
auto *data{memory->cursor()};
|
|
switch (indexsize)
|
|
{
|
|
case 1:
|
|
{
|
|
for (std::size_t i = 0; i < indexcount; ++i)
|
|
{
|
|
submodel.Indices[i] = *data++;
|
|
}
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
for (std::size_t i = 0; i < indexcount; ++i)
|
|
{
|
|
submodel.Indices[i] = e3d_uint16(data);
|
|
}
|
|
break;
|
|
}
|
|
case 4:
|
|
{
|
|
for (std::size_t i = 0; i < indexcount; ++i)
|
|
{
|
|
submodel.Indices[i] = e3d_uint32(data);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
switch (indexsize)
|
|
{
|
|
case 1:
|
|
{
|
|
for (auto &index : submodel.Indices)
|
|
{
|
|
index = sn_utils::d_uint8(s);
|
|
}
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
for (auto &index : submodel.Indices)
|
|
{
|
|
index = sn_utils::ld_uint16(s);
|
|
}
|
|
break;
|
|
}
|
|
case 4:
|
|
{
|
|
for (auto &index : submodel.Indices)
|
|
{
|
|
index = sn_utils::ld_uint32(s);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (type == MAKE_ID4('T', 'R', 'A', '0'))
|
|
{
|
|
if (false == Matrices.empty())
|
|
throw std::runtime_error("e3d: duplicated TRA chunk");
|
|
size_t t_cnt = size / 64;
|
|
|
|
Matrices.resize(t_cnt);
|
|
for (size_t i = 0; i < t_cnt; ++i)
|
|
Matrices[i].deserialize_float32(s);
|
|
}
|
|
else if (type == MAKE_ID4('T', 'R', 'A', '1'))
|
|
{
|
|
if (false == Matrices.empty())
|
|
throw std::runtime_error("e3d: duplicated TRA chunk");
|
|
size_t t_cnt = size / 128;
|
|
|
|
Matrices.resize(t_cnt);
|
|
for (size_t i = 0; i < t_cnt; ++i)
|
|
Matrices[i].deserialize_float64(s);
|
|
}
|
|
else if (type == MAKE_ID4('T', 'E', 'X', '0'))
|
|
{
|
|
if (Textures.size())
|
|
throw std::runtime_error("e3d: duplicated TEX chunk");
|
|
while (s.tellg() < end)
|
|
{
|
|
std::string str = sn_utils::d_str(s);
|
|
std::replace(str.begin(), str.end(), '\\', '/');
|
|
Textures.push_back(str);
|
|
}
|
|
}
|
|
else if (type == MAKE_ID4('N', 'A', 'M', '0'))
|
|
{
|
|
if (Names.size())
|
|
throw std::runtime_error("e3d: duplicated NAM chunk");
|
|
while (s.tellg() < end)
|
|
Names.push_back(sn_utils::d_str(s));
|
|
}
|
|
|
|
s.seekg(end);
|
|
}
|
|
|
|
if (!Root)
|
|
throw std::runtime_error("e3d: no submodels");
|
|
|
|
for (size_t i = 0; (int)i < iSubModelsCount; ++i)
|
|
{
|
|
Root[i].BinInit(Root, Matrices.data(), &Textures, &Names, dynamic);
|
|
|
|
if (Root[i].ChildGet())
|
|
Root[i].ChildGet()->Parent = &Root[i];
|
|
if (Root[i].NextGet())
|
|
Root[i].NextGet()->Parent = Root[i].Parent;
|
|
|
|
// remap geometry type for custom type submodels
|
|
int type;
|
|
switch (Root[i].eType)
|
|
{
|
|
case TP_FREESPOTLIGHT:
|
|
case TP_STARS:
|
|
{
|
|
type = GL_POINTS;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
type = Root[i].eType;
|
|
break;
|
|
}
|
|
}
|
|
if (false == hastangents)
|
|
{
|
|
gfx::calculate_tangents(Root[i].Vertices, Root[i].Indices, type);
|
|
}
|
|
Root[i].m_geometry.handle = GfxRenderer->Insert(Root[i].Indices, Root[i].Vertices, Root[i].Userdata, m_geometrybank, type);
|
|
}
|
|
}
|
|
|
|
void TSubModel::BinInit(TSubModel *s, float4x4 *m, std::vector<std::string> *t, std::vector<std::string> *n, bool dynamic)
|
|
{ // ustawienie wskaźników w submodelu
|
|
// m7todo: brzydko
|
|
iVisible = 1; // tymczasowo używane
|
|
Child = iChild > 0 ? s + iChild : nullptr; // zerowy nie może być potomnym
|
|
Next = iNext > 0 ? s + iNext : nullptr; // zerowy nie może być następnym
|
|
fMatrix = iMatrix >= 0 && m ? m + iMatrix : nullptr;
|
|
|
|
if (n->size() && iName >= 0)
|
|
{
|
|
pName = n->at(iName);
|
|
if (!pName.empty())
|
|
{ // jeśli dany submodel jest zgaszonym światłem, to
|
|
// domyślnie go ukrywamy
|
|
if (pName.starts_with("Light_On"))
|
|
{ // jeśli jest światłem numerowanym
|
|
iVisible = 0; // to domyślnie wyłączyć, żeby się nie nakładało z obiektem "Light_Off"
|
|
}
|
|
else if (dynamic)
|
|
{ // inaczej wyłączało smugę w latarniach
|
|
if (pName.ends_with("_on"))
|
|
{
|
|
// jeśli jest kontrolką w stanie zapalonym to domyślnie wyłączyć,
|
|
// żeby się nie nakładało z obiektem "_off"
|
|
iVisible = 0;
|
|
}
|
|
}
|
|
// hack: reset specular light value for shadow submodels
|
|
if (pName == "cien")
|
|
{
|
|
f4Specular = glm::vec4{0.0f, 0.0f, 0.0f, 1.0f};
|
|
}
|
|
}
|
|
}
|
|
else
|
|
pName = "";
|
|
if (iTexture > 0)
|
|
{ // obsługa stałej tekstury
|
|
auto const materialindex = static_cast<std::size_t>(iTexture);
|
|
if (materialindex < t->size())
|
|
{
|
|
m_materialname = t->at(materialindex);
|
|
/*
|
|
if( m_materialname.find_last_of( "/" ) == std::string::npos ) {
|
|
m_materialname = Global.asCurrentTexturePath + m_materialname;
|
|
}
|
|
*/
|
|
m_material = GfxRenderer->Fetch_Material(m_materialname);
|
|
// if we don't have phase flags set for some reason, try to fix it
|
|
if (!(iFlags & 0x30) && m_material != null_handle)
|
|
{
|
|
const IMaterial *mat = GfxRenderer->Material(m_material);
|
|
float opacity = mat->get_or_guess_opacity();
|
|
|
|
// set phase flag based on material opacity
|
|
if (opacity == 0.0f)
|
|
iFlags |= 0x20; // translucent
|
|
else
|
|
iFlags |= 0x10; // opaque
|
|
}
|
|
|
|
if (m_material != null_handle)
|
|
{
|
|
IMaterial const *mat = GfxRenderer->Material(m_material);
|
|
/*
|
|
// if material does have opacity set, replace submodel opacity with it
|
|
if (mat.opacity)
|
|
{
|
|
iFlags &= ~0x30;
|
|
if (*mat.opacity == 0.0f)
|
|
iFlags |= 0x20; // translucent
|
|
else
|
|
iFlags |= 0x10; // opaque
|
|
}
|
|
*/
|
|
// replace submodel selfillum with material one
|
|
if (mat->GetSelfillum())
|
|
{
|
|
fLight = mat->GetSelfillum().value();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ErrorLog("Bad model: reference to nonexistent texture index in sub-model" + (pName.empty() ? "" : " \"" + pName + "\""), logtype::model);
|
|
m_material = null_handle;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (iTexture == 0)
|
|
m_material = GfxRenderer->Fetch_Material("colored");
|
|
else
|
|
m_material = iTexture;
|
|
}
|
|
|
|
b_aAnim = b_Anim; // skopiowanie animacji do drugiego cyklu
|
|
|
|
if (eType == TP_STARS)
|
|
{
|
|
m_material = GfxRenderer->Fetch_Material("stars");
|
|
iFlags |= 0x10;
|
|
}
|
|
else if (eType == TP_FREESPOTLIGHT && iFlags & 0x10)
|
|
{
|
|
// we've added light glare which needs to be rendered during transparent phase,
|
|
// but models converted to e3d before addition won't have the render flag set correctly for this
|
|
// so as a workaround we're doing it here manually
|
|
iFlags |= 0x20;
|
|
}
|
|
// intercept and fix hotspot values if specified in degrees and not directly
|
|
if (fCosFalloffAngle > 1.0f)
|
|
{
|
|
fCosFalloffAngle = std::cos(glm::radians(0.5f * fCosFalloffAngle));
|
|
}
|
|
if (fCosHotspotAngle > 1.0f)
|
|
{
|
|
fCosHotspotAngle = std::cos(glm::radians(0.5f * fCosHotspotAngle));
|
|
}
|
|
// cap specular values for legacy models
|
|
f4Specular = glm::vec4{std::clamp(f4Specular.r, 0.0f, 1.0f), std::clamp(f4Specular.g, 0.0f, 1.0f), std::clamp(f4Specular.b, 0.0f, 1.0f), std::clamp(f4Specular.a, 0.0f, 1.0f)};
|
|
|
|
iFlags &= ~0x0200; // wczytano z pliku binarnego (nie jest właścicielem tablic)
|
|
|
|
if (fMatrix != nullptr)
|
|
{
|
|
auto const matrix = glm::make_mat4(fMatrix->readArray());
|
|
glm::vec3 const scale{glm::length(glm::vec3(glm::column(matrix, 0))), glm::length(glm::vec3(glm::column(matrix, 1))), glm::length(glm::vec3(glm::column(matrix, 2)))};
|
|
if (std::abs(scale.x - 1.0f) > 0.01 || std::abs(scale.y - 1.0f) > 0.01 || std::abs(scale.z - 1.0f) > 0.01)
|
|
{
|
|
ErrorLog("Bad model: transformation matrix for sub-model \"" + pName + "\" imposes geometry scaling (factors: " + to_string(scale) + ")", logtype::model);
|
|
m_normalizenormals = std::abs(scale.x - scale.y) < 0.01f && std::abs(scale.y - scale.z) < 0.01f ? rescale : normalize;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool TSubModel::HasAnyVertexUserData() const
|
|
{
|
|
for (const TSubModel *sm = this; sm; sm = sm->Next)
|
|
{
|
|
if (m_geometry.handle)
|
|
{
|
|
if (!GfxRenderer->UserData(m_geometry.handle).empty())
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (!sm->Userdata.empty())
|
|
return true;
|
|
}
|
|
if (sm->Child && sm->Child->HasAnyVertexUserData())
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
void TModel3d::LoadFromBinFile(std::string const &FileName, bool dynamic)
|
|
{ // wczytanie modelu z pliku binarnego
|
|
WriteLog("Loading binary format 3d model data from \"" + FileName + "\"...", logtype::model);
|
|
|
|
// the file is read whole in one go and parsed from memory
|
|
std::unique_ptr<char[]> filedata;
|
|
std::size_t filesize{0};
|
|
{
|
|
std::ifstream input(FileName, std::ios::binary | std::ios::ate);
|
|
auto const length{input.tellg()};
|
|
if (length > 0)
|
|
{
|
|
filedata.reset(new char[static_cast<std::size_t>(length)]);
|
|
input.seekg(0);
|
|
input.read(filedata.get(), length);
|
|
filesize = static_cast<std::size_t>(input.gcount());
|
|
}
|
|
}
|
|
e3d_memorybuffer buffer(filedata.get(), filedata.get() + filesize);
|
|
std::istream file(&buffer);
|
|
|
|
uint32_t type = sn_utils::ld_uint32(file);
|
|
uint32_t size = sn_utils::ld_uint32(file) - 8;
|
|
|
|
if (type == MAKE_ID4('E', '3', 'D', '0'))
|
|
{
|
|
deserialize(file, size, dynamic);
|
|
|
|
WriteLog("Finished loading 3d model data from \"" + FileName + "\"", logtype::model);
|
|
}
|
|
else
|
|
{
|
|
// throw std::runtime_error("e3d: unknown main chunk");
|
|
ErrorLog("Bad model: unknown main chunk in file \"" + FileName + "\"", logtype::model);
|
|
}
|
|
};
|
|
|
|
TSubModel *TModel3d::AppendChildFromGeometry(const std::string &name, const std::string &parent, const gfx::vertex_array &vertices, const gfx::index_array &indices)
|
|
{
|
|
iFlags |= 0x0200;
|
|
|
|
TSubModel *sm = new TSubModel();
|
|
sm->Parent = AddToNamed(parent.c_str(), sm);
|
|
sm->m_geometry.vertex_count = vertices.size();
|
|
sm->m_geometry.index_count = indices.size();
|
|
sm->eType = GL_TRIANGLES;
|
|
sm->pName = name;
|
|
sm->m_material = GfxRenderer->Fetch_Material("colored");
|
|
sm->fMatrix = new float4x4();
|
|
sm->fMatrix->Identity();
|
|
sm->iFlags |= 0x10;
|
|
sm->iFlags |= 0x8000;
|
|
sm->WillBeAnimated();
|
|
if (vertices.empty())
|
|
sm->iFlags &= ~0x3F;
|
|
sm->Vertices = vertices;
|
|
sm->Indices = indices;
|
|
m_vertexcount += vertices.size();
|
|
m_indexcount += indices.size();
|
|
|
|
if (!Root)
|
|
Root = sm;
|
|
|
|
return sm;
|
|
}
|
|
|
|
void TModel3d::LoadFromTextFile(std::string const &FileName, bool dynamic)
|
|
{ // wczytanie submodelu z pliku tekstowego
|
|
WriteLog("Loading text format 3d model data from \"" + FileName + "\"...", logtype::model);
|
|
iFlags |= 0x0200; // wczytano z pliku tekstowego (właścicielami tablic są submodle)
|
|
cParser parser(FileName, cParser::buffer_FILE); // Ra: tu powinno być "models/"...
|
|
TSubModel *SubModel;
|
|
std::string token = parser.getToken<std::string>();
|
|
while (token != "" || parser.eof())
|
|
{
|
|
std::string parent;
|
|
// parser.getToken(parent);
|
|
parser.getTokens(1, false); // nazwa submodelu nadrzędnego bez zmieny na małe
|
|
parser >> parent;
|
|
if (parent == "")
|
|
{
|
|
break;
|
|
}
|
|
SubModel = new TSubModel();
|
|
SubModel->Parent = GetFromName(parent);
|
|
|
|
{
|
|
auto const result{SubModel->Load(parser, dynamic)};
|
|
m_indexcount += result.first;
|
|
m_vertexcount += result.second;
|
|
}
|
|
|
|
if (SubModel->Parent == nullptr && parent != "none")
|
|
ErrorLog("Bad model: parent for sub-model \"" + SubModel->pName + "\" doesn't exist or is located later in the model data", logtype::model);
|
|
AddTo(SubModel->Parent, SubModel);
|
|
|
|
parser.getTokens();
|
|
parser >> token;
|
|
}
|
|
// Ra: od wersji 334 przechylany jest cały model, a nie tylko pierwszy submodel
|
|
// ale bujanie kabiny nadal używa bananów :( od 393 przywrócone, ale z dodatkowym warunkiem
|
|
if (Global.iConvertModels & 4)
|
|
{ // automatyczne banany czasem psuły przechylanie kabin...
|
|
if (dynamic && Root)
|
|
{
|
|
if (Root->NextGet()) // jeśli ma jakiekolwiek kolejne
|
|
{ // dynamic musi mieć "banana", bo tylko pierwszy obiekt jest animowany, a następne nie
|
|
SubModel = new TSubModel(); // utworzenie pustego
|
|
SubModel->ChildAdd(Root);
|
|
Root = SubModel;
|
|
++iSubModelsCount;
|
|
}
|
|
Root->WillBeAnimated(); // bo z tym jest dużo problemów
|
|
}
|
|
}
|
|
}
|
|
|
|
std::uint32_t TSubModel::VariantId(TSubModel const *Submodel)
|
|
{ // the seed is already well scattered, so the group is merely added to it. this way branches of one model
|
|
// are guaranteed to land on different variants, as long as the pool holds at least as many entries as
|
|
// the model has branches -- two containers carried by the same wagon won't come up with the same number
|
|
return iVariantSeed + static_cast<std::uint32_t>(Submodel != nullptr ? Submodel->m_variantgroup : 0);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
// two branches sitting no farther than this from each other are treated as one object.
|
|
// generous, a simplified lod mesh doesn't have to sit exactly where the detailed one does
|
|
float const variantgrouptolerance{0.25f};
|
|
|
|
// assigns provided variant group id to the submodel and all of its children
|
|
void mark_variant_group(TSubModel *Submodel, int const Group, bool const Includesiblings)
|
|
{
|
|
while (Submodel != nullptr)
|
|
{
|
|
Submodel->m_variantgroup = Group;
|
|
mark_variant_group(Submodel->ChildGet(), Group, true);
|
|
if (false == Includesiblings)
|
|
{
|
|
break;
|
|
}
|
|
Submodel = Submodel->NextGet();
|
|
}
|
|
}
|
|
|
|
// expands provided bounding area to cover geometry carried by the submodel and all of its children
|
|
void accumulate_mesh_bounds(TSubModel const *Submodel, glm::vec3 &Minimum, glm::vec3 &Maximum, bool &Isempty, bool const Includesiblings)
|
|
{
|
|
while (Submodel != nullptr)
|
|
{
|
|
if (auto const &vertices{(Submodel->m_geometry.handle != null_handle ? GfxRenderer->Vertices(Submodel->m_geometry.handle) : Submodel->Vertices)};
|
|
false == vertices.empty())
|
|
{
|
|
// the transformation chain can still hold a rotation, so the vertices go through it
|
|
// rather than get taken at their face value
|
|
float4x4 parentmatrix;
|
|
Submodel->ParentMatrix(&parentmatrix);
|
|
auto const transformationmatrix{glm::make_mat4(parentmatrix.readArray())};
|
|
for (auto const &vertex : vertices)
|
|
{
|
|
auto const position{glm::vec3{transformationmatrix * glm::vec4{vertex.position, 1}}};
|
|
Minimum = glm::min(Minimum, position);
|
|
Maximum = glm::max(Maximum, position);
|
|
}
|
|
Isempty = false;
|
|
}
|
|
accumulate_mesh_bounds(Submodel->Child, Minimum, Maximum, Isempty, true);
|
|
if (false == Includesiblings)
|
|
{
|
|
break;
|
|
}
|
|
Submodel = Submodel->Next;
|
|
}
|
|
}
|
|
|
|
// returns position of provided branch of the model, relative to the model root
|
|
glm::vec3 branch_position(TSubModel const *Branch)
|
|
{
|
|
// the transformation chain is the cheap way to get there, as it doesn't touch the geometry,
|
|
// which keeps this affordable even for models made of thousands of branches...
|
|
auto const offset{Branch->offset()};
|
|
if (glm::length2(offset) > variantgrouptolerance * variantgrouptolerance)
|
|
{
|
|
return offset;
|
|
}
|
|
// ...but it only tells us anything for branches which actually carry a transform. models loaded from
|
|
// text format can have theirs baked into the vertices instead, which leaves the whole model piled up
|
|
// at the origin and, without the fallback below, all of its branches in a single variant group.
|
|
// NOTE: it's the centre of the bounding area we're after, and not the average vertex position offset()
|
|
// would give us -- the latter drifts with vertex density, which puts each lod mesh of one object in
|
|
// a group of its own and has the object change its texture variant as the lods get swapped
|
|
glm::vec3 minimum{std::numeric_limits<float>::max()};
|
|
glm::vec3 maximum{std::numeric_limits<float>::lowest()};
|
|
bool isempty{true};
|
|
accumulate_mesh_bounds(Branch, minimum, maximum, isempty, false);
|
|
return (false == isempty ? (minimum + maximum) * 0.5f : offset);
|
|
}
|
|
} // namespace
|
|
|
|
void TModel3d::assign_variant_groups()
|
|
{ // each top level branch of the model gets a group id, which it passes down to all of its children.
|
|
// for load models a branch is a single load chunk, and the group id is what makes two chunks of the same
|
|
// model pick different texture variants, while keeping all parts of one chunk on the same variant.
|
|
// the id comes from the position of the branch, not from its ordinal: lod variants of one object are
|
|
// separate branches drawn in disjoint distance ranges, and grouping them by position is what keeps the
|
|
// object on the same variant no matter which of them is currently on screen
|
|
std::vector<glm::vec3> grouppositions; // position of each group, indexed by its id
|
|
auto const groupid = [&grouppositions](TSubModel const *Branch) {
|
|
auto const position{branch_position(Branch)};
|
|
for (std::size_t index = 0; index < grouppositions.size(); ++index)
|
|
{
|
|
if (glm::length2(grouppositions[index] - position) < variantgrouptolerance * variantgrouptolerance)
|
|
{
|
|
return static_cast<int>(index);
|
|
}
|
|
}
|
|
grouppositions.emplace_back(position);
|
|
return static_cast<int>(grouppositions.size() - 1);
|
|
};
|
|
|
|
for (auto *trunk = Root; trunk != nullptr; trunk = trunk->NextGet())
|
|
{
|
|
// root level submodels are usually mere containers, but can carry geometry of their own
|
|
trunk->m_variantgroup = groupid(trunk);
|
|
for (auto *branch = trunk->ChildGet(); branch != nullptr; branch = branch->NextGet())
|
|
{
|
|
mark_variant_group(branch, groupid(branch), false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TModel3d::Init()
|
|
{ // obrócenie początkowe układu współrzędnych, dla
|
|
// pojazdów wykonywane po analizie animacji
|
|
if (iFlags & 0x8000)
|
|
return; // operacje zostały już wykonane
|
|
if (Root)
|
|
{
|
|
if (iFlags & 0x0200)
|
|
{
|
|
// jeśli wczytano z pliku tekstowego jest jakiś dziwny błąd,
|
|
// że obkręcany ma być tylko ostatni submodel głównego łańcucha
|
|
// argumet określa, czy wykonać pierwotny obrót
|
|
Root->InitialRotate(true);
|
|
}
|
|
iFlags |= Root->FlagsCheck() | 0x8000; // flagi całego modelu
|
|
if (m_vertexcount)
|
|
{
|
|
if (m_geometrybank == null_handle)
|
|
{
|
|
m_geometrybank = GfxRenderer->Create_Bank();
|
|
}
|
|
std::size_t indexoffset = 0;
|
|
std::size_t vertexoffset = 0;
|
|
Root->create_geometry(indexoffset, vertexoffset, m_geometrybank);
|
|
}
|
|
// determine final bounding radius from the root-level siblings
|
|
auto const *root{Root};
|
|
while ((root = root->Next) != nullptr)
|
|
{
|
|
Root->m_boundingradius = std::max(Root->m_boundingradius, root->m_boundingradius);
|
|
}
|
|
|
|
assign_variant_groups();
|
|
|
|
if (Global.iConvertModels & 1 && false == asBinary.empty())
|
|
{
|
|
SaveToBinFile(asBinary);
|
|
asBinary = ""; // zablokowanie powtórnego zapisu
|
|
}
|
|
}
|
|
// check if the model contains particle emitters
|
|
find_smoke_sources();
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// 2012-02 funkcje do tworzenia terenu z E3D
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int TModel3d::TerrainCount() const
|
|
{ // zliczanie kwadratów kilometrowych (główna
|
|
// linia po Next) do tworznia tablicy
|
|
int i = 0;
|
|
TSubModel *r = Root;
|
|
while (r)
|
|
{
|
|
r = r->NextGet();
|
|
++i;
|
|
}
|
|
return i;
|
|
};
|
|
TSubModel *TModel3d::TerrainSquare(int n)
|
|
{ // pobieranie wskaźnika do submodelu (n)
|
|
int i = 0;
|
|
TSubModel *r = Root;
|
|
while (i < n)
|
|
{
|
|
r = r->NextGet();
|
|
++i;
|
|
}
|
|
r->UnFlagNext(); // blokowanie wyświetlania po Next głównej listy
|
|
return r;
|
|
};
|