Stick Ninja
The following is a set of C++ routines for performing skeletal animation, including ninjitsu. These routines rely on our free matrix library. You may
use them freely in your code. If you have any comments on them, please
email us.

Download demo and full source
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;
#include "matrixlib.h"
struct Joint {
float offset[3];
int numchannels;
vector v;
};
vector< vector > frames;
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
void drawit(const Joint &j, const Matrix &m, int fctr);
void drawit_helper(Joint j, Matrix m, int &rotateoffset, int fctr);
Joint createjoint(FILE *fp, char *jointname);
int count(Joint &j);
Joint createjoint(FILE *fp)
{
Joint j;
char line[512];
j.numchannels = 0;
char *p;
while (fgets(line, 512, fp))
{
strlwr(line);
p = strstr(line, "offset");
if (p) sscanf(p+7, "%f %f %f", &j.offset[0], &j.offset[1], &j.offset[2]);
p = strstr(line, "channels");
if (p) sscanf(p+9, "%d", &j.numchannels);
p = strstr(line, "joint");
if (p) j.v.insert(j.v.end(), createjoint(fp));
if (strstr(line, "end site")) j.v.insert(j.v.end(), createjoint(fp));
if (strstr(line, "}")) return j;
}
return j;
}
int count(Joint &j)
{
int numvalues=0;
vector::iterator iter = j.v.begin();
while (iter != j.v.end()) numvalues+=count(*iter++);
return numvalues+j.numchannels;
}
vector > readframes(FILE *fp, int jointcount)
{
const maxlinelen = 9999;
char line[maxlinelen];
int numberofframes=0;
while (fgets(line, maxlinelen, fp))
{
if (strstr(line, "Frame Time")) break;
if (strstr(line, "Frames:")) numberofframes = atoi(strstr(line, "Frames:")+8);
}
vector< vector > frames;
for (int fctr = 0; fctr < numberofframes; fctr++)
{
vector v;
float rotation;
for (int ctr = 0; ctr < jointcount; ctr++) {
fscanf(fp, "%f", &rotation);
v.insert(v.end(),rotation);
}
frames.insert(frames.end(), v);
}
return frames;
}
double degtorad(double degrees)
{
double radians = degrees/360.0 * 3.14159265358979323846 * 2;
return radians;
}
void drawit(const Joint &j, const Matrix &m, int fctr)
{
int rotateoffset = 0;
drawit_helper(j, m, rotateoffset, fctr);
}
void drawit_helper(Joint j, Matrix m, int &rotateoffset, int fctr)
{
vector::iterator iter = j.v.begin();
if (j.numchannels == 3) {
float angle1 = degtorad(frames[fctr][rotateoffset]);
float angle2 = degtorad(frames[fctr][rotateoffset+1]);
float angle3 = degtorad(frames[fctr][rotateoffset+2]);
m*=buildrotationmatrix(angle1, 0, 0, 1);
m*=buildrotationmatrix(angle2, 1, 0, 0);
m*=buildrotationmatrix(angle3, 0, 1, 0);
Vector originvec = m*buildvector(0, 0, 0);
Vector vec = m*buildvector(j.offset[0], j.offset[1], j.offset[2]);
glBegin(GL_LINES);
glVertex3f(originvec[0], originvec[1], originvec[2]);
glVertex3f(vec[0], vec[1], vec[2]);
glEnd();
m*=buildtranslationmatrix(j.offset[0], j.offset[1], j.offset[2]);
rotateoffset+=3;
while (iter != j.v.end()) drawit_helper(*iter++, m, rotateoffset, fctr);
}
else if (j.numchannels==6)
{
m*=buildtranslationmatrix(frames[fctr][rotateoffset], frames[fctr][rotateoffset+1], frames[fctr][rotateoffset+2]);
float angle1 = degtorad(frames[fctr][rotateoffset+3]);
float angle2 = degtorad(frames[fctr][rotateoffset+4]);
float angle3 = degtorad(frames[fctr][rotateoffset+5]);
m*=buildrotationmatrix(angle1, 0, 0, 1);
m*=buildrotationmatrix(angle2, 1, 0, 0);
m*=buildrotationmatrix(angle3, 0, 1, 0);
m*=buildtranslationmatrix(j.offset[0], j.offset[1], j.offset[2]);
rotateoffset+=6;
while (iter != j.v.end()) drawit_helper(*iter++, m, rotateoffset, fctr);
}
}
bool go = false;
void CALLBACK cbFunct(UINT , UINT , DWORD_PTR , DWORD_PTR , DWORD_PTR )
{
go=true;
}
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
FILE *fp = fopen("ninja.bvh", "r");
if (!fp) {
MessageBox(NULL, "could not open bvh file", "error", MB_OK);
exit(1);
}
Joint j = createjoint(fp);
frames = readframes(fp, count(j));
fclose(fp);
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 556, 556,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
int mikectr = 0;
timeSetEvent(33, 0, cbFunct, NULL, TIME_PERIODIC);
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
while (go == false) {Sleep(1);}
/* OpenGL animation code goes here */
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluPerspective(90.0f,1 ,0.1f,100.0f);
Matrix m = buildtranslationmatrix(0, 0, -3);
m*=buildscalematrix(.03, .03, .03);
m*=buildtranslationmatrix(-67, -67, 0);
m*=buildscalematrix(1.5, 1.5, 1.5);
drawit(j, m, mikectr++);
mikectr%=frames.size();
SwapBuffers (hDC);
go=false;
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}