2021-01-27 15:15:40 -06:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2021-01-13 10:42:57 -06:00
|
|
|
#include "Mesh.h"
|
|
|
|
|
2021-01-27 15:15:40 -06:00
|
|
|
// Thirdparty includes
|
|
|
|
#include <assimp/Importer.hpp>
|
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/postprocess.h>
|
|
|
|
|
|
|
|
|
|
|
|
SubMesh *aiMesh2SubMesh(aiMesh *mesh, aiMaterial *material);
|
|
|
|
Color aiColor3D2Color(aiColor3D aicolor);
|
|
|
|
|
|
|
|
|
2021-01-20 15:16:44 -06:00
|
|
|
SubMesh::SubMesh() {}
|
2021-01-17 14:36:38 -06:00
|
|
|
|
2021-01-20 14:54:54 -06:00
|
|
|
SubMesh::SubMesh(VertexList vertices, IndexList indices) {
|
2021-01-13 10:42:57 -06:00
|
|
|
this->vertices = vertices;
|
|
|
|
this->indices = indices;
|
|
|
|
}
|
|
|
|
|
2021-01-20 14:54:54 -06:00
|
|
|
void SubMesh::SetupSubMesh() {
|
2021-01-13 10:42:57 -06:00
|
|
|
glGenVertexArrays(1, &VAO);
|
|
|
|
glGenBuffers(1, &VBO);
|
|
|
|
glGenBuffers(1, &EBO);
|
|
|
|
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex),
|
|
|
|
&vertices[0], GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index),
|
|
|
|
&indices[0], GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// Vertex positions
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
|
2021-01-21 15:26:39 -06:00
|
|
|
// Vertex normal
|
2021-01-17 14:36:38 -06:00
|
|
|
glEnableVertexAttribArray(1);
|
2021-01-21 15:26:39 -06:00
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (3 * sizeof(float)));
|
|
|
|
// Vertex UV
|
|
|
|
glEnableVertexAttribArray(2);
|
|
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (6 * sizeof(float)));
|
2021-01-13 10:42:57 -06:00
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
2021-01-20 14:54:54 -06:00
|
|
|
void SubMesh::Draw(Shader *shader) {
|
2021-01-22 15:27:32 -06:00
|
|
|
shader->UpdateMaterial(material);
|
2021-01-20 14:54:54 -06:00
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
2021-01-24 16:37:35 -06:00
|
|
|
SubMesh *SubMesh::Duplicate() {
|
|
|
|
SubMesh* submesh = new SubMesh();
|
|
|
|
|
|
|
|
submesh->VAO = VAO;
|
|
|
|
submesh->indices = indices;
|
|
|
|
submesh->material = material;
|
|
|
|
|
|
|
|
return submesh;
|
|
|
|
}
|
|
|
|
|
2021-01-20 14:54:54 -06:00
|
|
|
Mesh::~Mesh() {
|
|
|
|
for (SubMesh *sub : submeshes) {
|
|
|
|
delete sub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:15:40 -06:00
|
|
|
int Mesh::GetNumSubmeshes() {
|
|
|
|
return submeshes.size();
|
2021-01-20 14:54:54 -06:00
|
|
|
}
|
|
|
|
|
2021-01-20 20:49:12 -06:00
|
|
|
Material Mesh::GetMaterial(int submesh) {
|
2021-01-27 15:15:40 -06:00
|
|
|
if (submesh >= GetNumSubmeshes()) {
|
|
|
|
Util::Die("Submesh index out of range");
|
|
|
|
}
|
2021-01-20 20:49:12 -06:00
|
|
|
return submeshes[submesh]->material;
|
|
|
|
}
|
|
|
|
|
2021-01-20 15:16:44 -06:00
|
|
|
void Mesh::SetMaterial(int submesh, Material material) {
|
2021-01-27 15:15:40 -06:00
|
|
|
if (submesh >= GetNumSubmeshes()) {
|
|
|
|
Util::Die("Submesh index out of range");
|
|
|
|
}
|
2021-01-20 14:54:54 -06:00
|
|
|
submeshes[submesh]->material = material;
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:25:47 -06:00
|
|
|
Mesh* Mesh::FromFile(const char *filename) {
|
|
|
|
// HOCUS: https://assimp-docs.readthedocs.io/en/latest/usage/use_the_lib.html
|
|
|
|
Assimp::Importer importer;
|
|
|
|
|
|
|
|
const aiScene* scene = importer.ReadFile(
|
|
|
|
filename, // Read the file
|
|
|
|
aiProcess_Triangulate | // We only do triangles
|
|
|
|
aiProcess_GenNormals | // We want normals precalculated
|
|
|
|
aiProcess_GenUVCoords // We want UV mappings precalculated
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!scene) {
|
2021-01-19 16:36:10 -06:00
|
|
|
Util::Die(importer.GetErrorString());
|
2021-01-18 18:25:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
aiNode *root = scene->mRootNode;
|
2021-01-22 18:44:43 -06:00
|
|
|
if (root->mNumChildren == 1) {
|
|
|
|
root = root->mChildren[0];
|
|
|
|
}
|
2021-01-20 14:54:54 -06:00
|
|
|
Mesh *my_mesh = new Mesh();
|
|
|
|
for (int i = 0; i < root->mNumMeshes; i++) {
|
|
|
|
aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[i]];
|
2021-01-22 18:44:43 -06:00
|
|
|
my_mesh->submeshes.push_back(aiMesh2SubMesh(mesh_to_import, scene->mMaterials[mesh_to_import->mMaterialIndex]));
|
2021-01-20 14:54:54 -06:00
|
|
|
}
|
2021-01-18 18:25:47 -06:00
|
|
|
|
2021-01-20 17:05:59 -06:00
|
|
|
my_mesh->SetupMesh();
|
|
|
|
|
2021-01-20 14:54:54 -06:00
|
|
|
return my_mesh;
|
2021-01-18 13:31:09 -06:00
|
|
|
}
|
|
|
|
|
2021-01-27 15:15:40 -06:00
|
|
|
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){
|
2021-01-20 14:54:54 -06:00
|
|
|
SubMesh *sub = new SubMesh();
|
2021-01-19 16:36:10 -06:00
|
|
|
for (int i = 0; i < aimesh->mNumVertices; i++) {
|
|
|
|
aiVector3D aiPosition = aimesh->mVertices[i];
|
2021-01-21 15:26:39 -06:00
|
|
|
aiVector3D aiNormal = aimesh->mNormals[i];
|
2021-01-19 16:36:10 -06:00
|
|
|
aiVector3D aiUV = aimesh->mTextureCoords[0][i]; // TODO get ALL texture coords
|
2021-01-21 15:26:39 -06:00
|
|
|
Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z,
|
|
|
|
aiNormal.x, aiNormal.y, aiNormal.z,
|
|
|
|
aiUV.x, aiUV.y);
|
2021-01-20 14:54:54 -06:00
|
|
|
sub->vertices.push_back(vertex);
|
2021-01-19 16:36:10 -06:00
|
|
|
}
|
|
|
|
for (int i = 0; i < aimesh->mNumFaces; i++) {
|
|
|
|
// We're importing triangulated meshes, so each face is three indices
|
|
|
|
unsigned int *face = aimesh->mFaces[i].mIndices;
|
|
|
|
Index index(face[0], face[1], face[2]);
|
2021-01-20 14:54:54 -06:00
|
|
|
sub->indices.push_back(index);
|
2021-01-19 16:36:10 -06:00
|
|
|
}
|
2021-01-22 18:44:43 -06:00
|
|
|
|
|
|
|
// Get material properties
|
|
|
|
aiColor3D ambient;
|
|
|
|
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_AMBIENT, ambient))
|
|
|
|
sub->material.ambient = aiColor3D2Color(ambient);
|
|
|
|
|
|
|
|
aiColor3D diffuse;
|
|
|
|
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse))
|
|
|
|
sub->material.diffuse = aiColor3D2Color(diffuse);
|
|
|
|
|
|
|
|
aiColor3D specular;
|
|
|
|
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_SPECULAR, specular))
|
|
|
|
sub->material.specular = aiColor3D2Color(specular);
|
|
|
|
|
|
|
|
float shininess;
|
|
|
|
if(AI_SUCCESS == material->Get(AI_MATKEY_SHININESS, shininess))
|
|
|
|
sub->material.shininess = (int) shininess;
|
|
|
|
|
2021-01-20 14:54:54 -06:00
|
|
|
return sub;
|
2021-01-19 16:36:10 -06:00
|
|
|
}
|
|
|
|
|
2021-01-27 15:15:40 -06:00
|
|
|
Color aiColor3D2Color(aiColor3D aicolor) {
|
2021-01-22 18:44:43 -06:00
|
|
|
Color color;
|
|
|
|
color.r = aicolor.r;
|
|
|
|
color.g = aicolor.g;
|
|
|
|
color.b = aicolor.b;
|
|
|
|
return color;
|
|
|
|
}
|