std::vector<SVertexP3NT2> CIdentity3DShape::GetVertexes() const { std::vector<SVertexP3NT2> result; for //(const auto & index : m_indicies) (size_t index = 0; index < m_vertices.size(); ++index) { auto vertexWithTransform = m_vertices[index]; vertexWithTransform.position = GetVertexPosition(index); result.push_back(vertexWithTransform); } return result; }
void VertexBuffer::Copy(const VertexBuffer *source, uint destIndex) { ASSERT(source != NULL); ASSERT(source->GetNumElements() > 0); ASSERT(source->GetStandardAttribs() == m_standardTypeAttribs); ASSERT(destIndex >= 0); ASSERT(destIndex + source->GetNumElements() <= GetNumElements()); uint destOffset = GetVertexPosition(destIndex); memcpy(&m_buffer[destOffset], source->GetBuffer(), GetNumElements() * GetElementWidthInBytes()); SetDirty(); }
YSRESULT YsShell::SaveMs3(int &nIgnored,const char fn[]) const { FILE *fp; YsShellVertexHandle vtHd; YsShellPolygonHandle plHd; Encache(); nIgnored=0; fp=fopen(fn,"w"); if(fp!=NULL) { vtHd=NULL; while((vtHd=FindNextVertex(vtHd))!=NULL) { YsVec3 pos; GetVertexPosition(pos,vtHd); fprintf(fp,"V %lf %lf %lf\n",pos.x(),pos.y(),pos.z()); } plHd=NULL; while((plHd=FindNextPolygon(plHd))!=NULL) { int n; const YsShellVertexHandle *plVtHd; n=GetNumVertexOfPolygon(plHd); plVtHd=GetVertexListOfPolygon(plHd); if(n==3) { fprintf(fp,"D %d %d %d\n", // D of Delta GetVertexIdFromHandle(plVtHd[0]), GetVertexIdFromHandle(plVtHd[1]), GetVertexIdFromHandle(plVtHd[2])); } else if(n==4) { fprintf(fp,"Q %d %d %d %d\n", GetVertexIdFromHandle(plVtHd[0]), GetVertexIdFromHandle(plVtHd[1]), GetVertexIdFromHandle(plVtHd[2]), GetVertexIdFromHandle(plVtHd[3])); } } fclose(fp); return YSOK; } return YSERR; }
bool IShape::ContainsPoint(const glm::vec2& point) { // For each triangle. for (unsigned int t = 0; t < GetVertexCount() / 3; ++t) { // Index for the first vertex that composes triangle. const unsigned int triangleStartIndex = t * 3; // Get the viewport coordinates of the three vertices that // compose the triangle. glm::vec2 A = TransformPoint(GetVertexPosition(triangleStartIndex)); glm::vec2 B = TransformPoint(GetVertexPosition(triangleStartIndex + 1)); glm::vec2 C = TransformPoint(GetVertexPosition(triangleStartIndex + 2)); // Compute barycentric coordinates for the specified point with // respect to the triangle ABC. const glm::vec2 v0(C - A); const glm::vec2 v1(B - A); const glm::vec2 v2(point - A); float dot00 = glm::dot(v0, v0); float dot01 = glm::dot(v0, v1); float dot02 = glm::dot(v0, v2); float dot11 = glm::dot(v1, v1); float dot12 = glm::dot(v1, v2); float recipDenom = 1.0f / (dot00 * dot11 - dot01 * dot01); float u = (dot11 * dot02 - dot01 * dot12) * recipDenom; float v = (dot00 * dot12 - dot01 * dot02) * recipDenom; // Return true if the point is inside of the triangle. if (u >= 0.0f && v >= 0.0f && (u + v) < 1.0f) return true; } // The point was not contained by any of the triangles that compose // the shape. return false; }
/** Actually extract data from vertex animation into UVertexAnimation object */ void ConvertCacheToAnim(FbxCache* Cache, int ChannelIndex, UVertexAnimation* NewVertexAnim, const FbxTime& StartTime, const FbxTime& DeltaTime, int32 NumFrames) { check(NewVertexAnim->BaseSkelMesh != NULL); check(NewVertexAnim->NumAnimatedVerts > 0); // Allocate buffer to read in to TArray<double> ReadBuffer; ReadBuffer.AddUninitialized(NewVertexAnim->NumAnimatedVerts*3); // Pre-size frame array to avoid re-allocations (costly!) NewVertexAnim->VertexAnimData.AddZeroed(NumFrames); // Build map from animated vert to final skel mesh verts FStaticLODModel& Model = NewVertexAnim->BaseSkelMesh->GetImportedResource()->LODModels[0]; TArray< TArray<int32> > ImportToFinal; CreateImportToFinalMap(Model.MeshToImportVertexMap, Model.MaxImportVertex+1, ImportToFinal); FbxTime CurrentTime = StartTime; for(int32 FrameNum=0; FrameNum<NumFrames; FrameNum++) { bool bReadOk = Cache->Read(ChannelIndex, CurrentTime, ReadBuffer.GetData(), NewVertexAnim->NumAnimatedVerts); check(bReadOk); FVertexAnimFrame& Frame = NewVertexAnim->VertexAnimData[FrameNum]; Frame.Time = (float)((CurrentTime - StartTime).GetSecondDouble()); Frame.Deltas.AddUninitialized(NewVertexAnim->NumAnimatedVerts); for(int32 PointIdx=0; PointIdx<NewVertexAnim->NumAnimatedVerts; PointIdx++) { // Find mesh position for this vertex so we can convert to a delta TArray<int32>& FinalVerts = ImportToFinal[PointIdx]; check(FinalVerts.Num() > 0); // Should always have one final vert for every import vert... int32 FinalVertIndex = FinalVerts[0]; const FVector MeshVertexPos = GetVertexPosition(Model, FinalVertIndex); FVector AnimVertexPos; AnimVertexPos.X = ReadBuffer[(PointIdx*3)+0]; AnimVertexPos.Y = ReadBuffer[(PointIdx*3)+1] * -1.f; // flip Y, the importer does this to the mesh, so we do it here as well AnimVertexPos.Z = ReadBuffer[(PointIdx*3)+2]; // Create delta struct Frame.Deltas[PointIdx] = (AnimVertexPos - MeshVertexPos); } CurrentTime += DeltaTime; } }
glm::vec2 IShape::GetTextureCoordinate(unsigned int index) const { // We can only return valid texture coordinates if the shape // has a texture. if (m_texture) { // Get the position of the vertex position. const glm::vec2 vertexPosition = GetVertexPosition(index); // Get the texture dimensions. const glm::vec2 textureDimensions( static_cast<float>(m_texture->GetWidth()), static_cast<float>(m_texture->GetHeight()) ); // Return the vertex position normalised by the texture // dimensions. return vertexPosition / textureDimensions; } else { return glm::vec2(0.0f, 0.0f); } }
void IShape::Update() { const unsigned int numVertices = GetVertexCount(); // Get all the data. std::unique_ptr<glm::vec3[]> vertexPositions(new glm::vec3[numVertices]); std::unique_ptr<glm::vec4[]> vertexColors(new glm::vec4[numVertices]); std::unique_ptr<glm::vec2[]> vertexTextureCoordinates(new glm::vec2[numVertices]); for (unsigned int i = 0; i < numVertices; ++i) { const glm::vec2 position = GetVertexPosition(i); vertexPositions[i] = glm::vec3(position, 0.0f); vertexColors[i] = m_fillColor; vertexTextureCoordinates[i] = GetTextureCoordinate(i); } // Bind the Vertex Array Object. glBindVertexArray(m_VAO); // Send the vertex position data to the corresponding VBO. glBindBuffer(GL_ARRAY_BUFFER, m_positionVBO); glBufferData( GL_ARRAY_BUFFER, sizeof(glm::vec3) * numVertices, &vertexPositions[0], GL_STATIC_DRAW ); // Set the vertex position data to be the first attribute // passed to the shader (index 0). glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Send the vertex color data to the corresponding VBO. glBindBuffer(GL_ARRAY_BUFFER, m_colorVBO); glBufferData( GL_ARRAY_BUFFER, sizeof(glm::vec4) * numVertices, &vertexColors[0], GL_STATIC_DRAW ); // Set the vertex color data to be the third attribute // passed to the shader (index 2). glEnableVertexAttribArray(2); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, 0); // Texturing... if (m_texture) { // Send the texture coordinate data to the corresponding VBO. glBindBuffer(GL_ARRAY_BUFFER, m_textureCoordinatesVBO); glBufferData( GL_ARRAY_BUFFER, sizeof(glm::vec2) * numVertices, &vertexTextureCoordinates[0], GL_STATIC_DRAW ); // Set the vertex texture coordinate data to be the fourth // attribute passed to the shader (index 3). glEnableVertexAttribArray(3); glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, 0); } // Unbind the Vertex Array Object. glBindVertexArray(0); }
YSRESULT YsShell::SaveMsh(int &nIgnored,const char fn[]) const { FILE *fp; YsShellVertexHandle vtHd; YsShellPolygonHandle plHd; Encache(); nIgnored=0; fp=fopen(fn,"w"); if(fp!=NULL) { int id,nofel2; fprintf(fp,"nofnod %d\n",GetNumVertex()); id=1; vtHd=NULL; while((vtHd=FindNextVertex(vtHd))!=NULL) { YsVec3 pos; GetVertexPosition(pos,vtHd); fprintf(fp,"nod %d %lf %lf %lf\n",id,pos.x(),pos.y(),pos.z()); id++; } nofel2=0; plHd=NULL; while((plHd=FindNextPolygon(plHd))!=NULL) { if(GetNumVertexOfPolygon(plHd)==3 || GetNumVertexOfPolygon(plHd)==4) { nofel2++; } else { nIgnored++; } } fprintf(fp,"nofel2 %d\n",nofel2); id=1; plHd=NULL; while((plHd=FindNextPolygon(plHd))!=NULL) { int i,n; const YsShellVertexHandle *plVtHd; n=GetNumVertexOfPolygon(plHd); plVtHd=GetVertexListOfPolygon(plHd); if(n==3 || n==4) { fprintf(fp,"el2 %d %d",id,n); for(i=0; i<n; i++) { fprintf(fp," %d",GetVertexIdFromHandle(plVtHd[i])+1); } fprintf(fp,"\n"); id++; } } fclose(fp); return YSOK; } return YSERR; }
YSRESULT YsShell::SaveSrf( class YsTextOutputStream &textOut, YSSIZE_T nNoShadingPolygon,const YsShellPolygonHandle noShadingPolygon[], YSSIZE_T nRoundVtx,const YsShellVertexHandle roundVtx[]) const { YsShellVertexHandle vtHd,tstVtHd; YsShellPolygonHandle plHd,tstPlHd; YsHashTable <YsShellVertexHandle> roundVtxTable(nRoundVtx+1); YsHashTable <YsShellPolygonHandle> noShadingPolygonTable(nNoShadingPolygon+1); int i; if(noShadingPolygon!=NULL) { for(i=0; i<nNoShadingPolygon; i++) { noShadingPolygonTable.AddElement(GetSearchKey(noShadingPolygon[i]),noShadingPolygon[i]); } } if(roundVtx!=NULL) { for(i=0; i<nRoundVtx; i++) { roundVtxTable.AddElement(GetSearchKey(roundVtx[i]),roundVtx[i]); } } Encache(); textOut.Printf("SURF\n"); vtHd=NULL; while((vtHd=FindNextVertex(vtHd))!=NULL) { YsVec3 pos; GetVertexPosition(pos,vtHd); if(roundVtxTable.FindElement(tstVtHd,GetSearchKey(vtHd))!=YSOK) { textOut.Printf("V %lf %lf %lf\n",pos.x(),pos.y(),pos.z()); } else { textOut.Printf("V %lf %lf %lf R\n",pos.x(),pos.y(),pos.z()); } } plHd=NULL; while((plHd=FindNextPolygon(plHd))!=NULL) { int i,n; YsVec3 cen,nom,pos; const YsShellVertexHandle *plVtHd; n=GetNumVertexOfPolygon(plHd); plVtHd=GetVertexListOfPolygon(plHd); textOut.Printf("F\n"); if(noShadingPolygonTable.FindElement(tstPlHd,GetSearchKey(plHd))==YSOK) { textOut.Printf("B\n"); } cen.Set(0.0,0.0,0.0); for(i=0; i<n; i++) { if(i%8==0) { textOut.Printf("V"); } textOut.Printf(" %d",GetVertexIdFromHandle(plVtHd[i])); if(i%8==7 || i==n-1) { textOut.Printf("\n"); } GetVertexPosition(pos,plVtHd[i]); cen+=pos; } cen/=double(n); GetNormalOfPolygon(nom,plHd); YsColor col; // unsigned int r,g,b; GetColorOfPolygon(col,plHd); // r=YsBound(int(col.Rd()*31.0),0,31); // g=YsBound(int(col.Gd()*31.0),0,31); // b=YsBound(int(col.Bd()*31.0),0,31); // textOut.Printf("C %d\n",g*1024+r*32+b); textOut.Printf("C %d\n",col.Get15BitColor()); textOut.Printf("N %lf %lf %lf %lf %lf %lf\n",cen.x(),cen.y(),cen.z(),nom.x(),nom.y(),nom.z()); textOut.Printf("E\n"); } textOut.Printf("E\n"); return YSOK; }