Mat4 Camera::GetTransform() { Mat4 transform; transform.MakeHRot(Vec3(1.0f, 0.0f, 0.0f), Pitch); transform *= HRot4(Vec3(0.0f, 1.0f, 0.0f), Yaw); transform *= HTrans4(Position); return transform; }
void CameraController::Update(float msSinceLastUpdate) { float timeFactor = msSinceLastUpdate / 16.0f; if (camera != 0) { if (len(velocity) > 1.0f) norm(velocity); camera->Position += velocity; if (HasMomentum) { velocity *= (1.0f - LinearFriction); } else { velocity.MakeZero(); } rightVec = camera->GetRightVector(); if (RestrictToXZPlane) { forwardVec = proj(Vec4(0.0f, 0.0f, -1.0f, 1.0f) * HRot4(Vec3(0.0f, 1.0f, 0.0f), camera->Yaw)); } else { forwardVec = camera->GetForwardVector(); } camera->Pitch += pitchVel; camera->Yaw += yawVel; if (camera->Pitch >= maxPitch) camera->Pitch = maxPitch; else if (camera->Pitch <= -maxPitch) camera->Pitch = -maxPitch; if (HasAngularMomentum) { pitchVel *= (1.0f - AngularFriction); yawVel *= (1.0f - AngularFriction); } else { pitchVel = 0.0f; yawVel = 0.0f; } } }
void display(void) { #if 1 glViewport(0, 0, gw, gh / 5 * 4); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80, 1, 1, 400); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); x = current_origin_x + _distance * cos(angle * vl_pi / 180); z = current_origin_z + _distance * sin(angle * vl_pi / 180); float y = bilinear(x, z); Vec3 tmove, ttri[3]; tmove = proj(HTrans4(Vec3(x, y, z)) * HRot4(Vec3(0, -1, 0), angle / 180 * vl_pi) * Vec4(move, 1)); for(int i = 0; i < 3; i++){ ttri[i] = proj(HTrans4(Vec3(x, 0, z)) * HRot4(Vec3(0, -1, 0), angle / 180 * vl_pi) * Vec4(tri[i], 1)); ttri[i][1] = bilinear(ttri[i][0], ttri[i][2]); } // car configuration Vec3 xx, yy, zz, tmp; tmp = ttri[1] - (ttri[0] + ttri[2]) / 2; tmp[1] = 0; yy = norm(cross((ttri[0] - ttri[2]), (ttri[1] - ttri[2]))); xx = norm(tmp - dot(yy, tmp) * yy); zz = cross(xx, yy); float mat[16] = {xx[0], xx[1], xx[2], 0.0, yy[0], yy[1], yy[2], 0.0, zz[0], zz[1], zz[2], 0.0, x, y + 4.5, z, 1.0}; // testing /* cout << "ttri[0] = " << ttri[0] << endl << "ttri[1] = " << ttri[1] << endl << "ttri[2] = " << ttri[2] << endl; cout << "ttri[0] - ttri[2] = " << ttri[0] - ttri[2] << endl << "ttri[1] - ttri[2] = " << ttri[1] - ttri[2] << endl; cout << "cross((ttri[0] - ttri[2]), (ttri[1] - ttri[2])) = " << cross((ttri[0] - ttri[2]), (ttri[1] - ttri[2])) << endl; cout << "yy = norm(cross((ttri[0] - ttri[2]), (ttri[1] - ttri[2])))\n = " << norm(cross((ttri[0] - ttri[2]), (ttri[1] - ttri[2]))) << endl << endl; printf("(%f, %f, %f)\nxx = (%f, %f, %f)\nyy = (%f, %f, %f)\nzz = (%f, %f, %f)\n", x, y, z, xx[0], xx[1], xx[2], yy[0], yy[1], yy[2], zz[0], zz[1], zz[2]); printf("ttri = (%f, %f, %f)\n", ttri[0][0], ttri[0][1], ttri[0][2]); printf("tmp = (%f, %f, %f)\n", tmp[0], tmp[1], tmp[2]); system("CLS"); */ gluLookAt(0, 100, 150, 0, 0, 0, 0, 1, 0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_LIGHTING); glColor3f (1,1,1); terrain(); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glPushMatrix(); glMultMatrixf(mat); glRotatef(-90, 0, -1, 0); glmDraw(car, GLM_MATERIAL | GLM_SMOOTH); glPopMatrix(); glDisable(GL_LIGHTING); #endif #if 1 // map glViewport(gw / 5 * 4 + 1, gh / 5 * 4 + 1, gw / 5, gh / 5); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80, 1, 1, 400); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 150, 1, 0, 0, 0, 0, 1, 0); glColor3f (1,1,1); terrain(); glPushAttrib(GL_ENABLE_BIT); glColor3f(1, 0, 0); glPointSize(7); glDisable(GL_TEXTURE_2D); glEnable(GL_POINT_SMOOTH); glBegin(GL_POINTS); glVertex3dv(tmove.Ref()); glEnd(); glPopAttrib(); #endif glutSwapBuffers(); }
Vec3 Camera::GetRightVector() { Vec3 rightVec; rightVec = proj(Vec4(1.0f, 0.0f, 0.0, 1.0f) * HRot4(Vec3(0.0f, 1.0f, 0.0f), Yaw)); return rightVec; }
Vec3 Camera::GetForwardVector() { Vec3 forwardVec; forwardVec = proj(Vec4(0.0f, 0.0f, -1.0f, 1.0f) * HRot4(Vec3(1.0f, 0.0f, 0.0f), Pitch) * HRot4(Vec3(0.0f, 1.0f, 0.0f), Yaw)); return forwardVec; }