mirror of
https://github.com/devZoGok/vb01.git
synced 2026-08-26 19:43:30 +00:00
fixed bug when deallocating memory from models with multiple children
This commit is contained in:
@@ -15,6 +15,8 @@ namespace vb01{
|
||||
inline Vector3 getDirection(){return direction;}
|
||||
inline Vector3 getLeft(){return left;}
|
||||
inline Vector3 getUp(){return up;}
|
||||
inline void setNearPlane(float near){this->nearPlane=near;}
|
||||
inline void setFarPlane(float far){this->farPlane=far;}
|
||||
inline float getFov(){return fov;}
|
||||
inline float getNearPlane(){return nearPlane;}
|
||||
inline float getFarPlane(){return farPlane;}
|
||||
|
||||
@@ -57,17 +57,14 @@ namespace vb01{
|
||||
std::vector<Material*> materials;
|
||||
rootNode->getDescendants(rootNode,descendants);
|
||||
descendants.push_back(rootNode);
|
||||
int numLights=0,thisId=-1;
|
||||
int thisId=-1;
|
||||
float fov=cam->getFov(),width=root->getWidth(),height=root->getHeight();
|
||||
|
||||
for(Node *d : descendants){
|
||||
std::vector<Light*> lights=d->getLights();
|
||||
for(Light *l : lights){
|
||||
if(l){
|
||||
numLights++;
|
||||
if(l==this)
|
||||
thisId=numLights-1;
|
||||
}
|
||||
for(int i=0;i<lights.size();i++){
|
||||
if(lights[i]==this)
|
||||
thisId=i;
|
||||
}
|
||||
for(Mesh *m : d->getMeshes())
|
||||
materials.push_back(m->getMaterial());
|
||||
@@ -130,6 +127,7 @@ namespace vb01{
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER,*(root->getFBO()));
|
||||
glViewport(0,0,root->getWidth(),root->getHeight());
|
||||
|
||||
for(Material *m : materials){
|
||||
Shader *shader=m->getShader();
|
||||
shader->use();
|
||||
@@ -138,13 +136,15 @@ namespace vb01{
|
||||
shader->setFloat(nearPlane,"light["+to_string(thisId)+"].near");
|
||||
shader->setFloat(farPlane,"light["+to_string(thisId)+"].far");
|
||||
shader->setInt(0,"diffuseMap");
|
||||
//shader->setInt(1,"normalMap");
|
||||
shader->setInt(1,"normalMap");
|
||||
shader->setInt(2,"specularMap");
|
||||
shader->setInt(3,"parallaxMap");
|
||||
shader->setInt(4,"environmentMap");
|
||||
shader->setInt(5,"light["+to_string(thisId)+"].depthMapCube");
|
||||
shader->setInt(6,"light["+to_string(thisId)+"].depthMap");
|
||||
depthMap->select(type==POINT?5:6);
|
||||
/*
|
||||
*/
|
||||
|
||||
switch(type){
|
||||
case POINT:
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace vb01{
|
||||
inline float getOuterAngle(){return outerAngle;}
|
||||
inline float getShadowNearPlane(){return nearPlane;}
|
||||
inline float getShadowFarPlane(){return farPlane;}
|
||||
inline Node* getNode(){return node;}
|
||||
private:
|
||||
Type type;
|
||||
Shader *depthMapShader;
|
||||
|
||||
+2
-2
@@ -1,5 +1,6 @@
|
||||
#include"material.h"
|
||||
#include"shader.h"
|
||||
#include"root.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -51,8 +52,7 @@ namespace vb01{
|
||||
shader->use();
|
||||
if(type==MATERIAL_2D){
|
||||
shader->setBool(lightingEnabled,"lightingEnabled");
|
||||
if(normalMapEnabled)
|
||||
shader->setBool(normalMapEnabled,"normalMapEnabled");
|
||||
shader->setBool(normalMapEnabled,"normalMapEnabled");
|
||||
}
|
||||
shader->setBool(texturingEnabled,"texturingEnabled");
|
||||
if(type==MATERIAL_GUI){
|
||||
|
||||
@@ -30,7 +30,8 @@ namespace vb01{
|
||||
for(Mesh *m : meshes)
|
||||
delete m;
|
||||
|
||||
delete material;
|
||||
if(material)
|
||||
delete material;
|
||||
|
||||
glDeleteVertexArrays(1,&VAO);
|
||||
glDeleteBuffers(1,&EBO);
|
||||
@@ -163,7 +164,7 @@ namespace vb01{
|
||||
if(reflect){
|
||||
shader->setBool(reflect,"environmentMapEnabled");
|
||||
root->getSkybox()->getMaterial()->getDiffuseMap(0)->select(4);
|
||||
//environmentMap->select(4);
|
||||
environmentMap->select(4);
|
||||
}
|
||||
if(skeleton){
|
||||
}
|
||||
|
||||
@@ -70,143 +70,157 @@ namespace vb01{
|
||||
|
||||
Model::Model(string path,bool b) : Node(){
|
||||
if(b){
|
||||
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,this);
|
||||
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,this);
|
||||
}
|
||||
else{
|
||||
int numTris,numVerts,numGroups,v=0,u=0,vg=0,b=0;
|
||||
Mesh::Vertex *vertices;
|
||||
Mesh::VertexGroup *groups=new Mesh::VertexGroup[5];
|
||||
unsigned int *indices;
|
||||
Vector3 *pos,*norm;
|
||||
|
||||
ifstream in(path);
|
||||
string l;
|
||||
bool verts=false,uv=false,vertexGroups=false,bones=false;
|
||||
|
||||
while(getline(in,l)){
|
||||
if(l.substr(0,8)=="numFaces"){
|
||||
numTris=atoi(l.substr(9,string::npos).c_str());
|
||||
vertices=new Mesh::Vertex[numTris*3];
|
||||
indices=new unsigned int[numTris*3];
|
||||
}
|
||||
else if(l.substr(0,11)=="numVertices"){
|
||||
numVerts=atoi(l.substr(12,string::npos).c_str());
|
||||
pos=new Vector3[numVerts];
|
||||
norm=new Vector3[numVerts];
|
||||
}
|
||||
else if(l.substr(0,9)=="numGroups"){
|
||||
numGroups=atoi(l.substr(10,string::npos).c_str());
|
||||
groups=new Mesh::VertexGroup[numGroups];
|
||||
}
|
||||
if(l=="vertices:"){
|
||||
verts=true;
|
||||
continue;
|
||||
}
|
||||
else if(l=="uv:"){
|
||||
verts=false;
|
||||
uv=true;
|
||||
continue;
|
||||
}
|
||||
else if(l=="groups:"){
|
||||
uv=false;
|
||||
vertexGroups=true;
|
||||
continue;
|
||||
}
|
||||
else if(l=="bones:"){
|
||||
vertexGroups=false;
|
||||
bones=true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(verts||uv||vertexGroups||bones){
|
||||
int numCoords=1;
|
||||
if(verts)numCoords=6;
|
||||
else if(uv)numCoords=3;
|
||||
else if(vg)numCoords=3;
|
||||
else if(groups)numCoords=1;
|
||||
|
||||
int nextSpace=0;
|
||||
int *spaceIds=new int[numCoords-1];
|
||||
for(int i=0;i<l.length();i++)
|
||||
if(l.c_str()[i]==' '){
|
||||
spaceIds[nextSpace]=i;
|
||||
nextSpace++;
|
||||
}
|
||||
float *coords=new float[numCoords];
|
||||
for(int i=0;i<numCoords;i++){
|
||||
int firstChar,lastChar;
|
||||
if(i==0)
|
||||
firstChar=0,lastChar=spaceIds[i];
|
||||
else if(i==numCoords-1)
|
||||
firstChar=spaceIds[i-1],lastChar=string::npos;
|
||||
else
|
||||
firstChar=spaceIds[i-1],lastChar=spaceIds[i]-spaceIds[i-1];
|
||||
coords[i]=atof(l.substr(firstChar,lastChar).c_str());
|
||||
int numTris,numVerts,numGroups,v=0,u=0,vg=0,b=0;
|
||||
Mesh::Vertex *vertices;
|
||||
Mesh::VertexGroup *groups=new Mesh::VertexGroup[5];
|
||||
unsigned int *indices;
|
||||
Vector3 *pos,*norm;
|
||||
|
||||
ifstream in(path);
|
||||
string l;
|
||||
bool verts=false,uv=false,vertexGroups=false,bones=false;
|
||||
|
||||
while(getline(in,l)){
|
||||
if(l.substr(0,8)=="numFaces"){
|
||||
numTris=atoi(l.substr(9,string::npos).c_str());
|
||||
vertices=new Mesh::Vertex[numTris*3];
|
||||
indices=new unsigned int[numTris*3];
|
||||
}
|
||||
if(verts){
|
||||
pos[v]=Vector3(coords[0],coords[1],coords[2]);
|
||||
norm[v]=Vector3(coords[3],coords[4],coords[5]);
|
||||
//Vector2 uv=Vector2(coords[6],coords[7]);
|
||||
//vertices[v].pos=pos;
|
||||
//vertices[v].norm=norm;
|
||||
//vertices[v].texCoords=uv;
|
||||
v++;
|
||||
else if(l.substr(0,11)=="numVertices"){
|
||||
numVerts=atoi(l.substr(12,string::npos).c_str());
|
||||
pos=new Vector3[numVerts];
|
||||
norm=new Vector3[numVerts];
|
||||
}
|
||||
else if(uv){
|
||||
int id=(int)coords[0];
|
||||
Vector2 uv=Vector2(coords[1],coords[2]);
|
||||
vertices[u].pos=pos[id];
|
||||
vertices[u].norm=norm[id];
|
||||
vertices[u].texCoords=uv;
|
||||
indices[u]=u;
|
||||
u++;
|
||||
else if(l.substr(0,9)=="numGroups"){
|
||||
numGroups=atoi(l.substr(10,string::npos).c_str());
|
||||
groups=new Mesh::VertexGroup[numGroups];
|
||||
}
|
||||
else if(vertexGroups){
|
||||
if(l.c_str()[l.length()-1]==':'){
|
||||
numCoords=1;
|
||||
groups[vg].vertices=new Mesh::Vertex*;
|
||||
groups[vg].weights=new float;
|
||||
groups[vg].name=l.c_str()[l.length()-1];
|
||||
vg++;
|
||||
}
|
||||
else{
|
||||
numCoords=2;
|
||||
int *ids=new int,numVerts=0;
|
||||
for(int i=0;i<3*numTris;i++){
|
||||
if(vertices[i].pos==pos[(int)coords[0]]){
|
||||
ids[numVerts]=i;
|
||||
numVerts++;
|
||||
}
|
||||
if(l=="vertices:"){
|
||||
verts=true;
|
||||
continue;
|
||||
}
|
||||
else if(l=="uv:"){
|
||||
verts=false;
|
||||
uv=true;
|
||||
continue;
|
||||
}
|
||||
else if(l=="groups:"){
|
||||
uv=false;
|
||||
vertexGroups=true;
|
||||
continue;
|
||||
}
|
||||
else if(l=="bones:"){
|
||||
vertexGroups=false;
|
||||
bones=true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(verts||uv||vertexGroups||bones){
|
||||
int numCoords=1;
|
||||
if(verts)numCoords=6;
|
||||
else if(uv)numCoords=3;
|
||||
else if(vg)numCoords=3;
|
||||
else if(groups)numCoords=1;
|
||||
|
||||
int nextSpace=0;
|
||||
int *spaceIds=new int[numCoords-1];
|
||||
for(int i=0;i<l.length();i++)
|
||||
if(l.c_str()[i]==' '){
|
||||
spaceIds[nextSpace]=i;
|
||||
nextSpace++;
|
||||
}
|
||||
for(int i=0;i<3*numTris;i++)
|
||||
for(int j=0;j<numVerts;j++){
|
||||
groups[vg].vertices[groups[vg].numVertices]=&(vertices[ids[j]]);
|
||||
groups[vg].weights[ids[j]]=coords[1];
|
||||
}
|
||||
groups[vg].numVertices++;
|
||||
delete[] ids;
|
||||
float *coords=new float[numCoords];
|
||||
for(int i=0;i<numCoords;i++){
|
||||
int firstChar,lastChar;
|
||||
if(i==0)
|
||||
firstChar=0,lastChar=spaceIds[i];
|
||||
else if(i==numCoords-1)
|
||||
firstChar=spaceIds[i-1],lastChar=string::npos;
|
||||
else
|
||||
firstChar=spaceIds[i-1],lastChar=spaceIds[i]-spaceIds[i-1];
|
||||
coords[i]=atof(l.substr(firstChar,lastChar).c_str());
|
||||
}
|
||||
if(verts){
|
||||
pos[v]=Vector3(coords[0],coords[1],coords[2]);
|
||||
norm[v]=Vector3(coords[3],coords[4],coords[5]);
|
||||
//Vector2 uv=Vector2(coords[6],coords[7]);
|
||||
//vertices[v].pos=pos;
|
||||
//vertices[v].norm=norm;
|
||||
//vertices[v].texCoords=uv;
|
||||
v++;
|
||||
}
|
||||
else if(uv){
|
||||
int id=(int)coords[0];
|
||||
Vector2 uv=Vector2(coords[1],coords[2]);
|
||||
vertices[u].pos=pos[id];
|
||||
vertices[u].norm=norm[id];
|
||||
vertices[u].texCoords=uv;
|
||||
indices[u]=u;
|
||||
u++;
|
||||
}
|
||||
else if(vertexGroups){
|
||||
if(l.c_str()[l.length()-1]==':'){
|
||||
numCoords=1;
|
||||
groups[vg].vertices=new Mesh::Vertex*;
|
||||
groups[vg].weights=new float;
|
||||
groups[vg].name=l.c_str()[l.length()-1];
|
||||
vg++;
|
||||
}
|
||||
else{
|
||||
numCoords=2;
|
||||
int *ids=new int,numVerts=0;
|
||||
for(int i=0;i<3*numTris;i++){
|
||||
if(vertices[i].pos==pos[(int)coords[0]]){
|
||||
ids[numVerts]=i;
|
||||
numVerts++;
|
||||
}
|
||||
}
|
||||
for(int i=0;i<3*numTris;i++)
|
||||
for(int j=0;j<numVerts;j++){
|
||||
groups[vg].vertices[groups[vg].numVertices]=&(vertices[ids[j]]);
|
||||
groups[vg].weights[ids[j]]=coords[1];
|
||||
}
|
||||
groups[vg].numVertices++;
|
||||
delete[] ids;
|
||||
}
|
||||
}
|
||||
else if(bones){
|
||||
b++;
|
||||
}
|
||||
delete[] coords;
|
||||
}
|
||||
else if(bones){
|
||||
b++;
|
||||
}
|
||||
delete[] coords;
|
||||
}
|
||||
}
|
||||
delete[] pos;
|
||||
delete[] norm;
|
||||
attachChild(new Node());
|
||||
children[0]->attachMesh(new Mesh(vertices,indices,numTris));
|
||||
delete[] pos;
|
||||
delete[] norm;
|
||||
attachChild(new Node());
|
||||
children[0]->attachMesh(new Mesh(vertices,indices,numTris));
|
||||
}
|
||||
}
|
||||
|
||||
Model::~Model(){}
|
||||
Model::~Model(){
|
||||
delete children[0]->getMesh(0)->getMaterial();
|
||||
|
||||
vector<Node*> descendants;
|
||||
getDescendants(this,descendants);
|
||||
for(Node *node : descendants)
|
||||
for(Mesh *mesh : node->getMeshes())
|
||||
mesh->setMaterial(nullptr);
|
||||
|
||||
while(!children.empty()){
|
||||
Node *c=children[children.size()-1];
|
||||
dettachChild(c);
|
||||
delete c;
|
||||
}
|
||||
}
|
||||
|
||||
void Model::update(){
|
||||
Node::update();
|
||||
|
||||
@@ -17,10 +17,10 @@ namespace vb01{
|
||||
}
|
||||
|
||||
Node::~Node(){
|
||||
for(Light *l : lights)
|
||||
delete l;
|
||||
for(Mesh *m : meshes)
|
||||
delete m;
|
||||
for(Light *l : lights)
|
||||
delete l;
|
||||
for(ParticleEmitter *p : emitters)
|
||||
delete p;
|
||||
for(Text *t : texts)
|
||||
@@ -43,8 +43,6 @@ namespace vb01{
|
||||
c->update();
|
||||
for(ParticleEmitter *p : emitters)
|
||||
p->update();
|
||||
/*
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +57,8 @@ namespace vb01{
|
||||
for(int i=0;i<children.size()&&id==-1;i++)
|
||||
if(children[i]==child)
|
||||
id=i;
|
||||
children.erase(children.begin()+id);
|
||||
if(id!=-1)
|
||||
children.erase(children.begin()+id);
|
||||
}
|
||||
|
||||
void Node::attachMesh(Mesh *mesh){
|
||||
|
||||
+2
-19
@@ -14,8 +14,6 @@ using namespace glm;
|
||||
using namespace std;
|
||||
|
||||
namespace vb01{
|
||||
int n=0;
|
||||
|
||||
ParticleEmitter::ParticleEmitter(int numParticles){
|
||||
this->numParticles=numParticles;
|
||||
|
||||
@@ -101,12 +99,9 @@ namespace vb01{
|
||||
if(2*i+2+offset<numParticles&&(particles[i+offset]->d<particles[2*i+1+offset]->d||particles[i+offset]->d<particles[2*i+2+offset]->d)){
|
||||
bool leftChild=particles[2*i+1+offset]->d>particles[2*i+2+offset]->d;
|
||||
swap(particles[i+offset],leftChild?particles[2*i+1+offset]:particles[2*i+2+offset]);
|
||||
n++;
|
||||
}
|
||||
else if(2*i+2+offset==numParticles&&particles[i+offset]->d<particles[2*i+1+offset]->d){
|
||||
else if(2*i+2+offset==numParticles&&particles[i+offset]->d<particles[2*i+1+offset]->d)
|
||||
swap(particles[i+offset],particles[2*i+1+offset]);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
for(int i=0;2*i+1<numParticles;i++){
|
||||
if(2*i+2+offset<numParticles&&(particles[i+offset]->d<particles[2*i+1+offset]->d||particles[i+offset]->d<particles[2*i+2+offset]->d))
|
||||
@@ -120,10 +115,8 @@ namespace vb01{
|
||||
}
|
||||
|
||||
void ParticleEmitter::heapSort(){
|
||||
n=0;
|
||||
for(int i=0;i<numParticles;i++)
|
||||
makeHeap(i);
|
||||
cout<<n<<endl;
|
||||
}
|
||||
|
||||
void ParticleEmitter::update(){
|
||||
@@ -180,18 +173,8 @@ namespace vb01{
|
||||
particles[i]->d=cos(camDir.getAngleBetween((v0-camPos).norm()))*camPos.getDistanceFrom(v0);
|
||||
}
|
||||
heapSort();
|
||||
/*
|
||||
for(int i=0;i<numParticles;i++){
|
||||
for(int j=i;j<numParticles-1;j++){
|
||||
Vector3 v1=particles[j]->trans,v2=particles[j+1]->trans;
|
||||
float d1=cos(camDir.getAngleBetween((v1-camPos).norm()))*camPos.getDistanceFrom(v1);
|
||||
float d2=cos(camDir.getAngleBetween((v2-camPos).norm()))*camPos.getDistanceFrom(v2);
|
||||
if(d1<d2)
|
||||
swap(particles[j],particles[j+1]);
|
||||
}
|
||||
}
|
||||
*/
|
||||
glBindVertexArray(VAO);
|
||||
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
|
||||
glDrawElementsInstanced(GL_TRIANGLES,6,GL_UNSIGNED_INT,0,numParticles);
|
||||
for(int i=0;i<numParticles;i++){
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user