Geode* createTextureQuad(Texture2D *texture) { Vec3Array *vertices = new Vec3Array; vertices->push_back(Vec3(-1.0, -1.0, 0.0)); vertices->push_back(Vec3(1.0, -1.0, 0.0)); vertices->push_back(Vec3(1.0, 1.0, 0.0)); vertices->push_back(Vec3(-1.0, 1.0, 0.0)); Vec2Array *texcoord = new Vec2Array; texcoord->push_back(Vec2(0.0, 0.0)); texcoord->push_back(Vec2(1.0, 0.0)); texcoord->push_back(Vec2(1.0, 1.0)); texcoord->push_back(Vec2(0.0, 1.0)); Geometry *geom = new Geometry; geom->setVertexArray(vertices); geom->setTexCoordArray(0, texcoord); geom->addPrimitiveSet(new DrawArrays(GL_QUADS, 0, 4)); Geode *geode = new Geode; geode->addDrawable(geom); geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, StateAttribute::ON); return geode; }
/** This creates the wireframe PickBox around the widget. Volume vertex names: <PRE> 4____ 7 y /___ /| | 0| 3| | |___x | 5 | /6 / |/___|/ z 1 2 </PRE> */ Geometry *PickBox::createWireframe(const Vec4 &color) { Geometry *geom = new Geometry(); updateVertices(geom); // Set colors: Vec4Array *colors = new Vec4Array(); colors->push_back(color); geom->setColorArray(colors); geom->setColorBinding(Geometry::BIND_OVERALL); // Set normals: Vec3Array *normals = new Vec3Array(); normals->push_back(Vec3(0.0f, 0.0f, 1.0f)); geom->setNormalArray(normals); geom->setNormalBinding(Geometry::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords // to use since we know up front: geom->addPrimitiveSet(new DrawArrays(PrimitiveSet::LINES, 0, 24)); geom->setUseDisplayList(false); // allow dynamic changes return geom; }
/*************************************************************** * 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); }
bool Object3D::findNearestVertex(Vec3f point, std::pair<unsigned int, Vec3f>& foundVertex) { bool found = false; unsigned int nbDrawables = getNumDrawables(); Vec3Array *vertexArray = NULL; double distMin = 100.0; double dist; for(unsigned int i=0; i<nbDrawables; i++) { vertexArray = getVertexList(i); if(vertexArray) { std::vector<Vec3>::iterator itr; for (itr=vertexArray->begin(); itr != vertexArray->end(); ++itr) { dist = Util::computeDist(*itr, point); if(dist < distMin) { distMin = dist; foundVertex = std::make_pair(i, *itr); found = true; } } } } return found; }
Geode* ChessUtils::createRectangleWithTexture(Vec3 centerPosition, Image* image, int width, int height, Vec4 color) { int halfWidth = width / 2; int halfHeight = height / 2; Vec3Array* vertices = new Vec3Array(); vertices->push_back(Vec3(centerPosition.x() - halfWidth, centerPosition.y() - halfHeight, centerPosition.z())); vertices->push_back(Vec3(centerPosition.x() + halfWidth, centerPosition.y() - halfHeight, centerPosition.z())); vertices->push_back(Vec3(centerPosition.x() + halfWidth, centerPosition.y() + halfHeight, centerPosition.z())); vertices->push_back(Vec3(centerPosition.x() - halfWidth, centerPosition.y() + halfHeight, centerPosition.z())); Vec3Array* normals = new Vec3Array(); normals->push_back(Vec3(0.0f, 0.0f, 1.0f)); 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)); Vec4Array* colors = new Vec4Array(); colors->push_back(color); Geometry* quad = new Geometry(); quad->setVertexArray(vertices); quad->setNormalArray(normals); quad->setNormalBinding(osg::Geometry::BIND_OVERALL); quad->setColorArray(colors); quad->setColorBinding(osg::Geometry::BIND_OVERALL); quad->setTexCoordArray(0, texcoords); quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4)); Texture2D* texture = new Texture2D(); if (image != NULL) { texture->setImage(image); } Geode* geode = new Geode(); geode->addDrawable(quad); osg::BlendFunc* blendFunc = new osg::BlendFunc(); blendFunc->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); osg::TexEnv* blendTexEnv = new osg::TexEnv(); blendTexEnv->setMode(osg::TexEnv::BLEND); osg::StateSet* geodeStateset = geode->getOrCreateStateSet(); geodeStateset->setAttributeAndModes(blendFunc); geodeStateset->setTextureAttribute(0, blendTexEnv); geodeStateset->setTextureAttributeAndModes(0, texture); geodeStateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); return geode; }
void DragonBustNode::genBezierSubdivide(Vec3Array g, uint depth, Vec3Array& points) { /* * Based on code from "Curved Surfaces Using Bézier Patches" * http://www.gamasutra.com/view/feature/131755/curved_surfaces_using_bzier_.php?page=6 * See: Bibliography. */ if (depth == 0) { points.push_back(g[0]); points.push_back(g[2]); return; } Vec3Array gl(3); Vec3Array gr(3); gl[0] = g[0]; gl[1] = (g[0] + g[1]) * 0.5f; gr[1] = (g[1] + g[2]) * 0.5f; gl[2] = gr[0] = (gl[1] + gr[1]) * 0.5f; gr[2] = g[2]; genBezierSubdivide(gl, --depth, points); genBezierSubdivide(gr, depth, points); }
OpenRAVE::GraphHandlePtr OSGViewer::plot3(const float* ppoints, int numPoints, int stride, float pointsize, const float* colors, int drawstyle, bool bhasalpha) { osg::Geometry* geom = new osg::Geometry; int floats_per_pt = stride / sizeof(float); Vec3Array* osgPts = new Vec3Array; osgPts->reserve(numPoints); for (int i=0; i < numPoints; ++i) { const float* p = ppoints + i*floats_per_pt; if (isfinite(p[0])) osgPts->push_back(osg::Vec3(p[0], p[1], p[2])); } osg::StateSet* ss = geom->getOrCreateStateSet(); osg::Point *point = new osg::Point(); point->setSize(pointsize); ss->setAttribute(point); ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF); osg::BlendFunc* blendFunc = new osg::BlendFunc; blendFunc->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); ss->setAttributeAndModes(blendFunc); ss->setMode(GL_BLEND, osg::StateAttribute::ON); geom->setVertexArray(osgPts); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,osgPts->size())); // // if (colors != NULL) { // Vec4Array* osgCols = new Vec4Array; // for (int i=0; i < numPoints; ++i) { // float* p = colors + i; // osgCols->push_back(osg::Vec4(p[0], p[1], p[2],1)); // } // geom->setColorArray(osgCols); // geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); // } Geode* geode = new osg::Geode(); geode->addDrawable(geom); return GraphHandlePtr(new OsgGraphHandle(geode, m_root.get())); }
void SWWReaderTest::testBedslopeNormalArray() { CPPUNIT_ASSERT( _sww->isValid() ); osg::ref_ptr<osg::Vec3Array> actual = _sww->getBedslopeNormalArray(); CPPUNIT_ASSERT( actual ); // expected number of bedslope normals const size_t nvertices = 24; CPPUNIT_ASSERT_EQUAL( actual->size(), nvertices ); // hard-coded values (bedslope is flat plane) using osg::Vec3; using osg::Vec3Array; Vec3Array *expected = new Vec3Array; expected->assign(24, Vec3( 0.301511, -0.301511, 0.904534 ) ); for (size_t i=0; i<nvertices; i++) CPPUNIT_ASSERT_VEC3_EQUAL( actual->at(i), expected->at(i) ); }
void SWWReaderTest::testBedslopeVertexArray() { osg::ref_ptr<osg::Vec3Array> actual = _sww->getBedslopeVertexArray(); CPPUNIT_ASSERT( _sww->isValid() ); CPPUNIT_ASSERT( actual ); // expected number of bedslope vertices const size_t nvertices = 20; CPPUNIT_ASSERT_EQUAL( actual->size(), nvertices ); // hard-coded values extracted from sww file - note that these values are normalised by the reader using osg::Vec3; using osg::Vec3Array; Vec3Array *expected = new Vec3Array(20); (*expected)[ 0] = Vec3(-0.5, -0.5, 0.333333); (*expected)[ 1] = Vec3(-0.5, -0.166667, 0.444444); (*expected)[ 2] = Vec3(-0.5, 0.166667, 0.555556); (*expected)[ 3] = Vec3(-0.5, 0.5, 0.666667); (*expected)[ 4] = Vec3(-0.25, -0.5, 0.25); (*expected)[ 5] = Vec3(-0.25, -0.166667, 0.361111); (*expected)[ 6] = Vec3(-0.25, 0.166667, 0.472222); (*expected)[ 7] = Vec3(-0.25, 0.5, 0.583333); (*expected)[ 8] = Vec3(0, -0.5, 0.166667); (*expected)[ 9] = Vec3(0, -0.166667, 0.277778); (*expected)[10] = Vec3(0, 0.166667, 0.388889); (*expected)[11] = Vec3(0, 0.5, 0.5); (*expected)[12] = Vec3(0.25, -0.5, 0.0833333); (*expected)[13] = Vec3(0.25, -0.166667, 0.194444); (*expected)[14] = Vec3(0.25, 0.166667, 0.305556); (*expected)[15] = Vec3(0.25, 0.5, 0.416667); (*expected)[16] = Vec3(0.5, -0.5, 0); (*expected)[17] = Vec3(0.5, -0.166667, 0.111111); (*expected)[18] = Vec3(0.5, 0.166667, 0.222222); (*expected)[19] = Vec3(0.5, 0.5, 0.333333); for (size_t i=0; i<nvertices; i++) { CPPUNIT_ASSERT_VEC3_EQUAL( expected->at(i), actual->at(i) ); } }
Hyperspace::Hyperspace() { setGlows(false); //start by creating a bunch of "stars" int stars = 2500; osg::Geode* geode = new Geode(); Geometry* geom = new Geometry; geode->addDrawable(geom); Vec4Array* colors = new Vec4Array(); colors->push_back(Vec4(1, 1, 1, 1)); geom->setColorArray(colors); geom->setColorBinding(Geometry::BIND_OVERALL); Vec3Array* verts = new Vec3Array(); geom->setVertexArray(verts); geom->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLES, 0, stars*3)); geom->getOrCreateStateSet()->setMode(GL_LIGHTING, false); for(int i = 0; i < stars; i++) { float radius = Util::random(10, 150); float theta = 6.28 * Util::loggedRandom("Hyperspace theta") / RAND_MAX; float s = cosf(theta); float c = sinf(theta); Vec3 localUp(-s, c, 0); float width = Util::random(0.1, 0.25) * radius / 25;; float length = Util::random(10, 30) * radius / 20; float z = 0; Vec3 basePos(c*radius, s*radius, z+length*0.5); Vec3 zDir(0, 0, 1); verts->push_back(basePos + zDir * length * 0.5 + localUp * width * 0); // verts->push_back(basePos + zDir * length * 0.5 - localUp * width * 0.5); verts->push_back(basePos - zDir * length * 0.5 - localUp * width * 0.5); verts->push_back(basePos - zDir * length * 0.5 + localUp * width * 0.5); } mPat->setPosition(Vec3(0, 0, -250)); mPat->addChild(geode); mHSTime = -1; update(0); }
OpenRAVE::GraphHandlePtr OSGViewer::drawlinelist(const float *ppoints, int numPoints, int stride, float fwidth, const RaveVectorf &color) { osg::Geometry* geom = new osg::Geometry; int floats_per_pt = stride / sizeof(float); Vec3Array* osgPts = new Vec3Array; osgPts->reserve(numPoints); for (int i=0; i < numPoints; ++i) { const float* p = ppoints + i*floats_per_pt; if (isfinite(p[0])) osgPts->push_back(osg::Vec3(p[0], p[1], p[2])); } osg::StateSet* ss = geom->getOrCreateStateSet(); osg::LineWidth *lw= new osg::LineWidth; lw->setWidth(fwidth); ss->setAttribute(lw); ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF); osg::BlendFunc* blendFunc = new osg::BlendFunc; blendFunc->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); ss->setAttributeAndModes(blendFunc); ss->setMode(GL_BLEND, osg::StateAttribute::ON); osg::Vec4Array* osgColor = new osg::Vec4Array(); osgColor->push_back( osg::Vec4( color[0], color[1], color[2], color[3] ) ); geom->setColorArray(osgColor); geom->setColorBinding(osg::Geometry::BIND_OVERALL); geom->setVertexArray(osgPts); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,osgPts->size())); Geode* geode = new osg::Geode(); geode->addDrawable(geom); return GraphHandlePtr(new OsgGraphHandle(geode, m_root.get())); }
/*************************************************************** * 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); }
bool Geometry_readLocalData(Object& obj, Input& fr) { bool iteratorAdvanced = false; deprecated_osg::Geometry& geom = static_cast<deprecated_osg::Geometry&>(obj); if (fr.matchSequence("Primitives %i {") || fr.matchSequence("PrimitiveSets %i {") ) { int entry = fr[1].getNoNestedBrackets(); int capacity; fr[1].getInt(capacity); Geometry::PrimitiveSetList& primitives = geom.getPrimitiveSetList(); if (capacity>0) primitives.reserve(capacity); fr += 3; while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { if (!Primitive_readLocalData(fr,geom)) fr.advanceOverCurrentFieldOrBlock(); } ++fr; iteratorAdvanced = true; } if (fr[0].matchWord("VertexArray")) { if (fr.matchSequence("VertexArray %i {")) { int entry = fr[0].getNoNestedBrackets(); int capacity; fr[1].getInt(capacity); Vec3Array* vertices = new Vec3Array; vertices->reserve(capacity); fr += 3; while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec3 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z())) { fr += 3; vertices->push_back(v); } else { ++fr; } } geom.setVertexArray(vertices); iteratorAdvanced = true; ++fr; } else { // post 0.9.3 releases. ++fr; Array* vertices = Array_readLocalData(fr); if (vertices) { geom.setVertexArray(vertices); } iteratorAdvanced = true; } } if (fr[0].matchWord("VertexIndices")) { ++fr; IndexArray* indices = dynamic_cast<IndexArray*>(Array_readLocalData(fr)); if (indices) { geom.setVertexIndices(indices); } iteratorAdvanced = true; } deprecated_osg::Geometry::AttributeBinding normalBinding = deprecated_osg::Geometry::BIND_OFF; if (fr[0].matchWord("NormalBinding") && Geometry_matchBindingTypeStr(fr[1].getStr(),normalBinding)) { fr+=2; iteratorAdvanced = true; } if (fr[0].matchWord("NormalArray")) { if (fr.matchSequence("NormalArray %i {")) { // pre 0.9.3 releases.. int entry = fr[0].getNoNestedBrackets(); int capacity; fr[1].getInt(capacity); Vec3Array* normals = new Vec3Array; normals->reserve(capacity); fr += 3; while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec3 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z())) { fr += 3; normals->push_back(v); } else { ++fr; } } geom.setNormalArray(normals); iteratorAdvanced = true; ++fr; } else { // post 0.9.3 releases. ++fr; Array* normals = Array_readLocalData(fr); if (normals) { geom.setNormalArray(normals); } iteratorAdvanced = true; } geom.setNormalBinding(normalBinding); } if (fr[0].matchWord("NormalIndices")) { ++fr; IndexArray* indices = dynamic_cast<IndexArray*>(Array_readLocalData(fr)); if (indices) { geom.setNormalIndices(indices); } iteratorAdvanced = true; } deprecated_osg::Geometry::AttributeBinding colorBinding = deprecated_osg::Geometry::BIND_OFF; if (fr[0].matchWord("ColorBinding") && Geometry_matchBindingTypeStr(fr[1].getStr(),colorBinding)) { fr+=2; iteratorAdvanced = true; } if (fr[0].matchWord("ColorArray")) { ++fr; Array* colors = Array_readLocalData(fr); if (colors) { geom.setColorArray(colors); geom.setColorBinding(colorBinding); } iteratorAdvanced = true; } if (fr[0].matchWord("ColorIndices")) { ++fr; IndexArray* indices = dynamic_cast<IndexArray*>(Array_readLocalData(fr)); if (indices) { geom.setColorIndices(indices); } iteratorAdvanced = true; } deprecated_osg::Geometry::AttributeBinding secondaryColorBinding = deprecated_osg::Geometry::BIND_OFF; if (fr[0].matchWord("SecondaryColorBinding") && Geometry_matchBindingTypeStr(fr[1].getStr(),secondaryColorBinding)) { fr+=2; iteratorAdvanced = true; } if (fr[0].matchWord("SecondaryColorArray")) { ++fr; Array* colors = Array_readLocalData(fr); if (colors) { geom.setSecondaryColorArray(colors); geom.setSecondaryColorBinding(secondaryColorBinding); } iteratorAdvanced = true; } if (fr[0].matchWord("SecondaryColorIndices")) { ++fr; IndexArray* indices = dynamic_cast<IndexArray*>(Array_readLocalData(fr)); if (indices) { geom.setSecondaryColorIndices(indices); } iteratorAdvanced = true; } deprecated_osg::Geometry::AttributeBinding fogCoordBinding = deprecated_osg::Geometry::BIND_OFF; if (fr[0].matchWord("FogCoordBinding") && Geometry_matchBindingTypeStr(fr[1].getStr(),fogCoordBinding)) { fr+=2; iteratorAdvanced = true; } if (fr[0].matchWord("FogCoordArray")) { ++fr; Array* fogcoords = Array_readLocalData(fr); if (fogcoords) { geom.setFogCoordArray(fogcoords); geom.setFogCoordBinding(fogCoordBinding); } iteratorAdvanced = true; } if (fr[0].matchWord("FogCoordIndices")) { ++fr; IndexArray* indices = dynamic_cast<IndexArray*>(Array_readLocalData(fr)); if (indices) { geom.setFogCoordIndices(indices); } iteratorAdvanced = true; } if (fr.matchSequence("TexCoordArray %i")) { int unit=0; fr[1].getInt(unit); fr+=2; Array* texcoords = Array_readLocalData(fr); if (texcoords) { geom.setTexCoordArray(unit,texcoords); } iteratorAdvanced = true; } if (fr.matchSequence("TexCoordIndices %i")) { int unit=0; fr[1].getInt(unit); fr+=2; IndexArray* indices = dynamic_cast<IndexArray*>(Array_readLocalData(fr)); if (indices) { geom.setTexCoordIndices(unit,indices); } iteratorAdvanced = true; } deprecated_osg::Geometry::AttributeBinding vertexAttribBinding = deprecated_osg::Geometry::BIND_OFF; if (fr.matchSequence("VertexAttribBinding %i %w") && Geometry_matchBindingTypeStr(fr[2].getStr(),vertexAttribBinding)) { int unit=0; fr[1].getInt(unit); fr+=3; iteratorAdvanced = true; } bool vertexAttribNormalize = false; if (fr.matchSequence("VertexAttribNormalize %i %w")) { int unit=0; fr[1].getInt(unit); vertexAttribNormalize = fr[2].matchString("TRUE"); fr+=3; iteratorAdvanced = true; } if (fr.matchSequence("VertexAttribArray %i")) { int unit=0; fr[1].getInt(unit); fr+=2; Array* vertexattrib = Array_readLocalData(fr); if (vertexattrib) { geom.setVertexAttribArray(unit,vertexattrib); geom.setVertexAttribBinding(unit,vertexAttribBinding); geom.setVertexAttribNormalize(unit,vertexAttribNormalize); } iteratorAdvanced = true; } if (fr.matchSequence("VertexAttribIndices %i")) { int unit=0; fr[1].getInt(unit); fr+=2; IndexArray* indices = dynamic_cast<IndexArray*>(Array_readLocalData(fr)); if (indices) { geom.setVertexAttribIndices(unit,indices); } iteratorAdvanced = true; } return iteratorAdvanced; }
Array* Array_readLocalData(Input& fr) { if (fr[0].matchWord("Use")) { if (fr[1].isString()) { Object* obj = fr.getObjectForUniqueID(fr[1].getStr()); if (obj) { fr+=2; return dynamic_cast<Array*>(obj); } } osg::notify(osg::WARN)<<"Warning: invalid uniqueID found in file."<<std::endl; return NULL; } std::string uniqueID; if (fr[0].matchWord("UniqueID") && fr[1].isString()) { uniqueID = fr[1].getStr(); fr += 2; } int entry = fr[0].getNoNestedBrackets(); const char* arrayName = fr[0].getStr(); unsigned int capacity = 0; fr[1].getUInt(capacity); ++fr; fr += 2; Array* return_array = 0; if (strcmp(arrayName,"ByteArray")==0) { ByteArray* array = new ByteArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { int int_value; if (fr[0].getInt(int_value)) { ++fr; array->push_back(int_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"ShortArray")==0) { ShortArray* array = new ShortArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { int int_value; if (fr[0].getInt(int_value)) { ++fr; array->push_back(int_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"IntArray")==0) { IntArray* array = new IntArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { int int_value; if (fr[0].getInt(int_value)) { ++fr; array->push_back(int_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UByteArray")==0) { UByteArray* array = new UByteArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int uint_value; if (fr[0].getUInt(uint_value)) { ++fr; array->push_back(uint_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UShortArray")==0) { UShortArray* array = new UShortArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int uint_value; if (fr[0].getUInt(uint_value)) { ++fr; array->push_back(uint_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UIntArray")==0) { UIntArray* array = new UIntArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int uint_value; if (fr[0].getUInt(uint_value)) { ++fr; array->push_back(uint_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UVec4bArray")==0 || strcmp(arrayName,"Vec4ubArray")==0) { Vec4ubArray* array = new Vec4ubArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b,a; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b) && fr[3].getUInt(a)) { fr+=4; array->push_back(osg::Vec4ub(r,g,b,a)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"FloatArray")==0) { FloatArray* array = new FloatArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { float float_value; if (fr[0].getFloat(float_value)) { ++fr; array->push_back(float_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"DoubleArray")==0) { DoubleArray* array = new DoubleArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { double double_value; if (fr[0].getFloat(double_value)) { ++fr; array->push_back(double_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2Array")==0) { Vec2Array* array = new Vec2Array; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec2 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())) { fr += 2; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2dArray")==0) { Vec2dArray* array = new Vec2dArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec2d v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())) { fr += 2; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3Array")==0) { Vec3Array* array = new Vec3Array; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec3 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z())) { fr += 3; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3dArray")==0) { Vec3dArray* array = new Vec3dArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec3d v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z())) { fr += 3; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4Array")==0) { Vec4Array* array = new Vec4Array; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec4 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w())) { fr += 4; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4dArray")==0) { Vec4dArray* array = new Vec4dArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec4d v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w())) { fr += 4; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2bArray")==0) { Vec2bArray* array = new Vec2bArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g; if (fr[0].getUInt(r) && fr[1].getUInt(g)) { fr+=2; array->push_back(osg::Vec2b(r,g)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3bArray")==0) { Vec3bArray* array = new Vec3bArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b)) { fr+=3; array->push_back(osg::Vec3b(r,g,b)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4bArray")==0) { Vec4bArray* array = new Vec4bArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b,a; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b) && fr[3].getUInt(a)) { fr+=4; array->push_back(osg::Vec4b(r,g,b,a)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2sArray")==0) { Vec2sArray* array = new Vec2sArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g; if (fr[0].getUInt(r) && fr[1].getUInt(g)) { fr+=2; array->push_back(osg::Vec2s(r,g)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3sArray")==0) { Vec3sArray* array = new Vec3sArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b)) { fr+=3; array->push_back(osg::Vec3s(r,g,b)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4sArray")==0) { Vec4sArray* array = new Vec4sArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b,a; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b) && fr[3].getUInt(a)) { fr+=4; array->push_back(osg::Vec4s(r,g,b,a)); } else ++fr; } ++fr; return_array = array; } if (return_array) { if (!uniqueID.empty()) fr.registerUniqueIDForObject(uniqueID.c_str(),return_array); } return return_array; }
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; }
osg::Node *JTOpenPlugin::createShape(JtkShape *partShape, const char *objName) { //cout << "JtkSHAPE\n"; Geode *geode = new Geode(); ref_ptr<Geometry> geom = new Geometry(); StateSet *geoState = geode->getOrCreateStateSet(); Vec3Array *vert = new Vec3Array; Vec3Array *normalArray = new Vec3Array(); Vec3Array *colorArray = new Vec3Array(); Vec2Array *tcArray = new Vec2Array(); DrawArrayLengths *primitives = NULL; if (partShape->typeID() == JtkEntity::JtkPOLYGONSET) { primitives = new DrawArrayLengths(PrimitiveSet::POLYGON); } else if (partShape->typeID() == JtkEntity::JtkLINESTRIPSET) { primitives = new DrawArrayLengths(PrimitiveSet::LINE_STRIP); } else if (partShape->typeID() == JtkEntity::JtkTRISTRIPSET) { primitives = new DrawArrayLengths(PrimitiveSet::TRIANGLE_STRIP); } else { cerr << "unknown partShape->typeID " << partShape->typeID() << endl; } geode->setName(objName); if (primitives) { for (int set = 0; set < partShape->numOfSets(); set++) { float *vertex = NULL, *normal = NULL, *color = NULL, *texture = NULL; int vertexCount = -1, normCount = -1, colorCount = -1, textCount = -1; partShape->getInternal(vertex, vertexCount, normal, normCount, color, colorCount, texture, textCount, set); primitives->push_back(vertexCount); // backFaceCulling nur dann, wenn es im CoviseConfig enabled ist /*if(backFaceCulling && (mask & Viewer::MASK_SOLID)) { CullFace *cullFace = new CullFace(); // da viele Modelle backface Culling nicht vertragen (nicht richtig modelliert sind) cullFace->setMode(CullFace::BACK); geoState->setAttributeAndModes(cullFace, StateAttribute::ON); } // already done in updateMaterial() #if 0 if(Blended) { BlendFunc *blendFunc = new BlendFunc(); blendFunc->setFunction(BlendFunc::SRC_ALPHA, BlendFunc::ONE_MINUS_SRC_ALPHA); geoState->setAttributeAndModes(blendFunc, StateAttribute::ON); #if 1 AlphaFunc *alphaFunc = new AlphaFunc(); alphaFunc->setFunction(AlphaFunc::ALWAYS,1.0); geoState->setAttributeAndModes(alphaFunc, StateAttribute::OFF); #endif } #endif #ifdef HAVE_OSGNV if((strncmp(d_currentObject->node->name(),"combineTextures",15)==0)||(strncmp(objName,"combineTextures",15)==0)) { geoState->setAttributeAndModes(combineTextures.get(), StateAttribute::ON); } if((strncmp(d_currentObject->node->name(),"combineEnvTextures",15)==0)||(strncmp(objName,"combineEnvTextures",15)==0)) { geoState->setAttributeAndModes(combineEnvTextures.get(), StateAttribute::ON); } #endif*/ if (vertex && (vertexCount > 0)) { for (int elems = 0; elems < vertexCount; elems++) { vert->push_back(Vec3(vertex[elems * 3 + 0], vertex[elems * 3 + 1], vertex[elems * 3 + 2])); } JtkEntityFactory::deleteMemory(vertex); } if (normal && (normCount > 0)) { for (int elems = 0; elems < normCount; elems++) { normalArray->push_back(Vec3(normal[elems * 3 + 0], normal[elems * 3 + 1], normal[elems * 3 + 2])); } if (normCount == vertexCount) { } else { //geom->setNormalBinding(Geometry::BIND_PER_PRIMITIVE); std::cerr << "JTOpen: normals per primitive not supported" << std::endl; } JtkEntityFactory::deleteMemory(normal); } else // generate normals { } if (color && (colorCount > 0)) { for (int elems = 0; elems < colorCount; elems++) { colorArray->push_back(Vec3(color[elems * 3 + 0], color[elems * 3 + 1], color[elems * 3 + 2])); } if (colorCount == vertexCount) { } else { //geom->setColorBinding(Geometry::BIND_PER_PRIMITIVE); std::cerr << "JTOpen: colors per primitive not supported" << std::endl; } JtkEntityFactory::deleteMemory(color); } if (texture && (textCount > 0)) { for (int elems = 0; elems < textCount; elems++) { tcArray->push_back(Vec2(texture[elems * 2 + 0], texture[elems * 2 + 1])); } JtkEntityFactory::deleteMemory(texture); } /* if(!(mask & Viewer::MASK_CONVEX)) { osgUtil::Tesselator *tess = new osgUtil::Tesselator; tess->retesselatePolygons(*geom); //delete[] tess; }*/ // if enabled, generate tri strips, but not for animated objects // if(genStrips && strncmp(objName, "Animated", 8)) { // d_stripper->stripify(*geom); } } geom->setVertexArray(vert); geom->addPrimitiveSet(primitives); if (normalArray->size() > 0) { geom->setNormalArray(normalArray); geom->setNormalBinding(Geometry::BIND_PER_VERTEX); } if (colorArray->size() > 0) { geom->setColorArray(colorArray); geom->setColorBinding(Geometry::BIND_PER_VERTEX); } if (tcArray->size() > 0) geom->setTexCoordArray(0, tcArray); if (normalArray->size() == 0) { osgUtil::SmoothingVisitor::smooth(*(geom.get()), 40.0 / 180.0 * M_PI); } geode->addDrawable(geom.get()); geode->setStateSet(geoState); return geode; } return NULL; }
void Normals::MakeNormalsVisitor::apply( Geode &geode ) { for( unsigned int i = 0; i < geode.getNumDrawables(); i++ ) { Geometry *geom = dynamic_cast<Geometry *>(geode.getDrawable(i)); if( geom ) { if (geom->containsDeprecatedData()) geom->fixDeprecatedData(); Vec3Array *coords = dynamic_cast<Vec3Array*>(geom->getVertexArray()); if( coords == 0L ) continue; Vec3Array *normals = dynamic_cast<Vec3Array*>(geom->getNormalArray()); if( normals == 0L ) continue; Geometry::AttributeBinding binding = geom->getNormalBinding(); if( binding == Geometry::BIND_OFF ) continue; if( binding == Geometry::BIND_OVERALL ) { Vec3 v(0,0,0); Vec3 n = normals->front(); Vec3Array::iterator coord_index = coords->begin(); while( coord_index != coords->end() ) v += *(coord_index++) * _mat; v /= (float)(coords->size()); n *= _normal_scale; _local_coords->push_back( v ); _local_coords->push_back( (v + n)); } else // BIND_PER_PRIMITIVE_SET, BIND_PER_VERTEX { Geometry::PrimitiveSetList& primitiveSets = geom->getPrimitiveSetList(); Geometry::PrimitiveSetList::iterator itr; Vec3Array::iterator coord_index = coords->begin(); Vec3Array::iterator normals_index = normals->begin(); for(itr=primitiveSets.begin(); itr!=primitiveSets.end(); ++itr) { #ifdef DEBUG _printPrimitiveType( (*itr).get() ); #endif if( binding == Geometry::BIND_PER_PRIMITIVE_SET ) { Vec3 v(0,0,0); Vec3 n = *(normals_index++); int ni = (*itr)->getNumIndices(); for( int i = 0; i < ni; i++ ) v += *(coord_index++) * _mat; v /= (float)(ni); n *= _normal_scale; _local_coords->push_back( v ); _local_coords->push_back( (v + n)); } else { switch((*itr)->getMode()) { case(PrimitiveSet::TRIANGLES): { for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ ) { _processPrimitive( 3, coord_index, normals_index, binding ); coord_index += 3; normals_index+=3; } break; } case(PrimitiveSet::TRIANGLE_STRIP): { for( unsigned int j = 0; j < (*itr)->getNumIndices()-2; j++ ) { _processPrimitive( 3, coord_index, normals_index, binding ); coord_index++; normals_index++; } coord_index += 2; if( binding == Geometry::BIND_PER_VERTEX ) normals_index += 2; break; } case(PrimitiveSet::TRIANGLE_FAN): break; case(PrimitiveSet::QUADS): { for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ ) { _processPrimitive( 4, coord_index, normals_index, binding ); coord_index += 4; normals_index +=4; } break; } case(PrimitiveSet::QUAD_STRIP): break; case(PrimitiveSet::POLYGON): { DrawArrayLengths* dal = dynamic_cast<DrawArrayLengths*>((*itr).get()); if (dal) { for (unsigned int j = 0; j < dal->size(); ++j) { unsigned int num_prim = (*dal)[j]; //OSG_WARN << "j=" << j << " num_prim=" << num_prim << std::endl; _processPrimitive(num_prim, coord_index, normals_index, binding); coord_index += num_prim; normals_index += num_prim; } } break; } default: break; } } } } } } traverse( geode ); }
/*************************************************************** * 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); }
/*************************************************************** * 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: 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); }
HRESULT GLSkyNode::VOnRestore(Scene *pScene) { /*HRESULT hr; V_RETURN(SceneNode::VOnRestore(pScene)); m_camera = pScene->GetCamera(); //TODO: onRestore for shader classes /*V_RETURN(m_pShaderProgram.OnRestore(pScene)); V_RETURN(m_PixelShader.OnRestore(pScene)); m_numVerts = 20; // Fill the vertex buffer. We are setting the tu and tv texture // coordinates, which range from 0.0 to 1.0 SkyVertices data; data.mPositions.resize(m_numVerts); //LOG_ASSERT(data.mPositions.size() != m_numVerts && "Out of memory in GLSkyNode::VOnRestore()"); if (data.mPositions.size() != (size_t)m_numVerts) return E_FAIL; // Loop through the grid squares and calc the values // of each index. Each grid square has two triangles: // // A - B // | / | // C - D Color skyVertColor = g_White; float dim = 50.0f; data.mPositions.resize(4); data.mUVCoords.resize(4); data.mPositions[0] = glm::vec3(dim, dim, dim); data.mUVCoords[0] = glm::vec2(1.0f, 0.0f); data.mPositions[1] = glm::vec3(-dim, dim, dim); data.mUVCoords[1] = glm::vec2(0.0f, 0.0f); data.mPositions[2] = glm::vec3(dim, -dim, dim); data.mUVCoords[2] = glm::vec2(1.0f, 1.0f); data.mPositions[3] = glm::vec3(-dim, -dim, dim); data.mUVCoords[3] = glm::vec2(0.0f, 1.0f); glm::vec3 triangle[3]; triangle[0] = glm::vec3(0.f, 0.f, 0.f); triangle[1] = glm::vec3(5.f, 0.f, 0.f); triangle[2] = glm::vec3(5.f, 5.f, 0.f); glm::vec3 edge1 = triangle[1] - triangle[0]; glm::vec3 edge2 = triangle[2] - triangle[0]; glm::vec3 normal; normal = glm::cross(edge1,edge2); normal = glm::normalize(normal); glm::mat4 rotY; rotY = BuildRotationY(AR_PI / 2.0f); glm::mat4 rotX; rotX = BuildRotationX(-AR_PI / 2.0f); m_sides = 5; //glm::vec3 skyVerts[4] = {}; data.mPositions.resize(m_sides * 4); data.mUVCoords.resize(m_sides * 4); for (DWORD side = 0; side < m_sides; side++) { for (DWORD v = 0; v < 4; v++) { DWORD sv = (side * 4) + v; glm::vec3 temp; if (side < m_sides - 1) { temp = Xform(rotY,data.mPositions[sv]); } else { DWORD ssv = (side * 4); data.mUVCoords[ssv] = glm::vec2(1.0f, 1.0f); data.mUVCoords[ssv + 1] = glm::vec2(1.0f, 0.0f); data.mUVCoords[ssv + 2] = glm::vec2(0.0f, 1.0f); data.mUVCoords[ssv + 3] = glm::vec2(0.0f, 0.0f); temp = Xform(rotX, data.mPositions[sv]); } data.mPositions[sv] = temp; } //memcpy(&pVertices[side * 4], skyVerts, sizeof(skyVerts)); } /* //create the buffers glGenVertexArrays(1, &m_VAO); glBindVertexArray(m_VAO); glGenBuffers(1, &m_VertexBuffer); glGenBuffers(1, &m_IndexBuffer); glGenBuffers(1, &m_TextureVertBuffer); //verts glBindBuffer(GL_VERTEX_ARRAY, m_VertexBuffer); glBufferStorage(GL_VERTEX_ARRAY, m_VertexBufferData.size() * 3 * sizeof(GLfloat), m_VertexBufferData.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(m_VertexBuffer); glVertexAttribPointer(m_VertexBuffer, 3, GL_FLOAT, GL_FALSE, 0, 0); //UV glBindBuffer(GL_VERTEX_ARRAY, m_TextureVertBuffer); glBufferStorage(GL_VERTEX_ARRAY, m_TextureVertBufferData.size() * 2 * sizeof(GLfloat), m_TextureVertBufferData.data(), GL_STATIC_DRAW); glBindBuffer(GL_VERTEX_ARRAY, 0); // Loop through the grid squares and calc the values // of each index. Each grid square has two triangles: // // A - B // | / | // C - D GLuint *pIndices = QSE_NEW GLuint[m_sides * 2 * 3]; GLuint *current = pIndices; for (DWORD i = 0; i<m_sides; i++) { // Triangle #1 ACB *(current) = GLuint(i * 4); *(current + 1) = GLuint(i * 4 + 2); *(current + 2) = GLuint(i * 4 + 1); // Triangle #2 BCD *(current + 3) = GLuint(i * 4 + 1); *(current + 4) = GLuint(i * 4 + 2); *(current + 5) = GLuint(i * 4 + 3); current += 6; } data.mIndices = GLUFArrToVec(pIndices, m_sides * 2 * 3); //now the indicies /*glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer); glBufferStorage(GL_ELEMENT_ARRAY_BUFFER, m_sides * 2 * 3 * sizeof(GLfloat), m_IndexBufferData.data(), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); SAFE_DELETE_ARRAY(pIndices); //buffer the data m_pVertexArray.BufferData(mPositionLoc, data.mPositions.size(), &data.mPositions[0]); m_pVertexArray.BufferData(mUVLoc, data.mUVCoords.size(), &data.mPositions[0]); m_pVertexArray.BufferIndices(pIndices, m_sides * 2 * 3); */ Vec3Array verts; float val = 1.0f; float depth = val; m_sides = 6; //float N = 1.0f / sqrt(3.0f); //north /*verts.push_back(glm::vec3(-val, -val, depth)); verts.push_back(glm::vec3(val, -val, depth)); verts.push_back(glm::vec3(val, val, depth)); verts.push_back(glm::vec3(-val, val, depth)); //uvws.push_back(glm::vec3(-N, -N, N)); //uvws.push_back(glm::vec3(N, -N, N)); //uvws.push_back(glm::vec3(N, N, N)); //uvws.push_back(glm::vec3(-N, N, N)); //south verts.push_back(glm::vec3(val, -val, -depth)); verts.push_back(glm::vec3(-val, -val, -depth)); verts.push_back(glm::vec3(-val, val, -depth)); verts.push_back(glm::vec3(val, val, -depth)); //uvws.push_back(glm::vec3(N, -N, -N)); //uvws.push_back(glm::vec3(-N, -N, -N)); //uvws.push_back(glm::vec3(-N, N, -N)); //uvws.push_back(glm::vec3(N, N, -N)); //east verts.push_back(glm::vec3(-val, -val, -depth)); verts.push_back(glm::vec3(-val, -val, depth)); verts.push_back(glm::vec3(-val, val, depth)); verts.push_back(glm::vec3(-val, val, -depth)); //uvws.push_back(glm::vec3(-N, -N, -N)); //uvws.push_back(glm::vec3(-N, -N, N)); //uvws.push_back(glm::vec3(-N, N, N)); //uvws.push_back(glm::vec3(-N, N, -N)); //west verts.push_back(glm::vec3(val, -val, depth)); verts.push_back(glm::vec3(val, -val, -depth)); verts.push_back(glm::vec3(val, val, -depth)); verts.push_back(glm::vec3(val, val, depth)); //uvws.push_back(glm::vec3(N, -N, N)); //uvws.push_back(glm::vec3(N, -N, -N)); //uvws.push_back(glm::vec3(N, N, -N)); //uvws.push_back(glm::vec3(N, N, N)); //top verts.push_back(glm::vec3(-val, val, depth)); verts.push_back(glm::vec3(val, val, depth)); verts.push_back(glm::vec3(val, val, -depth)); verts.push_back(glm::vec3(-val, val, -depth)); //uvws.push_back(glm::vec3(-N, N, N)); //uvws.push_back(glm::vec3(N, N, N)); //uvws.push_back(glm::vec3(N, N, -N)); //uvws.push_back(glm::vec3(-N, N, -N)); //bottom verts.push_back(glm::vec3(-val, -val, -depth)); verts.push_back(glm::vec3(val, -val, -depth)); verts.push_back(glm::vec3(val, -val, depth)); verts.push_back(glm::vec3(-val, -val, depth));*/ //new plan, just 8 vertices //back verts.push_back(glm::vec3(val, -val, -depth)); verts.push_back(glm::vec3(-val, -val, -depth)); verts.push_back(glm::vec3(-val, val, -depth)); verts.push_back(glm::vec3(val, val, -depth)); //front verts.push_back(glm::vec3(-val, -val, depth)); verts.push_back(glm::vec3(val, -val, depth)); verts.push_back(glm::vec3(val, val, depth)); verts.push_back(glm::vec3(-val, val, depth)); /*// Loop through the grid squares and calc the values // of each index. Each grid square has two triangles: // // D - C // | / | // A - B //two triangles per face for (unsigned int i = 0; i < m_sides; ++i) { unsigned int base = i * 4; //triangle ABC mTriangles.push_back(glm::u32vec3(base, base + 1, base + 2)); //triangle CDA mTriangles.push_back(glm::u32vec3(base + 2, base + 3, base)); }*/ std::vector<glm::u32vec3> mTriangles; //north mTriangles.push_back(glm::u32vec3(4, 5, 6)); mTriangles.push_back(glm::u32vec3(6, 7, 4)); //south mTriangles.push_back(glm::u32vec3(0, 1, 2)); mTriangles.push_back(glm::u32vec3(2, 3, 0)); //east mTriangles.push_back(glm::u32vec3(1, 4, 7)); mTriangles.push_back(glm::u32vec3(7, 2, 1)); //west mTriangles.push_back(glm::u32vec3(5, 0, 3)); mTriangles.push_back(glm::u32vec3(3, 6, 5)); //top mTriangles.push_back(glm::u32vec3(7, 6, 3)); mTriangles.push_back(glm::u32vec3(3, 2, 7)); //bottom mTriangles.push_back(glm::u32vec3(1, 0, 5)); mTriangles.push_back(glm::u32vec3(5, 4, 1)); m_pVertexArray.BufferData(mPositionLoc, verts.size(), &verts[0][0]); m_pVertexArray.BufferIndices(mTriangles); return S_OK; }
void DragonBustNode::genBezierIKMatrices() { /* Copy translation values to bezier chain for bezier-based IK solving. */ for (uint i = 0; i < _nBones; i++) { _bezierIKChain[i]._length = _globalIKChain[i]._length; _bezierIKChain[i]._pose._localPoseTranslation = _globalIKChain[i]._pose._localPoseTranslation; } /* Generate segments along bezier curve. */ /* DEBUG: armature rendering. */ Vec3Array points; _renderer->setWorldTransformation(_W); Vec3Array g(3); g[0] = Vec3(0, 0, 0); g[1] = Vec3(0, 10.0, -1.0); g[2] = Vec3(0, 5, 6); /* Tuning for cursor-following. */ if (_input->isEntered) { g[1].x = -(_input->x-0.5f)*2.5f; g[2].x = 10.0f*(_input->x-0.5f); g[2].y = -(5.0f*(_input->y-1.75)); } else { g[2].y = 6.25f; } _target = g[2]; /* Apply acceleration to points. */ float fac; float d; Vec3 lookAtTargetDir; Vec3 vel; Vec3 lookAtTarget; lookAtTarget = g[1] - _lookAtPosG1; if (lookAtTarget != Vec3(0,0,0)) { d = glm::length(lookAtTarget); fac = (d*2); lookAtTargetDir = glm::normalize(lookAtTarget); vel = 2.0f * fac * lookAtTargetDir; _lookAtPosG1 += vel * *_tDelta; g[1] = _lookAtPosG1; } lookAtTarget = g[2] - _lookAtPosG2; d = glm::length(lookAtTarget); fac = (d*2); lookAtTargetDir = glm::normalize(lookAtTarget); vel = 2.0f * fac * lookAtTargetDir; _lookAtPosG2 += vel * *_tDelta; g[2] = _lookAtPosG2; points.push_back(g[0]); points.push_back(g[1]); points.push_back(g[1]); points.push_back(g[2]); _renderer->renderDebug(points, 5.0f); Vec3Array bezPoints; genBezierSubdivide(g, 3, bezPoints); uint segmentCount = 8; _renderer->renderDebug(bezPoints, 5.0f); /* Compact list of points for intersection testing. */ Vec3Array segmentPoints; for (uint i = 0; i < bezPoints.size(); i+=2) { segmentPoints.push_back(bezPoints[i]); } segmentPoints.push_back(bezPoints[bezPoints.size()-1]); /* Match armature _ik_global_chain[i] to curve. */ Vec3 p1; /* Start delimiter of line segment. */ Vec3 p2; /* End delimiter of line segment. */ Vec3 p3; /* Location of sphere centre. */ Vec3 p; /* Location of intersection. */ Vec3 t; /* Translation vector for realignment of child joints. */ float r; /* Sphere radius. */ int segmentIndex = -1; /* For each bone. */ for (uint i = 0; i < _nBones-1; i++) { /* * Get radius of sphere defined by all possible rotations of the * current bone "vector". */ r = _bezierIKChain[i]._length; /* Get location of sphere's centre. */ p3 = _bezierIKChain[i]._pose._localPoseTranslation; /* For each curve segment. */ for (int j = 0; j < segmentCount; j++) { /* Get start and end delimiters of curve segment. */ p1 = segmentPoints[j]; p2 = segmentPoints[j+1]; if (CollisionDetection::prelimLineSphereTest(p1, p2, p3, r)) { if (j > segmentIndex) { segmentIndex = j; } } } /* * Get intersection point of farthest matched curve segment and * realign armature. */ if (segmentIndex >= 0) { /* Get intersection point. */ p1 = segmentPoints[segmentIndex]; p2 = segmentPoints[segmentIndex+1]; p = CollisionDetection::lineSphereTest(p1, p2, p3, r); points.clear(); points.push_back(p); points.push_back(p); /* * Move joint's children to new point. * The first child joint can be set to the point directly. * The sequential joints can be moved by adding the point to their existing position. */ t = p - _bezierIKChain[i+1]._pose._localPoseTranslation; Vec3 start = _bezierIKChain[i+1]._pose._localPoseTranslation; for (uint j = i+1; j < _nBones; j++) { _bezierIKChain[j]._pose._localPoseTranslation += t; } /* * Find relative rotation between old child joint and new child * joint where origin is the parent (current) joint. */ Vec3 dest = p; _bezierIKChain[i]._pose._localPoseRotation = Math::rotation(start, dest); _bezierIKChain[0]._pose._localPoseRotation = Quat(1, 0, 0, 0); _bezierIKChain[0]._pose._localPoseRotation = Quat(Vec3(glm::radians(20.0f), 0.0f, 0.0f)); } segmentIndex = -1; } _eyeOrigin = _bezierIKChain[_nBones-2]._pose._localPoseTranslation; /* Reset bezier chain translation. */ for (uint i = 0; i < _nBones; i++) { _bezierIKChain[i]._length = _globalIKChain[i]._length; _bezierIKChain[i]._pose._localPoseTranslation = _globalIKChain[i]._pose._localPoseTranslation; } /* Convert global armature to local armature for animation matrix generation. */ _localIKChain[0]._pose._localPoseTranslation = _bezierIKChain[0]._pose._localPoseTranslation - _bezierIKChain[1]._pose._localPoseTranslation; _localIKChain[0]._pose._localPoseRotation = _bezierIKChain[0]._pose._localPoseRotation; uint n = _nBones; for (uint i = 1; i < n; i++) { if (i < n) { _localIKChain[n-i]._pose._localPoseTranslation = _bezierIKChain[n-i]._pose._localPoseTranslation - _bezierIKChain[(n-i)-1]._pose._localPoseTranslation; _localIKChain[i]._pose._localPoseRotation = _bezierIKChain[i]._pose._localPoseRotation; } } for (uint i = 0; i < _nBones; i++) { Quat rot = (_localIKChain[i]._pose._localPoseRotation); Vec3 pos = _localIKChain[i]._pose._localPoseTranslation; Mat4 rotMat = glm::mat4_cast(rot); Mat4 posMat = glm::translate(glm::mat4(1.0f), pos); Mat4 animMat = rotMat*posMat; _IKPoses[i] = animMat; } /* DEBUG: Draw bones. */ _renderer->setWorldTransformation(_W); /* DEBUG: Render to-target line. */ points.clear(); for (uint i = 0; i < _nBones; i++) { points.push_back(_bezierIKChain[i]._pose._localPoseTranslation); if (i < _nBones-1) points.push_back(_bezierIKChain[i+1]._pose._localPoseTranslation); else points.push_back(_bezierIKChain[i]._pose._localPoseTranslation); } }
/*************************************************************** * 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); }
/*************************************************************** * Constructor: CAVEGeodeShape() * * 'mDOCollectorIndex' will not be copied unless the shape is * selected by 'DOGeometryCollecotr' * ***************************************************************/ CAVEGeodeShape::CAVEGeodeShape(CAVEGeodeShape *geodeShapeRef): mDOCollectorIndex(-1) { mVertexArray = new Vec3Array; mNormalArray = new Vec3Array; mUDirArray = new Vec3Array; mVDirArray = new Vec3Array; mTexcoordArray = new Vec2Array; Vec3Array* geodeVertexArray = geodeShapeRef->mVertexArray; Vec3Array* geodeNormalArray = geodeShapeRef->mNormalArray; Vec3Array* geodeUDirArray = geodeShapeRef->mUDirArray; Vec3Array* geodeVDirArray = geodeShapeRef->mVDirArray; Vec2Array* geodeTexcoordArray = geodeShapeRef->mTexcoordArray; Vec3 *geodeVertexDataPtr, *geodeNormalDataPtr, *geodeUDirDataPtr, *geodeVDirDataPtr; Vec2 *geodeTexcoordDataPtr; /* check the valid status of all data field from 'CAVEGeodeShape' */ if (geodeVertexArray->getType() == Array::Vec3ArrayType) geodeVertexDataPtr = (Vec3*) (geodeVertexArray->getDataPointer()); else return; if (geodeNormalArray->getType() == Array::Vec3ArrayType) geodeNormalDataPtr = (Vec3*) (geodeNormalArray->getDataPointer()); else return; if (geodeUDirArray->getType() == Array::Vec3ArrayType) geodeUDirDataPtr = (Vec3*) (geodeUDirArray->getDataPointer()); else return; if (geodeVDirArray->getType() == Array::Vec3ArrayType) geodeVDirDataPtr = (Vec3*) (geodeVDirArray->getDataPointer()); else return; if (geodeTexcoordArray->getType() == Array::Vec2ArrayType) geodeTexcoordDataPtr = (Vec2*) (geodeTexcoordArray->getDataPointer()); else return; mNumVertices = geodeShapeRef->mNumVertices; for (int i = 0; i < mNumVertices; i++) mVertexArray->push_back(geodeVertexDataPtr[i]); mNumNormals = geodeShapeRef->mNumNormals; for (int i = 0; i < mNumNormals; i++) { mNormalArray->push_back(geodeNormalDataPtr[i]); mUDirArray->push_back(geodeUDirDataPtr[i]); mVDirArray->push_back(geodeVDirDataPtr[i]); } mNumTexcoords = geodeShapeRef->mNumTexcoords; for (int i = 0; i < mNumTexcoords; i++) mTexcoordArray->push_back(geodeTexcoordDataPtr[i]); /* make deep copy of 'CAVEGeometry' objects into this 'CAVEGeodeShape' */ CAVEGeometryVector &geomVector = geodeShapeRef->getCAVEGeometryVector(); const int nGeoms = geomVector.size(); if (nGeoms > 0) { for (int i = 0; i < nGeoms; i++) { CAVEGeometry *geometry = new CAVEGeometry(geomVector[i]); geometry->setVertexArray(mVertexArray); geometry->setNormalArray(mNormalArray); geometry->setTexCoordArray(0, mTexcoordArray); geometry->setNormalBinding(Geometry::BIND_PER_VERTEX); mGeometryVector.push_back(geometry); addDrawable(geometry); } } /* copy the same center vector from 'geodeShapeRef', vertex masking vector is with all false values */ mCenterVect = geodeShapeRef->mCenterVect; mVertexMaskingVector.resize(mNumVertices, false); /* apply color texture to virtual surface */ applyColorTexture(geodeShapeRef->mDiffuse, geodeShapeRef->mSpecular, geodeShapeRef->mAlpha, geodeShapeRef->mTexFilename); }
void Skybox::drawImplementation(osg::RenderInfo& renderInfo) const{ int positionCote=mSize/2; Vec3Array* boxVertices = new osg::Vec3Array; boxVertices->push_back(Vec3f(-positionCote,-positionCote,-positionCote)); //back bottom left 0 boxVertices->push_back(Vec3f(positionCote,-positionCote,-positionCote)); //back bottom right 1 boxVertices->push_back(Vec3f(positionCote,positionCote,-positionCote)); //back up right 2 boxVertices->push_back(Vec3f(-positionCote,positionCote,-positionCote)); //back up left 3 boxVertices->push_back(Vec3f(-positionCote,-positionCote,positionCote)); //front bottom left 4 boxVertices->push_back(Vec3f(positionCote,-positionCote,positionCote)); //front bottom right 5 boxVertices->push_back(Vec3f(positionCote,positionCote,positionCote)); //front up right 6 boxVertices->push_back(Vec3f(-positionCote,positionCote,positionCote)); //front up left 7 osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D(image.get())); osg::ref_ptr<osg::StateSet> texSS (new osg::StateSet); texture->setInternalFormat(GL_RGBA); texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR); texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR); texSS->setTextureAttributeAndModes (0, texture.get(), osg::StateAttribute::ON); texSS->setMode(GL_LIGHTING,osg::StateAttribute::OFF); renderInfo.getState()->apply(texSS.get()); boxVertices->at(0)._v; glBegin(GL_QUADS); glTexCoord2f(1.0/4.0, 1.01/3.0);glVertex3fv(boxVertices->at(0)._v); glTexCoord2f(2.0/4.0, 1.01/3.0);glVertex3fv(boxVertices->at(1)._v); glTexCoord2f(2.0/4.0, 1.99/3.0);glVertex3fv(boxVertices->at(2)._v); glTexCoord2f(1.0/4.0, 1.99/3.0);glVertex3fv(boxVertices->at(3)._v); glTexCoord2f(1.0/4.0, 1.01/3.0);glVertex3fv(boxVertices->at(0)._v); glTexCoord2f(0, 1.01/3.0);glVertex3fv(boxVertices->at(4)._v); glTexCoord2f(0, 1.99/3.0);glVertex3fv(boxVertices->at(7)._v); glTexCoord2f(1.0/4.0, 1.99/3.0);glVertex3fv(boxVertices->at(3)._v); glTexCoord2f(1, 1.01/3.0);glVertex3fv(boxVertices->at(4)._v); glTexCoord2f(3.0/4.0, 1.01/3.0);glVertex3fv(boxVertices->at(5)._v); glTexCoord2f(3.0/4.0, 1.99/3.0);glVertex3fv(boxVertices->at(6)._v); glTexCoord2f(1, 1.99/3.0);glVertex3fv(boxVertices->at(7)._v); glTexCoord2f(2.0/4.0, 1.01/3.0);glVertex3fv(boxVertices->at(1)._v); glTexCoord2f(3.0/4.0, 1.01/3.0);glVertex3fv(boxVertices->at(5)._v); glTexCoord2f(3.0/4.0, 1.99/3.0);glVertex3fv(boxVertices->at(6)._v); glTexCoord2f(2.0/4.0, 1.99/3.0);glVertex3fv(boxVertices->at(2)._v); glTexCoord2f(1.01/4.0, 0);glVertex3fv(boxVertices->at(0)._v); glTexCoord2f(1.99/4.0, 0);glVertex3fv(boxVertices->at(1)._v); glTexCoord2f(1.99/4.0, 1.0/3.0);glVertex3fv(boxVertices->at(5)._v); glTexCoord2f(1.01/4.0, 1.0/3.0);glVertex3fv(boxVertices->at(4)._v); glTexCoord2f(1.99/4.0, 2.0/3.0);glVertex3fv(boxVertices->at(2)._v); glTexCoord2f(1.01/4.0, 2.0/3.0);glVertex3fv(boxVertices->at(3)._v); glTexCoord2f(1.01/4.0, 1);glVertex3fv(boxVertices->at(7)._v); glTexCoord2f(1.99/4.0, 1);glVertex3fv(boxVertices->at(6)._v); glEnd(); renderInfo.getState()->apply(); }
/*************************************************************** * Function: acceptCAVEGeodeShape() * * Compared with original coordinates data in 'CAVEGeodeShape', * generated coordinates in 'CAVEGeodeIconSurface' is eaxctly the * the same as those appeared in 'CAVEGeodeShape'. The scaling and * translation to 'gIconCenter' effects are impletemented by its * acendent 'PositionAltitudeTransform' object. * ***************************************************************/ void CAVEGroupIconSurface::acceptCAVEGeodeShape(CAVEGeodeShape *shapeGeode, CAVEGeodeShape *shapeGeodeRef) { mCAVEGeodeShapeOriginPtr = shapeGeode; CAVEGeometryVector &orgGeomVector = shapeGeode->getCAVEGeometryVector(); CAVEGeometryVector &refGeomVector = shapeGeodeRef->getCAVEGeometryVector(); int nGeoms = orgGeomVector.size(); if (nGeoms <= 0) return; /* re-generate vertex coordinate list, keep normal and texcoords the same from 'CAGEGeodeShape' */ mSurfVertexArray = new Vec3Array; mSurfNormalArray = new Vec3Array; mSurfUDirArray = new Vec3Array; mSurfVDirArray = new Vec3Array; mSurfTexcoordArray = new Vec2Array; Vec3Array* geodeVertexArray = shapeGeode->mVertexArray; Vec3Array* geodeNormalArray = shapeGeode->mNormalArray; Vec3Array* geodeUDirArray = shapeGeode->mUDirArray; Vec3Array* geodeVDirArray = shapeGeode->mVDirArray; Vec2Array* geodeTexcoordArray = shapeGeode->mTexcoordArray; Vec3 *geodeVertexDataPtr, *geodeNormalDataPtr, *geodeUDirDataPtr, *geodeVDirDataPtr; Vec2 *geodeTexcoordDataPtr; /* check the valid status of all data field from 'CAVEGeodeShape' */ if (geodeVertexArray->getType() == Array::Vec3ArrayType) geodeVertexDataPtr = (Vec3*) (geodeVertexArray->getDataPointer()); else return; if (geodeNormalArray->getType() == Array::Vec3ArrayType) geodeNormalDataPtr = (Vec3*) (geodeNormalArray->getDataPointer()); else return; if (geodeUDirArray->getType() == Array::Vec3ArrayType) geodeUDirDataPtr = (Vec3*) (geodeUDirArray->getDataPointer()); else return; if (geodeVDirArray->getType() == Array::Vec3ArrayType) geodeVDirDataPtr = (Vec3*) (geodeVDirArray->getDataPointer()); else return; if (geodeTexcoordArray->getType() == Array::Vec2ArrayType) geodeTexcoordDataPtr = (Vec2*) (geodeTexcoordArray->getDataPointer()); else return; /* convert vertex coordinates from CAVEGeodeShape space to CAVEGeodeIcon space */ int nVerts = shapeGeode->mNumVertices; for (int i = 0; i < nVerts; i++) mSurfVertexArray->push_back(geodeVertexDataPtr[i]); /* preserve the same normals and texture coordinates */ int nNormals = shapeGeode->mNumNormals; for (int i = 0; i < nNormals; i++) { mSurfNormalArray->push_back(geodeNormalDataPtr[i]); mSurfUDirArray->push_back(geodeUDirDataPtr[i]); mSurfVDirArray->push_back(geodeVDirDataPtr[i]); } int nTexcoords = shapeGeode->mNumTexcoords; for (int i = 0; i < nTexcoords; i++) mSurfTexcoordArray->push_back(geodeTexcoordDataPtr[i]); /* apply offset from 'gShapeCenter' to Vec3(0, 0, 0) on root level */ Matrixf transMat; transMat.makeTranslate(-gShapeCenter); mRootTrans->setMatrix(transMat); /* copy CAVEGeometry objects into separate 'CAVEGeodeIconSurface' */ for (int i = 0; i < nGeoms; i++) { CAVEGeodeIconSurface *iconSurface = new CAVEGeodeIconSurface(&mSurfVertexArray, &mSurfNormalArray, &mSurfTexcoordArray, &(orgGeomVector[i]), &(refGeomVector[i])); /* 1) take record of 'iconSurface' in mCAVEGeodeIconVector; 2) add it to 'this' group */ mCAVEGeodeIconVector.push_back(iconSurface); mRootTrans->addChild(iconSurface); } }
void DragonBustNode::genBindMatrices() { QuatArray globalBindRotations; globalBindRotations.resize(_nBones); Vec3Array globalBindTranslations; globalBindTranslations.resize(_nBones); uint n = _nBones; /* Load local armature. */ for (uint i = 0; i < _nBones; i++) { Bone& bone = _armatureClip["idle1"]._boneChain[i]; Quat rot = bone._poses[0]._localPoseRotation; Vec3 pos = bone._poses[0]._localPoseTranslation; Mat4 rotMat = glm::mat4_cast(rot); Mat4 posMat = glm::translate(Mat4(1.0f), pos); Mat4 bindMat = rotMat * posMat; _bindPoses[i] = bindMat; } /* Globalise armature, shift armature and convert back to local. */ for (uint i = 0; i < _nBones; i++) { Mat4 globalMat = accumulateBoneSpaceTransformations(_pBoneChain, (*_pBoneChain)[i]._parentIndex) * _bindPoses[i]; /* Extract LocRot from global matrix. */ Quat rot = glm::toQuat(globalMat); Vec3 pos; pos.x = globalMat[3].x; pos.y = globalMat[3].y; pos.z = globalMat[3].z; globalBindTranslations[i] = pos; globalBindRotations[i] = rot; } /* Convert global transformations to local transformations for matrix gen. */ _armatureClip["idle1"]._boneChain[0]._poses[0]._localPoseTranslation = globalBindTranslations[0] - globalBindTranslations[0]; for (uint i = 1; i < n; i++) { if (i < n) { _armatureClip["idle1"]._boneChain[n-i]._poses[0]._localPoseTranslation = globalBindTranslations[n-i] - globalBindTranslations[(n-i)-1]; } } /* Generate local bind matrices */ for (uint i = 0; i < n; i++) { Bone& bone = _armatureClip["idle1"]._boneChain[i]; Quat rot = bone._poses[0]._localPoseRotation; Vec3 pos = bone._poses[0]._localPoseTranslation; Mat4 rotMat = glm::mat4_cast(rot); Mat4 posMat = glm::translate(Mat4(1.0f), pos); Mat4 bindMat = rotMat * posMat; _bindPoses[i] = bindMat; } }