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 )
class Heatmap (osg.Geode) : Heatmap(float width, float depth, float maxheight, unsigned int K, unsigned int N, float maximum, float transparency) ~Heatmap() setData = void(float *buffer, float maxheight, float maximum, float transparency) m_K = unsigned int() m_N = unsigned int() *m_data = float() m_img2 = osg.Image() m_tex2 = osg.Texture2D() colorimg = osg.Image() colortex = osg.Texture1D() *maximumUniform = osg.Uniform() *maxheightUniform = osg.Uniform() *transparencyUniform = osg.Uniform()
"void main(void)\n" "\n" " gl_FragData[0] = \n" " vec4(texture2DRect( textureID0, gl_TexCoord[0].st ).rgb, 1) + \n" " vec4(texture2DRect( textureID1, gl_TexCoord[0].st ).rgb, 1) + \n" " vec4(texture2DRect( textureID2, gl_TexCoord[0].st ).rgb, 1) + \n" " -0.5*vec4(texture2DRect( textureID3, gl_TexCoord[0].st ).rgb, 1) \n" "\n" fshader = osg.Shader( osg.Shader.FRAGMENT , shaderSource) program = osg.Program() program.addShader( fshader) stateset.setAttributeAndModes( program, osg.StateAttribute.ON | osg.StateAttribute.OVERRIDE ) stateset.addUniform(osg.Uniform("textureID0", 0)) stateset.addUniform(osg.Uniform("textureID1", 1)) stateset.addUniform(osg.Uniform("textureID2", 2)) stateset.addUniform(osg.Uniform("textureID3", 3)) #stateset.setDataVariance(osg.Object.DYNAMIC) geode = osg.Geode() geode.addDrawable(polyGeom) parent.addChild(geode) # now create the camera to do the multiple render to texture camera = osg.Camera() # set up the background color and clear mask.
#####################################/ # Compose a scenegraph with examples of GLSL shaders #define TEXUNIT_SINE 1 #define TEXUNIT_NOISE 2 osg.Group GL2Scene.buildScene() noiseTexture = osgUtil.create3DNoiseTexture( 32 #128 ) sineTexture = make1DSineTexture( 32 #1024 ) # the root of our scenegraph. rootNode = osg.Group() # attach some Uniforms to the root, to be inherited by Programs. OffsetUniform = osg.Uniform( "Offset", osg.Vec3(0.0, 0.0, 0.0) ) SineUniform = osg.Uniform( "Sine", 0.0 ) Color1Uniform = osg.Uniform( "Color1", osg.Vec3(0.0, 0.0, 0.0) ) Color2Uniform = osg.Uniform( "Color2", osg.Vec3(0.0, 0.0, 0.0) ) OffsetUniform.setUpdateCallback(AnimateCallback(AnimateCallback.OFFSET)) SineUniform.setUpdateCallback(AnimateCallback(AnimateCallback.SIN)) Color1Uniform.setUpdateCallback(AnimateCallback(AnimateCallback.COLOR1)) Color2Uniform.setUpdateCallback(AnimateCallback(AnimateCallback.COLOR2)) ss = rootNode.getOrCreateStateSet() ss.addUniform( OffsetUniform ) ss.addUniform( SineUniform ) ss.addUniform( Color1Uniform ) ss.addUniform( Color2Uniform )
"uniform vec4 cutoff_color\n" "uniform sampler2D movie_texture\n" "void main(void)\n" "\n" " vec4 texture_color = texture2D(movie_texture, gl_TexCoord[0].st) \n" " if all(lessThanEqual(texture_color,cutoff_color)) : discard \n" " gl_FragColor = texture_color\n" "\n" program = osg.Program() program.addShader(osg.Shader(osg.Shader.FRAGMENT, shaderSourceTextureRec if (useTextureRectangle) else shaderSourceTexture2D)) stateset.addUniform(osg.Uniform("cutoff_color",osg.Vec4(0.1,0.1,0.1,1.0))) stateset.addUniform(osg.Uniform("movie_texture",0)) stateset.setAttribute(program) pos = osg.Vec3(0.0,0.0,0.0) topleft = pos bottomright = pos xyPlane = fullscreen useAudioSink = False while arguments.read("--audio") : useAudioSink = True #if USE_SDL
stateSet.setTextureAttribute(_texUnit, texMat) if _texUnit not = 0 : # # GLSL pipeline support # #ifdef USE_TEXTURE_RECTANGLE # osg.Uniform.SAMPLER_2D_RECT_SHADOW not yet available in OSG 3.0.1 # osg.Uniform* depthUniform = osg.Uniform(osg.Uniform.SAMPLER_2D_RECT_SHADOW, "depthtex") # depthUniform.set((int)_texUnit) depthUniform = osg.Uniform("depthtex", (int)_texUnit) invWidthUniform = osg.Uniform("invWidth", (float)1.0) invHeightUniform = osg.Uniform("invHeight", (float)1.0) #else: depthUniform = osg.Uniform(osg.Uniform.SAMPLER_2D_SHADOW, "depthtex") depthUniform.set((int)_texUnit) invWidthUniform = osg.Uniform("invWidth", (float)1.0 / _texWidth) invHeightUniform = osg.Uniform("invHeight", (float)1.0 / _texHeight) #endif offsetXUniform = osg.Uniform("offsetX", (float)viewport.x()) offsetYUniform = osg.Uniform("offsetY", (float)viewport.y()) # uniforms required for any any GLSL implementation in the rendered geometry stateSet.addUniform(depthUniform) stateSet.addUniform(invWidthUniform) stateSet.addUniform(invHeightUniform) stateSet.addUniform(offsetXUniform) stateSet.addUniform(offsetYUniform) cullVisitor.pushStateSet(stateSet)
class AdaptNumPixelUniform (osg.Camera.DrawCallback) : AdaptNumPixelUniform() _atomicCounterArray = osg.UIntArray() _atomicCounterArray.push_back(0) virtual void operator () (osg.RenderInfo renderInfo) _acbb.readData(*renderInfo.getState(), *_atomicCounterArray) numPixel = osg.maximum(1u, _atomicCounterArray.front()) if renderInfo.getView().getFrameStamp().getFrameNumber() % 10 : == 0 : OSG_INFO, "osgatomiccounter : draw ", numPixel, " pixels." _invNumPixelUniform.set( 1.0 / static_cast<float>(numPixel) ) _invNumPixelUniform = osg.Uniform() _atomicCounterArray = osg.UIntArray() _acbb = osg.AtomicCounterBufferBinding() def createProgram(): vp = strstream() vp, "#version 420 compatibility\n", "\n", "void main(void)\n", "\n", " gl_Position = ftransform()\n", "\n" vpShader = osg.Shader( osg.Shader.VERTEX, vp.str() )
def CreateAdvancedHierarchy(model): if not model : return NULL offset = model.getBound().radius() * 1.3 # diameter # Create transforms for translated instances of the model transformCenterMiddle = osg.MatrixTransform( ) transformCenterMiddle.setMatrix( osg.Matrix.translate( 0,0, offset * 0.5 ) ) transformCenterMiddle.addChild( model ) transformCenterTop = osg.MatrixTransform( ) transformCenterMiddle.addChild( transformCenterTop ) transformCenterTop.setMatrix( osg.Matrix.translate( 0,0,offset ) ) transformCenterTop.addChild( model ) transformCenterBottom = osg.MatrixTransform( ) transformCenterMiddle.addChild( transformCenterBottom ) transformCenterBottom.setMatrix( osg.Matrix.translate( 0,0,-offset ) ) transformCenterBottom.addChild( model ) transformLeftBottom = osg.MatrixTransform( ) transformCenterBottom.addChild( transformLeftBottom ) transformLeftBottom.setMatrix( osg.Matrix.translate( -offset * 0.8,0, -offset * 0.8 ) ) transformLeftBottom.addChild( model ) transformRightBottom = osg.MatrixTransform( ) transformCenterBottom.addChild( transformRightBottom ) transformRightBottom.setMatrix( osg.Matrix.translate( offset * 0.8,0, -offset * 0.8 ) ) transformRightBottom.addChild( model ) # Set default VirtualProgram in root StateSet # With main vertex and main fragment shaders calling # lighting and texture functions defined in aditional shaders # Lighting is done per vertex using simple directional light # Texture uses stage 0 TexCoords and TexMap if 1 : # NOTE: # duplicating the same semantics name in virtual program # is only possible if its used for shaders of differing types # here for VERTEX and FRAGMENT vp = VirtualProgram( ) transformCenterMiddle.getOrCreateStateSet().setAttribute( vp ) AddLabel( transformCenterMiddle, "Per Vertex Lighting Virtual Program", offset ) SetVirtualProgramShader( vp, "main", osg.Shader.VERTEX, "Vertex Main", MainVertexShaderSource ) SetVirtualProgramShader( vp, "main", osg.Shader.FRAGMENT, "Fragment Main", MainFragmentShaderSource ) SetVirtualProgramShader( vp, "texture",osg.Shader.VERTEX, "Vertex Texture Coord 0", TexCoordTextureVertexShaderSource ) SetVirtualProgramShader( vp, "texture",osg.Shader.FRAGMENT, "Fragment Texture", TextureFragmentShaderSource ) SetVirtualProgramShader( vp, "lighting",osg.Shader.VERTEX, "Vertex Lighting", PerVertexDirectionalLightingVertexShaderSource ) SetVirtualProgramShader( vp, "lighting",osg.Shader.FRAGMENT, "Fragment Lighting", PerVertexLightingFragmentShaderSource ) transformCenterMiddle.getOrCreateStateSet(). addUniform( osg.Uniform( "baseTexture", 0 ) )
program.addShader( vertexShader ) ss.setAttribute( program, osg.StateAttribute.ON | osg.StateAttribute.PROTECTED ) iLogo = osgDB.readImageFile( "Images/osg128.png" ) if not iLogo.valid() : osg.notify( osg.ALWAYS ), "Can't open image file osg128.png" return( NULL ) texLogo = osg.Texture2D( iLogo ) texLogo.setFilter( osg.Texture2D.MIN_FILTER, osg.Texture2D.LINEAR ) texLogo.setFilter( osg.Texture2D.MAG_FILTER, osg.Texture2D.LINEAR ) ss.setTextureAttribute( 0, texLogo ) texLogoUniform = osg.Uniform( "osgLogo", 0 ) ss.addUniform( texLogoUniform ) return( ss.release() ) def main(argv): arguments = osg.ArgumentParser(argv) # Make a scene graph consisting of a single Geode, containing # a single Geometry, and a single PrimitiveSet. geode = osg.Geode()
program = osg.Program() stateset.setAttribute(program) fragSource = "uniform sampler2D baseTexture\n" "uniform mat4 colorMatrix\n" "void main(void)\n" "\n" " vec4 color = texture2D( baseTexture, gl_TexCoord[0].st )\n" " gl_FragColor = colorMatrix * color\n" "\n" program.addShader(osg.Shader(osg.Shader.FRAGMENT, fragSource)) stateset.addUniform(osg.Uniform("baseTexture",0)) colorMatrix = osg.Matrixf( 0.3, 0.3, 0.3, 0.0, 0.59, 0.59, 0.59, 0.0, 0.11, 0.11, 0.11, 0.0, 0.0, 0.0, 0.0, 1.0 ) stateset.addUniform(osg.Uniform("colorMatrix",colorMatrix)) return stateset def createSectorForImage(image, texmat, s, t, radius, height, length):
"out vec4 fragData \n" " \n" "void main() \n" " \n" " fragData = color \n" " \n" fShader = osg.Shader( osg.Shader.FRAGMENT, fragmentSource ) program = osg.Program() program.addShader( vShader ) program.addShader( fShader ) stateSet.setAttribute( program ) lightDir = osg.Vec3f( 0., 0.5, 1. ) lightDir.normalize() stateSet.addUniform( osg.Uniform( "ecLightDir", lightDir ) ) def main(argv): arguments = osg.ArgumentParser( argc, argv ) root = osgDB.readNodeFiles( arguments ) if root == NULL : osg.notify( osg.FATAL ), "Unable to load model from command line." return( 1 ) configureShaders( root.getOrCreateStateSet() ) int width( 800 ), height( 450 ) version = str( "3.1" ) traits = osg.GraphicsContext.Traits()
program.addShader(_shader) for (int i = 0 i < nbAttribs i++) ss = strstream() ss, "boneWeight", i program.addBindAttribLocation(ss.str(), attribIndex + i) osg.notify(osg.INFO), "set vertex attrib ", ss.str() for (int i = 0 i < nbAttribs i++) ss = strstream() ss, "boneWeight", i geom.setVertexAttribArray(attribIndex + i, getVertexAttrib(i)) ss = osg.StateSet() ss.addUniform(getMatrixPaletteUniform()) ss.addUniform(osg.Uniform("nbBonesPerVertex", getNumBonesPerVertex())) ss.setAttributeAndModes(program) geom.setStateSet(ss) _needInit = False return True class SetupRigGeometry (osg.NodeVisitor) : _hardware = bool() SetupRigGeometry( bool hardware = True) : osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _hardware(hardware) def apply(geode):
def createModel(shader, textureFileName, terrainFileName, dynamic, useVBO): geode = osg.Geode() geom = osg.Geometry() geode.addDrawable(geom) # dimensions for ~one million triangles :-) num_x = 708 num_y = 708 # set up state stateset = geom.getOrCreateStateSet() program = osg.Program() stateset.setAttribute(program) if shader=="simple" : vertex_shader = osg.Shader(osg.Shader.VERTEX, vertexShaderSource_simple) program.addShader(vertex_shader) coeff = osg.Uniform("coeff",osg.Vec4(1.0,-1.0,-1.0,1.0)) stateset.addUniform(coeff) if dynamic : coeff.setUpdateCallback(UniformVarying)() coeff.setDataVariance(osg.Object.DYNAMIC) stateset.setDataVariance(osg.Object.DYNAMIC) elif shader=="matrix" : vertex_shader = osg.Shader(osg.Shader.VERTEX, vertexShaderSource_matrix) program.addShader(vertex_shader) origin = osg.Uniform("origin",osg.Vec4(0.0,0.0,0.0,1.0)) stateset.addUniform(origin) coeffMatrix = osg.Uniform("coeffMatrix", osg.Matrix(1.0,0.0,1.0,0.0, 0.0,0.0,-1.0,0.0, 0.0,1.0,-1.0,0.0, 0.0,0.0,1.0,0.0)) stateset.addUniform(coeffMatrix) elif shader=="texture" : vertex_shader = osg.Shader(osg.Shader.VERTEX, vertexShaderSource_texture) program.addShader(vertex_shader) image = 0 if terrainFileName.empty() : image = osg.Image() tx = 38 ty = 39 image.allocateImage(tx,ty,1,GL_LUMINANCE,GL_FLOAT,1) for(unsigned int r=0r<ty++r) for(unsigned int c=0c<tx++c) *((float*)image.data(c,r)) = vertex[r+c*39][2]*0.1 num_x = tx num_y = tx
num_y = tx else: image = osgDB.readImageFile(terrainFileName) num_x = image.s() num_y = image.t() vertexTexture = osg.Texture2D(image) vertexTexture.setFilter(osg.Texture.MIN_FILTER,osg.Texture.NEAREST) vertexTexture.setFilter(osg.Texture.MAG_FILTER,osg.Texture.NEAREST) vertexTexture.setInternalFormat(GL_LUMINANCE_FLOAT32_ATI) stateset.setTextureAttributeAndModes(1,vertexTexture) vertexTextureSampler = osg.Uniform("vertexTexture",1) stateset.addUniform(vertexTextureSampler) fragment_shader = osg.Shader(osg.Shader.FRAGMENT, fragmentShaderSource) program.addShader(fragment_shader) texture = osg.Texture2D(osgDB.readImageFile(textureFileName)) stateset.setTextureAttributeAndModes(0,texture) baseTextureSampler = osg.Uniform("baseTexture",0) stateset.addUniform(baseTextureSampler) # set up geometry data.
v = osg.Vec3Array() for(TreeList.iterator itr=cell._trees.begin() not = cell._trees.end() ++itr) tree = **itr v.push_back(tree._position) v.push_back(osg.Vec3(#tree._height30.0,(double)random(0.75,1.15),(double)random(1.0,1.250))) geometry.setVertexArray( v ) geometry.addPrimitiveSet( osg.DrawArrays( GL_LINES, 0, v.size() ) ) sset = geode.getOrCreateStateSet() sset.setMode( GL_LIGHTING, osg.StateAttribute.OFF ) sset.setAttribute( createGeometryShader() ) baseTextureSampler = osg.Uniform("baseTexture",0) sset.addUniform(baseTextureSampler) if needGroup : group = osg.Group() for(Cell.CellList.iterator itr=cell._cells.begin() not = cell._cells.end() ++itr) group.addChild(createGeometryShaderGraph(itr,dstate)) if geode : group.addChild(geode) if group : return group else return geode
vertex_shader.addCodeInjection(-1,"varying vec4 color\n") vertex_shader.addCodeInjection(-1,"varying vec4 texcoord\n") vertex_shader.addCodeInjection(0,"color = gl_Color\n") vertex_shader.addCodeInjection(0,"texcoord = gl_MultiTexCoord0\n") vertex_shader.addCodeInjection(0,"gl_Position = ftransform()\n") sa.addShader(vertex_shader) fragment_shader = osg.Shader(osg.Shader.FRAGMENT) fragment_shader.addCodeInjection(-1,"varying vec4 color\n") fragment_shader.addCodeInjection(-1,"varying vec4 texcoord\n") fragment_shader.addCodeInjection(-1,"uniform sampler2D baseTexture \n") fragment_shader.addCodeInjection(0,"gl_FragColor = color * texture2DProj( baseTexture, texcoord )\n") sa.addShader(fragment_shader) sa.addUniform(osg.Uniform("baseTexture",0)) # inherit the ShaderComponents entirely from above pat = osg.PositionAttitudeTransform() pat.setPosition(position) pat.addChild(node) position.x() += spacing group.addChild(pat) pat = osg.PositionAttitudeTransform() pat.setPosition(position) pat.addChild(node)
int main(int, char **) # construct the viewer. viewer = osgViewer.Viewer() # use a geode with a Box ShapeDrawable basicModel = osg.Geode() basicModel.addDrawable(osg.ShapeDrawable(osg.Box(osg.Vec3(0.0,0.0,0.0),1.0))) # create the "blocky" shader, a simple animation test ss = basicModel.getOrCreateStateSet() program = osg.Program() program.setName( "blocky" ) program.addShader( osg.Shader( osg.Shader.VERTEX, blockyVertSource ) ) program.addShader( osg.Shader( osg.Shader.FRAGMENT, blockyFragSource ) ) ss.setAttributeAndModes(program, osg.StateAttribute.ON) # attach some animated Uniform variable to the state set SineUniform = osg.Uniform( "Sine", 0.0 ) ss.addUniform( SineUniform ) SineUniform.setUpdateCallback(AnimateCallback(AnimateCallback.SIN)) # run the osg.Viewer using our model viewer.setSceneData( basicModel ) return viewer.run() #EOF if __name__ == "__main__": main(sys.argv)
ss = transformRightBottom.getOrCreateStateSet() vp = VirtualProgram( ) ss.setAttribute( vp ) SetVirtualProgramShader( vp, "texture",osg.Shader.VERTEX, "Vertex Texture Sphere Map", SphereMapTextureVertexShaderSource ) texture = osg.Texture2D( # osgDB.readImageFile("Images/reflect.rgb") osgDB.readImageFile("Images/skymap.jpg") ) # Texture is set on stage 1 to not interfere with label text # The same could be achieved with texture override # but such approach also turns off label texture ss.setTextureAttributeAndModes( 1, texture, osg.StateAttribute.ON ) ss.addUniform( osg.Uniform( "baseTexture", 1 ) ) #if 0 # Could be useful with Fixed Vertex Pipeline texGen = osg.TexGen() texGen.setMode( osg.TexGen.SPHERE_MAP ) # Texture states applied ss.setTextureAttributeAndModes( 1, texGen, osg.StateAttribute.ON ) #endif # Top center model usues osg.Program overriding VirtualProgram in model if 1 : AddLabel( transformCenterTop, "Fixed Vertex + Simple Fragment osg.Program", offset ) program = osg.Program()