-
Notifications
You must be signed in to change notification settings - Fork 2
/
carrousel.cpp
391 lines (300 loc) · 12.2 KB
/
carrousel.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <cmath>
#include <iostream>
#include <osg/Texture2D>
#include <osg/TexGen>
#include <osg/TexEnv>
#include <osg/TexEnvCombine>
#include <osg/Material>
#include <osg/Matrix>
#include <osg/LightModel>
#include <osgDB/ReadFile>
#include "carrousel.h"
#include "util.h"
#include "animationcallbacks.h"
#include "userinputhandlers.h"
using namespace osg;
using namespace std;
using namespace amussementpark;
//################################################################################
CarrouselRoof::CarrouselRoof(DOUBLE rad, DOUBLE height, DOUBLE yPos):
rad(rad),height(height),yPos(yPos) {
initialize();
}
CarrouselRoof::CarrouselRoof(CarrouselRoof &o):
rad(o.rad),height(o.height),yPos(o.yPos) {
initialize();
}
void CarrouselRoof::initialize() {
setShape(new Cone(Vec3(0.0,0.0,yPos),rad,height));
}
//################################################################################
CarrouselBase::CarrouselBase(DOUBLE rad, DOUBLE height, DOUBLE yPos):
rad(rad),height(height),yPos(yPos) {
initialize();
}
CarrouselBase::CarrouselBase(CarrouselBase &o):
rad(o.rad),height(o.height),yPos(o.yPos) {
initialize();
}
void CarrouselBase::initialize() {
setShape(new Cylinder(Vec3(0.0f,0.0f, yPos),rad,height));
}
//################################################################################
//################################################################################
CarrouselUpperNode::CarrouselUpperNode(DOUBLE sizeFactor) {
// Parametros editables
DOUBLE roofRadius = 2.2 * sizeFactor;
DOUBLE roofHeight = 0.8 * sizeFactor;
// Parametros no editables;
DOUBLE startingHeight = (1.525 * sizeFactor);
DOUBLE defaultRoofHeight = startingHeight + (roofHeight / 4.0) + 0.2;
ref_ptr<ShapeDrawable> baseRoof = new ShapeDrawable(new Cylinder(Vec3(0, 0, startingHeight + 0.1),roofRadius, 0.2));
addDrawable(baseRoof);
ref_ptr<CarrouselRoof> roof = new CarrouselRoof(roofRadius, roofHeight, defaultRoofHeight);
roof->setColor(Vec4(1.0,1.0,1.0,1.0));
addDrawable(roof);
ref_ptr<ShapeDrawable> light1 = new ShapeDrawable(new Cylinder( Vec3(roofRadius / 2.0, 0, startingHeight - 0.025), 0.05, 0.05));
ref_ptr<ShapeDrawable> light2 = new ShapeDrawable(new Cylinder( Vec3( -(roofRadius / 2.0), 0, startingHeight - 0.025), 0.05, 0.05));
addDrawable(light1);
addDrawable(light2);
//Image* image = new Image(*TextureTable::getInstance().getTexture( string("carrouselRoof.jpg") ));
Image *image = osgDB::readImageFile( string("textures/carrouselRoof.jpg") );
if ( image )
{
ref_ptr<Texture2D> tex2d = new Texture2D( image );
getOrCreateStateSet()->setTextureAttributeAndModes( 0, tex2d);
}
}
//################################################################################
CarrouselLightNode::CarrouselLightNode(DOUBLE sizeFactor) {
DOUBLE startingHeight = (1.525 * sizeFactor);
DOUBLE roofRadius = 2.2 * sizeFactor;
DOUBLE deltaAngle = 360.0 / 20;
DOUBLE angle;
ref_ptr<Geode> geode = new Geode;
for(INT i = 0; i < 20; i++) {
DOUBLE x = (cos((inDegrees(angle))) * (roofRadius + 0.005));
DOUBLE y = (sin((inDegrees(angle))) * (roofRadius + 0.005));
ref_ptr<ShapeDrawable> lightSupport = new ShapeDrawable(new Box(Vec3(x,y,startingHeight+0.1), 0.05,0.07,0.07));
lightSupport->setColor(Vec4(255,255,255,1));
geode->addDrawable(lightSupport);
angle += deltaAngle;
lights.push_back(lightSupport);
}
addChild(geode);
setUpdateCallback(new AnimationCallBack);
currentCounter = 0;
currentIndex = 0;
}
void CarrouselLightNode::onKeyPress() {
}
void CarrouselLightNode::onCtrlKeyPress() {
}
void CarrouselLightNode::onMouseClick() {
}
void CarrouselLightNode::updateAnimation() {
if(currentCounter == 30) {
currentCounter = 0;
for(INT i = 0; i < lights.size(); i++) {
if((i % 2) == 0) {
if(currentIndex == 0) {
lights[i]->setColor(Vec4(0,0,0,1));
} else {
lights[i]->setColor(Vec4(0,255,0,1));
}
} else {
if(currentIndex == 1) {
lights[i]->setColor(Vec4(0,0,0,1));
} else {
lights[i]->setColor(Vec4(255,0,0,1));
}
}
}
currentIndex = (currentIndex + 1) % 2;
} else
currentCounter++;
}
//################################################################################
CarrouselMiddleNode::CarrouselMiddleNode(DOUBLE sizeFactor) {
// Parametros editables
DOUBLE baseRadius = 2.0 * sizeFactor;
DOUBLE axisBaseRadius = 0.4 * sizeFactor;
DOUBLE axisRadius = 0.25 * sizeFactor;
DOUBLE baseHeight = 0.075 * sizeFactor;
DOUBLE axisBaseHeight = 0.15 * sizeFactor;
DOUBLE axisHeight = 1.0 * sizeFactor;
// Parametros no editables
DOUBLE yPos = 0.15 * sizeFactor;
DOUBLE halfBaseHeight = (baseHeight / 2.0);
DOUBLE halfAxisBaseHeight = (axisBaseHeight / 2.0);
DOUBLE halfAxisHeight = (axisHeight / 2.0);
// Base
yPos += halfBaseHeight;
addDrawable(new CarrouselBase(baseRadius, baseHeight, yPos));
// Base del eje
yPos += halfBaseHeight + halfAxisBaseHeight;
addDrawable(new CarrouselBase(axisBaseRadius, axisBaseHeight, yPos));
// Eje
yPos += halfAxisBaseHeight + halfAxisHeight;
addDrawable(new CarrouselBase(axisRadius, axisHeight, yPos));
// Base superior del eje
yPos += halfAxisHeight + halfAxisBaseHeight;
addDrawable(new CarrouselBase(axisBaseRadius, axisBaseHeight, yPos));
}
//################################################################################
CarrouselBottomNode::CarrouselBottomNode(DOUBLE sizeFactor) {
// Parametros editables
DOUBLE baseRadius = 2.2 * sizeFactor;
DOUBLE baseHeight = 0.15 * sizeFactor;
addDrawable(new CarrouselBase(baseRadius, baseHeight, baseHeight / 2.0));
}
//################################################################################
const DOUBLE HobbyHorse::MAX_HEIGHT = 0.40;
const DOUBLE HobbyHorse::MIN_HEIGHT = 0.0;
HobbyHorse::HobbyHorse(DOUBLE x, DOUBLE y, BOOLEAN startsUp, DOUBLE angleCorrection) {
goingUp = !startsUp;
enabled = true;
ref_ptr<MatrixTransform> matrix = new MatrixTransform(Matrix::rotate(osg::inDegrees(-angleCorrection - 180.0), Vec3(0.0,0.0,1.0)) * Matrix::translate ( Vec3(x, y, 0.0) ));
addChild(matrix);
// Limpiamos el color de las mallas
ref_ptr<Vec4Array> color = new Vec4Array;
color->push_back( Vec4(1.0,1.0,1.0,1.0) );
// Añadimos la barra
ref_ptr<Geode> barra = new Geode;
Geometry *geomBarra = NULL;
barra->addDrawable( (geomBarra = new Geometry(*MeshTable::getInstance().getMesh( string("barra_carrusel.ply") ) )) );
//ref_ptr<Geode> barra = (Geode*)osgDB::readNodeFile("meshes/barra_carrusel.ply");
//Geometry *geomBarra = barra->getDrawable(0)->asGeometry();
geomBarra->setColorArray(color);
geomBarra->setColorBinding(Geometry::BIND_OVERALL);
// Añadimos un material a la barra
ref_ptr<Material> material = new Material;
material->setAmbient(Material::FRONT_AND_BACK, Vec4(0.68,0.65,0.41,0));
material->setDiffuse(Material::FRONT_AND_BACK, Vec4(0.93,0.9,0.66,0));
material->setSpecular(Material::FRONT_AND_BACK, Vec4(1.0,1.0,1.0,0));
material->setShininess(Material::FRONT_AND_BACK, 30.0f);
barra->getOrCreateStateSet()->setAttributeAndModes( material, osg::StateAttribute::ON );
matrix->addChild(barra);
// Creamos la animación del caballo
curHeight = startsUp? MAX_HEIGHT : MIN_HEIGHT;
animationMatrix = new MatrixTransform(Matrix::translate(Vec3(0.0,0.0,curHeight)));
setUpdateCallback(new AnimationCallBack());
// Añadimos el caballo a la animación (animamos el caballo) y lo añadimos al grafo
ref_ptr<Geode> caballo = new Geode;
Geometry *geomCaballo = NULL;
caballo->addDrawable( (geomCaballo = new Geometry(*MeshTable::getInstance().getMesh( string("caballito_carrusel.ply") ) )) );
//ref_ptr<Geode> caballo = (Geode*)osgDB::readNodeFile("meshes/caballito_carrusel.ply");
//Geometry *geomCaballo = caballo->getDrawable(0)->asGeometry();
geomCaballo->setColorArray(color);
geomCaballo->setColorBinding(Geometry::BIND_OVERALL);
//Image* image = new Image(startsUp? *TextureTable::getInstance().getTexture( string("plastico.jpg") ) :
// *TextureTable::getInstance().getTexture( string("madera.png") ));
Image *image = startsUp ? osgDB::readImageFile( string("textures/plastico.jpg") ) : osgDB::readImageFile( string("textures/madera.png") );
if ( image )
{
ref_ptr<Texture2D> tex2d = new Texture2D( image );
osg::TexGen* texgen = new osg::TexGen;
texgen->setMode(osg::TexGen::OBJECT_LINEAR);
caballo->getOrCreateStateSet()->setTextureAttributeAndModes(0, texgen, osg::StateAttribute::ON);
caballo->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex2d);
}
animationMatrix->addChild(caballo);
matrix->addChild(animationMatrix);
}
void HobbyHorse::updateAnimation() {
if(enabled) {
if(goingUp) {
curHeight += 0.01;
if(curHeight >= MAX_HEIGHT)
goingUp = false;
} else {
curHeight -= 0.01;
if(curHeight <= MIN_HEIGHT)
goingUp = true;
}
animationMatrix->setMatrix(Matrix::translate(Vec3(0.0,0.0,curHeight)));
}
}
void HobbyHorse::onKeyPress() {
}
void HobbyHorse::onCtrlKeyPress() {
}
void HobbyHorse::onMouseClick() {
enabled != enabled;
}
//################################################################################
CarrouselLight::CarrouselLight(INT light, DOUBLE x, DOUBLE z) {
ref_ptr<Light> sunLight = getLight();
sunLight->setPosition( osg::Vec4( x, 0.0f, z, 1.0f ) );
sunLight->setAmbient( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) );
sunLight->setSpotCutoff(10.0f);
sunLight->setLightNum(light);
setLight( sunLight );
setLocalStateSetModes( osg::StateAttribute::ON );
getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
LightModel* lightModel = new osg::LightModel;
lightModel->setAmbientIntensity(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
getOrCreateStateSet()->setAttribute(lightModel);
}
//################################################################################
//################################################################################
CarrouselInstance::CarrouselInstance(DOUBLE xPos, DOUBLE yPos, INT horseCount, BOOLEAN addLights, DOUBLE sizeFactor) {
rotationSpeed = 0.6;
enabled = true;
rotationAngle = 0.0;
ref_ptr<MatrixTransform> cRoot = new MatrixTransform(Matrix::translate( Vec3 (xPos, yPos, 0.0) ));
addChild(cRoot);
//if(addLights)
initializeWithoutLights(cRoot, horseCount, sizeFactor);
}
void CarrouselInstance::initializeWithoutLights(osg::MatrixTransform *cRoot, INT horseCount, DOUBLE sizeFactor) {
// Construimos el objeto 3D
ref_ptr<CarrouselUpperNode> roof = new CarrouselUpperNode(sizeFactor);
ref_ptr<CarrouselLightNode> lights = new CarrouselLightNode(sizeFactor);
ref_ptr<CarrouselMiddleNode> carrouselStruct = new CarrouselMiddleNode(sizeFactor);
ref_ptr<CarrouselBottomNode> base = new CarrouselBottomNode(sizeFactor);
DOUBLE lightHeight = 1.525 * sizeFactor;
DOUBLE radius = 2.2 * sizeFactor;
ref_ptr<Material> material = new Material;
material->setAmbient(Material::FRONT_AND_BACK, Vec4(0.4,0.4,0.4,0));
material->setDiffuse(Material::FRONT_AND_BACK, Vec4(0.6,0.6,0.6,0));
material->setSpecular(Material::FRONT_AND_BACK, Vec4(1.0,1.0,1.0,0));
material->setShininess(Material::FRONT_AND_BACK, 30.0f);
carrouselStruct->getOrCreateStateSet()->setAttributeAndModes( material, osg::StateAttribute::ON );
base->getOrCreateStateSet()->setAttributeAndModes( material, osg::StateAttribute::ON );
// Lo animamos
rotation = new MatrixTransform(Matrix::rotate(osg::inDegrees(0.0), Vec3(0,0,1)));
setUpdateCallback(new AnimationCallBack());
rotation->addChild(roof);
rotation->addChild(lights);
rotation->addChild(carrouselStruct);
// Añadimos los caballitos
DOUBLE angleFactor = (360.0 / (DOUBLE)horseCount);
DOUBLE angle = 0.0;
for(int i = 0; i < horseCount; i++) {
DOUBLE x = cos( (M_PI * angle) / 180.0 ) * 1.8;
DOUBLE y = sin( (M_PI * angle) / 180.0 ) * 1.8;
ref_ptr<HobbyHorse> horse = new HobbyHorse(x, y, (i % 2 == 0), -angle);
rotation->addChild(horse);
angle += angleFactor;
}
cRoot->addChild(rotation);
cRoot->addChild(base);
}
void CarrouselInstance::updateAnimation() {
if(enabled) {
rotationAngle += rotationSpeed;
rotation->setMatrix(Matrix::rotate(osg::inDegrees(rotationAngle), Vec3(0,0,1)));
}
}
void CarrouselInstance::onKeyPress() {
if(rotationSpeed < 360.0)
rotationSpeed += 0.3;
}
void CarrouselInstance::onCtrlKeyPress() {
if(rotationSpeed > 0.0)
rotationSpeed -= 0.3;
}
void CarrouselInstance::onMouseClick() {
enabled = !enabled;
}