void Cylinder::buildTopCap(VertexList& vertices, IndexList& indices) { UINT baseIndex = (UINT)vertices.size(); // Duplicate cap vertices because the texture coordinates and normals differ. float y = 0.5f*mHeight; // vertices of ring float dTheta = 2.0f*PI/mNumSlices; for(UINT i = 0; i <= mNumSlices; ++i) { float x = mTopRadius*cosf(i*dTheta); float z = mTopRadius*sinf(i*dTheta); // Map [-1,1]-->[0,1] for planar texture coordinates. float u = +0.5f*x/mTopRadius + 0.5f; float v = -0.5f*z/mTopRadius + 0.5f; vertices.push_back( Vertex(x, y, z, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, u, v) ); } // cap center vertex vertices.push_back( Vertex(0.0f, y, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f) ); // index of center vertex UINT centerIndex = (UINT)vertices.size()-1; for(UINT i = 0; i < mNumSlices; ++i) { indices.push_back(centerIndex); indices.push_back(baseIndex + i+1); indices.push_back(baseIndex + i); } }
void CpuClipmapRenderer::createBlockIndices( GeometryClipmapLevel& level, const Rectangle2i& blockRect, int levelSampleCount, IndexList& indices ) { // todo: this needs rework.. if( blockRect.isEmpty() ) { return; } assert( blockRect._x0 >= 0 ); assert( blockRect._y0 >= 0 ); assert( blockRect._x1 > blockRect._x0 ); assert( blockRect._y1 > blockRect._y0 ); assert( blockRect._x0 < levelSampleCount ); assert( blockRect._y0 < levelSampleCount ); assert( blockRect._x1 < levelSampleCount ); assert( blockRect._y1 < levelSampleCount ); for( int y = blockRect._y0; y < blockRect._y1; ++y ) { assert( y + 1 < levelSampleCount ); const int row0 = ( level.blockOrigin[ 1 ] + y ) % levelSampleCount; const int row1 = ( level.blockOrigin[ 1 ] + y + 1 ) % levelSampleCount; for( int x = blockRect._x0; x < blockRect._x1; ++x ) { assert( x + 1 < levelSampleCount ); const int col0 = ( level.blockOrigin[ 0 ] + x ) % levelSampleCount; const int col1 = ( level.blockOrigin[ 0 ] + x + 1 ) % levelSampleCount; const int idx0 = row0 * levelSampleCount + col0; const int idx1 = row0 * levelSampleCount + col1; const int idx2 = row1 * levelSampleCount + col0; const int idx3 = row1 * levelSampleCount + col1; assert( idx0 < 65536 ); assert( idx1 < 65536 ); assert( idx2 < 65536 ); assert( idx3 < 65536 ); indices.push_back( idx0 ); indices.push_back( idx2 ); indices.push_back( idx1 ); indices.push_back( idx1 ); indices.push_back( idx2 ); indices.push_back( idx3 ); } } }
void fillMesh(PolygonMesh &mesh, double *verts, int nVerts, double *faceIdxs, int nVertsPerFace, int nFaces) { using namespace mas::mesh; PVertex3dList vtxList; IndexListList faceIdxList; for (int i=0; i<nVerts; i++) { PVertex3d vtx = MeshFactory::createVertex(verts[i*DIM], verts[i*DIM+1], verts[i*DIM+2], i); vtxList.push_back(vtx); } for (int i=0; i<nFaces; i++) { IndexList face; for (int j=0; j<nVertsPerFace; j++) { int vidx = (int)faceIdxs[i*nVertsPerFace+j]-1; face.push_back(vidx); } faceIdxList.push_back(face); } mesh.set(vtxList, faceIdxList); }
void IndexPseudoPyramidTree::insertToStructure(const Point& point, bool searchKeyExists) { // Add raw point adn the sum of all its elements to the vectors points.push_back(point); pointSums.push_back(point.sum()); int searchKey = computePseudoPyramidValue(numDimensions, point, minPoint, maxPoint, scaleFactors, cumulativeSFProducts); int currentIndex = points.size() - 1; if (searchKeyExists) { // If there is no inserted data, but there is a search key, // be sure to insert the current index (latest inserted point // index) into that point's bucket (this one) IndexList& indices = hashMap.find(searchKey)->second; // storing REFERFENCE so changes are made indices.push_back(currentIndex); } else { // If there is no inserted data and there is no search key IndexList indices; indices.reserve(10); indices.push_back(currentIndex); hashMap[searchKey] = indices; } }
PWIZ_API_DECL IndexList SpectrumList::findSpotID(const string& spotID) const { IndexList result; for (size_t index=0; index<size(); ++index) if (spectrumIdentity(index).spotID == spotID) result.push_back(index); return result; }
//*************************************************************************************** // Name: Subdivide // Desc: Function subdivides every input triangle into four triangles of equal area. //*************************************************************************************** void Subdivide(VertexList& vertices, IndexList& indices) { VertexList vin = vertices; IndexList iin = indices; vertices.resize(0); indices.resize(0); // v1 // * // / \ // / \ // m0*-----*m1 // / \ / \ // / \ / \ // *-----*-----* // v0 m2 v2 UINT numTris = (UINT)iin.size()/3; for(UINT i = 0; i < numTris; ++i) { D3DXVECTOR3 v0 = vin[ iin[i*3+0] ]; D3DXVECTOR3 v1 = vin[ iin[i*3+1] ]; D3DXVECTOR3 v2 = vin[ iin[i*3+2] ]; D3DXVECTOR3 m0 = 0.5f*(v0 + v1); D3DXVECTOR3 m1 = 0.5f*(v1 + v2); D3DXVECTOR3 m2 = 0.5f*(v0 + v2); vertices.push_back(v0); // 0 vertices.push_back(v1); // 1 vertices.push_back(v2); // 2 vertices.push_back(m0); // 3 vertices.push_back(m1); // 4 vertices.push_back(m2); // 5 indices.push_back(i*6+0); indices.push_back(i*6+3); indices.push_back(i*6+5); indices.push_back(i*6+3); indices.push_back(i*6+4); indices.push_back(i*6+5); indices.push_back(i*6+5); indices.push_back(i*6+4); indices.push_back(i*6+2); indices.push_back(i*6+3); indices.push_back(i*6+1); indices.push_back(i*6+4); } }
PWIZ_API_DECL IndexList ProteinList::findKeyword(const string& keyword, bool caseSensitive /*= true*/) const { IndexList indexList; if (caseSensitive) { for (size_t index = 0, end = size(); index < end; ++index) if (protein(index, false)->description.find(keyword) != string::npos) indexList.push_back(index); } else { string lcKeyword = keyword; for (size_t index = 0, end = size(); index < end; ++index) { string lcDescription = protein(index, false)->description; bal::to_lower(lcDescription); if (lcDescription.find(lcKeyword) != string::npos) indexList.push_back(index); } } return indexList; }
double scn::GetRichClubCoeff(UGraph::pGraph graph,size_t degree) { IndexList setOfHighNode;//whose degree is greater than //argument degree for(auto node = graph->begin(); node != graph->end(); node++) { if(node->GetDegree() > degree) { setOfHighNode.push_back(*node); } } double sum = 0; for(auto one = setOfHighNode.begin(); one != setOfHighNode.end(); one++) { for(auto two = one + 1; two != setOfHighNode.end(); two++) { if(graph->HasEdge(*one, *two)) sum++; } } return 2 * sum / static_cast<double>(setOfHighNode.size() * (setOfHighNode.size() - 1)); }
void TriStripVisitor::stripify(Geometry& geom) { if (geom.containsDeprecatedData()) geom.fixDeprecatedData(); if (osg::getBinding(geom.getNormalArray())==osg::Array::BIND_PER_PRIMITIVE_SET) return; if (osg::getBinding(geom.getColorArray())==osg::Array::BIND_PER_PRIMITIVE_SET) return; if (osg::getBinding(geom.getSecondaryColorArray())==osg::Array::BIND_PER_PRIMITIVE_SET) return; if (osg::getBinding(geom.getFogCoordArray())==osg::Array::BIND_PER_PRIMITIVE_SET) return; // no point tri stripping if we don't have enough vertices. if (!geom.getVertexArray() || geom.getVertexArray()->getNumElements()<3) return; // check for the existence of surface primitives unsigned int numSurfacePrimitives = 0; unsigned int numNonSurfacePrimitives = 0; Geometry::PrimitiveSetList& primitives = geom.getPrimitiveSetList(); Geometry::PrimitiveSetList::iterator itr; for(itr=primitives.begin(); itr!=primitives.end(); ++itr) { switch((*itr)->getMode()) { case(PrimitiveSet::TRIANGLES): case(PrimitiveSet::TRIANGLE_STRIP): case(PrimitiveSet::TRIANGLE_FAN): case(PrimitiveSet::QUADS): case(PrimitiveSet::QUAD_STRIP): case(PrimitiveSet::POLYGON): ++numSurfacePrimitives; break; default: ++numNonSurfacePrimitives; break; } } // nothitng to tri strip leave. if (!numSurfacePrimitives) return; // compute duplicate vertices typedef std::vector<unsigned int> IndexList; unsigned int numVertices = geom.getVertexArray()->getNumElements(); IndexList indices(numVertices); unsigned int i,j; for(i=0;i<numVertices;++i) { indices[i] = i; } VertexAttribComparitor arrayComparitor(geom); std::sort(indices.begin(),indices.end(),arrayComparitor); unsigned int lastUnique = 0; unsigned int numUnique = 1; unsigned int numDuplicate = 0; for(i=1;i<numVertices;++i) { if (arrayComparitor.compare(indices[lastUnique],indices[i])==0) { //std::cout<<" found duplicate "<<indices[lastUnique]<<" and "<<indices[i]<<std::endl; ++numDuplicate; } else { //std::cout<<" unique "<<indices[i]<<std::endl; lastUnique = i; ++numUnique; } } // std::cout<<" Number of duplicates "<<numDuplicate<<std::endl; // std::cout<<" Number of unique "<<numUnique<<std::endl; // std::cout<<" Total number of vertices required "<<numUnique<<" vs original "<<numVertices<<std::endl; // std::cout<<" % size "<<(float)numUnique/(float)numVertices*100.0f<<std::endl; IndexList remapDuplicatesToOrignals(numVertices); lastUnique = 0; for(i=1;i<numVertices;++i) { if (arrayComparitor.compare(indices[lastUnique],indices[i])!=0) { // found a new vertex entry, so previous run of duplicates needs // to be put together. unsigned int min_index = indices[lastUnique]; for(j=lastUnique+1;j<i;++j) { min_index = osg::minimum(min_index,indices[j]); } for(j=lastUnique;j<i;++j) { remapDuplicatesToOrignals[indices[j]]=min_index; } lastUnique = i; } } unsigned int min_index = indices[lastUnique]; for(j=lastUnique+1;j<i;++j) { min_index = osg::minimum(min_index,indices[j]); } for(j=lastUnique;j<i;++j) { remapDuplicatesToOrignals[indices[j]]=min_index; } // copy the arrays. IndexList finalMapping(numVertices); IndexList copyMapping; copyMapping.reserve(numUnique); unsigned int currentIndex=0; for(i=0;i<numVertices;++i) { if (remapDuplicatesToOrignals[i]==i) { finalMapping[i] = currentIndex; copyMapping.push_back(i); currentIndex++; } } for(i=0;i<numVertices;++i) { if (remapDuplicatesToOrignals[i]!=i) { finalMapping[i] = finalMapping[remapDuplicatesToOrignals[i]]; } } MyTriangleIndexFunctor taf; taf._remapIndices.swap(finalMapping); Geometry::PrimitiveSetList new_primitives; new_primitives.reserve(primitives.size()); for(itr=primitives.begin(); itr!=primitives.end(); ++itr) { switch((*itr)->getMode()) { case(PrimitiveSet::TRIANGLES): case(PrimitiveSet::TRIANGLE_STRIP): case(PrimitiveSet::TRIANGLE_FAN): case(PrimitiveSet::QUADS): case(PrimitiveSet::QUAD_STRIP): case(PrimitiveSet::POLYGON): (*itr)->accept(taf); break; default: new_primitives.push_back(*itr); break; } } float minimum_ratio_of_indices_to_unique_vertices = 1; float ratio_of_indices_to_unique_vertices = ((float)taf._in_indices.size()/(float)numUnique); OSG_INFO<<"TriStripVisitor::stripify(Geometry&): Number of indices"<<taf._in_indices.size()<<" numUnique"<< numUnique << std::endl; OSG_INFO<<"TriStripVisitor::stripify(Geometry&): ratio indices/numUnique"<< ratio_of_indices_to_unique_vertices << std::endl; // only tri strip if there is point in doing so. if (!taf._in_indices.empty() && ratio_of_indices_to_unique_vertices>=minimum_ratio_of_indices_to_unique_vertices) { OSG_INFO<<"TriStripVisitor::stripify(Geometry&): doing tri strip"<< std::endl; unsigned int in_numVertices = 0; for(triangle_stripper::indices::iterator itr=taf._in_indices.begin(); itr!=taf._in_indices.end(); ++itr) { if (*itr>in_numVertices) in_numVertices=*itr; } // the largest indice is in_numVertices, but indices start at 0 // so increment to give to the corrent number of verticies. ++in_numVertices; // remap any shared vertex attributes RemapArray ra(copyMapping); arrayComparitor.accept(ra); triangle_stripper::tri_stripper stripifier(taf._in_indices); stripifier.SetCacheSize(_cacheSize); stripifier.SetMinStripSize(_minStripSize); triangle_stripper::primitive_vector outPrimitives; stripifier.Strip(&outPrimitives); if (outPrimitives.empty()) { OSG_WARN<<"Error: TriStripVisitor::stripify(Geometry& geom) failed."<<std::endl; return; } triangle_stripper::primitive_vector::iterator pitr; if (_generateFourPointPrimitivesQuads) { OSG_INFO<<"Collecting all quads"<<std::endl; typedef triangle_stripper::primitive_vector::iterator prim_iterator; typedef std::multimap<unsigned int,prim_iterator> QuadMap; QuadMap quadMap; // pick out quads and place them in the quadMap, and also look for the max for(pitr=outPrimitives.begin(); pitr!=outPrimitives.end(); ++pitr) { if (pitr->Indices.size()==4) { std::swap(pitr->Indices[2],pitr->Indices[3]); unsigned int minValue = *(std::max_element(pitr->Indices.begin(),pitr->Indices.end())); quadMap.insert(QuadMap::value_type(minValue,pitr)); } } // handle the quads if (!quadMap.empty()) { IndexList indices; indices.reserve(4*quadMap.size()); // adds all the quads into the quad primitive, in ascending order // and the QuadMap stores the quad's in ascending order. for(QuadMap::iterator qitr=quadMap.begin(); qitr!=quadMap.end(); ++qitr) { pitr = qitr->second; unsigned int min_pos = 0; for(i=1;i<4;++i) { if (pitr->Indices[min_pos]>pitr->Indices[i]) min_pos = i; } indices.push_back(pitr->Indices[min_pos]); indices.push_back(pitr->Indices[(min_pos+1)%4]); indices.push_back(pitr->Indices[(min_pos+2)%4]); indices.push_back(pitr->Indices[(min_pos+3)%4]); } bool inOrder = true; unsigned int previousValue = indices.front(); for(IndexList::iterator qi_itr=indices.begin()+1; qi_itr!=indices.end() && inOrder; ++qi_itr) { inOrder = (previousValue+1)==*qi_itr; previousValue = *qi_itr; } if (inOrder) { new_primitives.push_back(new osg::DrawArrays(GL_QUADS,indices.front(),indices.size())); } else { unsigned int maxValue = *(std::max_element(indices.begin(),indices.end())); if (maxValue>=65536) { osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_QUADS); std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); new_primitives.push_back(elements); } else { osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUADS); std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); new_primitives.push_back(elements); } } } } // handle non quad primitives for(pitr=outPrimitives.begin(); pitr!=outPrimitives.end(); ++pitr) { if (!_generateFourPointPrimitivesQuads || pitr->Indices.size()!=4) { bool inOrder = true; unsigned int previousValue = pitr->Indices.front(); for(triangle_stripper::indices::iterator qi_itr=pitr->Indices.begin()+1; qi_itr!=pitr->Indices.end() && inOrder; ++qi_itr) { inOrder = (previousValue+1)==*qi_itr; previousValue = *qi_itr; } if (inOrder) { new_primitives.push_back(new osg::DrawArrays(pitr->Type,pitr->Indices.front(),pitr->Indices.size())); } else { unsigned int maxValue = *(std::max_element(pitr->Indices.begin(),pitr->Indices.end())); if (maxValue>=65536) { osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(pitr->Type); elements->reserve(pitr->Indices.size()); std::copy(pitr->Indices.begin(),pitr->Indices.end(),std::back_inserter(*elements)); new_primitives.push_back(elements); } else { osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(pitr->Type); elements->reserve(pitr->Indices.size()); std::copy(pitr->Indices.begin(),pitr->Indices.end(),std::back_inserter(*elements)); new_primitives.push_back(elements); } } } } geom.setPrimitiveSetList(new_primitives); #if 0 // debugging code for indentifying the tri-strips. osg::Vec4Array* colors = new osg::Vec4Array(new_primitives.size()); for(i=0;i<colors->size();++i) { (*colors)[i].set(((float)rand()/(float)RAND_MAX), ((float)rand()/(float)RAND_MAX), ((float)rand()/(float)RAND_MAX), 1.0f); } geom.setColorArray(colors); geom.setColorBinding(osg::Array::BIND_PER_PRIMITIVE_SET); #endif } else { OSG_INFO<<"TriStripVisitor::stripify(Geometry&): not doing tri strip *****************"<< std::endl; } }
void Cylinder::buildStacks(VertexList& vertices, IndexList& indices) { float stackHeight = mHeight / mNumStacks; // Amount to increment radius as we move up each stack level from bottom to top. float radiusStep = (mTopRadius - mBottomRadius) / mNumStacks; UINT numRings = mNumStacks+1; // Compute vertices for each stack ring. for(UINT i = 0; i < numRings; ++i) { float y = -0.5f*mHeight + i*stackHeight; float r = mBottomRadius + i*radiusStep; // Height and radius of next ring up. float y_next = -0.5f*mHeight + (i+1)*stackHeight; float r_next = mBottomRadius + (i+1)*radiusStep; // vertices of ring float dTheta = 2.0f*PI/mNumSlices; for(UINT j = 0; j <= mNumSlices; ++j) { float c = cosf(j*dTheta); float s = sinf(j*dTheta); float u = (float)j/mNumSlices; float v = 1.0f - (float)i/mNumStacks; // Partial derivative in theta direction to get tangent vector (this is a unit vector). D3DXVECTOR3 T(-s, 0.0f, c); // Compute tangent vector down the slope of the cone (if the top/bottom // radii differ then we get a cone and not a true cylinder). D3DXVECTOR3 P(r*c, y, r*s); D3DXVECTOR3 P_next(r_next*c, y_next, r_next*s); D3DXVECTOR3 B = P - P_next; D3DXVec3Normalize(&B, &B); D3DXVECTOR3 N; D3DXVec3Cross(&N, &T, &B); D3DXVec3Normalize(&N, &N); vertices.push_back( Vertex(P.x, P.y, P.z, T.x, T.y, T.z, N.x, N.y, N.z, u, v) ); } } UINT numRingVertices = mNumSlices+1; // Compute indices for each stack. for(UINT i = 0; i < mNumStacks; ++i) { for(UINT j = 0; j < mNumSlices; ++j) { indices.push_back(i*numRingVertices + j); indices.push_back((i+1)*numRingVertices + j); indices.push_back((i+1)*numRingVertices + j+1); indices.push_back(i*numRingVertices + j); indices.push_back((i+1)*numRingVertices + j+1); indices.push_back(i*numRingVertices + j+1); } } }
void OutBuilding::build(float w, float h, float d){ VertexPNTList vertices; VertexPNT v; float width = w; float height = h; float depth = d; float thrirdwidth = w/3; float twothirdwidth = (2*w)/3; float twothirdheight = (2*h)/3; //Front v.normal = D3DXVECTOR3( 0.0f, 0.0f, -1.0f); v.pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f); v.texC = D3DXVECTOR2(0.0,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, height, 0.0f); v.texC = D3DXVECTOR2(0.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(thrirdwidth, height, 0.0f); v.texC = D3DXVECTOR2(0.4,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(thrirdwidth, 0.0f, 0.0f); v.texC = D3DXVECTOR2(0.4,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(thrirdwidth, twothirdheight, 0.0f); v.texC = D3DXVECTOR2(0.4,0.7); vertices.push_back(v); v.pos = D3DXVECTOR3(twothirdwidth, twothirdheight, 0.0f); v.texC = D3DXVECTOR2(0.8,0.7); vertices.push_back(v); v.pos = D3DXVECTOR3(twothirdwidth, height, 0.0f); v.texC = D3DXVECTOR2(0.8,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, height, 0.0f); v.texC = D3DXVECTOR2(1.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, 0.0f, 0.0f); v.texC = D3DXVECTOR2(1.0,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(twothirdwidth, 0.0f, 0.0f); v.texC = D3DXVECTOR2(0.8,0.0); vertices.push_back(v); //Left Face v.normal = D3DXVECTOR3( -1.0f, 0.0f, 0.0f); v.pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f); v.texC = D3DXVECTOR2(0.0,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, height, 0.0f); v.texC = D3DXVECTOR2(0.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, height, depth); v.texC = D3DXVECTOR2(1.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, 0.0f, depth); v.texC = D3DXVECTOR2(1.0,0.0); vertices.push_back(v); //Top v.normal = D3DXVECTOR3( 0.0f, 1.0f, 0.0f); v.pos = D3DXVECTOR3(0.0f, height, 0.0f); v.texC = D3DXVECTOR2(0.0,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, height, depth); v.texC = D3DXVECTOR2(0.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, height, depth); v.texC = D3DXVECTOR2(1.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, height, 0.0f); v.texC = D3DXVECTOR2(1.0,0.0); vertices.push_back(v); //Right v.normal = D3DXVECTOR3( 1.0f, 0.0f, 0.0f); v.pos = D3DXVECTOR3(width, 0.0f, 0.0f); v.texC = D3DXVECTOR2(0.0,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, 0.0f, depth); v.texC = D3DXVECTOR2(0.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, height, depth); v.texC = D3DXVECTOR2(1.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, height, 0.0f); v.texC = D3DXVECTOR2(1.0,0.0); vertices.push_back(v); //Bottom v.normal = D3DXVECTOR3( 0.0f, -1.0f, 0.0f); v.pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f); v.texC = D3DXVECTOR2(0.0,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, 0.0f, depth); v.texC = D3DXVECTOR2(0.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, 0.0f, depth); v.texC = D3DXVECTOR2(1.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, 0.0f, 0.0f); v.texC = D3DXVECTOR2(1.0,0.0); vertices.push_back(v); //Back v.normal = D3DXVECTOR3( 0.0f, 0.0f, 1.0f); v.pos = D3DXVECTOR3(width, 0.0f, depth); v.texC = D3DXVECTOR2(0.0,0.0); vertices.push_back(v); v.pos = D3DXVECTOR3(width, height, depth); v.texC = D3DXVECTOR2(0.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, height, depth); v.texC = D3DXVECTOR2(1.0,1.0); vertices.push_back(v); v.pos = D3DXVECTOR3(0.0f, 0.0f, depth); v.texC = D3DXVECTOR2(1.0,0.0); vertices.push_back(v); mNumVertices = (UINT)vertices.size(); buildVB(vertices); IndexList indices; indices.push_back(0); indices.push_back(1); indices.push_back(2); indices.push_back(2); indices.push_back(3); indices.push_back(0); indices.push_back(4); indices.push_back(2); indices.push_back(6); indices.push_back(6); indices.push_back(5); indices.push_back(4); indices.push_back(9); indices.push_back(6); indices.push_back(7); indices.push_back(7); indices.push_back(8); indices.push_back(9); indices.push_back(10); indices.push_back(13); indices.push_back(12); indices.push_back(12); indices.push_back(11); indices.push_back(10); indices.push_back(14); indices.push_back(15); indices.push_back(16); indices.push_back(16); indices.push_back(17); indices.push_back(14); indices.push_back(18); indices.push_back(21); indices.push_back(16); indices.push_back(16); indices.push_back(19); indices.push_back(18); indices.push_back(22); indices.push_back(25); indices.push_back(24); indices.push_back(24); indices.push_back(23); indices.push_back(22); indices.push_back(26); indices.push_back(27); indices.push_back(28); indices.push_back(28); indices.push_back(29); indices.push_back(26); mNumFaces = (UINT)indices.size()/3; mNumIndices = (UINT)indices.size(); buildIB(indices); }
void Sphere::buildStacks(VertexList& vertices, IndexList& indices) { float phiStep = PI/mNumStacks; // do not count the poles as rings UINT numRings = mNumStacks-1; // Compute vertices for each stack ring. for(UINT i = 1; i <= numRings; ++i) { float phi = i*phiStep; // vertices of ring float thetaStep = 2.0f*PI/mNumSlices; for(UINT j = 0; j <= mNumSlices; ++j) { float theta = j*thetaStep; Vertex v; // spherical to cartesian v.pos.x = mRadius*sinf(phi)*cosf(theta); v.pos.y = mRadius*cosf(phi); v.pos.z = mRadius*sinf(phi)*sinf(theta); // partial derivative of P with respect to theta v.tangent.x = -mRadius*sinf(phi)*sinf(theta); v.tangent.y = 0.0f; v.tangent.z = mRadius*sinf(phi)*cosf(theta); D3DXVec3Normalize(&v.normal, &v.pos); v.texC.x = theta / (2.0f*PI); v.texC.y = phi / PI; vertices.push_back( v ); } } // poles: note that there will be texture coordinate distortion vertices.push_back( Vertex(0.0f, -mRadius, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f) ); vertices.push_back( Vertex(0.0f, mRadius, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f) ); UINT northPoleIndex = (UINT)vertices.size()-1; UINT southPoleIndex = (UINT)vertices.size()-2; UINT numRingVertices = mNumSlices+1; // Compute indices for inner stacks (not connected to poles). for(UINT i = 0; i < mNumStacks-2; ++i) { for(UINT j = 0; j < mNumSlices; ++j) { indices.push_back(i*numRingVertices + j); indices.push_back(i*numRingVertices + j+1); indices.push_back((i+1)*numRingVertices + j); indices.push_back((i+1)*numRingVertices + j); indices.push_back(i*numRingVertices + j+1); indices.push_back((i+1)*numRingVertices + j+1); } } // Compute indices for top stack. The top stack was written // first to the vertex buffer. for(UINT i = 0; i < mNumSlices; ++i) { indices.push_back(northPoleIndex); indices.push_back(i+1); indices.push_back(i); } // Compute indices for bottom stack. The bottom stack was written // last to the vertex buffer, so we need to offset to the index // of first vertex in the last ring. UINT baseIndex = (numRings-1)*numRingVertices; for(UINT i = 0; i < mNumSlices; ++i) { indices.push_back(southPoleIndex); indices.push_back(baseIndex+i); indices.push_back(baseIndex+i+1); } }
//Load TSM file GLC_World GLWidget::loadTSMFile( const QString &filename ) { RhBuilderPtr reader = makePtr<RhBuilder>(filename.toStdString()); TSplinePtr spline = reader->findTSpline(); GLC_World w; IndexList face; QList<float> vertex; QList<float> normal; TTessellator tessellator(spline); TImagePtr image = spline->getTImage(); // Go through all the faces in TImage and create abjects TFacVIterator fiter = image->faceIteratorBegin(); for (;fiter!=image->faceIteratorEnd();fiter++) { TFacePtr tface = *fiter; TriMeshPtr trimesh = tessellator.interpolateFace(tface); P3dVIterator piter = trimesh->pointIteratorBegin(); for (piter;piter!=trimesh->pointIteratorEnd();piter++) { vertex.push_back((*piter)->x()); vertex.push_back((*piter)->y()); vertex.push_back((*piter)->z()); } N3dVIterator niter = trimesh->normalIteratorBegin(); for (;niter!=trimesh->normalIteratorEnd();niter++) { normal.push_back((*niter)->i()); normal.push_back((*niter)->j()); normal.push_back((*niter)->k()); } TriVIterator titer = trimesh->triangleIteratorBegin(); for (;titer!=trimesh->triangleIteratorEnd();titer++) { face.push_back((*titer)->point_indices[0]); face.push_back((*titer)->point_indices[1]); face.push_back((*titer)->point_indices[2]); } GLC_Mesh* glc_mesh = new GLC_Mesh(); glc_mesh->addTriangles(0,face); face.clear(); glc_mesh->addVertice(vertex.toVector()); vertex.clear(); glc_mesh->addNormals(normal.toVector()); normal.clear(); glc_mesh->finish(); GLC_3DRep *rep = new GLC_3DRep(glc_mesh); glc_mesh = NULL; // Set the material GLC_Material* pCurrentMat= NULL; pCurrentMat= rep->geomAt(0)->firstMaterial(); pCurrentMat->setAmbientColor(Qt::gray); pCurrentMat->setDiffuseColor(Qt::gray); // Add objects (faces) to the world collection w.collection()->add(*rep); } return w; }
void IndexMeshVisitor::apply(osg::Geometry& geom) { // TODO: this is deprecated if (geom.getNormalBinding() == osg::Geometry::BIND_PER_PRIMITIVE_SET) return; if (geom.getColorBinding() == osg::Geometry::BIND_PER_PRIMITIVE_SET) return; if (geom.getSecondaryColorBinding() == osg::Geometry::BIND_PER_PRIMITIVE_SET) return; if (geom.getFogCoordBinding() == osg::Geometry::BIND_PER_PRIMITIVE_SET) return; // no point optimizing if we don't have enough vertices. if (!geom.getVertexArray() || geom.getVertexArray()->getNumElements() < 3) return; osgUtil::SharedArrayOptimizer deduplicator; deduplicator.findDuplicatedUVs(geom); // duplicate shared arrays as it isn't safe to rearrange vertices when arrays are shared. if (geom.containsSharedArrays()) { geom.duplicateSharedArrays(); } osg::Geometry::PrimitiveSetList& primitives = geom.getPrimitiveSetList(); osg::Geometry::PrimitiveSetList::iterator itr; osg::Geometry::PrimitiveSetList new_primitives; new_primitives.reserve(primitives.size()); // compute duplicate vertices typedef std::vector<unsigned int> IndexList; unsigned int numVertices = geom.getVertexArray()->getNumElements(); IndexList indices(numVertices); unsigned int i, j; for(i = 0 ; i < numVertices ; ++ i) { indices[i] = i; } VertexAttribComparitor arrayComparitor(geom); std::sort(indices.begin(), indices.end(), arrayComparitor); unsigned int lastUnique = 0; unsigned int numUnique = 1; for(i = 1 ; i < numVertices ; ++ i) { if (arrayComparitor.compare(indices[lastUnique], indices[i]) != 0) { lastUnique = i; ++ numUnique; } } IndexList remapDuplicatesToOrignals(numVertices); lastUnique = 0; for(i = 1 ; i < numVertices ; ++ i) { if (arrayComparitor.compare(indices[lastUnique],indices[i]) != 0) { // found a new vertex entry, so previous run of duplicates needs // to be put together. unsigned int min_index = indices[lastUnique]; for(j = lastUnique + 1 ; j < i ; ++ j) { min_index = osg::minimum(min_index, indices[j]); } for(j = lastUnique ; j < i ; ++ j) { remapDuplicatesToOrignals[indices[j]] = min_index; } lastUnique = i; } } unsigned int min_index = indices[lastUnique]; for(j = lastUnique + 1 ; j < i ; ++ j) { min_index = osg::minimum(min_index, indices[j]); } for(j = lastUnique ; j < i ; ++ j) { remapDuplicatesToOrignals[indices[j]] = min_index; } // copy the arrays. IndexList finalMapping(numVertices); IndexList copyMapping; copyMapping.reserve(numUnique); unsigned int currentIndex = 0; for(i = 0 ; i < numVertices ; ++ i) { if (remapDuplicatesToOrignals[i] == i) { finalMapping[i] = currentIndex; copyMapping.push_back(i); currentIndex++; } else { finalMapping[i] = finalMapping[remapDuplicatesToOrignals[i]]; } } // remap any shared vertex attributes RemapArray ra(copyMapping); arrayComparitor.accept(ra); // triangulate faces { TriangleIndexor ti; ti._maxIndex = numVertices; ti._remapping = finalMapping; for(itr = primitives.begin() ; itr != primitives.end() ; ++ itr) { (*itr)->accept(ti); } addDrawElements(ti._indices, osg::PrimitiveSet::TRIANGLES, new_primitives); } // line-ify line-type primitives { LineIndexor li, wi; // lines and wireframes li._maxIndex = numVertices; wi._maxIndex = numVertices; li._remapping = finalMapping; wi._remapping = finalMapping; for(itr = primitives.begin() ; itr != primitives.end() ; ++ itr) { bool isWireframe = false; if((*itr)->getUserValue("wireframe", isWireframe) && isWireframe) { (*itr)->accept(wi); } else { (*itr)->accept(li); } } addDrawElements(li._indices, osg::PrimitiveSet::LINES, new_primitives); addDrawElements(wi._indices, osg::PrimitiveSet::LINES, new_primitives, "wireframe"); } // adds points primitives { IndexList points; for(itr = primitives.begin() ; itr != primitives.end() ; ++ itr) { if((*itr) && (*itr)->getMode() == osg::PrimitiveSet::POINTS) { for(unsigned int k = 0 ; k < (*itr)->getNumIndices() ; ++ k) { points.push_back(finalMapping[(*itr)->index(k)]); } } } addDrawElements(points, osg::PrimitiveSet::POINTS, new_primitives); } geom.setPrimitiveSetList(new_primitives); deduplicator.deduplicateUVs(geom); setProcessed(&geom); }