i want to try writing a playground similar to this 4D toys,
so i started learning opengl.
from my current understanding, people use VBOs and uniform transformation matrix for mostly-static objects
(like cubes, skeletal animations etc., which usually just involves transformations)
i also heard that morphing between models also uses VBOs for caching both models, since both of them will be well defined and not so much intermediates.
but in the 4D toys mentioned above, objects are morphing and clipped a lot.
and it is likely that there is no defined models, and many transitions in between.
(it might be a simple square now, and a spiky ball clipped in half later ).
in this case, is updating-vertex-VBO-per-frame or Vertex Arrays(which i saw in another question) a suitable solution?
For starters I would use
4D -> 3D
projection instead of cut by hyperplane. The result is not the same but will get you closer to your goal (so you can latter upgrade this to cut). So similarly like in3D -> 2D
conversions used in graphics you got 2 choices one is using perspective projection and second is just ignoring the 4th dimension coordinate while rendering. I will use the latter as it is simpler.structures
To make this as simple as I can I will use wire-frame instead of BR rendering. So you need to handle 4D mesh (wire-frame). I would use 2 tables:
first one stores all the vertexes of your mesh and the second hold index pairs of points connected by lines in wire-frame representation.
transforms
If I would only ignore the 4th coordinate then we would not get the desired functionality. So to make the 4th dimension work we need to add 4D transform to orient our mesh in 4D before rendering. So use homogenous transform matrix and lets call ir
rep
. In 4D it should be5x5
orthonormal matrix with4x4
rotation partrot
.To make this even easier avoid smooth rotations for now (as in 4D that is not as easy) and compute random rotation
4x4
matrix instead. So just set all the cells randomly<-1,+1>
. Handle each row as basis vector. To make them orthonormal just make them unit and exploit cross product. For more info see:render
simply convert point table by your transform matrix
then take the (x
,y
,z`) and render ...Here simple OpenGL/C++ example of 4D hyper cube:
I used mine dynamic
list.h
template so:List<double> xxx;
is the same asdouble xxx[];
xxx.add(5);
adds5
to end of the listxxx[7]
access array element (safe)xxx.dat[7]
access array element (unsafe but fast direct access)xxx.num
is the actual used size of the arrayxxx.reset()
clears the array and setxxx.num=0
xxx.allocate(100)
preallocate space for100
itemsthe
nd_math.h
is mine lib for N-Dimensional computations. What you need is just 4D,5D vector and4x4
,5x5
matrix math from linear algebra.Both libs are a bit big in size and also legal issues prevent me to share their code here.
The usage is simple:
And here preview for some
rep
rotations ...so this way you can render any wire-frame mesh (even BR rendering should works this way).
If you want to upgrade to the cut then you should take each Wire-frame line and compute its intersection with cutting hyperplane. If we chose hyperplane that goes through point
and has normal
Then the task will simplify a lot. there are 3 options. Lets consider edge line with endpoints
A,B
:no intersection
just ignore such edge
1 intersection
so compute the intersection via linear interpolation
and remember such point and also edge to which it belongs to.
fully inside
just remember both endpoints and also render this edge.
After all edges are processed this way then you need to analyze the remembered intersection points and create new edges from them based on connectivity info between edges. I did not do this yet so I can not help with this. I would try to connect remembered points sharing the same neighbor but not sure if that is enough in 4D.
For more info take a look at related QAs I found or answer:
[Edit1] code with perspective
And preview:
[Edit2] solid mesh and cross-section
so I changed the architecture quite a bit. I moved the 4D
5x5
homogenuous transform matrix (reper4D
) to separate file and added colors and mesh definition by 4D simplexes (4 point 4 side tetrahedrons). The cut is simply computing the intersection (as described above) of simplex and cutting hyperplane resulting in either 3 points (triangle) , 4 points (tetrahedron) or 0 points. Which can be rendered easily (no need to analyze the connections between edges). For more info see this:Btw. I think this is how Miegakure works. Here updated code:
And the preview for cross section render:
The worst part was to define the hypercube as set of simplexes ...