This is a project about 3D-cube MVP matrix transformation without using any 3D graphic library.
Especially thanks for YouTube video which is a great instructional video, and I strongly recommand it.
| cube | regular-triangular-pyramid | regular-octahedron |
|---|---|---|
![]() |
![]() |
![]() |
if you only want to see or use this part, jump to this commit is enough.
download src.zip files and unzip it as ./src for static lib
git clone git@github.com:luzhixing12345/cube-rotation.git
cd cube-rotation
makeThere are three basic model in 3D-polyhedron folder, you could use them as
-
cube
./main cube
-
regular-triangular-pyramid
./main regular-triangular-pyramid
-
regular-octahedron
./main regular-octahedron
If you prefer to use a more compilicated polyhedron, see more information in 3D-polyhedron model
This project uses SDL to create a window and show the result. I have linked all the related files and put them under src so you could directly compile it with g++ without downloading SDL first.
But if you need to set a permanent SDL workspace, this video may help you setup your SDL environment in Vscode.
There are also many excellent library such as OpenGL, MFC, QT, EasyX and so on which can help you create a interactive window and show the picture. Use them as you like.
first calculate the centriod of the polyhedron
then use the rotating matrix transformation
point.x -= centriod.x;
point.y -= centriod.y;
point.z -= centriod.z;
float rad = 0;
rad = x_axis;
point.y = std::cos(rad) * point.y - std::sin(rad) * point.z;
point.z = std::sin(rad) * point.y + std::cos(rad) * point.z;
rad = y_axis;
point.x = std::cos(rad) * point.x + std::sin(rad) * point.z;
point.z = -std::sin(rad) * point.x + std::cos(rad) * point.z;
rad = z_axis;
point.x = std::cos(rad) * point.x - std::sin(rad) * point.y;
point.y = std::sin(rad) * point.x + std::cos(rad) * point.y;
point.x += centriod.x;
point.y += centriod.y;
point.z += centriod.z;As time flies, the rotation of each axis will restore to a constant value. The restore_var is the step.
change_var is a constant value which will be multipled with distance to calculate new rotating speech for X or Y axis. Z axis doesn't participate in calcualation.
float restore_var = 0.00001;
float change_var = 0.0002;Yes! That's a big problem. As you can see, if you set a high rotating speed. This cube will be seen like below.
Change float to double doesn't solve the problem, I still couldn't find a good solution.






