Minor Update - Loading Obj Model is now faster than f1 - V10.4.0

This commit is contained in:
CatChow0 2025-05-12 14:18:43 +02:00
parent 32b71ac97f
commit c5bfa2e621
3 changed files with 110 additions and 90 deletions

View File

@ -7,7 +7,7 @@
<list default="true" id="e81d6e08-efc7-40a0-909d-ec4943d948e9" name="Changes" comment=""> <list default="true" id="e81d6e08-efc7-40a0-909d-ec4943d948e9" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/enginecustom/imgui.ini" beforeDir="false" afterPath="$PROJECT_DIR$/enginecustom/imgui.ini" afterDir="false" /> <change beforePath="$PROJECT_DIR$/enginecustom/imgui.ini" beforeDir="false" afterPath="$PROJECT_DIR$/enginecustom/imgui.ini" afterDir="false" />
<change beforePath="$PROJECT_DIR$/enginecustom/src/src/system/imguiManager.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/enginecustom/src/src/system/imguiManager.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/enginecustom/src/src/system/modelclass.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/enginecustom/src/src/system/modelclass.cpp" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -179,7 +179,11 @@
<workItem from="1746538260893" duration="5781000" /> <workItem from="1746538260893" duration="5781000" />
<workItem from="1746546014271" duration="1814000" /> <workItem from="1746546014271" duration="1814000" />
<workItem from="1746619988287" duration="12282000" /> <workItem from="1746619988287" duration="12282000" />
<workItem from="1746711806997" duration="6619000" /> <workItem from="1746711806997" duration="6736000" />
<workItem from="1746719840054" duration="991000" />
<workItem from="1746795732153" duration="33000" />
<workItem from="1746795828588" duration="592000" />
<workItem from="1747049871967" duration="2341000" />
</task> </task>
<task id="LOCAL-00001" summary="Minor update - viewport window tweak"> <task id="LOCAL-00001" summary="Minor update - viewport window tweak">
<option name="closed" value="true" /> <option name="closed" value="true" />

View File

@ -4,20 +4,20 @@ Size=400,400
Collapsed=0 Collapsed=0
[Window][Khaotic Engine] [Window][Khaotic Engine]
Pos=1745,19 Pos=1281,19
Size=303,1110 Size=303,842
Collapsed=0 Collapsed=0
DockId=0x00000005,0 DockId=0x00000005,0
[Window][Objects] [Window][Objects]
Pos=0,19 Pos=0,19
Size=281,307 Size=281,615
Collapsed=0 Collapsed=0
DockId=0x00000007,0 DockId=0x00000007,0
[Window][Terrain] [Window][Terrain]
Pos=0,19 Pos=0,328
Size=281,883 Size=281,306
Collapsed=0 Collapsed=0
DockId=0x00000008,0 DockId=0x00000008,0
@ -35,7 +35,7 @@ DockId=0x00000001,2
[Window][Engine Settings] [Window][Engine Settings]
Pos=0,19 Pos=0,19
Size=281,307 Size=281,615
Collapsed=0 Collapsed=0
DockId=0x00000007,1 DockId=0x00000007,1
@ -51,7 +51,7 @@ DockId=0x00000007,0
[Window][DockSpace] [Window][DockSpace]
Pos=0,0 Pos=0,0
Size=2048,1129 Size=1584,861
Collapsed=0 Collapsed=0
[Window][Add Object] [Window][Add Object]
@ -66,14 +66,14 @@ Collapsed=0
DockId=0x0000000C,0 DockId=0x0000000C,0
[Window][Log Window] [Window][Log Window]
Pos=652,636 Pos=641,636
Size=649,225 Size=638,225
Collapsed=0 Collapsed=0
DockId=0x00000006,0 DockId=0x00000006,0
[Window][Render Stats] [Window][Render Stats]
Pos=0,904 Pos=0,636
Size=1743,225 Size=639,225
Collapsed=0 Collapsed=0
DockId=0x00000004,0 DockId=0x00000004,0
@ -81,7 +81,7 @@ DockId=0x00000004,0
DockSpace ID=0xC0DFADC4 Pos=8,27 Size=1568,826 Split=X DockSpace ID=0xC0DFADC4 Pos=8,27 Size=1568,826 Split=X
DockNode ID=0x00000001 Parent=0xC0DFADC4 SizeRef=330,1094 Selected=0x393905AB DockNode ID=0x00000001 Parent=0xC0DFADC4 SizeRef=330,1094 Selected=0x393905AB
DockNode ID=0x00000003 Parent=0xC0DFADC4 SizeRef=1700,1094 CentralNode=1 DockNode ID=0x00000003 Parent=0xC0DFADC4 SizeRef=1700,1094 CentralNode=1
DockSpace ID=0xCCBD8CF7 Window=0x3DA2F1DE Pos=0,19 Size=2048,1110 Split=X DockSpace ID=0xCCBD8CF7 Window=0x3DA2F1DE Pos=0,19 Size=1584,842 Split=X
DockNode ID=0x00000002 Parent=0xCCBD8CF7 SizeRef=1743,826 Split=Y DockNode ID=0x00000002 Parent=0xCCBD8CF7 SizeRef=1743,826 Split=Y
DockNode ID=0x0000000A Parent=0x00000002 SizeRef=1568,599 Split=X DockNode ID=0x0000000A Parent=0x00000002 SizeRef=1568,599 Split=X
DockNode ID=0x00000009 Parent=0x0000000A SizeRef=281,974 Split=Y Selected=0x031DC75C DockNode ID=0x00000009 Parent=0x0000000A SizeRef=281,974 Split=Y Selected=0x031DC75C

View File

@ -240,65 +240,88 @@ bool ModelClass::LoadModel(char* filename)
bool ModelClass::LoadObjModel(char* filename) bool ModelClass::LoadObjModel(char* filename)
{ {
// Lecture optimisée du fichier en mode binaire
std::string line; std::ifstream fin(filename, std::ios::in | std::ios::binary);
std::ifstream fin(filename);
// If it could not open the file then exit.
if (!fin) if (!fin)
{ {
Logger::Get().Log("Failed to open model file", __FILE__, __LINE__, Logger::LogLevel::Error); Logger::Get().Log("Échec d'ouverture du fichier modèle", __FILE__, __LINE__, Logger::LogLevel::Error);
return false; return false;
} }
// Lecture du fichier entier d'un coup (évite la lecture ligne par ligne)
fin.seekg(0, std::ios::end);
const size_t fileSize = fin.tellg();
fin.seekg(0, std::ios::beg);
std::string fileContent;
fileContent.resize(fileSize);
fin.read(&fileContent[0], fileSize);
fin.close();
// Pré-allocation des vecteurs (évite les réallocations)
const size_t estimatedVertices = fileSize / 150;
std::vector<XMFLOAT3> temp_positions; std::vector<XMFLOAT3> temp_positions;
std::vector<XMFLOAT2> temp_texcoords; std::vector<XMFLOAT2> temp_texcoords;
std::vector<XMFLOAT3> temp_normals; std::vector<XMFLOAT3> temp_normals;
std::vector<ModelType> temp_model; std::vector<ModelType> temp_model;
// Read the file line by line. temp_positions.reserve(estimatedVertices);
while (std::getline(fin, line)) temp_texcoords.reserve(estimatedVertices);
{ temp_normals.reserve(estimatedVertices);
std::istringstream iss(line); temp_model.reserve(estimatedVertices * 3);
std::string type;
iss >> type;
if (type == "v") // Vertex position // Analyse du contenu
std::istringstream iss(fileContent);
std::string line;
while (std::getline(iss, line))
{
if (line.empty() || line[0] == '#') continue;
// Analyse plus rapide basée sur le premier caractère
if (line[0] == 'v')
{
if (line[1] == ' ') // Position de sommet
{ {
XMFLOAT3 pos; XMFLOAT3 pos;
iss >> pos.x >> pos.y >> pos.z; sscanf_s(line.c_str() + 2, "%f %f %f", &pos.x, &pos.y, &pos.z);
temp_positions.push_back(pos); temp_positions.push_back(pos);
} }
else if (type == "vt") // Texture coordinate else if (line[1] == 't') // Coordonnées de texture
{ {
XMFLOAT2 tex; XMFLOAT2 tex;
iss >> tex.x >> tex.y; sscanf_s(line.c_str() + 3, "%f %f", &tex.x, &tex.y);
temp_texcoords.push_back(tex); temp_texcoords.push_back(tex);
} }
else if (type == "vn") // Vertex normal else if (line[1] == 'n') // Normales
{ {
XMFLOAT3 norm; XMFLOAT3 norm;
iss >> norm.x >> norm.y >> norm.z; sscanf_s(line.c_str() + 3, "%f %f %f", &norm.x, &norm.y, &norm.z);
temp_normals.push_back(norm); temp_normals.push_back(norm);
} }
else if (type == "f") // Face }
else if (line[0] == 'f')
{ {
int posIndex[3], texIndex[3], normIndex[3]; int posIndex[3], texIndex[3], normIndex[3];
char slash; // To skip the slashes '/' in the .obj file format const char* linePtr = line.c_str() + 2; // Sauter "f "
for (int i = 0; i < 3; i++) // Each face in .obj format has 3 vertices
{ for (int i = 0; i < 3; i++)
iss >> posIndex[i] >> slash >> texIndex[i] >> slash >> normIndex[i]; {
// Analyse rapide du format v/vt/vn
sscanf_s(linePtr, "%d/%d/%d", &posIndex[i], &texIndex[i], &normIndex[i]);
// Avancer au prochain ensemble d'indices
while (*linePtr && *linePtr != ' ') linePtr++;
while (*linePtr == ' ') linePtr++;
// Handle negative indices
if (posIndex[i] < 0) posIndex[i] += temp_positions.size() + 1; if (posIndex[i] < 0) posIndex[i] += temp_positions.size() + 1;
if (texIndex[i] < 0) texIndex[i] += temp_texcoords.size() + 1; if (texIndex[i] < 0) texIndex[i] += temp_texcoords.size() + 1;
if (normIndex[i] < 0) normIndex[i] += temp_normals.size() + 1; if (normIndex[i] < 0) normIndex[i] += temp_normals.size() + 1;
} }
// .obj indices start from 1, not 0
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
ModelType vertex; ModelType vertex{};
vertex.x = temp_positions[posIndex[i] - 1].x; vertex.x = temp_positions[posIndex[i] - 1].x;
vertex.y = temp_positions[posIndex[i] - 1].y; vertex.y = temp_positions[posIndex[i] - 1].y;
vertex.z = temp_positions[posIndex[i] - 1].z; vertex.z = temp_positions[posIndex[i] - 1].z;
@ -312,22 +335,15 @@ bool ModelClass::LoadObjModel(char* filename)
} }
} }
// Allocation et copie efficace du modèle final
m_vertexCount = temp_model.size(); m_vertexCount = temp_model.size();
m_indexCount = temp_model.size(); m_indexCount = temp_model.size();
// Create the model using the vertex count that was read in.
m_model = new ModelType[m_vertexCount]; m_model = new ModelType[m_vertexCount];
for (int i = 0; i < m_vertexCount; i++) std::memcpy(m_model, temp_model.data(), m_vertexCount * sizeof(ModelType));
{
m_model[i] = temp_model[i];
}
fin.close();
return true; return true;
} }
bool ModelClass::LoadTxtModel(char* filename) bool ModelClass::LoadTxtModel(char* filename)
{ {
ifstream fin; ifstream fin;