| 1 | #include <math.h> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Geode> |
|---|
| 4 | #include <osg/Geometry> |
|---|
| 5 | #include <osg/Texture2D> |
|---|
| 6 | #include <osg/TexEnv> |
|---|
| 7 | #include <osg/Depth> |
|---|
| 8 | #include <osg/StateSet> |
|---|
| 9 | |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | |
|---|
| 12 | #ifdef _MSC_VER |
|---|
| 13 | #pragma warning( disable : 4244 ) |
|---|
| 14 | #pragma warning( disable : 4305 ) |
|---|
| 15 | #endif |
|---|
| 16 | |
|---|
| 17 | using namespace osg; |
|---|
| 18 | |
|---|
| 19 | Node *makeSky( void ) |
|---|
| 20 | { |
|---|
| 21 | int i, j; |
|---|
| 22 | float lev[] = { -5, -1.0, 1.0, 15.0, 30.0, 60.0, 90.0 }; |
|---|
| 23 | float cc[][4] = |
|---|
| 24 | { |
|---|
| 25 | { 0.0, 0.0, 0.15 }, |
|---|
| 26 | { 0.0, 0.0, 0.15 }, |
|---|
| 27 | { 0.4, 0.4, 0.7 }, |
|---|
| 28 | { 0.2, 0.2, 0.6 }, |
|---|
| 29 | { 0.1, 0.1, 0.6 }, |
|---|
| 30 | { 0.1, 0.1, 0.6 }, |
|---|
| 31 | { 0.1, 0.1, 0.6 }, |
|---|
| 32 | }; |
|---|
| 33 | float x, y, z; |
|---|
| 34 | float alpha, theta; |
|---|
| 35 | float radius = 20.0f; |
|---|
| 36 | int nlev = sizeof( lev )/sizeof(float); |
|---|
| 37 | |
|---|
| 38 | Geometry *geom = new Geometry; |
|---|
| 39 | |
|---|
| 40 | Vec3Array& coords = *(new Vec3Array(19*nlev)); |
|---|
| 41 | Vec4Array& colors = *(new Vec4Array(19*nlev)); |
|---|
| 42 | Vec2Array& tcoords = *(new Vec2Array(19*nlev)); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | int ci = 0; |
|---|
| 46 | |
|---|
| 47 | for( i = 0; i < nlev; i++ ) |
|---|
| 48 | { |
|---|
| 49 | for( j = 0; j <= 18; j++ ) |
|---|
| 50 | { |
|---|
| 51 | alpha = osg::DegreesToRadians(lev[i]); |
|---|
| 52 | theta = osg::DegreesToRadians((float)(j*20)); |
|---|
| 53 | |
|---|
| 54 | x = radius * cosf( alpha ) * cosf( theta ); |
|---|
| 55 | y = radius * cosf( alpha ) * -sinf( theta ); |
|---|
| 56 | z = radius * sinf( alpha ); |
|---|
| 57 | |
|---|
| 58 | coords[ci][0] = x; |
|---|
| 59 | coords[ci][1] = y; |
|---|
| 60 | coords[ci][2] = z; |
|---|
| 61 | |
|---|
| 62 | colors[ci][0] = cc[i][0]; |
|---|
| 63 | colors[ci][1] = cc[i][1]; |
|---|
| 64 | colors[ci][2] = cc[i][2]; |
|---|
| 65 | colors[ci][3] = 1.0; |
|---|
| 66 | |
|---|
| 67 | tcoords[ci][0] = (float)j/18.0; |
|---|
| 68 | tcoords[ci][1] = (float)i/(float)(nlev-1); |
|---|
| 69 | |
|---|
| 70 | ci++; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | for( i = 0; i < nlev-1; i++ ) |
|---|
| 77 | { |
|---|
| 78 | DrawElementsUShort* drawElements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP); |
|---|
| 79 | drawElements->reserve(38); |
|---|
| 80 | |
|---|
| 81 | for( j = 0; j <= 18; j++ ) |
|---|
| 82 | { |
|---|
| 83 | drawElements->push_back((i+1)*19+j); |
|---|
| 84 | drawElements->push_back((i+0)*19+j); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | geom->addPrimitiveSet(drawElements); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | geom->setVertexArray( &coords ); |
|---|
| 91 | geom->setTexCoordArray( 0, &tcoords ); |
|---|
| 92 | |
|---|
| 93 | geom->setColorArray( &colors ); |
|---|
| 94 | geom->setColorBinding( Geometry::BIND_PER_VERTEX ); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | Texture2D *tex = new Texture2D; |
|---|
| 98 | tex->setImage(osgDB::readImageFile("Images/white.rgb")); |
|---|
| 99 | |
|---|
| 100 | StateSet *dstate = new StateSet; |
|---|
| 101 | |
|---|
| 102 | dstate->setTextureAttributeAndModes(0, tex, StateAttribute::OFF ); |
|---|
| 103 | dstate->setTextureAttribute(0, new TexEnv ); |
|---|
| 104 | dstate->setMode( GL_LIGHTING, StateAttribute::OFF ); |
|---|
| 105 | dstate->setMode( GL_CULL_FACE, StateAttribute::ON ); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | osg::Depth* depth = new osg::Depth; |
|---|
| 110 | depth->setFunction(osg::Depth::ALWAYS); |
|---|
| 111 | depth->setRange(1.0,1.0); |
|---|
| 112 | dstate->setAttributeAndModes(depth,StateAttribute::ON ); |
|---|
| 113 | |
|---|
| 114 | dstate->setRenderBinDetails(-2,"RenderBin"); |
|---|
| 115 | |
|---|
| 116 | geom->setStateSet( dstate ); |
|---|
| 117 | |
|---|
| 118 | Geode *geode = new Geode; |
|---|
| 119 | geode->addDrawable( geom ); |
|---|
| 120 | |
|---|
| 121 | geode->setName( "Sky" ); |
|---|
| 122 | |
|---|
| 123 | return geode; |
|---|
| 124 | } |
|---|