Skip to content

Commit

Permalink
Added: Static Model loading and Basic App tests (spinning square)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillisMedwell committed Jan 28, 2024
1 parent 2293033 commit 8d2d451
Show file tree
Hide file tree
Showing 31 changed files with 491 additions and 112 deletions.
Empty file added .github/workflows/clang.yml
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion code/Demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if(DEFINED EMSCRIPTEN)
endforeach()
message(STATUS "PRELOAD FILES: \"${PRELOAD_FILES}\"")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PRELOAD_FILES}")
target_compile_options(Demos PRIVATE -msimd128 -mrelaxed-simd -msse -msse2 -mavx)
target_compile_options(Demos PRIVATE -msimd128 -mrelaxed-simd -msse -msse2 -mavx -O0 -g -sDEMANGLE_SUPPORT=2 -sASSERTIONS=1 -sSAFE_HEAP=1 -sSTACK_OVERFLOW_CHECK=2 -sNO_DISABLE_EXCEPTION_CATCHING -Wno-unused-command-line-argument -sEXCEPTION_DEBUG=1 -v)
else()
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
target_compile_options(Demos PRIVATE -march=native)
Expand Down
127 changes: 98 additions & 29 deletions code/Demos/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "App.hpp"
#include "Config.hpp"
#include "Models/Models.hpp"
#include "Model/Model.hpp"

#include <chrono>
#include <iostream>
Expand All @@ -9,48 +9,117 @@

#include <Utily/Utily.hpp>

// auto print_then_quit = [](auto& error) {
// std::cerr
// << error.what()
// << std::endl;
// exit(1);
// };

// auto print_num_faces = [](Model::Staging::Model& model) {
// std::println("Triangle Count: {}", model.index_data.size() / 3);
// };

// void timeLoadModel(const std::string& file_path) {
// auto start = std::chrono::high_resolution_clock::now();
// Models::Staging::loadModel(file_path).on_error(print_then_quit).on_value(print_num_faces);
// auto end = std::chrono::high_resolution_clock::now();
// std::chrono::duration<double, std::milli> duration = end - start;
// std::cout << "Loading " << file_path << " took " << duration.count() << " milliseconds." << std::endl;
// }
using namespace std::literals;

void* operator new(std::size_t size) {
std::cout << "Custom new called, size = " << size << std::endl;
void* memory = std::malloc(size);
return memory;
}

void* operator new[](std::size_t size) {
std::cout << "Custom new[] called, size = " << size << std::endl;
void* memory = std::malloc(size);
return memory;
}

struct Data {
Cameras::StationaryPerspective camera {
glm::vec3(0, 0, 0),
glm::vec3(1, 0, 0),
90.0f
};
std::chrono::steady_clock::time_point start_time;
constexpr static auto TRIANGLE_VERTICES = std::to_array({ 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f });
constexpr static auto TRIANGLE_INDICES = std::to_array<uint32_t>({ 0, 1, 3, 1, 2, 3 });

AppRenderer::ShaderId s_id;
AppRenderer::IndexBufferId ib_id;
AppRenderer::VertexBufferId vb_id;
AppRenderer::VertexArrayId va_id;

Cameras::StationaryPerspective camera { glm::vec3(0, 0, -1), glm::vec3(0, 0, 1) };

constexpr static glm::vec4 BACKGROUND_COLOUR = { 0, 0, 0, 1 };

constexpr static glm::vec3 ROTATION_AXIS = { 0, 0, 1.0f };
double angle = 0.0f;
constexpr static double ROTATIONS_PER_SECOND = 1;
};

struct Logic {
void init(AppRenderer& renderer, Data& data) {
data.start_time = std::chrono::high_resolution_clock::now();
constexpr auto vert =
"precision highp float; "
"uniform mat4 u_mvp;"
"layout(location = 0) in vec3 aPos;"
"void main() {"
" gl_Position = u_mvp * vec4(aPos, 1.0);"
"}"sv;

constexpr auto frag =
"precision highp float; "
"out vec4 FragColor; "
" void main()"
" {"
" FragColor = vec4(1.0, 0.5, 0.2, 1.0); "
" }"sv;

renderer.add_shader(vert, frag)
.on_error(Utily::ErrorHandler::print_then_quit)
.on_value([&](auto& id) {
data.s_id = id;
});
renderer.add_vertex_buffer()
.on_error(Utily::ErrorHandler::print_then_quit)
.on_value([&](auto& id) {
data.vb_id = id;
});
renderer.add_index_buffer()
.on_error(Utily::ErrorHandler::print_then_quit)
.on_value([&](auto& id) {
data.ib_id = id;
});

renderer.add_vertex_array(Renderer::VertexBufferLayout<glm::vec3> {}, data.vb_id)
.on_error(Utily::ErrorHandler::print_then_quit)
.on_value([&](auto& id) {
data.va_id = id;
});
}
void update(float dt, const AppInput& input, AppState& state, Data& data) {
state.should_close = true;
void update(double dt, AppInput& input, AppState& state, Data& data) {

auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - data.start_time);
data.angle = data.angle + data.ROTATIONS_PER_SECOND * 360.0 * dt;
}

void draw(AppRenderer& renderer, Data& data) {
const auto proj_mat = data.camera.projection_matrix(renderer.window_width, renderer.window_height);
const auto view_mat = data.camera.view_matrix();
renderer.screen_frame_buffer.bind();
renderer.screen_frame_buffer.clear(data.BACKGROUND_COLOUR);
renderer.screen_frame_buffer.resize(renderer.window_width, renderer.window_height);

Renderer::IndexBuffer& ib = renderer.index_buffers[data.ib_id.id];
Renderer::VertexBuffer& vb = renderer.vertex_buffers[data.vb_id.id];
Renderer::VertexArray& va = renderer.vertex_arrays[data.va_id.id];
Renderer::Shader& s = renderer.shaders[data.s_id.id];

auto pm = data.camera.projection_matrix(renderer.window_width, renderer.window_height);
auto vm = data.camera.view_matrix();
auto mm = glm::rotate(glm::mat4(1.0f), static_cast<float>(glm::radians(data.angle)), data.ROTATION_AXIS);

auto mvp = pm * vm * mm;

s.bind();
s.set_uniform("u_mvp", mvp);
va.bind();
ib.bind();
vb.bind();
ib.load_indices(data.TRIANGLE_INDICES);
vb.load_vertices(data.TRIANGLE_VERTICES);
glDrawElements(GL_TRIANGLES, ib.get_count(), GL_UNSIGNED_INT, (void*)0);
}

void stop() {
}
};

int main() {
autoRunApp<Data, Logic>();
auto_run_app<Data, Logic>("Demo", 1000, 500);
return 0;
}
4 changes: 2 additions & 2 deletions code/Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ if(DEFINED EMSCRIPTEN)
)
target_include_directories(Engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${Stb_INCLUDE_DIR})
target_compile_options(Engine PUBLIC
"$<$<CONFIG:Debug>:-O0;-g;-sDEMANGLE_SUPPORT=1;-sFORCE_FILESYSTEM=1;-sASSERTIONS=1;-sSAFE_HEAP=1;-sSTACK_OVERFLOW_CHECK=2;-sNO_DISABLE_EXCEPTION_CATCHING;-Wno-unused-command-line-argument>"
"$<$<CONFIG:Debug>:-O0;-g3;-sDEMANGLE_SUPPORT=1;-sFORCE_FILESYSTEM=1;-sASSERTIONS=1;-sSAFE_HEAP=1;-sSTACK_OVERFLOW_CHECK=2;-sNO_DISABLE_EXCEPTION_CATCHING;-Wno-unused-command-line-argument>"
"$<$<CONFIG:Release>:-Oz;-sGL_FFP_ONLY;-msimd128;-Wno-unused-command-line-argument>"
)
target_link_options(Engine PUBLIC -sUSE_WEBGL2=1 -sUSE_GLFW=3 -sFULL_ES3=1 -sFULL_ES2=1 -Wno-unused-command-line-argument)
target_link_options(Engine PUBLIC -sUSE_WEBGL2=1 -sUSE_GLFW=3 -sFULL_ES3=1 -sFULL_ES2=1 -Wno-unused-command-line-argument -sALLOW_MEMORY_GROWTH)
else()
find_package(OpenGL REQUIRED)
find_package(OpenAL CONFIG REQUIRED)
Expand Down
31 changes: 25 additions & 6 deletions code/Engine/include/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
#include "AppInput.hpp"
#include "AppRenderer.hpp"

#include <chrono>
#include <thread>

struct AppState {
bool should_close = false;
};

template <typename T, typename AppData>
concept HasValidAppLogic = requires(T t, float dt, AppState& state, AppData& data, AppRenderer& renderer, AppInput& input) {
concept HasValidAppLogic = requires(T t, double dt, AppState& state, AppData& data, AppRenderer& renderer, AppInput& input) {
{
t.init(renderer, data)
} -> std::same_as<void>;
Expand Down Expand Up @@ -63,14 +66,15 @@ class App
_context.stop();
}
auto update() -> void {
float dt = std::chrono::duration<float> { std::chrono::high_resolution_clock::now() - _last_update }.count();
double dt = std::chrono::duration<double> { std::chrono::high_resolution_clock::now() - _last_update }.count();
_logic.update(dt, _input, _state, _data);
_last_update = std::chrono::high_resolution_clock::now();
}
auto render() -> void {
_renderer.window_width = _context.window_width;
_renderer.window_height = _context.window_height;
_logic.draw(_renderer, _data);
_context.swap_buffers();
}

auto poll_events() -> void {
Expand All @@ -86,13 +90,28 @@ class App
};

template <typename Data, typename Logic>
void autoRunApp(std::string_view app_name = "Auto Running App") {
App<Data, Logic> app;
app.init(app_name, 400, 400);
void auto_run_app(std::string_view app_name = "Auto Running App", uint16_t width = 400, uint16_t height = 400) {
static App<Data, Logic> app;
app.init(app_name, width, height);

#if defined(CONFIG_TARGET_NATIVE)
while (app.is_running()) {
app.poll_events();
app.update();
app.render();
}
app.stop();
}
#elif defined(CONFIG_TARGET_WEB)
emscripten_set_main_loop(
[]() {
if (!app.is_running()) {
emscripten_cancel_main_loop();
}
app.poll_events();
app.update();
app.render();
},
0,
0);
#endif
}
2 changes: 2 additions & 0 deletions code/Engine/include/AppRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class AppRenderer
Utily::StaticVector<Renderer::VertexBuffer, 20> vertex_buffers;
Utily::StaticVector<Renderer::IndexBuffer, 20> index_buffers;
Utily::StaticVector<Renderer::VertexArray, 20> vertex_arrays;

Renderer::ScreenFrameBuffer screen_frame_buffer;

float window_width;
float window_height;
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions code/Engine/include/Model/Model.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "Model/Types.hpp"
#include "Model/Static.hpp"
#include "Model/Animated.hpp"
#include "Model/Dynamic.hpp"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "Models/Types.hpp"
#include "Model/Types.hpp"
#include "Utily/Utily.hpp"

namespace Models {

namespace Model {
// Contiguous vertices and indices.
struct Static {
std::array<Vec3, 2> axis_align_bounding_box;
std::unique_ptr<std::byte[]> data;
Expand All @@ -13,7 +13,7 @@ namespace Models {
};

auto decode_as_static_model(
std::span<std::byte> file_data,
std::span<uint8_t> file_data,
Utily::StaticVector<char, 16> file_extension)
-> Utily::Result<Static, Utily::Error>;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include <array>
#include <iostream>
#include <glm/ext/quaternion_common.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

namespace Models {
namespace Model {
using Vec2 = glm::vec2;
using Vec3 = glm::vec3;
using Quat = glm::quat;
Expand All @@ -15,6 +16,14 @@ namespace Models {
Vec3 position;
Vec3 normal;
Vec2 uv_coord;

friend auto operator<<(std::ostream& stream, const Vertex& vertex) -> std::ostream& {
stream << "v(" << vertex.position.x << ',' << vertex.position.y << ',' << vertex.position.z << ") \t"
<< "n(" << vertex.normal.x << ',' << vertex.normal.y << ',' << vertex.normal.z << ") \t"
<< "uv(" << vertex.uv_coord.x << ',' << vertex.uv_coord.y << ")";

return stream;
}
};

using Index = uint32_t;
Expand Down
6 changes: 0 additions & 6 deletions code/Engine/include/Models/Models.hpp

This file was deleted.

8 changes: 0 additions & 8 deletions code/Engine/include/Renderer/DefaultFrameBuffer.hpp

This file was deleted.

13 changes: 13 additions & 0 deletions code/Engine/include/Renderer/FrameBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Utily/Utily.hpp>

#include "Config.hpp"
#include <glm/glm.hpp>
#include <optional>

namespace Renderer {
Expand All @@ -26,4 +27,16 @@ namespace Renderer {
std::optional<uint32_t> _colour_attachment_index = std::nullopt;
uint32_t _width { 0 }, _height { 0 };
};

class ScreenFrameBuffer
{
public:
static void clear(glm::vec4 colour = { 0, 0, 0, 1.0f }) noexcept;
static void bind() noexcept;
static void resize(uint32_t screen_width, uint32_t screen_height) noexcept;

private:
constexpr static uint32_t SCREEN_ID = 0;
static uint32_t width, height;
};
}
16 changes: 15 additions & 1 deletion code/Engine/include/Renderer/IndexBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@ namespace Renderer {
IndexBuffer(const IndexBuffer&) = delete;
IndexBuffer(IndexBuffer&& other) noexcept;

auto init() noexcept -> Utily::Result<void, Utily::Error>;
[[nodiscard]] auto init() noexcept -> Utily::Result<void, Utily::Error>;
void stop() noexcept;

void bind() noexcept;
void unbind() noexcept;

template <typename Range>
requires std::ranges::range<Range>
&& std::contiguous_iterator<std::ranges::iterator_t<Range>>
&& std::same_as<std::ranges::range_value_t<Range>, uint32_t>
&& std::ranges::sized_range<Range>
void load_indices(const Range& indices) noexcept {
this->bind();
size_t size_in_bytes = indices.size() * sizeof(uint32_t);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size_in_bytes, &(*indices.begin()), GL_DYNAMIC_DRAW);
_count = indices.size();
}

size_t get_count() const noexcept { return _count; }

private:
std::optional<uint32_t> _id = std::nullopt;
size_t _count;
Expand Down
1 change: 1 addition & 0 deletions code/Engine/include/Renderer/OpenglContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Renderer {
[[nodiscard]] auto init(std::string_view app_name, uint_fast16_t width, uint_fast16_t height) -> Utily::Result<void, Utily::Error>;
void poll_events();
void stop();
void swap_buffers() noexcept;
[[nodiscard]] auto should_close() -> bool;

uint_fast16_t window_width, window_height;
Expand Down
Loading

0 comments on commit 8d2d451

Please sign in to comment.