TransWikia.com

Crystal ball rotation - I don't get why the code works

Computer Graphics Asked by user13665 on August 27, 2021

For context:

I’m absolutely new to graphics programming and I’ve always had trouble getting the math.

I had the task to write code for a crystal ball interface using OpenGL.
There’s an object in the center of the screen and by using arrow keys, the user can rotate around it.

The code below is working and passed the grader.

The problem:

I don’t get why this code is working. I’m reading up on linear algebra (vectors, matrices…) and I understand the operations that can be performed on those structures. But when it comes to applying, it all doesn’t seem to make sense anymore.

Maybe someone here can explain to me in simple terms why this part works:

    mat3 xmat(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
    mat3 ymat(x * x, x * y, x * z, x * y, y * y, y * z, x * z, y * z, z * z);
    mat3 zmat(0.0, z, -y, -z, 0.0, x, y, -x, 0.0);

    // This part I "understand"
    mat3 rotMat((cos(radians) * xmat) + ((1 - cos(radians)) * ymat) + (sin(radians) * (zmat)));

I feel like someone would have to explain me all of it with lots of pictures.
But I hope that the above part will make the rest click for me.

Wouldn’t this way be equivalent – it looks like it-?

    mat3 xmat(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
    mat3 zmat(0.0, z, -y, -z, 0.0, x, y, -x, 0.0);

    mat3 rotMat(xmat + zmat * sin(radians) + (zmat * zmat) * (1-cos(radians)));

// Helper rotation function.  Please implement this.  
mat3 Transform::rotate(const float degrees, const vec3& axis)
{
    // YOUR CODE HERE
    vec3 normalAxis = glm::normalize(axis);
    float radians = ((2 * pi) / 360) * degrees;
    float x = axis.x;
    float y = axis.y;
    float z = axis.z;

    mat3 xmat(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
    mat3 ymat(x * x, x * y, x * z, x * y, y * y, y * z, x * z, y * z, z * z);
    mat3 zmat(0.0, z, -y, -z, 0.0, x, y, -x, 0.0);

    mat3 rotMat((cos(radians) * xmat) + ((1 - cos(radians)) * ymat) + (sin(radians) * (zmat)));
    return rotMat;
}

void Transform::left(float degrees, vec3& eye, vec3& up) {
    mat3 rotMat = rotate(degrees, up);
    eye = rotMat * eye;
}

// Transforms the camera up around the "crystal ball" interface
void Transform::up(float degrees, vec3& eye, vec3& up) {
    vec3 unit = glm::cross(eye, up);
    vec3 unitNormal = glm::normalize(unit);

    mat3 rotMat = rotate(degrees, unitNormal);
    eye = (rotMat * eye);
    up = glm::normalize(rotMat * up);

    int dot1 = glm::dot(eye, up);
}

Thanks a lot and stay healthy,
Cheers.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP