// *************************************************************************** void CTransformShape::traverseLoadBalancingPass0() { CLoadBalancingTrav &loadTrav= getOwnerScene()->getLoadBalancingTrav(); CSkeletonModel *skeleton= getSkeletonModel(); // World Model position const CVector *modelPos; // If this isntance is binded or skinned to a skeleton, take the world matrix of this one as // center for LoadBalancing Resolution. if(skeleton) { // Take the root bone of the skeleton as reference (bone 0) // And so get our position. modelPos= &skeleton->Bones[0].getWorldMatrix().getPos(); } else { // get our position from modelPos= &getWorldMatrix().getPos(); } // Then compute distance from camera. float modelDist= ( loadTrav.CamPos - *modelPos).norm(); // Get the number of triangles this model use now. _FaceCount= getNumTriangles(modelDist); _LoadBalancingGroup->addNbFacesPass0(_FaceCount); }
void Writer::writeTrianglesRaw(size_type first, size_type count, const std::tr1::uint8_t *data) { MLSGPU_ASSERT(isOpen(), state_error); MLSGPU_ASSERT(first + count <= getNumTriangles() && first <= std::numeric_limits<size_type>::max() - count, std::out_of_range); Statistics::Timer timer(writeTrianglesTime); handle->write(data, count * triangleSize, triangleStart + first * triangleSize); }
void TriMesh::recalculateNormals() { mNormals.assign( mVertices.size(), Vec3f::zero() ); size_t n = getNumTriangles(); for( size_t i = 0; i < n; ++i ) { uint32_t index0 = mIndices[i * 3]; uint32_t index1 = mIndices[i * 3 + 1]; uint32_t index2 = mIndices[i * 3 + 2]; Vec3f v0 = mVertices[ index0 ]; Vec3f v1 = mVertices[ index1 ]; Vec3f v2 = mVertices[ index2 ]; Vec3f e0 = v1 - v0; Vec3f e1 = v2 - v0; Vec3f normal = e0.cross(e1).normalized(); mNormals[ index0 ] += normal; mNormals[ index1 ] += normal; mNormals[ index2 ] += normal; } std::for_each( mNormals.begin(), mNormals.end(), std::mem_fun_ref(&Vec3f::normalize) ); }
//============================================================================== void cTriangleArray::compress() { // get number of allocated triangles unsigned int numTriangles = getNumTriangles(); // remove non allocated triangles unsigned j=-1; for (int i=0; i<numTriangles; i++) { if (getAllocated(i)) { j++; if (i!=j) { unsigned int index0 = getVertexIndex0(i); unsigned int index1 = getVertexIndex1(i); unsigned int index2 = getVertexIndex2(i); m_allocated[j] = true; setVertices(j, index0, index1, index2); } } } // resize arrays unsigned size = j+1; m_allocated.resize(size); m_indices.resize(3*size); }
bool PViewData::writeSTL(const std::string &fileName) { FILE *fp = Fopen(fileName.c_str(), "w"); if(!fp){ Msg::Error("Unable to open file '%s'", fileName.c_str()); return false; } if(!getNumTriangles() && !getNumQuadrangles()){ Msg::Error("No surface elements to save"); fclose(fp); return false; } int step = getFirstNonEmptyTimeStep(); fprintf(fp, "solid Created by Gmsh\n"); for(int ent = 0; ent < getNumEntities(step); ent++){ for(int ele = 0; ele < getNumElements(step, ent); ele++){ if(getDimension(step, ent, ele) != 2) continue; if(skipElement(step, ent, ele)) continue; int N = getNumNodes(step, ent, ele); if(N != 3 && N != 4) continue; double x[4], y[4], z[4], n[3]; for(int i = 0; i < N; i++) getNode(step, ent, ele, i, x[i], y[i], z[i]); normal3points(x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2], n); if(N == 3){ fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]); fprintf(fp, " outer loop\n"); fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]); fprintf(fp, " vertex %g %g %g\n", x[1], y[1], z[1]); fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]); fprintf(fp, " endloop\n"); fprintf(fp, "endfacet\n"); } else{ fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]); fprintf(fp, " outer loop\n"); fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]); fprintf(fp, " vertex %g %g %g\n", x[1], y[1], z[1]); fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]); fprintf(fp, " endloop\n"); fprintf(fp, "endfacet\n"); fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]); fprintf(fp, " outer loop\n"); fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]); fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]); fprintf(fp, " vertex %g %g %g\n", x[3], y[3], z[3]); fprintf(fp, " endloop\n"); fprintf(fp, "endfacet\n"); } } } fprintf(fp, "endsolid Created by Gmsh\n"); fclose(fp); return true; }
void Writer::writeTrianglesRaw( Timeplot::Worker &tworker, size_type first, size_type count, const boost::shared_ptr<AsyncWriterItem> &data, AsyncWriter &async) { MLSGPU_ASSERT(isOpen(), state_error); MLSGPU_ASSERT(first + count <= getNumTriangles() && first <= std::numeric_limits<size_type>::max() - count, std::out_of_range); async.push(tworker, data, handle, count * triangleSize, triangleStart + first * triangleSize); }
void Writer::open(const std::string &filename) { MLSGPU_ASSERT(!isOpen(), state_error); handle = handleFactory(); handle->open(filename); std::string header = makeHeader(); handle->resize(header.size() + getNumVertices() * vertexSize + getNumTriangles() * triangleSize); handle->write(header.data(), header.size(), 0); vertexStart = header.size(); triangleStart = vertexStart + getNumVertices() * vertexSize; }
char BaseMesh::selectComponent(IntersectionContext * ctx) const { Vector3F threeCorners[3]; unsigned fvi[3]; const unsigned nf = getNumTriangles(); for(unsigned i = 0; i < nf; i++) { getTriangle(i, fvi); threeCorners[0] = _vertices[fvi[0]]; threeCorners[1] = _vertices[fvi[1]]; threeCorners[2] = _vertices[fvi[2]]; if(triangleIntersect(threeCorners, ctx)) { ctx->m_componentIdx = BaseMesh::closestVertex(i, ctx->m_hitP);; return 1; } } return 0; }
void Writer::writeTriangles(size_type first, size_type count, const std::tr1::uint32_t *data) { MLSGPU_ASSERT(isOpen(), state_error); MLSGPU_ASSERT(first + count <= getNumTriangles() && first <= std::numeric_limits<size_type>::max() - count, std::out_of_range); Statistics::Timer timer(writeTrianglesTime); BinaryWriter::offset_type pos = triangleStart + first * triangleSize; while (count > 0) { const unsigned int bufferTriangles = 8192; char buffer[bufferTriangles * triangleSize]; char *ptr = buffer; unsigned int triangles = std::min(size_type(bufferTriangles), count); for (unsigned int i = 0; i < triangles; i++, ptr += triangleSize, data += 3) { ptr[0] = 3; std::memcpy(ptr + 1, data, 3 * sizeof(data[0])); } pos += handle->write(buffer, ptr - buffer, pos); count -= triangles; } }
void BaseMesh::verbose() const { std::cout<<"mesh status:\n"; std::cout<<" num vertices "<<getNumVertices()<<"\n"; std::cout<<" num polygons "<<getNumPolygons()<<"\n"; std::map<unsigned, unsigned> faceCountList; for(unsigned i = 0; i < getNumPolygons(); i++) { if(faceCountList.find(m_polygonCounts[i]) == faceCountList.end()) faceCountList[m_polygonCounts[i]] = 1; else faceCountList[m_polygonCounts[i]] += 1; } std::map<unsigned, unsigned>::const_iterator it = faceCountList.begin(); for(; it != faceCountList.end(); ++it) { std::clog<<" "<<(*it).second<<" "<<(*it).first<<"-sided faces\n"; } std::cout<<" num polygon face vertices "<<getNumPolygonFaceVertices()<<"\n"; std::cout<<" num triangles "<<getNumTriangles()<<"\n"; std::cout<<" num quads "<<numQuads()<<"\n"; std::cout<<" num uvs "<<getNumUVs()<<"\n"; std::cout<<" num uvs indices "<<getNumUVIds()<<"\n"; }
//============================================================================== cTriangleArrayPtr cTriangleArray::copy() { // create new array of triangles cTriangleArrayPtr triangleArray = cTriangleArray::create(m_vertices); // get number of allocated triangles unsigned int numTriangles = getNumTriangles(); // copy every allocated triangle for (unsigned int i=0; i<numTriangles; i++) { if (getAllocated(i)) { unsigned int index0 = getVertexIndex0(i); unsigned int index1 = getVertexIndex1(i); unsigned int index2 = getVertexIndex2(i); triangleArray->newTriangle(index0, index1, index2); } } // return new triangle array return (triangleArray); }
void Geometry::write(IResource* resource) { ChunkHeader geometryHeader( BA_GEOMETRY, sizeof( Chunk ) ); geometryHeader.write( resource ); Chunk chunk; chunk.id = (auid)( this ); memset( chunk.name, 0, engine::maxNameLength ); if( _name.length() > engine::maxNameLength ) { strncpy( chunk.name, _name.c_str(), engine::maxNameLength - 1 ); } else { strcpy( chunk.name, _name.c_str() ); } chunk.numShaders = getNumShaders(); chunk.numVertices = getNumVertices(); chunk.numUVSets = getNumUVSets(); chunk.numPrelights = getNumPrelights(); chunk.numTriangles = getNumTriangles(); chunk.sharedShaders = _sharedShaders; if( _ocTreeRoot ) { chunk.numOcTreeSectors = _ocTreeRoot->getOcTreeSize(); } else { chunk.numOcTreeSectors = 0; } chunk.hasEffect = ( _effect != NULL ); fwrite( &chunk, sizeof( Chunk ), 1, resource->getFile() ); // write shaders if( !_sharedShaders ) for( int i=0; i<getNumShaders(); i++ ) { _shaders[i]->write( resource ); } // write vertices ChunkHeader verticesHeader( BA_BINARY, sizeof(Vector) * getNumVertices() ); verticesHeader.write( resource ); fwrite( getVertices(), verticesHeader.size, 1, resource->getFile() ); // write normals ChunkHeader normalsHeader( BA_BINARY, sizeof(Vector) * getNumVertices() ); normalsHeader.write( resource ); fwrite( getNormals(), normalsHeader.size, 1, resource->getFile() ); // write UV-sets int i; for( i=0; i<getNumUVSets(); i++ ) { ChunkHeader uvsHeader( BA_BINARY, sizeof(Flector) * getNumVertices() ); uvsHeader.write( resource ); fwrite( getUVSet(i), uvsHeader.size, 1, resource->getFile() ); } // write prelights for( i=0; i< getNumPrelights(); i++ ) { ChunkHeader prelightsHeader( BA_BINARY, sizeof(Color) * getNumVertices() ); prelightsHeader.write( resource ); fwrite( getPrelights(i), prelightsHeader.size, 1, resource->getFile() ); } // write triangles ChunkHeader trianglesHeader( BA_BINARY, sizeof(Triangle) * getNumTriangles() ); trianglesHeader.write( resource ); fwrite( getTriangles(), trianglesHeader.size, 1, resource->getFile() ); // write shaders if( !_sharedShaders ) { ChunkHeader shadersHeader( BA_BINARY, sizeof(auid) * getNumShaders() ); shadersHeader.write( resource ); fwrite( _shaders, shadersHeader.size, 1, resource->getFile() ); } // write octree if( _ocTreeRoot ) { _ocTreeRoot->write( resource ); } // write effect if( chunk.hasEffect ) { reinterpret_cast<Effect*>( _effect )->write( resource ); } }
unsigned BaseMesh::getNumFaces() const { return getNumTriangles(); }
// @todo remove ... or just remove this complete class void VertexData::debugDraw(int drawMode) { #if ROXLU_GL_MODE != ROXLU_GL_STRICT glPushAttrib(GL_ALL_ATTRIB_BITS); glDisable(GL_TEXTURE_2D); glUseProgram(0); glEnable(GL_DEPTH_TEST); // we adjust the size of the normals lines dynamically float line_length = 0.2; if(vertices.size() > 2) { float length = (vertices[1] - vertices[2]).length(); if(length >= 3) { line_length = 3.2; } } float colors[4][3] = { {0.88,0.25,0.11} ,{0.6,0.78,0.212} ,{0.2,0.65,0.698} ,{0.94,0.72,0.29} }; // draw using indices if(getNumTriangles() > 0) { // triangles (fills) Triangle tri; glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glColor3f(0.8,0.8,0.8); glBegin(GL_TRIANGLES); for(int i = 0; i < triangles.size(); ++i) { tri = triangles[i]; glVertex3fv(vertices[tri.va].getPtr()); glVertex3fv(vertices[tri.vb].getPtr()); glVertex3fv(vertices[tri.vc].getPtr()); } glEnd(); // triangles lines. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glColor3f(0.1,0.1,0.1); glBegin(GL_TRIANGLES); for(int i = 0; i < triangles.size(); ++i) { tri = triangles[i]; glVertex3fv(vertices[tri.va].getPtr()); glVertex3fv(vertices[tri.vb].getPtr()); glVertex3fv(vertices[tri.vc].getPtr()); } glEnd(); // normals, tangents and binormals bool draw_normals= true; bool draw_tangents = true; bool draw_binormals = true; float s = 0.2; glBegin(GL_LINES); for(int i = 0; i < triangles.size(); ++i) { tri = triangles[i]; // normal - a if(draw_normals && tri.na) { glColor3f(0,1,1); glVertex3fv(vertices[tri.va].getPtr()); glVertex3fv((vertices[tri.va]+normals[tri.na]*s).getPtr()); } // tangent - a if(draw_tangents && tri.ta) { glColor3f(1,0,0); glVertex3fv(vertices[tri.va].getPtr()); glVertex3fv((vertices[tri.va]+(tangents[tri.ta]*s)).getPtr()); } // binormal - a if(draw_binormals && tri.ba) { glColor3f(0,1,0); glVertex3fv(vertices[tri.va].getPtr()); glVertex3fv((vertices[tri.va]+(binormals[tri.ba]*s)).getPtr()); } // normal - b if(draw_normals && tri.nb) { glColor3f(0,1,1); glVertex3fv(vertices[tri.vb].getPtr()); glVertex3fv((vertices[tri.vb]+normals[tri.nb]*s).getPtr()); } // tangent - b if(draw_tangents && tri.tb) { glColor3f(1,0,0); glVertex3fv(vertices[tri.vb].getPtr()); glVertex3fv((vertices[tri.vb]+(tangents[tri.tb]*s)).getPtr()); } // binormal - b if(draw_binormals && tri.bb) { glColor3f(0,1,0); glVertex3fv(vertices[tri.vb].getPtr()); glVertex3fv((vertices[tri.vb]+(binormals[tri.bb]*s)).getPtr()); } // normal - c if(draw_normals && tri.nc) { glColor3f(0,1,1); glVertex3fv(vertices[tri.vc].getPtr()); glVertex3fv((vertices[tri.vc]+normals[tri.nc]*s).getPtr()); } // tangent - c if(draw_tangents && tri.tc) { glColor3f(1,0,0); glVertex3fv(vertices[tri.vc].getPtr()); glVertex3fv((vertices[tri.vc]+(tangents[tri.tc]*s)).getPtr()); } // binormal - c if(draw_binormals && tri.bc) { glColor3f(0,1,0); glVertex3fv(vertices[tri.vc].getPtr()); glVertex3fv((vertices[tri.vc]+(binormals[tri.bc]*s)).getPtr()); } } glEnd(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } else if(getNumIndices() > 0) { if(getNumTexCoords() > 0) { int len = indices.size(); glBegin(drawMode); for(int i = 0; i < len; ++i) { glTexCoord2fv(texcoords[indices[i]].getPtr()); glVertex3fv(vertices[indices[i]].getPtr()); } glEnd(); } else { int len = indices.size(); int mod = (drawMode == GL_QUADS) ? 4 : 3; Vec4 colors[4]; colors[0].set(1,0,0, 0.8); colors[1].set(0,1,0, 0.8); colors[2].set(0,0,1, 0.8); colors[3].set(1,1,0, 0.8); glBegin(drawMode); for(int i = 0; i < len; ++i) { glColor4fv(colors[i%mod].getPtr()); glVertex3fv(vertices[indices[i]].getPtr()); } glEnd(); if(normals.size() > 0) { glBegin(GL_LINES); for(int i = 0; i < len; ++i) { int dx = indices[i]; //printf("%d <--\n", dx); Vec3 pos = vertices[dx]; Vec3 norm = normals[dx]; Vec3 end = pos + (norm.scale(line_length*2)); glColor4f(1.0f,0.0f,0.4f,1.0f); glColor4f(0.98, 0.92, 0.75, 0.6); glVertex3fv(pos.getPtr()); glColor4f(1.0f, 0.0f,1.0f,1.0f); glColor3f(0.98, 0.92, 0.75); glColor4f(0.98, 0.92, 0.75,0.6); glVertex3fv(end.getPtr()); } glEnd(); } } } // w/o indices else { glColor4f(1,1,1,1); glUseProgram(0); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); bool draw_texture = false; bool draw_tangents = true; bool draw_normals = true; bool draw_binormals = true; if(draw_texture && getNumTexCoords() > 0) { glColor3f(0.98, 0.92, 0.75); // yellowish int len = vertices.size(); glEnable(GL_TEXTURE_2D); glBegin(drawMode); for(int i = 0; i < len; ++i) { int parts = 3; if(drawMode == GL_QUADS) { parts = 4; } glColor3fv(colors[i%parts]); glTexCoord2fv(texcoords[i].getPtr()); glVertex3fv(vertices[i].getPtr()); } glEnd(); glDisable(GL_TEXTURE_2D); } else { // shape //glColor3f(0.98, 0.92, 0.75); // yellowish glColor3f(0.8,0.8,0.8); glBegin(drawMode); vector<Vec3>::iterator it = vertices.begin(); float len = vertices.size(); while(it != vertices.end()) { glVertex3fv((*it).getPtr()); ++it; } glEnd(); } // normals. int len = normals.size(); if(draw_normals && len == vertices.size()) { glColor3f(0,1,1); glLineWidth(3.0); glBegin(GL_LINES); for(int i = 0; i < len; ++i) { Vec3 pos = vertices[i]; Vec3 norm = normals[i]; norm.normalize(); Vec3 end = pos + (norm.scale(line_length)); glVertex3fv(pos.getPtr()); glVertex3fv(end.getPtr()); } glEnd(); } // tangents if(draw_tangents && tangents.size() > 0) { glLineWidth(3.0); glColor3f(1.0f,0.0f,0.0f); int len = tangents.size(); glBegin(GL_LINES); for(int i = 0; i < len; ++i) { Vec3 pos = vertices[i]; Vec3 norm = tangents[i]; norm.normalize(); Vec3 end = pos + (norm.scale(line_length)); glVertex3fv(pos.getPtr()); glVertex3fv(end.getPtr()); }; glEnd(); } // binormals if(draw_binormals && binormals.size() > 0) { glLineWidth(3.0); glColor4f(0.0f,1.0f,0.0f,1.0f); int len = tangents.size(); glBegin(GL_LINES); for(int i = 0; i < len; ++i) { Vec3 pos = vertices[i]; Vec3 binorm = binormals[i].normalize(); Vec3 end = pos + (binorm.scale(line_length)); glVertex3fv(pos.getPtr()); glVertex3fv(end.getPtr()); }; glEnd(); } // draw lines glPolygonOffset(-1.0f, -1.0f); glEnable(GL_POLYGON_OFFSET_LINE); glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLineWidth(0.5f); glDisable(GL_TEXTURE_2D); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glColor3f(0.4,0.4,0.4); glBegin(drawMode); for(int i = 0; i < vertices.size(); ++i) { glVertex3fv(vertices[i].getPtr()); } glEnd(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_TEXTURE_2D); glDisable(GL_POLYGON_OFFSET_LINE); glLineWidth(1.0f); glDisable(GL_BLEND); glDisable(GL_LINE_SMOOTH); glColor3f(1,1,1); } // if(!blend_enabled) { // glDisable(GL_BLEND); // } glPopAttrib(); #endif // roxlu_gl_variant }