-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCSceneNodeAnimatorCameraMayainvert
executable file
·204 lines (156 loc) · 5.2 KB
/
CSceneNodeAnimatorCameraMayainvert
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
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
class CSceneNodeAnimatorCamera2 : public scene::ISceneNodeAnimator
{
s32 m_bZoomIn; // 1 = Zoom in, 0 = No Zoom, -1 = Zoom out
s32 m_bDragging;
s32 SCREENWIDTH;
s32 SCREENHEIGHT;
core::vector2df m_lastMousePos;
core::vector2df m_curMousePos;
public:
CSceneNodeAnimatorCamera2(s32 screenWidth, s32 screenHeight)
: m_bDragging(0), m_bZoomIn(0), SCREENWIDTH(screenWidth), SCREENHEIGHT(screenHeight)
{
}
virtual void animateNode(ISceneNode* node, u32 timeMs)
{
if (!node || node->getType() != ESNT_CAMERA)
return;
scene::ICameraSceneNode* camera = static_cast<ICameraSceneNode*>(node);
// active ?
if(!camera->isInputReceiverEnabled())
return;
scene::ISceneManager * smgr = camera->getSceneManager();
if(smgr && smgr->getActiveCamera() != camera)
return;
if( m_bDragging )
{
// Calculate a rotational offset in the range of -PI to +PI
f32 dx = -(( m_curMousePos.X - m_lastMousePos.X ) / SCREENWIDTH ) * PI * 2;
f32 dy = -(( m_curMousePos.Y - m_lastMousePos.Y ) / SCREENHEIGHT ) * PI * 2;
if(core::max_(core::abs_(dx), core::abs_(dy)) >= 0.1f)
{
core::vector3df pos = camera->getPosition();
core::vector3df target = camera->getTarget();
core::vector3df originUp = camera->getUpVector();
originUp.normalize();
core::vector3df tgtv = target - pos;
tgtv.normalize();
f32 dp = tgtv.dotProduct(originUp);
if ( core::equals(core::abs_<f32>(dp), 1.f) )
{
originUp.X += 0.5f;
}
core::vector3df right = originUp.crossProduct(tgtv);
right.normalize();
core::vector3df up = tgtv.crossProduct(right);
core::quaternion q;
if(core::abs_(dy) > core::abs_(dx))
{
q.fromAngleAxis(dy, right.normalize());
}
else
{
q.fromAngleAxis(dx, up.normalize());
}
core::matrix4 mx = q.getMatrix();
core::vector3df relatedpos;
mx.rotateVect(relatedpos, pos - target);
camera->setPosition(relatedpos + target);
camera->setUpVector(up);
m_lastMousePos.X = m_curMousePos.X;
m_lastMousePos.Y = m_curMousePos.Y;
}
}
else if (m_bZoomIn != 0)
{
const core::vector3df& pos = camera->getPosition();
const core::vector3df& target = camera->getTarget();
core::vector3df newpos = target + (pos - target) * (1 + (m_bZoomIn > 0 ? -0.1f : 0.1f));
camera->setPosition(newpos);
m_bZoomIn = 0;
}
}
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == EET_MOUSE_INPUT_EVENT)
{
const SEvent::SMouseInput *ev = &event.MouseInput;
if( ev->Event == EMIE_MOUSE_WHEEL )
{
if( ev->Wheel >= 0 )
m_bZoomIn = 1;
else
m_bZoomIn = -1;
}
else
{
if( !m_bDragging && ev->isLeftPressed() )
{
m_lastMousePos.X = ev->X;
m_lastMousePos.Y = ev->Y;
m_bDragging = true;
m_curMousePos = m_lastMousePos;
}
else if( m_bDragging && !ev->isLeftPressed() )
{
m_bDragging = false;
}
else if( m_bDragging && ev->isLeftPressed() )
{
m_curMousePos.X = ev->X;
m_curMousePos.Y = ev->Y;
}
}
}
return false;
}
virtual bool isEventReceiverEnabled() const
{
return true;
}
virtual ESCENE_NODE_ANIMATOR_TYPE getType() const
{
return ESNAT_UNKNOWN;
}
virtual ISceneNodeAnimator* createClone(ISceneNode* node, ISceneManager* newManager=0)
{
return new CSceneNodeAnimatorCamera2(SCREENWIDTH, SCREENHEIGHT);
}
};
int main()
{
IrrlichtDevice *device = device = createDevice(EDT_OPENGL);
if (!device)
return 1;
device->setWindowCaption(L"Camera2");
video::IVideoDriver *driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
scene::ICameraSceneNode * camera = smgr->addCameraSceneNode();
camera->setPosition(core::vector3df(30, 20, 30));
camera->setTarget(core::vector3df(10, 0, 0));
if(camera)
{
CSceneNodeAnimatorCamera2* anim = new CSceneNodeAnimatorCamera2(driver->getScreenSize().Width, driver->getScreenSize().Height);
camera->addAnimator(anim);
anim->drop();
}
scene::ISceneNode* node = 0;
scene::ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(core::vector3df(10, 0, 0));
cube->setMaterialFlag(video::EMF_LIGHTING, false);
cube->getMaterial(0).setTexture(0, driver->getTexture("../../media/fire.bmp"));
while(device->run())
{
driver->beginScene(true, true, SColor(255,100,100,100));
smgr->drawAll();
driver->endScene();
}
device->drop();
device = 0;
return 0;
}