Changeset 1071 for GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshLoader.cpp
- Timestamp:
- 06/30/06 17:40:29 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshLoader.cpp
r1070 r1071 1381 1381 }; 1382 1382 1383 1384 class vertex_arranger_node 1383 class VertexArranger 1385 1384 { 1386 1385 public: 1387 int v, n, t; 1388 1389 vertex_arranger_node(void){ 1390 v=n=t=-1; 1391 } 1392 vertex_arranger_node(int iv, int in=-1, int it=-1){ 1393 v=iv; 1394 n=in; 1395 t=it; 1396 } 1397 1398 bool operator<(const vertex_arranger_node &vu) const { 1399 if (v<vu.v) return true; 1400 if (v>vu.v) return false; 1401 if (n<vu.n) return true; 1402 if (n>vu.n) return false; 1403 if (t<vu.t) return true; 1404 if (t>vu.t) return false; 1405 return false; 1406 } 1407 bool operator==(const vertex_arranger_node &vu) const { 1408 return (v==vu.v && v==vu.v && n==vu.n && n==vu.n && t==vu.t && t==vu.t); 1409 } 1386 VertexArranger(Geometry::Mesh *m){ 1387 mesh=m; 1388 current_submesh=0; 1389 current_tris=new int[m->mSubMeshCount]; 1390 for (int i=0; i<m->mSubMeshCount; i++) 1391 current_tris[i] = 0; 1392 } 1393 ~VertexArranger(void){ 1394 delete[] current_tris; 1395 } 1396 1397 void AddFace(int v1, int v2, int v3, int t1, int t2, int t3, int n1, int n2, int n3){ 1398 Geometry::SubMesh *submesh = mesh->mSubMesh + current_submesh; 1399 1400 vertex_arranger_node vertex1(v1,n1,t1); 1401 vertex_arranger_node vertex2(v2,n2,t2); 1402 vertex_arranger_node vertex3(v3,n3,t3); 1403 1404 int j = current_tris[current_submesh]; 1405 1406 std::map<vertex_arranger_node,int>::iterator res; 1407 res=vertex_map.find(vertex1); 1408 if (res==vertex_map.end()) 1409 { 1410 int val = vertex_map.size(); 1411 vertex_map[vertex1] = val; 1412 submesh->mIndex[j*3+0] = val; 1413 } 1414 else 1415 submesh->mIndex[j*3+0] = res->second; 1416 1417 res=vertex_map.find(vertex2); 1418 if (res==vertex_map.end()) 1419 { 1420 int val = vertex_map.size(); 1421 vertex_map[vertex2] = val; 1422 submesh->mIndex[j*3+1] = val; 1423 } 1424 else 1425 submesh->mIndex[j*3+1] = res->second; 1426 1427 res=vertex_map.find(vertex3); 1428 if (res==vertex_map.end()) 1429 { 1430 int val = vertex_map.size(); 1431 vertex_map[vertex3] = val; 1432 submesh->mIndex[j*3+2] = val; 1433 } 1434 else 1435 submesh->mIndex[j*3+2] = res->second; 1436 1437 current_tris[current_submesh] ++; 1438 } 1439 1440 int GetVertexCount(void) const { return vertex_map.size(); } 1441 1442 void SetCurrentSubMesh(int i){ current_submesh = i; } 1443 1444 class vertex_arranger_node 1445 { 1446 public: 1447 int v, n, t; 1448 1449 vertex_arranger_node(void){ 1450 v=n=t=-1; 1451 } 1452 vertex_arranger_node(int iv, int in=-1, int it=-1){ 1453 v=iv; 1454 n=in; 1455 t=it; 1456 } 1457 1458 bool operator<(const vertex_arranger_node &vu) const { 1459 if (v<vu.v) return true; 1460 if (v>vu.v) return false; 1461 if (n<vu.n) return true; 1462 if (n>vu.n) return false; 1463 if (t<vu.t) return true; 1464 if (t>vu.t) return false; 1465 return false; 1466 } 1467 bool operator==(const vertex_arranger_node &vu) const { 1468 return (v==vu.v && v==vu.v && n==vu.n && n==vu.n && t==vu.t && t==vu.t); 1469 } 1470 }; 1471 1472 /* VertexArranger::vertex_arranger_node *GetVertexInfo(void) const { 1473 vertex_arranger_node * vertex_list = new vertex_arranger_node[GetVertexCount()]; 1474 for (std::map<vertex_arranger_node,int>::const_iterator it=vertex_map.begin(); it!=vertex_map.end(); it++) 1475 vertex_list[it->second] = it->first; 1476 return vertex_list; 1477 } 1478 1479 const std::vector<int> GetIndexInfo(void) const { return indices; }*/ 1480 1481 void Arrange( const std::vector<Geometry::Vector3> & vertices, 1482 const std::vector<Geometry::Vector3> & normals, 1483 const std::vector<Geometry::Vector2> & texcoords){ 1484 1485 mesh->mVertexBuffer=new Geometry::VertexBuffer; 1486 mesh->mVertexBuffer->mVertexCount = GetVertexCount(); 1487 mesh->mVertexBuffer->mPosition = new Geometry::Vector3[mesh->mVertexBuffer->mVertexCount]; 1488 mesh->mVertexBuffer->mTexCoords = texcoords.empty()?NULL:new Geometry::Vector2[mesh->mVertexBuffer->mVertexCount]; 1489 mesh->mVertexBuffer->mNormal = normals.empty()?NULL:new Geometry::Vector3[mesh->mVertexBuffer->mVertexCount]; 1490 mesh->mVertexBuffer->mVertexInfo = Geometry::VERTEX_POSITION | (texcoords.empty()?0:Geometry::VERTEX_TEXCOORDS) | (normals.empty()?0:Geometry::VERTEX_NORMAL); 1491 1492 // sort the calculated vertices 1493 1494 vertex_arranger_node * vertex_list = new vertex_arranger_node[mesh->mVertexBuffer->mVertexCount]; 1495 for (std::map<vertex_arranger_node,int>::iterator it=vertex_map.begin(); it!=vertex_map.end(); it++) 1496 vertex_list[it->second] = it->first; 1497 1498 for (int i=0; i<mesh->mSubMeshCount; i++) 1499 mesh->mSubMesh[i].mVertexBuffer = mesh->mVertexBuffer; 1500 1501 for (int j=0; j<mesh->mVertexBuffer->mVertexCount; j++) 1502 { 1503 int vi = vertex_list[j].v; 1504 int ti = vertex_list[j].t; 1505 int ni = vertex_list[j].n; 1506 1507 Geometry::Vector3 auxpos(vertices[vi]); 1508 if (auxpos.x < mesh->mMeshBounds.minX) mesh->mMeshBounds.minX = auxpos.x; 1509 if (auxpos.y < mesh->mMeshBounds.minY) mesh->mMeshBounds.minY = auxpos.y; 1510 if (auxpos.z < mesh->mMeshBounds.minZ) mesh->mMeshBounds.minZ = auxpos.z; 1511 if (auxpos.x > mesh->mMeshBounds.maxX) mesh->mMeshBounds.maxX = auxpos.x; 1512 if (auxpos.y > mesh->mMeshBounds.maxY) mesh->mMeshBounds.maxY = auxpos.y; 1513 if (auxpos.z > mesh->mMeshBounds.maxZ) mesh->mMeshBounds.maxZ = auxpos.z; 1514 1515 mesh->mVertexBuffer->mPosition[j] = auxpos; 1516 mesh->mVertexBuffer->mNormal[j] = normals[ni]; 1517 if (texcoords.empty()==false) 1518 mesh->mVertexBuffer->mTexCoords[j] = texcoords[ti]; 1519 } 1520 1521 delete[] vertex_list; 1522 } 1523 1524 //private: 1525 std::map<vertex_arranger_node,int> vertex_map; 1526 Geometry::Mesh *mesh; 1527 int current_submesh; 1528 int *current_tris; 1410 1529 }; 1411 1530 … … 1532 1651 // fill up the mesh structure with the loaded information 1533 1652 1534 std::map<vertex_arranger_node,int> vertex_map;1653 VertexArranger vertex_arranger(mesh); 1535 1654 1536 1655 mesh->mSubMesh=new Geometry::SubMesh[mesh->mSubMeshCount]; … … 1544 1663 submesh->mIndexCount = vecfaces.size()*3; 1545 1664 submesh->mIndex=new Geometry::Index[submesh->mIndexCount]; 1665 1666 vertex_arranger.SetCurrentSubMesh(i); 1546 1667 1547 1668 for (std::vector<face_t>::const_iterator it=vecfaces.begin(); it!=vecfaces.end(); it++,j++) … … 1549 1670 const face_t & auxface = *it; 1550 1671 1551 vertex_arranger_node v1(auxface.v1,auxface.n1,auxface.t1); 1552 vertex_arranger_node v2(auxface.v2,auxface.n2,auxface.t2); 1553 vertex_arranger_node v3(auxface.v3,auxface.n3,auxface.t3); 1554 1555 std::map<vertex_arranger_node,int>::iterator res; 1556 res=vertex_map.find(v1); 1557 if (res==vertex_map.end()) 1558 { 1559 int val = vertex_map.size(); 1560 vertex_map[v1] = val; 1561 submesh->mIndex[j*3+0] = val; 1562 } 1563 else 1564 submesh->mIndex[j*3+0] = res->second; 1565 1566 res=vertex_map.find(v2); 1567 if (res==vertex_map.end()) 1568 { 1569 int val = vertex_map.size(); 1570 vertex_map[v2] = val; 1571 submesh->mIndex[j*3+1] = val; 1572 } 1573 else 1574 submesh->mIndex[j*3+1] = res->second; 1575 1576 res=vertex_map.find(v3); 1577 if (res==vertex_map.end()) 1578 { 1579 int val = vertex_map.size(); 1580 vertex_map[v3] = val; 1581 submesh->mIndex[j*3+2] = val; 1582 } 1583 else 1584 submesh->mIndex[j*3+2] = res->second; 1672 vertex_arranger.AddFace(auxface.v1,auxface.v2,auxface.v3, 1673 auxface.t1,auxface.t2,auxface.t3, 1674 auxface.n1,auxface.n2,auxface.n3); 1585 1675 } 1586 1676 } 1587 1677 1588 mesh->mVertexBuffer=new Geometry::VertexBuffer; 1589 mesh->mVertexBuffer->mVertexCount = vertex_map.size(); 1590 mesh->mVertexBuffer->mPosition = new Geometry::Vector3[mesh->mVertexBuffer->mVertexCount]; 1591 mesh->mVertexBuffer->mTexCoords = texcoords.empty()?NULL:new Geometry::Vector2[mesh->mVertexBuffer->mVertexCount]; 1592 mesh->mVertexBuffer->mNormal = normals.empty()?NULL:new Geometry::Vector3[mesh->mVertexBuffer->mVertexCount]; 1593 mesh->mVertexBuffer->mVertexInfo = Geometry::VERTEX_POSITION | (texcoords.empty()?0:Geometry::VERTEX_TEXCOORDS) | (normals.empty()?0:Geometry::VERTEX_NORMAL); 1594 1595 // sort the calculated vertices 1596 1597 vertex_arranger_node * vertex_list = new vertex_arranger_node[mesh->mVertexBuffer->mVertexCount]; 1598 for (std::map<vertex_arranger_node,int>::iterator it=vertex_map.begin(); it!=vertex_map.end(); it++) 1599 vertex_list[it->second] = it->first; 1600 1601 1602 for (int i=0; i<mesh->mSubMeshCount; i++) 1603 mesh->mSubMesh[i].mVertexBuffer=mesh->mVertexBuffer; 1604 1605 for (int j=0; j<mesh->mVertexBuffer->mVertexCount; j++) 1606 { 1607 int vi = vertex_list[j].v; 1608 int ti = vertex_list[j].t; 1609 int ni = vertex_list[j].n; 1610 1611 Geometry::Vector3 auxpos(vertices[vi]); 1612 if (auxpos.x < mesh->mMeshBounds.minX) mesh->mMeshBounds.minX = auxpos.x; 1613 if (auxpos.y < mesh->mMeshBounds.minY) mesh->mMeshBounds.minY = auxpos.y; 1614 if (auxpos.z < mesh->mMeshBounds.minZ) mesh->mMeshBounds.minZ = auxpos.z; 1615 if (auxpos.x > mesh->mMeshBounds.maxX) mesh->mMeshBounds.maxX = auxpos.x; 1616 if (auxpos.y > mesh->mMeshBounds.maxY) mesh->mMeshBounds.maxY = auxpos.y; 1617 if (auxpos.z > mesh->mMeshBounds.maxZ) mesh->mMeshBounds.maxZ = auxpos.z; 1618 1619 mesh->mVertexBuffer->mPosition[j] = auxpos; 1620 mesh->mVertexBuffer->mNormal[j] = normals[ni]; 1621 if (found_texcoords) 1622 mesh->mVertexBuffer->mTexCoords[j] = texcoords[ti]; 1623 } 1624 1625 delete[] vertex_list; 1626 } 1627 1678 vertex_arranger.Arrange(vertices,normals,texcoords); 1679 } 1680
Note: See TracChangeset
for help on using the changeset viewer.