@@ -152,6 +152,8 @@ bool mouseRightPressed;
152152float curr_quat[4 ];
153153float prev_quat[4 ];
154154float eye[3 ], lookat[3 ], up[3 ];
155+ float g_angleX = 0 .0f ; // in degree
156+ float g_angleY = 0 .0f ; // in degree
155157bool g_show_wire = true ;
156158bool g_cull_face = false ;
157159
@@ -235,6 +237,29 @@ struct mat3 {
235237 }
236238};
237239
240+ struct mat4 {
241+ float m[4 ][4 ];
242+ mat4 () {
243+ m[0 ][0 ] = 1 .0f ;
244+ m[0 ][1 ] = 0 .0f ;
245+ m[0 ][2 ] = 0 .0f ;
246+ m[0 ][3 ] = 0 .0f ;
247+ m[1 ][0 ] = 0 .0f ;
248+ m[1 ][1 ] = 1 .0f ;
249+ m[1 ][2 ] = 0 .0f ;
250+ m[1 ][3 ] = 0 .0f ;
251+ m[2 ][0 ] = 0 .0f ;
252+ m[2 ][1 ] = 0 .0f ;
253+ m[2 ][2 ] = 1 .0f ;
254+ m[2 ][3 ] = 0 .0f ;
255+ m[3 ][0 ] = 0 .0f ;
256+ m[3 ][1 ] = 0 .0f ;
257+ m[3 ][2 ] = 0 .0f ;
258+ m[3 ][3 ] = 1 .0f ;
259+ }
260+ };
261+
262+
238263void matmul3x3 (const mat3 &a, const mat3 &b, mat3 &dst) {
239264 for (size_t i = 0 ; i < 3 ; i++) {
240265 for (size_t j = 0 ; j < 3 ; j++) {
@@ -247,6 +272,18 @@ void matmul3x3(const mat3 &a, const mat3 &b, mat3 &dst) {
247272 }
248273}
249274
275+ void matmul4x4 (const mat4 &a, const mat4 &b, mat4 &dst) {
276+ for (size_t i = 0 ; i < 4 ; i++) {
277+ for (size_t j = 0 ; j < 4 ; j++) {
278+ float v = 0 .0f ;
279+ for (size_t k = 0 ; k < 4 ; k++) {
280+ v += a.m [i][k] * b.m [k][j];
281+ }
282+ dst.m [i][j] = v;
283+ }
284+ }
285+ }
286+
250287void normalizeVector (vec3 &v) {
251288 float len2 = v.v [0 ] * v.v [0 ] + v.v [1 ] * v.v [1 ] + v.v [2 ] * v.v [2 ];
252289 if (len2 > 0 .0f ) {
@@ -258,12 +295,13 @@ void normalizeVector(vec3 &v) {
258295 }
259296}
260297
261- // Maya-like turntable
298+ // Maya-like turntable
262299// Reference:
263300// https://gamedev.stackexchange.com/questions/204367/implementing-a-maya-like-orbit-camera-in-vulkan-opengl
264301//
265302// angleX, angleY = angle in degree.
266- static void turntable (float angleX, float angleY, float center[3 ]) {
303+ // TODO: scale
304+ static void turntable (float angleX, float angleY, float center[3 ], float dst[4 ][4 ]) {
267305 float pivot[3 ];
268306 pivot[0 ] = center[0 ];
269307 pivot[1 ] = center[1 ];
@@ -299,7 +337,7 @@ static void turntable(float angleX, float angleY, float center[3]) {
299337 rotX.m [2 ][1 ] = -sinX;
300338 rotX.m [2 ][2 ] = cosX;
301339
302-
340+
303341
304342}
305343
@@ -1138,15 +1176,25 @@ int main(int argc, char** argv) {
11381176 GLfloat mat[4 ][4 ];
11391177 gluLookAt (eye[0 ], eye[1 ], eye[2 ], lookat[0 ], lookat[1 ], lookat[2 ], up[0 ],
11401178 up[1 ], up[2 ]);
1179+
1180+ float center[3 ];
1181+ center[0 ] = 0.5 * (bmax[0 ] + bmin[0 ]);
1182+ center[1 ] = 0.5 * (bmax[1 ] + bmin[1 ]);
1183+ center[2 ] = 0.5 * (bmax[2 ] + bmin[2 ]);
1184+ float rotm[4 ][4 ];
1185+ turntable (g_angleX, g_angleY, center, rotm);
1186+
11411187 build_rotmatrix (mat, curr_quat);
11421188 glMultMatrixf (&mat[0 ][0 ]);
11431189
11441190 // Fit to -1, 1
11451191 glScalef (1 .0f / maxExtent, 1 .0f / maxExtent, 1 .0f / maxExtent);
11461192
1193+ #if 0
11471194 // Centerize object.
11481195 glTranslatef(-0.5 * (bmax[0] + bmin[0]), -0.5 * (bmax[1] + bmin[1]),
11491196 -0.5 * (bmax[2] + bmin[2]));
1197+ #endif
11501198
11511199 Draw (gDrawObjects , materials, textures);
11521200
0 commit comments