clray
diff src/mesh.cc @ 13:407935b73af3
bollocks
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 04 Aug 2010 04:51:06 +0100 |
parents | d9a1bab1c3f5 |
children | 29f9330cfa4b |
line diff
1.1 --- a/src/mesh.cc Tue Aug 03 13:06:59 2010 +0100 1.2 +++ b/src/mesh.cc Wed Aug 04 04:51:06 2010 +0100 1.3 @@ -115,6 +115,26 @@ 1.4 1.5 static map<string, int> matnames; 1.6 1.7 +bool Scene::add_mesh(Mesh *m) 1.8 +{ 1.9 + // make sure triangles have material ids 1.10 + for(size_t i=0; i<m->faces.size(); i++) { 1.11 + m->faces[i].matid = m->matid; 1.12 + } 1.13 + meshes.push_back(m); 1.14 + return true; 1.15 +} 1.16 + 1.17 +int Scene::get_num_faces() const 1.18 +{ 1.19 + int num_faces = 0; 1.20 + for(size_t i=0; i<meshes.size(); i++) { 1.21 + num_faces += meshes[i]->faces.size(); 1.22 + } 1.23 + printf("get_num_faces() = %d\n", num_faces); 1.24 + return num_faces; 1.25 +} 1.26 + 1.27 1.28 #define INVALID_IDX INT_MIN 1.29 1.30 @@ -200,7 +220,7 @@ 1.31 if(!obj.f.empty()) { 1.32 Mesh *mesh = cons_mesh(&obj); 1.33 mesh->matid = matnames[obj.cur_mat]; 1.34 - meshes.push_back(mesh); 1.35 + add_mesh(mesh); 1.36 obj_added++; 1.37 1.38 obj.f.clear(); // clean the face list 1.39 @@ -252,6 +272,7 @@ 1.40 mat.kr = 0.0; // TODO 1.41 mat.spow = vmtl[i].shininess; 1.42 1.43 + matlib.push_back(mat); 1.44 matnames[vmtl[i].name] = i; 1.45 } 1.46 } 1.47 @@ -309,7 +330,7 @@ 1.48 if(!obj.f.empty()) { 1.49 Mesh *mesh = cons_mesh(&obj); 1.50 mesh->matid = matnames[obj.cur_mat]; 1.51 - meshes.push_back(mesh); 1.52 + add_mesh(mesh); 1.53 obj_added++; 1.54 } 1.55 1.56 @@ -342,16 +363,24 @@ 1.57 face.v[j].pos[0] = obj->v[f->v[j]].x; 1.58 face.v[j].pos[1] = obj->v[f->v[j]].y; 1.59 face.v[j].pos[2] = obj->v[f->v[j]].z; 1.60 + face.v[j].pos[3] = 0.0; 1.61 1.62 int nidx = f->n[j] < 0 ? 0 : f->n[j]; 1.63 face.v[j].normal[0] = obj->vn[nidx].x; 1.64 face.v[j].normal[1] = obj->vn[nidx].y; 1.65 face.v[j].normal[2] = obj->vn[nidx].z; 1.66 + face.v[j].normal[3] = 0.0; 1.67 1.68 int tidx = f->t[j] < 0 ? 0 : f->t[j]; 1.69 face.v[j].tex[0] = obj->vt[tidx].x; 1.70 face.v[j].tex[1] = obj->vt[tidx].y; 1.71 } 1.72 + 1.73 + face.normal[0] = face.v[0].normal[0]; 1.74 + face.normal[1] = face.v[1].normal[1]; 1.75 + face.normal[2] = face.v[2].normal[2]; 1.76 + face.normal[3] = 0.0; 1.77 + 1.78 mesh->faces.push_back(face); 1.79 } 1.80 1.81 @@ -390,6 +419,7 @@ 1.82 case CMD_NEWMTL: 1.83 // add the previous material, and start a new one 1.84 if(mat.name.length() > 0) { 1.85 + printf("Adding material: %s\n", mat.name.c_str()); 1.86 vmtl->push_back(mat); 1.87 mat.reset(); 1.88 } 1.89 @@ -442,6 +472,7 @@ 1.90 } 1.91 1.92 if(mat.name.length() > 0) { 1.93 + printf("Adding material: %s\n", mat.name.c_str()); 1.94 vmtl->push_back(mat); 1.95 } 1.96 return true; 1.97 @@ -603,9 +634,10 @@ 1.98 end++; 1.99 } 1.100 1.101 - int sz = end - beg + 1; 1.102 - char *pathname = (char*)alloca(sz + fnamelen + 2); 1.103 - memcpy(pathname, beg, sz); 1.104 + int res_len = end - beg; 1.105 + char *pathname = (char*)alloca(res_len + fnamelen + 2); 1.106 + memcpy(pathname, beg, res_len); 1.107 + pathname[res_len] = 0; 1.108 strcat(pathname, "/"); 1.109 strcat(pathname, fname); 1.110 1.111 @@ -615,7 +647,7 @@ 1.112 return true; 1.113 } 1.114 1.115 - beg += sz; 1.116 + beg += res_len; 1.117 } 1.118 return false; 1.119 } 1.120 @@ -630,7 +662,7 @@ 1.121 strncpy(buf, str, PATH_MAX); 1.122 char *ptr = strrchr(buf, '/'); 1.123 1.124 - if(*ptr) *ptr = 0; 1.125 + if(ptr && *ptr) *ptr = 0; 1.126 } 1.127 return buf; 1.128 }