Skip to content

Commit

Permalink
Zephyr: Next: implement cube benchmarking scene
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed May 10, 2024
1 parent 392509f commit 7bfa732
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
95 changes: 94 additions & 1 deletion app/next/src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "main_window.hpp"

static const bool enable_validation_layers = true;
static const bool benchmark_scene_size = true;

namespace zephyr {

Expand All @@ -27,7 +28,11 @@ namespace zephyr {
}

void MainWindow::Setup() {
CreateScene();
if(benchmark_scene_size) {
CreateBenchmarkScene();
} else {
CreateScene();
}

#ifdef ZEPHYR_OPENGL
CreateOpenGLEngine();
Expand Down Expand Up @@ -165,6 +170,94 @@ namespace zephyr {
m_scene_root->Add(m_behemoth_scene);
}

void MainWindow::CreateBenchmarkScene() {
m_scene_root = SceneNode::New();

// TODO(fleroviux): engine crashes when there is no camera in the scene! VERY BAD!!!
m_camera_node = m_scene_root->CreateChild("RenderCamera");
m_camera_node->CreateComponent<PerspectiveCameraComponent>(45.0f, 16.f / 9.f, 0.01f, 100.f);
m_camera_node->GetTransform().GetPosition() = {0.f, 0.f, 5.f};

// TODO(fleroviux): fix cube geometry is rendered incorrectly.

/**
* 4-------5
* /| /|
* 0-------1 |
* | 6-----|-7
* |/ |/
* 2-------3
*/
RenderGeometryLayout layout{};
layout.AddAttribute(RenderGeometryAttribute::Position);
layout.AddAttribute(RenderGeometryAttribute::Color);

std::shared_ptr<Geometry> cube_geometry = std::make_shared<Geometry>(layout, 8, 36);

auto positions = cube_geometry->GetPositions();
positions[0] = Vector3{-1.0, -1.0, 1.0};
positions[1] = Vector3{ 1.0, -1.0, 1.0};
positions[2] = Vector3{-1.0, 1.0, 1.0};
positions[3] = Vector3{ 1.0, 1.0, 1.0};
positions[4] = Vector3{-1.0, -1.0, -1.0};
positions[5] = Vector3{ 1.0, -1.0, -1.0};
positions[6] = Vector3{-1.0, 1.0, -1.0};
positions[7] = Vector3{ 1.0, 1.0, -1.0};

auto colors = cube_geometry->GetColors();
if(colors.IsValid()) {
colors[0] = Vector4{1.0, 0.0, 0.0, 1.0};
colors[1] = Vector4{0.0, 1.0, 0.0, 1.0};
colors[2] = Vector4{0.0, 0.0, 1.0, 1.0};
colors[3] = Vector4{1.0, 0.0, 1.0, 1.0};
colors[4] = Vector4{1.0, 1.0, 0.0, 1.0};
colors[5] = Vector4{0.0, 1.0, 1.0, 1.0};
colors[6] = Vector4{1.0, 1.0, 1.0, 1.0};
colors[7] = Vector4{0.0, 0.0, 0.0, 1.0};
}

auto indices = cube_geometry->GetIndices();
u32 index_data[] {
// front
0, 1, 2,
1, 3, 2,

// back
4, 5, 6,
5, 7, 6,

// left
0, 4, 6,
0, 6, 2,

// right
1, 5, 7,
1, 7, 3,

// top
4, 1, 0,
4, 5, 1,

// bottom
6, 3, 2,
6, 7, 3
};
std::copy_n(index_data, sizeof(index_data) / sizeof(u32), indices.begin());

const int grid_size = 64;

for(int x = -grid_size / 2; x < grid_size / 2; x++) {
for(int y = -grid_size / 2; y < grid_size / 2; y++) {
for(int z = -grid_size / 2; z < grid_size / 2; z++) {
std::shared_ptr<SceneNode> cube = m_scene_root->CreateChild("Cube");
cube->CreateComponent<MeshComponent>(cube_geometry);
cube->GetTransform().GetPosition() = {(f32)x, (f32)y, (f32)-z};
cube->GetTransform().GetScale() = {0.1, 0.1, 0.1};
}
}
}
}

void MainWindow::CleanupVulkan() {
vkDestroySurfaceKHR(m_vk_instance->Handle(), m_vk_surface, nullptr);
SDL_DestroyWindow(m_window);
Expand Down
1 change: 1 addition & 0 deletions app/next/src/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace zephyr {
void MainLoop();
void RenderFrame();
void CreateScene();
void CreateBenchmarkScene();

void CreateVulkanEngine();
void CleanupVulkan();
Expand Down

0 comments on commit 7bfa732

Please sign in to comment.