This commit is contained in:
2026-03-05 01:42:40 +03:00
parent 2815369bb8
commit 4290e99c61
31 changed files with 2285 additions and 99 deletions

View File

@@ -12,6 +12,9 @@
#include "render/shadersystem.h"
#include "render/render.h"
#include "render/debugrender.h"
#include "render/gpu_buffer.h"
#include "engine/camera.h"
#include "engine/physics/physicsworld.h"
#include <pugixml.hpp>
@@ -54,6 +57,37 @@ static std::string getFilenameWithoutPathAndExtension(const std::string& filenam
return string;
}
template <typename T>
void TReadFile(FileHandle_t handle, T* value)
{
GetFileSystem()->ReadFile(handle, (void*)value, sizeof(T));
}
template <typename T>
void TWriteFile(FileHandle_t handle, const T* value)
{
GetFileSystem()->WriteFile(handle, (void*)value, sizeof(T));
}
static void WriteString(FileHandle_t handle, const std::string& string)
{
uint16_t size = string.length();
TWriteFile(handle, &size);
GetFileSystem()->WriteFile(handle, (void*)string.c_str(), size);
}
static void ReadString(FileHandle_t handle, std::string& string)
{
uint16_t size;
TReadFile(handle, &size);
//string.resize(size + 1);
string.resize(size);
GetFileSystem()->ReadFile(handle, (void*)string.c_str(), size);
//string[size] = '\0';
}
//#ifdef NDEBUG
//#pragma comment(lib, "assimp-vc141-mt.lib")
//#else
@@ -329,7 +363,13 @@ void SceneManager::loadStaticMesh(const char* filename)
if (GetFileSystem()->IsExist(filenamebuf)) {
SceneStaticMesh* staticMesh = new SceneStaticMesh();
staticMesh->LoadObj(filenamebuf);
const char* ext = strrchr(filenamebuf, '.');
if (ext && strcmp(ext, ".obj") == 0)
staticMesh->LoadObj(filenamebuf);
else if (ext && strcmp(ext, ".wmb") == 0)
staticMesh->LoadBin(filenamebuf);
m_sceneMeshes.push_back(staticMesh);
}
}
@@ -593,6 +633,42 @@ void SceneStaticMesh::LoadObj(const char* filename)
}
}
// Fill face indices
std::vector<uint32_t> indices;
for (uint32_t i = 0; i < vertices.size(); i++) {
indices.push_back(i);
}
std::string binaryCache = getFileNameWithoutExtension(filename);
binaryCache += ".wmb";
if (!GetFileSystem()->IsExist(binaryCache.c_str()))
{
FileHandle_t handle = GetFileSystem()->OpenFile(binaryCache.c_str(), "wb");
// write header
WriteString(handle, "StaticSceneMeshFile");
WriteString(handle, "0.1");
// write material file
std::string mtlfilename = getFileNameWithoutExtension(filename);
mtlfilename += ".mtl";
WriteString(handle, mtlfilename);
// write vertices
uint32_t vbcount = vertices.size();
TWriteFile(handle, &vbcount);
GetFileSystem()->WriteFile(handle, vertices.data(), vertices.size() * sizeof(StaticMeshVertex));
// write indices
uint32_t ibcount = indices.size();
TWriteFile(handle, &ibcount);
GetFileSystem()->WriteFile(handle, indices.data(), indices.size() * sizeof(uint32_t));
GetFileSystem()->CloseFile(handle);
}
// m_Vertices = vertices;
m_vb = g_renderDevice->CreateVertexBuffer(vertices.data(), (int)sizeof(StaticMeshVertex) * (int)vertices.size());
@@ -601,6 +677,75 @@ void SceneStaticMesh::LoadObj(const char* filename)
std::string mtlfilename = getFileNameWithoutExtension(filename);
mtlfilename += ".mtl";
LoadMtl(mtlfilename.c_str());
if (g_PhysicsWorld) {
g_PhysicsWorld->AddCollisionModel(vertices.data(), vertices.size(), indices.data(), indices.size());
}
}
void SceneStaticMesh::LoadBin(const char* filename)
{
FileHandle_t handle = GetFileSystem()->OpenFile(filename, "rb");
std::string header;
ReadString(handle, header);
if (header != "StaticSceneMeshFile")
Core::Error(" SceneStaticMesh::LoadBin: Error during scene loading!\n%s is not a proper scene mesh.\n%s (should be %s)", filename, header.c_str(), "StaticSceneMeshFile");
std::string version;
ReadString(handle, version);
if (version != "0.1")
Core::Error(" SceneStaticMesh::LoadBin: Error during scene loading!\n%s is outdated.\nversion %s (current is %s)", filename, version.c_str(), "0.1");
std::string mtlfilename;
ReadString(handle, mtlfilename);
LoadMtl(mtlfilename.c_str());
// read vertices
uint32_t vbcount;
TReadFile(handle, &vbcount);
std::vector<StaticMeshVertex> vertices;
vertices.resize(vbcount);
GetFileSystem()->ReadFile(handle, vertices.data(), vertices.size() * sizeof(StaticMeshVertex));
// read indices
uint32_t ibcount;
TReadFile(handle, &ibcount);
std::vector<unsigned int> vertexIndices;
vertexIndices.resize(ibcount);
GetFileSystem()->ReadFile(handle, vertexIndices.data(), vertexIndices.size() * sizeof(unsigned int));
// bbox calculation
for (unsigned int i = 0; i < vertices.size(); i++)
{
if (i == 0)
{
m_boundingBox.m_min = vertices[i].position;
m_boundingBox.m_max = vertices[i].position;
}
else
{
m_boundingBox.m_min = glm::min(m_boundingBox.m_min, vertices[i].position);
m_boundingBox.m_max = glm::max(m_boundingBox.m_max, vertices[i].position);
}
}
// create vb
m_vb = g_renderDevice->CreateVertexBuffer(vertices.data(), (int)sizeof(StaticMeshVertex) * (int)vertices.size());
m_vbcount = vertices.size();
// create ib ??
if (g_PhysicsWorld) {
g_PhysicsWorld->AddCollisionModel(vertices.data(), vertices.size(), vertexIndices.data(), vertexIndices.size());
}
GetFileSystem()->CloseFile(handle);
}
void SceneStaticMesh::LoadMtl(const char* filename)
@@ -643,12 +788,57 @@ void SceneStaticMesh::LoadMtl(const char* filename)
fclose(file);
}
void SceneStaticMesh::RenderObjects()
static glm::mat4 s_identity = glm::mat4(1.0f);
void R_SceneStaticMesh_BindShader(const glm::mat4& worldMatrix, Texture2D* albedoTexture)
{
extern Shader* g_unlitShader;
extern Shader* g_litShader;
SDL_assert(g_unlitShader);
SDL_assert(g_litShader);
Shader* shader = g_litShader;
g_shaderSystem->SetShader(shader);
g_shaderSystem->SetUniformMatrix(shader, UNIFORM_MODEL_MATRIX, &worldMatrix[0]);
glm::mat4 mvp = g_render->GetProjectionMatrix() * g_render->GetViewMatrix() * worldMatrix;
g_shaderSystem->SetUniformMatrix(shader, UNIFORM_MVP_MATRIX, &mvp[0]);
Camera* camera = g_cameraManager.GetActiveCamera();
if (camera && shader->HasUniform(UNIFORM_CAMERA_POS))
{
glm::vec4 campos = glm::vec4(camera->GetPosition(), 1.0f);
g_shaderSystem->SetUniformFloat4(shader, UNIFORM_CAMERA_POS, &campos);
}
if (shader->HasUniform(UNIFORM_SUN_DIRECTION))
{
glm::vec4 lightPos = glm::vec4(1.0f, 1.0f, 1.0f, 0.0f);
g_debugRender->DrawAxis(glm::vec3(lightPos));
Camera* camera = g_cameraManager.GetActiveCamera();
if (camera)
lightPos = glm::vec4(camera->GetPosition(), 1.0f);
g_shaderSystem->SetUniformFloat4(shader, UNIFORM_SUN_DIRECTION, &lightPos);
}
if (shader->HasUniform(UNIFORM_SUN_AMBIENT))
{
glm::vec4 lightColor = glm::vec4(0.1f);
g_shaderSystem->SetUniformFloat4(shader, UNIFORM_SUN_AMBIENT, &lightColor);
}
g_texturesManager->SetTexture(0, albedoTexture);
g_shaderSystem->SetUniformSampler(shader, SAMPLER_ALBEDO, 0);
}
void SceneStaticMesh::RenderObjects()
{
glFrontFace(GL_CCW);
glDepthFunc(GL_LESS);
@@ -660,17 +850,7 @@ void SceneStaticMesh::RenderObjects()
g_renderDevice->SetVerticesBuffer(m_vb);
g_shaderSystem->SetShader(g_litShader);
static glm::mat4 s_identity = glm::mat4(1.0f);
g_shaderSystem->SetUniformMatrix(g_litShader, UNIFORM_MODEL_MATRIX, &s_identity[0]);
glm::mat4 mvp = glm::identity<glm::mat4>();
mvp = g_render->GetProjectionMatrix() * g_render->GetViewMatrix();
g_shaderSystem->SetUniformMatrix(g_litShader, UNIFORM_MVP_MATRIX, &mvp[0]);
g_texturesManager->SetTexture(0, m_albedoTexture);
g_shaderSystem->SetUniformSampler(g_litShader, SAMPLER_ALBEDO, 0);
R_SceneStaticMesh_BindShader(s_identity, m_albedoTexture);
g_renderDevice->DrawArrays(PT_TRIANGLES, 0, m_vbcount);
}