Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

quaternion graphics in C or C-style C++?

86 views
Skip to first unread message

David Melik

unread,
Aug 20, 2017, 12:58:21 AM8/20/17
to
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,
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.)

I.e., the only thing I'd want to use from C++ for this (since I'm trying
to learn more C and linear algebra) is in the case in C++ you can set
your matrix's values all at once, rather than I recall, in C, I had to
do one element at a time. So, I want to be able to comment out that
one-line assignment, and write C-style multiple lines, if I want to
save as a .C instead of .CC.

I combined the three standard 3x3 3D rotation matrices into one in which
I can input angles I want, then multiplied it by my 3x8 matrix of the
cube vertices (actually one by one, with a for-loop,) and after doing
perspective and displaying the cube, iterated through time (t) to move
the cube. But, I recall from a university math club lecture,
quaternions already have (x,y,z) defined for all t with respect to
whatever 3D rotation angles you use.

So, I'd like to know, how can quaternions simplify this process? I
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, or not so much iteration, that would be a benefit... is
that what one could do with quaternions? Or, is there some simpler way,
that will still reduce the amount of code you need to write, and amount
of variables/objects you need to use, as well as the calculations?

David (Darwin in USA code/math/graphics/art/music Demoscene)
http://www.cwu.edu/~melikd/

Öö Tiib

unread,
Aug 20, 2017, 6:48:25 AM8/20/17
to
On Sunday, 20 August 2017 07:58:21 UTC+3, 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,
> 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.)

So you are writing new linear algebra library in C for rotating wireframe
cubes? What is the alleged benefit?

>
> I.e., the only thing I'd want to use from C++ for this (since I'm trying
> to learn more C and linear algebra) is in the case in C++ you can set
> your matrix's values all at once, rather than I recall, in C, I had to
> do one element at a time. So, I want to be able to comment out that
> one-line assignment, and write C-style multiple lines, if I want to
> save as a .C instead of .CC.

Are you sure that what you describe is not available in C? Sounds like
something that is doable with compound literals. Can you provide example?

>
> I combined the three standard 3x3 3D rotation matrices into one in which
> I can input angles I want, then multiplied it by my 3x8 matrix of the
> cube vertices (actually one by one, with a for-loop,) and after doing
> perspective and displaying the cube, iterated through time (t) to move
> the cube. But, I recall from a university math club lecture,
> quaternions already have (x,y,z) defined for all t with respect to
> whatever 3D rotation angles you use.

It likely does not matter if to use matrices or quaternions to represent
rotation for one wireframe cube.

bartc

unread,
Aug 20, 2017, 7:21:46 AM8/20/17
to
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

fir

unread,
Aug 20, 2017, 7:26:19 AM8/20/17
to
it does not to be wireframe it may be filled ;c

i wrote something like this couple years ago

heres youtube video

https://www.youtube.com/watch?v=ha_epYAJoiI

here is win32 executable

minddetonator.htw.pl/some.zip

(no malware, at live it looks much better so i encourage to run and see)

it works as you see in 50Hz at 95k tris 3d model in lowres though

every pixel is counted on cpu by pure my code

purpose is i was qute happy with that

Alf P. Steinbach

unread,
Aug 20, 2017, 7:30:26 AM8/20/17
to
On 8/20/2017 12:48 PM, Öö Tiib wrote:
>> whatever 3D rotation angles you use.
>
> It likely does not matter if to use matrices or quaternions to represent
> rotation for one wireframe cube.

I know next to nothing about quaternions (except they're not the end of
the road in the list of kinds of numbers), but I think it's very
relevant that the rotation is about a 3D angle.

A (3x3) matrix can describe a 2D rotation plus an affine transformation.

I guess using matrices could therefore be awkward. But disclaimer: I
know next to nothing about this stuff. Going by gut feeling. :)


Cheers!,

- Alf

Öö Tiib

unread,
Aug 20, 2017, 4:10:23 PM8/20/17
to
For transformations in 3D space we can use transformation
matrices or quaternions. Matrices support more transformation
operations (most of what are less frequently needed) but also take
more memory, composing two matrices takes more operations and
transformations by matrices are more expensive to interpolate.

So if one wants to use transformations like non-uniform scaling,
skewing or projection transformations then those can be done
with matrices but not quaternions. If such transformations are
not needed then for rotations and uniform scaling the quaternions
are likely cheaper and more convenient to use.

The popular game engines like Unity3D and Unreal Engine
accept quaternions, transformation matrices and Euler angles. :)

Joe Pfeiffer

unread,
Aug 20, 2017, 7:16:44 PM8/20/17
to
If you're looking for what I think you are, search on affine
transformation matrices. The wikipedia page
https://en.wikipedia.org/wiki/Transformation_matrix appears at first
glance to give a good introduction; it's also a chapter in just about
any computer graphics textbook.

SG

unread,
Aug 21, 2017, 8:12:23 AM8/21/17
to
On Sunday, August 20, 2017 at 6:58:21 AM UTC+2, David Melik wrote:
>
> So, I'd like to know, how can quaternions simplify this process? I
> recall they're something like a scalar on some (x,y,z) but forgot how
> that would seem to simplify any multiplication or iteration.

Quaternions are useful if you need a compact representation of a 3D
rotation matrix. Given a normalized quaternion q, it's rather easy to
determine a corresponding rotation matrix R

R = q2rot(q) (I won't bother defining q2rot)

And for every rotation matrix R there are two such quaternions:
q and -q. This q2rot functions has the following properties:

q2rot(q) = q2rot(-q) and

q2rot(a * b) = q2rot(a) * q2rot(b).

Given the last equality and the fact that multiplying quaternions is
cheaper than multiplying 3x3 rotation matrices, quaternions allow you
to efficiently multiply lots of 3D rotations together. So, if you
need to multiply lots of rotations, quaternions are going to be more
efficient for that.

However, if you want to apply the resulting rotation to a collection
of points (like the vertices of your cube), you should probably
convert the quaternion back to a 3x3 rotation matrix because this
matrix representation is more efficient for such things in terms of
number of necessary floating point operations.

I think that answers your question?

Cheers!
SG

David Melik

unread,
Aug 23, 2017, 5:58:12 AM8/23/17
to
(reply below is on Usenet and 'blind carbon copy' (BCC) to a listserv)

On 08/21/2017 05:12 AM, SG wrote:
> On Sunday, August 20, 2017 at 6:58:21 AM UTC+2, David Melik wrote:
>>
>> So, I'd like to know, how can quaternions simplify this process? I
>> recall they're something like a scalar on some (x,y,z) but forgot how
>> that would seem to simplify any multiplication or iteration.
>
> Quaternions are useful if you need a compact representation of a 3D
> rotation matrix. Given a normalized quaternion q, it's rather easy to
> determine a corresponding rotation matrix R
>
> R = q2rot(q) (I won't bother defining q2rot)

Not define... why? If, for example, quaternions (or anything) were
being described in a pure mathematics textbook, *everything* would be
defined, probably full detail (unless left as an exercise, where at
least they'd define their terms.) It turns out I won't necessarily need
definition now (if you see my reply to your question below,) but...


> And for every rotation matrix R there are two such quaternions:
> q and -q. This q2rot functions has the following properties:
>
> q2rot(q) = q2rot(-q) and
>
> q2rot(a * b) = q2rot(a) * q2rot(b).
>
> Given the last equality and the fact that multiplying quaternions is
> cheaper than multiplying 3x3 rotation matrices, quaternions allow you
> to efficiently multiply lots of 3D rotations together. So, if you
> need to multiply lots of rotations, quaternions are going to be more
> efficient for that.
>
> However, if you want to apply the resulting rotation to a collection
> of points (like the vertices of your cube), you should probably
> convert the quaternion back to a 3x3 rotation matrix because this
> matrix representation is more efficient for such things in terms of
> number of necessary floating point operations.

Ok, so apparently they don't really improve something as basic as
rotating a cube. So, if I made a larger or generalized 3D system, they
could be useful.

For the cube I did, I combined my rotation matrices for the three angles
myself, beforehand. So, it seems, I won't achieve anything more by
replacing that.


> I think that answers your question?

Part of it (and all most important parts for now.) I still want to
learn quaternions for a 3D C or C-style C++ program, and now have a
better overview (not details)... but you've clarified, I should try a 3D
thing they're more useful for, first, or just a calculation program.
So, I'll have to choose which way to continue, before any more detailed
questions.

That's all for now on on Usenet from me.


------------------------------------------------------------------------
Note to listserv I sent this to (after recent discussion.) Double-check
any reply you write won't also have Usenet newsgroups in 'To:,' unless
desired (probably will only be 'To:' me, but I forgot how it works when
you post to a listserv & Usenet both.) My original post (only small
part quoted above) was to Usenet news://comp.graphics.algorithms ,
news://comp.lang.c , news://comp.lang.c++ newsgroups.

Richard Damon

unread,
Aug 23, 2017, 7:56:24 AM8/23/17
to
On 8/23/17 5:57 AM, David Melik wrote:
> (reply below is on Usenet and 'blind carbon copy' (BCC) to a listserv)
>
> On 08/21/2017 05:12 AM, SG wrote:
>> On Sunday, August 20, 2017 at 6:58:21 AM UTC+2, David Melik wrote:
>>>
>>> So, I'd like to know, how can quaternions simplify this process?  I
>>> recall they're something like a scalar on some (x,y,z) but forgot how
>>> that would seem to simplify any multiplication or iteration.
>>
>> Quaternions are useful if you need a compact representation of a 3D
>> rotation matrix.  Given a normalized quaternion q, it's rather easy to
>> determine a corresponding rotation matrix R
>>
>>    R = q2rot(q)   (I won't bother defining q2rot)
>
> Not define... why?  If, for example, quaternions (or anything) were
> being described in a pure mathematics textbook, *everything* would be
> defined, probably full detail (unless left as an exercise, where at
> least they'd define their terms.)  It turns out I won't necessarily need
> definition now (if you see my reply to your question below,) but...
>
>
I think he meant that he wasn't going to write out the code for q2rot().
Quaternions are well defined mathematically, and the creation of q2rot()
is a mostly mechanical process of looking at the definition and putting
it to code.

>> And for every rotation matrix R there are two such quaternions:
>> q and -q. This q2rot functions has the following properties:
>>
>>    q2rot(q) = q2rot(-q)  and
>>
>>    q2rot(a * b) = q2rot(a) * q2rot(b).
>>
>> Given the last equality and the fact that multiplying quaternions is
>> cheaper than multiplying 3x3 rotation matrices, quaternions allow you
>> to efficiently multiply lots of 3D rotations together.  So, if you
>> need to multiply lots of rotations, quaternions are going to be more
>> efficient for that.
>>
>> However, if you want to apply the resulting rotation to a collection
>> of points (like the vertices of your cube), you should probably
>> convert the quaternion back to a 3x3 rotation matrix because this
>> matrix representation is more efficient for such things in terms of
>> number of necessary floating point operations.
>
> Ok, so apparently they don't really improve something as basic as
> rotating a cube.  So, if I made a larger  or generalized 3D system, they
> could be useful.
>
> For the cube I did, I combined my rotation matrices for the three angles
> myself, beforehand. So, it seems, I won't achieve anything more by
> replacing that.
>
Quaternions provide a compact notation for representing an orientation,
and a fairly simple way to chain rotations. For actually doing the
rotations to lots of objects, the simple rotation matrix can be
simpler/faster (so converting the Quaternion to a matrix near the end
makes sense). One other factor is computational stability, chaining
Quaternions can't mess up the scale of orthogonality of the axes, while
with a rotation matrix, the round off errors in each operation can
gradually build up to cause these sorts of errors,

SG

unread,
Aug 25, 2017, 5:24:00 AM8/25/17
to
Am Mittwoch, 23. August 2017 11:58:12 UTC+2 schrieb David Melik:
> (reply below is on Usenet and 'blind carbon copy' (BCC) to a listserv)
>
> On 08/21/2017 05:12 AM, SG wrote:
> > On Sunday, August 20, 2017 at 6:58:21 AM UTC+2, David Melik wrote:
> >>
> >> So, I'd like to know, how can quaternions simplify this process? I
> >> recall they're something like a scalar on some (x,y,z) but forgot how
> >> that would seem to simplify any multiplication or iteration.
> >
> > Quaternions are useful if you need a compact representation of a 3D
> > rotation matrix. Given a normalized quaternion q, it's rather easy to
> > determine a corresponding rotation matrix R
> >
> > R = q2rot(q) (I won't bother defining q2rot)
>
> Not define... why?

See
<https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix>

Cheers!
SG
0 new messages