-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain.h
141 lines (106 loc) · 4.56 KB
/
terrain.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
osg::ref_ptr<osg::Group> createTerrain(const std::string filename)
{
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(osgDB::readImageFile(filename));
texture->setDataVariance(osg::Object::DYNAMIC);
// Create a new StateSet with default settings:
osg::StateSet* stateOne = new osg::StateSet();
stateOne->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
// Create an object to store geometry in.
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
// Create an array of four vertices.
osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
geom->setVertexArray( v.get() );
float halfWidth = 100.0;
v->push_back( osg::Vec3( -halfWidth, -halfWidth, 0.f) );
v->push_back( osg::Vec3( -halfWidth, halfWidth, 0.f) );
v->push_back( osg::Vec3( halfWidth, halfWidth, 0.f) );
v->push_back( osg::Vec3( halfWidth, -halfWidth, 0.f) );
osg::Vec2Array* texcoords = new osg::Vec2Array(4);
(*texcoords)[0].set(0.00f,0.0f); // tex coord for vertex 0
(*texcoords)[1].set(0.00f,1.0f); // tex coord for vertex 1
(*texcoords)[2].set(1.00f,1.0f); // ""
(*texcoords)[3].set(1.00f,0.0f); // ""
geom->setTexCoordArray(0,texcoords);
// Create an array for the single normal.
osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
geom->setNormalArray( n.get() );
geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
n->push_back( osg::Vec3( 0.f, 0.f, 1.f ) );
// Draw a four-vertex quad from the stored data.
geom->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
// Add the Geometry (Drawable) to a Geode and
// return the Geode.
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable( geom.get() );
geode->setStateSet(stateOne);
// return geode;
osg::ref_ptr<osg::Group> grp = new osg::Group;
int i, j;
int dim = 11;
for(i=-dim; i<=dim; i++)
{
for(j=-dim; j<=dim; j++)
{
osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform;
osg::Vec3 newPos(2*i*halfWidth,2*j*halfWidth,0);
pat->setPosition(newPos);
pat->addChild(geode);
grp->addChild(pat);
}
}
return grp;
}
osg::ref_ptr<osg::Group> createCeiling(const std::string filename)
{
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(osgDB::readImageFile(filename));
texture->setDataVariance(osg::Object::DYNAMIC);
// Create a new StateSet with default settings:
osg::StateSet* stateOne = new osg::StateSet();
stateOne->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
// Create an object to store geometry in.
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
// Create an array of four vertices.
osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
geom->setVertexArray( v.get() );
float halfWidth = 100.0;
v->push_back( osg::Vec3( -halfWidth, -halfWidth, 500.0f) );
v->push_back( osg::Vec3( -halfWidth, halfWidth, 500.0f) );
v->push_back( osg::Vec3( halfWidth, halfWidth, 500.0f) );
v->push_back( osg::Vec3( halfWidth, -halfWidth, 500.0f) );
osg::Vec2Array* texcoords = new osg::Vec2Array(4);
(*texcoords)[0].set(0.00f,0.0f); // tex coord for vertex 0
(*texcoords)[1].set(0.00f,1.0f); // tex coord for vertex 1
(*texcoords)[2].set(1.00f,1.0f); // ""
(*texcoords)[3].set(1.00f,0.0f); // ""
geom->setTexCoordArray(0,texcoords);
// Create an array for the single normal.
osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
geom->setNormalArray( n.get() );
geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
n->push_back( osg::Vec3( 0.f, 0.f, 1.f ) );
// Draw a four-vertex quad from the stored data.
geom->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
// Add the Geometry (Drawable) to a Geode and
// return the Geode.
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable( geom.get() );
geode->setStateSet(stateOne);
// return geode;
osg::ref_ptr<osg::Group> grp = new osg::Group;
int i, j;
int dim = 10;
for(i=-dim; i<=dim; i++)
{
for(j=-dim; j<=dim; j++)
{
osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform;
osg::Vec3 newPos(2*i*halfWidth,2*j*halfWidth,0);
pat->setPosition(newPos);
pat->addChild(geode);
grp->addChild(pat);
}
}
return grp;
}