mesh loading

This commit is contained in:
Asuro
2025-02-10 01:49:03 +01:00
parent 90bc5753a9
commit 16fcfa8af9
17 changed files with 203 additions and 63 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
# assets
*.blend1

3
.gitmodules vendored
View File

@@ -4,3 +4,6 @@
[submodule "src/dependency/SDL"]
path = src/dependency/SDL
url = https://github.com/libsdl-org/SDL
[submodule "src/dependency/tinygltf"]
path = src/dependency/tinygltf
url = https://github.com/syoyo/tinygltf

BIN
assets/blender/cube.blend Normal file

Binary file not shown.

View File

@@ -24,7 +24,8 @@ set_property(TARGET PuzGameEngine PROPERTY CXX_STANDARD 17)
# Game
file(GLOB_RECURSE sources_game game/*.cpp game/*.h)
add_library(PuzGame SHARED ${sources_game})
file(GLOB source_singleheader dependency/tinygltf/stb_image.h dependency/tinygltf/stb_image_write.h dependency/tinygltf/json.hpp dependency/tinygltf/tiny_gltf.h)
add_library(PuzGame SHARED ${sources_game} ${source_singleheader})
set_property(TARGET PuzGame PROPERTY CXX_STANDARD 17)
# SDL
@@ -35,6 +36,7 @@ SET(BGFX_BUILD_TOOLS ON)
SET(BGFX_BUILD_EXAMPLES OFF)
add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake")
# Link
target_link_libraries(PuzGame bx bimg bgfx)
target_link_libraries(PuzGameEngine bx SDL3::SDL3)
set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2")

View File

@@ -18,8 +18,6 @@
//#define VISUAL_STUDIO
constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1;
#ifdef VISUAL_STUDIO
constexpr const char* DLLPath = "PuzGame.dll";
constexpr const wchar_t* DLLWatch = L"PuzGame2.dll";
@@ -250,7 +248,7 @@ int main()
bx::strCopy(DevData.FileWatcher.DLLWatcher.DirPath, sizeof(DevData.FileWatcher.DLLWatcher.DirPath), "cmake-build");
bx::strCopy(DevData.FileWatcher.ShaderWatcher.DirPath, sizeof(DevData.FileWatcher.ShaderWatcher.DirPath), "game/shaders");
bx::strCopy(DevData.FileWatcher.CompiledShaderWatcher.DirPath, sizeof(DevData.FileWatcher.CompiledShaderWatcher.DirPath), "game/compiled-shaders");
bx::strCopy(DevData.FileWatcher.CompiledShaderWatcher.DirPath, sizeof(DevData.FileWatcher.CompiledShaderWatcher.DirPath), "game/compiled-shaders/dx11");
wcscpy(DevData.FileWatcher.DLLWatcher.CompName, DLLWatch);

61
src/game/Mesh.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "Mesh.h"
#include "Log.h"
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../dependency/tinygltf/tiny_gltf.h"
namespace Game
{
void LoadMesh(Model& mesh)
{
mesh.VertLayout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
tinygltf::Model model;
tinygltf::TinyGLTF loader;
std::string warn;
std::string err;
bool loadSuccess = loader.LoadASCIIFromFile(&model, &err, &warn, "models/cube.gltf");
if (!warn.empty()) Log("WARN: %s", warn.c_str());
if (!err.empty()) Log("ERR: %s", err.c_str());
if (!loadSuccess) Log("Model load failed!");
tinygltf::Primitive primitive = model.meshes[0].primitives[0];
{
tinygltf::Accessor accessor = model.accessors.at(primitive.indices);
tinygltf::BufferView bufferView = model.bufferViews.at(accessor.bufferView);
tinygltf::Buffer buffer = model.buffers[bufferView.buffer];
const bgfx::Memory* ibMem = bgfx::alloc(bufferView.byteLength);
bx::memCopy(ibMem->data, &buffer.data.at(bufferView.byteOffset), bufferView.byteLength);
mesh.IndexBuffer = bgfx::createIndexBuffer(ibMem);
}
{
tinygltf::Accessor posAccessor = model.accessors.at(primitive.attributes.at("POSITION"));
tinygltf::Accessor uvAccessor = model.accessors.at(primitive.attributes.at("TEXCOORD_0"));
tinygltf::BufferView posBufferView = model.bufferViews[posAccessor.bufferView];
tinygltf::BufferView uvBufferView = model.bufferViews[uvAccessor.bufferView];
int posStride = posAccessor.ByteStride(posBufferView);
int uvStride = uvAccessor.ByteStride(uvBufferView);
tinygltf::Buffer posBuffer = model.buffers[posBufferView.buffer];
tinygltf::Buffer uvBuffer = model.buffers[uvBufferView.buffer];
uint32_t vertexCount = posBufferView.byteLength / posStride;
const bgfx::Memory* vbMem = bgfx::alloc(vertexCount * sizeof(PosColorVertex));
for (uint32_t i = 0; i < vertexCount; ++i)
{
PosColorVertex& v = *reinterpret_cast<PosColorVertex*>(vbMem->data + i * sizeof(PosColorVertex));
bx::memCopy(&v.x, &posBuffer.data.at(posBufferView.byteOffset + i * posStride), posStride);
v.abgr = 0;
bx::memCopy(&v.uv_x, &uvBuffer.data.at(uvBufferView.byteOffset + i * uvStride), uvStride);
}
mesh.VertexBuffer = bgfx::createVertexBuffer(vbMem, mesh.VertLayout);
}
}
}

8
src/game/Mesh.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
#include "rendering/Rendering.h"
namespace Game
{
void LoadMesh(Model& mesh);
}

View File

@@ -3,6 +3,7 @@
#include "../../engine/Shared.h"
#include "../Global.h"
#include "../Instance.h"
#include "../Mesh.h"
#include "bgfx/defines.h"
#include "bx/timer.h"
#include <bx/file.h>
@@ -13,44 +14,6 @@ using namespace std::chrono_literals;
namespace Game
{
struct PosColorVertex
{
float x;
float y;
float z;
uint32_t abgr;
float uv_x;
float uv_y;
};
static PosColorVertex cubeVertices[] =
{
{-1.0f, 1.0f, 1.0f, 0xff000000, 0.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff, 0.0f, 0.0f },
{-1.0f, -1.0f, 1.0f, 0xff00ff00, 0.0f, 0.0f },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff, 0.0f, 0.0f },
{-1.0f, 1.0f, -1.0f, 0xffff0000, 0.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff, 0.0f, 0.0f },
{-1.0f, -1.0f, -1.0f, 0xffffff00, 0.0f, 0.0f },
{ 1.0f, -1.0f, -1.0f, 0xffffffff, 0.0f, 0.0f },
};
static const uint16_t cubeTriList[] =
{
0, 1, 2,
1, 3, 2,
4, 6, 5,
5, 6, 7,
0, 2, 4,
4, 2, 6,
1, 5, 3,
5, 7, 3,
0, 4, 1,
4, 5, 1,
2, 3, 6,
6, 3, 7,
};
namespace
{
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
@@ -121,7 +84,7 @@ namespace Game
}
}
void GameRendering::Setup()
void GameRendering::Setup()
{
Log("Game rendering setup...");
SharedData& shared = GetShared();
@@ -150,14 +113,8 @@ namespace Game
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
Models[0].VertLayout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
Models[0].VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), Models[0].VertLayout);
Models[0].IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList)));
LoadMesh(Models[0]);
bgfx::ShaderHandle vertexShader = loadShader("vert");
bgfx::ShaderHandle fragmentShader = loadShader("frag");
@@ -196,11 +153,11 @@ namespace Game
if (isValid(newProgram))
{
Materials[0].Shader = newProgram;
}
else
{
Log("Failed to load shader!");
}
}
else
{
Log("Failed to load shader!");
}
}
}

View File

@@ -5,12 +5,14 @@
namespace Game
{
struct VertexData
struct PosColorVertex
{
float X;
float Y;
float Z;
uint32_t VertCol;
float x;
float y;
float z;
uint32_t abgr;
float uv_x;
float uv_y;
};
struct BgfxCallback : public bgfx::CallbackI

View File

@@ -6,11 +6,13 @@ $input v_uv0
float circle(vec2 uv, vec2 center, float size)
{
vec2 relPos = uv - center;
vec2 distSq = relPos.x * relPos.x + relPos.y * relPos.y;
float distSq = relPos.x * relPos.x + relPos.y * relPos.y;
return sqrt(distSq) <= size ? 1.0 : 0.0;
}
void main()
{
gl_FragColor = circle(v_uv0, vec2(0.5, 0.5), 0.5);
// gl_FragColor = circle(v_uv0, vec2(0.5, 0.5), 1.0);
float color = v_uv0.x < 0.5 && v_uv0.y < 0.5;
gl_FragColor = vec4(color, color, color, 1.0);
}

BIN
src/models/cube.bin Normal file

Binary file not shown.

104
src/models/cube.gltf Normal file
View File

@@ -0,0 +1,104 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.3.47",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
0
]
}
],
"nodes":[
{
"mesh":0,
"name":"Cube"
}
],
"meshes":[
{
"name":"Cube",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3
}
]
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":24,
"max":[
1,
1,
1
],
"min":[
-1,
-1,
-1
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":24,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":24,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":36,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":288,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":288,
"byteOffset":288,
"target":34962
},
{
"buffer":0,
"byteLength":192,
"byteOffset":576,
"target":34962
},
{
"buffer":0,
"byteLength":72,
"byteOffset":768,
"target":34963
}
],
"buffers":[
{
"byteLength":840,
"uri":"cube.bin"
}
]
}

Binary file not shown.