static void draw() { static int last_time = 0; const int current_time = glutGet(GLUT_ELAPSED_TIME); const float delta = float(current_time - last_time) / 1000.0f; camera.translation += get_walk_direction() * Vec3f(delta) * Vec3f(0.05); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLoadMatrixf(to_mat4(inverse(camera)).data); // LIGHT SETUP HERE const Vec3f tmp = normalize(Vec3f(1, 1, 0)); const Vec4f light_dir(tmp.x, tmp.y, tmp.z, 0); const Vec4f ambient(0.4, 0.4, 0.4, 1); const Vec4f diffuse(1, 1, 1, 1); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient.data); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse.data); glLightfv(GL_LIGHT0, GL_POSITION, light_dir.data); // RENDER HERE glBegin(GL_TRIANGLES); for (int idx : indices) { const Vertex &v = vertices[idx]; glNormal3fv(v.normal.data); glVertex3fv(v.position.data); } glEnd(); glutSwapBuffers(); glutPostRedisplay(); }
void get_camera_vectors(Vec3 *look_dir, Vec3 *up, Vec3 *right, const Quat &orient) { NG_ASSERT(look_dir != nullptr); NG_ASSERT(up != nullptr); NG_ASSERT(right != nullptr); const Mat4 m = to_mat4(inverse(orient)); *right = { m[0], m[4], m[8]}; *up = { m[1], m[5], m[9]}; *look_dir = {-m[2], -m[6], -m[10]}; }
Mat4 to_mat4(const Transform &tf) { return to_mat4(tf.orientation) * Mat4_Translate(tf.translation); }