// calculates matrix from rotation axis and rotation angle // http://www.flipcode.com/documents/matrfaq.html void CMathLib::getMatrixFromRotation(CMatrix &mat, CVec &axis, float angle) { CVec tmp, quater; float sin_a = sin(angle / 2.0); float cos_a = cos(angle / 2.0); tmp.x = axis.x * sin_a; tmp.y = axis.y * sin_a; tmp.z = axis.z * sin_a; tmp.w = cos_a; // normalisieren float tmpf = 1.0/sqrt(tmp.x*tmp.x+ tmp.y*tmp.y+ tmp.z*tmp.z+ tmp.w*tmp.w); //tmpf = 1.0; quater.x = tmp.x * tmpf; quater.y = tmp.y * tmpf; quater.z = tmp.z * tmpf; quater.w = tmp.w * tmpf; matrixFromQuaternion(quater, mat); }
// The seed value of the random number generator is accessed here! void createTransforms(float changer) { float quaternion[4]; for (int transform = 0; transform < 4; transform++) { // Set quaternion values randomQuaternion(quaternion, changer); matrixFromQuaternion(quaternion, transformMat[transform]); // Multiply by scaling for (int dim = 0; dim < 3; dim++) { // RANDOM!!! //float scaling = frand() * 0.25f + 0.625f; float scaling = (0.875f - frand(&transformationSeed) * frand(&transformationSeed)); for (int i = 0; i < 4; i++) { transformMat[transform][dim][i] *= scaling; } } // Transform x-y for (int dim = 0; dim < 3; dim++) { // RANDOM!!! transformMat[transform][dim][3] = frand(&transformationSeed) - 0.5f; } } }
void Camera::updateViewMatrix() { viewMatrix = matrixFromQuaternion(); xAxis = QVector3D(viewMatrix.getEntry(0), viewMatrix.getEntry(4), viewMatrix.getEntry(8)); yAxis = QVector3D(viewMatrix.getEntry(1), viewMatrix.getEntry(5), viewMatrix.getEntry(9)); zAxis = QVector3D(viewMatrix.getEntry(2), viewMatrix.getEntry(6), viewMatrix.getEntry(10)); updateTranslationViewMatrix(); }
// This function first generates a transformation matrix for the camera // Then it multiplies it with all the transforms in the fractal tree leaves... void generateOGLTransforms(float ftime) { float quaternion[2][4]; float distance[2]; float finalTransform[4][4]; randomQuaternion(quaternion[0], 0.0f); randomQuaternion(quaternion[1], 0.0f); // linear interpolation of the two quaternions float invQuatSize = 0.0f; for (int dim = 0; dim < 4; dim++) { quaternion[0][dim] = 0.5f*ftime * quaternion[1][dim] + (1.0f - 0.5f*ftime) * quaternion[0][dim]; invQuatSize += quaternion[0][dim] * quaternion[0][dim]; } invQuatSize = 1.0f / sqrtf(invQuatSize); for (int dim = 0; dim < 4; dim++) { quaternion[0][dim] *= invQuatSize; } matrixFromQuaternion(quaternion[0], finalTransform); distance[0] = frand(&transformationSeed) - 0.2f; distance[1] = frand(&transformationSeed) + 0.2f; finalTransform[2][3] = ftime * distance[0] + (1.0f - ftime) * distance[1]; // multiply camera transform with leaf matrices for (int draw = 0; draw < FRACTAL_NUM_PRE_LEAVES; draw++) { float tmpMatrix[4][4]; matrixMult(finalTransform, fractalTree[firstPreLeaf+draw], tmpMatrix); for (int s = 0; s < 4; s++) { for (int t = 0; t < 4; t++) { fractalTree[firstPreLeaf+draw][s][t] = tmpMatrix[s][t]; } } // encode the color in the matrix fractalTree[firstPreLeaf+draw][3][0] = fractalColorTree[firstPreLeaf+draw][0]; fractalTree[firstPreLeaf+draw][3][1] = fractalColorTree[firstPreLeaf+draw][1]; fractalTree[firstPreLeaf+draw][3][2] = fractalColorTree[firstPreLeaf+draw][2]; int location = glGetUniformLocation(shaderPrograms[0], "t"); glUniformMatrix4fv(location, 1, GL_FALSE, &(fractalTree[firstPreLeaf+draw][0][0])); glDrawArrays(GL_POINTS, 0, FRACTAL_NUM_LEAVES); } }