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,7 +12,7 @@
#include "modelsystem.h"
#include "texturesmanager.h"
extern Shader* g_litShader;
extern Shader* g_unlitShader;
static std::string getFileNameWithoutExtension(const std::string& filename)
{
@@ -220,7 +220,7 @@ void Model::LoadMtl(const char* filename)
void Model::Draw(const glm::mat4& model, bool isTransparent /*= false*/)
{
SDL_assert(g_litShader);
SDL_assert(g_unlitShader);
glFrontFace(GL_CCW);
glDepthFunc(GL_LESS);
@@ -236,20 +236,20 @@ void Model::Draw(const glm::mat4& model, bool isTransparent /*= false*/)
g_renderDevice->SetBlendingFunction(BF_SRC_ALPHA, BF_ONE_MINUS_SRC_ALPHA);
glm::vec4 color = glm::vec4(1.f, 1.f, 1.f, .5f);
g_shaderSystem->SetUniformFloat4(g_litShader, UNIFORM_CUSTOM_COLOR, glm::value_ptr(color));
g_shaderSystem->SetUniformFloat4(g_unlitShader, UNIFORM_CUSTOM_COLOR, glm::value_ptr(color));
}
else
{
g_renderDevice->SetBlending(false);
//glm::vec4 color = glm::vec4(1.f, 1.f, 1.f, 1.f);
//g_shaderSystem->SetUniformFloat4(g_litShader, UNIFORM_CUSTOM_COLOR, glm::value_ptr(color));
//g_shaderSystem->SetUniformFloat4(g_unlitShader, UNIFORM_CUSTOM_COLOR, glm::value_ptr(color));
}
g_renderDevice->SetVerticesBuffer(m_data.vb);
g_shaderSystem->SetShader(g_litShader);
g_shaderSystem->SetUniformMatrix(g_litShader, UNIFORM_MODEL_MATRIX, &model[0]);
g_shaderSystem->SetShader(g_unlitShader);
g_shaderSystem->SetUniformMatrix(g_unlitShader, UNIFORM_MODEL_MATRIX, &model[0]);
//static float test = 0.0f;
//test += g_systemTimer->GetDelta() * 6.0f;
@@ -277,10 +277,10 @@ void Model::Draw(const glm::mat4& model, bool isTransparent /*= false*/)
glm::mat4 mvp = glm::identity<glm::mat4>();
mvp = g_render->GetProjectionMatrix() * g_render->GetViewMatrix() * model;
g_shaderSystem->SetUniformMatrix(g_litShader, UNIFORM_MVP_MATRIX, &mvp[0]);
g_shaderSystem->SetUniformMatrix(g_unlitShader, UNIFORM_MVP_MATRIX, &mvp[0]);
g_texturesManager->SetTexture(0, m_AlbedoTexture);
g_shaderSystem->SetUniformSampler(g_litShader, SAMPLER_ALBEDO, 0);
g_shaderSystem->SetUniformSampler(g_unlitShader, SAMPLER_ALBEDO, 0);
g_renderDevice->DrawArrays(PT_TRIANGLES, 0, m_data.vbcount);

View File

@@ -26,6 +26,7 @@ static InputLayoutDesc_t g_skinnedVertexLayout[] = {
{ VERTEXATTR_VEC2, SHADERSEMANTIC_TEXCOORD }
};
Shader* g_unlitShader = nullptr;
Shader* g_litShader = nullptr;
static std::string getFileNameWithoutExtension(const std::string& filename)
@@ -48,7 +49,11 @@ ModelSystem::~ModelSystem()
void ModelSystem::Init()
{
// Load model generic shader
// Load unlighted model generic shader
g_unlitShader = g_shaderSystem->CreateShader("unlit_generic", "data/shaders/unlit_generic.vs", "data/shaders/unlit_generic.ps",
g_staticVertexLayout, sizeof(g_staticVertexLayout) / sizeof(g_staticVertexLayout[0]));
// Load lighted model generic shader
g_litShader = g_shaderSystem->CreateShader("lit_generic", "data/shaders/lit_generic.vs", "data/shaders/lit_generic.ps",
g_staticVertexLayout, sizeof(g_staticVertexLayout) / sizeof(g_staticVertexLayout[0]));
}

View File

@@ -206,6 +206,7 @@ enum ShaderUniform_t
UNIFORM_SUN_DIRECTION,
UNIFORM_SUN_COLOR,
UNIFORM_SUN_AMBIENT,
UNIFORM_CAMERA_POS,
UNIFORM_MAX,
};

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);
}

View File

@@ -15,6 +15,7 @@ public:
~SceneStaticMesh();
void LoadObj(const char* filename);
void LoadBin(const char* filename);
void LoadMtl(const char* filename);
const BoundingBox& GetBoundingBox() { return m_boundingBox; }

View File

@@ -12,6 +12,7 @@ static const char* g_uniformNameTable[UNIFORM_MAX] =
"u_sunDirection",
"u_sunColor",
"u_sunAmbientColor",
"u_cameraPos",
};
static const char* g_samplersNameTable[SAMPLER_MAX] =