Click here to Skip to main content
13,597,589 members
Click here to Skip to main content
Add your own
alternative version

Stats

4.9K views
183 downloads
15 bookmarked
Posted 12 Jun 2018
Licenced CPOL

Learn 3D Math made by the GPU by Creating a 3D Engine on CPU

, 14 Jun 2018
Rate this:
Please Sign up or sign in to vote.
This article will show you the basic 3D math done on the CPU.

(You need Visual Studio 2017 to compile the project.)

Introduction

This article will help you to understand how 3D calculations are made inside the GPU.

But also to make a 3D Engine for voxel.

A voxel is just a point within a space, like an atom that composes the material, it's my own definition after, for the other engine, voxel are just cube of pixels like Minecraft and I don't use this definition for my voxel engine.

Also, this 3D engine does not use matrices calculations, it uses the linear equation. I do that just because I don't like matrices, it's out of my understanding of math concept.

Background

This article was possible with my wish (2010) to see a game with texture and object built only by voxel.

Using the Code

First of all, we need to ask few questions before:

  • What is a space?
  • what is a dimension?
  • What are trigonometric functions?
  • What is a vector?
  • What is a matrix?
  • What is a rotation and translation?
  • what is an axis?

Now let's give anwsers to these questions:

A space is defined by a number of axes in unique direction that form the dimensions of the space.

And to form an axis, we use mathematical equations as additions and trigonometric functions where each number is visually a point within the axis.

A vector is a point into the space localized by coordinates on the different axes that compose the space.

A 1D space is represented by a line in one direction/axis where X represents each point of this line, it's its coordinate:  X ----------------------------

To add an object in a 1D space, we just need X as coordinate for the object's point and to move this object, just apply X = X + Object[Point][X].

     -3  -2  -1   0 +1 +2 +3                                                                                                                                               X  --|---|---|---|---|---|---|-- 

By consequence, the object moves within the same line.

A translation is done within a line, 1D space, so we have 1 translation possible on X-axis.

A 2D space is just a 1D space with another line in a direction at 90° of the X-axis where Y represents it:

To add an object into this 2D space, we need to set 2 coordinates for each point of the object and to move the object, we apply an addition like we did before for translate the object on X or Y-axis.

But we can also rotate the object around the center of the 2 axis, for doing that, an addition is not enough, we need to call a math operator that is designed for rotation calculations.

A rotation is done within a plan, 2D space, so we have 1 rotation possible around the center of the two axis.

This math operator are the trigonometric functions: sin() and cos() that take the angle between the two lines: [center, object's point] and X-axis for cos() and Y-axis for sin().

Visually, I have an object point at x = -1, y = 1 and we apply a rotation of +90° (anticlockwise) around the center, the result is x = 1, y = 1.

And the calculations for found this result is:

x' = x.cos(angle) - y.sin(angle)
y' = x.sin(angle) + y.cos(angle)

This equation is also just a linear form of the Z rotation matrix multiply (where Z is the center of the 2 axis) by the object point called vector: (phi is the angle around the axis in the context)

Rotation matrix on z:                                              Vector:
 ______________ ______________ ______________ ______________       ______________
|              |              |              |              |     |              |
|  cos(phi_z)  | -sin(phi_z)  |       0      |       0      |     |       x      |
|______________|______________|______________|______________|     |______________|
|              |              |              |              |     |              |
|  sin(phi_z)  |  cos(phi_z)  |       0      |       0      |     |       y      |
|______________|______________|______________|______________|  *  |______________|
|              |              |              |              |     |              |
|       0      |       0      |       1      |       0      |     |       z      |
|______________|______________|______________|______________|     |______________|
|              |              |              |              |     |              |
|       0      |       0      |       0      |       1      |     |     color    |
|______________|______________|______________|______________|     |______________|

And a 3D space is 2D space with another axis called Z and at 90° of the XY plan.

The moves possible are translation (addition equation) and rotation (sin and cos).

But now rotations are in number of 3, around Z, around X and around Y.

Because as we said before, a rotation is made on a 2D plan, but now we have a mix of 3x 2D space/plan:               - X/Y, Z/X and Z/Y.

So we need to gather all the rotation matrices and use them by multiplying each of them by a vector.

X Rotation Matrix

 ______________ ______________ ______________ ______________
|              |              |              |              |
|       1      |       0      |       0      |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|       0      |  cos(phi_x)  | -sin(phi_x)  |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|       0      |  sin(phi_x)  |  cos(phi_x)  |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|       0      |       0      |       0      |       1      |
|______________|______________|______________|______________|

Y Rotation Matrix

 ______________ ______________ ______________ ______________
|              |              |              |              |
|  cos(phi_y)  |       0      | -sin(phi_y)  |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|       0      |       1      |       0      |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|  sin(phi_y)  |       0      |  cos(phi_y)  |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|       0      |       0      |       0      |       1      |
|______________|______________|______________|______________|

Z Rotation Matrix

 ______________ ______________ ______________ ______________
|              |              |              |              |
|  cos(phi_z)  | -sin(phi_z)  |       0      |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|  sin(phi_z)  |  cos(phi_z)  |       0      |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|       0      |       0      |       1      |       0      |
|______________|______________|______________|______________|
|              |              |              |              |
|       0      |       0      |       0      |       1      |
|______________|______________|______________|______________|

But we have a problem, if we are doing that, we will not be able to rotate an object around XYZ axis together, it's 1 rotation on 1 axis only.

To fix that, we need to form 1 single matrix by multiplying XYZ matrices together.

So instead having 3 rotations on 3 x 2D plan, we will have only 1 rotation for a 3D space.

Rotation matrix on X, Y and Z:

 ___________________________________ ___________________________________ _________________________ ___
|                                   |                                   |                         |   |
|       cos(phi_y) * cos(phi_z)     |       cos(phi_y) * -sin(phi_z)    |       -sin(phi_y)       | 0 |
|___________________________________|___________________________________|_________________________|___|
|                                   |                                   |                         |   |
| -sin(phi_x)*sin(phi_y)*cos(phi_z) |  sin(phi_x)*sin(phi_y)*sin(phi_z) | -sin(phi_x)*cos(phi_y)  | 0 |
|    + cos(phi_x) * sin(phi_z)      |     + cos(phi_x)*cos(phi_z)       |                         |   |
|___________________________________|___________________________________|_________________________|___|
|                                   |                                   |                         |   |
|  cos(phi_x)*sin(phi_y)*cos(phi_z) | cos(phi_x)*sin(phi_y)*-sin(phi_z) |  cos(phi_x)*cos(phi_y)  | 0 |
|    + sin(phi_x)*sin(phi_z)        |    + sin(phi_x)*cos(phi_z)        |                         |   |
|___________________________________|___________________________________|_________________________|___|
|                                   |                                   |                         |   |
|                 0                 |                 0                 |            0            | 1 |
|___________________________________|___________________________________|_________________________|___|

Now, we just need to multiply this matrix to the vector point of the object to be able to rotate the object around XYZ axis.

And the result is:

Now it's almost finished, we need to create a camera to be able to navigate into the scene.

The camera is defined by 3 vectors: Forward, Right and Up:

          X   Y   Z
right   ( 1,  0,  0)
left    (-1,  0,  0)
up      ( 0,  1,  0)
down    ( 0, -1,  0)
forward ( 0,  0, -1)
backward( 0,  0, -1)

Then, we multiply these 3 vectors to XYZ matrix separately because we need each of them to be able to move the camera Forward/Backward/Right/Left/Up/Down.

Example: If press W, Forward vector is used, if press D, Right vector is used,  ...

And in the same time, we simplify the equations knowing that we have coordinate set at zero.

Now that we have our 3 camera vectors to be able to move in the space, we just need to add them to the object vector point, like that:

Vector Object:                   Vector Camera:
  _________________________       _________________________
 |                         |     |                         |
 |            x            |     |            x            |
 |_________________________|     |_________________________|
 |                         |     |                         |
 |            y            |  +  |            y            |
 |_________________________|     |_________________________|
 |                         |     |                         |
 |            z            |     |            z            |
 |_________________________|     |_________________________|

Then, we multiply another XYZ matrix based on the camera angle with the vector point from the object to be able to move the object depending on the camera position and angle.

That's all for now, I hope I will give updates often because this code is not perfect, it has some bugs and does not have all the features of 3D math.

History

  • 12th June, 2018: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

irakyu
France France
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionTry this as one of your tables - created in Word Pin
davesmills14-Jun-18 3:59
memberdavesmills14-Jun-18 3:59 
AnswerRe: Try this as one of your tables - created in Word Pin
irakyu14-Jun-18 7:04
memberirakyu14-Jun-18 7:04 
QuestionI fell miss of some concepts and some complements Pin
PySams14-Jun-18 3:27
professionalPySams14-Jun-18 3:27 
AnswerRe: I fell miss of some concepts and some complements Pin
irakyu14-Jun-18 6:59
memberirakyu14-Jun-18 6:59 
GeneralMy vote of 5 Pin
Arun Virupaksha12-Jun-18 21:31
professionalArun Virupaksha12-Jun-18 21:31 
GeneralRe: My vote of 5 Pin
irakyu12-Jun-18 22:55
memberirakyu12-Jun-18 22:55 
GeneralRe: My vote of 5 Pin
Arun Virupaksha12-Jun-18 23:09
professionalArun Virupaksha12-Jun-18 23:09 
GeneralRe: My vote of 5 Pin
irakyu12-Jun-18 23:22
memberirakyu12-Jun-18 23:22 
QuestionTables: a bad idea Pin
ppolymorphe12-Jun-18 16:32
mvpppolymorphe12-Jun-18 16:32 
AnswerRe: Tables: a bad idea Pin
irakyu12-Jun-18 20:22
memberirakyu12-Jun-18 20:22 
GeneralRe: Tables: a bad idea Pin
ppolymorphe12-Jun-18 21:02
mvpppolymorphe12-Jun-18 21:02 
GeneralRe: Tables: a bad idea Pin
irakyu12-Jun-18 21:08
memberirakyu12-Jun-18 21:08 
GeneralRe: Tables: a bad idea Pin
ppolymorphe12-Jun-18 21:15
mvpppolymorphe12-Jun-18 21:15 
QuestionOne omission Pin
Rick York12-Jun-18 10:01
memberRick York12-Jun-18 10:01 
AnswerRe: One omission Pin
irakyu12-Jun-18 20:22
memberirakyu12-Jun-18 20:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 14 Jun 2018
Article Copyright 2018 by irakyu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid