bool GameRenderer::Initialize(MaterialHandler* mtlHandlerPtr, MeshHandler* meshHandlerPtr, TextureHandler* texHandlerPtr, TransformHandler* transformHandlerPtr) { materialHandler = mtlHandlerPtr; meshHandler = meshHandlerPtr; textureHandler = texHandlerPtr; transformHandler = transformHandlerPtr; glfwWindow = glfwGetCurrentContext(); glfwGetFramebufferSize(glfwWindow, &screenWidth, &screenHeight); float ratio = (float)screenWidth / (float) screenHeight; glViewport(0, 0, screenWidth, screenHeight); glClearColor(0.4f, 0.4f, 0.5f, 1.0f); glm::vec3 camPos(0.0f, 25.0f, -70.0f); glm::vec3 treePos(0.0f, 0.0f, 0.0f); glm::vec3 scalingVector(1.5f); glm::vec3 targetOffset = glm::vec3(0.0f, 6.0f, 0.0f) * scalingVector; scaleMatrix = glm::scale(glm::mat4(), scalingVector); wvpMatrixStruct.projMatrix = glm::perspective(45.0f, ratio, 0.25f, 300.0f); wvpMatrixStruct.viewMatrix = glm::lookAt( camPos, treePos+targetOffset, glm::vec3(0.0f, 1.0f, 0.0f) ); glGenBuffers(1, &matrixUBO); glBindBuffer(GL_UNIFORM_BUFFER, matrixUBO); glBufferData(GL_UNIFORM_BUFFER, sizeof(Matrices), (void *)(&wvpMatrixStruct), GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); SetupRenderPasses(); //Loading and compiling all shaders etc if(!shaderManager.Initialize()) { return false; } Shader* genericShader = nullptr; if(!shaderManager.GetShader("objshader", &genericShader)) { LOG(ERROR) << "Couldn't fetch objshader from shadermanager. Aborting."; return false; } else { objShader = static_cast<OBJShader*>(genericShader); objShader->SetupBuffers(matrixUBO, 0); } return true; }
void Foam::GAMGSolver::scale ( scalargpuField& field, scalargpuField& Acf, const lduMatrix& A, const FieldField<gpuField, scalar>& interfaceLevelBouCoeffs, const lduInterfaceFieldPtrsList& interfaceLevel, const scalargpuField& source, const direction cmpt ) const { A.Amul ( Acf, field, interfaceLevelBouCoeffs, interfaceLevel, cmpt ); scalar scalingFactorNum = 0.0; scalar scalingFactorDenom = 0.0; scalingFactorNum = thrust::reduce ( thrust::make_transform_iterator ( thrust::make_zip_iterator(thrust::make_tuple ( source.begin(), field.begin() )), multiplyTupleFunctor() ), thrust::make_transform_iterator ( thrust::make_zip_iterator(thrust::make_tuple ( source.end(), field.end() )), multiplyTupleFunctor() ), 0.0, thrust::plus<scalar>() ); scalingFactorDenom = thrust::reduce ( thrust::make_transform_iterator ( thrust::make_zip_iterator(thrust::make_tuple ( Acf.begin(), field.begin() )), multiplyTupleFunctor() ), thrust::make_transform_iterator ( thrust::make_zip_iterator(thrust::make_tuple ( Acf.end(), field.end() )), multiplyTupleFunctor() ), 0.0, thrust::plus<scalar>() ); /* forAll(field, i) { scalingFactorNum += source[i]*field[i]; scalingFactorDenom += Acf[i]*field[i]; } */ vector2D scalingVector(scalingFactorNum, scalingFactorDenom); A.mesh().reduce(scalingVector, sumOp<vector2D>()); scalar sf = scalingVector.x()/stabilise(scalingVector.y(), VSMALL); if (debug >= 2) { Pout<< sf << " "; } const scalargpuField& D = A.diag(); /* forAll(field, i) { field[i] = sf*field[i] + (source[i] - sf*Acf[i])/D[i]; } */ thrust::transform ( field.begin(), field.end(), thrust::make_zip_iterator(thrust::make_tuple ( source.begin(), Acf.begin(), D.begin() )), field.begin(), GAMGSolverScaleFunctor(sf) ); }
void Tester::testMatrixClass() { std::cout << "Matrix Class Tester\n" << std::endl; Matrix4x4 m(1, 1, 2, 3, 4, 5, 6, 5, 4, 6, 4, 6, 1, 8, 3, 6); std::cout << "Matrix4x4 m:\n" << m << std::endl; std::cout << "m * 2: \n" << m*2 << std::endl; Matrix4x4 mInv; m.inverse(mInv); std::cout << "Inverse of m:\n" << mInv << std::endl; std::cout << "inv(m) * m: \n" << mInv*m << std::endl; Matrix4x4 mPrime; mInv.inverse(mPrime); std::cout << "Inverse of the inverse of m (i.e., m):\n" << mPrime << std::endl; Matrix4x4 mTranspose; mPrime.transpose(mTranspose); std::cout << "Transpose of m:\n" << mTranspose << std::endl; std::cout << "m + (m)^T:\n" << m + mTranspose << std::endl; std::cout << "m :\n" << m << std::endl; std::cout << "m - (m)^T:\n" << m - mTranspose << std::endl; std::cout << "Matrix Vector Operations:\n" << std::endl; Vector3D v(1, -2, 3); std::cout << "Vector3D v = " << v << "\n" << std::endl; Matrix4x4 id; std::cout << "id matrix:\n" << id << std::endl; std::cout << "id.transformVector(v) = " << id.transformVector(v) << std::endl; std::cout << "id.transformPoint(v) = " << id.transformPoint(v) << "\n" << std::endl; Matrix4x4 id2 = id * 2; std::cout << "id2 = id * 2:\n" << id2 << std::endl; std::cout << "id2.transformVector(v) = " << id2.transformVector(v) << std::endl; std::cout << "id2.transformPoint(v) = " << id2.transformPoint(v) << "\n" << std::endl; std::cout << "Testing transformations:\n" << std::endl; Vector3D delta(4, 7, -5); Matrix4x4 t = Matrix4x4::translate(delta); std::cout << "Create a translation matrix t from vector delta = " << delta << ":\n" << t << std::endl; std::cout << "Result of translating the vector delta by delta: " << t.transformVector(delta) << std::endl; std::cout << "Result of translating the point delta by delta: " << t.transformPoint(delta) << std::endl; Vector3D scalingVector(2, 2, -2); Matrix4x4 s = Matrix4x4::scale(scalingVector); std::cout << "\nCreate a scale matrix s from vector scalingVector = " << scalingVector << ":\n" << s << std::endl; std::cout << "Result of scaling the vector scalingVector by scalingVector: " << s.transformVector(scalingVector) << std::endl; std::cout << "Result of translating the point scalingVector by scalingVector: " << s.transformPoint(scalingVector) << std::endl; std::cout << "\nTransformation matrix resulting from translating and then scaling (s*t):\n" << s*t << std::endl; std::cout << "(s*t).transformVector(Vector3D(1,1,1)) = " << (s*t).transformVector(Vector3D(1,1,1)) << std::endl; std::cout << "(s*t).transformPoint(Vector3D(1,1,1)) = " << (s*t).transformPoint(Vector3D(1,1,1)) << std::endl; std::cout << "\nTransformation matrix resulting from scaling and then translating (t*s):\n" << t*s << std::endl; std::cout << "(t*s).transformVector(Vector3D(1,1,1)) = " << (t*s).transformVector(Vector3D(1,1,1)) << std::endl; std::cout << "(t*s).transformPoint(Vector3D(1,1,1)) = " << (t*s).transformPoint(Vector3D(1,1,1)) << std::endl; Vector3D axis(0, 1, 0); double angle = 45.0 * 3.14159 / 180.0; Matrix4x4 r = Matrix4x4::rotate(angle, axis); std::cout << "\nCreate a rotation matrix r of " << angle << " rads around " << axis << ":\n" << r << std::endl; }