Skip to content

Commit 388cc39

Browse files
committed
Add IJKL translation movements to orbital camera.
1 parent 3923a4b commit 388cc39

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

src/apps/ch11_voxel/ch11_voxel/Scene.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ void Scene::voxelize()
217217
}
218218

219219

220-
void Scene::step(const graphics::Timer & /*aTimer*/,
220+
void Scene::step(const graphics::Timer & aTimer,
221221
math::Size<2, int> aWindowResolution)
222222
{
223-
mOrbitalCamera.update(aWindowResolution.height());
223+
mOrbitalCamera.update(aTimer.delta(), aWindowResolution.height());
224224

225225
//
226226
// Camera

src/libs/scenic/scenic/Camera.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ void Orbital::incrementOrbit(math::Radian<float> aAzimuthal, math::Radian<float>
123123
}
124124

125125

126-
void Orbital::pan(math::Vec<2, float> aPanning)
126+
void Orbital::translate(math::Vec<3, float> aTranslation)
127127
{
128128
math::OrthonormalBase<3, float> tangent = mSpherical.computeTangentFrame().base;
129-
mSphericalOrigin -= aPanning.x() * tangent.u() - aPanning.y() * tangent.v();
129+
mSphericalOrigin -= aTranslation.x() * tangent.u()
130+
- aTranslation.y() * tangent.v()
131+
+ aTranslation.z() * tangent.w();
130132
}
131133

132134

@@ -199,11 +201,19 @@ void OrbitalControl::callbackCursorPosition(double xpos, double ypos)
199201
}
200202

201203

202-
void OrbitalControl::update(float aViewHeightInWorld, int aWindowHeight)
204+
void OrbitalControl::update(float aDeltaTime, float aViewHeightInWorld, int aWindowHeight)
203205
{
204206
assert(aWindowHeight != 0);
205-
mOrbital.pan(mDragVector_cursor * aViewHeightInWorld / aWindowHeight);
207+
math::Vec<3, float> translation{
208+
mDragVector_cursor * aViewHeightInWorld / aWindowHeight,
209+
0.f};
206210
mDragVector_cursor = {0.f, 0.f};
211+
212+
// Handle translations
213+
float movement = aDeltaTime * gTranslationSpeed;
214+
translation += mMovementVector * movement;
215+
216+
mOrbital.translate(translation);
207217
}
208218

209219

@@ -236,6 +246,32 @@ void OrbitalControl::callbackScroll(double xoffset, double yoffset)
236246
}
237247

238248

249+
void OrbitalControl::callbackKeyboard(int key, int scancode, int action, int mods)
250+
{
251+
if(action != GLFW_PRESS && action != GLFW_RELEASE)
252+
{
253+
return;
254+
}
255+
256+
const float v = (action == GLFW_PRESS) ? 1 : 0;
257+
switch(key)
258+
{
259+
case GLFW_KEY_I:
260+
mMovementVector.z() = v;
261+
break;
262+
case GLFW_KEY_K:
263+
mMovementVector.z() = -v;
264+
break;
265+
case GLFW_KEY_J:
266+
mMovementVector.x() = v;
267+
break;
268+
case GLFW_KEY_L:
269+
mMovementVector.x() = -v;
270+
break;
271+
}
272+
}
273+
274+
239275
////////
240276
// FirstPersonControl
241277
////////

src/libs/scenic/scenic/Camera.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ struct Orbital
148148
void incrementOrbitRadians(math::Vec<2, float> aIncrements)
149149
{ incrementOrbit(math::Radian<float>{aIncrements.x()}, math::Radian<float>{aIncrements.y()}); }
150150

151-
void pan(math::Vec<2, float> aPanning);
151+
void translate(math::Vec<3, float> aTranslation);
152+
153+
inline void pan(math::Vec<2, float> aPanning)
154+
{
155+
translate({aPanning.x(), aPanning.y(), 0.f});
156+
}
152157

153158
float & radius();
154159
float radius() const;
@@ -196,15 +201,14 @@ struct OrbitalControl
196201
void callbackMouseButton(int button, int action, int mods, double xpos, double ypos);
197202
void callbackCursorPosition(double xpos, double ypos);
198203
void callbackScroll(double xoffset, double yoffset);
199-
void callbackKeyboard(int key, int scancode, int action, int mods)
200-
{}
204+
void callbackKeyboard(int key, int scancode, int action, int mods);
201205

202206
// Note: Initially, this class was storing a copy of the VFOV,
203207
// and thus could do panning directly in the cursor position callback.
204208
// Yet this copy violated DRY, and was only behaving well with perspective projection.
205209
// Note: As an alternative to taking the window size, the class could store a pointer to the appinterface
206210
// and query when needed.
207-
void update(float aViewHeightInWorld, int aWindowHeight);
211+
void update(float aDeltaTime, float aViewHeightInWorld, int aWindowHeight);
208212

209213
Orbital mOrbital;
210214

@@ -218,10 +222,12 @@ struct OrbitalControl
218222

219223
static constexpr math::Vec<2, float> gMouseControlFactor{1/700.f, 1/700.f};
220224
static constexpr float gScrollFactor = 0.05f;
225+
static constexpr float gTranslationSpeed = 4.f; // worldunit/s
221226

222227
// The drag quantity in cursor unit (usually pixels)
223228
// This allow deferring the actual panning until update(), which can convert this quantity to world unit.
224229
math::Vec<2, float> mDragVector_cursor;
230+
math::Vec<3, float> mMovementVector;
225231
ControlMode mControlMode{ControlMode::None};
226232
math::Position<2, float> mPreviousDragPosition{0.f, 0.f};
227233
};

src/libs/scenic/scenic/CameraSystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
namespace ad::scenic {
55

66

7-
void OrbitalCamera::update(int aWindowHeight)
7+
void OrbitalCamera::update(float aDeltaTime, int aWindowHeight)
88
{
9-
mOrbitalControl.update(mViewHeightInWorld, aWindowHeight);
9+
mOrbitalControl.update(aDeltaTime, mViewHeightInWorld, aWindowHeight);
1010
}
1111

1212

src/libs/scenic/scenic/CameraSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ad::scenic {
99

1010
struct OrbitalCamera
1111
{
12-
void update(int aWindowHeight);
12+
void update(float aDeltaTime, int aWindowHeight);
1313

1414
void reset(float aAspectRatio);
1515

0 commit comments

Comments
 (0)