Skip to content

Commit 2f1ef75

Browse files
committed
turntable rotatation w.i.p.
1 parent cab4ad7 commit 2f1ef75

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

examples/viewer/viewer.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,33 @@ struct vec3 {
220220
}
221221
};
222222

223+
struct mat3 {
224+
float m[3][3];
225+
mat3() {
226+
m[0][0] = 1.0f;
227+
m[0][1] = 0.0f;
228+
m[0][2] = 0.0f;
229+
m[1][0] = 0.0f;
230+
m[1][1] = 1.0f;
231+
m[1][2] = 0.0f;
232+
m[2][0] = 0.0f;
233+
m[2][1] = 0.0f;
234+
m[2][2] = 1.0f;
235+
}
236+
};
237+
238+
void matmul3x3(const mat3 &a, const mat3 &b, mat3 &dst) {
239+
for (size_t i = 0; i < 3; i++) {
240+
for (size_t j = 0; j < 3; j++) {
241+
float v = 0.0f;
242+
for (size_t k = 0; k < 3; k++) {
243+
v += a.m[i][k] * b.m[k][j];
244+
}
245+
dst.m[i][j] = v;
246+
}
247+
}
248+
}
249+
223250
void normalizeVector(vec3 &v) {
224251
float len2 = v.v[0] * v.v[0] + v.v[1] * v.v[1] + v.v[2] * v.v[2];
225252
if (len2 > 0.0f) {
@@ -231,6 +258,51 @@ void normalizeVector(vec3 &v) {
231258
}
232259
}
233260

261+
// Maya-like turntable
262+
// Reference:
263+
// https://gamedev.stackexchange.com/questions/204367/implementing-a-maya-like-orbit-camera-in-vulkan-opengl
264+
//
265+
// angleX, angleY = angle in degree.
266+
static void turntable(float angleX, float angleY, float center[3]) {
267+
float pivot[3];
268+
pivot[0] = center[0];
269+
pivot[1] = center[1];
270+
pivot[2] = center[2];
271+
272+
// rotate Y
273+
const float kPI = 3.141592f;
274+
float cosY = std::cos(kPI * angleY / 180.0f);
275+
float sinY = std::sin(kPI * angleY / 180.0f);
276+
277+
mat3 rotY;
278+
rotY.m[0][0] = cosY;
279+
rotY.m[0][1] = 0.0f;
280+
rotY.m[0][2] = -sinY;
281+
rotY.m[1][0] = 0.0f;
282+
rotY.m[1][1] = 1.0f;
283+
rotY.m[1][2] = 0.0f;
284+
rotY.m[2][0] = sinY;
285+
rotY.m[2][1] = 0.0f;
286+
rotY.m[2][2] = cosY;
287+
288+
float cosX = std::cos(kPI * angleX / 180.0f);
289+
float sinX = std::sin(kPI * angleX / 180.0f);
290+
291+
mat3 rotX;
292+
rotX.m[0][0] = 1.0f;
293+
rotX.m[0][1] = 0.0f;
294+
rotX.m[0][2] = 0.0f;
295+
rotX.m[1][0] = 0.0f;
296+
rotX.m[1][1] = cosX;
297+
rotX.m[1][2] = sinX;
298+
rotX.m[2][0] = 0.0f;
299+
rotX.m[2][1] = -sinX;
300+
rotX.m[2][2] = cosX;
301+
302+
303+
304+
}
305+
234306
/*
235307
There are 2 approaches here to automatically generating vertex normals. The
236308
old approach (computeSmoothingNormals) doesn't handle multiple smoothing

0 commit comments

Comments
 (0)