Mesh refactor and documentation
This commit is contained in:
		
							
								
								
									
										114
									
								
								core/Mesh.cpp
									
									
									
									
									
								
							
							
						
						
									
										114
									
								
								core/Mesh.cpp
									
									
									
									
									
								
							@@ -1,5 +1,38 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					  Dane Johnson <dane@danejohnson.org>
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  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"
 | 
					#include "Mesh.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Thirdparty includes
 | 
				
			||||||
 | 
					#include <assimp/Importer.hpp>
 | 
				
			||||||
 | 
					#include <assimp/scene.h>
 | 
				
			||||||
 | 
					#include <assimp/postprocess.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SubMesh *aiMesh2SubMesh(aiMesh *mesh, aiMaterial *material);
 | 
				
			||||||
 | 
					Color aiColor3D2Color(aiColor3D aicolor);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SubMesh::SubMesh() {}
 | 
					SubMesh::SubMesh() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SubMesh::SubMesh(VertexList vertices, IndexList indices) {
 | 
					SubMesh::SubMesh(VertexList vertices, IndexList indices) {
 | 
				
			||||||
@@ -51,47 +84,27 @@ SubMesh *SubMesh::Duplicate() {
 | 
				
			|||||||
  return submesh;
 | 
					  return submesh;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Name Mesh::GetType() const {return "Mesh";}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Mesh::Mesh() {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Mesh::~Mesh() {
 | 
					Mesh::~Mesh() {
 | 
				
			||||||
  for (SubMesh *sub : submeshes) {
 | 
					  for (SubMesh *sub : submeshes) {
 | 
				
			||||||
    delete sub;
 | 
					    delete sub;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Mesh *Mesh::Create() {
 | 
					int Mesh::GetNumSubmeshes() {
 | 
				
			||||||
  return new Mesh;
 | 
					  return submeshes.size();
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Mesh *Mesh::Duplicate() {
 | 
					 | 
				
			||||||
  Mesh *mesh = static_cast<Mesh*>(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<Mesh*>(Node::Instance());
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void Mesh::SetupMesh() {
 | 
					 | 
				
			||||||
  for (SubMesh *sub : submeshes) {
 | 
					 | 
				
			||||||
    sub->SetupSubMesh();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Material Mesh::GetMaterial(int submesh) {
 | 
					Material Mesh::GetMaterial(int submesh) {
 | 
				
			||||||
 | 
					  if (submesh >= GetNumSubmeshes()) {
 | 
				
			||||||
 | 
					    Util::Die("Submesh index out of range");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  return submeshes[submesh]->material;
 | 
					  return submeshes[submesh]->material;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Mesh::SetMaterial(int submesh, Material material) {
 | 
					void Mesh::SetMaterial(int submesh, Material material) {
 | 
				
			||||||
 | 
					  if (submesh >= GetNumSubmeshes()) {
 | 
				
			||||||
 | 
					    Util::Die("Submesh index out of range");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  submeshes[submesh]->material = material;
 | 
					  submeshes[submesh]->material = material;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -125,7 +138,42 @@ Mesh* Mesh::FromFile(const char *filename) {
 | 
				
			|||||||
  return my_mesh;
 | 
					  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<Mesh*>(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<Mesh*>(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();
 | 
					  SubMesh *sub = new SubMesh();
 | 
				
			||||||
  for (int i = 0; i < aimesh->mNumVertices; i++) {
 | 
					  for (int i = 0; i < aimesh->mNumVertices; i++) {
 | 
				
			||||||
    aiVector3D aiPosition = aimesh->mVertices[i];
 | 
					    aiVector3D aiPosition = aimesh->mVertices[i];
 | 
				
			||||||
@@ -163,16 +211,10 @@ SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh, aiMaterial* material){
 | 
				
			|||||||
  return sub;
 | 
					  return sub;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Color Mesh::aiColor3D2Color(aiColor3D aicolor) {
 | 
					Color aiColor3D2Color(aiColor3D aicolor) {
 | 
				
			||||||
  Color color;
 | 
					  Color color;
 | 
				
			||||||
  color.r = aicolor.r;
 | 
					  color.r = aicolor.r;
 | 
				
			||||||
  color.g = aicolor.g;
 | 
					  color.g = aicolor.g;
 | 
				
			||||||
  color.b = aicolor.b;
 | 
					  color.b = aicolor.b;
 | 
				
			||||||
  return color;
 | 
					  return color;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
void Mesh::Draw(Shader *shader) {
 | 
					 | 
				
			||||||
  for (SubMesh *sub : submeshes) {
 | 
					 | 
				
			||||||
    sub->Draw(shader);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										75
									
								
								core/Mesh.h
									
									
									
									
									
								
							
							
						
						
									
										75
									
								
								core/Mesh.h
									
									
									
									
									
								
							@@ -1,16 +1,34 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					   @file
 | 
				
			||||||
 | 
					   @author Dane Johnson <dane@danejohnson.org>
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					   @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
 | 
					#ifndef MESH_H
 | 
				
			||||||
#define MESH_H
 | 
					#define MESH_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <list>
 | 
					#include <list>
 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Thirdparty includes
 | 
					 | 
				
			||||||
#include <assimp/Importer.hpp>
 | 
					 | 
				
			||||||
#include <assimp/scene.h>
 | 
					 | 
				
			||||||
#include <assimp/postprocess.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include "types.h"
 | 
					#include "types.h"
 | 
				
			||||||
#include "Util.h"
 | 
					 | 
				
			||||||
#include "Spatial.h"
 | 
					#include "Spatial.h"
 | 
				
			||||||
#include "Vertex.h"
 | 
					#include "Vertex.h"
 | 
				
			||||||
#include "Index.h"
 | 
					#include "Index.h"
 | 
				
			||||||
@@ -35,25 +53,58 @@ private:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
typedef std::vector<SubMesh*> SubMeshList;
 | 
					typedef std::vector<SubMesh*> 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 {
 | 
					class Mesh : public Spatial {
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
  Mesh();
 | 
					 | 
				
			||||||
  ~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);
 | 
					  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);
 | 
					  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);
 | 
					  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 void Draw(Shader *shader);
 | 
				
			||||||
  virtual Name GetType() const;
 | 
					
 | 
				
			||||||
  virtual Mesh *Create();
 | 
					  virtual Mesh *Create();
 | 
				
			||||||
  virtual Mesh *Duplicate();
 | 
					  virtual Mesh *Duplicate();
 | 
				
			||||||
  virtual Mesh *Instance();
 | 
					  virtual Mesh *Instance();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  virtual Name GetType() const;
 | 
				
			||||||
protected:
 | 
					protected:
 | 
				
			||||||
  SubMeshList submeshes;
 | 
					  SubMeshList submeshes;
 | 
				
			||||||
  virtual void SetupMesh();
 | 
					  virtual void SetupMesh();
 | 
				
			||||||
private:
 | 
					 | 
				
			||||||
  static SubMesh *aiMesh2SubMesh(aiMesh *mesh, aiMaterial *material);
 | 
					 | 
				
			||||||
  static Color aiColor3D2Color(aiColor3D aicolor);
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef std::list<Mesh*> MeshList;
 | 
					typedef std::list<Mesh*> MeshList;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user