N64® Functions Menu

al - Audio Library Functions
gDP - DP GBI Macros
gdSP - General GBI Macros
gSP - SP GBI Macros
gt - Turbo Microcode RDP
gu - Graphics Utilities
Math - Math Functions
nuSys - NuSystem
os - N64 Operating System
sp - Sprite Library Functions
uh - Host to Target IO
64DD - N64 Disk Drive

Nintendo® Confidential

   

gSPMatrix

Format
#include "gbi.h"

/*
 4x4 Matrix, Fixed-point s15.16 format.
 First 8 words are integer portion of
 the 4x4 matrix. Last 8 words are the
 fraction portion of the 4x4 matrix
*/

typedef long   Mtx_t[4][4];

typedef {
   Mtx_t     m;
   long long int  force_structure_alignment;
} Mtx;

gSPMatrix(Gfx *gdl,
   Mtx *matrix,
   unsigned char param)
gsSPMatrix(Mtx *matrix,
   unsigned char param)
Arguments
  • gdl is the pointer to the graphics display list
  • matrix is the pointer to the 4x4 fixed-point matrix (see note below about format)
  • param are the bit OR'd parameters to the matrix macro (G_MTX_PROJECTION, G_MTX_MODELVIEW, G_MTX_MUL, G_MTX_LOAD, G_MTX_NOPUSH)
What This Macro Does
It inserts a matrix operation in the display list. The parameters allow you to select which matrix stack to use (projection or model view), where to load or concatenate, and whether or not to push the matrix stack. The following parameters are bit OR'ed together:
  • G_MTX_PROJECTION G_MTX_MODELVIEW - specifies whether the matrix operation will be performed on the projection or the model view matrix.
  • G_MTX_MUL - concatenates the matrix (m) with the top of the matrix stack.
  • G_MTX_LOAD - loads the matrix (m) onto the top of the matrix stack.
  • G_MTX_NOPUSH - specifies do not push the matrix stack prior to matrix operations
  • G_MTX_PUSH - specifies push the matrix stack prior to matrix operations
Matrix Format
The format of the fixed-point matrices may seem a little awkward to the application programmer because it is optimized for the RSP geometry engine. This unusual format is hidden in the graphics utility libraries and not usually exposed to the application programmer, but in some cases (static matrix declarations or direct element manipulation) it is necessary to understand the format.

The integer and fractional components of the matrix elements are separated. The first 8 words (16 shorts) hold the 16-bit integer elements, the second 8 words (16 shorts) hold the 16-bit fractional elements. The fact that the Mtx type is declared as a long [4][4] array is slightly misleading. For example, to declare a static identity matrix, use code similar to this:
#include "gbi.h"
static Mtx ident =
{
/* integer portion: */
0x00010000, 0x00000000,
0x00000001, 0x00000000,
0x00000000, 0x00010000,
0x00000000, 0x00000001,

/* fractional portion: */
0x00000000, 0x00000000,
0x00000000, 0x00000000,
0x00000000, 0x00000000,
0x00000000, 0x00000000,
};
To force the translation elements of a matrix to be (10.5, 20.5, 30.5), use code similar to this:

#include "gbi.h"

mat.m[1][2] =
   (10 << 16) | (20);
mat.m[1][3] =
   (30 << 16) | (1);

mat.m[3][2] =
   (0x8000 << 16) | (0x8000);
mat.m[3][3] =
   (0x8000 << 16) | (0);
Note
Matrix concatenation in the RSP geometry engine is done using 32-bit integer arithmetic. A 32 x 32 bit multiply results in a 64-bit number. Only the middle 32 bits of this 64-bit result are kept for the new matrix. Therefore, when concatenating matrices, remember about the resulting fixed-point numerical error.

For example, to retain maximum precision, the number ranges must be similar. Large-scale and translate parameters can decrease the transformation precision. Because rotation and projection matrices require quite a bit of fractional accuracy, these fractions may get tossed out if multiplied against large integer numbers.

Each concatenation results in the rounding of the LSB of each matrix term. This means that each concatenation injects 1/2 LSB of error into the matrix. To keep full precision, concatenate matrices in floating-point on the processor and just load the result into the RSP.

Performance
Each G_MTX_MODELVIEW matrix operation has an implicit matrix multiplication even if you specify G_MTX_LOAD. This is the combined model view (M) and projection (P) matrix that is necessary for the vertex transformation to use a single matrix during transformation.

You can optimize this by concatenating modeling matrices on the CPU and then putting the viewing (V) and projection matrices on the projection stack. By doing this, you only incur the single MxVP matrix concatenation each time you load a modeling matrix. Furthermore, the application has more information on how to do a cheap hack for modeling matrix concatenation. For example, if you want to combine a single axis rotation with a translation, just place the coefficients in the correct entries of the resulting matrix.

See Also
gSPPopMatrix
gSPPerspNormalize
gSPInsertMatrix
gSPForceMatrix



Nintendo® Confidential

Warning: all information in this document is confidential and covered by a non-disclosure agreement. You are responsible for keeping this information confidential and protected. Nintendo will vigorously enforce this responsibility.


Copyright © 1998
Nintendo of America Inc. All rights reserved
Nintendo and N64 are registered trademarks of Nintendo
Last updated January 1998