def createSceneGraph(arguments): node = osgDB.readNodeFiles(arguments) if not node : return 0 group = osg.Group() spacing = node.getBound().radius() * 2.0 position = osg.Vec3d(0.0,0.0,0.0) sa1 = NULL stateset = group.getOrCreateStateSet() sa = osg.ShaderAttribute() sa.setType(osg.StateAttribute.Type(10000)) sa1 = sa stateset.setAttribute(sa) vertex_shader = osg.Shader(osg.Shader.VERTEX) 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)
def createShader(): pgm = osg.Program() pgm.setName( "osgshader2 demo" ) pgm.addShader( osg.Shader( osg.Shader.VERTEX, vertSource ) ) pgm.addShader( osg.Shader( osg.Shader.FRAGMENT, fragSource ) ) #ifdef ENABLE_GEOMETRY_SHADER pgm.addShader( osg.Shader( osg.Shader.GEOMETRY, geomSource ) ) pgm.setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, 4 ) pgm.setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, GL_POINTS ) pgm.setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_LINE_STRIP ) #endif return pgm
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() ) fp = strstream() fp, "#version 420 compatibility\n", "\n", "layout(binding = 0) uniform atomic_uint acRed\n", "layout(binding = 0, offset = 4) uniform atomic_uint acGreen\n", "layout(binding = 2) uniform atomic_uint acBlue\n", "\n", "uniform float invNumPixel\n", "\n", "void main(void)\n", "\n", " float r = float(atomicCounterIncrement(acRed)) * invNumPixel\n", " float g = float(atomicCounterIncrement(acGreen)) * invNumPixel\n", " float b = float(atomicCounterIncrement(acBlue)) * invNumPixel\n", " gl_FragColor = vec4(r, g, b, 1.0)\n", "\n", "\n" fpShader = osg.Shader( osg.Shader.FRAGMENT, fp.str() ) program = osg.Program() program.addShader(vpShader) program.addShader(fpShader) return program
def main(argv): arguments = osg.ArgumentParser( argc, argv ) # Create the texture as both the output of compute shader and the input of a normal quad tex2D = osg.Texture2D() tex2D.setTextureSize( 512, 512 ) tex2D.setFilter( osg.Texture2D.MIN_FILTER, osg.Texture2D.LINEAR ) tex2D.setFilter( osg.Texture2D.MAG_FILTER, osg.Texture2D.LINEAR ) tex2D.setInternalFormat( GL_R32F ) tex2D.setSourceFormat( GL_RED ) tex2D.setSourceType( GL_FLOAT ) tex2D.bindToImageUnit( 0, osg.Texture.WRITE_ONLY ) # So we can use 'image2D' in the compute shader # The compute shader can't work with other kinds of shaders # It also requires the work group numbers. Setting them to 0 will disable the compute shader computeProg = osg.Program() computeProg.setComputeGroups( 512/16, 512/16, 1 ) computeProg.addShader( osg.Shader(osg.Shader.COMPUTE, computeSrc) ) # Create a node for outputting to the texture. # It is OK to have just an empty node here, but seems inbuilt uniforms like osg_FrameTime won't work then. # TODO: maybe we can have a custom drawable which also will implement sourceNode = osgDB: if (glMemoryBarrier) else readNodeFile("axes.osgt") if not sourceNode : sourceNode = osg.Node() sourceNode.setDataVariance( osg.Object.DYNAMIC ) sourceNode.getOrCreateStateSet().setAttributeAndModes( computeProg ) sourceNode.getOrCreateStateSet().addUniform( osg.Uniform("targetTex", (int)0) ) sourceNode.getOrCreateStateSet().setTextureAttributeAndModes( 0, tex2D ) # Display the texture on a quad. We will also be able to operate on the data if reading back to CPU side geom = osg.createTexturedQuadGeometry( osg.Vec3(), osg.Vec3(1.0,0.0,0.0), osg.Vec3(0.0,0.0,1.0) ) quad = osg.Geode() quad.addDrawable( geom ) quad.getOrCreateStateSet().setMode( GL_LIGHTING, osg.StateAttribute.OFF ) quad.getOrCreateStateSet().setTextureAttributeAndModes( 0, tex2D ) # Create the scene graph and start the viewer scene = osg.Group() scene.addChild( sourceNode ) scene.addChild( quad ) viewer = osgViewer.Viewer() viewer.addEventHandler( osgGA.StateSetManipulator(viewer.getCamera().getOrCreateStateSet()) ) viewer.addEventHandler( osgViewer.StatsHandler )() viewer.addEventHandler( osgViewer.WindowSizeHandler )() viewer.setSceneData( scene ) return viewer.run()
"\n" program = osg.Program() stateset.setAttribute(program) vertex_shader = osg.Shader(osg.Shader.VERTEX, vertexShaderSource) program.addShader(vertex_shader) #if 0 ################################# # fragment shader # char fragmentShaderSource[] = "void main(void) \n" " \n" " gl_FragColor = gl_Color \n" "\n" fragment_shader = osg.Shader(osg.Shader.FRAGMENT, fragmentShaderSource) program.addShader(fragment_shader) #endif return viewer.run() if __name__ == "__main__": main(sys.argv)
"\n" "void main(void) \n" " \n" " vec4 finalColor = texture2D( baseTexture, texcoord) \n" " vec4 color = finalColor * intensity\n" " color.w = finalColor.w\n" " color.x *= red_intensity\n" " gl_FragColor = color\n" "\n" pgm = osg.Program() pgm.setName( "osgshader2 demo" ) pgm.addShader( osg.Shader( osg.Shader.VERTEX, vertSource ) ) pgm.addShader( osg.Shader( osg.Shader.FRAGMENT, fragSource ) ) pgm.addShader( osg.Shader( osg.Shader.GEOMETRY, geomSource ) ) pgm.setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, 8 ) pgm.setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, GL_LINES ) pgm.setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP) return pgm void ForestTechniqueManager.CollectTreePositions(Cell* cell, std.vector< osg.Vec3 > positions) needGroup = not (cell._cells.empty()) needTrees = not (cell._trees.empty()) if needTrees : for(TreeList.iterator itr=cell._trees.begin()
# Attach shader, glFragData is used to create data for multiple render targets if useHDR : static char *shaderSource = "uniform int width" "void main(void)\n" "\n" " gl_FragData[0] = vec4(-1e-12,0,0,1)\n" " gl_FragData[1] = vec4(0,1e-12,0,1)\n" " gl_FragData[2] = vec4(0,0,1e-12,1)\n" " gl_FragData[3] = vec4(0,0,1e-12,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 ) else: static char *shaderSource = "uniform int width" "void main(void)\n" "\n" " gl_FragData[0] = vec4(1,0,0,1)\n" " gl_FragData[1] = vec4(0,1,0,1)\n" " gl_FragData[2] = vec4(0,0,1,1)\n" " gl_FragData[3] = vec4(0,0,1,1)\n" "\n"
static char *shaderSourceTexture2D = "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
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 ) # the simple Microshader (its source appears earlier in this file) ss = ModelInstance() program = osg.Program() program.setName( "microshader" ) _programList.push_back( program ) program.addShader( osg.Shader( osg.Shader.VERTEX, microshaderVertSource ) ) program.addShader( osg.Shader( osg.Shader.FRAGMENT, microshaderFragSource ) ) ss.setAttributeAndModes( program, osg.StateAttribute.ON ) # the "blocky" shader, a simple animation test ss = ModelInstance() BlockyProgram = osg.Program() BlockyProgram.setName( "blocky" ) _programList.push_back( BlockyProgram ) BlockyVertObj = osg.Shader( osg.Shader.VERTEX ) BlockyFragObj = osg.Shader( osg.Shader.FRAGMENT ) BlockyProgram.addShader( BlockyFragObj ) BlockyProgram.addShader( BlockyVertObj ) ss.setAttributeAndModes(BlockyProgram, osg.StateAttribute.ON) # the "eroded" shader, uses a noise texture to discard fragments
def SetVirtualProgramShader(virtualProgram, shader_semantics, shader_type, shader_name, shader_source): shader = osg.Shader( shader_type ) shader.setName( shader_name ) shader.setShaderSource( shader_source ) virtualProgram.setShader( shader_semantics, shader )
i = 0 for (unsigned int x=0 x < O*K x++) indices[i++] = base2+x indices[i++] = base+x indices[i++] = base2+O*K meshGeom.addPrimitiveSet(osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLE_STRIP, i, indices)) else: base = (y/2) * (O*K+O*K+1) + O*K base2 = (y/2) * (O*K+O*K+1) + O*K + O*K+1 i = 0 for (unsigned int x=0 x < O*K x++) indices[i++] = base+x indices[i++] = base2+x indices[i++] = base+O*K meshGeom.addPrimitiveSet(osg.DrawElementsUInt(osg.PrimitiveSet.TRIANGLE_STRIP, i, indices)) delete[] indices # create vertex and fragment shader program = osg.Program() program.setName( "mesh" ) program.addBindAttribLocation("xypos", 6) program.addShader( osg.Shader( osg.Shader.VERTEX, VertexShader ) ) program.addShader( osg.Shader( osg.Shader.FRAGMENT, DepthPeeling.PeelingShader ) ) program.addShader( osg.Shader( osg.Shader.FRAGMENT, FragmentShader ) ) # create a 1D texture for color lookups colorimg = osg.Image() colorimg.allocateImage(5, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE) data = colorimg.data() *data++ = 0 *data++ = 0 *data++ = 255 *data++ = 0 # fully transparent blue *data++ = 0 *data++ = 255 *data++ = 255 *data++ = 255 # turquoise *data++ = 0 *data++ = 255 *data++ = 0 *data++ = 255 # green *data++ = 255 *data++ = 255 *data++ = 0 *data++ = 255 # yellow *data++ = 255 *data++ = 0 *data++ = 0 *data++ = 255 # red colortex = osg.Texture1D(colorimg) colortex.setFilter(osg.Texture.MIN_FILTER, osg.Texture.LINEAR) colortex.setFilter(osg.Texture.MAG_FILTER, osg.Texture.LINEAR)
pat = osg.PositionAttitudeTransform() pat.setPosition(position) pat.addChild(node) position.x() += spacing stateset = pat.getOrCreateStateSet() stateset.setMode(GL_BLEND, osg.StateAttribute.ON) sa = osg.ShaderAttribute() sa.setType(osg.StateAttribute.Type(10001)) stateset.setAttribute(sa) fragment_shader = osg.Shader(osg.Shader.FRAGMENT) fragment_shader.addCodeInjection(0.9,"gl_FragColor.a = gl_FragColor.a*0.5\n") sa.addShader(fragment_shader) group.addChild(pat) # resuse the first ShaderAttribute's type and ShaderComponent, just use uniform pat = osg.PositionAttitudeTransform() pat.setPosition(position) pat.addChild(node) position.x() += spacing stateset = pat.getOrCreateStateSet() sa = osg.ShaderAttribute(*sa1)
def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(argv) # set up the usage document, in case we need to print out how to use this program. arguments.getApplicationUsage().setApplicationName(arguments.getApplicationName()) arguments.getApplicationUsage().setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds..") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...") arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") arguments.getApplicationUsage().addCommandLineOption("--sprites","Point sprites.") arguments.getApplicationUsage().addCommandLineOption("--points","Sets the polygon mode to GL_POINT for front and back faces.") # construct the viewer. viewer = osgViewer.Viewer() shader = False while arguments.read("--shader") : shader = True # if user request help write it out to cout. if arguments.read("-h") or arguments.read("--help") : arguments.getApplicationUsage().write(std.cout) return 1 usePointSprites = False while arguments.read("--sprites") : usePointSprites = True forcePointMode = False while arguments.read("--points") : forcePointMode = True if arguments.argc()<=1 : arguments.getApplicationUsage().write(std.cout,osg.ApplicationUsage.COMMAND_LINE_OPTION) return 1 # read the scene from the list of file specified commandline args. loadedModel = osgDB.readNodeFiles(arguments) # if no model has been successfully loaded report failure. if not loadedModel : print arguments.getApplicationName(), ": No data loaded" return 1 # optimize the scene graph, remove redundant nodes and state etc. optimizer = osgUtil.Optimizer() optimizer.optimize(loadedModel) # set the scene to render viewer.setSceneData(loadedModel) stateset = loadedModel.getOrCreateStateSet() if usePointSprites : #/ Setup cool blending fn = osg.BlendFunc() stateset.setAttributeAndModes(fn, osg.StateAttribute.ON) #/ Setup the point sprites sprite = osg.PointSprite() stateset.setTextureAttributeAndModes(0, sprite, osg.StateAttribute.ON) #/ The texture for the sprites tex = osg.Texture2D() tex.setImage(osgDB.readImageFile("Images/particle.rgb")) stateset.setTextureAttributeAndModes(0, tex, osg.StateAttribute.ON) if forcePointMode : #/ Set polygon mode to GL_POINT pm = osg.PolygonMode( osg.PolygonMode.FRONT_AND_BACK, osg.PolygonMode.POINT ) stateset.setAttributeAndModes( pm, osg.StateAttribute.ON | osg.StateAttribute.OVERRIDE) # register the handler for modifying the point size viewer.addEventHandler(KeyboardEventHandler(viewer.getCamera().getOrCreateStateSet())) if shader : stateset = loadedModel.getOrCreateStateSet() #################################/ # vertex shader using just Vec4 coefficients char vertexShaderSource[] = "void main(void) \n" " \n" "\n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex\n" "\n" program = osg.Program() stateset.setAttribute(program) vertex_shader = osg.Shader(osg.Shader.VERTEX, vertexShaderSource) program.addShader(vertex_shader) #if 0 ################################# # fragment shader # char fragmentShaderSource[] = "void main(void) \n" " \n" " gl_FragColor = gl_Color \n" "\n"
def CreateSimpleHierarchy(node): if not node : return NULL r = node.getBound().radius() # diameter root = osg.Group() group = osg.Group() # Create four matrices for translated instances of the cow transform0 = osg.MatrixTransform( ) transform0.setMatrix( osg.Matrix.translate( 0,0,r ) ) transform1 = osg.MatrixTransform( ) transform1.setMatrix( osg.Matrix.translate( 0,0,0 ) ) transform2 = osg.MatrixTransform( ) transform2.setMatrix( osg.Matrix.translate( -r,0,-r ) ) transform3 = osg.MatrixTransform( ) transform3.setMatrix( osg.Matrix.translate( r,0,-r ) ) root.addChild( transform0 ) root.addChild( group ) group.addChild( transform1 ) group.addChild( transform2 ) group.addChild( transform3 ) transform0.addChild( node ) transform1.addChild( node ) transform2.addChild( node ) transform3.addChild( node ) # At the scene root apply standard program if 1 : program = osg.Program() main = osg.Shader( osg.Shader.FRAGMENT ) main.setShaderSource( "uniform sampler2D base \n" "void main(void) \n" "\n" " gl_FragColor = gl_Color * texture2DProj( base, gl_TexCoord[0] )\n" " gl_FragColor *= vec4( 1.0, 1.0, 1.0, 0.5 ) \n" "\n" ) program.addShader( main ) main.setName( "White" ) root.getOrCreateStateSet( ).setAttributeAndModes( program ) # Now override root program with default VirtualProgram for three bottom cows if 1 : virtualProgram = VirtualProgram( ) # Create main shader which declares and calls ColorFilter function main = osg.Shader( osg.Shader.FRAGMENT ) main.setShaderSource( "vec4 ColorFilter( in vec4 color ) \n" "uniform sampler2D base \n" "void main(void) \n" " \n" " gl_FragColor = gl_Color * texture2DProj( base, gl_TexCoord[0] ) \n" " gl_FragColor = ColorFilter( gl_FragColor ) \n" "\n" ) virtualProgram.setShader( "main", main ) main.setName( "Virtual Main" ) # Create filter shader which implements greem ColorFilter function colorFilter = osg.Shader( osg.Shader.FRAGMENT ) colorFilter.setShaderSource( "vec4 ColorFilter( in vec4 color ) \n" " \n" " return color * vec4( 0.0, 1.0, 0.0, 1.0 ) \n" "\n" ) colorFilter.setName( "Virtual Green" ) virtualProgram.setShader( "ColorFilter", colorFilter ) group.getOrCreateStateSet( ).setAttributeAndModes( virtualProgram ) # Create "incomplete" VirtualProgram overriding ColorFilter function # Lower left cow drawn will be red if 1 : virtualProgram = VirtualProgram() redFilter = osg.Shader( osg.Shader.FRAGMENT ) redFilter.setShaderSource( "vec4 ColorFilter( in vec4 color ) \n" " \n" " return color * vec4( 1, 0, 0, 1 ) \n" "\n" ) virtualProgram.setShader( "ColorFilter", redFilter ) redFilter.setName( "Virtual Red" ) transform2.getOrCreateStateSet( ).setAttribute( virtualProgram ) # Create "incomplete" VirtualProgram overriding ColorFilter function # Lower right cow will be drawn with grid pattern on yellow background if 1 : virtualProgram = VirtualProgram() gridFilter = osg.Shader( osg.Shader.FRAGMENT ) gridFilter.setShaderSource( "vec4 ColorFilter( in vec4 color ) \n" " \n" " vec2 grid = clamp( mod( gl_FragCoord.xy, 16.0 ), 0.0, 1.0 ) \n" " return color * vec4( grid, 0.0, 1.0 ) \n" "\n" ) virtualProgram.setShader( "ColorFilter", gridFilter ) gridFilter.setName( "Virtual Grid" ) transform3.getOrCreateStateSet( ).setAttribute( virtualProgram ) return root
"vec4 pos = vec4( tC.s * 48., 0., tC.t * 48., 1. ) \n" # Compute a rotation angle from the instanceID and elapsed time. "float timeOffset = gl_InstanceID / (32. * 32.) \n" "float angle = ( osg_SimulationTime - timeOffset ) * 6.283 \n" "float sa = sin( angle ) \n" "float ca = cos( angle ) \n" # New orientation, rotate around z axis. "vec4 newX = vec4( ca, sa, 0., 0. ) \n" "vec4 newY = vec4( sa, ca, 0., 0. ) \n" "vec4 newZ = vec4( 0., 0., 1., 0. ) \n" "mat4 mV = mat4( newX, newY, newZ, pos ) \n" "gl_Position = ( gl_ModelViewProjectionMatrix * mV * gl_Vertex ) \n" " \n" vertexShader = osg.Shader() vertexShader.setType( osg.Shader.VERTEX ) vertexShader.setShaderSource( vertexSource ) program = osg.Program() 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 )
"uniform mat3 osg_NormalMatrix \n" "uniform vec3 ecLightDir \n" " \n" "in vec4 osg_Vertex \n" "in vec3 osg_Normal \n" "out vec4 color \n" " \n" "void main() \n" " \n" " vec3 ecNormal = normalize( osg_NormalMatrix * osg_Normal ) \n" " float diffuse = max( dot( ecLightDir, ecNormal ), 0. ) \n" " color = vec4( vec3( diffuse ), 1. ) \n" " \n" " gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex \n" " \n" vShader = osg.Shader( osg.Shader.VERTEX, vertexSource ) fragmentSource = "#version 140 \n" " \n" "in vec4 color \n" "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 = osg.StateSet() 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 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
_operation = Operation() 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
# 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() program.setName( "Trivial Fragment + Fixed Vertex Program" ) transformCenterTop.getOrCreateStateSet( ).setAttributeAndModes ( program, osg.StateAttribute.ON | osg.StateAttribute.OVERRIDE ) shader = osg.Shader( osg.Shader.FRAGMENT ) shader.setName( "Trivial Fragment Shader" ) shader.setShaderSource( "uniform sampler2D baseTexture \n" "void main(void) \n" " \n" " gl_FragColor = gl_Color * texture2D( baseTexture,gl_TexCoord[0].xy)\n" " \n" ) program.addShader( shader ) return transformCenterMiddle ######################################## # Shders not used in the example but left for fun if anyone wants to play