On 20/08/2017 05:57, David Melik wrote:
> I'm interested learning how quaternions simplify three-dimensional (3D)
> graphics programming, such as for a wireframe cube in C or C-style C++
> (which I've programmed before, and is on my homepage for GCC C++ w/SDL,
(
http://www.cwu.edu/~melikd/math/demosrc/demo.cpp)
> maybe modifiable to C, and has a simple BSD-style license,) doing all
> the details, i.e., not using libraries (except, perhaps put_pixel() and
> line(), not even some matrix library, let alone graphics ones doing it
> all for you.)
One comment:
//define polyhedra
int cube_100[3][8]={{-50, -50, 50, 50, -50, -50, 50, 50},
{-50, 50, 50, -50, -50, 50, 50, -50},
{-50, -50, -50, -50, 50, 50, 50, 50}};
int cube_a[3][8]={{-50, -50, 50, 50, -50, -50, 50, 50},
{-50, 50, 50, -50, -50, 50, 50, -50},
{-50, -50, -50, -50, 50, 50, 50, 50}};
This looks rather peculiar; is each (x,y,z) point represented as a
vertical column here?
It is more normal to store x, y and z together, for example (also using
floats rather than ints, but I don't know if the above is a requirement
of SDL):
typedef struct { float x,y,z;} Point;
Point cube[8] = {
{-50, -50, -50},
{-50, 50, -50},
etc
Then, if vertices of a cube are indexed 0 to 7, the Z component of
vertex 4 would be:
cube[4].z
(Your code which uses indices for both, and in a backwards order, gets
confusing later on.)
> So, I'd like to know, how can quaternions simplify this process? I
(Can't help there; don't know quaternions.)
> recall they're something like a scalar on some (x,y,z) but forgot how
> that would seem to simplify any multiplication or iteration.
>
> Rather than in one suggestion I was given, saying break this down into
> more objects such as vertex vectors and a rotation matrix with twice as
> many angles than I need, I'd still prefer to use an object matrix
> (defining my cube's vertices,) and rotation matrices (and saw at least a
> couple different types, maybe still with several of each that could be
> multiplied,) but if there's a way to do either fewer matrix
> multiplications,
How many were you thinking of? A cube has 8 corners, so would need 8
transformations (applying a transformation matrix to each point to yield
a new point).
You only need to multiply matrices to combine transformations. That's
done once then you can apply the same result to any number of points
(ie. vertices).
Not sure about all the things your code does; one part seems to rotate a
2D cube 360 degrees, 6 degrees at a time so 60 (or 61) rotations are
applied.
To do similar with a 3D cube, which has 8 corners, you might try this
for each iteration:
Set up a new rotation matrix for this new angle
Copy the 8 vertices into a new cube
Apply the matrix on the new cube
Draw it (presumably using 12 edges between 12 pairs of those 8 vertices)
This is compared with, for example, taking an edge at a time, and
transforming the two endpoints, as you will do 24 transformations rather
than 8.
A faster way of rotating such a cube is to set up a rotation matrix for
6 degrees. Take a copy of the original cube. Then:
Draw the cube
Apply the 6 degree rotation to each of the 8 vertices
Repeat 60 times
So this avoids copying vertices, or re-calculating the rotation matrix.
But incremental errors can build up.
--
bartc