From cd0fe6f27e14eadd71fb8ab5be0010659f2fd399 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 27 Jan 2021 15:15:40 -0600 Subject: [PATCH] Mesh refactor and documentation --- core/Mesh.cpp | 114 ++++++++++++++++++++++++++++++++++---------------- core/Mesh.h | 75 +++++++++++++++++++++++++++------ 2 files changed, 141 insertions(+), 48 deletions(-) diff --git a/core/Mesh.cpp b/core/Mesh.cpp index a3b7138..e599cd4 100644 --- a/core/Mesh.cpp +++ b/core/Mesh.cpp @@ -1,5 +1,38 @@ +/* + Dane Johnson + + LICENSE + + Couch Copyright (C) 2021 Dane Johnson + + This program comes with ABSOLUTELY NO WARRANTY; without event the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for details at + https://www.gnu.org/licenses/gpl-3.0.html + + This is free software, and you are welcome to redistribute it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + DESCRIPTION + + Meshes and their subclasses are anything that can be rendered + on screen. They are divided into submeshes, each of which + can have its own material properties. +*/ #include "Mesh.h" +// Thirdparty includes +#include +#include +#include + + +SubMesh *aiMesh2SubMesh(aiMesh *mesh, aiMaterial *material); +Color aiColor3D2Color(aiColor3D aicolor); + + SubMesh::SubMesh() {} SubMesh::SubMesh(VertexList vertices, IndexList indices) { @@ -51,47 +84,27 @@ SubMesh *SubMesh::Duplicate() { return submesh; } -Name Mesh::GetType() const {return "Mesh";} - -Mesh::Mesh() {} - Mesh::~Mesh() { for (SubMesh *sub : submeshes) { delete sub; } } -Mesh *Mesh::Create() { - return new Mesh; -} - -Mesh *Mesh::Duplicate() { - Mesh *mesh = static_cast(Spatial::Duplicate()); - // Duplicate submeshes - mesh->submeshes = SubMeshList(); - - for (SubMesh *submesh : submeshes) { - mesh->submeshes.push_back(submesh->Duplicate()); - } - - return mesh; -} - -Mesh *Mesh::Instance() { - return static_cast(Node::Instance()); -} - -void Mesh::SetupMesh() { - for (SubMesh *sub : submeshes) { - sub->SetupSubMesh(); - } +int Mesh::GetNumSubmeshes() { + return submeshes.size(); } Material Mesh::GetMaterial(int submesh) { + if (submesh >= GetNumSubmeshes()) { + Util::Die("Submesh index out of range"); + } return submeshes[submesh]->material; } void Mesh::SetMaterial(int submesh, Material material) { + if (submesh >= GetNumSubmeshes()) { + Util::Die("Submesh index out of range"); + } submeshes[submesh]->material = material; } @@ -125,7 +138,42 @@ Mesh* Mesh::FromFile(const char *filename) { return my_mesh; } -SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh, aiMaterial* material){ +void Mesh::Draw(Shader *shader) { + for (SubMesh *sub : submeshes) { + sub->Draw(shader); + } +} + +Mesh *Mesh::Create() { + return new Mesh; +} + +Mesh *Mesh::Duplicate() { + Mesh *mesh = static_cast(Spatial::Duplicate()); + // Duplicate submeshes + mesh->submeshes = SubMeshList(); + + for (SubMesh *submesh : submeshes) { + mesh->submeshes.push_back(submesh->Duplicate()); + } + + return mesh; +} + +Mesh *Mesh::Instance() { + return static_cast(Node::Instance()); +} + +Name Mesh::GetType() const {return "Mesh";} + +void Mesh::SetupMesh() { + for (SubMesh *sub : submeshes) { + sub->SetupSubMesh(); + } +} + + +SubMesh *aiMesh2SubMesh(aiMesh *aimesh, aiMaterial* material){ SubMesh *sub = new SubMesh(); for (int i = 0; i < aimesh->mNumVertices; i++) { aiVector3D aiPosition = aimesh->mVertices[i]; @@ -163,16 +211,10 @@ SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh, aiMaterial* material){ return sub; } -Color Mesh::aiColor3D2Color(aiColor3D aicolor) { +Color aiColor3D2Color(aiColor3D aicolor) { Color color; color.r = aicolor.r; color.g = aicolor.g; color.b = aicolor.b; return color; } - -void Mesh::Draw(Shader *shader) { - for (SubMesh *sub : submeshes) { - sub->Draw(shader); - } -} diff --git a/core/Mesh.h b/core/Mesh.h index a355abe..4a942ed 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -1,16 +1,34 @@ +/** + @file + @author Dane Johnson + + @section LICENSE + + Couch Copyright (C) 2021 Dane Johnson + + This program comes with ABSOLUTELY NO WARRANTY; without event the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for details at + https://www.gnu.org/licenses/gpl-3.0.html + + This is free software, and you are welcome to redistribute it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + @section DESCRIPTION + + Meshes and their subclasses are anything that can be rendered + on screen. They are divided into submeshes, each of which + can have its own material properties. +*/ #ifndef MESH_H #define MESH_H #include #include -// Thirdparty includes -#include -#include -#include - #include "types.h" -#include "Util.h" #include "Spatial.h" #include "Vertex.h" #include "Index.h" @@ -35,25 +53,58 @@ private: typedef std::vector SubMeshList; +/** + Mesh and its subclasses are anything that can be rendered on screen. + A mesh is made up of submeshes, each of which can have one material + property. +*/ class Mesh : public Spatial { public: - Mesh(); ~Mesh(); + + /** + @return The number of submeshes this mesh is made up of + */ + int GetNumSubmeshes(); + /** + Get the material property of a submesh + @param submesh The index of the submesh who's material is desired + @return The material property + */ Material GetMaterial(int submesh); + /** + Set the material property of a submesh + @param submesh The index of the submesh to update + @param material The desired material property + */ void SetMaterial(int submesh, Material material); + + /** + Create a new mesh from a Wavefront file. + A submesh will be generated for each material on the mesh. + Material properties will be generated based on the .mat + file, if present + @param filename The name of the file containing the mesh. + @return A new mesh prefab + */ static Mesh *FromFile(const char *filename); - virtual bool IsDrawable() const {return true;} + + /** + Draw this mesh, by calling Draw on all submeshes. + The shader will be updated on each call to the material + property of the submesh + @param shader The shader program to use to draw + */ virtual void Draw(Shader *shader); - virtual Name GetType() const; + virtual Mesh *Create(); virtual Mesh *Duplicate(); virtual Mesh *Instance(); + + virtual Name GetType() const; protected: SubMeshList submeshes; virtual void SetupMesh(); -private: - static SubMesh *aiMesh2SubMesh(aiMesh *mesh, aiMaterial *material); - static Color aiColor3D2Color(aiColor3D aicolor); }; typedef std::list MeshList;