const Vec3 GetDirection() const { if (mOverridingCamera){ return mOverridingCamera->GetDirection(); } return mTransformation.GetMatrix().Column(1); }
void Custom::AppendTransformation(Transformation const& Trafo) { Matrix const& TrafoMatrix = Trafo.GetMatrix(); if (TrafoMatrix.size2() != m_Matrix.size1()) { throw std::invalid_argument("Matrix bounds do not match."); } Matrix Result(m_Matrix.size1(), TrafoMatrix.size2()); boost::numeric::ublas::axpy_prod(m_Matrix, TrafoMatrix, Result, true); m_Matrix = std::move(Result); }
void DrawPrimitiveQuad(Transformation &QuadTransformation, const EBlendMode &Mode, const ColorRGB &Color) { Image::BindNull(); WindowFrame.SetUniform(U_COLOR, Color.Red, Color.Green, Color.Blue, Color.Alpha); SetBlendingMode(Mode); Mat4 Mat = QuadTransformation.GetMatrix(); WindowFrame.SetUniform(U_MVP, &(Mat[0][0])); // Assign position attrib. pointer SetPrimitiveQuadVBO(); DoQuadDraw(); FinalizeDraw(); Image::ForceRebind(); }
//---------------------------------------------------------------------------- void Update() { if (mOverridingCamera){ mOverridingCamera->Update(); } // world coordinates (Blender style) // x: right // y: forward // z: up bool viewChanged = mViewPropertyChanged; if (mViewPropertyChanged) { mViewPropertyChanged = false; Vec3 right = mTransformation.GetMatrix().Column(0); Vec3 forward = mTransformation.GetMatrix().Column(1); Vec3 up = mTransformation.GetMatrix().Column(2); const Vec3& pos = mTransformation.GetTranslation(); mMatrices[View] = fb::MakeViewMatrix(pos, right, forward, up); mTransformation.GetHomogeneous(mMatrices[InverseView]); mFrustum.mOrigin = mTransformation.GetTranslation(); mFrustum.mOrientation = mTransformation.GetRotation(); } bool projChanged = mProjPropertyChanged; if (mProjPropertyChanged) { mAspectRatio = GetWidth() / (Real)GetHeight(); mProjPropertyChanged = false; if (!mOrthogonal) { mMatrices[ProjBeforeSwap] = mMatrices[Proj] = MakeProjectionMatrix(mFov, mAspectRatio, mNear, mFar); } else { mMatrices[ProjBeforeSwap] = mMatrices[Proj] = MakeOrthogonalMatrix((Real)mOrthogonalData.left, (Real)mOrthogonalData.top, (Real)mOrthogonalData.right, (Real)mOrthogonalData.bottom, mNear, mFar); } if (mYZSwap) { Mat44 swapMat( 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1); mMatrices[Proj] = mMatrices[Proj] * swapMat; } mMatrices[InverseProj] = mMatrices[Proj].Inverse(); mFrustum.SetData(mNear, mFar, mFov, mAspectRatio); } if (projChanged || viewChanged) { mMatrices[ViewProj] = mMatrices[Proj] * mMatrices[View]; mMatrices[InverseViewProj] = mMatrices[ViewProj].Inverse(); UpdateFrustum(); if (viewChanged && !mSelf->mObservers_.empty()){ auto& observers = mSelf->mObservers_[TransformChanged]; for (auto it = observers.begin(); it != observers.end(); /**/){ auto observer = it->lock(); if (!observer){ it = observers.erase(it); continue; } ++it; observer->OnViewMatrixChanged(); } } if (projChanged && !mSelf->mObservers_.empty()){ auto& observers = mSelf->mObservers_[TransformChanged]; for (auto it = observers.begin(); it != observers.end(); /**/){ auto observer = it->lock(); if (!observer){ it = observers.erase(it); continue; } ++it; observer->OnProjMatrixChanged(); } } mRayCache.clear(); } }
//---------------------------------------------------------------------------- void Sample5::OnIdle() { Double time = System::GetTime(); Double elapsedTime = time - mLastTime; mLastTime = time; mAngle += static_cast<Float>(elapsedTime); mAngle = MathF::FMod(mAngle, MathF::TWO_PI * 2); // scene graph transformations // Node* pLitGroup = DynamicCast<Node>(mspRoot->GetChild(0)); WIRE_ASSERT(pLitGroup); // rotate the 2 cubes Matrix3F rotate1(Vector3F(0.75F, 0.25F, 0.5F), -mAngle * 0.5F); Spatial* pCube1 = pLitGroup->GetChild(0); pCube1->Local.SetRotate(rotate1); Matrix3F rotate2(Vector3F(-0.75F, -0.25F, -0.5F), -mAngle * 0.5F); Spatial* pCube2 = pLitGroup->GetChild(1); pCube2->Local.SetRotate(rotate2); // move the green light up and down Float y = MathF::FMod(static_cast<Float>(time), MathF::TWO_PI); Vector3F lightPos1(0, MathF::Sin(y*2) * 1.5F, 2); Node* pLightNode1 = DynamicCast<Node>(mspRoot->GetChild(1)); WIRE_ASSERT(pLightNode1); pLightNode1->Local.SetTranslate(lightPos1); // rotate the red light about the y axis Node* pLightNode2 = DynamicCast<Node>(mspRoot->GetChild(2)); WIRE_ASSERT(pLightNode2); Matrix34F rotateLight2(Vector3F::UNIT_Y, -mAngle); Vector3F lightPos2 = rotateLight2 * Vector3F(5, 0, 0); pLightNode2->Local.SetTranslate(lightPos2); mspRoot->UpdateGS(time); mCuller.ComputeVisibleSet(mspRoot); // manual transformation from local to world space of // the non-scene graph part Transformation transformation; Float angle = MathF::Sin(mAngle*2); angle = angle * MathF::HALF_PI*0.3F + MathF::PI; Matrix34F rotateLocalLight3(Vector3F(0, 1, 0), angle); Matrix34F rotateWorldLight3(Vector3F(1, 0, 0), -0.5F); transformation.SetTranslate(Vector3F(0.5F, -1.0F, 4+MathF::Sin(y*1.0F)*2)); transformation.SetRotate(rotateWorldLight3 * rotateLocalLight3); transformation.SetUniformScale(0.15F); mspSpotLight->Position = transformation.GetTranslate(); mspSpotLight->Direction = transformation.GetMatrix().GetColumn(2); GetRenderer()->ClearBuffers(); GetRenderer()->PreDraw(mspCamera); // render the scene graph GetRenderer()->Draw(mCuller.GetVisibleSets()); // before we start drawing objects in 'manual' mode, release all resources // cached by the Renderer to return the renderer to its default state. // This is necessary when identical resources (mspTexture in this case) // are used by the scene graph and other objects that are being draw // manually. GetRenderer()->ReleaseResources(); // render the white cube representing the spot light GetRenderer()->Draw(mspWhiteCube, transformation); Matrix34F matrix(Vector3F(1.0F, 0, 0), -1.0F, Vector3F(0, -2.5F, 0)); transformation.SetMatrix(matrix, false); transformation.SetUniformScale(1); // render the bottom plane which is being lit by the spot light GetRenderer()->SetLight(mspSpotLight); GetRenderer()->EnableLighting(mspSpotLight->Ambient); GetRenderer()->Draw(mspPlane, transformation); GetRenderer()->DisableLighting(); GetRenderer()->PostDraw(); GetRenderer()->DisplayBackBuffer(); }