From eefd79572cc521e5f6a0e2ddb3135ed8de925e70 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 22 Feb 2015 15:18:06 +0000 Subject: Fixed valgrind warnings. --- src/model/mapfile.cc | 2 +- src/model/objfile.cc | 144 +++++++++++++++++++++++++++++++++++++++++---------- src/model/objfile.h | 6 +++ 3 files changed, 123 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/model/mapfile.cc b/src/model/mapfile.cc index 05cea2a..3adcbb0 100644 --- a/src/model/mapfile.cc +++ b/src/model/mapfile.cc @@ -46,7 +46,7 @@ public: delete[] matrix_values[i]; } - delete matrix_values; + delete[] matrix_values; } T * operator[](size_t row) diff --git a/src/model/objfile.cc b/src/model/objfile.cc index e4cec92..49a691f 100644 --- a/src/model/objfile.cc +++ b/src/model/objfile.cc @@ -38,27 +38,27 @@ OBJFile::OBJFile(std::string const &name) OBJFile::~OBJFile() { - for (VertexList::iterator it = obj_vertexlist.begin(); it != obj_vertexlist.end(); it++) { + for (VertexList::iterator it = obj_vertexlist.begin(); it != obj_vertexlist.end(); ++it) { delete(*it).second; } obj_vertexlist.clear(); - for (NormalList::iterator it = obj_normallist.begin(); it != obj_normallist.end(); it++) { + for (NormalList::iterator it = obj_normallist.begin(); it != obj_normallist.end(); ++it) { delete(*it).second; } obj_normallist.clear(); - for (UVList::iterator it = obj_uvlist.begin(); it != obj_uvlist.end(); it++) { + for (UVList::iterator it = obj_uvlist.begin(); it != obj_uvlist.end(); ++it) { delete(*it).second; } obj_uvlist.clear(); - for (TriList::iterator it = obj_trilist.begin(); it != obj_trilist.end(); it++) { + for (TriList::iterator it = obj_trilist.begin(); it != obj_trilist.end(); ++it) { delete(*it).second; } obj_trilist.clear(); - for (QuadList::iterator it = obj_quadlist.begin(); it != obj_quadlist.end(); it++) { + for (QuadList::iterator it = obj_quadlist.begin(); it != obj_quadlist.end(); ++it) { delete(*it).second; } obj_quadlist.clear(); @@ -67,6 +67,45 @@ OBJFile::~OBJFile() objfile_ifs.close(); } +math::Vector3f * OBJFile::find_vertex(size_t index) +{ + VertexList::iterator it = obj_vertexlist.find(index); + if (it == obj_vertexlist.end()) + { + return 0; + } + else + { + return (*it).second; + } +} + +math::Vector3f *OBJFile::find_normal(size_t index) +{ + NormalList::iterator it = obj_normallist.find(index); + if (it == obj_normallist.end()) + { + return 0; + } + else + { + return (*it).second; + } +} + +math::Vector2f *OBJFile::find_uv(size_t index) +{ + UVList::iterator it = obj_uvlist.find(index); + if (it == obj_uvlist.end()) + { + return 0; + } + else + { + return (*it).second; + } +} + bool OBJFile::read() { char data[1024]; @@ -143,6 +182,15 @@ bool OBJFile::read() bool have_vn[4], have_uv[4]; size_t nslash; + + for (size_t j = 0; j < 4; j++) + { + v[j] = 0; + uv[j] = 0; + vn[j] = 0; + have_vn[j] = false; + have_uv[j] = false; + } std::string strbuf(line.str()); strbuf.erase(0,2); //remove "f " @@ -194,32 +242,42 @@ bool OBJFile::read() continue; } - math::Vector3f *v0 = obj_vertexlist.find(v[0]-1)->second; - math::Vector3f *v1 = obj_vertexlist.find(v[1]-1)->second; - math::Vector3f *v2 = obj_vertexlist.find(v[2]-1)->second; - math::Vector3f *v3 = obj_vertexlist.find(v[3]-1)->second; - - math::Vector3f *n0 = obj_normallist[vn[0]-1]; - math::Vector3f *n1 = obj_normallist[vn[1]-1]; - math::Vector3f *n2 = obj_normallist[vn[2]-1]; - math::Vector3f *n3 = obj_normallist[vn[3]-1]; - - math::Vector2f *t0 = obj_uvlist.find(uv[0]-1)->second; - math::Vector2f *t1 = obj_uvlist.find(uv[1]-1)->second; - math::Vector2f *t2 = obj_uvlist.find(uv[2]-1)->second; - math::Vector2f *t3 = obj_uvlist.find(uv[3]-1)->second; - if(points.size() == 3) { + math::Vector3f *v0 = find_vertex(v[0]-1); + math::Vector3f *v1 = find_vertex(v[1]-1); + math::Vector3f *v2 = find_vertex(v[2]-1); + + + math::Vector3f *n0 = find_normal(vn[0]-1); + math::Vector3f *n1 = find_normal(vn[1]-1); + math::Vector3f *n2 = find_normal(vn[2]-1); + + math::Vector2f *t0 = find_uv(uv[0]-1); + math::Vector2f *t1 = find_uv(uv[1]-1); + math::Vector2f *t2 = find_uv(uv[2]-1); + if(!v0 || !v1 || !v2) { con_warn << objfile_name << " invalid vertex called from line " << index_line << std::endl; continue; } - + Triangle *triangle = new Triangle(*v0, *v1, *v2); - if(!n0) n0 = new math::Vector3f(normal(triangle->v0(), triangle->v1(), triangle->v2())); - if(!n1) n1 = new math::Vector3f(normal(triangle->v1(), triangle->v2(), triangle->v0())); - if(!n2) n2 = new math::Vector3f(normal(triangle->v2(), triangle->v0(), triangle->v1())); + if(!n0) + { + n0 = new math::Vector3f(normal(triangle->v0(), triangle->v1(), triangle->v2())); + obj_normallist[vn[0]-1] = n0; + } + if(!n1) + { + n1 = new math::Vector3f(normal(triangle->v1(), triangle->v2(), triangle->v0())); + obj_normallist[vn[1]-1] = n1; + } + if(!n2) + { + n2 = new math::Vector3f(normal(triangle->v2(), triangle->v0(), triangle->v1())); + obj_normallist[vn[2]-1] = n2; + } triangle->n0().assign(n0->x(), n0->y(), n0->z()); triangle->n1().assign(n1->x(), n1->y(), n1->z()); @@ -233,7 +291,21 @@ bool OBJFile::read() obj_trilist.push_back(poly); } else if(points.size() == 4) { + math::Vector3f *v0 = find_vertex(v[0]-1); + math::Vector3f *v1 = find_vertex(v[1]-1); + math::Vector3f *v2 = find_vertex(v[2]-1); + math::Vector3f *v3 = find_vertex(v[3]-1); + + math::Vector3f *n0 = find_normal(vn[0]-1); + math::Vector3f *n1 = find_normal(vn[1]-1); + math::Vector3f *n2 = find_normal(vn[2]-1); + math::Vector3f *n3 = find_normal(vn[3]-1); + math::Vector2f *t0 = find_uv(uv[0]-1); + math::Vector2f *t1 = find_uv(uv[1]-1); + math::Vector2f *t2 = find_uv(uv[2]-1); + math::Vector2f *t3 = find_uv(uv[3]-1); + if(!v0 || !v1 || !v2 || !v3) { con_warn << objfile_name << " invalid vertex called from line " << index_line << std::endl; continue; @@ -241,10 +313,26 @@ bool OBJFile::read() Quad *quad = new Quad(*v0, *v1, *v2, *v3, zero); - if(!n0) n0 = new math::Vector3f(normal(quad->v0(), quad->v1(), quad->v3())); - if(!n1) n1 = new math::Vector3f(normal(quad->v1(), quad->v2(), quad->v0())); - if(!n2) n2 = new math::Vector3f(normal(quad->v2(), quad->v3(), quad->v1())); - if(!n3) n3 = new math::Vector3f(normal(quad->v3(), quad->v0(), quad->v2())); + if(!n0) + { + n0 = new math::Vector3f(normal(quad->v0(), quad->v1(), quad->v3())); + obj_normallist[vn[0]-1] = n0; + } + if(!n1) + { + n1 = new math::Vector3f(normal(quad->v1(), quad->v2(), quad->v0())); + obj_normallist[vn[1]-1] = n1; + } + if(!n2) + { + n2 = new math::Vector3f(normal(quad->v2(), quad->v3(), quad->v1())); + obj_normallist[vn[2]-1] = n2; + } + if(!n3) + { + n2 = new math::Vector3f(normal(quad->v3(), quad->v0(), quad->v2())); + obj_normallist[vn[3]-1] = n3; + } quad->n0().assign(n0->x(), n0->y(), n0->z()); quad->n1().assign(n1->x(), n1->y(), n1->z()); diff --git a/src/model/objfile.h b/src/model/objfile.h index 4e7f0c7..7db90bc 100644 --- a/src/model/objfile.h +++ b/src/model/objfile.h @@ -90,6 +90,12 @@ private: return obj_box; } + math::Vector3f *find_vertex(size_t index); + + math::Vector3f *find_normal(size_t index); + + math::Vector2f *find_uv(size_t index); + std::string objfile_name; filesystem::IFileStream objfile_ifs; -- cgit v1.2.3