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

Revision 1697, 3.0 kB (checked in by robert, 11 years ago)

Ported osgGLUT based src/Demos across to being osgProducer based, and placed
them in the new examples/ directory.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// #include <math.h>
2
3#include <osg/Geode>
4#include <osg/Geometry>
5#include <osg/Texture2D>
6#include <osg/TexEnv>
7#include <osg/StateSet>
8
9#include <osgDB/ReadFile>
10
11#include "terrain_coords.h"
12#include "terrain_texcoords.h"
13
14using namespace osg;
15
16void getDatabaseCenterRadius( float dbcenter[3], float *dbradius )
17{
18    int i;
19    double n=0.0;
20    double center[3] = { 0.0f, 0.0f, 0.0f };
21    float  cnt;
22
23    cnt = 39 * 38;
24    for( i = 0; i < cnt; i++ )
25    {
26        center[0] += (double)vertex[i][0];
27        center[1] += (double)vertex[i][1];
28        center[2] += (double)vertex[i][2];
29
30        n = n + 1.0;
31    }
32
33    center[0] /= n;
34    center[1] /= n;
35    center[2] /= n;
36
37    float r = 0.0;
38
39    //    for( i = 0; i < sizeof( vertex ) / (sizeof( float[3] )); i++ )
40    for( i = 0; i < cnt; i++ )
41    {
42        double d = sqrt(
43            (((double)vertex[i][0] - center[0]) * ((double)vertex[i][0] - center[0])) +
44            (((double)vertex[i][1] - center[1]) * ((double)vertex[i][1] - center[1])) +
45            (((double)vertex[i][2] - center[2]) * ((double)vertex[i][2] - center[2]))  );
46
47        if( d > (double)r ) r = (float)d;
48
49    }
50
51    *dbradius = r;
52    dbcenter[0] = (float)center[0];
53    dbcenter[1] = (float)center[1];
54    dbcenter[2] = (float)center[2];
55
56    int index = 19 * 39 + 19;
57    dbcenter[0] = vertex[index][0] - 0.15;
58    dbcenter[1] = vertex[index][1];
59    dbcenter[2] = vertex[index][2] + 0.35;
60
61}
62
63
64Node *makeTerrain( void )
65{
66    int m, n;
67    int  i, j;
68    float dbcenter[3];
69    float dbradius;
70
71    getDatabaseCenterRadius( dbcenter, &dbradius );
72
73    m = (sizeof( vertex ) /(sizeof( float[3])))/39;
74    n = 39;
75
76    Vec3Array& v    = *(new Vec3Array(m*n));
77    Vec2Array& t    = *(new Vec2Array(m*n));
78    Vec4Array& col  = *(new Vec4Array(1));
79   
80    col[0][0] = col[0][1] = col[0][2] = col[0][3] = 1.0f;
81
82    for( i = 0; i < m * n; i++ )
83    {
84        v[i][0] = vertex[i][0] - dbcenter[0];
85        v[i][1] = vertex[i][1] - dbcenter[1];
86        v[i][2] = vertex[i][2];
87
88        t[i][0] = texcoord[i][0] + 0.025;
89        t[i][1] = texcoord[i][1];
90    }
91
92    Geometry *geom = new Geometry;
93
94    geom->setVertexArray( &v );
95    geom->setTexCoordArray( 0, &t );
96
97    geom->setColorArray( &col );
98    geom->setColorBinding( Geometry::BIND_OVERALL );
99
100    for( i = 0; i < m-2; i++ )
101    {
102        DrawElementsUShort* elements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
103        elements->reserve(39*2);
104        for( j = 0; j < n; j++ )
105        {
106            elements->push_back((i+0)*n+j);
107            elements->push_back((i+1)*n+j);
108        }
109        geom->addPrimitiveSet(elements);
110    }
111
112
113    Texture2D *tex = new Texture2D;
114
115    tex->setImage(osgDB::readImageFile("Images/lz.rgb"));
116
117    StateSet *dstate = new StateSet;
118    dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
119    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON );
120    dstate->setTextureAttribute(0, new TexEnv );
121
122    geom->setStateSet( dstate );
123
124    Geode *geode = new Geode;
125    geode->addDrawable( geom );
126
127    return geode;
128}
Note: See TracBrowser for help on using the browser.