//---------------------------------------------------------------------------- Camera::Camera(const Vector3F& location, const Vector3F& direction, const Vector3F& up, Bool isPerspective) : mIsPerspective(isPerspective) { Init(); Vector3F right = direction.Cross(up); SetFrame(location, direction, up, right); }
//---------------------------------------------------------------------------- void Player::UpdateGun(Double deltaTime) { Float width = Application::GetApplication()->GetWidthF(); Float height = Application::GetApplication()->GetHeightF(); Vector2F cursorPosition(mLookAt.X()*2/width, mLookAt.Y()*2/height); Matrix4F projectionMatrix = mspCamera->GetProjectionMatrix(); Vector3F pickDirection(-cursorPosition.X() / projectionMatrix(0, 0), cursorPosition.Y() / projectionMatrix(1, 1), 1); Vector3F tempUp(0, 1, 0); Vector3F right = pickDirection.Cross(tempUp); Vector3F up = right.Cross(pickDirection); pickDirection.Normalize(); right.Normalize(); up.Normalize(); Matrix3F mat(-right, up, pickDirection, true); // to reduce the Wiimote's tilt jitter we average the last few sampled // tilt values Float tilt = 0; if (mRolls.GetQuantity() > 0) { for (UInt i = 0; i < mRolls.GetQuantity(); i++) { tilt += mRolls[i]; } tilt /= mRolls.GetQuantity(); } Matrix3F roll(Vector3F(0, 0, 1), tilt); mpGun->Local.SetRotate(mat * roll); UpdateShot(deltaTime, cursorPosition); }
//---------------------------------------------------------------------------- void Camera::LookAt(const Vector3F& rLocation, const Vector3F& rLookAt, const Vector3F& rUp) { const Vector3F direction = rLookAt - rLocation; SetFrame(rLocation, direction, rUp, direction.Cross(rUp)); }
//---------------------------------------------------------------------------- Bool Player::Update(Double appTime) { Float deltaTime = static_cast<Float>(appTime - mLastApplicationTime); if (mLastApplicationTime == -MathD::MAX_REAL) { deltaTime = 0.0F; } mLastApplicationTime = appTime; ProcessInput(); // Apply accumulators if (mpPhysicsWorld) { mMove *= static_cast<Float>(mpPhysicsWorld->GetFixedTimeStep()); } mPitch += (mPitchIncrement * deltaTime); mPitch = MathF::Clamp(-mMaximumVerticalAngle, mPitch, mMaximumVerticalAngle); mYaw += mYawIncrement * deltaTime; if (mYaw > MathF::TWO_PI) { mYaw = mYaw - MathF::TWO_PI; } else if (mYaw < -MathF::TWO_PI) { mYaw = MathF::TWO_PI - mYaw; } // Calculate rotation matrices Matrix3F rotationX(Vector3F::UNIT_X, mPitch); Matrix3F rotationY(Vector3F::UNIT_Y, mYaw); Matrix3F rotationYX = rotationY * rotationX; // Calculate up, gunDirection and lookup Vector3F eyeDirection = rotationYX.GetColumn(2); Vector3F up = rotationYX.GetColumn(1); mForward = rotationY.GetColumn(2); mRight = eyeDirection.Cross(up); // update player node mpNode->Local.SetTranslate(GetPosition()); mpNode->Local.SetRotate(rotationYX); // update camera mspCamera->SetFrame(GetPosition(), eyeDirection, up, mRight); UpdateGun(deltaTime); // move physics entity if (mJump) { mspCharacter->GetCharacter()->jump(); mJump = false; } mspCharacter->GetCharacter()->setWalkDirection(PhysicsWorld::Convert(mMove)); // Reset accumulators mMove = Vector3F::ZERO; mPitchIncrement = 0; mYawIncrement = 0; return true; }
//---------------------------------------------------------------------------- void Node::WarmUpRendering(Renderer* pRenderer) { #ifndef WIRE_WII // Wii does not need to warm up by submitting draw calls WIRE_ASSERT(pRenderer); UpdateGS(0, true, false); Vector3F cameraLocation = WorldBound->GetCenter(); cameraLocation.Z() += WorldBound->GetRadius(); Vector3F viewDirection = -Vector3F::UNIT_Z; Vector3F up = Vector3F::UNIT_Y; Vector3F right = viewDirection.Cross(up); CameraPtr spCamera = WIRE_NEW Camera; spCamera->SetFrame(cameraLocation, viewDirection, up, right); Float fieldOfView = 60.0F; Float aspectRatio = 2; Float nearPlane = 0.1F; Float farPlane = WorldBound->GetRadius() * 2.0F; spCamera->SetFrustum(fieldOfView, aspectRatio, nearPlane, farPlane); CullerSorting culler; culler.SetCamera(spCamera); culler.ComputeVisibleSet(this); pRenderer->PreDraw(spCamera); // draw scene to warm up batching buffers pRenderer->Draw(culler.GetVisibleSets()); // collect and draw all materials separately so none will be missed // by CULL_ALWAYS or Switch/LOD nodes. THashSet<Material*> materials; TStack<Node*> scene(1000); scene.Push(this); while (!scene.IsEmpty()) { Node* pNode = NULL; scene.Pop(pNode); RenderObject* pRenderObject = pNode->GetRenderObject(); if (pRenderObject && pRenderObject->GetMaterial()) { materials.Insert(pRenderObject->GetMaterial()); } for (UInt i = 0; i < pNode->GetQuantity(); i++) { Node* pChild = DynamicCast<Node>(pNode->GetChild(i)); if (pChild) { scene.Push(pChild); } } } RenderObjectPtr spCube = StandardMesh::CreateCube24(4, pRenderer-> GetMaxTextureStages(), true); THashSet<Material*>::Iterator it(&materials); Transformation transformation; transformation.SetTranslate(cameraLocation - Vector3F(0, 0, 3)); for (Material** pMaterial = it.GetFirst(); pMaterial; pMaterial = it.GetNext()) { spCube->SetMaterial(*pMaterial); pRenderer->Draw(spCube, transformation); } pRenderer->PostDraw(); #endif }