Skip to content

Commit

Permalink
Suzanne
Browse files Browse the repository at this point in the history
  • Loading branch information
QFSW committed May 14, 2023
1 parent 7bbe174 commit 19c4383
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 35 deletions.
1 change: 1 addition & 0 deletions PengEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ xcopy /y /d /s $(SolutionDir)resources $(OutDir)\resources\</Command>
<None Include="resources\audio\demo\bounce_wall.asset" />
<None Include="resources\audio\demo\goal.asset" />
<None Include="resources\entities\demo\pong\ball.asset" />
<None Include="resources\meshes\demo\suzanne.asset" />
<None Include="resources\scenes\demo\pong.json" />
<None Include="resources\shaders\core\fallback.asset" />
<None Include="resources\shaders\core\phong.asset" />
Expand Down
1 change: 1 addition & 0 deletions PengEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@
<None Include="resources\audio\core\menu_click.asset" />
<None Include="resources\audio\core\menu_select.asset" />
<None Include="resources\entities\demo\pong\ball.asset" />
<None Include="resources\meshes\demo\suzanne.asset" />
</ItemGroup>
<ItemGroup>
<Natvis Include="src\core\entity.natvis" />
Expand Down
4 changes: 4 additions & 0 deletions resources/meshes/demo/suzanne.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Suzanne",
"mesh": "resources/meshes/demo/suzanne.obj"
}
20 changes: 20 additions & 0 deletions resources/scenes/demo/sandbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@
"components::MeshRenderer"
]
},
{
"type": "Entity",
"name": "Suzanne",
"transform": {
"position": {
"x": -30,
"y": -3,
"z": 3
},
"rotation": {
"y": 180
}
},
"components": [
{
"type": "components::MeshRenderer",
"mesh": "resources/meshes/demo/suzanne.asset"
}
]
},
{
"type": "entities::Camera",
"transform": {
Expand Down
7 changes: 5 additions & 2 deletions src/components/mesh_renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "mesh_renderer.h"

#include <core/logger.h>
#include <core/asset.h>
#include <core/serialized_member.h>
#include <entities/camera.h>
#include <entities/point_light.h>
#include <entities/spot_light.h>
#include <entities/directional_light.h>
#include <rendering/mesh.h>
#include <rendering/primitives.h>
#include <rendering/material.h>
#include <rendering/render_queue.h>
#include <core/logger.h>
#include <utils/utils.h>
#include <math/math.h>

Expand All @@ -33,7 +36,7 @@ MeshRenderer::MeshRenderer(
, _mesh(std::move(mesh))
, _material(std::move(material))
{
// TODO: serialize _mesh
SERIALIZED_MEMBER(_mesh);
// TODO: serialize _material
}

Expand Down
3 changes: 0 additions & 3 deletions src/demo/pong/peng_pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "goal.h"
#include "paddle.h"
#include "pause_menu.h"
#include "rendering/mesh_decoder.h"

IMPLEMENT_ENTITY(demo::pong::PengPong);

Expand Down Expand Up @@ -48,8 +47,6 @@ void PengPong::post_create()
build_camera();
build_main_menu();

MeshDecoder::load_file("resources/meshes/demo/suzanne.obj");

Logger::success("PengPong started");
}

Expand Down
18 changes: 17 additions & 1 deletion src/rendering/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

#include <utils/utils.h>
#include <utils/check.h>
#include <utils/strtools.h>
#include <utils/vectools.h>
#include <core/archive.h>
#include <core/logger.h>
#include <memory/gc.h>
#include <profiling/scoped_event.h>

#include "mesh_decoder.h"

using namespace rendering;
using namespace math;

Expand Down Expand Up @@ -53,6 +56,13 @@ Mesh::Mesh(const std::string& name, const RawMeshData& raw_data)
)
{ }

Mesh::Mesh(const std::string& name, const std::string& mesh_path)
: Mesh(
name,
MeshDecoder::load_file(mesh_path)
)
{ }

Mesh::~Mesh()
{
SCOPED_EVENT("Destroying mesh", _name.c_str());
Expand All @@ -63,6 +73,12 @@ Mesh::~Mesh()
glDeleteVertexArrays(1, &_vao);
}

peng::shared_ref<Mesh> Mesh::load_asset(const Archive& archive)
{
const std::string mesh_path = archive.read<std::string>("mesh");
return memory::GC::alloc<Mesh>(archive.name, mesh_path);
}

void Mesh::render() const
{
bind();
Expand Down
11 changes: 8 additions & 3 deletions src/rendering/mesh.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include <string>
#include <vector>

#include <GL/glew.h>

#include <memory/shared_ref.h>

#include "raw_mesh_data.h"

struct Archive;

namespace rendering
{
// TODO: generate bounds for the mesh to be used in culling
Expand All @@ -15,11 +17,14 @@ namespace rendering
public:
Mesh(std::string&& name, RawMeshData&& raw_data);
Mesh(const std::string& name, const RawMeshData& raw_data);
Mesh(const std::string& name, const std::string& mesh_path);

Mesh(const Mesh&) = delete;
Mesh(Mesh&&) = delete;
~Mesh();

static peng::shared_ref<Mesh> load_asset(const Archive& archive);

// Renders the mesh
// A shader/material must already be in use before calling this
void render() const;
Expand All @@ -41,4 +46,4 @@ namespace rendering
GLuint _vbo;
GLuint _vao;
};
}
}
53 changes: 27 additions & 26 deletions src/rendering/mesh_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ RawMeshData MeshDecoder::load_obj(const std::string& path)
line_number, line.c_str()
);

Vector3u triangle;
uint32_t triangle[3];
for (uint8_t i = 0; i < 3; i++)
{
const std::string& token = tokens[1 + i];
const std::string& vertex_token = tokens[1 + i];
uint32_t vertex_index;

if (const auto it = vertex_cache.find(token); it != vertex_cache.end())
if (const auto it = vertex_cache.find(vertex_token); it != vertex_cache.end())
{
vertex_index = it->second;
}
Expand All @@ -139,52 +139,53 @@ RawMeshData MeshDecoder::load_obj(const std::string& path)
// Decode token
static thread_local std::vector<std::string> inner_tokens;
inner_tokens.clear();
strtools::split(token, '/', std::back_inserter(inner_tokens));
strtools::split(vertex_token, '/', std::back_inserter(inner_tokens));

DECODE_CHECK(inner_tokens.size() == 3,
"Invalid OBJ file - vertex '%s' does not use the v/vt/vn format\n%d: %s",
line_number, token.c_str(), line.c_str()
line_number, vertex_token.c_str(), line.c_str()
);

// OBJ files use 1 indexing instead of 0 indexing
const uint32_t position_index = std::stoi(inner_tokens[0]);
const uint32_t tex_coord_index = std::stoi(inner_tokens[1]);
const uint32_t normal_index = std::stoi(inner_tokens[2]);

if (!(position_index < vertex_positions.size()))
{
Logger::error("Invalid OBJ file - vertex '%s' has invalid position index %d\n%d: %s", token.c_str(), position_index, line_number, line.c_str()); return RawMeshData::corrupt_data();
} do {} while (0);
DECODE_CHECK(position_index <= vertex_positions.size(),
"Invalid OBJ file - vertex '%s' has invalid position index %d\n%d: %s",
vertex_token.c_str(), position_index, line_number, line.c_str()
);

DECODE_CHECK(tex_coord_index < uv_coords.size(),
DECODE_CHECK(tex_coord_index <= uv_coords.size(),
"Invalid OBJ file - vertex '%s' has invalid tex coord index %d\n%d: %s",
token.c_str(), tex_coord_index, line_number, line.c_str()
vertex_token.c_str(), tex_coord_index, line_number, line.c_str()
);

DECODE_CHECK(normal_index < vertex_normals.size(),
DECODE_CHECK(normal_index <= vertex_normals.size(),
"Invalid OBJ file - vertex '%s' has invalid normal index %d\n%d: %s",
token.c_str(), normal_index, line_number, line.c_str()
vertex_token.c_str(), normal_index, line_number, line.c_str()
);

const Vertex vertex(
vertex_positions[position_index],
vertex_normals[normal_index],
uv_coords[tex_coord_index]
vertex_positions[position_index - 1],
vertex_normals[normal_index - 1],
uv_coords[tex_coord_index - 1]
);

vertex_index = vertex_cache[token] = static_cast<uint32_t>(raw_data.vertices.size());
raw_data.vertices.push_back(vertex);
}
vertex_index = static_cast<uint32_t>(raw_data.vertices.size());
vertex_cache[vertex_token] = vertex_index;

switch (i)
{
case 0: triangle.x = vertex_index; break;
case 1: triangle.y = vertex_index; break;
case 2: triangle.z = vertex_index; break;
default: break;
raw_data.vertices.push_back(vertex);
}

raw_data.triangles.push_back(triangle);
triangle[i] = vertex_index;
}

raw_data.triangles.emplace_back(
triangle[0],
triangle[1],
triangle[2]
);
}
}

Expand Down

0 comments on commit 19c4383

Please sign in to comment.