diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1584dc6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# assets +*.blend1 diff --git a/.gitmodules b/.gitmodules index 3e0ff9b..e8d4f9d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/assets/blender/cube.blend b/assets/blender/cube.blend new file mode 100644 index 0000000..3631264 Binary files /dev/null and b/assets/blender/cube.blend differ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c542f58..a12d979 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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") diff --git a/src/dependency/tinygltf b/src/dependency/tinygltf new file mode 160000 index 0000000..a5e653e --- /dev/null +++ b/src/dependency/tinygltf @@ -0,0 +1 @@ +Subproject commit a5e653e46cca64bbc0ecee7ede007347a94c39b0 diff --git a/src/engine/main.cpp b/src/engine/main.cpp index 7d3b046..77ae55c 100644 --- a/src/engine/main.cpp +++ b/src/engine/main.cpp @@ -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); diff --git a/src/game/Mesh.cpp b/src/game/Mesh.cpp new file mode 100644 index 0000000..c9c4476 --- /dev/null +++ b/src/game/Mesh.cpp @@ -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(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); + } + } +} diff --git a/src/game/Mesh.h b/src/game/Mesh.h new file mode 100644 index 0000000..a339b9d --- /dev/null +++ b/src/game/Mesh.h @@ -0,0 +1,8 @@ +#pragma once + +#include "rendering/Rendering.h" + +namespace Game +{ + void LoadMesh(Model& mesh); +} diff --git a/src/game/compiled-shaders/dx11/frag.bin b/src/game/compiled-shaders/dx11/frag.bin index 3ceede1..3f37315 100644 Binary files a/src/game/compiled-shaders/dx11/frag.bin and b/src/game/compiled-shaders/dx11/frag.bin differ diff --git a/src/game/compiled-shaders/glsl/frag.bin b/src/game/compiled-shaders/glsl/frag.bin index 8d9f5f3..df3a911 100644 Binary files a/src/game/compiled-shaders/glsl/frag.bin and b/src/game/compiled-shaders/glsl/frag.bin differ diff --git a/src/game/compiled-shaders/spirv/frag.bin b/src/game/compiled-shaders/spirv/frag.bin index aa8cee3..25a5547 100644 Binary files a/src/game/compiled-shaders/spirv/frag.bin and b/src/game/compiled-shaders/spirv/frag.bin differ diff --git a/src/game/rendering/Rendering.cpp b/src/game/rendering/Rendering.cpp index 3a19634..018308d 100644 --- a/src/game/rendering/Rendering.cpp +++ b/src/game/rendering/Rendering.cpp @@ -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 @@ -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!"); + } } } diff --git a/src/game/rendering/Rendering.h b/src/game/rendering/Rendering.h index bfdd4c2..85c013c 100644 --- a/src/game/rendering/Rendering.h +++ b/src/game/rendering/Rendering.h @@ -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 diff --git a/src/game/shaders/frag.sc b/src/game/shaders/frag.sc index 2b0d4b9..6599465 100644 --- a/src/game/shaders/frag.sc +++ b/src/game/shaders/frag.sc @@ -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); } diff --git a/src/models/cube.bin b/src/models/cube.bin new file mode 100644 index 0000000..f7ba573 Binary files /dev/null and b/src/models/cube.bin differ diff --git a/src/models/cube.gltf b/src/models/cube.gltf new file mode 100644 index 0000000..890d2a7 --- /dev/null +++ b/src/models/cube.gltf @@ -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" + } + ] +} diff --git a/tools/remedy-session.rdbg b/tools/remedy-session.rdbg index cfe2b36..a9aaa04 100644 Binary files a/tools/remedy-session.rdbg and b/tools/remedy-session.rdbg differ