mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-25 14:15:02 +00:00
code
This commit is contained in:
@@ -705,11 +705,8 @@ void VulkanCubeApp::DrawFrame(float time) {
|
||||
throw std::runtime_error("Failed to acquire swap chain image");
|
||||
}
|
||||
|
||||
auto view = core::LookAt({2.0f, 2.0f, 2.5f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f});
|
||||
auto projection = core::Perspective(0.78f, static_cast<float>(swapChainExtent_.width) /
|
||||
static_cast<float>(swapChainExtent_.height),
|
||||
0.1f, 10.0f);
|
||||
auto viewProj = core::MultiplyMatrix(projection, view);
|
||||
float aspect = static_cast<float>(swapChainExtent_.width) / static_cast<float>(swapChainExtent_.height);
|
||||
auto viewProj = cubeScript_.GetViewProjectionMatrix(aspect);
|
||||
|
||||
vkResetCommandBuffer(commandBuffers_[imageIndex], 0);
|
||||
RecordCommandBuffer(commandBuffers_[imageIndex], imageIndex, time, viewProj);
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include <SDL3/SDL_vulkan.h>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#include "core/math.hpp"
|
||||
#include "core/vertex.hpp"
|
||||
#include "script/cube_script.hpp"
|
||||
|
||||
namespace sdl3cpp::app {
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
#ifndef SDL3CPP_CORE_MATH_HPP
|
||||
#define SDL3CPP_CORE_MATH_HPP
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace sdl3cpp::core {
|
||||
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
std::array<float, 3> position;
|
||||
std::array<float, 3> color;
|
||||
};
|
||||
|
||||
struct PushConstants {
|
||||
std::array<float, 16> model;
|
||||
std::array<float, 16> viewProj;
|
||||
};
|
||||
|
||||
static_assert(sizeof(PushConstants) == sizeof(float) * 32, "push constant size mismatch");
|
||||
|
||||
inline std::array<float, 16> MultiplyMatrix(const std::array<float, 16>& a,
|
||||
const std::array<float, 16>& b) {
|
||||
std::array<float, 16> result{};
|
||||
for (int row = 0; row < 4; ++row) {
|
||||
for (int col = 0; col < 4; ++col) {
|
||||
float sum = 0.0f;
|
||||
for (int idx = 0; idx < 4; ++idx) {
|
||||
sum += a[idx * 4 + row] * b[col * 4 + idx];
|
||||
}
|
||||
result[col * 4 + row] = sum;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::array<float, 16> IdentityMatrix() {
|
||||
return {1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f};
|
||||
}
|
||||
|
||||
inline Vec3 Normalize(Vec3 v) {
|
||||
float len = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
|
||||
if (len == 0.0f) {
|
||||
return v;
|
||||
}
|
||||
return {v.x / len, v.y / len, v.z / len};
|
||||
}
|
||||
|
||||
inline Vec3 Cross(const Vec3& a, const Vec3& b) {
|
||||
return {a.y * b.z - a.z * b.y,
|
||||
a.z * b.x - a.x * b.z,
|
||||
a.x * b.y - a.y * b.x};
|
||||
}
|
||||
|
||||
inline float Dot(const Vec3& a, const Vec3& b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
inline std::array<float, 16> LookAt(const Vec3& eye, const Vec3& center, const Vec3& up) {
|
||||
Vec3 f = Normalize({center.x - eye.x, center.y - eye.y, center.z - eye.z});
|
||||
Vec3 s = Normalize(Cross(f, up));
|
||||
Vec3 u = Cross(s, f);
|
||||
|
||||
std::array<float, 16> result = IdentityMatrix();
|
||||
result[0] = s.x;
|
||||
result[1] = u.x;
|
||||
result[2] = -f.x;
|
||||
result[4] = s.y;
|
||||
result[5] = u.y;
|
||||
result[6] = -f.y;
|
||||
result[8] = s.z;
|
||||
result[9] = u.z;
|
||||
result[10] = -f.z;
|
||||
result[12] = -Dot(s, eye);
|
||||
result[13] = -Dot(u, eye);
|
||||
result[14] = Dot(f, eye);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::array<float, 16> Perspective(float fovRadians, float aspect, float zNear, float zFar) {
|
||||
float tanHalf = std::tan(fovRadians / 2.0f);
|
||||
std::array<float, 16> result{};
|
||||
result[0] = 1.0f / (aspect * tanHalf);
|
||||
result[5] = -1.0f / tanHalf;
|
||||
result[10] = zFar / (zNear - zFar);
|
||||
result[11] = -1.0f;
|
||||
result[14] = (zNear * zFar) / (zNear - zFar);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace sdl3cpp::core
|
||||
|
||||
#endif // SDL3CPP_CORE_MATH_HPP
|
||||
22
src/core/vertex.hpp
Normal file
22
src/core/vertex.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef SDL3CPP_CORE_VERTEX_HPP
|
||||
#define SDL3CPP_CORE_VERTEX_HPP
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace sdl3cpp::core {
|
||||
|
||||
struct Vertex {
|
||||
std::array<float, 3> position;
|
||||
std::array<float, 3> color;
|
||||
};
|
||||
|
||||
struct PushConstants {
|
||||
std::array<float, 16> model;
|
||||
std::array<float, 16> viewProj;
|
||||
};
|
||||
|
||||
static_assert(sizeof(PushConstants) == sizeof(float) * 32, "push constant size mismatch");
|
||||
|
||||
} // namespace sdl3cpp::core
|
||||
|
||||
#endif // SDL3CPP_CORE_VERTEX_HPP
|
||||
@@ -6,11 +6,38 @@
|
||||
|
||||
namespace sdl3cpp::script {
|
||||
|
||||
namespace {
|
||||
|
||||
std::array<float, 16> IdentityMatrix() {
|
||||
return {1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CubeScript::CubeScript(const std::filesystem::path& scriptPath) : L_(luaL_newstate()) {
|
||||
if (!L_) {
|
||||
throw std::runtime_error("Failed to create Lua state");
|
||||
}
|
||||
luaL_openlibs(L_);
|
||||
auto scriptDir = scriptPath.parent_path();
|
||||
if (!scriptDir.empty()) {
|
||||
lua_getglobal(L_, "package");
|
||||
if (lua_istable(L_, -1)) {
|
||||
lua_getfield(L_, -1, "path");
|
||||
const char* currentPath = lua_tostring(L_, -1);
|
||||
std::string newPath = scriptDir.string() + "/?.lua;";
|
||||
if (currentPath) {
|
||||
newPath += currentPath;
|
||||
}
|
||||
lua_pop(L_, 1);
|
||||
lua_pushstring(L_, newPath.c_str());
|
||||
lua_setfield(L_, -2, "path");
|
||||
}
|
||||
lua_pop(L_, 1);
|
||||
}
|
||||
if (luaL_dofile(L_, scriptPath.string().c_str()) != LUA_OK) {
|
||||
std::string message = LuaErrorMessage(L_);
|
||||
lua_pop(L_, 1);
|
||||
@@ -97,7 +124,7 @@ std::array<float, 16> CubeScript::ComputeModelMatrix(int functionRef, float time
|
||||
lua_getglobal(L_, "compute_model_matrix");
|
||||
if (!lua_isfunction(L_, -1)) {
|
||||
lua_pop(L_, 1);
|
||||
return core::IdentityMatrix();
|
||||
return IdentityMatrix();
|
||||
}
|
||||
} else {
|
||||
lua_rawgeti(L_, LUA_REGISTRYINDEX, functionRef);
|
||||
@@ -119,6 +146,27 @@ std::array<float, 16> CubeScript::ComputeModelMatrix(int functionRef, float time
|
||||
return matrix;
|
||||
}
|
||||
|
||||
std::array<float, 16> CubeScript::GetViewProjectionMatrix(float aspect) {
|
||||
lua_getglobal(L_, "get_view_projection");
|
||||
if (!lua_isfunction(L_, -1)) {
|
||||
lua_pop(L_, 1);
|
||||
throw std::runtime_error("Lua function 'get_view_projection' is missing");
|
||||
}
|
||||
lua_pushnumber(L_, aspect);
|
||||
if (lua_pcall(L_, 1, 1, 0) != LUA_OK) {
|
||||
std::string message = LuaErrorMessage(L_);
|
||||
lua_pop(L_, 1);
|
||||
throw std::runtime_error("Lua get_view_projection failed: " + message);
|
||||
}
|
||||
if (!lua_istable(L_, -1)) {
|
||||
lua_pop(L_, 1);
|
||||
throw std::runtime_error("'get_view_projection' did not return a table");
|
||||
}
|
||||
std::array<float, 16> matrix = ReadMatrix(L_, -1);
|
||||
lua_pop(L_, 1);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
std::vector<core::Vertex> CubeScript::ReadVertexArray(lua_State* L, int index) {
|
||||
int absIndex = lua_absindex(L, index);
|
||||
if (!lua_istable(L, absIndex)) {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
#include "core/math.hpp"
|
||||
#include "core/vertex.hpp"
|
||||
|
||||
namespace sdl3cpp::script {
|
||||
|
||||
@@ -32,6 +32,7 @@ public:
|
||||
|
||||
std::vector<SceneObject> LoadSceneObjects();
|
||||
std::array<float, 16> ComputeModelMatrix(int functionRef, float time);
|
||||
std::array<float, 16> GetViewProjectionMatrix(float aspect);
|
||||
std::unordered_map<std::string, ShaderPaths> LoadShaderPathsMap();
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user