#!/usr/bin/env python from PyOSG import * from OpenGL.GL import * from math import sin, cos import random RAND_MAX = 1.0 rand = random.random def makeGalaxy(nvertices): geode = osg.Geode() galaxy = osg.Geometry() vertices = osg.Vec3Array() colors = osg.Vec4Array() ini = osg.Vec4(1,1,0,1) fin = osg.Vec4(0,0,1,1) # /** Formula for the two spirals */ for i in range(nvertices/2): val = i*2/float(nvertices) * 2 * 3.14159265359 modx1 = rand() / RAND_MAX*2 mody1 = rand() / RAND_MAX*2 modx2 = rand() / RAND_MAX*2 mody2 = rand() / RAND_MAX*2 modz1 = ((rand()-RAND_MAX/2) / RAND_MAX)*3/(val+1) modz2 = ((rand()-RAND_MAX/2) / RAND_MAX)*3/(val+1) vertices.push_back(osg.Vec3(cos(val)*val+modx1, sin(val)*val+mody1, modz1)) vertices.push_back(osg.Vec3(-cos(val)*val+modx2, -sin(val)*val+mody2, modz2)) colors.push_back(ini+(fin-ini)*(i*2.0/nvertices)) colors.push_back(ini+(fin-ini)*(i*2.0/nvertices)) galaxy.setVertexArray(vertices) galaxy.setColorArray(colors) galaxy.setColorBinding(osg.Geometry.BIND_PER_VERTEX) galaxy.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POINTS, 0, nvertices)) geode.addDrawable(galaxy) return geode def makeStateSet(size): set = osg.StateSet() # Setup cool blending set.setMode(GL_BLEND, osg.StateAttribute.ON) fn = osg.BlendFunc() fn.setFunction(osg.BlendFunc.SRC_ALPHA, osg.BlendFunc.DST_ALPHA) set.setAttributeAndModes(fn, osg.StateAttribute.ON) # Setup the point sprites sprite = osg.PointSprite() set.setTextureAttributeAndModes(0, sprite, osg.StateAttribute.ON) # Give some size to the points to be able to see the sprite point = osg.Point() point.setSize(size) set.setAttribute(point) # Disable depth test to avoid sort problems and Lighting set.setMode(GL_DEPTH_TEST, osg.StateAttribute.OFF) set.setMode(GL_LIGHTING, osg.StateAttribute.OFF) # The texture for the sprites tex = osg.Texture2D() tex.setImage(osgDB.readImageFile("Images/particle.rgb")) set.setTextureAttributeAndModes(0, tex, osg.StateAttribute.ON) return set def main(): viewer = osgProducer.Viewer() # Make the galaxy of points node = makeGalaxy(5000) node.setStateSet(makeStateSet(10.0)) viewer.setUpViewer(osgProducer.Viewer.STANDARD_SETTINGS) viewer.setSceneData(node) viewer.realize() while not viewer.done(): viewer.sync() viewer.update() viewer.frame() return if __name__ == "__main__": main()