Ok i am going to post the code to this because I have the joint angles wrong and I used the look at command in agk and it does not work.
Maybe someone can see the error I am making in the angles.
bvh.h
#include "Main.h"
#include <vector>
// let the compiler know we're using the AGK namespace
using namespace AGK;
using namespace std;
vector<char *> afile;
// declare our app
vector<vector<float>> motion;
#include <fstream>
#include <string.h>
#include "BVH.h"
#include "3D.h"
//double Matrix[4][4];
///double mat[4][4];
//_3D threed;
struct ALLCHILDREN
{
int objid;
int jointid;
float xdist;
float ydist;
float zdist;
vector<float> xpos;
vector<float> ypos;
vector<float> zpos;
vector<float> xrot;
vector<float> yrot;
vector<float> zrot;
vector<int> allchildren;
int firstchild;
};
ALLCHILDREN allchildren;
vector<ALLCHILDREN> allchildrenlist;
struct OBJECTS
{
int intobjs;
int id;
};
OBJECTS objects;
vector<OBJECTS> objectsl;
void matrixmult(double mat1[][4],double mat2[][4],double result[][4]);
void trans(double X, double Y,double Z,double trans[][4])
{
trans [4][4];
trans [0][1] = trans[0][2] = trans[0][3] =
trans[1][0] = trans[1][2] = trans[1][3] =
trans[2][0] = trans[2][1] = trans[2][3] = 0 ;
trans[0][0] = trans[1][1] = trans[2][2] = trans[3][3] = 1 ;
trans[3][0] = X ;
trans[3][1] = Y ;
trans[3][2] = Z ;
//return M;
}
vector <float> retqut(float theta1, float theta2, float theta3)
{
//agk::
vector<float> retp;
if(theta1==0)
{
theta1=0.0001;
}
if(theta2==0)
{
theta2=0.0001;
}
if(theta3==0)
{
theta3=0.0001;
}
float xpart=agk::Cos(theta1/2)*agk::Cos(theta2/2)*agk::Cos(theta3/2)+
agk::Sin(theta1/2)*agk::Sin(theta2/2)*agk::Sin(theta3/2);
retp.push_back(xpart);
xpart=agk::Sin(theta1/2)*agk::Cos(theta2/2)*agk::Cos(theta3/2)-
agk::Cos(theta1/2)*agk::Sin(theta2/2)*agk::Sin(theta3/2);
retp.push_back(xpart);
xpart=agk::Cos(theta1/2)*agk::Sin(theta2/2)*agk::Cos(theta3/2)+
agk::Sin(theta1/2)*agk::Cos(theta2/2)*agk::Sin(theta3/2);
retp.push_back(xpart);
xpart=agk::Cos(theta1/2)*agk::Cos(theta2/2)*agk::Sin(theta3/2)-
agk::Sin(theta1/2)*agk::Sin(theta2/2)*agk::Cos(theta3/2);
retp.push_back(xpart);
return retp;
}
void retmatx(double thetax,double mat[][4])
{
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
mat[x][y]=0;
}
mat[x][x]=1;
}
mat[1][1]=agk::Cos(thetax);
mat[1][2]=-agk::Sin(thetax);
mat[2][1]=agk::Sin(thetax);
mat[2][2]=agk::Cos(thetax);
}
void retmaty(double thetax,double mat[][4])
{
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
mat[x][y]=0;
}
mat[x][x]=1;
}
mat[0][0]=agk::Cos(thetax);
mat[0][2]=agk::Sin(thetax);
mat[2][0]=-agk::Sin(thetax);
mat[2][2]=agk::Cos(thetax);
}
void retmatz(double thetax,double mat[][4])
{
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
mat[x][y]=0;
}
mat[x][x]=1;
}
mat[0][0]=agk::Cos(thetax);
mat[0][1]=-agk::Sin(thetax);
mat[1][0]=agk::Sin(thetax);
mat[1][1]=agk::Cos(thetax);
}
void retmatzyx(double xmat1[][4],double ymat2[][4],double zmat3[][4],double mat[][4])
{
double retmatzy[4][4];
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
retmatzy[x][y]=0;
mat[x][y]=0;
}
}
matrixmult(zmat3,ymat2,retmatzy);
matrixmult(retmatzy,xmat1,mat);
}
void retmat(double X, double Y, double Z, double W,double mat[][4])
{
for(int i=0;i<4;i++)
{
for(int k=0;k<4;k++)
{
mat[i][k]=0;
}
}
double xx = X * X;
double xy = X * Y;
double xz = X * Z;
double xw = X * W;
double yy = Y * Y;
double yz = Y * Z;
double yw = Y * W;
double zz = Z * Z;
double zw = Z * W;
mat[0][0] = 1 - 2 * ( yy + zz );
mat[1][0] = 2 * ( xy - zw );
mat[2][0] = 2 * ( xz + yw );
mat[0][1] = 2 * ( xy + zw );
mat[1][1] = 1 - 2 * ( xx + zz );
mat[2][1] = 2 * ( yz - xw );
mat[0][2] = 2 * ( xz - yw );
mat[1][2] = 2 * ( yz + xw );
mat[2][2] = 1 - 2 * ( xx + yy );
// mat[3] = mat[7] = mat[11 = mat[12] = mat[13] = mat[14] = 0;
mat[3][3] = 1;
}
bool gluInvertMatrix(double inv[][4], double invOut[][4])
{
double m[16];
m[0]=inv[0][0];
m[1]=inv[0][1];
m[2]=inv[0][2];
m[3]=inv[0][3];
m[4]=inv[1][0];
m[5]=inv[1][1];
m[6]=inv[1][2];
m[7]=inv[1][3];
m[8]=inv[2][0];
m[9]=inv[2][1];
m[10]=inv[2][2];
m[11]=inv[2][3];
m[12]=inv[3][0];
m[13]=inv[3][1];
m[14]=inv[3][2];
m[14]=inv[3][3];
double inv2[16], det;
int i;
inv2[0] = m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10];
inv2[4] = -m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10];
inv2[8] = m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9];
inv2[12] = -m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9];
inv2[1] = -m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10];
inv2[5] = m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10];
inv2[9] = -m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9];
inv2[13] = m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9];
inv2[2] = m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6];
inv2[6] = -m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6];
inv2[10] = m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5];
inv2[14] = -m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5];
inv2[3] = -m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6];
inv2[7] = m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6];
inv2[11] = -m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5];
inv2[15] = m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5];
det = m[0] * inv2[0] + m[1] * inv2[4] + m[2] * inv2[8] + m[3] * inv2[12];
if (det == 0)
return false;
det = 1.0 / det;
double invOut2[16];
for (i = 0; i < 16; i++)
invOut2[i] = inv2[i] * det;
// invOut[0];
invOut[0][0]=invOut2[0];
invOut[0][1]=invOut2[1];
invOut[0][2]=invOut2[2];
invOut[0][3]=invOut2[3];
invOut[1][0]=invOut2[4];
invOut[1][1]=invOut2[5];
invOut[1][2]=invOut2[6];
invOut[1][3]=invOut2[7];
invOut[2][0]=invOut2[8];
invOut[2][1]=invOut2[9];
invOut[2][2]=invOut2[10];
invOut[2][3]=invOut2[11];
invOut[3][0]=invOut2[12];
invOut[3][1]=invOut2[13];
invOut[3][2]=invOut2[14];
invOut[3][3]=invOut2[15];
return true;
}
void matrixmult(double mat1[][4],double mat2[][4],double result[][4])
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
result[i][j] = 0;
for(int k=0; k<4; k++)
{
result[j][i] += mat1[i][k]*mat2[k][j];
}
// cout<<res<<"\t";
}
//cout<<endl;
}
}
BVH::BVH()
{
motion.resize(0);
// motion = NULL;
Clear();
objectsl.resize(0);
// objects.id=0;
}
BVH::BVH( const char * bvh_file_name,float scale )
{
// motion = NULL;
Clear();
// zeroinputmatrix();
Load( bvh_file_name,scale );
}
BVH::~BVH()
{
Clear();
}
void BVH::Clear()
{
int i;
for ( i=0; i<channels.size(); i++ )
delete channels[ i ];
for ( i=0; i<joints.size(); i++ )
delete joints[ i ];
if ( motion.size() != 0 )
motion.resize(0);
is_load_success = false;
file_name = "";
motion_name = "";
num_channel = 0;
channels.clear();
joints.clear();
joint_index.clear();
num_frame = 0;
interval = 0.0;
motion.resize(0);
}
//
// frame
//
void BVH::calcframes2(int frame,int joint,float scale,Joint *jointt)
{
double trantem[4][4];
double rotmat[4][4];
double result[4][4];
double result2[4][4];
double zrott[4][4];
double yrott[4][4];
double xrott[4][4];
double totrot[4][4];
Joint *parent=jointt;
if(allchildrenlist[joint].allchildren.size()!=0)
{
for(int icnt=0;icnt<allchildrenlist[joint].allchildren.size();icnt++)
{
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
trantem[x][y]=0;
rotmat[x][y]=0;
result[x][y]=0;
}
}
int id=allchildrenlist[joint].allchildren[icnt];
float xdist=allchildrenlist[id].xpos[frame];
float ydist=allchildrenlist[id].ypos[frame];
float zdist=allchildrenlist[id].zpos[frame];
float xdist2=allchildrenlist[joint].xpos[frame];
float ydist2=allchildrenlist[joint].ypos[frame];
float zdist2=allchildrenlist[joint].zpos[frame];
float totxdist=xdist-xdist2;
float totydist=ydist-ydist2;
float totzdist=zdist-zdist2;
double xrot,yrot,zrot;
bool torf=true;
for (int i=0; i<jointt->channels.size(); i++ )
{
Channel * channel = jointt->channels[ i ];
int placet=channel->index;
if ( channel->type == X_ROTATION )
{
xrot=motion[frame][channel->index];
}
//glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
{
yrot=motion[frame][channel->index];
}
// glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
{
// zrot=agk::GetObjectAngleZ(allchildrenlist[joint].objid);
zrot=motion[frame][channel->index];
// zrot=zrot;
}
}
vector<float> rr;
agk::SetObjectRotation(allchildrenlist[joint].objid,xrot,yrot,zrot);
if(xrot>=-0.01&&xrot<=.01&&yrot>=-0.01&&yrot<=0.01&&zrot>=-0.01&&zrot<=0.01)
{
for(int xr=0;xr<4;xr++)
{
for(int xs=0;xs<4;xs++)
{
result2[xr][xs]=0;
}
}
result2[0][0]=result2[1][1]=result2[2][2]=result2[3][3]=1;
}
else
{
rr=retqut(xrot,yrot,zrot);
if(rr.size()==4)
{
retmat(rr[1],rr[2],rr[3],rr[0],result2);
}
}
//parent=parent->parent;
/*float x1=agk::GetObjectQuatX(allchildrenlist[joint].objid);
float y1=agk::GetObjectQuatY(allchildrenlist[joint].objid);
float z1=agk::GetObjectQuatZ(allchildrenlist[joint].objid);
float w1=agk::GetObjectQuatW(allchildrenlist[joint].objid);
*/// rr[0];
allchildrenlist[id].xrot[frame]+=xrot;
allchildrenlist[id].yrot[frame]+=zrot;
allchildrenlist[id].zrot[frame]+=yrot;
//agk::SetObjectRotation(allchildrenlist[id].objid,xrot,yrot,zrot);
retmatz(zrot,zrott);
retmatx(xrot,xrott);
retmaty(yrot,yrott);
retmatzyx(xrott,yrott,zrott,totrot);
trans(totxdist,totydist,totzdist,trantem);
matrixmult(trantem,result2,result);
allchildrenlist[id].xpos[frame]=result[0][3]+allchildrenlist[joint].xpos[frame];
allchildrenlist[id].ypos[frame]=result[1][3]+allchildrenlist[joint].ypos[frame];
allchildrenlist[id].zpos[frame]=result[2][3]+allchildrenlist[joint].zpos[frame];
// float angx=motion[frame][parent]*scale;
// float angy=motion[frame][1]*scale;
// float angz = motion[frame][2]*scale;
}
}
}
void BVH::calcframes(int frame,int joint,float scale, Joint *jointt)
{
allchildrenlist[joint].xpos[frame]+=motion[frame][0]*scale;
allchildrenlist[joint].ypos[frame]+=motion[frame][1]*scale;
allchildrenlist[joint].zpos[frame]+=motion[frame][2]*scale;
agk::SetObjectRotation(allchildrenlist[joint].objid,0,0,0);
Joint *tempj=jointt->children[0];
//tempj->index;
int tt=tempj->index;
/*if(allchildrenlist[joint].allchildren.size()!=0)
{
float xp=agk::GetObjectX(allchildrenlist[allchildrenlist[joint].allchildren[0]].xpos[frame]);
float yp=agk::GetObjectY(allchildrenlist[allchildrenlist[joint].allchildren[0]].ypos[frame]);
float zp=agk::GetObjectZ(allchildrenlist[allchildrenlist[joint].allchildren[0]].zpos[frame]);
agk::SetObjectLookAt(allchildrenlist[joint].objid,xp,yp,zp,0);
allchildrenlist[joint].xrot[frame]=agk::GetObjectAngleX(allchildrenlist[joint].objid);
allchildrenlist[joint].yrot[frame]=agk::GetObjectAngleY(allchildrenlist[joint].objid);
allchildrenlist[joint].zrot[frame]=agk::GetObjectAngleZ(allchildrenlist[joint].objid);
// allchildrenlist[joint].zrot[frame]=agk::ATan((tx-ttx)/(ty-tty));
// allchildrenlist[joint].xrot[frame]=agk::ATan((ty-tty)/(tz-ttz));
}*/
/* double trantem[4][4];
double rotmat[4][4];
double result[4][4];
double zrott[4][4];
double yrott[4][4];
double xrott[4][4];
double totrot[4][4];
//Joint *parent=jointt;
if(allchildrenlist[joint].allchildren.size()!=0)
{
for(int icnt=0;icnt<allchildrenlist[joint].allchildren.size();icnt++)
{
int id=allchildrenlist[joint].allchildren[icnt];
agk::SetObjectRotation(allchildrenlist[id].objid,0,0,0);
}
for(int icnt=0;icnt<allchildrenlist[joint].allchildren.size();icnt++)
{
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
trantem[x][y]=0;
rotmat[x][y]=0;
result[x][y]=0;
}
}
int id=allchildrenlist[joint].allchildren[icnt];
float xdist=allchildrenlist[id].xpos[frame];
float ydist=allchildrenlist[id].ypos[frame];
float zdist=allchildrenlist[id].zpos[frame];
float xdist2=allchildrenlist[joint].xpos[frame];
float ydist2=allchildrenlist[joint].ypos[frame];
float zdist2=allchildrenlist[joint].zpos[frame];
float totxdist=xdist2-xdist;
float totydist=ydist2-ydist;
float totzdist=zdist2-zdist;
float xrot=0,yrot=0,zrot=0;
for (int i=0; i<jointt->channels.size(); i++ )
{
Channel * channel = jointt->channels[ i ];
int placet=channel->index;
if ( channel->type == X_ROTATION )
{
xrot=agk::GetObjectAngleX(allchildrenlist[joint].objid)+motion[frame][channel->index];
}
//glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
{
yrot=agk::GetObjectAngleY(allchildrenlist[joint].objid)+motion[frame][channel->index];
}
// glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
{
zrot=agk::GetObjectAngleZ(allchildrenlist[joint].objid)+motion[frame][channel->index];
}
}
allchildrenlist[id].xrot[frame]=xrot;
allchildrenlist[id].yrot[frame]=yrot;
allchildrenlist[id].zrot[frame]=zrot;
agk::SetObjectRotation(allchildrenlist[id].objid,xrot,yrot,zrot);
trans(totxdist,totydist,totzdist,trantem);
retmatz(zrot,zrott);
retmatx(xrot,xrott);
retmaty(yrot,yrott);
retmatzyx(xrott,yrott,zrott,totrot);
matrixmult(trantem,totrot,result);
allchildrenlist[id].xpos[frame]=result[0][3];
allchildrenlist[id].ypos[frame]=result[1][3];
allchildrenlist[id].zpos[frame]=result[2][3];
// float angx=motion[frame][parent]*scale;
// float angy=motion[frame][1]*scale;
// float angz = motion[frame][2]*scale;
}
}
//parent=parent->parent;
//trans(joint->offset[0]*scale,joint->offset[1]*scale,joint->offset[2]*scale,trantem);
*/
}
//
//
//
void BVH::Load( const char * bvh_file_name,float scale )
{
#define BUFFER_LENGTH 1024*4
ifstream file;
char line[ BUFFER_LENGTH ];
char * token;
char separater[] = " :,\t";
vector< Joint * > joint_stack;
Joint * joint = NULL;
Joint * new_joint = NULL;
bool is_site = false;
double x, y ,z;
int i, j;
Clear();
file_name = bvh_file_name;
const char * mn_first = bvh_file_name;
const char * mn_last = bvh_file_name + strlen( bvh_file_name );
if ( strrchr( bvh_file_name, '\\' ) != NULL )
mn_first = strrchr( bvh_file_name, '\\' ) + 1;
else if ( strrchr( bvh_file_name, '/' ) != NULL )
mn_first = strrchr( bvh_file_name, '/' ) + 1;
if ( strrchr( bvh_file_name, '.' ) != NULL )
mn_last = strrchr( bvh_file_name, '.' );
if ( mn_last < mn_first )
mn_last = bvh_file_name + strlen( bvh_file_name );
motion_name.assign( mn_first, mn_last );
file.open( bvh_file_name, ios::in );
if ( file.is_open() == 0 ) return;
if(objectsl.size()!=0)
{
for(int i=0;i<objectsl.size();i++)
{
agk::DeleteObject(objectsl[i].intobjs);
}
}
objectsl.resize(0);
objects.id=0;
allchildrenlist.resize(0);
allchildren.allchildren.resize(0);
while ( ! file.eof() )
{
if ( file.eof() ) goto bvh_error;
file.getline( line, BUFFER_LENGTH );
token = strtok( line, separater );
if ( token == NULL ) continue;
if ( strcmp( token, "{" ) == 0 )
{
int tid=agk::LoadImage("");
objects.intobjs=agk::CreateObjectCylinder(2,.3,5);
agk::SetObjectImage(objects.intobjs,tid,0);
objects.id=objects.id+1;
objectsl.push_back(objects);
// c
joint_stack.push_back( joint );
joint = new_joint;
continue;
}
// ŠÖ߃uƒƒbƒN‚ÌI—¹
if ( strcmp( token, "}" ) == 0 )
{
// agk::DeleteObject(objectsl[objectsl.size()-1].intobjs);
//agk::DeleteObject(objectsl[objectsl.size()-2].intobjs);
//agk::DeleteObject(objectsl[objectsl.size()-3].intobjs);
// objectsl.pop_back();
// Œ»Ý‚ÌŠÖß‚ðƒXƒ^ƒbƒN‚©‚çŽæ‚èo‚·
joint = joint_stack.back();
joint_stack.pop_back();
is_site = false;
continue;
}
if ( ( strcmp( token, "ROOT" ) == 0 ) ||
( strcmp( token, "JOINT" ) == 0 ) )
{
new_joint = new Joint();
new_joint->index = joints.size();
new_joint->parent = joint;
new_joint->has_site = false;
new_joint->offset[0] = 0.0; new_joint->offset[1] = 0.0; new_joint->offset[2] = 0.0;
new_joint->site[0] = 0.0; new_joint->site[1] = 0.0; new_joint->site[2] = 0.0;
joints.push_back( new_joint );
if ( joint )
joint->children.push_back( new_joint );
// ŠÖß–¼‚̓ǂݞ‚Ý
token = strtok( NULL, "" );
while ( *token == ' ' ) token ++;
new_joint->name = token;
joint_index[ new_joint->name ] = new_joint;
continue;
}
if ( ( strcmp( token, "End" ) == 0 ) )
{
new_joint = joint;
is_site = true;
continue;
}
if ( strcmp( token, "OFFSET" ) == 0 )
{
token = strtok( NULL, separater );
x = token ? atof( token ) : 0.0;
token = strtok( NULL, separater );
y = token ? atof( token ) : 0.0;
token = strtok( NULL, separater );
z = token ? atof( token ) : 0.0;
if ( is_site )
{
joint->has_site = true;
joint->site[0] = x;
joint->site[1] = y;
joint->site[2] = z;
}
else
{
joint->offset[0] = x;
joint->offset[1] = y;
joint->offset[2] = z;
}
continue;
}
if ( strcmp( token, "CHANNELS" ) == 0 )
{
token = strtok( NULL, separater );
joint->channels.resize( token ? atoi( token ) : 0 );
for ( i=0; i<joint->channels.size(); i++ )
{
Channel * channel = new Channel();
channel->joint = joint;
channel->index = channels.size();
channels.push_back( channel );
joint->channels[ i ] = channel;
token = strtok( NULL, separater );
if ( strcmp( token, "Xrotation" ) == 0 )
channel->type = X_ROTATION;
else if ( strcmp( token, "Yrotation" ) == 0 )
channel->type = Y_ROTATION;
else if ( strcmp( token, "Zrotation" ) == 0 )
channel->type = Z_ROTATION;
else if ( strcmp( token, "Xposition" ) == 0 )
channel->type = X_POSITION;
else if ( strcmp( token, "Yposition" ) == 0 )
channel->type = Y_POSITION;
else if ( strcmp( token, "Zposition" ) == 0 )
channel->type = Z_POSITION;
}
}
if ( strcmp( token, "MOTION" ) == 0 )
break;
}
file.getline( line, BUFFER_LENGTH );
token = strtok( line, separater );
if ( strcmp( token, "Frames" ) != 0 ) goto bvh_error;
token = strtok( NULL, separater );
if ( token == NULL ) goto bvh_error;
num_frame = atoi( token );
file.getline( line, BUFFER_LENGTH );
token = strtok( line, ":" );
if ( strcmp( token, "Frame Time" ) != 0 ) goto bvh_error;
token = strtok( NULL, separater );
if ( token == NULL ) goto bvh_error;
interval = atof( token );
num_channel = channels.size();
// motion = new double[ num_frame * num_channel ];
for ( i=0; i<num_frame; i++ )
{
vector<float> tt;
file.getline( line, BUFFER_LENGTH );
token = strtok( line, separater );
// motion.push_back(i);
for ( j=0; j<num_channel; j++ )
{
if ( token == NULL )
goto bvh_error;
float ttemp = atof( token );
tt.push_back(ttemp);
token = strtok( NULL, separater );
}
motion.push_back(tt);
}
file.close();
is_load_success = true;
agk::DeleteObject(objectsl[objectsl.size()-1].intobjs);
agk::DeleteObject(objectsl[objectsl.size()-2].intobjs);
agk::DeleteObject(objectsl[objectsl.size()-3].intobjs);
agk::DeleteObject(objectsl[objectsl.size()-4].intobjs);
agk::DeleteObject(objectsl[objectsl.size()-5].intobjs);
agk::DeleteObject(objectsl[objectsl.size()-6].intobjs);
agk::DeleteObject(objectsl[objectsl.size()-7].intobjs);
Joint *tjoint=joints[1];
Joint *parent=joints[0];
allchildren.objid=objectsl[0].intobjs;
allchildren.jointid=0;
allchildren.xdist=parent->offset[0];
allchildren.ydist=parent->offset[1];
allchildren.zdist=parent->offset[2];
allchildren.xpos.resize(num_frame);
allchildren.ypos.resize(num_frame);
allchildren.zpos.resize(num_frame);
allchildren.xrot.resize(num_frame);
allchildren.yrot.resize(num_frame);
allchildren.zrot.resize(num_frame);
allchildren.firstchild= parent->children[0]->index;
allchildrenlist.push_back(allchildren);
parent=joints[1];
float distx=0,disty=0,distz=0;
int asize=0;
for(int yycnt=1; yycnt<objectsl.size()-7;yycnt++)
{
parent=joints[yycnt];
//tjoint=joints[yycnt];
distx=parent->offset[0];
disty=parent->offset[1];
distz=parent->offset[2];
asize=tjoint->index;
if(parent->children.size()!=0)
{
allchildren.firstchild=parent->children[0]->index;
}
allchildrenlist.push_back(allchildren);
while(parent->parent!=NULL)
{
parent=parent->parent;
asize=parent->index;
for(int ic=0;ic<num_frame;ic++)
{
allchildrenlist[allchildrenlist.size()-1].xpos[ic]=distx*scale;
allchildrenlist[allchildrenlist.size()-1].ypos[ic]=disty*scale;
allchildrenlist[allchildrenlist.size()-1].zpos[ic]=distz*scale;
allchildrenlist[allchildrenlist.size()-1].xrot[ic]=0;
allchildrenlist[allchildrenlist.size()-1].yrot[ic]=0;
allchildrenlist[allchildrenlist.size()-1].zrot[ic]=0;
}
allchildrenlist[allchildrenlist.size()-1].objid=objectsl[yycnt].intobjs;
allchildrenlist[allchildrenlist.size()-1].jointid=yycnt;
allchildrenlist[asize].allchildren.push_back(objectsl[yycnt].id-1);
distx+=parent->offset[0];
disty+=parent->offset[1];
distz+=parent->offset[2];
asize=tjoint->index;
}
distx=0;
disty=0;
distz=0;
}
for(int ic=1;ic<num_frame; ic++)
{
for(int ii=1;ii<joints.size();ii++)
{
for(int ik=0; ik<joints.size();ik++)
{
if(allchildrenlist[ik].allchildren.size()==ii)
{
calcframes2(ic,ik,scale,joints[ik]);
}
}
}
for(int ik=0; ik<joints.size();ik++)
{
calcframes(ic,ik,scale,joints[0]);
}
}
// allchildren.jointid=0;
return;
bvh_error:
file.close();
}
//vector BVH::XVECTOR(
//
//
//
void BVH::RenderFigure( int frame_no, float scale )
{
RenderFigure( joints[ 0 ], frame_no, scale,0 );
}
void BVH::exportbvhfile()
{
agk::OpenToWrite(3,"bvhexport.file",0);
agk::WriteInteger(3,allchildrenlist[0].xpos.size()-1);
for(int ic=0;ic<allchildrenlist[0].xpos.size();ic++)
{
agk::WriteInteger(3,ic);
for(int ik=0;ik<allchildrenlist.size();ik++)
{
agk::WriteInteger(3,allchildrenlist[ik].jointid);
agk::WriteFloat(3,allchildrenlist[ik].xpos[ic]);
agk::WriteFloat(3,allchildrenlist[ik].ypos[ic]);
agk::WriteFloat(3,allchildrenlist[ik].zpos[ic]);
agk::WriteFloat(3,allchildrenlist[ik].xrot[ic]);
agk::WriteFloat(3,allchildrenlist[ik].yrot[ic]);
agk::WriteFloat(3,allchildrenlist[ik].zrot[ic]);
}
}
agk::CloseFile(3);
}
void BVH::RenderFigure( const Joint * joint, int frame_no, float scale,int iis )
{
for(int ic=0;ic<allchildrenlist.size();ic++)
{
agk::SetObjectPosition(allchildrenlist[ic].objid,allchildrenlist[ic].xpos[frame_no],allchildrenlist[ic].ypos[frame_no],allchildrenlist[ic].zpos[frame_no]);
agk::SetObjectRotation(allchildrenlist[ic].objid,allchildrenlist[ic].xrot[frame_no],allchildrenlist[ic].xrot[frame_no],allchildrenlist[ic].zrot[frame_no]);
if(allchildrenlist[ic].allchildren.size()!=0)
{
//allchildren.firstchild
float xp=agk::GetObjectX(allchildrenlist[allchildrenlist[ic].firstchild].objid);
float yp=agk::GetObjectY(allchildrenlist[allchildrenlist[ic].firstchild].objid);
float zp=agk::GetObjectZ(allchildrenlist[allchildrenlist[ic].firstchild].objid);
agk::SetObjectLookAt(allchildrenlist[ic].objid,xp,yp,zp,0);
}
}
/* if ( joint->parent == NULL )
{
// initfinalmatrix();
agk::SetObjectPosition(objectsl[0].intobjs,motion[frame_no][0]*scale,motion[frame_no][1]*scale,motion[frame_no][2]*scale);
agk::SetObjectRotation(objectsl[0].intobjs,motion[frame_no][5],motion[frame_no][4],motion[frame_no][3]);
// zeroinputmatrix();
// Joint* tjoint=joint;
}
allchildrenlist[0].allchildren[0];*/
/*else
{
if(joint->name=="LHipJoint"||joint->name=="RHipJoint"||joint->name=="LowerBack")
{
Joint *tjoint=joint->parent;
double quantmat[4]={0,0,0,0};
double trantem[4][4];
double rotmat[4][4];
double result[4][4];
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
trantem[x][y]=0;
rotmat[x][y]=0;
result[x][y]=0;
}
}
trans(joint->offset[0]*scale,joint->offset[1]*scale,joint->offset[2]*scale,trantem);
//quantmat[0]=agk::GetObjectQuatX(objectsl[tjoint->index].intobjs);
//quantmat[1]=agk::GetObjectQuatX(objectsl[tjoint->index].intobjs);
//quantmat[2]=agk::GetObjectQuatX(objectsl[tjoint->index].intobjs);
//quantmat[3]=agk::GetObjectQuatX(objectsl[tjoint->index].intobjs);
//retmat(quantmat[0],quantmat[1],quantmat[2],quantmat[3],rotmat);
matrixmult(rotmat,trantem,result);
if(joint->name=="LowerBack")
{
agk::SetObjectPosition(objectsl[joint->index].intobjs,result[0][3]+motion[frame_no][0]*scale-1,result[1][3]+motion[frame_no][1]*scale,result[2][3]+motion[frame_no][2]*scale);
}
else if(joint->name=="LHipJoint")
{
agk::SetObjectPosition(objectsl[joint->index].intobjs,result[0][3]+motion[frame_no][0]*scale+1,result[1][3]+motion[frame_no][1]*scale,result[2][3]+motion[frame_no][2]*scale);
tjoint=tjoint->children[0];
int xdif=tjoint->offset[0]+joint->offset[0];
int ydif=tjoint->offset[1]+joint->offset[1];
int zdif=tjoint->offset[2]+joint->offset[2];
trans(xdif,ydif,zdif,trantem);
matrixmult(trantem,rotmat,result);
//float xp=agk::GetObjectX(objectsl[joint->parent->index].intobjs];
//float yp=agk::GetObjectY(objectsl[joint->parent->index].intobjs];
//float zp=agk::GetObjectZ(objectsl[joint->parent->index].intobjs];
allchildren.LeftUpLegnc[0]=result[0][3];
allchildren.LeftUpLegnc[1]=result[1][3];
allchildren.LeftUpLegnc[2]=result[2][3];
}
else if(joint->name=="RHipJoint")
{
agk::SetObjectPosition(objectsl[joint->index].intobjs,1,1,0);
}
float xrot=0,yrot=0,zrot=0;
for (int i=0; i<tjoint->channels.size(); i++ )
{
Channel * channel = tjoint->channels[ i ];
int placet=channel->index;
if ( channel->type == X_ROTATION )
{
xrot=motion[frame_no][channel->index];
}
//glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
{
yrot=motion[frame_no][channel->index];
}
// glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
{
zrot=motion[frame_no][channel->index];
}
}
agk::SetObjectRotation(objectsl[joint->index].intobjs,xrot,yrot,
zrot);
//joint->offset[0]=agk::GetObjectX(joint->index);
//joint->offset[1]=agk::GetObjectY(joint->index);
//joint->offest[2]=agk::GetObjectZ(joint->index);
}
if(joint->name=="LeftUpLeg"||joint->name=="RightUpLeg"||joint->name=="Spine")
{
double distx=0;
double disty=0;
double distz=0;
//distx=joint->offset[0]*scale*joint->offset[0]*scale
Joint *tjoint=joint->parent;
float xrot=0,yrot=0,zrot=0, xrot2=0,yrot2=0,zrot2=0;
for(int i=0;i<tjoint->channels.size();i++)
{
Channel * channel2=tjoint->channels[i];
int placet2=channel2->index;
if ( channel2->type == X_ROTATION )
{
xrot2=motion[frame_no][channel2->index];
}
//glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel2->type == Y_ROTATION )
{
yrot2=motion[frame_no][channel2->index];
}
// glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel2->type == Z_ROTATION )
{
zrot2=motion[frame_no][channel2->index];
}
}
tjoint=tjoint->parent;
for (int i=0; i<tjoint->channels.size(); i++ )
{
Channel * channel = tjoint->channels[ i ];
int placet=channel->index;
if ( channel->type == X_ROTATION )
{
xrot=motion[frame_no][channel->index];
}
//glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
{
yrot=motion[frame_no][channel->index];
}
// glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
{
zrot=motion[frame_no][channel->index];
}
}
agk::SetObjectRotation(objectsl[joint->index].intobjs,xrot+xrot2,yrot+yrot2,zrot+zrot2);
double quantmat[4]={0,0,0,0};
double trantem[4][4];
double rotmat[4][4];
double result[4][4];
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
trantem[x][y]=0;
rotmat[x][y]=0;
result[x][y]=0;
}
}
double totxdist=joint->offset[0]*scale;
double totydist=joint->offset[1]*scale;
double totzdist=joint->offset[2]*scale;
double tempx;
double tempy;
double tempz;
double zrott[4][4],xrott[4][4],yrott[4][4];
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
rotmat[x][y]=0;
result[x][y]=0;
}
}
retmatz(zrot2+zrot,zrott);
retmaty(yrot2+yrot,yrott);
retmatz(xrot2+xrot,xrott);
retmatzyx(xrott,yrott,zrott,rotmat);
if(joint->name=="LeftUpLeg"||joint->name=="RightUpLeg")
{
Joint *ttjoint=joint->children[0];
while (ttjoint->name!="LeftToeBase" && ttjoint->name!="RightToeBase" )
{
totxdist=ttjoint->offset[0];
totydist=ttjoint->offset[1];
totzdist=ttjoint->offset[2];
trans(totxdist,totydist,totzdist,trantem);
matrixmult(trantem,rotmat,result);
if(ttjoint->name=="LeftLeg")
{
allchildren.LeftLegnc[0]=result[0][3];
allchildren.LeftLegnc[1]=result[1][3];
allchildren.LeftLegnc[2]=result[2][3];
agk::SetObjectPosition(objectsl[ttjoint->index].intobjs,result[0][3]*scale+motion[frame_no][0]*scale,result[1][3]*scale+motion[frame_no][1]*scale,result[2][3]*scale+motion[frame_no][2]*scale);
}
if(ttjoint->name=="RightLeg")
{
allchildren.LeftLegnc[0]=result[0][3];
allchildren.LeftLegnc[1]=result[1][3];
allchildren.LeftLegnc[2]=result[2][3];
agk::SetObjectPosition(objectsl[ttjoint->index].intobjs,result[0][3]*scale+motion[frame_no][0]*scale,result[1][3]*scale+motion[frame_no][1]*scale,result[2][3]*scale+motion[frame_no][2]*scale);
}
ttjoint=ttjoint->children[0];
}
}
//RenderFigure( joint->children[ i ], frame_no, scale,i );
trans(joint->offset[0]*scale,joint->offset[1]*scale,joint->offset[2]*scale,trantem);
/*quantmat[0]=agk::GetObjectQuatX(objectsl[tjoint->index].intobjs);
quantmat[1]=agk::GetObjectQuatY(objectsl[tjoint->index].intobjs);
quantmat[2]=agk::GetObjectQuatZ(objectsl[tjoint->index].intobjs);
quantmat[3]=agk::GetObjectQuatW(objectsl[tjoint->index].intobjs);
retmat(quantmat[0],quantmat[1],quantmat[2],quantmat[3],rotmat); */
/*if(objectsl[joint->index].intobjs==10007)
{
agk::Print("true");
}
matrixmult(trantem,rotmat,result);
if(joint->name=="Spine")
{
agk::SetObjectPosition(objectsl[joint->index].intobjs,motion[frame_no][0]*scale,motion[frame_no][1]*scale,motion[frame_no][2]*scale);
}
else if(joint->name=="LeftUpLeg")
{
agk::SetObjectPosition(objectsl[joint->index].intobjs,result[0][3]+motion[frame_no][0]*scale,result[1][3]+motion[frame_no][1]*scale,result[2][3]+motion[frame_no][2]*scale);
}
else if(joint->name=="RightUpLeg")
{
agk::SetObjectPosition(objectsl[joint->index].intobjs,result[0][3]+motion[frame_no][0]*scale,result[1][3]+motion[frame_no][1]*scale,result[2][3]+motion[frame_no][2]*scale);
}
}
/*agk::Print(agk::GetObjectQuatX(objectsl[tjoint->index].intobjs));
agk::Print(agk::GetObjectQuatY(objectsl[tjoint->index].intobjs));
agk::Print(agk::GetObjectQuatZ(objectsl[tjoint->index].intobjs));
agk::Print(agk::GetObjectQuatW(objectsl[tjoint->index].intobjs));
agk::Print(agk::GetObjectAngleX(objectsl[tjoint->index].intobjs));
agk::Print(agk::GetObjectAngleY(objectsl[tjoint->index].intobjs));
agk::Print(agk::GetObjectAngleZ(objectsl[tjoint->index].intobjs));
//float length =agk::Sqrt(joint->offset[0]*scale*joint->offset[0]+joint->offset[1]*scale*joint->offset[1]*scale+
// joint->offset[2]*scale*joint->offset[2]*scale);
/* agk::SetObjectPosition(objectsl[joint->index].intobjs,
agk::GetObjectQuatX(objectsl[tjoint->index].intobjs)*length,
agk::GetObjectQuatY(objectsl[tjoint->index].intobjs)*length,
agk::GetObjectQuatZ(objectsl[tjoint->index].intobjs)*length);*/
//}
/*if(joint->name=="LeftToeBase")
{
vector <Joint *> jvec;
Joint *tjoint=joint->parent;
jvec.push_back(tjoint);
while(tjoint->parent!=NULL )
{
Joint *tjoint=joint->parent;
jvec.push_back(tjoint);
}
// agk::SetObjectPosition(objectsl[joint->index].intobjs,2,2,0);
}
joint->allchildren.push_back
if(joint->name=="RightToeBase")
{
}*/
for (int i=0; i<joint->children.size(); i++ )
{
RenderFigure( joint->children[ i ], frame_no, scale,i );
}
/*
const Joint * tjoint=joint;
vector<Joint const *> jointvec;
if ( joint->children.size() == 0 )
{
if(joint->name=="RightToeBase"||joint->name=="LeftToeBase")
{
jointvec.push_back(joint);
while(tjoint->parent!=NULL)
{
jointvec.push_back(tjoint->parent);
tjoint=tjoint->parent;
}
zeroinputmatrix();
// initrotationmatrix();
tjoint=jointvec[jointvec.size()-1];
for (int i=0; i<tjoint->channels.size(); i++ )
{
Channel * channel = tjoint->channels[ i ];
int placet=channel->index;
if ( channel->type == X_ROTATION )
{
setrotationaboutx(motion[frame_no][placet]);
// setrotationaboutx(data[channel->index]);//xrot
mulitplyMatrixrot2();
multiplyMatrixOtoI();
// multiplyMatrixOtoI2();
}
//glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
{
setrotationabouty(motion[frame_no][placet]);
// setrotationabouty(data[channel->index]);//xrot
//multiplyMatrixOtoI();
mulitplyMatrixrot2();
multiplyMatrixOtoI();
// multiplyMatrixOtoI2();
}
// glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
{
setrotationaboutz(motion[frame_no][placet]);
// setrotationaboutz(data[channel->index]);//xrot
// multiplyMatrixOtoI();
mulitplyMatrixrot2();
multiplyMatrixOtoI();
// multiplyMatrixOtoI2();
//glRotatef( data[ channel->index ], 0.0f, 0.0f, 1.0f );
}
}
//initrotationmatrix();
//setxyztransformation(motion[frame_no][0]*scale,motion[frame_no][1]*scale,motion[frame_no][2]*scale);
//multiplyMatrixrot();
//addtransrot();
//l tjoint=jointvec[jointvec.size()-1];
jointvec.pop_back();
while(jointvec.size()!=0)
{
zeroinputmatrix();
tjoint=jointvec[jointvec.size()-1];
for (int i=0; i<tjoint->channels.size(); i++ )
{
Channel * channel = tjoint->channels[ i ];
int placet=channel->index;
if ( channel->type == X_ROTATION )
{
setrotationaboutx(motion[frame_no][placet]);
// setrotationaboutx(data[channel->index]);//xrot
mulitplyMatrixrot2();
multiplyMatrixOtoI();
// multiplyMatrixOtoI2();
}
//glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
{
setrotationabouty(motion[frame_no][placet]);
// setrotationabouty(data[channel->index]);//xrot
//multiplyMatrixOtoI();
mulitplyMatrixrot2();
multiplyMatrixOtoI();
// multiplyMatrixOtoI2();
}
// glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
{
setrotationaboutz(motion[frame_no][placet]);
// setrotationaboutz(data[channel->index]);//xrot
// multiplyMatrixOtoI();
mulitplyMatrixrot2();
multiplyMatrixOtoI();
// multiplyMatrixOtoI2();
//glRotatef( data[ channel->index ], 0.0f, 0.0f, 1.0f );
}
}
// settemprotmatrix();
setxyztransformation(tjoint->offset[0]*scale,tjoint->offset[1]*scale,tjoint->offset[2]*scale);
multiplyMatrixrot();
// multiplyMatrixOtoI();
addtransrot();
jointvec.pop_back();
}
agk::SetObjectPosition(objectsl[joint->index].intobjs,finalmatrix2[0][3]+joint->site[0]*scale,finalmatrix2[1][3]+joint->site[1]*scale,finalmatrix2[2][3]+joint->site[2]*scale);
}
else
{
float xpos=0;
float ypos=0;
float zpos=0;
xpos=joint->offset[0]*scale;
ypos=joint->offset[1]*scale;
zpos=joint->offset[2]*scale;
while(tjoint->parent!=NULL)
{
xpos+=tjoint->parent->offset[0]*scale;
ypos+=tjoint->parent->offset[1]*scale;
zpos+=tjoint->parent->offset[2]*scale;
tjoint=tjoint->parent;
}
// float xposs=data[0]*scale+xpos+joint->site[ 0 ] * scale;
// agk::SetObjectPosition(objectsl[joint->index].intobjs,xposs,data[1]*scale+ypos+joint->site[ 1 ] * scale,data[2]*scale+zpos+joint->site[ 2 ] * scale);
}
}
else if ( joint->children.size() == 1 )
{
//if(joint->name=="Left
if(joint->name=="Neck1"||joint->name=="LeftArm"||joint->name=="RightArm")
{
Joint * child = joint->children[ 0 ];
Joint * parent = joint->parent->parent;
Joint * child1=parent->children[1];
Joint * child2=parent->children[2];
float xpos=0;
float ypos=0;
float zpos=0;
xpos=joint->offset[0]*scale;
ypos=joint->offset[1]*scale;
zpos=joint->offset[2]*scale;
while(tjoint->parent!=NULL)
{
xpos+=tjoint->parent->offset[0]*scale;
ypos+=tjoint->parent->offset[1]*scale;
zpos+=tjoint->parent->offset[2]*scale;
tjoint=tjoint->parent;
}
// agk::SetObjectPosition(objectsl[joint->index].intobjs,xpos+data[0]*scale,ypos+data[1]*scale,zpos+data[2]*scale);
}
else
{
Joint * child = joint->children[ 0 ];
float xpos=0;
float ypos=0;
float zpos=0;
xpos=joint->offset[0]*scale;
ypos=joint->offset[1]*scale;
zpos=joint->offset[2]*scale;
while(tjoint->parent!=NULL)
{
xpos+=tjoint->parent->offset[0]*scale;
ypos+=tjoint->parent->offset[1]*scale;
zpos+=tjoint->parent->offset[2]*scale;
tjoint=tjoint->parent;
}
// agk::SetObjectPosition(objectsl[joint->index].intobjs,xpos+data[0]*scale,ypos+data[1]*scale,zpos+data[2]*scale);
}
}
else if ( joint->children.size() > 1 )
{
int i=0;
float center[ 3 ] = { 0.0f, 0.0f, 0.0f };
for ( i=0; i<joint->children.size(); i++ )
{
Joint * child = joint->children[ i ];
center[ 0 ] += child->offset[ 0 ];
center[ 1 ] += child->offset[ 1 ];
center[ 2 ] += child->offset[ 2 ];
}
center[ 0 ] /= joint->children.size() + 1;
center[ 1 ] /= joint->children.size() + 1;
center[ 2 ] /= joint->children.size() + 1;
if(joint->parent==NULL)
{
// agk::Print(data[0]*scale);
// agk::SetObjectPosition(objectsl[joint->index].intobjs,data[0]*scale,data[1]*scale,data[2]*scale);
}
else
{
Joint * child = joint->children[ 0 ];
float xpos=0;
float ypos=0;
float zpos=0;
xpos=center[0]*scale;
ypos=center[1]*scale;
zpos=center[2]*scale;
while(tjoint->parent!=NULL)
{
xpos+=tjoint->parent->offset[0]*scale;
ypos+=tjoint->parent->offset[1]*scale;
zpos+=tjoint->parent->offset[2]*scale;
tjoint=tjoint->parent;
}
// agk::SetObjectPosition(objectsl[joint->index].intobjs,xpos+data[0]*scale,ypos+data[1]*scale,zpos+data[2]*scale);
}
//if(joint->name==leftcollar
// RenderBone( 0.0f, 0.0f, 0.0f, center[ 0 ] * scale, center[ 1 ] * scale, center[ 2 ] * scale,i );
// setxyztransformation(center[0]*scale+data[ 0 ] * scale+data[0]*scale,center[1]*scale+data[ 1 ] * scale,center[2]*scale+data[ 2 ] * scale+data[2]*scale);
// multiplyMatrixrot();
// agk::SetObjectPosition(objectsl[joint->index].intobjs,outputmatrix[0][3],outputmatrix[1][3],outputmatrix[2][3]);
}
else
{
// agk::SetObjectPosition(objectsl[joint->index].intobjs,data[0]*scale,data[1]*scale,data[2]*scale);
}
for (int i=0; i<joint->children.size(); i++ )
{
RenderFigure( joint->children[ i ], frame_no, scale,i );
}
//zeroinputmatrix();
// glPopMatrix();*/
}
#ifndef _BVH_H_
#define _BVH_H_
#include <vector>
#include <map>
#include <string>
using namespace std;
//
//
//
class BVH
{
public:
enum ChannelEnum
{
X_ROTATION, Y_ROTATION, Z_ROTATION,
X_POSITION, Y_POSITION, Z_POSITION
};
struct Joint;
struct Channel
{
Joint * joint;
ChannelEnum type;
int index;
};
struct Joint
{
string name;
int index;
Joint * parent;
vector< Joint * > children;
float offset[3];
bool has_site;
double site[3];
vector< Channel * > channels;
};
private:
bool is_load_success;
string file_name;
string motion_name;
int num_channel;
vector< Channel * > channels;
vector< Joint * > joints;
map< string, Joint * > joint_index;
int num_frame;
double interval;
public:
BVH();
BVH( const char * bvh_file_name,float scale );
~BVH();
void exportbvhfile();
void Clear();
void Load( const char * bvh_file_name,float scale );
public:
bool IsLoadSuccess() const { return is_load_success; }
const string & GetFileName() const { return file_name; }
const string & GetMotionName() const { return motion_name; }
const int GetNumJoint() const { return joints.size(); }
const Joint * GetJoint( int no ) const { return joints[no]; }
const int GetNumChannel() const { return channels.size(); }
const Channel * GetChannel( int no ) const { return channels[no]; }
const Joint * GetJoint( const string & j ) const {
map< string, Joint * >::const_iterator i = joint_index.find( j );
return ( i != joint_index.end() ) ? (*i).second : NULL; }
const Joint * GetJoint( const char * j ) const {
map< string, Joint * >::const_iterator i = joint_index.find( j );
return ( i != joint_index.end() ) ? (*i).second : NULL; }
int GetNumFrame() const { return num_frame; }
double GetInterval() const { return interval; }
// double GetMotion( int f, int c ) const { return motion[ f*num_channel + c ]; }
// void SetMotion( int f, int c, double v ) { motion[ f*num_channel + c ] = v; }
public:
void RenderFigure( int frame_no, float scale = 1.0f );
static void RenderFigure( const Joint * root, int frame, float scale = 1.0f,int ii=0 );
static void RenderBone( float x0, float y0, float z0, float x1, float y1, float z1,int i );
private:
void calcframes(int frame,int joint,float scale, Joint *jointt);
void calcframes2(int frame,int joint,float scale,Joint *jointt);
};
#endif // _BVH_H_
/*
Name: Box2D - Box2D
Description: Getting started with Box2D
*/
// include our main header file
#include "Main.h"
#include <vector>
#include "bvh.h"
#include "resource.h"
// let the compiler know we're using the AGK namespace
using namespace AGK;
using namespace std;
bool boolbase1=false;
float scaleofimport=0.4f;
extern HWND g_hWnd;
BOOL CALLBACK CREATESCALE(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK CREATESCALE(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
bool torf;
char buffer[100];
int place=0;
HWND hwnd;
switch(message)
{
case WM_INITDIALOG:
{
boolbase1=false;
SetDlgItemText(hwndDlg,IDC_EDIT1,itoa(0,buffer,10));
//hwnd =GetDlgItem(hwndDlg,IDC_RADIO1);
//SendMessageA(hwnd,BM_SETCHECK,1,(LPARAM)NULL);
//SetDlgItemText(hwndDlg,IDC_EDIT1,itoa(spritealpha,buffer,10));
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
{
boolbase1=true;
//conbool=true;
GetDlgItemText(hwndDlg,IDC_EDIT1,buffer,255);
scaleofimport=atof(buffer);
//hwnd = GetDlgItem(hwndDlg, IDC_LIST1);
EndDialog(hwndDlg, wParam);
return true;
}
case IDCANCEL:
boolbase1=false;
// DeleteObject(MyBitmap);
EndDialog(hwndDlg, wParam);
return true;
}
}
return false;
}
static float camera_yaw = 0.0f;
static float camera_pitch = -20.0f;
static float camera_distance = 5.0f;
static int drag_mouse_r = 0;
static int drag_mouse_l = 0;
static int drag_mouse_m = 0;
static int last_mouse_x, last_mouse_y;
static int win_width, win_height;
bool on_animation = true;
float animation_time = 0.0f;
int frame_no = 2;
// declare our app
app App;
BVH * bvh = NULL;
void app::Begin ( void )
{
agk::SetVirtualResolution(1500,800);
//agk::UpdateDeviceSize(2000,800);
//agk::setde
agk::SetCameraPosition(1,agk::GetCameraX(1),agk::GetCameraY(1)+16,-40);
//l agk::SetCameraRotation(1,agk::GetCameraAngleX(1),agk::GetCameraAngleY(1),agk::GetCameraZ(1)+20);
//agk::SetCameraRotation(1,20,-5,0);
//agk::SetCameraRotation(1,0,10,0);
//
}
void app::Loop ( void )
{
agk::Print("Press L to load bvh file");
agk::Print("Press E to export bvh file");
agk::Print("Press S to scale bvh file");
char *file;
for(int icnt=0;icnt<255;icnt++)
{
if(agk::GetRawKeyState(icnt))
{
agk::Print(icnt);
}
}
//DialogBox(NULL,MAKEINTRESOURCE(IDD_OBJECTSCALE),g_hWnd,(DLGPROC) OBJECTSCALE);
if(agk::GetRawKeyPressed(83)==1)
{
DialogBox(NULL,MAKEINTRESOURCE(IDD_DIALOG1),g_hWnd,(DLGPROC) CREATESCALE);
}
if(agk::GetRawKeyPressed(69)==1)
{
if ( bvh )
{
bvh->exportbvhfile();
}
}
if(agk::GetRawKeyPressed(76)==1)
{
file=agk::ChooseRawFile("*.bvh");
if(file[0]!='\0')
{
delete bvh;
bvh=new BVH(file,scaleofimport);
frame_no=0;
}
}
if(agk::GetRawKeyPressed(187)==1)
{
agk::SetCameraPosition(1,agk::GetCameraX(1),agk::GetCameraY(1)-.5,agk::GetCameraZ(1)+1);
}
if(agk::GetRawKeyPressed(189)==1)
{
agk::SetCameraPosition(1,agk::GetCameraX(1),agk::GetCameraY(1)+.5,agk::GetCameraZ(1)-1);
}
if(agk::GetRawKeyPressed(189)==1)
{
agk::SetCameraPosition(1,agk::GetCameraX(1),agk::GetCameraY(1)+.5,agk::GetCameraZ(1)-1);
}
if(agk::GetRawKeyState(79)==1)
{
agk::SetCameraRotation(1,agk::GetCameraAngleX(1),agk::GetCameraAngleY(1),agk::GetCameraZ(1)+.1);
}
if(agk::GetRawKeyState(80)==1)
{
agk::SetCameraRotation(1,agk::GetCameraAngleX(1),agk::GetCameraAngleY(1),agk::GetCameraZ(1)-.1);
}
if ( on_animation )
{
if ( bvh )
{
if(frame_no==bvh->GetNumFrame())
{
frame_no=0;
}
bvh->RenderFigure( frame_no, .4f ); //agk::Print(agk::GetCameraZ(1));
}
if ( bvh )
{
frame_no = frame_no+1;
if(frame_no==bvh->GetInterval())
{
frame_no=0;
}
//frame_no = frame_no % bvh->GetNumFrame();
//bvh->
// agk::Print(frame_no);
}
else
frame_no = 0;
// agk::Print(agk::GetCameraY(1));
// agk::Print(agk::GetCameraX(1));
}
// this function gets called on a continual basis, use this to update your
// game logic and deal with input and other events
// we call agk::Sync to update the screen, when this happens our sprite
// will get drawn on screen
agk::Sync ( );
}
void app::End ( void )
{
//agk::Print(agk::GetFolder());
// this function gets called when the game ends, in here you can clean up
// all resources and deal with things like saving game data
}
I almost have it just the angles between the joints and if I have someone else look at it then maybe they can help fix it. I am also posting this because the cords are correct, and I think that animation needs to be in agk.
Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps. Developed the tiled map engine seen on the showcase. Veteran for the military.