Mathew Kirschbaum
Mathew Kirschbaum

Reputation: 145

The math to render a cube?

My friend and I are making a 3d rendering engine from scratch in our VB class at school, but I am not sure how the math to form the cube would work. Given six variables:

    rotX
    rotY
    rotZ
    lenX
    lenY
    lenZ

Which represent the rotation on x,y,z and the length on x,y,z respectively, what would be the formulas to make the cube? I know that all I have to do is calculate three segments and from those segments just create three parallelograms, so I just need the math to find what the three segments are. Thanks!

Upvotes: 0

Views: 1811

Answers (1)

Spektre
Spektre

Reputation: 51913

there are 2 basic 3D object representations for both are your data is insufficient.

  1. surface representation

    • objects are set of surface polygons/vertexes/...
    • for cube its a set of 8 points + the triangles/quads for 6 faces
  2. analytical representation

    • objects are set of equations describing the object
    • for cube its a intersection of 6 planes

I think you are using option 1 so what you need is: - position - orientation - size

usually an axis aligned cube looks like this:

const double a=1.0; //cube size;
double  pnt[8][3]=  //cube points
    {
    +a,-a,+a,
    +a,+a,+a,
    -a,+a,+a,
    -a,-a,+a,
    +a,-a,-a,
    +a,+a,-a,
    -a,+a,-a,
    -a,-a,-a
    };
int     tab[24]=
    {
    0,1,2,3,    // 1st.quad
    7,6,5,4,    // 2nd.quad
    4,5,1,0,    // 3th.quad ...
    5,6,2,1,
    6,7,3,2,
    7,4,0,3
    };

well for size and orientation you can apply transformation matrix
or directly recompute points by direction vectors

  • so you need to remember position (point) and orientation (3 vectors) and size (scalar)
  • all above can be stored in single transformation matrix 4x4
  • but if you want the vectors then points will be like this:

P(+a,-a,+a) -> +a*I -a*J +a*K

  • where I,J,K are the orientation vectors
  • a is cube size
  • P(+a,-a,+a) is original axis aligned point in table above

Option 2 is more tricky to implement and unless you really need it (ray-tracing renders) then forget about it.

Upvotes: 1

Related Questions