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 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 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 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 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 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(): # 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 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 = 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 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)
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 ) ) c = osg.Vec4Array() geom.setColorArray( c ) geom.setColorBinding( deprecated_osg.Geometry.BIND_OVERALL ) c.push_back( osg.Vec4( 0., 1., 1., 1. ) ) n = osg.Vec3Array() geom.setNormalArray( n ) geom.setNormalBinding( deprecated_osg.Geometry.BIND_PER_PRIMITIVE ) n.push_back( osg.Vec3( -1., 0., 0. ) ) n.push_back( osg.Vec3( 1., 0., 0. ) ) n.push_back( osg.Vec3( 0., 0., -1. ) ) n.push_back( osg.Vec3( 0., 0., 1. ) ) n.push_back( osg.Vec3( 0., 1., 0. ) ) geom.addPrimitiveSet( osg.DrawArrays( GL_QUADS, 0, 20 ) ) box.addDrawable( geom )
def createRectangle(bb, filename): top_left = osg.Vec3(bb.xMin(),bb.yMax(),bb.zMax()) bottom_left = osg.Vec3(bb.xMin(),bb.yMax(),bb.zMin()) bottom_right = osg.Vec3(bb.xMax(),bb.yMax(),bb.zMin()) top_right = osg.Vec3(bb.xMax(),bb.yMax(),bb.zMax()) # create geometry geom = osg.Geometry() vertices = osg.Vec3Array(4) (*vertices)[0] = top_left (*vertices)[1] = bottom_left (*vertices)[2] = bottom_right (*vertices)[3] = top_right geom.setVertexArray(vertices) texcoords = osg.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) normals = osg.Vec3Array(1) (*normals)[0].set(0.0,-1.0,0.0) geom.setNormalArray(normals, osg.Array.BIND_OVERALL) colors = osg.Vec4Array(1) (*colors)[0].set(1.0,1.0,1.0,1.0) geom.setColorArray(colors, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(GL_QUADS, 0, 4)) # disable display list so our modified tex coordinates show up geom.setUseDisplayList(False) # load image img = osgDB.readImageFile(filename) # setup texture texture = osg.TextureRectangle(img) texmat = osg.TexMat() texmat.setScaleByTextureRectangleSize(True) # setup state state = geom.getOrCreateStateSet() state.setTextureAttributeAndModes(0, texture, osg.StateAttribute.ON) state.setTextureAttributeAndModes(0, texmat, osg.StateAttribute.ON) # turn off lighting state.setMode(GL_LIGHTING, osg.StateAttribute.OFF) # install 'update' callback geode = osg.Geode() geode.addDrawable(geom) geode.setUpdateCallback(TexturePanCallback(texmat)) return geode
def createDistortionSubgraph(subgraph, clearColour): distortionNode = osg.Group() tex_width = 1024 tex_height = 1024 texture = osg.Texture2D() texture.setTextureSize(tex_width, tex_height) texture.setInternalFormat(GL_RGBA) texture.setFilter(osg.Texture2D.MIN_FILTER,osg.Texture2D.LINEAR) texture.setFilter(osg.Texture2D.MAG_FILTER,osg.Texture2D.LINEAR) # set up the render to texture camera. camera = osg.Camera() # set clear the color and depth buffer camera.setClearColor(clearColour) camera.setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) # just inherit the main cameras view camera.setReferenceFrame(osg.Transform.RELATIVE_RF) camera.setProjectionMatrix(osg.Matrixd.identity()) camera.setViewMatrix(osg.Matrixd.identity()) # set viewport camera.setViewport(0,0,tex_width,tex_height) # set the camera to render before the main camera. camera.setRenderOrder(osg.Camera.PRE_RENDER) # tell the camera to use OpenGL frame buffer object where supported. camera.setRenderTargetImplementation(osg.Camera.FRAME_BUFFER_OBJECT) # attach the texture and use it as the color buffer. camera.attach(osg.Camera.COLOR_BUFFER, texture) # add subgraph to render camera.addChild(subgraph) distortionNode.addChild(camera) # set up the hud camera # create the quad to visualize. polyGeom = osg.Geometry() polyGeom.setSupportsDisplayList(False) origin = osg.Vec3(0.0,0.0,0.0) xAxis = osg.Vec3(1.0,0.0,0.0) yAxis = osg.Vec3(0.0,1.0,0.0) height = 1024.0 width = 1280.0 noSteps = 50 vertices = osg.Vec3Array() texcoords = osg.Vec2Array() colors = osg.Vec4Array() bottom = origin dx = xAxis*(width/((float)(noSteps-1))) dy = yAxis*(height/((float)(noSteps-1))) bottom_texcoord = osg.Vec2(0.0,0.0) dx_texcoord = osg.Vec2(1.0/(float)(noSteps-1),0.0) dy_texcoord = osg.Vec2(0.0,1.0/(float)(noSteps-1)) int i,j for(i=0i<noSteps++i) cursor = bottom+dy*(float)i texcoord = bottom_texcoord+dy_texcoord*(float)i for(j=0j<noSteps++j)
def createDomeDistortionMesh(origin, widthVector, heightVector, arguments): sphere_radius = 1.0 if arguments.read("--radius", sphere_radius) : collar_radius = 0.45 if arguments.read("--collar", collar_radius) : center = osg.Vec3d(0.0,0.0,0.0) eye = osg.Vec3d(0.0,0.0,0.0) distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius) if arguments.read("--distance", distance) : centerProjection = False projector = eye - osg.Vec3d(0.0,0.0, distance) osg.notify(osg.NOTICE), "Projector position = ", projector osg.notify(osg.NOTICE), "distance = ", distance # create the quad to visualize. geometry = osg.Geometry() geometry.setSupportsDisplayList(False) xAxis = osg.Vec3(widthVector) width = widthVector.length() xAxis /= width yAxis = osg.Vec3(heightVector) height = heightVector.length() yAxis /= height noSteps = 50 vertices = osg.Vec3Array() texcoords = osg.Vec3Array() colors = osg.Vec4Array() bottom = origin dx = xAxis*(width/((float)(noSteps-1))) dy = yAxis*(height/((float)(noSteps-1))) screenCenter = origin + widthVector*0.5 + heightVector*0.5 screenRadius = heightVector.length() * 0.5 int i,j if centerProjection : for(i=0i<noSteps++i) cursor = bottom+dy*(float)i for(j=0j<noSteps++j) delta = osg.Vec2(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y()) theta = atan2(-delta.y(), delta.x()) phi = osg.PI_2 * delta.length() / screenRadius if phi > osg.PI_2 : phi = osg.PI_2 phi *= 2.0 # osg.notify(osg.NOTICE), "theta = ", theta, "phi=", phi texcoord = osg.Vec3(sin(phi) * cos(theta), sin(phi) * sin(theta), cos(phi)) vertices.push_back(cursor) colors.push_back(osg.Vec4(1.0,1.0,1.0,1.0)) texcoords.push_back(texcoord) cursor += dx
def createPyramid(): pyramidGeode = osg.Geode() pyramidGeometry = osg.Geometry() pyramidGeode.addDrawable(pyramidGeometry) # Specify the vertices: pyramidVertices = osg.Vec3Array() pyramidVertices.append(osg.Vec3(0, 0, 0)) # front left pyramidVertices.append(osg.Vec3(2, 0, 0)) # front right pyramidVertices.append(osg.Vec3(2, 2, 0)) # back right pyramidVertices.append(osg.Vec3(0, 2, 0)) # back left pyramidVertices.append(osg.Vec3(1, 1, 2)) # peak # Associate this set of vertices with the geometry associated with the # geode we added to the scene. pyramidGeometry.setVertexArray(pyramidVertices) # Create a QUAD primitive for the base by specifying the # vertices from our vertex list that make up this QUAD: pyramidBase = osg.DrawElementsUInt(osg.PrimitiveSet.QUADS, 0) pyramidBase.append(3) pyramidBase.append(2) pyramidBase.append(1) pyramidBase.append(0) # Add this primitive to the geometry: # pyramidGeometry.addPrimitiveSet(pyramidBase) # code to create other faces goes here! pyramidGeometry.addPrimitiveSet(pyramidBase) # Repeat the same for each of the four sides. Again, vertices are specified in counter-clockwise order. pyramidFaceOne = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceOne.append(0) pyramidFaceOne.append(1) pyramidFaceOne.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceOne) pyramidFaceTwo = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceTwo.append(1) pyramidFaceTwo.append(2) pyramidFaceTwo.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceTwo) pyramidFaceThree = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceThree.append(2) pyramidFaceThree.append(3) pyramidFaceThree.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceThree) pyramidFaceFour = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceFour.append(3) pyramidFaceFour.append(0) pyramidFaceFour.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceFour) colors = osg.Vec4Array() colors.append(osg.Vec4(1.0, 0.0, 0.0, 1.0)) #index 0 red colors.append(osg.Vec4(0.0, 1.0, 0.0, 1.0)) #index 1 green colors.append(osg.Vec4(0.0, 0.0, 1.0, 1.0)) #index 2 blue colors.append(osg.Vec4(1.0, 1.0, 1.0, 1.0)) #index 3 white colors.append(osg.Vec4(1.0, 0.0, 0.0, 1.0)) #index 4 red pyramidGeometry.setColorArray(colors) pyramidGeometry.setColorBinding(osg.Geometry.BIND_PER_VERTEX) # Since the mapping from vertices to texture coordinates is 1:1, # we don't need to use an index array to map vertices to texture # coordinates. We can do it directly with the 'setTexCoordArray' # method of the Geometry class. # This method takes a variable that is an array of two dimensional # vectors (osg.Vec2). This variable needs to have the same # number of elements as our Geometry has vertices. Each array element # defines the texture coordinate for the cooresponding vertex in the # vertex array. texcoords = osg.Vec2Array(5) texcoords[0].set(0.00, 0.0) # tex coord for vertex 0 texcoords[1].set(0.25, 0.0) # tex coord for vertex 1 texcoords[2].set(0.50, 0.0) # "" texcoords[3].set(0.75, 0.0) # "" texcoords[4].set(0.50, 1.0) # "" pyramidGeometry.setTexCoordArray(0, texcoords) return pyramidGeode
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) # 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_LOOP,0,numCoords))
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) # set up colours colours = osg.Vec4Array(1) geometry.setColorArray(colours, osg.Array.BIND_OVERALL) (*colours)[0].set(1.0,1.0,1.0,1.0) # set up the primitive set to draw lines geometry.addPrimitiveSet(osg.DrawArrays(GL_LINES,0,noStars*2)) # set up the points for the stars. points = osg.DrawElementsUShort(GL_POINTS,noStars) geometry.addPrimitiveSet(points) for(i=0i<noStars++i) (*points)[i] = i*2 geometry.setUseDisplayList(False) geometry.setDrawCallback(DrawCallback)()
vx.push_back(osg.Vec3(rect.x0, rect.y1, z)) if shadow : vx.push_back(osg.Vec3(rect.x0+shadow_space, rect.y0-shadow_size, z)) vx.push_back(osg.Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z)) vx.push_back(osg.Vec3(rect.x1, rect.y0, z)) vx.push_back(osg.Vec3(rect.x0+shadow_space, rect.y0, z)) vx.push_back(osg.Vec3(rect.x1, rect.y1-shadow_space, z)) vx.push_back(osg.Vec3(rect.x1, rect.y0, z)) vx.push_back(osg.Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z)) vx.push_back(osg.Vec3(rect.x1+shadow_size, rect.y1-shadow_space, z)) geo.setVertexArray(vx) clr = osg.Vec4Array() clr.push_back(color) clr.push_back(color) clr.push_back(color) clr.push_back(color) if shadow : alpha = color.w() * 0.5 black = osg.Vec3(0, 0, 0) clr.push_back(osg.Vec4(black, 0)) clr.push_back(osg.Vec4(black, 0)) clr.push_back(osg.Vec4(black, alpha)) clr.push_back(osg.Vec4(black, alpha))
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
def createPyramid(): pyramidGeode = osg.Geode() pyramidGeometry = osg.Geometry() pyramidGeode.addDrawable(pyramidGeometry) pyramidVertices = osg.Vec3Array() pyramidVertices.append( osg.Vec3(0, 0, 0) ) # front left pyramidVertices.append( osg.Vec3(2, 0, 0) ) # front right pyramidVertices.append( osg.Vec3(2, 2, 0) ) # back right pyramidVertices.append( osg.Vec3( 0,2, 0) ) # back left pyramidVertices.append( osg.Vec3( 1, 1,2) ) # peak # Associate this set of vertices with the geometry associated with the # geode we added to the scene. pyramidGeometry.setVertexArray( pyramidVertices ) pyramidBase = osg.DrawElementsUInt(osg.PrimitiveSet.QUADS, 3) pyramidBase.append(3) pyramidBase.append(2) pyramidBase.append(1) pyramidBase.append(0) pyramidGeometry.addPrimitiveSet(pyramidBase) pyramidFaceOne = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceOne.append(0) pyramidFaceOne.append(1) pyramidFaceOne.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceOne) pyramidFaceTwo = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceTwo.append(1) pyramidFaceTwo.append(2) pyramidFaceTwo.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceTwo) pyramidFaceThree = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceThree.append(2) pyramidFaceThree.append(3) pyramidFaceThree.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceThree) pyramidFaceFour = osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLES, 0) pyramidFaceFour.append(3) pyramidFaceFour.append(0) pyramidFaceFour.append(4) pyramidGeometry.addPrimitiveSet(pyramidFaceFour) colors = osg.Vec4Array() colors.append(osg.Vec4(1.0, 0.0, 0.0, 1.0) ) #index 0 red colors.append(osg.Vec4(0.0, 1.0, 0.0, 1.0) ) #index 1 green colors.append(osg.Vec4(0.0, 0.0, 1.0, 1.0) ) #index 2 blue colors.append(osg.Vec4(1.0, 1.0, 1.0, 1.0) ) #index 3 white colorIndexArray = osg.UIntArray() colorIndexArray.append(0) # vertex 0 assigned color array element 0 colorIndexArray.append(1) # vertex 1 assigned color array element 1 colorIndexArray.append(2) # vertex 2 assigned color array element 2 colorIndexArray.append(3) # vertex 3 assigned color array element 3 colorIndexArray.append(0) # vertex 4 assigned color array element 0 pyramidGeometry.setColorArray(colors) pyramidGeometry.setColorIndices(colorIndexArray) pyramidGeometry.setColorBinding(osg.Geometry.BIND_PER_VERTEX) texcoords = osg.Vec2Array(5) texcoords[0].set(0.00,0.0) texcoords[1].set(0.25,0.0) texcoords[2].set(0.50,0.0) texcoords[3].set(0.75,0.0) texcoords[4].set(0.50,1.0) pyramidGeometry.setTexCoordArray(0,texcoords) return pyramidGeode
0, 1, 5, 5, 4, 0, 1, 6, 5, 2, 6, 1, 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)
geodeSpace = osg.Geode() geodeSpace.setName( name ) geodeSpace.addDrawable( sSpaceSphere ) return( geodeSpace ) # end SolarSystem.createSpace osg.Geode* SolarSystem.createPlanet( double radius, str name, osg.Vec4 color , str textureName) # create a container that makes the sphere drawable sPlanetSphere = osg.Geometry() # set the single colour so bind overall colours = osg.Vec4Array(1) (*colours)[0] = color sPlanetSphere.setColorArray(colours, osg.Array.BIND_OVERALL) # now set up the coords, normals and texcoords for geometry numX = 100 numY = 50 numVertices = numX*numY coords = osg.Vec3Array(numVertices) sPlanetSphere.setVertexArray(coords) normals = osg.Vec3Array(numVertices) sPlanetSphere.setNormalArray(normals, osg.Array.BIND_PER_VERTEX) texcoords = osg.Vec2Array(numVertices)
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)
def createOccluder(v1, v2, v3, v4, holeRatio): # create an occluder which will sit alongside the loaded model. occluderNode = osg.OccluderNode() # create the convex planar occluder cpo = osg.ConvexPlanarOccluder() # attach it to the occluder node. occluderNode.setOccluder(cpo) occluderNode.setName("occluder") # set the occluder up for the front face of the bounding box. occluder = cpo.getOccluder() occluder.add(v1) occluder.add(v2) occluder.add(v3) occluder.add(v4) # create a hole at the center of the occluder if needed. if holeRatio>0.0 : # create hole. ratio = holeRatio one_minus_ratio = 1-ratio center = (v1+v2+v3+v4)*0.25 v1dash = v1*ratio + center*one_minus_ratio v2dash = v2*ratio + center*one_minus_ratio v3dash = v3*ratio + center*one_minus_ratio v4dash = v4*ratio + center*one_minus_ratio hole = osg.ConvexPlanarPolygon() hole.add(v1dash) hole.add(v2dash) hole.add(v3dash) hole.add(v4dash) cpo.addHole(hole) # create a drawable for occluder. geom = osg.Geometry() coords = osg.Vec3Array(occluder.getVertexList().begin(),occluder.getVertexList().end()) geom.setVertexArray(coords) colors = osg.Vec4Array(1) (*colors)[0].set(1.0,1.0,1.0,0.5) geom.setColorArray(colors, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) geode = osg.Geode() geode.addDrawable(geom) stateset = osg.StateSet() stateset.setMode(GL_LIGHTING,osg.StateAttribute.OFF) stateset.setMode(GL_BLEND,osg.StateAttribute.ON) stateset.setRenderingHint(osg.StateSet.TRANSPARENT_BIN) geom.setStateSet(stateset) # add the occluder geode as a child of the occluder, # as the occluder can't self occlude its subgraph the # geode will never be occluded by this occluder. occluderNode.addChild(geode) return occluderNode