Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » DirectX » General » Downloads
 
Add your own
alternative version

A New Perspective on Viewing

, 6 Oct 2009
Simple yet comprehensive viewing code for OpenGL and Direct3D.
Prize winner in Competition "Best C++/MFC article of October 2009"
// SpatialMath.cpp
//
// Yet another spatial math module.
//
// Copyright (C) 2009 John Hilton
//
// Provides only the methods used by the "NewView" example.

#include "StdAfx.h"
#include "SpatialMath.h"

namespace SpatialMath
{

const float CSpatialMathf::m_kVerySmall = 1e-6f;
const AXformf CSpatialMathf::m_Identity = CXformf(1, 1,0,0, 0,1,0, 0,0,1, 0,0,0);

template< typename T > AVec3<T> operator*( const AVec3<T>& a, const AMat3x3<T>& m )
{
    AVec3<T> r;
    for (int col=0; col<3; col++)
        r.v3[col] = a.x * m.m3x3[0][col]
                  + a.y * m.m3x3[1][col]
                  + a.z * m.m3x3[2][col];
    return r;
}
// Instantiate the operator
template AVec3f operator*<float>( const AVec3f& a, const AMat3x3f& m );

template< typename T > AMat3x3<T> operator*( const AMat3x3<T>& a, const AMat3x3<T>& b )
{
    AMat3x3<T> r;
    for (int row=0; row<3; row++)
        for (int col=0; col<3; col++)
            r.m3x3[row][col] = a.m3x3[row][0] * b.m3x3[0][col]
                             + a.m3x3[row][1] * b.m3x3[1][col]
                             + a.m3x3[row][2] * b.m3x3[2][col];
    return r;
}
// Instantiate the operator
template AMat3x3f operator*<float>( const AMat3x3f& a, const AMat3x3f& b );

template< typename T > T operator!( const AVec3<T>& a )
{
    return sqrt( a.x*a.x+a.y*a.y+a.z*a.z );
}
// Instantiate the operator
template float operator!<float>( const AVec3f& a );

template< typename T > CMat3x3<T>::CMat3x3( T SpinAngle, const AVec3<T>& SpinAxis )
{
    // Implement the rotation about an arbitrary axis algorithm from
    // "Robot Manipulators" by Richard P Paul, 1981

    T length = LENGTH SpinAxis;
    if (length < CSpatialMath<T>::m_kVerySmall)
    {
        *(AMat3x3*) this = CSpatialMath<T>::m_Identity.mat3x3;
        return;
    }
    T c = cos( SpinAngle );
    T s = sin( SpinAngle );
    AVec3<T> unit = SpinAxis / length;

    AVec3<T> kvers = unit * (1 - c);
    AVec3<T> ksin  = unit * s;

    xx = unit.x * kvers.x + c;
    xy = unit.x * kvers.y - ksin.z;
    xz = unit.x * kvers.z + ksin.y;
    yx = unit.y * kvers.x + ksin.z;
    yy = unit.y * kvers.y + c;
    yz = unit.y * kvers.z - ksin.x;
    zx = unit.z * kvers.x - ksin.y;
    zy = unit.z * kvers.y + ksin.x;
    zz = unit.z * kvers.z + c;
}
// Instantiate the constructor
template CMat3x3<float>::CMat3x3( float SpinAngle, const AVec3f& SpinAxis );


} // end namespace SpatialMath

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

John Hilton
Founder Spatial Freedom
Australia Australia
Software engineer, mechanical engineer, electronics engineer, inventor, manager, entrepreneur, husband, father, friend.
B.Sc. B.E.(Hons) M.Eng.Sc.
Some things I've done
- Invented the Spaceball(R)/1983 and Astroid(R)/2002 3D mice
- Patents: 3D mouse, data compression, acoustic transducer
- Wrote animation software in mid 1980s for TV commercials
- Wrote a basic CAD drawing program in 1980s
- Lived in Boston, Massachusetts for 11 years
- Architected and managed full custom ASIC chip
- Reviewed bionic eye technology for investment purposes
- Product development on CPR aid for heart attacks
- Developed an electronic sports whistle
- Was actually stranded on a deserted Pacific island
- Software: lots - embedded, device driver, applications
Some things I want to do
- Develop more cool hardware/software products
- Solve the 3D mouse software barrier to proliferate 3D mice
- Help bring 3D to the masses
- Help others

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 6 Oct 2009
Article Copyright 2009 by John Hilton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid