#include <polymesh.hpp>
Inherits renderstack::mesh.
Inherited by renderstack::shapes::cube, renderstack::shapes::cuboctahedron, renderstack::shapes::dodecahedron, renderstack::shapes::great_rhombicosidodecahedron, renderstack::shapes::grid, renderstack::shapes::icosahedron, renderstack::shapes::johnson_solid, renderstack::shapes::octahedron, renderstack::shapes::quad, renderstack::shapes::sphere, renderstack::shapes::tetrahedron, renderstack::shapes::truncated_cube, renderstack::shapes::truncated_dodecahedron, renderstack::shapes::truncated_icosahedron, and renderstack::shapes::truncated_octahedron.
Definition at line 38 of file polymesh.hpp.
mesh * renderstack::shapes::polymesh::make_mesh_from_set_of_polygons | ( | std::set< uint32 > const & | polygon_indices | ) |
Definition at line 518 of file polymesh.cpp.
00521 { 00522 mesh3d::geometry &g = m_geometry; 00523 mesh3d::geometry sub_g; 00524 00525 /* */ 00526 /* FIRST: Figure out what sources we have, and compute some if necessary / possible */ 00527 /* */ 00528 00529 #if 0 // not done for submesh 00530 /* If polygon centroids or normals are not present, the can be computed */ 00531 if(g.polygon_attributes().has("polygon_normals") == false) 00532 { 00533 g.compute_polygon_normals(); 00534 } 00535 00536 if(g.polygon_attributes().has("polygon_centroids") == false) 00537 { 00538 g.compute_polygon_centroids(); 00539 } 00540 #endif 00541 00542 /* Polygon ID's are always generated here */ 00543 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, uint32> > polygon_ids; 00544 00545 //polygon_ids = g.polygon_attributes().find_or_create<uint32>("polygon_ids"); 00546 polygon_ids = g.polygon_attributes().find<uint32>("polygon_ids"); 00547 00548 /* Corner normals are generated, if no corner or */ 00549 /* point normals are available so far. */ 00550 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::corner*, mesh3d::vec3> > corner_normals; 00551 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec3> > point_normals; 00552 if(g.corner_attributes().has("corner_normals") == true) 00553 { 00554 //corner_normals = g.corner_attributes().find_or_create<mesh3d::vec3>("corner_normals"); 00555 corner_normals = g.corner_attributes().find<mesh3d::vec3>("corner_normals"); 00556 } 00557 else if(g.point_attributes().has("point_normals") == true) 00558 { 00559 //point_normals = g.point_attributes().find_or_create<mesh3d::vec3>("point_normals"); 00560 point_normals = g.point_attributes().find<mesh3d::vec3>("point_normals"); 00561 } 00562 else 00563 { 00564 //g.compute_corner_normals(RS_TWO_PI); 00565 //corner_normals = g.corner_attributes().find_or_create<mesh3d::vec3>("corner_normals"); 00566 corner_normals = g.corner_attributes().find<mesh3d::vec3>("corner_normals"); 00567 } 00568 00569 /* Texcoords are processed if found for corners or points, in that order */ 00570 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec2> > point_texcoords; 00571 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::corner*, mesh3d::vec2> > corner_texcoords; 00572 if(g.corner_attributes().has("corner_texcoords") == true) 00573 { 00574 //corner_texcoords = g.corner_attributes().find_or_create<mesh3d::vec2>("corner_texcoords"); 00575 corner_texcoords = g.corner_attributes().find<mesh3d::vec2>("corner_texcoords"); 00576 } 00577 else if(g.point_attributes().has("point_texcoords") == true) 00578 { 00579 //point_texcoords = g.point_attributes().find_or_create<mesh3d::vec2>("point_texcoords"); 00580 point_texcoords = g.point_attributes().find<mesh3d::vec2>("point_texcoords"); 00581 } 00582 00583 /* These should now be there */ 00584 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, mesh3d::vec3> > polygon_normals; 00585 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, mesh3d::vec3> > polygon_centroids; 00586 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec3> > point_locations; 00587 00588 polygon_normals = g.polygon_attributes().find<mesh3d::vec3>("polygon_normals"); 00589 polygon_centroids = g.polygon_attributes().find<mesh3d::vec3>("polygon_centroids"); 00590 point_locations = g.point_attributes ().find<mesh3d::vec3>("point_locations"); 00591 00592 /* Corner indices are always generated here */ 00593 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::corner*, int> > corner_indices; 00594 00595 //corner_indices = g.corner_attributes().find_or_create<int>("corner_indices"); 00596 corner_indices = sub_g.corner_attributes().find_or_create<int>("corner_indices"); 00597 00598 #if 1 00599 /* Point indices are? needed? when making submeshes */ 00600 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, int> > point_indices; 00601 point_indices = sub_g.point_attributes().find_or_create<int>("point_indices"); 00602 #endif 00603 00604 /* */ 00605 /* SECOND: Prepare buffers for writing */ 00606 /* */ 00607 00608 /* Setup vertex format */ 00609 vertex_attribute attribute_position(vertex_attribute_usage_position, GL_FLOAT, 0, 3); 00610 vertex_attribute attribute_normal (vertex_attribute_usage_normal, GL_FLOAT, 0, 3); 00611 vertex_attribute attribute_normal2 (vertex_attribute_usage_normal, GL_FLOAT, 1, 3); 00612 vertex_attribute attribute_texcoord(vertex_attribute_usage_texcoord, GL_FLOAT, 0, 2); 00613 vertex_attribute attribute_id (vertex_attribute_usage_id, GL_UNSIGNED_INT, 0, 1); 00614 00615 mesh *submesh = new mesh(); 00616 buffer *vertex_buffer = submesh->vertex_buffer(); 00617 vertex_buffer->vertex_format().append(attribute_position); 00618 vertex_buffer->vertex_format().append(attribute_normal); 00619 vertex_buffer->vertex_format().append(attribute_normal2); 00620 vertex_buffer->vertex_format().append(attribute_id); 00621 if(corner_texcoords || point_texcoords) 00622 { 00623 vertex_buffer->vertex_format().append(attribute_texcoord); 00624 } 00625 00626 buffer* polygon_fill_indices = submesh->find_or_create_index_buffer(mesh_mode_polygon_fill , GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_TRIANGLES); 00627 buffer* edge_line_indices = submesh->find_or_create_index_buffer(mesh_mode_edge_lines , GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_LINES); 00628 buffer* corner_point_indices = submesh->find_or_create_index_buffer(mesh_mode_corner_points , GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_POINTS); 00629 buffer* polygon_centroid_indices = submesh->find_or_create_index_buffer(mesh_mode_polygon_centroids, GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_POINTS); 00630 00631 /* Prepare writing to buffers */ 00632 vertex_buffer ->begin_edit(); 00633 polygon_fill_indices ->begin_edit(); 00634 edge_line_indices ->begin_edit(); 00635 corner_point_indices ->begin_edit(); 00636 polygon_centroid_indices->begin_edit(); 00637 00638 int index = 0; 00639 int vertex_index[mesh_mode_count] = {}; 00640 00641 /* Edges are collected to this work database while working through */ 00642 /* polygons. */ 00643 std::set<edge> edges; 00644 00645 uint32 polygon_index = 0; 00646 //mesh3d::geometry::polygon_collection &polygons = g.polygons(); 00647 00648 /* Go through polygons, create triangles, collect edges */ 00649 for( 00650 std::set<uint32>::const_iterator i = polygon_indices.begin(); 00651 i != polygon_indices.end(); 00652 ++i 00653 ) 00654 { 00655 mesh3d::polygon *pol = g.polygons()[*i]; 00656 mesh3d::vec3 pn = polygon_normals->value(pol); 00657 int first_index = index; 00658 int prev_index = -1; 00659 00660 mesh3d::corner *first_corner = *pol->corners().begin(); 00661 mesh3d::corner *last_corner = *pol->corners().rbegin(); 00662 mesh3d::point *first_point = first_corner->point(); 00663 mesh3d::point *last_point = last_corner->point(); 00664 mesh3d::point *prev_point = NULL; 00665 mesh3d::vec3 reference_location = point_locations->value(first_point); 00666 00667 for 00668 ( 00669 mesh3d::polygon::corner_collection::iterator j = pol->corners().begin(); 00670 j != pol->corners().end(); 00671 ++j 00672 ) 00673 { 00674 mesh3d::corner *cor = *j; 00675 mesh3d::point *pnt = cor->point(); 00676 mesh3d::vec3 p = point_locations->value(pnt); 00677 00678 /* Set vertex attributes */ 00679 vertex_buffer->set_index(index); 00680 vertex_buffer->put_position(p.x, p.y, p.z); 00681 00682 if(corner_normals) 00683 { 00684 mesh3d::vec3 n = corner_normals->value(cor); 00685 00686 vertex_buffer->put_normal(n.x, n.y, n.z); 00687 } 00688 else if(point_normals) 00689 { 00690 mesh3d::vec3 n = point_normals->value(pnt); 00691 00692 vertex_buffer->put_normal(n.x, n.y, n.z); 00693 } 00694 00695 vertex_buffer->put_normal(pn.x, pn.y, pn.z, 1); 00696 00697 if(corner_texcoords) 00698 { 00699 mesh3d::vec2 texcoord = corner_texcoords->value(cor); 00700 vertex_buffer->put_texcoord(texcoord.x, texcoord.y); 00701 } 00702 else if(point_texcoords) 00703 { 00704 mesh3d::vec2 texcoord = point_texcoords->value(pnt); 00705 vertex_buffer->put_texcoord(texcoord.x, texcoord.y); 00706 } 00707 00708 vertex_buffer->put_uint32(attribute_id, polygon_index); 00709 00710 #if 0 // not done for submesh 00711 /* Store polygon index to geometry database */ 00712 polygon_ids->set_value(pol, polygon_index); 00713 #endif 00714 00715 /* Write corner point index buffer */ 00716 corner_point_indices->set_index( 00717 vertex_index[mesh_mode_corner_points] 00718 ); 00719 corner_point_indices->point(index); 00720 ++vertex_index[mesh_mode_corner_points]; 00721 00722 /* Store corner index to geometry database */ 00723 corner_indices->set_value(cor, index); 00724 point_indices->set_value(pnt, index); 00725 00726 /* Write polygon fill triangle */ 00727 if(prev_index != -1) 00728 { 00729 polygon_fill_indices->set_index( 00730 vertex_index[mesh_mode_polygon_fill] 00731 ); 00732 polygon_fill_indices->triangle( 00733 first_index, 00734 index, 00735 prev_index 00736 ); 00737 vertex_index[mesh_mode_polygon_fill] += 3; 00738 } 00739 00740 /* Store edge line to working database */ 00741 if(prev_point != NULL) 00742 { 00743 edge e(prev_point, pnt); 00744 edges.insert(e); 00745 } 00746 00747 prev_index = index; 00748 prev_point = pnt; 00749 00750 ++index; 00751 } 00752 00753 /* Add last edge to working database */ 00754 { 00755 edge e(last_point, first_point); 00756 edges.insert(e); 00757 } 00758 00759 ++polygon_index; 00760 } 00761 00762 /* Write all edge lines from working database */ 00763 for( 00764 std::set<edge>::iterator k = edges.begin(); 00765 k != edges.end(); 00766 ++k 00767 ) 00768 { 00769 edge &e = *k; 00770 #if 0 00771 mesh3d::corner *c0 = e.m_pair.first->corners().front(); 00772 mesh3d::corner *c1 = e.m_pair.second->corners().front(); 00773 int i0 = corner_indices->value(c0); 00774 int i1 = corner_indices->value(c1); 00775 #endif 00776 int i0 = point_indices->value(e.m_pair.first); 00777 int i1 = point_indices->value(e.m_pair.second); 00778 00779 edge_line_indices->set_index( 00780 vertex_index[mesh_mode_edge_lines] 00781 ); 00782 edge_line_indices->line(i0, i1); 00783 vertex_index[mesh_mode_edge_lines] += 2; 00784 } 00785 00786 /* Generate polygon centroid vertices and index buffer */ 00787 for( 00788 std::set<uint32>::const_iterator i = polygon_indices.begin(); 00789 i != polygon_indices.end(); 00790 ++i 00791 ) 00792 { 00793 mesh3d::polygon *pol = g.polygons()[*i]; 00794 mesh3d::vec3 n = polygon_normals->value(pol); 00795 mesh3d::vec3 p = polygon_centroids->value(pol); 00796 00797 vertex_buffer->set_index (index); 00798 vertex_buffer->put_position(p.x, p.y, p.z); 00799 vertex_buffer->put_normal (n.x, n.y, n.z, 0); 00800 vertex_buffer->put_normal (n.x, n.y, n.z, 1); 00801 00802 polygon_centroid_indices->set_index( 00803 vertex_index[mesh_mode_polygon_centroids] 00804 ); 00805 polygon_centroid_indices->point(index); 00806 ++index; 00807 ++vertex_index[mesh_mode_polygon_centroids]; 00808 } 00809 00810 vertex_buffer ->end_edit(index); 00811 polygon_fill_indices ->end_edit(vertex_index[mesh_mode_polygon_fill ]); 00812 edge_line_indices ->end_edit(vertex_index[mesh_mode_edge_lines ]); 00813 corner_point_indices ->end_edit(vertex_index[mesh_mode_corner_points ]); 00814 polygon_centroid_indices->end_edit(vertex_index[mesh_mode_polygon_centroids]); 00815 00816 return submesh; 00817 }
void renderstack::shapes::polymesh::push_point | ( | float | x, | |
float | y, | |||
float | z | |||
) |
Definition at line 45 of file polymesh.cpp.
00046 { 00047 m_geometry.make_point(x, y, z); 00048 }
void renderstack::shapes::polymesh::push_point | ( | double | x, | |
double | y, | |||
double | z | |||
) |
Definition at line 50 of file polymesh.cpp.
00051 { 00052 m_geometry.make_point(x, y, z); 00053 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0 | ) |
Definition at line 55 of file polymesh.cpp.
00056 { 00057 return m_geometry.make_polygon(p0); 00058 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1 | |||
) |
Definition at line 59 of file polymesh.cpp.
00060 { 00061 return m_geometry.make_polygon(p0, p1); 00062 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2 | |||
) |
Definition at line 63 of file polymesh.cpp.
00064 { 00065 return m_geometry.make_polygon(p0, p1, p2); 00066 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2, | |||
mesh3d::point * | p3 | |||
) |
Definition at line 68 of file polymesh.cpp.
00069 { 00070 return m_geometry.make_polygon(p0, p1, p2, p3); 00071 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2, | |||
mesh3d::point * | p3, | |||
mesh3d::point * | p4 | |||
) |
Definition at line 72 of file polymesh.cpp.
00073 { 00074 return m_geometry.make_polygon(p0, p1, p2, p3, p4); 00075 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2, | |||
mesh3d::point * | p3, | |||
mesh3d::point * | p4, | |||
mesh3d::point * | p5 | |||
) |
Definition at line 76 of file polymesh.cpp.
00077 { 00078 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5); 00079 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2, | |||
mesh3d::point * | p3, | |||
mesh3d::point * | p4, | |||
mesh3d::point * | p5, | |||
mesh3d::point * | p6 | |||
) |
Definition at line 80 of file polymesh.cpp.
00081 { 00082 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6); 00083 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2, | |||
mesh3d::point * | p3, | |||
mesh3d::point * | p4, | |||
mesh3d::point * | p5, | |||
mesh3d::point * | p6, | |||
mesh3d::point * | p7 | |||
) |
Definition at line 84 of file polymesh.cpp.
00085 { 00086 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6, p7); 00087 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2, | |||
mesh3d::point * | p3, | |||
mesh3d::point * | p4, | |||
mesh3d::point * | p5, | |||
mesh3d::point * | p6, | |||
mesh3d::point * | p7, | |||
mesh3d::point * | p8 | |||
) |
Definition at line 88 of file polymesh.cpp.
00089 { 00090 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6, p7, p8); 00091 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | mesh3d::point * | p0, | |
mesh3d::point * | p1, | |||
mesh3d::point * | p2, | |||
mesh3d::point * | p3, | |||
mesh3d::point * | p4, | |||
mesh3d::point * | p5, | |||
mesh3d::point * | p6, | |||
mesh3d::point * | p7, | |||
mesh3d::point * | p8, | |||
mesh3d::point * | p9 | |||
) |
Definition at line 92 of file polymesh.cpp.
00093 { 00094 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); 00095 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0 | ) |
Definition at line 97 of file polymesh.cpp.
00098 { 00099 return m_geometry.make_polygon(p0); 00100 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1 | |||
) |
Definition at line 101 of file polymesh.cpp.
00102 { 00103 return m_geometry.make_polygon(p0, p1); 00104 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2 | |||
) |
Definition at line 105 of file polymesh.cpp.
00106 { 00107 return m_geometry.make_polygon(p0, p1, p2); 00108 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2, | |||
size_t | p3 | |||
) |
Definition at line 109 of file polymesh.cpp.
00110 { 00111 return m_geometry.make_polygon(p0, p1, p2, p3); 00112 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2, | |||
size_t | p3, | |||
size_t | p4 | |||
) |
Definition at line 113 of file polymesh.cpp.
00114 { 00115 return m_geometry.make_polygon(p0, p1, p2, p3, p4); 00116 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2, | |||
size_t | p3, | |||
size_t | p4, | |||
size_t | p5 | |||
) |
Definition at line 117 of file polymesh.cpp.
00118 { 00119 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5); 00120 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2, | |||
size_t | p3, | |||
size_t | p4, | |||
size_t | p5, | |||
size_t | p6 | |||
) |
Definition at line 121 of file polymesh.cpp.
00122 { 00123 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6); 00124 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2, | |||
size_t | p3, | |||
size_t | p4, | |||
size_t | p5, | |||
size_t | p6, | |||
size_t | p7 | |||
) |
Definition at line 125 of file polymesh.cpp.
00126 { 00127 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6, p7); 00128 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2, | |||
size_t | p3, | |||
size_t | p4, | |||
size_t | p5, | |||
size_t | p6, | |||
size_t | p7, | |||
size_t | p8 | |||
) |
Definition at line 129 of file polymesh.cpp.
00130 { 00131 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6, p7, p8); 00132 }
mesh3d::polygon * renderstack::shapes::polymesh::make_polygon | ( | size_t | p0, | |
size_t | p1, | |||
size_t | p2, | |||
size_t | p3, | |||
size_t | p4, | |||
size_t | p5, | |||
size_t | p6, | |||
size_t | p7, | |||
size_t | p8, | |||
size_t | p9 | |||
) |
Definition at line 133 of file polymesh.cpp.
00134 { 00135 return m_geometry.make_polygon(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); 00136 }
void renderstack::shapes::polymesh::fix_normals | ( | ) |
Definition at line 138 of file polymesh.cpp.
00139 { 00140 mesh3d::geometry &g = m_geometry; 00141 00142 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, mesh3d::vec3> > polygon_normals = g.polygon_attributes().find<mesh3d::vec3>("polygon_normals"); 00143 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec3> > point_locations = g.point_attributes ().find<mesh3d::vec3>("point_locations"); 00144 00145 mesh3d::geometry::polygon_collection &polygons = g.polygons(); 00146 00147 bool recompute_needed = false; 00148 00149 for( 00150 mesh3d::geometry::polygon_collection::iterator i = polygons.begin(); 00151 i != polygons.end(); 00152 ++i 00153 ) 00154 { 00155 mesh3d::polygon *pol = *i; 00156 if(pol->corners().size() < 3) 00157 { 00158 continue; 00159 } 00160 mesh3d::vec3 pn = polygon_normals->value(pol); 00161 mesh3d::corner *first_corner = *pol->corners().begin(); 00162 mesh3d::point *first_point = first_corner->point(); 00163 mesh3d::vec3 reference_location = point_locations->value(first_point); 00164 00165 float d = dot(pn, reference_location); 00166 00167 if(d < 0) 00168 { 00169 pol->reverse(); 00170 recompute_needed = true; 00171 } 00172 } 00173 00174 if(recompute_needed == true) 00175 { 00176 g.compute_polygon_normals(); 00177 } 00178 }
void renderstack::shapes::polymesh::build_mesh_from_geometry | ( | bool | fix_convex_normals = true |
) |
Definition at line 220 of file polymesh.cpp.
00221 { 00222 mesh3d::geometry &g = m_geometry; 00223 /* */ 00224 /* FIRST: Figure out what sources we have, and compute some if necessary / possible */ 00225 /* */ 00226 00227 /* If polygon centroids or normals are not present, the can be computed */ 00228 if(g.polygon_attributes().has("polygon_normals") == false) 00229 { 00230 g.compute_polygon_normals(); 00231 } 00232 00233 if(g.polygon_attributes().has("polygon_centroids") == false) 00234 { 00235 g.compute_polygon_centroids(); 00236 } 00237 00238 /* Optionally, face normals can be flipped outward from the center */ 00239 /* of the mesh. This only does good to convex polyhedra. */ 00240 if(fix_convex_normals == true) 00241 { 00242 fix_normals(); 00243 } 00244 00245 /* Polygon ID's are always generated here */ 00246 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, uint32> > polygon_ids; 00247 00248 polygon_ids = g.polygon_attributes().find_or_create<uint32>("polygon_ids"); 00249 00250 /* Corner normals are generated, if no corner or */ 00251 /* point normals are available so far. */ 00252 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::corner*, mesh3d::vec3> > corner_normals; 00253 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec3> > point_normals; 00254 if(g.corner_attributes().has("corner_normals") == true) 00255 { 00256 corner_normals = g.corner_attributes().find_or_create<mesh3d::vec3>("corner_normals"); 00257 } 00258 else if(g.point_attributes().has("point_normals") == true) 00259 { 00260 point_normals = g.point_attributes().find_or_create<mesh3d::vec3>("point_normals"); 00261 } 00262 else 00263 { 00264 g.compute_corner_normals(RS_TWO_PI); 00265 corner_normals = g.corner_attributes().find_or_create<mesh3d::vec3>("corner_normals"); 00266 } 00267 00268 /* Texcoords are processed if found for corners or points, in that order */ 00269 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec2> > point_texcoords; 00270 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::corner*, mesh3d::vec2> > corner_texcoords; 00271 if(g.corner_attributes().has("corner_texcoords") == true) 00272 { 00273 corner_texcoords = g.corner_attributes().find_or_create<mesh3d::vec2>("corner_texcoords"); 00274 } 00275 else if(g.point_attributes().has("point_texcoords") == true) 00276 { 00277 point_texcoords = g.point_attributes().find_or_create<mesh3d::vec2>("point_texcoords"); 00278 } 00279 00280 /* These should now be there */ 00281 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, mesh3d::vec3> > polygon_normals; 00282 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::polygon*, mesh3d::vec3> > polygon_centroids; 00283 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::point*, mesh3d::vec3> > point_locations; 00284 00285 polygon_normals = g.polygon_attributes().find<mesh3d::vec3>("polygon_normals"); 00286 polygon_centroids = g.polygon_attributes().find<mesh3d::vec3>("polygon_centroids"); 00287 point_locations = g.point_attributes ().find<mesh3d::vec3>("point_locations"); 00288 00289 /* Corner indices are always generated here */ 00290 std::tr1::shared_ptr<mesh3d::attribute_map<mesh3d::corner*, int> > corner_indices; 00291 00292 corner_indices = g.corner_attributes ().find_or_create<int>("corner_indices"); 00293 00294 /* */ 00295 /* SECOND: Prepare buffers for writing */ 00296 /* */ 00297 00298 /* Setup vertex format */ 00299 vertex_attribute attribute_position(vertex_attribute_usage_position, GL_FLOAT, 0, 3); 00300 vertex_attribute attribute_normal (vertex_attribute_usage_normal, GL_FLOAT, 0, 3); 00301 vertex_attribute attribute_normal2 (vertex_attribute_usage_normal, GL_FLOAT, 1, 3); 00302 vertex_attribute attribute_texcoord(vertex_attribute_usage_texcoord, GL_FLOAT, 0, 2); 00303 vertex_attribute attribute_id (vertex_attribute_usage_id, GL_UNSIGNED_INT, 0, 1); 00304 00305 buffer *vertex_buffer = mesh::vertex_buffer(); 00306 vertex_buffer->vertex_format().append(attribute_position); 00307 vertex_buffer->vertex_format().append(attribute_normal); 00308 vertex_buffer->vertex_format().append(attribute_normal2); 00309 vertex_buffer->vertex_format().append(attribute_id); 00310 if(corner_texcoords || point_texcoords) 00311 { 00312 vertex_buffer->vertex_format().append(attribute_texcoord); 00313 } 00314 00315 buffer* polygon_fill_indices = mesh::find_or_create_index_buffer(mesh_mode_polygon_fill , GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_TRIANGLES); 00316 buffer* edge_line_indices = mesh::find_or_create_index_buffer(mesh_mode_edge_lines , GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_LINES); 00317 buffer* corner_point_indices = mesh::find_or_create_index_buffer(mesh_mode_corner_points , GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_POINTS); 00318 buffer* polygon_centroid_indices = mesh::find_or_create_index_buffer(mesh_mode_polygon_centroids, GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, GL_UNSIGNED_INT, GL_POINTS); 00319 00320 /* Prepare writing to buffers */ 00321 vertex_buffer ->begin_edit(); 00322 polygon_fill_indices ->begin_edit(); 00323 edge_line_indices ->begin_edit(); 00324 corner_point_indices ->begin_edit(); 00325 polygon_centroid_indices->begin_edit(); 00326 00327 int index = 0; 00328 int vertex_index[mesh_mode_count] = {}; 00329 00330 /* Edges are collected to this work database while working through */ 00331 /* polygons. */ 00332 std::set<edge> edges; 00333 00334 uint32 polygon_index = 0; 00335 mesh3d::geometry::polygon_collection &polygons = g.polygons(); 00336 00337 /* Go through polygons, create triangles, collect edges */ 00338 for( 00339 mesh3d::geometry::polygon_collection::iterator i = polygons.begin(); 00340 i != polygons.end(); 00341 ++i 00342 ) 00343 { 00344 mesh3d::polygon *pol = *i; 00345 mesh3d::vec3 pn; 00346 if(pol->corners().size() > 2) 00347 { 00348 pn = polygon_normals->value(pol); 00349 } 00350 else 00351 { 00352 pn = mesh3d::vec3(0.0f, 1.0f, 0.0f); 00353 } 00354 int first_index = index; 00355 int prev_index = -1; 00356 00357 mesh3d::corner *first_corner = *pol->corners().begin(); 00358 mesh3d::corner *last_corner = *pol->corners().rbegin(); 00359 mesh3d::point *first_point = first_corner->point(); 00360 mesh3d::point *last_point = last_corner->point(); 00361 mesh3d::point *prev_point = NULL; 00362 mesh3d::vec3 reference_location = point_locations->value(first_point); 00363 00364 for 00365 ( 00366 mesh3d::polygon::corner_collection::iterator j = pol->corners().begin(); 00367 j != pol->corners().end(); 00368 ++j 00369 ) 00370 { 00371 mesh3d::corner *cor = *j; 00372 mesh3d::point *pnt = cor->point(); 00373 mesh3d::vec3 p = point_locations->value(pnt); 00374 00375 /* Set vertex attributes */ 00376 vertex_buffer->set_index(index); 00377 vertex_buffer->put_position(p.x, p.y, p.z); 00378 00379 if(corner_normals && pol->corners().size() > 2) 00380 { 00381 mesh3d::vec3 n = corner_normals->value(cor); 00382 00383 vertex_buffer->put_normal(n.x, n.y, n.z); 00384 } 00385 else if(point_normals) 00386 { 00387 mesh3d::vec3 n = point_normals->value(pnt); 00388 00389 vertex_buffer->put_normal(n.x, n.y, n.z); 00390 } 00391 else 00392 { 00393 vertex_buffer->put_normal(0.0f, 1.0f, 0.0f); 00394 } 00395 00396 vertex_buffer->put_normal(pn.x, pn.y, pn.z, 1); 00397 00398 if(corner_texcoords) 00399 { 00400 mesh3d::vec2 texcoord = corner_texcoords->value(cor); 00401 vertex_buffer->put_texcoord(texcoord.x, texcoord.y); 00402 } 00403 else if(point_texcoords) 00404 { 00405 mesh3d::vec2 texcoord = point_texcoords->value(pnt); 00406 vertex_buffer->put_texcoord(texcoord.x, texcoord.y); 00407 } 00408 00409 vertex_buffer->put_uint32(attribute_id, polygon_index); 00410 00411 /* Store polygon index to geometry database */ 00412 polygon_ids->set_value(pol, polygon_index); 00413 00414 /* Write corner point index buffer */ 00415 corner_point_indices->set_index( 00416 vertex_index[mesh_mode_corner_points] 00417 ); 00418 corner_point_indices->point(index); 00419 ++vertex_index[mesh_mode_corner_points]; 00420 00421 /* Store corner index to geometry database */ 00422 corner_indices->set_value(cor, index); 00423 00424 /* Write polygon fill triangle */ 00425 if(prev_index != -1) 00426 { 00427 polygon_fill_indices->set_index( 00428 vertex_index[mesh_mode_polygon_fill] 00429 ); 00430 polygon_fill_indices->triangle( 00431 first_index, 00432 index, 00433 prev_index 00434 ); 00435 vertex_index[mesh_mode_polygon_fill] += 3; 00436 } 00437 00438 /* Store edge line to working database */ 00439 if(prev_point != NULL) 00440 { 00441 edge e(prev_point, pnt); 00442 edges.insert(e); 00443 } 00444 00445 prev_index = index; 00446 prev_point = pnt; 00447 00448 ++index; 00449 } 00450 00451 /* Add last edge to working database */ 00452 { 00453 edge e(last_point, first_point); 00454 edges.insert(e); 00455 } 00456 00457 ++polygon_index; 00458 } 00459 00460 /* Write all edge lines from working database */ 00461 for( 00462 std::set<edge>::iterator k = edges.begin(); 00463 k != edges.end(); 00464 ++k 00465 ) 00466 { 00467 edge &e = *k; 00468 mesh3d::corner *c0 = e.m_pair.first->corners().front(); 00469 mesh3d::corner *c1 = e.m_pair.second->corners().front(); 00470 int i0 = corner_indices->value(c0); 00471 int i1 = corner_indices->value(c1); 00472 edge_line_indices->set_index( 00473 vertex_index[mesh_mode_edge_lines] 00474 ); 00475 edge_line_indices->line(i0, i1); 00476 vertex_index[mesh_mode_edge_lines] += 2; 00477 } 00478 00479 /* Generate polygon centroid vertices and index buffer */ 00480 for( 00481 mesh3d::geometry::polygon_collection::iterator i = polygons.begin(); 00482 i != polygons.end(); 00483 ++i 00484 ) 00485 { 00486 mesh3d::polygon *pol = *i; 00487 mesh3d::vec3 n; 00488 if(pol->corners().size() > 2) 00489 { 00490 n = polygon_normals->value(pol); 00491 } 00492 else 00493 { 00494 n = mesh3d::vec3(0.0f, 1.0f, 0.0f); 00495 } 00496 mesh3d::vec3 p = polygon_centroids->value(pol); 00497 00498 vertex_buffer->set_index (index); 00499 vertex_buffer->put_position(p.x, p.y, p.z); 00500 vertex_buffer->put_normal (n.x, n.y, n.z, 0); 00501 vertex_buffer->put_normal (n.x, n.y, n.z, 1); 00502 00503 polygon_centroid_indices->set_index( 00504 vertex_index[mesh_mode_polygon_centroids] 00505 ); 00506 polygon_centroid_indices->point(index); 00507 ++index; 00508 ++vertex_index[mesh_mode_polygon_centroids]; 00509 } 00510 00511 vertex_buffer ->end_edit(index); 00512 polygon_fill_indices ->end_edit(vertex_index[mesh_mode_polygon_fill ]); 00513 edge_line_indices ->end_edit(vertex_index[mesh_mode_edge_lines ]); 00514 corner_point_indices ->end_edit(vertex_index[mesh_mode_corner_points ]); 00515 polygon_centroid_indices->end_edit(vertex_index[mesh_mode_polygon_centroids]); 00516 }
Definition at line 72 of file polymesh.hpp.