13,194,231 members (48,781 online)

#### Stats

75.9K views
89 bookmarked
Posted 5 Oct 2009

# A New Perspective on Viewing

, 6 Oct 2009
Simple yet comprehensive viewing code for OpenGL and Direct3D.
 ```// 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 operator*( const AVec3& a, const AMat3x3& m ) { AVec3 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*( const AVec3f& a, const AMat3x3f& m ); template< typename T > AMat3x3 operator*( const AMat3x3& a, const AMat3x3& b ) { AMat3x3 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*( const AMat3x3f& a, const AMat3x3f& b ); template< typename T > T operator!( const AVec3& a ) { return sqrt( a.x*a.x+a.y*a.y+a.z*a.z ); } // Instantiate the operator template float operator!( const AVec3f& a ); template< typename T > CMat3x3::CMat3x3( T SpinAngle, const AVec3& SpinAxis ) { // Implement the rotation about an arbitrary axis algorithm from // "Robot Manipulators" by Richard P Paul, 1981 T length = LENGTH SpinAxis; if (length < CSpatialMath::m_kVerySmall) { *(AMat3x3*) this = CSpatialMath::m_Identity.mat3x3; return; } T c = cos( SpinAngle ); T s = sin( SpinAngle ); AVec3 unit = SpinAxis / length; AVec3 kvers = unit * (1 - c); AVec3 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::CMat3x3( float SpinAngle, const AVec3f& SpinAxis ); } // end namespace SpatialMath ```

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.

## Share

 Founder Spatial Freedom 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

 Pro