separated mesh data from Mesh class

This commit is contained in:
devZoGok
2022-03-15 07:25:20 +02:00
parent 8551823dd8
commit 993dddb130
10 changed files with 144 additions and 121 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ cmake_policy(SET CMP0015 NEW)
set(GUI text.cpp)
set(UTILS util.cpp)
set(MATH quaternion.cpp vector.cpp matrix.cpp ray.cpp)
set(RENDER lineRenderer.cpp particleEmitter.cpp camera.cpp light.cpp material.cpp mesh.cpp model.cpp node.cpp quad.cpp box.cpp root.cpp shader.cpp texture.cpp)
set(RENDER lineRenderer.cpp particleEmitter.cpp camera.cpp light.cpp material.cpp mesh.cpp meshData.cpp model.cpp node.cpp quad.cpp box.cpp root.cpp shader.cpp texture.cpp)
set(ANIM animationController.cpp animationChannel.cpp animation.cpp animatable.cpp keyframeChannel.cpp driver.cpp)
set(ARMATURE skeleton.cpp bone.cpp ikSolver.cpp)
set(ASSET_READERS abstractAssetReader.cpp imageReader.cpp fontReader.cpp modelReader.cpp assimpModelReader.cpp xmlModelReader.cpp)
+10 -5
View File
@@ -12,10 +12,12 @@ namespace vb01{
AssimpModelReader::AssimpModelReader(Model *model, string path) : ModelReader(model, path){
Importer importer;
const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals | aiProcess_CalcTangentSpace);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode){
cout<<"Failed to load model:"<<importer.GetErrorString()<<endl;
exit(-1);
}
processNode(scene->mRootNode, scene, model);
}
@@ -24,6 +26,7 @@ namespace vb01{
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
processMesh(mesh, scene, currentNode);
}
for(int i = 0;i < node->mNumChildren; i++){
Node *childNode = new Node();
currentNode->attachChild(childNode);
@@ -33,10 +36,11 @@ namespace vb01{
void AssimpModelReader::processMesh(aiMesh *mesh, const aiScene *scene, Node *currentNode){
const int numTris = mesh->mNumFaces;
Mesh::Vertex *vertices = new Mesh::Vertex[3 * numTris];
MeshData::Vertex *vertices = new MeshData::Vertex[3 * numTris];
u32 *indices = new u32[3 * numTris];
for(int i = 0; i < 3 * numTris; i++){
Mesh::Vertex v;
MeshData::Vertex v;
v.pos.x = mesh->mVertices[i].x;
v.pos.y = mesh->mVertices[i].y;
v.pos.z = mesh->mVertices[i].z;
@@ -61,18 +65,19 @@ namespace vb01{
v.uv = Vector2::VEC_ZERO;
vertices[i] = v;
}
for(int i = 0; i < numTris; i++){
int numFaceIndices = mesh->mFaces[i].mNumIndices;
for(int j = 0; j < numFaceIndices; j++){
for(int j = 0; j < numFaceIndices; j++)
indices[numFaceIndices * i + j] = mesh->mFaces[i].mIndices[j];
}
}
/*
if(mesh->mMaterialIndex>=0){
aiMaterial *material=scene->mMaterial[mesh->mMaterialIndex];
}
*/
Mesh *vbMesh = new Mesh(vertices, indices, numTris);
Mesh *vbMesh = new Mesh(MeshData(vertices, indices, numTris));
vbMesh ->construct();
currentNode->attachMesh(vbMesh);
}
+12 -8
View File
@@ -2,19 +2,13 @@
namespace vb01{
Box::Box(Vector3 size){
this->staticVerts = false;
numTris = 12;
indices = new u32[3 * numTris];
vertices = new Vertex[3 * numTris];
setSize(size);
construct();
}
void Box::setSize(Vector3 size){
this->size = size;
Vector3 pos[] = {
Vector3(size.x / 2, size.y / 2, size.z / 2),
Vector3(-size.x / 2, size.y / 2, size.z / 2),
@@ -26,6 +20,7 @@ namespace vb01{
Vector3(-size.x / 2, -size.y / 2, -size.z / 2),
Vector3(size.x / 2, -size.y / 2, -size.z / 2)
};
Vector3 norm[] = {
Vector3(0, 0, 1),
Vector3(0, 1, 0),
@@ -35,12 +30,14 @@ namespace vb01{
Vector3(0, -1, 0),
Vector3(-1, 0, 0)
};
Vector2 tex[] = {
Vector2(1, 1),
Vector2(0, 1),
Vector2(0, 0),
Vector2(1, 0)
};
unsigned int data[] = {
1, 3, 2, 4, 3, 0, 0, 3, 1,
4, 3, 0, 1, 3, 2, 5, 3, 3,
@@ -60,13 +57,20 @@ namespace vb01{
5, 5, 2, 6, 5, 1, 4, 5, 3,
7, 5, 0, 4, 5, 3, 6, 5, 1
};
int numTris = 12;
u32 *indices = new u32[3 * numTris];
MeshData::Vertex *vertices = new MeshData::Vertex[3 * numTris];
for(int i = 0; i < 3 * numTris; i++){
Vertex v;
MeshData::Vertex v;
v.pos = pos[data[3 * i]];
v.norm = norm[data[3 * i + 1]];
v.uv = tex[data[3 * i + 2]];
vertices[i] = v;
indices[i] = i;
}
meshBase = MeshData(vertices, indices, 3 * numTris);
}
}
+25 -59
View File
@@ -17,31 +17,8 @@ using namespace std;
using namespace glm;
namespace vb01{
void Mesh::ShapeKey::animate(float value, KeyframeChannel keyframeChannel){
switch(keyframeChannel.type){
case KeyframeChannel::SHAPE_KEY_MIN:
this->minValue = value;
break;
case KeyframeChannel::SHAPE_KEY_VALUE:
this->value = value;
break;
case KeyframeChannel::SHAPE_KEY_MAX:
this->maxValue = value;
break;
}
}
Mesh::Mesh(){}
Mesh::Mesh(Vertex *vertices, unsigned int *indices, int numTris, string *vertexGroups, int numVertexGroups, ShapeKey *shapeKeys, int numShapeKeys, string name){
this->vertices = vertices;
this->indices = indices;
this->numTris = numTris;
this->vertexGroups = vertexGroups;
this->numVertexGroups = numVertexGroups;
this->shapeKeys = shapeKeys;
this->numShapeKeys = numShapeKeys;
this->name = name;
Mesh::Mesh(MeshData meshBase){
this->meshBase = meshBase;
}
Mesh::~Mesh(){
@@ -54,9 +31,6 @@ namespace vb01{
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &EBO);
glDeleteBuffers(1, &VBO);
delete[] indices;
delete[] vertices;
}
void Mesh::construct(){
@@ -99,31 +73,31 @@ namespace vb01{
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
u32 size = sizeof(Vertex);
u32 size = sizeof(MeshData::Vertex);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, 3 * numTris * size, staticVerts ? vertices : NULL, staticVerts ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, 3 * meshBase.numTris * size, meshBase.vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * numTris * sizeof(u32), staticVerts ? indices : NULL, staticVerts ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * meshBase.numTris * sizeof(u32), meshBase.indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, size, (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(Vertex, norm)));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, norm)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(Vertex, uv)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, uv)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(Vertex, tan)));
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, tan)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(Vertex, biTan)));
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, biTan)));
glEnableVertexAttribArray(4);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(Vertex, weights)));
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, weights)));
glEnableVertexAttribArray(5);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(Vertex, boneIndices)));
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, boneIndices)));
glEnableVertexAttribArray(6);
for(int i = 0; i < numShapeKeys; i++){
glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(Vertex, shapeKeyOffsets) + 3 * i * sizeof(float)));
for(int i = 0; i < meshBase.numShapeKeys; i++){
glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, shapeKeyOffsets) + 3 * i * sizeof(float)));
glEnableVertexAttribArray(7 + i);
}
}
@@ -163,7 +137,7 @@ namespace vb01{
if(skeleton)
updateSkeleton(shader);
if(numShapeKeys > 0)
if(meshBase.numShapeKeys > 0)
updateShapeKeys(shader);
if(reflect)
@@ -187,22 +161,22 @@ namespace vb01{
}
void Mesh::updateShapeKeys(Shader *shader){
shader->editShader(Shader::VERTEX_SHADER, 5, "const int numShapeKeys=" + to_string(numShapeKeys) + ";");
shader->editShader(Shader::VERTEX_SHADER, 5, "const int numShapeKeys=" + to_string(meshBase.numShapeKeys) + ";");
for(int i = 0; i < numShapeKeys; i++){
if(shapeKeys[i].value < shapeKeys[i].minValue)
shapeKeys[i].value = shapeKeys[i].minValue;
else if(shapeKeys[i].value > shapeKeys[i].maxValue)
shapeKeys[i].value = shapeKeys[i].maxValue;
for(int i = 0; i < meshBase.numShapeKeys; i++){
if(meshBase.shapeKeys[i].value < meshBase.shapeKeys[i].minValue)
meshBase.shapeKeys[i].value = meshBase.shapeKeys[i].minValue;
else if(meshBase.shapeKeys[i].value > meshBase.shapeKeys[i].maxValue)
meshBase.shapeKeys[i].value = meshBase.shapeKeys[i].maxValue;
shader->setFloat(shapeKeys[i].value, "shapeKeyFactors[" + to_string(i) + "]");
shader->setFloat(meshBase.shapeKeys[i].value, "shapeKeyFactors[" + to_string(i) + "]");
}
}
void Mesh::updateSkeleton(Shader *shader){
skeleton->update();
shader->editShader(Shader::VERTEX_SHADER, 2, "const int numBones=" + to_string(skeleton->getNumBones()) + ";");
shader->editShader(Shader::VERTEX_SHADER, 3, "const int numVertGroups=" + to_string(numVertexGroups) + ";");
shader->editShader(Shader::VERTEX_SHADER, 3, "const int numVertGroups=" + to_string(meshBase.numVertexGroups) + ";");
Node *modelNode = skeleton->getRootBone()->getParent();
for(int i = 0; i < skeleton->getNumBones(); i++){
@@ -215,8 +189,8 @@ namespace vb01{
int vertGroupId = -1;
for(int j = 0; j < numVertexGroups; j++)
if(bone->getName() == vertexGroups[j])
for(int j = 0; j < meshBase.numVertexGroups; j++)
if(bone->getName() == meshBase.vertexGroups[j])
vertGroupId = j;
int parentId = -1;
@@ -429,15 +403,7 @@ namespace vb01{
void Mesh::render(){
glBindVertexArray(VAO);
if(!staticVerts){
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * numTris * sizeof(Vertex), vertices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, 3 * numTris * sizeof(u32), indices);
}
glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL);
glDrawElements(GL_TRIANGLES, 3 * numTris, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, 3 * meshBase.numTris, GL_UNSIGNED_INT, 0);
}
}
+8 -33
View File
@@ -5,6 +5,8 @@
#include "util.h"
#include "material.h"
#include "animatable.h"
#include "meshData.h"
#include <vector>
#include <string>
@@ -16,22 +18,7 @@ namespace vb01{
class Mesh{
public:
struct ShapeKey : public Animatable{
std::string name;
float minValue, value, maxValue;
void animate(float, KeyframeChannel);
};
struct Vertex{
Vector3 pos, norm, tan, biTan;
Vector2 uv;
float weights[4]{0, 0, 0, 0};
int boneIndices[4]{-1, -1, -1, -1};
Vector3 shapeKeyOffsets[100];
};
Mesh(Vertex*, u32*, int, std::string *vg = nullptr, int = 0, ShapeKey *sk = nullptr, int = 0, std::string = "");
Mesh(MeshData);
~Mesh();
void construct();
virtual void update();
@@ -45,22 +32,14 @@ namespace vb01{
inline void setSkeleton(Skeleton *sk){this->skeleton = sk;}
inline Node* getNode(){return node;}
inline Material* getMaterial(){return material;}
inline std::vector<Mesh*>& getMeshes(){return meshes;}
inline bool isReflective(){return reflective;}
inline bool isCastShadow(){return castShadow;}
inline int getNumVerts(){return 3 * numTris;}
inline Vertex* getVerts(){return vertices;}
inline std::string getName(){return name;}
inline Skeleton* getSkeleton(){return skeleton;}
inline std::string* getVertexGroups(){return vertexGroups;}
inline int getNumVertexGroups(){return numVertexGroups;}
inline u32* getIndices(){return indices;}
inline ShapeKey& getShapeKey(int i){return shapeKeys[i];}
inline int getNumShapeKeys(){return numShapeKeys;}
inline Texture* getPrefilterMap(){return prefilterMap;}
inline Texture* getIrradianceMap(){return irradianceMap;}
inline Texture* getPostfilterMap(){return postfilterMap;}
inline Texture* getBrdfIntegrationMap(){return brdfIntegrationMap;}
inline MeshData getMeshBase(){return meshBase;}
private:
void initMesh();
void initFramebuffer(u32&, u32&, int);
@@ -82,17 +61,13 @@ namespace vb01{
Material *material = nullptr;
Node *node = nullptr;
std::vector<Mesh*> meshes;
std::string name = "";
Skeleton *skeleton = nullptr;
protected:
Mesh();
Mesh(){}
bool staticVerts = true, castShadow = false, reflect = false, reflective = false, wireframe = false;
Vertex *vertices;
std::string *vertexGroups = nullptr;
ShapeKey *shapeKeys = nullptr;
u32 *indices, VAO, VBO, EBO;
int numTris, numVertexGroups = 0, numShapeKeys = 0;
MeshData meshBase;
bool castShadow = false, reflect = false, reflective = false, wireframe = false;
u32 VAO, VBO, EBO;
};
}
+29
View File
@@ -0,0 +1,29 @@
#include "meshData.h"
using namespace std;
namespace vb01{
void MeshData::ShapeKey::animate(float value, KeyframeChannel keyframeChannel){
switch(keyframeChannel.type){
case KeyframeChannel::SHAPE_KEY_MIN:
this->minValue = value;
break;
case KeyframeChannel::SHAPE_KEY_VALUE:
this->value = value;
break;
case KeyframeChannel::SHAPE_KEY_MAX:
this->maxValue = value;
break;
}
}
MeshData::MeshData(Vertex *vertices, u32 *indices, int numTris, string *vertexGroups, int numVertexGroups, ShapeKey *shapeKeys, int numShapeKeys){
this->vertices = vertices;
this->indices = indices;
this->numTris = numTris;
this->vertexGroups = vertexGroups;
this->numVertexGroups = numVertexGroups;
this->shapeKeys = shapeKeys;
this->numShapeKeys = numShapeKeys;
}
}
+39
View File
@@ -0,0 +1,39 @@
#ifndef MESH_BASE_H
#define MESH_BASE_H
#include <string>
#include "vector.h"
#include "util.h"
#include "keyframeChannel.h"
#include "animatable.h"
namespace vb01{
struct MeshData{
struct ShapeKey : public Animatable{
std::string name;
float minValue, value, maxValue;
void animate(float, KeyframeChannel);
};
struct Vertex{
Vector3 pos, norm, tan, biTan;
Vector2 uv;
float weights[4]{0, 0, 0, 0};
int boneIndices[4]{-1, -1, -1, -1};
Vector3 shapeKeyOffsets[100];
};
Vertex *vertices;
std::string *vertexGroups = nullptr;
ShapeKey *shapeKeys = nullptr;
u32 *indices, VAO, VBO, EBO;
int numTris, numVertexGroups = 0, numShapeKeys = 0;
MeshData(){}
MeshData(Vertex*, u32*, int, std::string *vg = nullptr, int = 0, ShapeKey *sk = nullptr, int = 0);
};
}
#endif
-1
View File
@@ -11,7 +11,6 @@
#include <string>
namespace vb01{
class Mesh;
class Model;
class Light;
class ParticleEmitter;
+10 -11
View File
@@ -1,24 +1,18 @@
#include "quad.h"
namespace vb01{
Quad::Quad(Vector3 size, bool spatial) : Mesh(){
Quad::Quad(Vector3 size, bool spatial){
this->spatial = spatial;
this->staticVerts = false;
int numPolyVerts = 3;
numTris = 2;
indices = new u32[numTris * numPolyVerts];
vertices = new Vertex[numTris * numPolyVerts];
setSize(size);
Mesh::construct();
}
void Quad::setSize(Vector3 size){
this->size = size;
Vertex v1, v2, v3, v4, v5, v6;
MeshData::Vertex v1, v2, v3, v4, v5, v6;
if(spatial){
v1.pos = Vector3(size.x / 2, size.y / 2, 0);
v2.pos = Vector3(-size.x / 2, size.y / 2, 0);
@@ -35,6 +29,7 @@ namespace vb01{
v5.pos = Vector3(size.x, size.y, 0);
v6.pos = Vector3(size.x, 0, 0);
}
v1.norm = Vector3(0, 0, -1);
v1.uv = Vector2(1, 1);
@@ -52,7 +47,9 @@ namespace vb01{
v6.norm = Vector3(0, 0, -1);
v6.uv = Vector2(1, 1);
int numTris = 2;
u32 *indices = new u32[numTris * 3];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
@@ -60,12 +57,14 @@ namespace vb01{
indices[4] = 4;
indices[5] = 5;
MeshData::Vertex *vertices = new MeshData::Vertex[numTris * 3];
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
vertices[3] = v4;
vertices[4] = v5;
vertices[5] = v6;
}
meshBase = MeshData(vertices, indices, numTris * 3);
}
}
+10 -3
View File
@@ -9,6 +9,7 @@ using namespace std;
namespace vb01{
void Ray::retrieveCollisions(Vector3 rayPos, Vector3 rayDir, Node *node, std::vector<CollisionResult> &results, const float rayLength){
castRay(rayPos, rayDir, node, results, rayLength);
for(Node *c : node->getChildren())
retrieveCollisions(rayPos, rayDir, c, results, rayLength);
}
@@ -16,24 +17,29 @@ namespace vb01{
void Ray::castRay(Vector3 rayPos, Vector3 rayDir, Node *node, vector<CollisionResult> &results, float rayLength){
Vector3 pos = node->localToGlobalPosition(Vector3::VEC_ZERO);
Quaternion rot = node->localToGlobalOrientation(Quaternion::QUAT_W);
for(Mesh *m : node->getMeshes()){
const int numVerts = m->getNumVerts();
Mesh::Vertex *vertices = m->getVerts();
u32 *indices = m->getIndices();
const int numVerts = m->getMeshBase().numTris * 3;
MeshData::Vertex *vertices = m->getMeshBase().vertices;
u32 *indices = m->getMeshBase().indices;
for(int i = 0; i < numVerts / 3; i++){
Vector3 pointA = pos + rot * vertices[indices[i * 3]].pos, pointB = pos + rot * vertices[indices[i * 3 + 1]].pos, pointC = pos + rot * vertices[indices[i * 3 + 2]].pos;
Vector3 hypVec = pointA - rayPos;
Vector3 perpVec = (pointB - pointA).cross(pointC - pointA);
float a1 = hypVec.norm().getAngleBetween(perpVec.norm());
if(a1 > PI / 2){
a1 = PI - a1;
perpVec = -perpVec;
}
float perpLine = hypVec.getLength() * cos(a1);
float a2 = perpVec.norm().getAngleBetween(rayDir.norm());
if(a2 <= PI / 2){
float distance = perpLine / cos(a2);
if((distance <= rayLength && rayLength != .0) || rayLength == .0){
Vector3 contactPoint = rayPos + rayDir.norm() * distance;
float angleA = (pointB - pointA).norm().getAngleBetween((pointC - pointA).norm());
@@ -45,6 +51,7 @@ namespace vb01{
bool withinBisecA = (contactPoint - pointA).norm().getAngleBetween(bisecAVec.norm()) <= angleA / 2;
bool withinBisecB = (contactPoint - pointB).norm().getAngleBetween(bisecBVec.norm()) <= angleB / 2;
bool withinBisecC = (contactPoint - pointC).norm().getAngleBetween(bisecCVec.norm()) <= angleC / 2;
if(withinBisecA && withinBisecB && withinBisecC){
CollisionResult result;
result.pos = contactPoint;