void doEarClipping( Geometry* planeGeometry, Vec2Array* planeVertices ) { //list of all unclipped vertices vector<int> remainingVertices; for ( int i = 0; i < planeVertices->size(); i++ ){ remainingVertices.push_back( i ); } while(remainingVertices.size() > 0) { DrawElementsUInt* currTriangle = new DrawElementsUInt( PrimitiveSet::TRIANGLES, 0 ); if ( remainingVertices.size() == 3 ) { currTriangle->push_back( remainingVertices[0] ); currTriangle->push_back( remainingVertices[1] ); currTriangle->push_back( remainingVertices[2] ); remainingVertices.clear(); } else { int currEarIndex = findEar( planeVertices, remainingVertices ); if ( currEarIndex == 0 ){ currTriangle->push_back( remainingVertices.back() ); } else { currTriangle->push_back( remainingVertices[(currEarIndex-1)] ); } currTriangle->push_back( remainingVertices[currEarIndex] ); if ( currEarIndex == remainingVertices.size() - 1 ) { currTriangle->push_back( remainingVertices.front() ); } else { currTriangle->push_back( remainingVertices[(currEarIndex+1)] ); } remainingVertices.erase( remainingVertices.begin() + currEarIndex ); } planeGeometry-> addPrimitiveSet( currTriangle ); } }
DrawElementsUInt* combinePolygons( DrawElementsUInt* poly1, DrawElementsUInt* poly2){ DrawElementsUInt* candidatePoly = new DrawElementsUInt( PrimitiveSet::POLYGON, 0 ); for (int i = 0; i < poly1->size(); i++){ candidatePoly->push_back(poly1->at(i)); } //Look for a shared edge int sharedIndex1 = -1; int sharedIndex2 = -1; bool opposite = false; for (int i = 0; i < candidatePoly->size(); i++) { if (sharedIndex1 != -1) { break; } for (int j = 0; j < poly2->size(); j++) { int ind11 = candidatePoly->at(i); int ind12 = candidatePoly->at((i+1) % candidatePoly->size()); int ind21 = poly2->at(j); int ind22 = poly2->at((j+1) % poly2->size()); if ((ind11 == ind21) && (ind12 == ind22)){ sharedIndex1 = i; sharedIndex2 = j; break; } if ((ind11 == ind22) && (ind12 == ind21)){ sharedIndex1 = i; sharedIndex2 = j; opposite = true; break; } } } if (sharedIndex1 == -1){ return NULL; } //insert places elements before selected location, so we go backwards when ordering is the same. if (!opposite){ for (int i = poly2->size()-1; i >= 0; --i) { int index = (sharedIndex2 + i + 2) % poly2->size(); candidatePoly->insert(candidatePoly->begin() + sharedIndex1 + 1, poly2->at(index)); } } else { int offset = sharedIndex1 + 1; for (int i = 0; i < poly2->size() - 2; ++i) { int index = (sharedIndex2 + i + 2) % poly2->size(); candidatePoly->insert(candidatePoly->begin() + offset, poly2->at(index)); offset += 1; } } return candidatePoly; }
/*************************************************************** * Function: initBaseGeometry() ***************************************************************/ void CAVEGeodeSnapWireframeCone::initBaseGeometry() { Vec3Array* vertices = new Vec3Array; float rad = 1.0f, height = 1.0f, intvl = M_PI * 2 / gMinFanSegments; // BaseGeometry contains (gMinFanSegments * 2) vertices for (int i = 0; i < gMinFanSegments; i++) { vertices->push_back(Vec3(rad * cos(i * intvl), rad * sin(i * intvl), height)); } for (int i = 0; i < gMinFanSegments; i++) { vertices->push_back(Vec3(rad * cos(i * intvl), rad * sin(i * intvl), 0)); } mBaseGeometry->setVertexArray(vertices); DrawElementsUInt* topEdges = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); DrawElementsUInt* bottomEdges = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); DrawElementsUInt* sideEdges = new DrawElementsUInt(PrimitiveSet::LINES, 0); for (int i = 0; i < gMinFanSegments; i++) { topEdges->push_back(i); bottomEdges->push_back(i + gMinFanSegments); sideEdges->push_back(i); sideEdges->push_back(i + gMinFanSegments); } topEdges->push_back(0); bottomEdges->push_back(gMinFanSegments); mBaseGeometry->addPrimitiveSet(topEdges); mBaseGeometry->addPrimitiveSet(bottomEdges); mBaseGeometry->addPrimitiveSet(sideEdges); }
/*************************************************************** * Function: initBaseGeometry() ***************************************************************/ void CAVEGeodeSnapWireframeBox::initBaseGeometry() { float xMin, yMin, zMin, xMax, yMax, zMax; xMin = 0.0; xMax = 1.0; yMin = 0.0; yMax = 1.0; zMin = 0.0; zMax = 1.0; Vec3Array* vertices = new Vec3Array; vertices->push_back(Vec3(xMax, yMax, zMax)); vertices->push_back(Vec3(xMin, yMax, zMax)); vertices->push_back(Vec3(xMin, yMin, zMax)); vertices->push_back(Vec3(xMax, yMin, zMax)); vertices->push_back(Vec3(xMax, yMax, zMin)); vertices->push_back(Vec3(xMin, yMax, zMin)); vertices->push_back(Vec3(xMin, yMin, zMin)); vertices->push_back(Vec3(xMax, yMin, zMin)); mBaseGeometry->setVertexArray(vertices); DrawElementsUInt* topEdges = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); DrawElementsUInt* bottomEdges = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); topEdges->push_back(0); bottomEdges->push_back(4); topEdges->push_back(1); bottomEdges->push_back(5); topEdges->push_back(2); bottomEdges->push_back(6); topEdges->push_back(3); bottomEdges->push_back(7); topEdges->push_back(0); bottomEdges->push_back(4); DrawElementsUInt* sideEdges = new DrawElementsUInt(PrimitiveSet::LINES, 0); sideEdges->push_back(0); sideEdges->push_back(4); sideEdges->push_back(1); sideEdges->push_back(5); sideEdges->push_back(2); sideEdges->push_back(6); sideEdges->push_back(3); sideEdges->push_back(7); mBaseGeometry->addPrimitiveSet(topEdges); mBaseGeometry->addPrimitiveSet(bottomEdges); mBaseGeometry->addPrimitiveSet(sideEdges); }
TFColorWidget::TFColorWidget(Interaction *interaction, Measure *b, int size) : Widget() , Events() { _interaction = interaction; Geode *geode = new Geode(); _geom = new Geometry(); _geom->setUseDisplayList(false); _vertices = new Vec3Array(); _vertices->push_back(Vec3(0, 0, 0)); _vertices->push_back(Vec3(0, 0, .2)); _vector = (*_vertices)[1] - (*_vertices)[0]; _vector.normalize(); _geom->setVertexArray(_vertices); DrawElementsUInt *line = new DrawElementsUInt(osg::PrimitiveSet::LINES, 0); line->push_back(0); line->push_back(1); _geom->addPrimitiveSet(line); _colors = new Vec4Array(1); (*_colors)[0].set(0.0, 0.0, 0.0, 1.0); _geom->setColorArray(_colors); _geom->setColorBinding(osg::Geometry::BIND_OVERALL); geode->addDrawable(_geom); _node->addChild(geode); LineWidth *width = new LineWidth(); width->setWidth(size); StateSet *state = _geom->getOrCreateStateSet(); state->setAttribute(width, osg::StateAttribute::ON); state->setMode(GL_LIGHTING, StateAttribute::OFF); _interaction->addListener(this, this); }
//Constructor: Making deep copy of 'refGeometry' without copy of 'mDOCollectorIndex' CAVEGeometry::CAVEGeometry(CAVEGeometry *refGeometry): mDOCollectorIndex(-1) { /* copy primitive set and index clusters */ unsigned int nPrimitiveSets = refGeometry->getNumPrimitiveSets(); if (nPrimitiveSets > 0) { for (int i = 0; i < nPrimitiveSets; i++) { PrimitiveSet* primSetRef = refGeometry->getPrimitiveSet(i); /* support primitive set 'DrawElementsUInt', add more types of primitive sets here if needed */ DrawElementsUInt* drawElementUIntRef = dynamic_cast <DrawElementsUInt*> (primSetRef); if (drawElementUIntRef) { unsigned int nIdices = drawElementUIntRef->getNumIndices(); const GLenum &mode = drawElementUIntRef->getMode(); /* create duplicated primitive set, copy index field and add it to 'this' */ DrawElementsUInt* drawElementUIntDup = new DrawElementsUInt(mode, 0); if (nIdices > 0) { for (int j = 0; j < nIdices; j++) drawElementUIntDup->push_back(drawElementUIntRef->index(j)); } addPrimitiveSet(drawElementUIntDup); } } } /* copy the field of overlapping index by calling addIndexCluster() function sets */ const IndexClusterVector &refClusterVector = refGeometry->mIndexClusterVector; if (refClusterVector.size() > 0) { for (IndexClusterVector::const_iterator itrCluster = refClusterVector.begin(); itrCluster != refClusterVector.end(); itrCluster++) { IndexClusterBase *clusterPtr = *itrCluster; addIndexCluster(clusterPtr); } } }
/*************************************************************** * Function: ANIMCreateSinglePageGeodeAnimation() ***************************************************************/ void ANIMCreateSinglePageGeodeAnimation(const string& texfilename, Geode **flipUpGeode, Geode **flipDownGeode, AnimationPathCallback **flipUpCallback, AnimationPathCallback **flipDownCallback) { /* coordinates of page object */ Vec3 topleft = Vec3(-0.19, 0, 0); Vec3 bottomleft = Vec3(-0.19, 0, -0.28); Vec3 bottomright = Vec3( 0.19, 0, -0.28); Vec3 topright = Vec3( 0.19, 0, 0); Vec3 start = Vec3(0, -0.004, 0); Vec3 end = Vec3(0, 0.008, 0); float pageH = 0.28, pageW = 0.38; /* create page pain geometry */ *flipUpGeode = new Geode; *flipDownGeode = new Geode; Geometry *pageGeometry = new Geometry(); Vec3Array* vertices = new Vec3Array; Vec2Array* texcoords = new Vec2Array(4); Vec3Array* normals = new Vec3Array; vertices->push_back(topleft); (*texcoords)[0].set(0, 1); vertices->push_back(bottomleft); (*texcoords)[1].set(0, 0); vertices->push_back(bottomright); (*texcoords)[2].set(1, 0); vertices->push_back(topright); (*texcoords)[3].set(1, 1); for (int i = 0; i < 4; i++) { normals->push_back(Vec3(0, -1, 0)); } DrawElementsUInt* rectangle = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); rectangle->push_back(0); rectangle->push_back(1); rectangle->push_back(2); rectangle->push_back(3); pageGeometry->addPrimitiveSet(rectangle); pageGeometry->setVertexArray(vertices); pageGeometry->setTexCoordArray(0, texcoords); pageGeometry->setNormalArray(normals); pageGeometry->setNormalBinding(Geometry::BIND_PER_VERTEX); (*flipUpGeode)->addDrawable(pageGeometry); (*flipDownGeode)->addDrawable(pageGeometry); /* apply image textures to page geodes */ Image* imgFloorplan = osgDB::readImageFile(texfilename); int imgW = imgFloorplan->s(); int imgH = imgFloorplan->t(); Texture2D* texFloorplan = new Texture2D(imgFloorplan); texFloorplan->setWrap(Texture::WRAP_S, Texture::CLAMP); texFloorplan->setWrap(Texture::WRAP_T, Texture::CLAMP); float imgRatio = (float) imgW / imgH; float pageRatio = pageW / pageH; if (imgRatio <= pageRatio) { (*texcoords)[0].set((1.0 - pageRatio / imgRatio) * 0.5, 1); (*texcoords)[1].set((1.0 - pageRatio / imgRatio) * 0.5, 0); (*texcoords)[2].set((1.0 + pageRatio / imgRatio) * 0.5, 0); (*texcoords)[3].set((1.0 + pageRatio / imgRatio) * 0.5, 1); } else { (*texcoords)[0].set(0, (1.0 + imgRatio / pageRatio) * 0.5); (*texcoords)[1].set(0, (1.0 - imgRatio / pageRatio) * 0.5); (*texcoords)[2].set(1, (1.0 - imgRatio / pageRatio) * 0.5); (*texcoords)[3].set(1, (1.0 + imgRatio / pageRatio) * 0.5); } Material *transmaterial = new Material; transmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); transmaterial->setAlpha(Material::FRONT_AND_BACK, 0.8f); Material *solidmaterial = new Material; solidmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); solidmaterial->setAlpha(Material::FRONT_AND_BACK, 1.0f); StateSet *flipUpStateSet = (*flipUpGeode)->getOrCreateStateSet(); flipUpStateSet->setTextureAttributeAndModes(0, texFloorplan, StateAttribute::ON); flipUpStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON ); flipUpStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); flipUpStateSet->setAttributeAndModes(transmaterial, StateAttribute::OVERRIDE | StateAttribute::ON); StateSet *flipDownStateSet = (*flipDownGeode)->getOrCreateStateSet(); flipDownStateSet->setTextureAttributeAndModes(0, texFloorplan, StateAttribute::ON); flipDownStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON ); flipDownStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); flipDownStateSet->setAttributeAndModes(solidmaterial, StateAttribute::OVERRIDE | StateAttribute::ON); /* create page flipping animation call backs */ AnimationPath* animationPathFlipUp = new AnimationPath; AnimationPath* animationPathFlipDown = new AnimationPath; animationPathFlipUp->setLoopMode(AnimationPath::NO_LOOPING); animationPathFlipDown->setLoopMode(AnimationPath::NO_LOOPING); Vec3 flipUpOffset, flipDownOffset; Quat flipUpQuat, flipDownQuat; Vec3 offsetstep = (end - start) / ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS; float anglestep = M_PI * 2 / ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS; float timestep = 1.0f / ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS; for (int i = 0; i < ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS + 1; i++) { float val = i * timestep; flipUpOffset = start + offsetstep * i; flipDownOffset = end - offsetstep * i; flipUpQuat = Quat(i * anglestep, Vec3(-1, 0, 0)); flipDownQuat = Quat(i * anglestep, Vec3(1, 0, 0)); animationPathFlipUp->insert(val, AnimationPath::ControlPoint(flipUpOffset, flipUpQuat, Vec3(1, 1, 1))); animationPathFlipDown->insert(val, AnimationPath::ControlPoint(flipDownOffset, flipDownQuat, Vec3(1, 1, 1))); } *flipUpCallback = new AnimationPathCallback(animationPathFlipUp, 0.0, 1.0f / ANIM_SKETCH_BOOK_PAGE_FLIP_TIME); *flipDownCallback = new AnimationPathCallback(animationPathFlipDown, 0.0, 1.0f / ANIM_SKETCH_BOOK_PAGE_FLIP_TIME); }
bool Primitive_readLocalData(Input& fr,osg::Geometry& geom) { bool iteratorAdvanced = false; bool firstMatched = false; if ((firstMatched = fr.matchSequence("DrawArrays %w %i %i %i")) || fr.matchSequence("DrawArrays %w %i %i") ) { GLenum mode; Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode); int first; fr[2].getInt(first); int count; fr[3].getInt(count); int numInstances = 0; if (firstMatched) { fr[4].getInt(numInstances); fr += 5; } else { fr += 4; } geom.addPrimitiveSet(new DrawArrays(mode, first, count, numInstances)); iteratorAdvanced = true; } else if ((firstMatched = fr.matchSequence("DrawArrayLengths %w %i %i %i {")) || fr.matchSequence("DrawArrayLengths %w %i %i {") ) { int entry = fr[1].getNoNestedBrackets(); GLenum mode; Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode); int first; fr[2].getInt(first); int capacity; fr[3].getInt(capacity); int numInstances = 0; if (firstMatched) { fr[4].getInt(numInstances); fr += 6; } else { fr += 5; } DrawArrayLengths* prim = new DrawArrayLengths; prim->setMode(mode); prim->setNumInstances(numInstances); prim->setFirst(first); prim->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int i; if (fr[0].getUInt(i)) { prim->push_back(i); ++fr; } } ++fr; geom.addPrimitiveSet(prim); iteratorAdvanced = true; } else if ((firstMatched = fr.matchSequence("DrawElementsUByte %w %i %i {")) || fr.matchSequence("DrawElementsUByte %w %i {")) { int entry = fr[1].getNoNestedBrackets(); GLenum mode; Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode); int capacity; fr[2].getInt(capacity); int numInstances = 0; if (firstMatched) { fr[3].getInt(numInstances); fr += 5; } else { fr += 4; } DrawElementsUByte* prim = new DrawElementsUByte; prim->setMode(mode); prim->setNumInstances(numInstances); prim->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int i; if (fr[0].getUInt(i)) { prim->push_back(i); ++fr; } } ++fr; geom.addPrimitiveSet(prim); iteratorAdvanced = true; } else if ((firstMatched = fr.matchSequence("DrawElementsUShort %w %i %i {")) || fr.matchSequence("DrawElementsUShort %w %i {")) { int entry = fr[1].getNoNestedBrackets(); GLenum mode; Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode); int capacity; fr[2].getInt(capacity); int numInstances = 0; if (firstMatched) { fr[3].getInt(numInstances); fr += 5; } else { fr += 4; } DrawElementsUShort* prim = new DrawElementsUShort; prim->setMode(mode); prim->setNumInstances(numInstances); prim->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int i; if (fr[0].getUInt(i)) { prim->push_back(i); ++fr; } } ++fr; geom.addPrimitiveSet(prim); iteratorAdvanced = true; } else if ((firstMatched = fr.matchSequence("DrawElementsUInt %w %i %i {")) || fr.matchSequence("DrawElementsUInt %w %i {")) { int entry = fr[1].getNoNestedBrackets(); GLenum mode; Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode); int capacity; fr[2].getInt(capacity); int numInstances = 0; if (firstMatched) { fr[3].getInt(numInstances); fr += 5; } else { fr += 4; } DrawElementsUInt* prim = new DrawElementsUInt; prim->setMode(mode); prim->setNumInstances(numInstances); prim->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int i; if (fr[0].getUInt(i)) { prim->push_back(i); ++fr; } } ++fr; geom.addPrimitiveSet(prim); iteratorAdvanced = true; } return iteratorAdvanced; }
PositionAttitudeTransform *VideoGeode::createVideoPlane(float sizeX, float sizeY, bool texRepeat) { // vertex array Vec3Array *vertexArray = new Vec3Array(); sizeX /= 2.0; sizeY /= 2.0; vertexArray->push_back(Vec3(-sizeX, 0, -sizeY)); vertexArray->push_back(Vec3(sizeX, 0, -sizeY)); vertexArray->push_back(Vec3(sizeX, 0, sizeY)); vertexArray->push_back(Vec3(-sizeX, 0, sizeY)); /*vertexArray->push_back(Vec3(-sizeX, -sizeY, 0)); vertexArray->push_back(Vec3(sizeX, -sizeY, 0)); vertexArray->push_back(Vec3(sizeX, sizeY, 0)); vertexArray->push_back(Vec3(-sizeX, sizeY, 0));*/ // face array DrawElementsUInt *faceArray = new DrawElementsUInt(PrimitiveSet::TRIANGLES, 0); faceArray->push_back(0); // face 1 faceArray->push_back(1); faceArray->push_back(2); faceArray->push_back(2); // face 2 faceArray->push_back(3); faceArray->push_back(0); // normal array Vec3Array *normalArray = new Vec3Array(); normalArray->push_back(Vec3(0, 0, 1)); // normal index TemplateIndexArray<unsigned int, Array::UIntArrayType, 24, 4> *normalIndexArray; normalIndexArray = new TemplateIndexArray<unsigned int, Array::UIntArrayType, 24, 4>(); normalIndexArray->push_back(0); normalIndexArray->push_back(0); normalIndexArray->push_back(0); normalIndexArray->push_back(0); // texture coordinates Vec2Array *texCoords = new Vec2Array(); texCoords->push_back(Vec2(0.0f, 0.0f)); texCoords->push_back(Vec2(1.0f, 0.0f)); texCoords->push_back(Vec2(1.0f, 1.0f)); texCoords->push_back(Vec2(0.0f, 1.0f)); Geometry *geometry = new Geometry(); geometry->setVertexArray(vertexArray); geometry->setNormalArray(normalArray); geometry->setNormalIndices(normalIndexArray); geometry->setNormalBinding(Geometry::BIND_PER_VERTEX); geometry->setTexCoordArray(0, texCoords); geometry->addPrimitiveSet(faceArray); Geode *plane = new Geode(); plane->addDrawable(geometry); // assign the material to the sphere StateSet *planeStateSet = plane->getOrCreateStateSet(); planeStateSet->ref(); planeStateSet->setAttribute(_material); try { planeStateSet->setTextureAttributeAndModes(0, createVideoTexture(texRepeat), StateAttribute::ON); } catch (char *e) { throw e; } PositionAttitudeTransform *planeTransform = new PositionAttitudeTransform(); planeTransform->addChild(plane); return planeTransform; }
/*************************************************************** * Function: createFloorplanGeometry() ***************************************************************/ void VirtualScenicHandler::createFloorplanGeometry(const int numPages, CAVEAnimationModeler::ANIMPageEntry **pageEntryArray) { if (numPages <= 0) { return; } for (int i = 0; i < numPages; i++) { // create floorplan geometry float length = pageEntryArray[i]->mLength; float width = pageEntryArray[i]->mWidth; float altitude = pageEntryArray[i]->mAlti; Geode *floorplanGeode = new Geode; Geometry *floorplanGeometry = new Geometry; Vec3Array* vertices = new Vec3Array; Vec3Array* normals = new Vec3Array; Vec2Array* texcoords = new Vec2Array(4); vertices->push_back(Vec3(-length / 2, width / 2, altitude)); (*texcoords)[0].set(0, 1); vertices->push_back(Vec3(-length / 2, -width / 2, altitude)); (*texcoords)[1].set(0, 0); vertices->push_back(Vec3( length / 2, -width / 2, altitude)); (*texcoords)[2].set(1, 0); vertices->push_back(Vec3( length / 2, width / 2, altitude)); (*texcoords)[3].set(1, 1); for (int k = 0; k < 4; k++) { normals->push_back(Vec3(0, 0, 1)); } DrawElementsUInt* rectangle = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); rectangle->push_back(0); rectangle->push_back(1); rectangle->push_back(2); rectangle->push_back(3); floorplanGeometry->addPrimitiveSet(rectangle); floorplanGeometry->setVertexArray(vertices); floorplanGeometry->setNormalArray(normals); floorplanGeometry->setTexCoordArray(0, texcoords); floorplanGeometry->setNormalBinding(Geometry::BIND_PER_VERTEX); floorplanGeode->addDrawable(floorplanGeometry); mFloorplanSwitch->addChild(floorplanGeode); /* load floorplan images */ Material *transmaterial = new Material; transmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); transmaterial->setAlpha(Material::FRONT_AND_BACK, 1.0f); Image* imgFloorplan = osgDB::readImageFile(pageEntryArray[i]->mTexFilename); Texture2D* texFloorplan = new Texture2D(imgFloorplan); StateSet *floorplanStateSet = floorplanGeode->getOrCreateStateSet(); floorplanStateSet->setTextureAttributeAndModes(0, texFloorplan, StateAttribute::ON); floorplanStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON ); floorplanStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); floorplanStateSet->setAttributeAndModes(transmaterial, StateAttribute::OVERRIDE | StateAttribute::ON); } }
/*************************************************************** * Function: initGeometryCylinder() ***************************************************************/ void CAVEGeodeShape::initGeometryCylinder(const Vec3 &initVect, const Vec3 &sVect) { int numFanSegs = CAVEGeodeSnapWireframeCylinder::gCurFanSegments; float cx = initVect.x(), cy = initVect.y(), cz = initVect.z(); float rad = sVect.x(), height = sVect.z(); if (rad < 0) rad = -rad; if (height < 0) { cz = initVect.z() + height; height = -height; } /* take record of center vector and number of vertices, normals, texcoords */ mCenterVect = Vec3(cx, cy, cz + height * 0.5); mNumVertices = mNumNormals = mNumTexcoords = (numFanSegs + 1) * 4; /* create vertical edges, cap radiating edges and ring strips on side surface */ float intvl = M_PI * 2 / numFanSegs; for (int i = 0; i <= numFanSegs; i++) { const float theta = i * intvl; const float cost = cos(theta); const float sint = sin(theta); mVertexArray->push_back(Vec3(cx, cy, cz) + Vec3(rad * cost, rad * sint, height)); // top surface mVertexArray->push_back(Vec3(cx, cy, cz) + Vec3(rad * cost, rad * sint, 0)); // bottom surface mVertexArray->push_back(Vec3(cx, cy, cz) + Vec3(rad * cost, rad * sint, height)); // upper side mVertexArray->push_back(Vec3(cx, cy, cz) + Vec3(rad * cost, rad * sint, 0)); // lower side mNormalArray->push_back(Vec3(0, 0, 1)); mNormalArray->push_back(Vec3(0, 0, -1)); mNormalArray->push_back(Vec3(cost, sint, 0)); mNormalArray->push_back(Vec3(cost, sint, 0)); mUDirArray->push_back(Vec3(1, 0, 0)); mVDirArray->push_back(Vec3(0, 1, 0)); // top surface mUDirArray->push_back(Vec3(1, 0, 0)); mVDirArray->push_back(Vec3(0, 1, 0)); // bottom surface mUDirArray->push_back(Vec3(0, 0, 0)); mVDirArray->push_back(Vec3(0, 0, 1)); // upper side mUDirArray->push_back(Vec3(0, 0, 0)); mVDirArray->push_back(Vec3(0, 0, 1)); // lower side mTexcoordArray->push_back(Vec2(rad * cost, rad * sint) / gTextureTileSize); mTexcoordArray->push_back(Vec2(rad * cost, rad * sint) / gTextureTileSize); mTexcoordArray->push_back(Vec2(rad * intvl * i, height) / gTextureTileSize); mTexcoordArray->push_back(Vec2(rad * intvl * i, 0.0f) / gTextureTileSize); } /* create geometries for each surface */ CAVEGeometry **geometryArrayPtr = new CAVEGeometry*[3]; for (int i = 0; i < 3; i++) { geometryArrayPtr[i] = new CAVEGeometry; geometryArrayPtr[i]->setVertexArray(mVertexArray); geometryArrayPtr[i]->setNormalArray(mNormalArray); geometryArrayPtr[i]->setTexCoordArray(0, mTexcoordArray); geometryArrayPtr[i]->setNormalBinding(Geometry::BIND_PER_VERTEX); mGeometryVector.push_back(geometryArrayPtr[i]); addDrawable(geometryArrayPtr[i]); } /* write primitive set and index clusters */ DrawElementsUInt* topSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); DrawElementsUInt* bottomSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); for (int i = 0; i <= numFanSegs; i++) { topSurface->push_back(i * 4); bottomSurface->push_back((numFanSegs - i) * 4 + 1); } geometryArrayPtr[0]->addPrimitiveSet(topSurface); geometryArrayPtr[1]->addPrimitiveSet(bottomSurface); for (int i = 0; i < numFanSegs; i++) { DrawElementsUInt* sideSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); sideSurface->push_back(i * 4 + 2); sideSurface->push_back(i * 4 + 3); sideSurface->push_back(i * 4 + 7); sideSurface->push_back(i * 4 + 6); geometryArrayPtr[2]->addPrimitiveSet(sideSurface); } for (int i = 0; i <= numFanSegs; i++) { geometryArrayPtr[0]->addIndexCluster(i * 4 , i * 4 + 2); geometryArrayPtr[1]->addIndexCluster(i * 4 + 1, i * 4 + 3); geometryArrayPtr[2]->addIndexCluster(i * 4 , i * 4 + 2); geometryArrayPtr[2]->addIndexCluster(i * 4 + 1, i * 4 + 3); } }
/*************************************************************** * Function: initGeometryBox() ***************************************************************/ void CAVEGeodeShape::initGeometryBox(const Vec3 &initVect, const Vec3 &sVect) { float xMin, yMin, zMin, xMax, yMax, zMax; xMin = initVect.x(); xMax = initVect.x() + sVect.x(); yMin = initVect.y(); yMax = initVect.y() + sVect.y(); zMin = initVect.z(); zMax = initVect.z() + sVect.z(); if (xMin > xMax) { xMin = initVect.x() + sVect.x(); xMax = initVect.x(); } if (yMin > yMax) { yMin = initVect.y() + sVect.y(); yMax = initVect.y(); } if (zMin > zMax) { zMin = initVect.z() + sVect.z(); zMax = initVect.z(); } Vec3 up, down, front, back, left, right; up = Vec3(0, 0, 1); down = Vec3(0, 0, -1); front = Vec3(0, -1, 0); back = Vec3(0, 1, 0); left = Vec3(-1, 0, 0); right = Vec3(1, 0, 0); /* decide x, y, z span and write 'mCenterVect' */ float xspan = xMax - xMin, yspan = yMax - yMin, zspan = zMax - zMin; mCenterVect = Vec3((xMax + xMin) * 0.5, (yMax + yMin) * 0.5, (zMax + zMin) * 0.5); mNumVertices = mNumNormals = mNumTexcoords = 24; mVertexArray->push_back(Vec3(xMax, yMax, zMax)); mNormalArray->push_back(up); mVertexArray->push_back(Vec3(xMin, yMax, zMax)); mNormalArray->push_back(up); mVertexArray->push_back(Vec3(xMin, yMin, zMax)); mNormalArray->push_back(up); mVertexArray->push_back(Vec3(xMax, yMin, zMax)); mNormalArray->push_back(up); mVertexArray->push_back(Vec3(xMax, yMax, zMin)); mNormalArray->push_back(down); mVertexArray->push_back(Vec3(xMin, yMax, zMin)); mNormalArray->push_back(down); mVertexArray->push_back(Vec3(xMin, yMin, zMin)); mNormalArray->push_back(down); mVertexArray->push_back(Vec3(xMax, yMin, zMin)); mNormalArray->push_back(down); for (int i = 0; i < 8; i++) { mUDirArray->push_back(right); mVDirArray->push_back(back); } for (int i = 0; i < 2; i++) { mTexcoordArray->push_back(Vec2(xspan, yspan) / gTextureTileSize); mTexcoordArray->push_back(Vec2(0, yspan) / gTextureTileSize); mTexcoordArray->push_back(Vec2(0, 0) / gTextureTileSize); mTexcoordArray->push_back(Vec2(xspan, 0) / gTextureTileSize); } mVertexArray->push_back(Vec3(xMax, yMin, zMax)); mNormalArray->push_back(front); mVertexArray->push_back(Vec3(xMin, yMin, zMax)); mNormalArray->push_back(front); mVertexArray->push_back(Vec3(xMin, yMin, zMin)); mNormalArray->push_back(front); mVertexArray->push_back(Vec3(xMax, yMin, zMin)); mNormalArray->push_back(front); mVertexArray->push_back(Vec3(xMax, yMax, zMax)); mNormalArray->push_back(back); mVertexArray->push_back(Vec3(xMin, yMax, zMax)); mNormalArray->push_back(back); mVertexArray->push_back(Vec3(xMin, yMax, zMin)); mNormalArray->push_back(back); mVertexArray->push_back(Vec3(xMax, yMax, zMin)); mNormalArray->push_back(back); for (int i = 0; i < 8; i++) { mUDirArray->push_back(right); mVDirArray->push_back(up); } for (int i = 0; i < 2; i++) { mTexcoordArray->push_back(Vec2(xspan, zspan) / gTextureTileSize); mTexcoordArray->push_back(Vec2(0, zspan) / gTextureTileSize); mTexcoordArray->push_back(Vec2(0, 0) / gTextureTileSize); mTexcoordArray->push_back(Vec2(xspan, 0) / gTextureTileSize); } mVertexArray->push_back(Vec3(xMin, yMax, zMax)); mNormalArray->push_back(left); mVertexArray->push_back(Vec3(xMin, yMin, zMax)); mNormalArray->push_back(left); mVertexArray->push_back(Vec3(xMin, yMin, zMin)); mNormalArray->push_back(left); mVertexArray->push_back(Vec3(xMin, yMax, zMin)); mNormalArray->push_back(left); mVertexArray->push_back(Vec3(xMax, yMax, zMax)); mNormalArray->push_back(right); mVertexArray->push_back(Vec3(xMax, yMin, zMax)); mNormalArray->push_back(right); mVertexArray->push_back(Vec3(xMax, yMin, zMin)); mNormalArray->push_back(right); mVertexArray->push_back(Vec3(xMax, yMax, zMin)); mNormalArray->push_back(right); for (int i = 0; i < 8; i++) { mUDirArray->push_back(back); mVDirArray->push_back(up); } for (int i = 0; i < 2; i++) { mTexcoordArray->push_back(Vec2(yspan, zspan) / gTextureTileSize); mTexcoordArray->push_back(Vec2(0, zspan) / gTextureTileSize); mTexcoordArray->push_back(Vec2(0, 0) / gTextureTileSize); mTexcoordArray->push_back(Vec2(yspan, 0) / gTextureTileSize); } /* create geometries for each surface */ CAVEGeometry **geometryArrayPtr = new CAVEGeometry*[6]; for (int i = 0; i < 6; i++) { geometryArrayPtr[i] = new CAVEGeometry; geometryArrayPtr[i]->setVertexArray(mVertexArray); geometryArrayPtr[i]->setNormalArray(mNormalArray); geometryArrayPtr[i]->setTexCoordArray(0, mTexcoordArray); geometryArrayPtr[i]->setNormalBinding(Geometry::BIND_PER_VERTEX); mGeometryVector.push_back(geometryArrayPtr[i]); addDrawable(geometryArrayPtr[i]); } /* write primitive set and index clusters */ DrawElementsUInt* topSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); DrawElementsUInt* bottomSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); DrawElementsUInt* frontSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); DrawElementsUInt* backSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); DrawElementsUInt* leftSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); DrawElementsUInt* rightSurface = new DrawElementsUInt(PrimitiveSet::POLYGON, 0); topSurface->push_back(0); bottomSurface->push_back(4); topSurface->push_back(1); bottomSurface->push_back(7); topSurface->push_back(2); bottomSurface->push_back(6); topSurface->push_back(3); bottomSurface->push_back(5); topSurface->push_back(0); bottomSurface->push_back(4); frontSurface->push_back(8); backSurface->push_back(12); frontSurface->push_back(9); backSurface->push_back(15); frontSurface->push_back(10); backSurface->push_back(14); frontSurface->push_back(11); backSurface->push_back(13); frontSurface->push_back(8); backSurface->push_back(12); leftSurface->push_back(16); rightSurface->push_back(20); leftSurface->push_back(19); rightSurface->push_back(21); leftSurface->push_back(18); rightSurface->push_back(22); leftSurface->push_back(17); rightSurface->push_back(23); leftSurface->push_back(16); rightSurface->push_back(20); geometryArrayPtr[0]->addPrimitiveSet(topSurface); geometryArrayPtr[1]->addPrimitiveSet(bottomSurface); geometryArrayPtr[2]->addPrimitiveSet(frontSurface); geometryArrayPtr[3]->addPrimitiveSet(backSurface); geometryArrayPtr[4]->addPrimitiveSet(leftSurface); geometryArrayPtr[5]->addPrimitiveSet(rightSurface); geometryArrayPtr[0]->addIndexCluster(0, 12, 20); geometryArrayPtr[0]->addIndexCluster(1, 13, 16); geometryArrayPtr[0]->addIndexCluster(2, 9, 17); geometryArrayPtr[0]->addIndexCluster(3, 8, 21); geometryArrayPtr[1]->addIndexCluster(4, 15, 23); geometryArrayPtr[1]->addIndexCluster(5, 14, 19); geometryArrayPtr[1]->addIndexCluster(6, 10, 18); geometryArrayPtr[1]->addIndexCluster(7, 11, 22); geometryArrayPtr[2]->addIndexCluster(3, 8, 21); geometryArrayPtr[2]->addIndexCluster(2, 9, 17); geometryArrayPtr[2]->addIndexCluster(6, 10, 18); geometryArrayPtr[2]->addIndexCluster(7, 11, 22); geometryArrayPtr[3]->addIndexCluster(0, 12, 20); geometryArrayPtr[3]->addIndexCluster(1, 13, 16); geometryArrayPtr[3]->addIndexCluster(5, 14, 19); geometryArrayPtr[3]->addIndexCluster(4, 15, 23); geometryArrayPtr[4]->addIndexCluster(1, 13, 16); geometryArrayPtr[4]->addIndexCluster(2, 9, 17); geometryArrayPtr[4]->addIndexCluster(6, 10, 18); geometryArrayPtr[4]->addIndexCluster(5, 14, 19); geometryArrayPtr[5]->addIndexCluster(0, 12, 20); geometryArrayPtr[5]->addIndexCluster(3, 8, 21); geometryArrayPtr[5]->addIndexCluster(7, 11, 22); geometryArrayPtr[5]->addIndexCluster(4, 15, 23); }
void BoxShape::generate() { /**************************************************************** * * * Vertices * * * **************************************************************** */ vertices = new Vec3Array(); //assuming box points coming this way: 5-----------6 // /| /| // / | / | // / | / | // 0---4-------3---7 // | / | / // | / | / // |/ |/ // 1-----------2 // check if all 4 points are present, otherwise use default method to calculate vertices if(!genVer) { vertices->push_back(p1); vertices->push_back(p2); vertices->push_back(p3); vertices->push_back(p4); vertices->push_back(p5); vertices->push_back(p6); vertices->push_back(p7); vertices->push_back(p8); //setVertexArray(vertices); } else if (genVer) { // -y = coming out of screen? vertices->push_back(Vec3( center.x()-(width/2) , center.y()-(depth/2) , center.z()+(height/2) )); vertices->push_back(Vec3( center.x()-(width/2) , center.y()-(depth/2) , center.z()-(height/2) )); vertices->push_back(Vec3( center.x()+(width/2) , center.y()-(depth/2) , center.z()-(height/2) )); vertices->push_back(Vec3( center.x()+(width/2) , center.y()-(depth/2) , center.z()+(height/2) )); vertices->push_back(Vec3( center.x()-(width/2) , center.y()+(depth/2) , center.z()-(height/2) )); vertices->push_back(Vec3( center.x()-(width/2) , center.y()+(depth/2) , center.z()+(height/2) )); vertices->push_back(Vec3( center.x()+(width/2) , center.y()+(depth/2) , center.z()+(height/2) )); vertices->push_back(Vec3( center.x()+(width/2) , center.y()+(depth/2) , center.z()-(height/2) )); //setVertexArray(vertices); } setVertexArray(vertices); //front face DrawElementsUInt* frontface = new DrawElementsUInt(PrimitiveSet::QUADS, 0); frontface->push_back(0); frontface->push_back(1); frontface->push_back(2); frontface->push_back(3); addPrimitiveSet(frontface); //back face DrawElementsUInt* backface = new DrawElementsUInt(PrimitiveSet::QUADS, 0); backface->push_back(4); backface->push_back(5); backface->push_back(6); backface->push_back(7); addPrimitiveSet(backface); //top face DrawElementsUInt* topface = new DrawElementsUInt(PrimitiveSet::QUADS, 0); topface->push_back(5); topface->push_back(0); topface->push_back(3); topface->push_back(6); addPrimitiveSet(topface); //bottom face DrawElementsUInt* bottomface = new DrawElementsUInt(PrimitiveSet::QUADS, 0); bottomface->push_back(1); bottomface->push_back(4); bottomface->push_back(7); bottomface->push_back(2); addPrimitiveSet(bottomface); //left face DrawElementsUInt* leftface = new DrawElementsUInt(PrimitiveSet::QUADS, 0); leftface->push_back(0); leftface->push_back(5); leftface->push_back(4); leftface->push_back(1); addPrimitiveSet(leftface); //right face DrawElementsUInt* rightface = new DrawElementsUInt(PrimitiveSet::QUADS, 0); rightface->push_back(3); rightface->push_back(2); rightface->push_back(7); rightface->push_back(6); addPrimitiveSet(rightface); /**************************************************************** * * * normals * * * **************************************************************** */ Vec3Array* normals = new Vec3Array(1); (*normals)[0].set(1.0f, 1.0f, 1.0f); setNormalArray(normals); setNormalBinding(Geometry::BIND_OVERALL); /**************************************************************** * * * colors * * * **************************************************************** */ /*if (!genVer) { colors = new Vec4Array(8); (*colors)[0].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[1].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[2].set(color2.x(), color2.y(), color2.z(), color2.w()); (*colors)[3].set(color2.x(), color2.y(), color2.z(), color2.w()); (*colors)[4].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[5].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[6].set(color2.x(), color2.y(), color2.z(), color2.w()); (*colors)[7].set(color2.x(), color2.y(), color2.z(), color2.w()); } else {*/ colors = new Vec4Array(8); (*colors)[0].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[1].set(color2.x(), color2.y(), color2.z(), color2.w()); (*colors)[2].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[3].set(color2.x(), color2.y(), color2.z(), color2.w()); (*colors)[4].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[5].set(color2.x(), color2.y(), color2.z(), color2.w()); (*colors)[6].set(color1.x(), color1.y(), color1.z(), color1.w()); (*colors)[7].set(color2.x(), color2.y(), color2.z(), color2.w()); //(*colors)[8].set(color1.x(), color1.y(), color1.z(), color1.w()); //(*colors)[9].set(color2.x(), color2.y(), color2.z(), color2.w()); //} setColorArray(colors); setColorBinding(Geometry::BIND_PER_VERTEX); /**************************************************************** * * * stateset and material * * * **************************************************************** */ StateSet* state = getOrCreateStateSet(); state->setMode(GL_BLEND,StateAttribute::ON|StateAttribute::OVERRIDE); Material* mat = new Material(); mat->setAlpha(Material::FRONT_AND_BACK, 0.1); mat->setColorMode(Material::AMBIENT_AND_DIFFUSE); state->setAttributeAndModes(mat,StateAttribute::ON | StateAttribute::OVERRIDE); /**************************************************************** * * * blending * * * **************************************************************** */ BlendFunc* bf = new BlendFunc(BlendFunc::SRC_ALPHA, BlendFunc::ONE_MINUS_SRC_ALPHA ); state->setAttributeAndModes(bf); state->setRenderingHint(StateSet::TRANSPARENT_BIN); state->setMode(GL_LIGHTING, StateAttribute::ON); setStateSet(state); }
/*************************************************************** * Function: resize() ***************************************************************/ void CAVEGeodeSnapWireframeLine::resize(osg::Vec3 &gridVect) { /* Material* material = new Material; material->setAmbient(Material::FRONT_AND_BACK, Vec4(0.0, 1.0, 0.0, 1.0)); material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1.0, 1.0, 0.0, 1.0)); material->setSpecular(Material::FRONT_AND_BACK, osg::Vec4( 1.f, 1.f, 1.f, 1.0f)); material->setAlpha(Material::FRONT_AND_BACK, 1.f); StateSet* stateset = new StateSet(); stateset->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON); setStateSet(stateset); */ // calculate grid vector float snapUnitX, snapUnitY, snapUnitZ; snapUnitX = snapUnitY = snapUnitZ = mSnappingUnitDist; if (mScaleVect.x() < 0) snapUnitX = -mSnappingUnitDist; if (mScaleVect.y() < 0) snapUnitY = -mSnappingUnitDist; if (mScaleVect.z() < 0) snapUnitZ = -mSnappingUnitDist; int xSeg = (int)(abs((int)((mScaleVect.x() + 0.5 * snapUnitX) / mSnappingUnitDist))); int ySeg = (int)(abs((int)((mScaleVect.y() + 0.5 * snapUnitY) / mSnappingUnitDist))); int zSeg = (int)(abs((int)((mScaleVect.z() + 0.5 * snapUnitZ) / mSnappingUnitDist))); Vec3 roundedVect; roundedVect.x() = xSeg * snapUnitX; gridVect.x() = roundedVect.x() / mSnappingUnitDist; roundedVect.y() = ySeg * snapUnitY; gridVect.y() = roundedVect.y() / mSnappingUnitDist; roundedVect.z() = zSeg * snapUnitZ; gridVect.z() = roundedVect.z() / mSnappingUnitDist; mDiagonalVect = roundedVect; // update box corners in 'mBaseGeometry' float xMin, yMin, zMin, xMax, yMax, zMax; xMin = mInitPosition.x(); xMax = xMin + roundedVect.x(); yMin = mInitPosition.y(); yMax = yMin + roundedVect.y(); zMin = mInitPosition.z(); zMax = zMin + roundedVect.z(); Array* baseVertArray = mBaseGeometry->getVertexArray(); if (baseVertArray->getType() == Array::Vec3ArrayType) { Vec3* vertexArrayDataPtr = (Vec3*) (baseVertArray->getDataPointer()); vertexArrayDataPtr[0] = Vec3(xMax, yMax, zMax); vertexArrayDataPtr[1] = Vec3(xMin, yMax, zMax); vertexArrayDataPtr[2] = Vec3(xMin, yMin, zMax); vertexArrayDataPtr[3] = Vec3(xMax, yMin, zMax); vertexArrayDataPtr[4] = Vec3(xMax, yMax, zMin); vertexArrayDataPtr[5] = Vec3(xMin, yMax, zMin); vertexArrayDataPtr[6] = Vec3(xMin, yMin, zMin); vertexArrayDataPtr[7] = Vec3(xMax, yMin, zMin); } mBaseGeometry->dirtyDisplayList(); mBaseGeometry->dirtyBound(); // update snapping wire geometry if (mSnapwireGeometry) removeDrawable(mSnapwireGeometry); mSnapwireGeometry = new Geometry(); Vec3Array* snapvertices = new Vec3Array; int vertoffset = 0; if (xSeg > 1) // (xSeg - 1) * 4 vertices { for (int i = 1; i <= xSeg - 1; i++) { snapvertices->push_back(Vec3(xMin + i * snapUnitX, yMin, zMin)); snapvertices->push_back(Vec3(xMin + i * snapUnitX, yMax, zMin)); snapvertices->push_back(Vec3(xMin + i * snapUnitX, yMax, zMax)); snapvertices->push_back(Vec3(xMin + i * snapUnitX, yMin, zMax)); DrawElementsUInt* edges = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); edges->push_back(vertoffset + (i-1)*4); edges->push_back(vertoffset + (i-1)*4 + 1); edges->push_back(vertoffset + (i-1)*4 + 2); edges->push_back(vertoffset + (i-1)*4 + 3); edges->push_back(vertoffset + (i-1)*4); mSnapwireGeometry->addPrimitiveSet(edges); } vertoffset += (xSeg - 1) * 4; } if (ySeg > 1) // (ySeg - 1) * 4 vertices { for (int i = 1; i <= ySeg - 1; i++) { snapvertices->push_back(Vec3(xMin, yMin + i * snapUnitY, zMin)); snapvertices->push_back(Vec3(xMax, yMin + i * snapUnitY, zMin)); snapvertices->push_back(Vec3(xMax, yMin + i * snapUnitY, zMax)); snapvertices->push_back(Vec3(xMin, yMin + i * snapUnitY, zMax)); DrawElementsUInt* edges = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); edges->push_back(vertoffset + (i-1)*4); edges->push_back(vertoffset + (i-1)*4 + 1); edges->push_back(vertoffset + (i-1)*4 + 2); edges->push_back(vertoffset + (i-1)*4 + 3); edges->push_back(vertoffset + (i-1)*4); mSnapwireGeometry->addPrimitiveSet(edges); } vertoffset += (ySeg - 1) * 4; } if (zSeg > 1) // (zSeg - 1) * 4 vertices { for (int i = 1; i <= zSeg - 1; i++) { snapvertices->push_back(Vec3(xMin, yMin, zMin + i * snapUnitZ)); snapvertices->push_back(Vec3(xMax, yMin, zMin + i * snapUnitZ)); snapvertices->push_back(Vec3(xMax, yMax, zMin + i * snapUnitZ)); snapvertices->push_back(Vec3(xMin, yMax, zMin + i * snapUnitZ)); DrawElementsUInt* edges = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); edges->push_back(vertoffset + (i-1)*4); edges->push_back(vertoffset + (i-1)*4 + 1); edges->push_back(vertoffset + (i-1)*4 + 2); edges->push_back(vertoffset + (i-1)*4 + 3); edges->push_back(vertoffset + (i-1)*4); mSnapwireGeometry->addPrimitiveSet(edges); } } mSnapwireGeometry->setVertexArray(snapvertices); addDrawable(mSnapwireGeometry); }
/*************************************************************** * Function: resize() ***************************************************************/ void CAVEGeodeSnapWireframeCone::resize(osg::Vec3 &gridVect) { // calculate rounded vector float height = mScaleVect.z(), rad = sqrt(mScaleVect.x() * mScaleVect.x() + mScaleVect.y() * mScaleVect.y()); int hSeg, radSeg, fanSeg, hDir = 1; if (height < 0) { height = -height; hDir = -1; } hSeg = (int)(abs((int)(height / mSnappingUnitDist)) + 0.5); radSeg = (int)(abs((int)(rad / mSnappingUnitDist)) + 0.5); fanSeg = (int)(abs((int)(rad * M_PI * 2 / mSnappingUnitDist)) + 0.5); if (fanSeg < gMinFanSegments) fanSeg = gMinFanSegments; float intvl = M_PI * 2 / fanSeg; height = hSeg * mSnappingUnitDist; rad = radSeg * mSnappingUnitDist; gridVect = Vec3(radSeg, 0, hSeg * hDir); mDiagonalVect = Vec3(rad, rad, height * hDir); gCurFanSegments = 10;//fanSeg; // update number of fan segment, this parameter is passed to 'CAVEGeodeShape' // update 'mSnapwireGeometry' geometry, do not use 'mBaseGeometry' anymore if (mBaseGeometry) { removeDrawable(mBaseGeometry); mBaseGeometry = NULL; } if (mSnapwireGeometry) removeDrawable(mSnapwireGeometry); mSnapwireGeometry = new Geometry(); Vec3Array* snapvertices = new Vec3Array; int vertoffset = 0; // create vertical edges, cap radiating edges and ring strips on side surface for (int i = 0; i <= hSeg; i++) { for (int j = 0; j < fanSeg; j++) { float theta = j * intvl; snapvertices->push_back(mInitPosition + Vec3(rad * cos(theta), rad * sin(theta), i * mSnappingUnitDist * hDir)); } } snapvertices->push_back(mInitPosition); snapvertices->push_back(mInitPosition + Vec3(0, 0, height * hDir)); for (int i = 0; i <= hSeg; i++) { DrawElementsUInt* sideRingStrip = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); for (int j = 0; j < fanSeg; j++) { sideRingStrip->push_back(vertoffset + i * fanSeg + j); } sideRingStrip->push_back(vertoffset + i * fanSeg); mSnapwireGeometry->addPrimitiveSet(sideRingStrip); } DrawElementsUInt* sideVerticalEdges = new DrawElementsUInt(PrimitiveSet::LINES, 0); DrawElementsUInt* capRadiatingEdges = new DrawElementsUInt(PrimitiveSet::LINES, 0); for (int j = 0; j < fanSeg; j++) { sideVerticalEdges->push_back(vertoffset + j); sideVerticalEdges->push_back(vertoffset + j + fanSeg * hSeg); capRadiatingEdges->push_back((hSeg + 1) * fanSeg); capRadiatingEdges->push_back(vertoffset + j); capRadiatingEdges->push_back((hSeg + 1) * fanSeg + 1); capRadiatingEdges->push_back(vertoffset + j + fanSeg * hSeg); } mSnapwireGeometry->addPrimitiveSet(sideVerticalEdges); mSnapwireGeometry->addPrimitiveSet(capRadiatingEdges); vertoffset += (hSeg + 1) * fanSeg + 2; // create ring strips on two caps for (int i = 1; i < radSeg; i++) { float r = i * mSnappingUnitDist; for (int j = 0; j < fanSeg; j++) { float theta = j * intvl; snapvertices->push_back(mInitPosition + Vec3(r * cos(theta), r * sin(theta), height * hDir)); snapvertices->push_back(mInitPosition + Vec3(r * cos(theta), r * sin(theta), 0)); } } for (int i = 1; i < radSeg; i++) { DrawElementsUInt* topRingStrip = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); DrawElementsUInt* bottomRingStrip = new DrawElementsUInt(PrimitiveSet::LINE_STRIP, 0); for (int j = 0; j < fanSeg; j++) { topRingStrip->push_back(vertoffset + (i-1) * fanSeg * 2 + j * 2); bottomRingStrip->push_back(vertoffset + (i-1) * fanSeg * 2 + j * 2 + 1); } topRingStrip->push_back(vertoffset + (i-1) * fanSeg * 2); bottomRingStrip->push_back(vertoffset + (i-1) * fanSeg * 2 + 1); mSnapwireGeometry->addPrimitiveSet(topRingStrip); mSnapwireGeometry->addPrimitiveSet(bottomRingStrip); } vertoffset += (radSeg - 1) * fanSeg * 2; mSnapwireGeometry->setVertexArray(snapvertices); addDrawable(mSnapwireGeometry); }