def createAxis(): geode = osg.Geode(osg.Geode()) geometry = osg.Geometry(osg.Geometry()) vertices = osg.Vec3Array(osg.Vec3Array()) vertices.push_back (osg.Vec3 ( 0.0, 0.0, 0.0)) vertices.push_back (osg.Vec3 ( 10.0, 0.0, 0.0)) vertices.push_back (osg.Vec3 ( 0.0, 0.0, 0.0)) vertices.push_back (osg.Vec3 ( 0.0, 10.0, 0.0)) vertices.push_back (osg.Vec3 ( 0.0, 0.0, 0.0)) vertices.push_back (osg.Vec3 ( 0.0, 0.0, 10.0)) geometry.setVertexArray (vertices) colors = osg.Vec4Array(osg.Vec4Array()) colors.push_back (osg.Vec4 (1.0, 0.0, 0.0, 1.0)) colors.push_back (osg.Vec4 (1.0, 0.0, 0.0, 1.0)) colors.push_back (osg.Vec4 (0.0, 1.0, 0.0, 1.0)) colors.push_back (osg.Vec4 (0.0, 1.0, 0.0, 1.0)) colors.push_back (osg.Vec4 (0.0, 0.0, 1.0, 1.0)) colors.push_back (osg.Vec4 (0.0, 0.0, 1.0, 1.0)) geometry.setColorArray (colors, osg.Array.BIND_PER_VERTEX) geometry.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.LINES,0,6)) geode.addDrawable( geometry ) geode.getOrCreateStateSet().setMode(GL_LIGHTING, False) return geode
def createMirrorSurface(xMin,xMax,yMin,yMax,z): #// set up the drawstate. #// set up the Geometry. geom = osg.Geometry(); coords = osg.Vec3Array(); coords.append(osg.Vec3f(xMin,yMax,z)) coords.append(osg.Vec3f(xMin,yMin,z)) coords.append(osg.Vec3f(xMax,yMin,z)) coords.append(osg.Vec3f(xMax,yMax,z)) geom.setVertexArray(coords); norms = osg.Vec3Array(); norms.append(osg.Vec3f(0.0,0.0,1.0)) geom.setNormalArray(norms); geom.normalBinding = osg.Geometry.BIND_OVERALL tcoords = osg.Vec2Array() tcoords.append(osg.Vec2f(0.0,1.0)) tcoords.append(osg.Vec2f(0.0,0.0)) tcoords.append(osg.Vec2f(1.0,0.0)) tcoords.append(osg.Vec2f(1.0,1.0)) geom.setTexCoordArray(0,tcoords); colours = osg.Vec4Array(); colours.append(osg.Vec4f(1.0,1.0,1.0,1.0)) geom.setColorArray(colours); geom.colorBinding = osg.Geometry.BIND_OVERALL; geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)); return geom;
def createMirrorSurface(xMin, xMax, yMin, yMax, z): # set up the drawstate. # set up the Geometry. geom = osg.Geometry() coords = osg.Vec3Array(4) (*coords)[0].set(xMin,yMax,z) (*coords)[1].set(xMin,yMin,z) (*coords)[2].set(xMax,yMin,z) (*coords)[3].set(xMax,yMax,z) geom.setVertexArray(coords) norms = osg.Vec3Array(1) (*norms)[0].set(0.0,0.0,1.0) geom.setNormalArray(norms, osg.Array.BIND_OVERALL) tcoords = osg.Vec2Array(4) (*tcoords)[0].set(0.0,1.0) (*tcoords)[1].set(0.0,0.0) (*tcoords)[2].set(1.0,0.0) (*tcoords)[3].set(1.0,1.0) geom.setTexCoordArray(0,tcoords) colours = osg.Vec4Array(1) (*colours)[0].set(1.0,1.0,1.0,1.0) geom.setColorArray(colours, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) return geom
def createTesselatedBox(nsplit, size): riggeometry = osgAnimation.RigGeometry() geometry = osg.Geometry() vertices = osg.Vec3Array(osg.Vec3Array()) colors = osg.Vec3Array(osg.Vec3Array()) geometry.setVertexArray (vertices) geometry.setColorArray (colors, osg.Array.BIND_PER_VERTEX) step = size / static_cast<float>(nsplit) s = 0.5/4.0 for (int i = 0 i < nsplit i++) x = -1.0 + static_cast<float>(i) * step print x vertices.push_back (osg.Vec3 ( x, s, s)) vertices.push_back (osg.Vec3 ( x, -s, s)) vertices.push_back (osg.Vec3 ( x, -s, -s)) vertices.push_back (osg.Vec3 ( x, s, -s)) c = osg.Vec3(0.0,0.0,0.0) c[i%3] = 1.0 colors.push_back (c) colors.push_back (c) colors.push_back (c) colors.push_back (c)
def createSquare(textureCoordMax): # set up the Geometry. geom = osg.Geometry() coords = osg.Vec3Array(4) (*coords)[0].set(-1.0,0.0,1.0) (*coords)[1].set(-1.0,0.0,-1.0) (*coords)[2].set(1.0,0.0,-1.0) (*coords)[3].set(1.0,0.0,1.0) geom.setVertexArray(coords) norms = osg.Vec3Array(1) (*norms)[0].set(0.0,-1.0,0.0) geom.setNormalArray(norms, osg.Array.BIND_OVERALL) tcoords = osg.Vec2Array(4) (*tcoords)[0].set(0.0,textureCoordMax) (*tcoords)[1].set(0.0,0.0) (*tcoords)[2].set(textureCoordMax,0.0) (*tcoords)[3].set(textureCoordMax,textureCoordMax) geom.setTexCoordArray(0,tcoords) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) return geom
def createTexturedQuadGeometry(corner, widthVec, heightVec, l, b, r, t): g = osg.Geometry() # Create vertex array vertices = osg.Vec3Array() vertices.append(corner + widthVec) vertices.append(corner) vertices.append(corner + heightVec) vertices.append(corner + widthVec + heightVec) g.setVertexArray(vertices) # Create texcoord array texcoords = osg.Vec2Array() texcoords.append(osg.Vec2f(l, t)) texcoords.append(osg.Vec2f(l, b)) texcoords.append(osg.Vec2f(r, b)) texcoords.append(osg.Vec2f(r, t)) g.setTexCoordArray(0, texcoords) # Create color array (single value, white) colors = osg.Vec4Array() colors.append(osg.Vec4f(1, 1, 1, 1)) g.setColorArray(colors) g.colorBinding = osg.Geometry.BIND_OVERALL # Create normal array (single value for all vertices) normals = osg.Vec3Array() normals.append(osg.Vec3f(0, -1, 0)) g.setNormalArray(normals) g.normalBinding = osg.Geometry.BIND_OVERALL # Add primitive set g.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS, 0, 4)) return g
class SomePoints (osg.Geometry) : SomePoints() cAry = osg.Vec4Array() setColorArray( cAry, osg.Array.BIND_OVERALL ) cAry.push_back( osg.Vec4(1,1,1,1) ) vAry = osg.Vec3Array() setVertexArray( vAry ) vAry.push_back( osg.Vec3(0,0,0) ) vAry.push_back( osg.Vec3(0,1,0) ) vAry.push_back( osg.Vec3(1,0,0) ) vAry.push_back( osg.Vec3(1,1,0) ) vAry.push_back( osg.Vec3(0,0,1) ) vAry.push_back( osg.Vec3(0,1,1) ) vAry.push_back( osg.Vec3(1,0,1) ) vAry.push_back( osg.Vec3(1,1,1) ) addPrimitiveSet( osg.DrawArrays( GL_POINTS, 0, vAry.size() ) ) sset = getOrCreateStateSet() sset.setMode( GL_LIGHTING, osg.StateAttribute.OFF ) # if things go wrong, fall back to big points p = osg.Point() p.setSize(6) sset.setAttribute( p ) #ifdef ENABLE_GLSL sset.setAttribute( createShader() ) # a generic cyclic animation value u_anim1 = osg.Uniform*( osg.Uniform( "u_anim1", 0.0 ) ) u_anim1.setUpdateCallback( SineAnimation( 4, 0.5, 0.5 ) ) sset.addUniform( u_anim1 )
def createTextureQuad(texture): vertices = osg.Vec3Array() vertices.push_back(osg.Vec3(-0.8, 0.0, -0.8)) vertices.push_back(osg.Vec3(0.8, 0.0, -0.8)) vertices.push_back(osg.Vec3(0.8, 0.0, 0.8)) vertices.push_back(osg.Vec3(-0.8, 0.0, 0.8)) texcoord = osg.Vec2Array() texcoord.push_back(osg.Vec2(0.0, 0.0)) texcoord.push_back(osg.Vec2(1.0, 0.0)) texcoord.push_back(osg.Vec2(1.0, 1.0)) texcoord.push_back(osg.Vec2(0.0, 1.0)) geom = osg.Geometry() geom.setVertexArray(vertices) geom.setTexCoordArray(0, texcoord) geom.addPrimitiveSet(osg.DrawArrays(GL_QUADS, 0, 4)) geode = osg.Geode() geode.addDrawable(geom) geode.getOrCreateStateSet().setTextureAttributeAndModes(0, texture, osg.StateAttribute.ON) return geode
def createRandomTriangles(num): tris = osg.Geode() ss = tris.getOrCreateStateSet() # Force wireframe. Many gfx cards handle this poorly. pm = osg.PolygonMode( osg.PolygonMode.FRONT_AND_BACK, osg.PolygonMode.LINE ) ss.setAttributeAndModes( pm, osg.StateAttribute.ON | osg.StateAttribute.PROTECTED) ss.setMode( GL_LIGHTING, osg.StateAttribute.OFF | osg.StateAttribute.PROTECTED) geom = deprecated_osg.Geometry() # Disable display lists to decrease performance. geom.setUseDisplayList( False ) v = osg.Vec3Array() geom.setVertexArray( v ) v.resize( num*3 ) i = unsigned int() srand( 0 ) #define RAND_NEG1_TO_1 ( ((rand()%20)-10)*.1 ) for (i=0 i<num i++) v0 = (*v)[ i*3+0 ] v1 = (*v)[ i*3+1 ] v2 = (*v)[ i*3+2 ] v0 = osg.Vec3( RAND_NEG1_TO_1, RAND_NEG1_TO_1, RAND_NEG1_TO_1 ) v1 = osg.Vec3( RAND_NEG1_TO_1, RAND_NEG1_TO_1, RAND_NEG1_TO_1 ) v2 = osg.Vec3( RAND_NEG1_TO_1, RAND_NEG1_TO_1, RAND_NEG1_TO_1 )
def createMask(): vertices = osg.Vec3Array() vertices.push_back(osg.Vec3(-0.5, -0.5, 0.0)) vertices.push_back(osg.Vec3(0.5, -0.5, 0.0)) vertices.push_back(osg.Vec3(0.5, 0.5, 0.0)) vertices.push_back(osg.Vec3(-0.5, 0.5, 0.0)) geom = osg.Geometry() geom.setVertexArray(vertices) geom.addPrimitiveSet(osg.DrawArrays(GL_QUADS, 0, 4)) geode = osg.Geode() geode.addDrawable(geom) stencil = osg.Stencil() stencil.setFunction(osg.Stencil.ALWAYS, 1, ~0u) stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.REPLACE) ss = geode.getOrCreateStateSet() ss.setAttributeAndModes(stencil, osg.StateAttribute.ON | osg.StateAttribute.OVERRIDE) ss.setAttribute(osg.ColorMask(False, False, False, False), osg.StateAttribute.ON | osg.StateAttribute.OVERRIDE) return geode
def createGeometry(): vertices = osg.Vec3Array() vertices.push_back(osg.Vec3(-1.0, -1.0, 0.0)) vertices.push_back(osg.Vec3(1.0, -1.0, 0.0)) vertices.push_back(osg.Vec3(1.0, 1.0, 0.0)) vertices.push_back(osg.Vec3(-1.0, 1.0, 0.0)) geom = osg.Geometry() geom.setVertexArray(vertices) geom.addPrimitiveSet(osg.DrawArrays(GL_QUADS, 0, 4)) geode = osg.Geode() geode.addDrawable(geom) stencil = osg.Stencil() stencil.setFunction(osg.Stencil.NOTEQUAL, 1, ~0u) stencil.setOperation(osg.Stencil.KEEP, osg.Stencil.KEEP, osg.Stencil.KEEP) ss = geode.getOrCreateStateSet() ss.setAttributeAndModes(stencil, osg.StateAttribute.ON | osg.StateAttribute.OVERRIDE) return geode
def createWall(v1, v2, v3, stateset): # create a drawable for occluder. geom = osg.Geometry() geom.setStateSet(stateset) noXSteps = 100 noYSteps = 100 coords = osg.Vec3Array() coords.reserve(noXSteps*noYSteps) dx = (v2-v1)/((float)noXSteps-1.0) dy = (v3-v1)/((float)noYSteps-1.0) row = unsigned int() vRowStart = v1 for(row=0row<noYSteps++row) v = vRowStart for(unsigned int col=0col<noXSteps++col) coords.push_back(v) v += dx
def allocate(): numVertices = _width * _height vertices = osg.Vec3Array(numVertices) for(int j=0 j<_height ++j) for(int i=0 i<_width ++i) (*vertices)[i+j*_width].set(float(i),float(j),0.0)
def createSquare(corner, width, height, image): # set up the Geometry. geom = osg.Geometry() coords = osg.Vec3Array(4) (*coords)[0] = corner (*coords)[1] = corner+width (*coords)[2] = corner+width+height (*coords)[3] = corner+height geom.setVertexArray(coords) norms = osg.Vec3Array(1) (*norms)[0] = width^height (*norms)[0].normalize() geom.setNormalArray(norms, osg.Array.BIND_OVERALL) tcoords = osg.Vec2Array(4) (*tcoords)[0].set(0.0,0.0) (*tcoords)[1].set(1.0,0.0) (*tcoords)[2].set(1.0,1.0) (*tcoords)[3].set(0.0,1.0) geom.setTexCoordArray(0,tcoords) colours = osg.Vec4Array(1) (*colours)[0].set(1.0,1.0,1.0,1.0) geom.setColorArray(colours, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) if image : stateset = osg.StateSet() texture = osg.Texture2D() texture.setImage(image) stateset.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON) stateset.setMode(GL_LIGHTING, osg.StateAttribute.OFF) stateset.setMode(GL_BLEND, osg.StateAttribute.ON) stateset.setRenderingHint(osg.StateSet.TRANSPARENT_BIN) geom.setStateSet(stateset) return geom
def makeFrustumFromCamera(camera): # Projection and ModelView matrices proj = osg.Matrixd() mv = osg.Matrixd() if camera : proj = camera.getProjectionMatrix() mv = camera.getViewMatrix() else: # Create some kind of reasonable default Projection matrix. proj.makePerspective( 30., 1., 1., 10. ) # leave mv as identity # Get near and far from the Projection matrix. near = proj(3,2) / (proj(2,2)-1.0) far = proj(3,2) / (1.0+proj(2,2)) # Get the sides of the near plane. nLeft = near * (proj(2,0)-1.0) / proj(0,0) nRight = near * (1.0+proj(2,0)) / proj(0,0) nTop = near * (1.0+proj(2,1)) / proj(1,1) nBottom = near * (proj(2,1)-1.0) / proj(1,1) # Get the sides of the far plane. fLeft = far * (proj(2,0)-1.0) / proj(0,0) fRight = far * (1.0+proj(2,0)) / proj(0,0) fTop = far * (1.0+proj(2,1)) / proj(1,1) fBottom = far * (proj(2,1)-1.0) / proj(1,1) # Our vertex array needs only 9 vertices: The origin, and the # eight corners of the near and far planes. v = osg.Vec3Array() v.resize( 9 ) (*v)[0].set( 0., 0., 0. ) (*v)[1].set( nLeft, nBottom, -near ) (*v)[2].set( nRight, nBottom, -near ) (*v)[3].set( nRight, nTop, -near ) (*v)[4].set( nLeft, nTop, -near ) (*v)[5].set( fLeft, fBottom, -far ) (*v)[6].set( fRight, fBottom, -far ) (*v)[7].set( fRight, fTop, -far ) (*v)[8].set( fLeft, fTop, -far ) geom = osg.Geometry() geom.setUseDisplayList( False ) geom.setVertexArray( v ) c = osg.Vec4Array() c.push_back( osg.Vec4( 1., 1., 1., 1. ) ) geom.setColorArray( c, osg.Array.BIND_OVERALL ) GLushort idxLines[8] = 0, 5, 0, 6, 0, 7, 0, 8
def createBase(center, radius): numTilesX = 10 numTilesY = 10 width = 2*radius height = 2*radius v000 = center - osg.Vec3(width*0.5,height*0.5,0.0) dx = osg.Vec3(width/(float(numTilesX)),0.0,0.0) dy = osg.Vec3(0.0,height/(float(numTilesY)),0.0) # fill in vertices for grid, note numTilesX+1 * numTilesY+1... coords = osg.Vec3Array() for iy in range(numTilesY): for ix in range(numTilesX): coords.append(v000+dx*float(ix)+dy*float(iy)) #Just two colours - black and white. colors = osg.Vec4Array() colors.append(osg.Vec4(1.0,1.0,1.0,1.0)) # white colors.append(osg.Vec4(0.0,0.0,0.0,1.0)) # black whitePrimitives = osg.DrawElementsUShort(osg.GL_QUADS) blackPrimitives = osg.DrawElementsUShort(osg.GL_QUADS) numIndicesPerRow = numTilesX+1 for iy in range(numTilesY): for ix in range(numTilesX): primitives = whitePrimitives if (((iy+ix)%2==0)) else blackPrimitives primitives.append(ix +(iy+1)*numIndicesPerRow) primitives.append(ix +iy*numIndicesPerRow) primitives.append((ix+1)+iy*numIndicesPerRow) primitives.append((ix+1)+(iy+1)*numIndicesPerRow) # set up a single normal normals = osg.Vec3Array() normals.append(osg.Vec3(0.0,0.0,1.0)) geom = osg.Geometry() geom.setVertexArray(coords) geom.setColorArray(colors, osg.Array.BIND_PER_PRIMITIVE_SET) geom.setNormalArray(normals, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(whitePrimitives) geom.addPrimitiveSet(blackPrimitives) geode = osg.Geode() geode.addDrawable(geom) return geode
def createSectorForImage(image, texmat, s, t, radius, height, length): flip = image.getOrigin()==osg.Image.TOP_LEFT numSegments = 20 Theta = length/radius dTheta = Theta/(float)(numSegments-1) ThetaZero = height*s/(t*radius) # set up the texture. texture = osg.Texture2D() texture.setFilter(osg.Texture2D.MIN_FILTER,osg.Texture2D.LINEAR) texture.setFilter(osg.Texture2D.MAG_FILTER,osg.Texture2D.LINEAR) texture.setWrap(osg.Texture2D.WRAP_S,osg.Texture2D.CLAMP_TO_BORDER) texture.setWrap(osg.Texture2D.WRAP_T,osg.Texture2D.CLAMP_TO_BORDER) texture.setResizeNonPowerOfTwoHint(False) texture.setImage(image) # set up the drawstate. dstate = osg.StateSet() dstate.setMode(GL_CULL_FACE,osg.StateAttribute.OFF) dstate.setMode(GL_LIGHTING,osg.StateAttribute.OFF) dstate.setTextureAttributeAndModes(0, texture,osg.StateAttribute.ON) dstate.setTextureAttribute(0, texmat) # set up the geoset. geom = osg.Geometry() geom.setStateSet(dstate) coords = osg.Vec3Array() tcoords = osg.Vec2Array() i = int() angle = -Theta/2.0 for(i=0 i<numSegments ++i, angle+=dTheta) coords.push_back(osg.Vec3(sinf(angle)*radius,cosf(angle)*radius,height*0.5)) # top coords.push_back(osg.Vec3(sinf(angle)*radius,cosf(angle)*radius,-height*0.5)) # bottom. tcoords.push_back(osg.Vec2(angle/ThetaZero+0.5, 0.0 if (flip) else 1.0)) # top tcoords.push_back(osg.Vec2(angle/ThetaZero+0.5, 1.0 if (flip) else 0.0)) # bottom.
def createDAIGeometry(geom, nInstances): halfDimX = float( .5 ) halfDimZ = float( .5 ) v = osg.Vec3Array() v.resize( 4 ) geom.setVertexArray( v ) # Geometry for a single quad. (*v)[ 0 ] = osg.Vec3( -halfDimX, 0., -halfDimZ ) (*v)[ 1 ] = osg.Vec3( halfDimX, 0., -halfDimZ ) (*v)[ 2 ] = osg.Vec3( halfDimX, 0., halfDimZ ) (*v)[ 3 ] = osg.Vec3( -halfDimX, 0., halfDimZ ) # Use the DrawArraysInstanced PrimitiveSet and tell it to draw 1024 instances. geom.addPrimitiveSet( osg.DrawArrays( GL_QUADS, 0, 4, nInstances ) )
def createBox(): box = osg.Geode() state = box.getOrCreateStateSet() pm = osg.PolygonMode( osg.PolygonMode.FRONT_AND_BACK, osg.PolygonMode.FILL ) state.setAttributeAndModes( pm, osg.StateAttribute.ON | osg.StateAttribute.PROTECTED ) geom = deprecated_osg.Geometry() v = osg.Vec3Array() geom.setVertexArray( v ) x = float( 0. ) y = float( 0. ) z = float( 0. ) r = float( 1.1 ) v.push_back( osg.Vec3( x-r, y-r, z-r ) ) #left -X v.push_back( osg.Vec3( x-r, y-r, z+r ) ) v.push_back( osg.Vec3( x-r, y+r, z+r ) ) v.push_back( osg.Vec3( x-r, y+r, z-r ) ) v.push_back( osg.Vec3( x+r, y-r, z+r ) ) #right +X v.push_back( osg.Vec3( x+r, y-r, z-r ) ) v.push_back( osg.Vec3( x+r, y+r, z-r ) ) v.push_back( osg.Vec3( x+r, y+r, z+r ) ) v.push_back( osg.Vec3( x-r, y-r, z-r ) ) # bottom -Z v.push_back( osg.Vec3( x-r, y+r, z-r ) ) v.push_back( osg.Vec3( x+r, y+r, z-r ) ) v.push_back( osg.Vec3( x+r, y-r, z-r ) ) v.push_back( osg.Vec3( x-r, y-r, z+r ) ) # top +Z v.push_back( osg.Vec3( x+r, y-r, z+r ) ) v.push_back( osg.Vec3( x+r, y+r, z+r ) ) v.push_back( osg.Vec3( x-r, y+r, z+r ) ) v.push_back( osg.Vec3( x-r, y+r, z-r ) ) # back +Y v.push_back( osg.Vec3( x-r, y+r, z+r ) ) v.push_back( osg.Vec3( x+r, y+r, z+r ) ) v.push_back( osg.Vec3( x+r, y+r, z-r ) )
def createEaseMotionGeometry(motion): geom = osg.Geometry() cols = osg.Vec4Array() v = osg.Vec3Array() for(float i = 0.0 i < M_DURATION i += M_DURATION / 256.0) v.push_back( osg.Vec3(i * 30.0, motion.getValueAt(i) * 30.0, 0.0) ) cols.push_back(osg.Vec4(1.0, 1.0, 1.0, 1.0)) geom.setUseDisplayList(False) geom.setVertexArray(v) geom.setColorArray(cols, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.LINE_STRIP, 0, v.size())) return geom
def createScene(noStars): geometry = osg.Geometry() # set up vertices vertices = osg.Vec3Array(noStars*2) geometry.setVertexArray(vertices) min = -1.0 max = 1.0 j = 0 i = 0 for(i=0i<noStars++i,j+=2) (*vertices)[j].set(random(min,max),random(min,max),random(min,max)) (*vertices)[j+1] = (*vertices)[j]+osg.Vec3(0.0,0.0,0.001)
def createScene(): # Create the Earth, in blue earth_sd = osg.ShapeDrawable() earth_sphere = osg.Sphere() earth_sphere.setName("EarthSphere") earth_sphere.setRadius(r_earth) earth_sd.setShape(earth_sphere) earth_sd.setColor(osg.Vec4(0, 0, 1.0, 1.0)) earth_geode = osg.Geode() earth_geode.setName("EarthGeode") earth_geode.addDrawable(earth_sd) # Create the Sun, in yellow sun_sd = osg.ShapeDrawable() sun_sphere = osg.Sphere() sun_sphere.setName("SunSphere") sun_sphere.setRadius(r_sun) sun_sd.setShape(sun_sphere) sun_sd.setColor(osg.Vec4(1.0, 0.0, 0.0, 1.0)) sun_geode = osg.Geode() sun_geode.setName("SunGeode") sun_geode.addDrawable(sun_sd) # Move the sun behind the earth pat = osg.PositionAttitudeTransform() pat.setPosition(osg.Vec3d(0.0, AU, 0.0)) pat.addChild(sun_geode) unitCircle = osg.Geometry() colours = osg.Vec4Array(1) (*colours)[0] = osg.Vec4d(1.0,1.0,1.0,1.0) unitCircle.setColorArray(colours, osg.Array.BIND_OVERALL) n_points = 1024 coords = osg.Vec3Array(n_points) dx = 2.0*osg.PI/n_points double s,c for (unsigned int j=0 j<n_points ++j)
def createWing(left, nose, right, chordRatio, color): geom = osg.Geometry() normal = (nose-right)^(left-nose) normal.normalize() left_to_right = right-left mid = (right+left)*0.5 mid_to_nose = (nose-mid)*chordRatio*0.5 vertices = osg.Vec3Array() vertices.push_back(left) #vertices.push_back(mid+mid_to_nose) noSteps = 40 for(unsigned int i=1i<noSteps++i) ratio = (float)i/(float)noSteps vertices.push_back(left + left_to_right*ratio + mid_to_nose* (cosf((ratio-0.5)*osg.PI*2.0)+1.0))
def createAxis(corner, xdir, ydir, zdir): # set up the Geometry. geom = osg.Geometry() coords = osg.Vec3Array(6) (*coords)[0] = corner (*coords)[1] = corner+xdir (*coords)[2] = corner (*coords)[3] = corner+ydir (*coords)[4] = corner (*coords)[5] = corner+zdir geom.setVertexArray(coords) x_color = osg.Vec4(0.0,1.0,1.0,1.0) y_color = osg.Vec4(0.0,1.0,1.0,1.0) z_color = osg.Vec4(1.0,0.0,0.0,1.0) color = osg.Vec4Array(6) (*color)[0] = x_color (*color)[1] = x_color (*color)[2] = y_color (*color)[3] = y_color (*color)[4] = z_color (*color)[5] = z_color geom.setColorArray(color, osg.Array.BIND_PER_VERTEX) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.LINES,0,6)) stateset = osg.StateSet() linewidth = osg.LineWidth() linewidth.setWidth(4.0) stateset.setAttributeAndModes(linewidth,osg.StateAttribute.ON) stateset.setMode(GL_LIGHTING,osg.StateAttribute.OFF) geom.setStateSet(stateset) return geom
def createBase(center, radius): numTilesX = 10 numTilesY = 10 width = 2*radius height = 2*radius v000 = osg.Vec3(center - osg.Vec3(width*0.5,height*0.5,0.0)) dx = osg.Vec3(osg.Vec3(width/((float)numTilesX),0.0,0.0)) dy = osg.Vec3(osg.Vec3(0.0,height/((float)numTilesY),0.0)) # fill in vertices for grid, note numTilesX+1 * numTilesY+1... coords = osg.Vec3Array() iy = int() for(iy=0iy<=numTilesY++iy) for(int ix=0ix<=numTilesX++ix) coords.push_back(v000+dx*(float)ix+dy*(float)iy)
def createAxis(s, e, numReps, autoRotateMode, axisAlignment, str): group = osg.Group() dv = e-s dv /= float(numReps-1) pos = s useAuto = True if useAuto : vertices = osg.Vec3Array() for(int i=0i<numReps++i) at = osg.AutoTransform() at.setPosition(pos) at.setAutoRotateMode(autoRotateMode) at.addChild(createLabel(osg.Vec3(0.0,0.0,0.0),dv.length()*0.2,str, axisAlignment)) vertices.push_back(pos) pos += dv group.addChild(at) colors = osg.Vec4Array() colors.push_back(osg.Vec4(1.0,1.0,1.0,1.0)) geom = osg.Geometry() geom.setVertexArray(vertices) geom.setColorArray(colors, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(GL_LINE_STRIP,0,vertices.size())) geode = osg.Geode() geode.addDrawable(geom) group.addChild(geode)
def createScene(): # create the Geode (Geometry Node) to contain all our osg.Geometry objects. geode = osg.Geode() # following are separate blocks for creating POINTS, LINES, LINE_STRIP, LINE_LOOP, POLYGON, QUADS, # QUAD_STRIP, TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN primitives. An image of these primitives # is provided in the distribution: OpenSceneGraph-Data/Images/primitives.gif. # create POINTS # create Geometry object to store all the vertices and points primitive. pointsGeom = osg.Geometry() # create a Vec3Array and add to it all my coordinates. # Like all the *Array variants (see include/osg/Array) , Vec3Array is derived from both osg.Array # and std.vector<>. osg.Array's are reference counted and hence sharable, # which std.vector<> provides all the convenience, flexibility and robustness # of the most popular of all STL containers. vertices = osg.Vec3Array() vertices.push_back(osg.Vec3(-1.02168, -2.15188e-09, 0.885735)) vertices.push_back(osg.Vec3(-0.976368, -2.15188e-09, 0.832179)) vertices.push_back(osg.Vec3(-0.873376, 9.18133e-09, 0.832179)) vertices.push_back(osg.Vec3(-0.836299, -2.15188e-09, 0.885735)) vertices.push_back(osg.Vec3(-0.790982, 9.18133e-09, 0.959889)) # pass the created vertex array to the points geometry object. pointsGeom.setVertexArray(vertices) # create the color of the geometry, one single for the whole geometry. # for consistency of design even one single color must added as an element # in a color array. colors = osg.Vec4Array() # add a white color, colors take the form r,g,b,a with 0.0 off, 1.0 full on. colors.push_back(osg.Vec4(1.0,1.0,0.0,1.0)) # pass the color array to points geometry, note the binding to tell the geometry # that only use one color for the whole object. pointsGeom.setColorArray(colors, osg.Array.BIND_OVERALL) # set the normal in the same way color. normals = osg.Vec3Array() normals.push_back(osg.Vec3(0.0,-1.0,0.0)) pointsGeom.setNormalArray(normals, osg.Array.BIND_OVERALL) # create and add a DrawArray Primitive (see include/osg/Primitive). The first # parameter passed to the DrawArrays constructor is the Primitive.Mode which # in this case is POINTS (which has the same value GL_POINTS), the second # parameter is the index position into the vertex array of the first point # to draw, and the third parameter is the number of points to draw. pointsGeom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POINTS,0,vertices.size())) # add the points geometry to the geode. geode.addDrawable(pointsGeom) # create LINES # create Geometry object to store all the vertices and lines primitive. linesGeom = osg.Geometry() # this time we'll preallocate the vertex array to the size we # need and then simple set them as array elements, 8 points # makes 4 line segments. vertices = osg.Vec3Array(8) (*vertices)[0].set(-1.13704, -2.15188e-09, 0.40373) (*vertices)[1].set(-0.856897, -2.15188e-09, 0.531441) (*vertices)[2].set(-0.889855, -2.15188e-09, 0.444927) (*vertices)[3].set(-0.568518, -2.15188e-09, 0.40373) (*vertices)[4].set(-1.00933, -2.15188e-09, 0.370773) (*vertices)[5].set(-0.716827, -2.15188e-09, 0.292498) (*vertices)[6].set(-1.07936, 9.18133e-09, 0.317217) (*vertices)[7].set(-0.700348, 9.18133e-09, 0.362533) # pass the created vertex array to the points geometry object. linesGeom.setVertexArray(vertices) # set the colors as before, plus using the above colors = osg.Vec4Array() colors.push_back(osg.Vec4(1.0,1.0,0.0,1.0)) linesGeom.setColorArray(colors, osg.Array.BIND_OVERALL) # set the normal in the same way color. normals = osg.Vec3Array() normals.push_back(osg.Vec3(0.0,-1.0,0.0)) linesGeom.setNormalArray(normals, osg.Array.BIND_OVERALL) # This time we simply use primitive, and hardwire the number of coords to use # since we know up front, linesGeom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.LINES,0,8)) # add the points geometry to the geode. geode.addDrawable(linesGeom) # create LINE_STRIP # create Geometry object to store all the vertices and lines primitive. linesGeom = osg.Geometry() # this time we'll preallocate the vertex array to the size # and then use an iterator to fill in the values, a bit perverse # but does demonstrate that we have just a standard std.vector underneath. vertices = osg.Vec3Array(5) vitr = vertices.begin() (vitr++).set(-0.0741545, -2.15188e-09, 0.416089) (vitr++).set(0.234823, -2.15188e-09, 0.259541) (vitr++).set(0.164788, -2.15188e-09, 0.366653) (vitr++).set(-0.0288379, -2.15188e-09, 0.333695) (vitr++).set(-0.0453167, -2.15188e-09, 0.280139) # pass the created vertex array to the points geometry object. linesGeom.setVertexArray(vertices) # set the colors as before, plus using the above colors = osg.Vec4Array() colors.push_back(osg.Vec4(1.0,1.0,0.0,1.0)) linesGeom.setColorArray(colors, osg.Array.BIND_OVERALL) # set the normal in the same way color. normals = osg.Vec3Array() normals.push_back(osg.Vec3(0.0,-1.0,0.0)) linesGeom.setNormalArray(normals, osg.Array.BIND_OVERALL) # This time we simply use primitive, and hardwire the number of coords to use # since we know up front, linesGeom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.LINE_STRIP,0,5)) # add the points geometry to the geode. geode.addDrawable(linesGeom) # create LINE_LOOP # create Geometry object to store all the vertices and lines primitive. linesGeom = osg.Geometry() # this time we'll a C arrays to initialize the vertices. osg.Vec3 myCoords[] = osg.Vec3(0.741546, -2.15188e-09, 0.453167), osg.Vec3(0.840418, -2.15188e-09, 0.304858), osg.Vec3(1.12468, -2.15188e-09, 0.300738), osg.Vec3(1.03816, 9.18133e-09, 0.453167), osg.Vec3(0.968129, -2.15188e-09, 0.337815), osg.Vec3(0.869256, -2.15188e-09, 0.531441)
2, 3, 7, 2, 7, 6, 4, 8, 7, 5, 6, 9, 4, 5, 8, 8, 5, 9, 6, 7, 8, 8, 9, 6 # use the same color, normal and indices for all houses. colors = osg.Vec4Array(1) (*colors)[0] = osg.Vec4(1.0, 1.0, 1.0, 1.0) # normals normals = osg.Vec3Array(16) (*normals)[0] = osg.Vec3( 0.0, -0.0, -1.0) (*normals)[1] = osg.Vec3( 0.0, -0.0, -1.0) (*normals)[2] = osg.Vec3( 0.0, -1.0, 0.0) (*normals)[3] = osg.Vec3( 0.0, -1.0, 0.0) (*normals)[4] = osg.Vec3( 1.0, -0.0, 0.0) (*normals)[5] = osg.Vec3( 1.0, -0.0, 0.0) (*normals)[6] = osg.Vec3( 0.0, 1.0, 0.0) (*normals)[7] = osg.Vec3( 0.0, 1.0, 0.0) (*normals)[8] = osg.Vec3(-1.0, -0.0, 0.0) (*normals)[9] = osg.Vec3(-1.0, -0.0, 0.0) (*normals)[10] = osg.Vec3( 0.0, -0.928477, 0.371391) (*normals)[11] = osg.Vec3( 0.0, 0.928477, 0.371391) (*normals)[12] = osg.Vec3( 0.707107, 0.0, 0.707107) (*normals)[13] = osg.Vec3( 0.707107, 0.0, 0.707107) (*normals)[14] = osg.Vec3(-0.707107, 0.0, 0.707107)
linesGeom = osg.Geometry() # this time we'll a C arrays to initialize the vertices. osg.Vec3 myCoords[] = osg.Vec3(0.741546, -2.15188e-09, 0.453167), osg.Vec3(0.840418, -2.15188e-09, 0.304858), osg.Vec3(1.12468, -2.15188e-09, 0.300738), osg.Vec3(1.03816, 9.18133e-09, 0.453167), osg.Vec3(0.968129, -2.15188e-09, 0.337815), osg.Vec3(0.869256, -2.15188e-09, 0.531441) numCoords = sizeof(myCoords)/sizeof(osg.Vec3) vertices = osg.Vec3Array(numCoords,myCoords) # pass the created vertex array to the points geometry object. linesGeom.setVertexArray(vertices) # set the colors as before, plus using the above colors = osg.Vec4Array() colors.push_back(osg.Vec4(1.0,1.0,0.0,1.0)) linesGeom.setColorArray(colors, osg.Array.BIND_OVERALL) # set the normal in the same way color. normals = osg.Vec3Array() normals.push_back(osg.Vec3(0.0,-1.0,0.0)) linesGeom.setNormalArray(normals, osg.Array.BIND_OVERALL)
for (int i=0 i<5 i++) dy = osg.Vec3(0.0,-30.0,0.0) dx = osg.Vec3(120.0,0.0,0.0) geode = osg.Geode() stateset = geode.getOrCreateStateSet() char *opts[]="One", "Two", "Three", "January", "Feb", "2003" quad = osg.Geometry() stateset.setMode(GL_LIGHTING,osg.StateAttribute.OFF) stateset.setMode(GL_DEPTH_TEST,osg.StateAttribute.OFF) name = "subOption" name += " " name += str(opts[i]) geode.setName(name) vertices = osg.Vec3Array(4) # 1 quad colors = osg.Vec4Array() colors = osg.Vec4Array() colors.push_back(osg.Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0)) quad.setColorArray(colors, osg.Array.BIND_OVERALL) (*vertices)[0]=position (*vertices)[1]=position+dx (*vertices)[2]=position+dx+dy (*vertices)[3]=position+dy quad.setVertexArray(vertices) quad.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) geode.addDrawable(quad) hudCamera.addChild(geode) position += delta