O'Reilly logo

Graphics Shaders, 2nd Edition by Steve Cunningham, Mike Bailey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

470
B. Matrix4 C++ Class
float GetDeterminant( );
float GetElement( int, int );
Matrix4 GetInverse( );
Matrix4 GetInverse3( );
void GetMatrix4( float [4][4] );
void GetMatrix43( float [3][3] );
Matrix4 GetTranspose( );
Matrix4 GetTranspose3( );
Matrix4& Invert( );
Matrix4& Invert3( );
Matrix4& LoadIdentity( );
Matrix4& LookAt( float, float, float, float, float, float,
float, float, float );
Vec3 MultBy( Vec3 );
Matrix4& operator=( const Matrix4& );
Matrix4 operator*( float );
Matrix4 operator*( Matrix4& );
Point3 operator*( Point3& );
Vec3 operator*( Vec3& );
Matrix4& operator*=( Matrix4& );
Matrix4& operator*=( float );
Matrix4 operator+( Matrix4& );
Matrix4& operator+=( Matrix4& );
Matrix4 operator-( Matrix4& ); // binary
Matrix4& operator-( ); // unary
Matrix4& operator-=( Matrix4& );
Matrix4& operator-=( float );
Matrix4& Ortho( float, float, float, float, float, float );
Matrix4& Ortho2D( float, float, float, float );
Matrix4& Perspective( float, float, float, float );
Matrix4& PopMatrix4( );
Matrix4& Print( char * = “”, FILE * = stderr );
Matrix4& PushMatrix4( );
Matrix4& Rotatef( float, float, float, float );
Matrix4& Scalef( float, float, float );
Matrix4& SetElement( int, int, float );
Matrix4& SetMatrix4( float [4][4] );
Matrix4& SetMatrix43( float [3][3] );
Matrix4& StereoPerspective( float, float, float, float, float,
float );
Matrix4& Translatef( float, float, float );
Matrix4& Transpose( );
The method names have been selected to mimic OpenGL procedure
names wherever possible, such as
Matrix4& Rotatef( float, float, float, float );
471
The Program Body
Matrix4& Scalef( float, float, float );
Matrix4& Translatef( float, float, float );
The default constructor sets the matrix to identity, but you can also
explicitly do that with the
LoadIdentity( ) method.
There are many operator overloads, so that you can use matrices in
expressions, such as
Matrix4 R;
R.Rotatef( 30., 1., 0., 0. );
Matrix4 T;
T.Translatef( 2., 3., 4. );
Matrix4 P = R * T;
Many of these methods return a reference to the result so that they can be
chained together, like this:
Matrix4 Comp;
Comp.LoadIdentity( ).Translatef(-A,-B,-C).Scalef(3.,4.,1.).
Translatef(A,B,C).Print(“Composite = “);
These operations are evaluated left-to-right.
Here are some examples of using the
Matrix4 class:
#include “matrix4.h”
Matrix4 I;
I.Print( “I = “ );
Matrix4 R;
R.Rotatef( 30., 1., 0., 0. );
R.Print( “R = “ );
Ovals->SetUniform( “uModelMatrix”, R );
Matrix4 T;
T.Translatef( 2., 3., 4. );
T.Print ( “T = “ );
Matrix4 P = R * T;
P.Print( “P1 = “ );
P = T * R;
P.Print( “P2 = “ );
Matrix4 RI = R;
472
B. Matrix4 C++ Class
RI.Invert( );
RI.Print( “Rinverse = “ );
RI *= R;
RI.Print( “Rinverse * R = “ );
Matrix4 Comp;
Comp.LoadIdentity( ).Translatef(-A,-B,-C).Scalef(3.,4.,1.).
Translatef(A,B,C).Print(“Composite = “);
Comp.Invert( ).Print( “Composite Inverse = “ );
fprintf( stderr, “Determinant of Composite Inverse = %8.3f\n”,
Comp.GetDeterminant( ) );
473
Vec3 C++ Class
C
It is also helpful to have a 3-element vector class. Here is one called Vec3. These
are its methods:
Vec3( float = 0., float = 0., float = 0. );
Vec3( const Vec3& );
Vec3& operator=( const Vec3& );
Vec3& operator*=( float );
Vec3 operator+( const Vec3& );
Vec3& operator+=( const Vec3& );
Vec3 operator-( const Vec3& ); // binary -
Vec3 operator-( ); // unary -
Vec3& operator-=( const Vec3& );
Vec3 Cross( Vec3 );
float Dot( Vec3 );
void GetVec3( float * );
474
C. Vec3 C++ Class
float Length( );
void Print( char * = “”, FILE * = stderr );
Vec3 Unit( );
Vec3& Unitize( );
Several operators are overloaded so you can use these vectors in expres-
sions, such as
Vec3 a( 1., 2., 3. );
Vec3 b( 4., 5., 6. );
Vec3 e = a + b;
Another class, Point3, is sub-classed from Vec3. A Point3 variable can
use all the same methods a
Vec3 class variable can, but by using the Point3
name you are making it clear that the three-element array is meant to be a
point (with positions) instead of a vector (with directions):
Point3 Q( 1., 2., 0. );
Point3 R( 5., 3., 0. );
Vec3 S = R - Q;
Some of these methods return a reference to the result so that they can be
chained together, like this:
float i = c.Dot( a.Cross(b) );
Vec3 normal = ( R - Q ).Cross( S - Q );
Here are some examples of using the Vec3 class.
#include “vec3.h”
Vec3 b( 1., 2., 3. );
Vec3 c( 5., 6., 7. );
Vec3 d( c );
Vec3 a = c;
a.Unitize( );
a.Print( “a =” );
b.Print( “b =” );
c.Print( “c =” );
d.Print( “d =” );
a = Vec3( 2., -5., 8. );
a.Print( “a =” );

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required