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="">
<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/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>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -179,7 +179,11 @@
<workItem from="1746538260893" duration="5781000" />
<workItem from="1746546014271" duration="1814000" />
<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 id="LOCAL-00001" summary="Minor update - viewport window tweak">
<option name="closed" value="true" />

View File

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

View File

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