Skip to content

luzhixing12345/graphix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cube MVP

C++ GitHub last commit (branch) GitHub

Introduction

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.

Visualization GIF

Only rotation

cube regular-triangular-pyramid regular-octahedron
smallcube tran obasd

if you only want to see or use this part, jump to this commit is enough.

With mouse interaction

mouseinter

Compile and Run

sudo apt install libsdl2-dev
cd cube-rotation
make

There 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

About

About Environment

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.

About basic rotating core method

first calculate the centriod of the polyhedron

then use the rotating matrix transformation

  • 2D

    20220626173256

  • 3D

    20220626173340

more information about MVP

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;

About mouse interaction with rotation

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;

About floating point precision loss

Yes! That's a big problem. As you can see, if you set a high rotating speed. This cube will be seen like below.

floatproblem

Change float to float doesn't solve the problem, I still couldn't find a good solution.

About

一些图形学的练习

Resources

License

Stars

Watchers

Forks

Packages

No packages published