1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | |
---|
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 | |
---|
32 | using namespace osg; |
---|
33 | |
---|
34 | void 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 | |
---|
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 | |
---|
82 | Node *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 | } |
---|