root/OpenSceneGraph/trunk/examples/osghangglide/sky.cpp @ 6941

Revision 6941, 3.9 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osghangglide.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <math.h>
20
21#include <osg/Geode>
22#include <osg/Geometry>
23#include <osg/Texture2D>
24#include <osg/TexEnv>
25#include <osg/Depth>
26#include <osg/StateSet>
27
28#include <osgDB/ReadFile>
29
30#ifdef _MSC_VER
31#pragma warning( disable : 4244 )
32#pragma warning( disable : 4305 )
33#endif
34
35using namespace osg;
36
37Node *makeSky( void )
38{
39    int i, j;
40    float lev[] = { -5, -1.0, 1.0, 15.0, 30.0, 60.0, 90.0  };
41    float cc[][4] =
42    {
43        { 0.0, 0.0, 0.15 },
44        { 0.0, 0.0, 0.15 },
45        { 0.4, 0.4, 0.7 },
46        { 0.2, 0.2, 0.6 },
47        { 0.1, 0.1, 0.6 },
48        { 0.1, 0.1, 0.6 },
49        { 0.1, 0.1, 0.6 },
50    };
51    float x, y, z;
52    float alpha, theta;
53    float radius = 20.0f;
54    int nlev = sizeof( lev )/sizeof(float);
55
56    Geometry *geom = new Geometry;
57
58    Vec3Array& coords = *(new Vec3Array(19*nlev));
59    Vec4Array& colors = *(new Vec4Array(19*nlev));
60    Vec2Array& tcoords = *(new Vec2Array(19*nlev));
61   
62   
63    int ci = 0;
64
65    for( i = 0; i < nlev; i++ )
66    {
67        for( j = 0; j <= 18; j++ )
68        {
69            alpha = osg::DegreesToRadians(lev[i]);
70            theta = osg::DegreesToRadians((float)(j*20));
71
72            x = radius * cosf( alpha ) * cosf( theta );
73            y = radius * cosf( alpha ) * -sinf( theta );
74            z = radius * sinf( alpha );
75
76            coords[ci][0] = x;
77            coords[ci][1] = y;
78            coords[ci][2] = z;
79
80            colors[ci][0] = cc[i][0];
81            colors[ci][1] = cc[i][1];
82            colors[ci][2] = cc[i][2];
83            colors[ci][3] = 1.0;
84
85            tcoords[ci][0] = (float)j/18.0;
86            tcoords[ci][1] = (float)i/(float)(nlev-1);
87
88            ci++;
89        }
90
91
92    }
93
94    for( i = 0; i < nlev-1; i++ )
95    {
96        DrawElementsUShort* drawElements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
97        drawElements->reserve(38);
98
99        for( j = 0; j <= 18; j++ )
100        {
101            drawElements->push_back((i+1)*19+j);
102            drawElements->push_back((i+0)*19+j);
103        }
104
105        geom->addPrimitiveSet(drawElements);
106    }
107   
108    geom->setVertexArray( &coords );
109    geom->setTexCoordArray( 0, &tcoords );
110
111    geom->setColorArray( &colors );
112    geom->setColorBinding( Geometry::BIND_PER_VERTEX );
113
114
115    Texture2D *tex = new Texture2D;
116    tex->setImage(osgDB::readImageFile("Images/white.rgb"));
117
118    StateSet *dstate = new StateSet;
119
120    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::OFF );
121    dstate->setTextureAttribute(0, new TexEnv );
122    dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
123    dstate->setMode( GL_CULL_FACE, StateAttribute::ON );
124   
125
126    // clear the depth to the far plane.
127    osg::Depth* depth = new osg::Depth;
128    depth->setFunction(osg::Depth::ALWAYS);
129    depth->setRange(1.0,1.0);   
130    dstate->setAttributeAndModes(depth,StateAttribute::ON );
131
132    dstate->setRenderBinDetails(-2,"RenderBin");
133
134    geom->setStateSet( dstate );
135
136    Geode *geode = new Geode;
137    geode->addDrawable( geom );
138
139    geode->setName( "Sky" );
140
141    return geode;
142}
Note: See TracBrowser for help on using the browser.