void Object3D::setObjectTextureState(Image *image, bool flipVertically) { // retrieve or create a StateSet StateSet* stateTexture = _originalNode->getOrCreateStateSet(); if(stateTexture && image) { // create a new two-dimensional texture object Texture2D *texCube = new Texture2D; texCube->setDataVariance(Object::DYNAMIC); // set the texture to the loaded image texCube->setImage(image); texCube->setResizeNonPowerOfTwoHint(false); texCube->setWrap(Texture2D::WRAP_S, Texture2D::REPEAT); texCube->setWrap(Texture2D::WRAP_T, Texture2D::REPEAT); // set the texture to the state stateTexture->setTextureAttributeAndModes(0, texCube, StateAttribute::ON|StateAttribute::OVERRIDE); if(flipVertically) { // apply a vertical (Y component) flip on the texture coordinates TexMat *texMat = dynamic_cast<TexMat*>(stateTexture->getAttribute(StateAttribute::TEXMAT,0)); if(!texMat) texMat = new TexMat; Matrix M = Matrix::scale(1, -1, 1); texMat->setMatrix(M); stateTexture->setTextureAttributeAndModes(0, texMat, StateAttribute::ON|StateAttribute::OVERRIDE); } } }
TextureWidget::TextureWidget(Interaction* interaction, float width, float height) : Widget(), Events() { StateSet* stateSet; _interaction = interaction; _width = width; _height = height; createBackground(); createTexturesGeometry(); initLabels(); initTextures(); _texture[0] = new Geode(); _texture[0]->addDrawable(_geom); _texture[0]->addDrawable(_label0); _texture[0]->addDrawable(_texGeom0); stateSet = _texGeom0->getOrCreateStateSet(); stateSet->setMode(GL_LIGHTING, StateAttribute::OFF); stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); stateSet->setTextureAttributeAndModes(StateAttribute::TEXTURE, _tex0, StateAttribute::ON); _texture[1] = new Geode(); _texture[1]->addDrawable(_geom); _texture[1]->addDrawable(_label1); _texture[1]->addDrawable(_texGeom1); stateSet = _texGeom1->getOrCreateStateSet(); stateSet->setMode(GL_LIGHTING, StateAttribute::OFF); stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); stateSet->setTextureAttributeAndModes(StateAttribute::TEXTURE, _tex1, StateAttribute::ON); _swTexture = new Switch(); _swTexture->addChild(_texture[0]); _swTexture->addChild(_texture[1]); _swTexture->setSingleChildOn(0); _node->addChild(_swTexture.get()); _leftGeode = new Geode(); _rightGeode = new Geode(); MatrixTransform* transLeft = new MatrixTransform(); MatrixTransform* transRight = new MatrixTransform(); Matrix trans; trans.makeTranslate(Vec3(-_width/2.0, -_height/2.0 - DEFAULT_LABEL_HEIGHT/2.0, 3*EPSILON_Z)); transLeft->setMatrix(trans); transLeft->addChild(_leftGeode); trans.makeTranslate(Vec3(+_width/2.0, -_height/2.0 - DEFAULT_LABEL_HEIGHT/2.0, 3*EPSILON_Z)); transRight->setMatrix(trans); transRight->addChild(_rightGeode); _node->addChild(transLeft); _node->addChild(transRight); _interaction->addListener(this, this); }
//Constructor CAVEGeodeShape::CAVEGeodeShape(const Type &typ, const Vec3 &initVect, const Vec3 &sVect): mCenterVect(Vec3(0, 0, 0)), mNumVertices(0), mNumNormals(0), mNumTexcoords(0), mDOCollectorIndex(-1) { mVertexArray = new Vec3Array; mNormalArray = new Vec3Array; mUDirArray = new Vec3Array; mVDirArray = new Vec3Array; mTexcoordArray = new Vec2Array; mVertexMaskingVector.clear(); switch (typ) { case BOX: initGeometryBox(initVect, sVect); break; case CYLINDER: initGeometryCylinder(initVect, sVect); break; default: break; } /* texture coordinates is associated with size of the geometry */ Image* img = osgDB::readImageFile(CAVEGeode::getDataDir() + "Textures/White.JPG"); Texture2D* texture = new Texture2D(img); texture->setWrap(Texture::WRAP_S, Texture::MIRROR); texture->setWrap(Texture::WRAP_T, Texture::MIRROR); Material *material = new Material; material->setSpecular(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); material->setAlpha(Material::FRONT_AND_BACK, 1.0f); StateSet *stateset = getOrCreateStateSet(); stateset->setTextureAttributeAndModes(0, texture, StateAttribute::ON); stateset->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON); stateset->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON ); stateset->setRenderingHint(StateSet::TRANSPARENT_BIN); }
/*************************************************************** * Function: applyColorTexture() ***************************************************************/ void CAVEGeode::applyColorTexture(const Vec3 &diffuse, const Vec3 &specular, const float &alpha, const string &texFilename) { StateSet *stateset = getOrCreateStateSet(); /* update material parameters */ Material *material = dynamic_cast <Material*> (stateset->getAttribute(StateAttribute::MATERIAL)); if (!material) material = new Material; material->setDiffuse(Material::FRONT_AND_BACK, Vec4(diffuse, 1.f)); material->setSpecular(Material::FRONT_AND_BACK, Vec4(specular, 1.f)); material->setAlpha(Material::FRONT_AND_BACK, alpha); stateset->setAttributeAndModes(material, StateAttribute::ON); /* update texture image */ Texture2D *texture = dynamic_cast <Texture2D*> (stateset->getAttribute(StateAttribute::TEXTURE)); if (!texture) texture = new Texture2D; Image* texImage = osgDB::readImageFile(texFilename); texture->setImage(texImage); texture->setWrap(Texture::WRAP_S,Texture::REPEAT); texture->setWrap(Texture::WRAP_T,Texture::REPEAT); texture->setDataVariance(Object::DYNAMIC); stateset->setTextureAttributeAndModes(0, texture, StateAttribute::ON); stateset->setMode(GL_BLEND, StateAttribute::OVERRIDE | osg::StateAttribute::ON ); stateset->setRenderingHint(StateSet::TRANSPARENT_BIN); /* update of color & texture properties */ mDiffuse = diffuse; mSpecular = specular; mAlpha = alpha; mTexFilename = texFilename; }
void Panel::setBGTexture(unsigned char* img, int width, int height) { //_texWidth = width; //_texHeight = height; Texture2D* texture; // Initialize panel texture: texture = new Texture2D(); _panelImage = new Image(); _panelImage->setImage(width, height, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, img, Image::USE_NEW_DELETE); texture->setImage(_panelImage); Vec2Array* texCoords = new Vec2Array(4); (*texCoords)[0].set(0.0, 0.0); (*texCoords)[1].set(1.0, 0.0); (*texCoords)[2].set(1.0, 1.0); (*texCoords)[3].set(0.0, 1.0); _geom->setTexCoordArray(0, texCoords); // Texture: StateSet* stateset = _geom->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING, StateAttribute::OFF); stateset->setRenderingHint(StateSet::TRANSPARENT_BIN); stateset->setTextureAttributeAndModes(0, texture, StateAttribute::ON); _panelImage->dirty(); }
PositionAttitudeTransform *VideoGeode::createVideoSphere(float size, bool texRepeat) { Geode *sphere = new Geode(); sphere->addDrawable(new ShapeDrawable(new Sphere(Vec3(0, 0, 4), size))); // assign the material to the sphere StateSet *sphereStateSet = sphere->getOrCreateStateSet(); sphereStateSet->ref(); sphereStateSet->setAttribute(_material); try { sphereStateSet->setTextureAttributeAndModes(0, createVideoTexture(texRepeat), StateAttribute::ON); } catch (char *e) { throw e; } PositionAttitudeTransform *sphereTransform = new PositionAttitudeTransform(); sphereTransform->addChild(sphere); return sphereTransform; }
Node *makeTrees( void ) { Group *group = new Group; int i; getDatabaseCenterRadius( dbcenter, &dbradius ); struct _tree *t; Texture2D *tex = new Texture2D; tex->setImage(osgDB::readImageFile("Images/tree0.rgba")); StateSet *dstate = new StateSet; dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON ); dstate->setTextureAttribute(0, new TexEnv ); dstate->setAttributeAndModes( new BlendFunc, StateAttribute::ON ); AlphaFunc* alphaFunc = new AlphaFunc; alphaFunc->setFunction(AlphaFunc::GEQUAL,0.05f); dstate->setAttributeAndModes( alphaFunc, StateAttribute::ON ); dstate->setMode( GL_LIGHTING, StateAttribute::OFF ); dstate->setRenderingHint( StateSet::TRANSPARENT_BIN ); int tt[] = { 15, 30, 45, 58, 72, 75, 93, 96, 105, -1 }; int *ttp = tt; i = 0; while( i < 105 ) { ttx = trees[i].x; tty = trees[i].y; qsort( &trees[i], 105 - i, sizeof( _tree ), ct ); i += *ttp; ttp++; } t = trees; i = 0; ttp = tt; while( *ttp != -1 ) { Billboard *bb = new Billboard; //int starti = i; for( ; i < (*ttp); i++ ) { t->x -= 0.3f; float h = Hat(t->x, t->y, t->z ); Vec3 pos( t->x, t->y, t->z-h ); Geometry *geom = makeTree( t, dstate ); bb->addDrawable( geom, pos ); t++; } group->addChild( bb ); ttp++; } return group; }
/*************************************************************** * 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); }
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); } }
void PluginTest::createSphereTexture() { osg::Image * image = new osg::Image(); image->allocateImage(256,256,1,GL_RED,GL_FLOAT); image->setInternalTextureFormat(1); float * textureData = (float *)image->data(); int index = 0; for(int i = 0; i < 256; i++) { for(int j = 0; j < 256; j++) { float x, y, z; x = ((float)j) / 255.0; x -= 0.5; x *= 2.0; y = ((float)i) / 255.0; y -= 0.5; y *= 2.0; if(x*x + y*y > 1.0) { textureData[index] = 0; } else { z = sqrt(1.0 - x*x - y*y); textureData[index] = z; } index++; } } osg::Texture2D * texture = new osg::Texture2D(image); osg::Geometry * geometry = new osg::Geometry(); Vec3Array* vertices = new Vec3Array(4); (*vertices)[0].set(-100,0,-100); (*vertices)[1].set(100,0,-100); (*vertices)[2].set(100,0,100); (*vertices)[3].set(-100,0,100); geometry->setVertexArray(vertices); /*Vec4Array* colors = new Vec4Array(1); (*colors)[0].set(1.0, 0.0, 0.0, 1.0); geometry->setColorArray(colors); geometry->setColorBinding(Geometry::BIND_OVERALL);*/ Vec2Array* texcoords = new Vec2Array(4); (*texcoords)[0].set(0.0, 0.0); (*texcoords)[1].set(1.0, 0.0); (*texcoords)[2].set(1.0, 1.0); (*texcoords)[3].set(0.0, 1.0); geometry->setTexCoordArray(0,texcoords); geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS, 0, 4)); StateSet* stateset = geometry->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING, StateAttribute::OFF); stateset->setTextureAttributeAndModes(0, texture, StateAttribute::ON); osg::Geode * geode = new osg::Geode(); geode->addDrawable(geometry); PluginHelper::getScene()->addChild(geode); if(geometry->areFastPathsUsed()) { std::cerr << "Using GL fast path." << std::endl; } else { std::cerr << "Not using GL fast path." << std::endl; } }
Node *makeSky( void ) { int i, j; float lev[] = { -5, -1.0, 1.0, 15.0, 30.0, 60.0, 90.0 }; float cc[][4] = { { 0.0, 0.0, 0.15 }, { 0.0, 0.0, 0.15 }, { 0.4, 0.4, 0.7 }, { 0.2, 0.2, 0.6 }, { 0.1, 0.1, 0.6 }, { 0.1, 0.1, 0.6 }, { 0.1, 0.1, 0.6 }, }; float x, y, z; float alpha, theta; float radius = 20.0f; int nlev = sizeof( lev )/sizeof(float); Geometry *geom = new Geometry; Vec3Array& coords = *(new Vec3Array(19*nlev)); Vec4Array& colors = *(new Vec4Array(19*nlev)); Vec2Array& tcoords = *(new Vec2Array(19*nlev)); int ci = 0; for( i = 0; i < nlev; i++ ) { for( j = 0; j <= 18; j++ ) { alpha = osg::DegreesToRadians(lev[i]); theta = osg::DegreesToRadians((float)(j*20)); x = radius * cosf( alpha ) * cosf( theta ); y = radius * cosf( alpha ) * -sinf( theta ); z = radius * sinf( alpha ); coords[ci][0] = x; coords[ci][1] = y; coords[ci][2] = z; colors[ci][0] = cc[i][0]; colors[ci][1] = cc[i][1]; colors[ci][2] = cc[i][2]; colors[ci][3] = 1.0; tcoords[ci][0] = (float)j/18.0; tcoords[ci][1] = (float)i/(float)(nlev-1); ci++; } } for( i = 0; i < nlev-1; i++ ) { DrawElementsUShort* drawElements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP); drawElements->reserve(38); for( j = 0; j <= 18; j++ ) { drawElements->push_back((i+1)*19+j); drawElements->push_back((i+0)*19+j); } geom->addPrimitiveSet(drawElements); } geom->setVertexArray( &coords ); geom->setTexCoordArray( 0, &tcoords ); geom->setColorArray( &colors ); geom->setColorBinding( Geometry::BIND_PER_VERTEX ); Texture2D *tex = new Texture2D; tex->setImage(osgDB::readImageFile("Images/white.rgb")); StateSet *dstate = new StateSet; dstate->setTextureAttributeAndModes(0, tex, StateAttribute::OFF ); dstate->setTextureAttribute(0, new TexEnv ); dstate->setMode( GL_LIGHTING, StateAttribute::OFF ); dstate->setMode( GL_CULL_FACE, StateAttribute::ON ); // clear the depth to the far plane. osg::Depth* depth = new osg::Depth; depth->setFunction(osg::Depth::ALWAYS); depth->setRange(1.0,1.0); dstate->setAttributeAndModes(depth,StateAttribute::ON ); dstate->setRenderBinDetails(-2,"RenderBin"); geom->setStateSet( dstate ); Geode *geode = new Geode; geode->addDrawable( geom ); geode->setName( "Sky" ); return geode; }
Node *makeTank( void ) { Geode *geode = new Geode; getDatabaseCenterRadius( dbcenter, &dbradius ); Matrix mat( 0.05, 0, 0, 0, 0, 0.05, 0, 0, 0, 0, 0.05, 0, 1.5999 - 0.3, 3.1474, dbcenter[2] + 0.6542 - 0.09, 1 ); // 42 required for sides, 22 for the top. Vec3Array& vc = *(new Vec3Array(42+22)); Vec2Array& tc = *(new Vec2Array(42+22)); Geometry *gset = new Geometry; gset->setVertexArray( &vc ); gset->setTexCoordArray( 0, &tc ); // create the sides of the tank. unsigned int i, c = 0; for( i = 0; i <= 360; i += 18 ) { float x, y, z; float s, t; float theta = osg::DegreesToRadians((float)i); s = (float)i/90.0; t = 1.0; x = radius * cosf( theta ); y = radius * sinf( theta ); z = 1.0; vc[c][0] = x; vc[c][1] = y; vc[c][2] = z; tc[c][0] = s; tc[c][1] = t; c++; t = 0.0; z = 0.0; vc[c][0] = x; vc[c][1] = y; vc[c][2] = z; tc[c][0] = s; tc[c][1] = t; c++; } gset->addPrimitiveSet( new DrawArrays(PrimitiveSet::TRIANGLE_STRIP,0,c) ); // create the top of the tank. int prev_c = c; vc[c][0] = 0.0f; vc[c][1] = 0.0f; vc[c][2] = 1.0f; tc[c][0] = 0.0f; tc[c][1] = 0.0f; c++; for( i = 0; i <= 360; i += 18 ) { float x, y, z; float s, t; float theta = osg::DegreesToRadians((float)i); // s = (float)i/360.0; // t = 1.0; s = cosf( theta ); t = sinf( theta ); x = radius * cosf( theta ); y = radius * sinf( theta ); z = 1.0; vc[c][0] = x; vc[c][1] = y; vc[c][2] = z; tc[c][0] = s; tc[c][1] = t; c++; } for( i = 0; i < c; i++ ) conv( vc[i], mat, vc[i] ); gset->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_FAN,prev_c,c-prev_c)); Texture2D *tex = new Texture2D; tex->setWrap( Texture2D::WRAP_S, Texture2D::REPEAT ); tex->setWrap( Texture2D::WRAP_T, Texture2D::REPEAT ); tex->setImage(osgDB::readImageFile("Images/tank.rgb")); StateSet *dstate = new StateSet; dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON ); dstate->setTextureAttribute(0, new TexEnv ); gset->setStateSet( dstate ); geode->addDrawable( gset ); return geode; }
/*************************************************************** * Function: ANIMLoadGeometryCreator() * * xformScaleFwd: Root transform node for inflating geometries * xformScaleBwd: Root transform node for shrinking geometries * sphereExteriorSwitch: Switch control for single exterior sphere * ***************************************************************/ void ANIMLoadGeometryCreator(PositionAttitudeTransform** xformScaleFwd, PositionAttitudeTransform** xformScaleBwd, Switch **sphereExteriorSwitch, Geode **sphereExteriorGeode, int &numTypes, ANIMShapeSwitchEntry ***shapeSwitchEntryArray) { *xformScaleFwd = new PositionAttitudeTransform; *xformScaleBwd = new PositionAttitudeTransform; MatrixTransform *geomCreatorTrans = new MatrixTransform; MatrixTransform *sphereExteriorTrans = new MatrixTransform; *sphereExteriorSwitch = new Switch; Switch *createBoxSwitch = new Switch; Switch *createCylinderSwitch = new Switch; (*xformScaleFwd)->addChild(geomCreatorTrans); (*xformScaleBwd)->addChild(geomCreatorTrans); geomCreatorTrans->addChild(*sphereExteriorSwitch); geomCreatorTrans->addChild(createBoxSwitch); geomCreatorTrans->addChild(createCylinderSwitch); osg::Vec3 pos(-1, 0, 0); // create drawables, geodes and attach them to animation switches *sphereExteriorGeode = new Geode(); Sphere *sphere = new Sphere(osg::Vec3(), ANIM_VIRTUAL_SPHERE_RADIUS); ShapeDrawable *sphereDrawable = new ShapeDrawable(sphere); (*sphereExteriorGeode)->addDrawable(sphereDrawable); Box *box = new Box(osg::Vec3(0.1, 0, 0), ANIM_VIRTUAL_SPHERE_RADIUS / 1.9); (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(box)); float r = ANIM_VIRTUAL_SPHERE_RADIUS / 3.0; Cylinder *cylinder = new Cylinder(osg::Vec3(-0.05, 0, -0.05), r, r * 2); (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(cylinder)); Cone *cone = new osg::Cone(osg::Vec3(0, -0.1, 0.05), r, r * 2); (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(cone)); Material *transmaterial = new Material; transmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); transmaterial->setAlpha(Material::FRONT_AND_BACK, 0.6f); Image* envMap = osgDB::readImageFile(ANIMDataDir() + "Textures/ShapeContainer.JPG"); Texture2D* envTex = new Texture2D(envMap); StateSet *sphereStateSet = (sphereDrawable)->getOrCreateStateSet(); sphereStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON ); sphereStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); sphereStateSet->setAttributeAndModes(transmaterial, StateAttribute::OVERRIDE | StateAttribute::ON); sphereStateSet->setTextureAttributeAndModes(0, envTex, StateAttribute::ON); sphereStateSet->setMode(GL_CULL_FACE, StateAttribute::ON); sphereExteriorTrans->addChild(*sphereExteriorGeode); (*sphereExteriorSwitch)->addChild(sphereExteriorTrans); (*sphereExteriorSwitch)->setAllChildrenOn(); // write into shape switch entry array record numTypes = 2; *shapeSwitchEntryArray = new ANIMShapeSwitchEntry*[numTypes]; (*shapeSwitchEntryArray)[0] = new ANIMShapeSwitchEntry; (*shapeSwitchEntryArray)[1] = new ANIMShapeSwitchEntry; (*shapeSwitchEntryArray)[0]->mSwitch = createBoxSwitch; (*shapeSwitchEntryArray)[1]->mSwitch = createCylinderSwitch; ANIMCreateSingleShapeSwitchAnimation(&((*shapeSwitchEntryArray)[0]), CAVEGeodeShape::BOX); ANIMCreateSingleShapeSwitchAnimation(&((*shapeSwitchEntryArray)[1]), CAVEGeodeShape::CYLINDER); /* set up the forward / backward scale animation paths for geometry creator */ AnimationPath* animationPathScaleFwd = new AnimationPath; AnimationPath* animationPathScaleBwd = new AnimationPath; animationPathScaleFwd->setLoopMode(AnimationPath::NO_LOOPING); animationPathScaleBwd->setLoopMode(AnimationPath::NO_LOOPING); Vec3 scaleFwd, scaleBwd; float step = 1.f / ANIM_VIRTUAL_SPHERE_NUM_SAMPS; for (int i = 0; i < ANIM_VIRTUAL_SPHERE_NUM_SAMPS + 1; i++) { float val = i * step; scaleFwd = Vec3(val, val, val); scaleBwd = Vec3(1.f-val, 1.f-val, 1.f-val); animationPathScaleFwd->insert(val, AnimationPath::ControlPoint(pos, Quat(), scaleFwd)); animationPathScaleBwd->insert(val, AnimationPath::ControlPoint(pos, Quat(), scaleBwd)); } AnimationPathCallback *animCallbackFwd = new AnimationPathCallback(animationPathScaleFwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); AnimationPathCallback *animCallbackBwd = new AnimationPathCallback(animationPathScaleBwd, 0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); (*xformScaleFwd)->setUpdateCallback(animCallbackFwd); (*xformScaleBwd)->setUpdateCallback(animCallbackBwd); }
Node *makeTerrain( void ) { int m, n; int i, j; float dbcenter[3]; float dbradius; getDatabaseCenterRadius( dbcenter, &dbradius ); m = (sizeof( vertex ) /(sizeof( float[3])))/39; n = 39; Vec3Array& v = *(new Vec3Array(m*n)); Vec2Array& t = *(new Vec2Array(m*n)); Vec4Array& col = *(new Vec4Array(1)); col[0][0] = col[0][1] = col[0][2] = col[0][3] = 1.0f; for( i = 0; i < m * n; i++ ) { float* vc = vertex[i]; v[i][0] = vc[0] - dbcenter[0]; v[i][1] = vc[1] - dbcenter[1]; v[i][2] = vc[2]; float* tc = texcoord[i]; t[i][0] = tc[0]; t[i][1] = tc[1]; } Geometry *geom = new Geometry; geom->setVertexArray( &v ); geom->setTexCoordArray( 0, &t ); geom->setColorArray( &col, Array::BIND_OVERALL ); for( i = 0; i < m-2; i++ ) { DrawElementsUShort* elements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP); elements->reserve(39*2); for( j = 0; j < n; j++ ) { elements->push_back((i+0)*n+j); elements->push_back((i+1)*n+j); } geom->addPrimitiveSet(elements); } Texture2D *tex = new Texture2D; tex->setImage(osgDB::readRefImageFile("Images/lz.rgb")); StateSet *dstate = new StateSet; dstate->setMode( GL_LIGHTING, StateAttribute::OFF ); dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON ); dstate->setTextureAttribute(0, new TexEnv ); geom->setStateSet( dstate ); Geode *geode = new Geode; geode->addDrawable( geom ); return geode; }