Animation... still bugged

This commit is contained in:
Thomas FORGIONE 2015-03-05 12:29:05 +01:00
parent c10aa04435
commit 44973895e0
4 changed files with 22 additions and 18 deletions

View File

@ -32,7 +32,7 @@ class AnimatedMesh
/// \return the associated segment for each point /// \return the associated segment for each point
/// ///
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
void associateBranches(std::vector<geo::Segment<float,3>> segments, geo::Mesh& mesh); void associateBranches(std::vector<geo::Segment<float,3>> const& segments, geo::Mesh& mesh);
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/// ///
@ -44,7 +44,7 @@ class AnimatedMesh
/// \return nearest segment index /// \return nearest segment index
/// ///
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
unsigned int findNearestSegment(std::vector<geo::Segment<float,3>> segments, geo::Point<float> p); unsigned int findNearestSegment(std::vector<geo::Segment<float,3>> const& segments, geo::Point<float> const& p);
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/// ///
@ -57,7 +57,7 @@ class AnimatedMesh
/// \return faces associated to each segment /// \return faces associated to each segment
/// ///
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void findFaces(std::vector<unsigned int> associatedSegments, geo::Mesh& mesh, unsigned int nbSegments); void findFaces(std::vector<unsigned int> const& associatedSegments, geo::Mesh& mesh, unsigned int nbSegments);
std::vector<geo::Vector3<float>> vertices; ///< Vertices of the mesh std::vector<geo::Vector3<float>> vertices; ///< Vertices of the mesh

View File

@ -150,9 +150,9 @@ struct distance_to_segment_helper<T1,T2,T3,3>
auto scal2 = x12 * x2v + y12 * y2v + z12 * z2v; auto scal2 = x12 * x2v + y12 * y2v + z12 * z2v;
auto norm12 = x12 * x12 + y12 * y12 + z12 * z12; auto norm12 = x12 * x12 + y12 * y12 + z12 * z12;
auto xH = get_x(p1) + scal1 / norm12 * x12; auto xH = get_x(p1) + (scal1 / norm12) * x12;
auto yH = get_y(p1) + scal1 / norm12 * y12; auto yH = get_y(p1) + (scal1 / norm12) * y12;
auto zH = get_z(p1) + scal1 / norm12 * z12; auto zH = get_z(p1) + (scal1 / norm12) * z12;
float dist; float dist;

View File

@ -23,6 +23,8 @@ void drawScene(sft::FreeFlyCamera const& camera, pae::AnimatedMesh const& mesh);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
std::srand(std::time(nullptr));
// Init mesh // Init mesh
pae::Skeleton3D<geo::Vector3<float>, float> skeleton; pae::Skeleton3D<geo::Vector3<float>, float> skeleton;

View File

@ -12,6 +12,9 @@ AnimatedMesh::AnimatedMesh(Skeleton3D<geo::Vector3<float>,float> const& skeleton
findFaces(associatedSegment, mesh, segments.size()); findFaces(associatedSegment, mesh, segments.size());
vertices = mesh.vertices; vertices = mesh.vertices;
for (auto const& s : segments)
std::cout << s.first << "\n" << s.second << std::endl << std::endl;
for (unsigned int i = 0; i < facesPerSegment.size()-1; i++) for (unsigned int i = 0; i < facesPerSegment.size()-1; i++)
{ {
colors.push_back({static_cast<float>(std::rand())/RAND_MAX, colors.push_back({static_cast<float>(std::rand())/RAND_MAX,
@ -20,7 +23,7 @@ AnimatedMesh::AnimatedMesh(Skeleton3D<geo::Vector3<float>,float> const& skeleton
} }
} }
void AnimatedMesh::associateBranches(std::vector<geo::Segment<float,3>> segments, geo::Mesh& mesh) void AnimatedMesh::associateBranches(std::vector<geo::Segment<float,3>> const& segments, geo::Mesh& mesh)
{ {
// stock each point at the index corresponding to the nearest segment // stock each point at the index corresponding to the nearest segment
for(unsigned int i=0;i<mesh.vertices.size();i++) for(unsigned int i=0;i<mesh.vertices.size();i++)
@ -31,15 +34,14 @@ void AnimatedMesh::associateBranches(std::vector<geo::Segment<float,3>> segments
} }
//finds the nearest segment to a point //finds the nearest segment to a point
unsigned int AnimatedMesh::findNearestSegment(std::vector<geo::Segment<float,3>> segments, geo::Point<float> p) unsigned int AnimatedMesh::findNearestSegment(std::vector<geo::Segment<float,3>> const& segments, geo::Point<float> const& p)
{ {
unsigned int nearestSegment = 0; unsigned int nearestSegment = 0;
float shortest_distance; float shortest_distance = std::numeric_limits<float>::max();
float distance;
for(unsigned int i=0;i<segments.size();i++) for(unsigned int i=0;i<segments.size();i++)
{ {
// find distance between point and segment // find distance between point and segment
distance = geo::distanceToSegment(p.second, segments[i].first, segments[i].second); float distance = geo::distanceToSegment(p.second, segments[i].first, segments[i].second);
if (shortest_distance > distance) if (shortest_distance > distance)
{ {
shortest_distance = distance; shortest_distance = distance;
@ -49,7 +51,7 @@ unsigned int AnimatedMesh::findNearestSegment(std::vector<geo::Segment<float,3>
return nearestSegment; return nearestSegment;
} }
void AnimatedMesh::findFaces(std::vector<unsigned int> associatedSegments, geo::Mesh& mesh, unsigned int nbSegments) void AnimatedMesh::findFaces(std::vector<unsigned int> const& associatedSegments, geo::Mesh& mesh, unsigned int nbSegments)
{ {
for (unsigned int i=0;i<nbSegments+1;i++) for (unsigned int i=0;i<nbSegments+1;i++)
{ {
@ -60,7 +62,7 @@ void AnimatedMesh::findFaces(std::vector<unsigned int> associatedSegments, geo:
for(unsigned int i=0;i<mesh.faces.size();i++) for(unsigned int i=0;i<mesh.faces.size();i++)
{ {
segment = associatedSegments[mesh.faces[i].x()]; segment = associatedSegments[mesh.faces[i].x()];
if ((segment == associatedSegments[mesh.faces[i].y()]) && (associatedSegments[mesh.faces[i].z()])) if ((segment == associatedSegments[mesh.faces[i].y()]) && (segment == associatedSegments[mesh.faces[i].z()]))
{ {
facesPerSegment[segment].push_back(mesh.faces[i]); facesPerSegment[segment].push_back(mesh.faces[i]);
} }
@ -78,15 +80,15 @@ void AnimatedMesh::draw() const
for (unsigned int i=0; i<facesPerSegment.size()-1;i++) for (unsigned int i=0; i<facesPerSegment.size()-1;i++)
{ {
/// Transformation TODO /// Transformation TODO
glBegin(GL_TRIANGLES);
glColor3fv(&colors[i][0]); glColor3fv(&colors[i][0]);
glBegin(GL_TRIANGLES);
//For each face in one segment //For each face in one segment
for (auto const& f : facesPerSegment[i]) for (auto const& f : facesPerSegment[i])
{ {
glVertex3fv(&vertices[f.x()].data[0]); glVertex3f(vertices[f.x()].x(),vertices[f.x()].y(),vertices[f.x()].z());
glVertex3fv(&vertices[f.y()].data[0]); glVertex3f(vertices[f.y()].x(),vertices[f.y()].y(),vertices[f.y()].z());
glVertex3fv(&vertices[f.z()].data[0]); glVertex3f(vertices[f.z()].x(),vertices[f.z()].y(),vertices[f.z()].z());
} }
glEnd(); glEnd();