AnswerBun.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!

Related Questions

glutSolid* source code

1  Asked on November 22, 2021 by tado-mi

 

How to retrieve data from Compute Shader to CPU?

0  Asked on November 15, 2021 by ethan-ma

   

unwanted patterns in simplex noise

1  Asked on November 13, 2021 by sam-apostel

 

Procedural generation of biological models

1  Asked on November 13, 2021 by daniel-cooke

 

What method is used for baking grayscale curvature maps

1  Asked on November 10, 2021 by jummit

     

Why are texture coordinates often called UVs?

2  Asked on August 27, 2021 by samanthaj

 

Dynamic Ray-Triangle Intersection

1  Asked on August 27, 2021 by cemklkn

     

Draw Line on Arbitrary Surface

0  Asked on August 27, 2021 by valentin-molina

 

How to raytrace Bezier surfaces?

5  Asked on August 27, 2021 by luser-droog

   

Better Shadow Mapping Techniques

2  Asked on August 27, 2021 by arjan-singh

       

What does GPU assembly look like?

4  Asked on August 27, 2021 by sebastien-finor

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir