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

Revision 6941, 4.0 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/StateSet>
26
27#include <osgDB/ReadFile>
28
29#include "terrain_coords.h"
30#include "terrain_texcoords.h"
31
32using namespace osg;
33
34void getDatabaseCenterRadius( float dbcenter[3], float *dbradius )
35{
36    int i;
37    double n=0.0;
38    double center[3] = { 0.0f, 0.0f, 0.0f };
39    float  cnt;
40
41    cnt = 39 * 38;
42    for( i = 0; i < cnt; i++ )
43    {
44        center[0] += (double)vertex[i][0];
45        center[1] += (double)vertex[i][1];
46        center[2] += (double)vertex[i][2];
47
48        n = n + 1.0;
49    }
50
51    center[0] /= n;
52    center[1] /= n;
53    center[2] /= n;
54
55    float r = 0.0;
56
57    //    for( i = 0; i < sizeof( vertex ) / (sizeof( float[3] )); i++ )
58    for( i = 0; i < cnt; i++ )
59    {
60        double d = sqrt(
61            (((double)vertex[i][0] - center[0]) * ((double)vertex[i][0] - center[0])) +
62            (((double)vertex[i][1] - center[1]) * ((double)vertex[i][1] - center[1])) +
63            (((double)vertex[i][2] - center[2]) * ((double)vertex[i][2] - center[2]))  );
64
65        if( d > (double)r ) r = (float)d;
66
67    }
68
69    *dbradius = r;
70    dbcenter[0] = (float)center[0];
71    dbcenter[1] = (float)center[1];
72    dbcenter[2] = (float)center[2];
73
74    int index = 19 * 39 + 19;
75    dbcenter[0] = vertex[index][0] - 0.15;
76    dbcenter[1] = vertex[index][1];
77    dbcenter[2] = vertex[index][2] + 0.35;
78
79}
80
81
82Node *makeTerrain( void )
83{
84    int m, n;
85    int  i, j;
86    float dbcenter[3];
87    float dbradius;
88
89    getDatabaseCenterRadius( dbcenter, &dbradius );
90
91    m = (sizeof( vertex ) /(sizeof( float[3])))/39;
92    n = 39;
93
94    Vec3Array& v    = *(new Vec3Array(m*n));
95    Vec2Array& t    = *(new Vec2Array(m*n));
96    Vec4Array& col  = *(new Vec4Array(1));
97   
98    col[0][0] = col[0][1] = col[0][2] = col[0][3] = 1.0f;
99
100    for( i = 0; i < m * n; i++ )
101    {
102        v[i][0] = vertex[i][0] - dbcenter[0];
103        v[i][1] = vertex[i][1] - dbcenter[1];
104        v[i][2] = vertex[i][2];
105
106        t[i][0] = texcoord[i][0] + 0.025;
107        t[i][1] = texcoord[i][1];
108    }
109
110    Geometry *geom = new Geometry;
111
112    geom->setVertexArray( &v );
113    geom->setTexCoordArray( 0, &t );
114
115    geom->setColorArray( &col );
116    geom->setColorBinding( Geometry::BIND_OVERALL );
117
118    for( i = 0; i < m-2; i++ )
119    {
120        DrawElementsUShort* elements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
121        elements->reserve(39*2);
122        for( j = 0; j < n; j++ )
123        {
124            elements->push_back((i+0)*n+j);
125            elements->push_back((i+1)*n+j);
126        }
127        geom->addPrimitiveSet(elements);
128    }
129
130
131    Texture2D *tex = new Texture2D;
132
133    tex->setImage(osgDB::readImageFile("Images/lz.rgb"));
134
135    StateSet *dstate = new StateSet;
136    dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
137    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON );
138    dstate->setTextureAttribute(0, new TexEnv );
139
140    geom->setStateSet( dstate );
141
142    Geode *geode = new Geode;
143    geode->addDrawable( geom );
144
145    return geode;
146}
Note: See TracBrowser for help on using the browser.