| 33 | | |
| 34 | | |
| 35 | | |
| | 35 | |
| | 36 | /** create quad at specified position. */ |
| | 37 | osg::Drawable* createSquare(const osg::Vec3& corner,const osg::Vec3& width,const osg::Vec3& height, osg::Image* image=NULL) |
| | 38 | { |
| | 39 | // set up the Geometry. |
| | 40 | osg::Geometry* geom = new osg::Geometry; |
| | 41 | |
| | 42 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
| | 43 | (*coords)[0] = corner; |
| | 44 | (*coords)[1] = corner+width; |
| | 45 | (*coords)[2] = corner+width+height; |
| | 46 | (*coords)[3] = corner+height; |
| | 47 | |
| | 48 | |
| | 49 | geom->setVertexArray(coords); |
| | 50 | |
| | 51 | osg::Vec3Array* norms = new osg::Vec3Array(1); |
| | 52 | (*norms)[0] = width^height; |
| | 53 | (*norms)[0].normalize(); |
| | 54 | |
| | 55 | geom->setNormalArray(norms); |
| | 56 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
| | 57 | |
| | 58 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
| | 59 | (*tcoords)[0].set(0.0f,0.0f); |
| | 60 | (*tcoords)[1].set(1.0f,0.0f); |
| | 61 | (*tcoords)[2].set(1.0f,1.0f); |
| | 62 | (*tcoords)[3].set(0.0f,1.0f); |
| | 63 | geom->setTexCoordArray(0,tcoords); |
| | 64 | |
| | 65 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
| | 66 | (*colours)[0].set(1.0f,1.0f,1.0f,1.0f); |
| | 67 | geom->setColorArray(colours); |
| | 68 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
| | 69 | |
| | 70 | |
| | 71 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
| | 72 | |
| | 73 | if (image) |
| | 74 | { |
| | 75 | osg::StateSet* stateset = new osg::StateSet; |
| | 76 | osg::Texture2D* texture = new osg::Texture2D; |
| | 77 | texture->setImage(image); |
| | 78 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
| | 79 | stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
| | 80 | stateset->setMode(GL_BLEND, osg::StateAttribute::ON); |
| | 81 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
| | 82 | geom->setStateSet(stateset); |
| | 83 | } |
| | 84 | |
| | 85 | return geom; |
| | 86 | } |
| | 87 | |
| | 88 | osg::Image* createBillboardImage(const osg::Vec4& centerColour, unsigned int size, float power) |
| | 89 | { |
| | 90 | osg::Vec4 backgroundColour = centerColour; |
| | 91 | backgroundColour[3] = 0.0f; |
| | 92 | |
| | 93 | osg::Image* image = new osg::Image; |
| | 94 | image->allocateImage(size,size,1, |
| | 95 | GL_RGBA,GL_UNSIGNED_BYTE); |
| | 96 | |
| | 97 | |
| | 98 | float mid = (float(size)-1)*0.5f; |
| | 99 | float div = 2.0f/float(size); |
| | 100 | for(unsigned int r=0;r<size;++r) |
| | 101 | { |
| | 102 | unsigned char* ptr = image->data(0,r,0); |
| | 103 | for(unsigned int c=0;c<size;++c) |
| | 104 | { |
| | 105 | float dx = (float(c) - mid)*div; |
| | 106 | float dy = (float(r) - mid)*div; |
| | 107 | float r = powf(1.0f-sqrtf(dx*dx+dy*dy),power); |
| | 108 | if (r<0.0f) r=0.0f; |
| | 109 | osg::Vec4 color = centerColour*r+backgroundColour*(1.0f-r); |
| | 110 | // color.set(1.0f,1.0f,1.0f,0.5f); |
| | 111 | *ptr++ = (unsigned char)((color[0])*255.0f); |
| | 112 | *ptr++ = (unsigned char)((color[1])*255.0f); |
| | 113 | *ptr++ = (unsigned char)((color[2])*255.0f); |
| | 114 | *ptr++ = (unsigned char)((color[3])*255.0f); |
| | 115 | } |
| | 116 | } |
| | 117 | return image; |
| | 118 | |
| | 119 | //return osgDB::readImageFile("spot.dds"); |
| | 120 | } |
| 253 | | |
| | 338 | |
| | 339 | osg::Billboard* sunBillboard = new osg::Billboard(); |
| | 340 | sunBillboard->setMode(osg::Billboard::POINT_ROT_EYE); |
| | 341 | sunBillboard->addDrawable( |
| | 342 | createSquare(osg::Vec3(-5.0f,0.0f,-5.0f),osg::Vec3(10.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,10.0f),createBillboardImage( osg::Vec4( 1.0, 1.0, 0, 1.0f), 64, 1.0) ), |
| | 343 | osg::Vec3(0.0f,0.0f,0.0f)); |
| | 344 | |
| | 345 | sunLight->addChild( sunBillboard ); |
| | 346 | |
| | 347 | |