# Bvh player - blank!

This topic is 1062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Solved by recompiling the program,

Update:

Does anyone know what this part does?

double  side_x, side_y, side_z;
side_x = up_y * dir_z - up_z * dir_y;
side_y = up_z * dir_x - up_x * dir_z;
side_z = up_x * dir_y - up_y * dir_x;

length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
if ( length < 0.0001 ) {
side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;
}
side_x /= length;  side_y /= length;  side_z /= length;

up_x = dir_y * side_z - dir_z * side_y;
up_y = dir_z * side_x - dir_x * side_z;
up_z = dir_x * side_y - dir_y * side_x;

GLdouble  m[16] = { side_x, side_y, side_z, 0.0,
up_x,   up_y,   up_z,   0.0,
dir_x,  dir_y,  dir_z,  0.0,
0.0,    0.0,    0.0,    1.0 };

I want to convert that into Direct3D equivalence.

thanks

Edited by lucky6969b

##### Share on other sites

Does anyone know what this part does?

That code computes the orientation matrix from a forward direction "dir" and a principal upward direction "up": Given 2 (normalized) vectors "up" and "dir", the vector "side" is computed as the cross-product of both and then normalized. Then "up" is recomputed, again using the cross-product. In the end m[16] is an orientation matrix with its z vector pointing along the direction of "dir", and its y vector pointing roughly in the direction of "up".

side = up x dir

length = length( side )

if length is very short then side = vector( 1, 0, 0 ), length = 1

side = side / length  # normalization

up = dir x side

m = matrix( side, up, dir )

It may be used for the orthonormalization of an orientation matrix. It may be part of a "look at" function.

Edited by haegarr

##### Share on other sites
Thanks, Merry Eve Day Merry Christmas and Happy New Year!

##### Share on other sites

transpose it to get directx compilant matrix

Edited by WiredCat

##### Share on other sites

transpose it to get directx compilant matrix

In memory, OpenGL and DirectX are the same. Only their multiplication functions are different, which is why multiplication happens in the opposite order.
The way his memory is now is fine for DirectX, except that he needs to use floats, not doubles.

L. Spiro

##### Share on other sites

Just to extend a bit on what L.Spiro mention. The only difference between opengl and direct x is that if you are using their build in fixed pipeline matrix operation. The multiplication is done by multiplying column*row and in the case of direct x it is done by row*column. But the memory layout is the same on both. Element 12,13,14 represent position for both gl/dx. That is the reason why in DirectX you would do something like Rotate*Scale*Translate but in GL it would be Translate*Rotate*Scale to get the same result. Row major multiplication are perform left to right and column major are perfomed right to left.

Edited by BornToCode

##### Share on other sites

I am getting a chunk of mess out of this.

The commented code is for your reference.

void  BVH::RenderFigure( const Joint * joint, const float * data, float scale )
{

D3DXMATRIX matTrans;
if (joint->parent == NULL) {
D3DXMatrixTranslation(&matTrans, data[0]*scale, data[1]*scale, data[2]*scale);

}
else
{
D3DXMatrixTranslation(&matTrans, joint->offset[0] * scale, joint->offset[1] * scale, joint->offset[2] * scale);
}

int  i, j;
D3DXMATRIX rotX, rotY, rotZ;
for ( i=0; i<joint->channels.size(); i++ )
{
Channel *  channel = joint->channels[ i ];
if (channel->type == X_ROTATION)
D3DXMatrixRotationAxis(&rotX, &D3DXVECTOR3(1.0, 0.0, 0.0), data[channel->index]);
else if ( channel->type == Y_ROTATION )
D3DXMatrixRotationAxis(&rotY, &D3DXVECTOR3(0.0, 1.0, 0.0), data[channel->index]);
else if ( channel->type == Z_ROTATION )
D3DXMatrixRotationAxis(&rotY, &D3DXVECTOR3(0.0, 0.0, 1.0), data[channel->index]);
}

D3DXMATRIX matIn = rotX * rotY * rotZ * matTrans;

d3d::m_pDevice->SetTransform(D3DTS_WORLD, &matIn);

if ( joint->children.size() == 0 )
{
RenderBone( 0.0f, 0.0f, 0.0f, joint->site[0] * scale, joint->site[1] * scale, joint->site[2] * scale, matIn );
}

if ( joint->children.size() == 1 )
{
Joint *  child = joint->children[ 0 ];
RenderBone( 0.0f, 0.0f, 0.0f, child->offset[0] * scale, child->offset[1] * scale, child->offset[2] * scale, matIn );
}

if ( joint->children.size() > 1 )
{

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;

RenderBone(    0.0f, 0.0f, 0.0f, center[0] * scale, center[1] * scale, center[2] * scale, matIn );

for ( i=0; i<joint->children.size(); i++ )
{
Joint *  child = joint->children[ i ];
RenderBone(    center[ 0 ] * scale, center[ 1 ] * scale, center[ 2 ] * scale,
child->offset[ 0 ] * scale, child->offset[ 1 ] * scale, child->offset[ 2 ] * scale, matIn );
}
}

for ( i=0; i<joint->children.size(); i++ )
{
RenderFigure( joint->children[ i ], data, scale );
}

#if 0
glPushMatrix();

// global translations
if ( joint->parent == NULL )
{
glTranslatef( data[ 0 ] * scale, data[ 1 ] * scale, data[ 2 ] * scale );
}
// global translations
else
{
glTranslatef( joint->offset[ 0 ] * scale, joint->offset[ 1 ] * scale, joint->offset[ 2 ] * scale );
}

// global rotations
int  i, j;
for ( i=0; i<joint->channels.size(); i++ )
{
Channel *  channel = joint->channels[ i ];
if ( channel->type == X_ROTATION )
glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
glRotatef( data[ channel->index ], 0.0f, 0.0f, 1.0f );
}

if ( joint->children.size() == 0 )
{
RenderBone( 0.0f, 0.0f, 0.0f, joint->site[ 0 ] * scale, joint->site[ 1 ] * scale, joint->site[ 2 ] * scale );
}

if ( joint->children.size() == 1 )
{
Joint *  child = joint->children[ 0 ];
RenderBone( 0.0f, 0.0f, 0.0f, child->offset[ 0 ] * scale, child->offset[ 1 ] * scale, child->offset[ 2 ] * scale );
}

if ( joint->children.size() > 1 )
{

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;

RenderBone(    0.0f, 0.0f, 0.0f, center[ 0 ] * scale, center[ 1 ] * scale, center[ 2 ] * scale );

for ( i=0; i<joint->children.size(); i++ )
{
Joint *  child = joint->children[ i ];
RenderBone(    center[ 0 ] * scale, center[ 1 ] * scale, center[ 2 ] * scale,
child->offset[ 0 ] * scale, child->offset[ 1 ] * scale, child->offset[ 2 ] * scale );
}
}

for ( i=0; i<joint->children.size(); i++ )
{
RenderFigure( joint->children[ i ], data, scale );
}

glPopMatrix();
#endif
}

void  BVH::RenderBone( float x0, float y0, float z0, float x1, float y1, float z1, const D3DXMATRIX& matIn)
{
// forward orientation
float dir_x = x1 - x0;
float dir_y = y1 - y0;
float dir_z = z1 - z0;
float bone_length = sqrt(dir_x*dir_x + dir_y*dir_y + dir_z*dir_z);

D3DXMATRIX matTrans;

D3DXMatrixTranslation(&matTrans, x0, y0, z0);

float  length;
length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
if ( length < 0.0001 ) {
dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;
}
dir_x /= length;  dir_y /= length;  dir_z /= length;

// up orientation
float  up_x, up_y, up_z;
up_x = 0.0;
up_y = 1.0;
up_z = 0.0;

// right orientation
float  side_x, side_y, side_z;
side_x = up_y * dir_z - up_z * dir_y;
side_y = up_z * dir_x - up_x * dir_z;
side_z = up_x * dir_y - up_y * dir_x;

length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
if ( length < 0.0001 ) {
side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;
}
side_x /= length;  side_y /= length;  side_z /= length;

// recompute up again
up_x = dir_y * side_z - dir_z * side_y;
up_y = dir_z * side_x - dir_x * side_z;
up_z = dir_x * side_y - dir_y * side_x;

D3DXMATRIX m;
m._11 = side_x; m._12 = side_y; m._13 = side_z; m._14 = 0.0f;
m._21 = up_x;   m._22 = up_y;   m._23 = up_z;   m._24 = 0.0f;
m._31 = dir_x;  m._32 = dir_y;  m._33 = dir_z;  m._34 = 0.0f;
m._41 = 0.0f;    m._42 = 0.0f;    m._43 = 0.0f;    m._44 = 1.0f;

D3DXMatrixTranspose(&m, &m);

D3DXMATRIX resM;

D3DXMatrixIdentity(&resM);

resM = m * matTrans * matIn;

float radius = 0.01;
float slices = 8.0;
float stack = 3.0;

CComPtr<ID3DXMesh> pMesh = NULL;

&pMesh, NULL);

d3d::m_pDevice->SetTransform(D3DTS_WORLD, &resM);
pMesh->DrawSubset(0);

#if 0
GLdouble  dir_x = x1 - x0;
GLdouble  dir_y = y1 - y0;
GLdouble  dir_z = z1 - z0;
GLdouble  bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );

if ( quad_obj == NULL )

// not affecting the original matrix by pushing the original matrix onto the stack
// but still using it
glPushMatrix();

// multiply the current matrix with the translation matrix i.e: result = currentmatrix * trans
glTranslated( x0, y0, z0 );

double  length;
length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
if ( length < 0.0001 ) {
dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;
}
dir_x /= length;  dir_y /= length;  dir_z /= length;

GLdouble  up_x, up_y, up_z;
up_x = 0.0;
up_y = 1.0;
up_z = 0.0;

double  side_x, side_y, side_z;
side_x = up_y * dir_z - up_z * dir_y;
side_y = up_z * dir_x - up_x * dir_z;
side_z = up_x * dir_y - up_y * dir_x;

length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
if ( length < 0.0001 ) {
side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;
}
side_x /= length;  side_y /= length;  side_z /= length;

up_x = dir_y * side_z - dir_z * side_y;
up_y = dir_z * side_x - dir_x * side_z;
up_z = dir_x * side_y - dir_y * side_x;

GLdouble  m[16] = { side_x, side_y, side_z, 0.0,
up_x,   up_y,   up_z,   0.0,
dir_x,  dir_y,  dir_z,  0.0,
0.0,    0.0,    0.0,    1.0 };

// multiply the current matrix with m i.e.: result = current * m
glMultMatrixd( m );

// so the result matrix however is
// result = transm * m * currentm;

GLdouble slices = 8.0;
GLdouble stack = 3.0;

glPopMatrix();
#endif
}
Edited by lucky6969b

##### Share on other sites

Modified version, but still doesn't work.

Just see a plain stick which is close to the original shape, but it isn't.

Thanks

Jack

void  BVH::RenderFigure( const Joint * joint, const float * data, float scale )
{
D3DXMATRIX matIn;

D3DXMatrixIdentity(&matIn);

int  i, j;
D3DXMATRIX rotX, rotY, rotZ;

// aggregate each channel
for (i = 0; i<joint->channels.size(); i++)
{
Channel *  channel = joint->channels[i];
if (channel->type == X_ROTATION) {
D3DXMatrixIdentity(&rotX);
D3DXMatrixRotationAxis(&rotX, &D3DXVECTOR3(1.0, 0.0, 0.0), RAD2DEG(data[channel->index]));
matIn *= rotX;
}
else if (channel->type == Y_ROTATION) {
D3DXMatrixIdentity(&rotY);
D3DXMatrixRotationAxis(&rotY, &D3DXVECTOR3(0.0, 1.0, 0.0), RAD2DEG(data[channel->index]));
matIn *= rotY;
}
else if (channel->type == Z_ROTATION) {
D3DXMatrixIdentity(&rotZ);
D3DXMatrixRotationAxis(&rotZ, &D3DXVECTOR3(0.0, 0.0, 1.0), RAD2DEG(data[channel->index]));
matIn *= rotZ;
}

}

// aggregate translation
D3DXMATRIX matTrans;
D3DXMatrixIdentity(&matTrans);
if (joint->parent == NULL) {
D3DXMatrixTranslation(&matTrans, data[0]*scale, data[1]*scale, data[2]*scale);

}
else
{
D3DXMatrixTranslation(&matTrans, joint->offset[0] * scale, joint->offset[1] * scale, joint->offset[2] * scale);
}

matIn *= matTrans;

// world transformation for this bone
d3d::m_pDevice->SetTransform(D3DTS_WORLD, &matIn);

if ( joint->children.size() == 0 )
{
RenderBone(0.0f, 0.0f, 0.0f, joint->site[0] * scale, joint->site[1] * scale, joint->site[2] * scale);
}

if ( joint->children.size() == 1 )
{
Joint *  child = joint->children[ 0 ];
RenderBone(0.0f, 0.0f, 0.0f, child->offset[0] * scale, child->offset[1] * scale, child->offset[2] * scale);
}

if ( joint->children.size() > 1 )
{

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;

RenderBone(0.0f, 0.0f, 0.0f, center[0] * scale, center[1] * scale, center[2] * scale);

for ( i=0; i<joint->children.size(); i++ )
{
Joint *  child = joint->children[ i ];
RenderBone(    center[ 0 ] * scale, center[ 1 ] * scale, center[ 2 ] * scale,
child->offset[0] * scale, child->offset[1] * scale, child->offset[2] * scale);
}
}

// render the rest of skeleton , data is affected by frame transitions, and proprogate to
// children in some contiguous data
for ( i=0; i<joint->children.size(); i++ )
{
RenderFigure( joint->children[ i ], data, scale );
}

#if 0
glPushMatrix();

// global translations
if ( joint->parent == NULL )
{
glTranslatef( data[ 0 ] * scale, data[ 1 ] * scale, data[ 2 ] * scale );
}
// global translations
else
{
glTranslatef( joint->offset[ 0 ] * scale, joint->offset[ 1 ] * scale, joint->offset[ 2 ] * scale );
}

// global rotations
int  i, j;
for ( i=0; i<joint->channels.size(); i++ )
{
Channel *  channel = joint->channels[ i ];
if ( channel->type == X_ROTATION )
glRotatef( data[ channel->index ], 1.0f, 0.0f, 0.0f );
else if ( channel->type == Y_ROTATION )
glRotatef( data[ channel->index ], 0.0f, 1.0f, 0.0f );
else if ( channel->type == Z_ROTATION )
glRotatef( data[ channel->index ], 0.0f, 0.0f, 1.0f );
}

if ( joint->children.size() == 0 )
{
RenderBone( 0.0f, 0.0f, 0.0f, joint->site[ 0 ] * scale, joint->site[ 1 ] * scale, joint->site[ 2 ] * scale );
}

if ( joint->children.size() == 1 )
{
Joint *  child = joint->children[ 0 ];
RenderBone( 0.0f, 0.0f, 0.0f, child->offset[ 0 ] * scale, child->offset[ 1 ] * scale, child->offset[ 2 ] * scale );
}

if ( joint->children.size() > 1 )
{

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;

RenderBone(    0.0f, 0.0f, 0.0f, center[ 0 ] * scale, center[ 1 ] * scale, center[ 2 ] * scale );

for ( i=0; i<joint->children.size(); i++ )
{
Joint *  child = joint->children[ i ];
RenderBone(    center[ 0 ] * scale, center[ 1 ] * scale, center[ 2 ] * scale,
child->offset[ 0 ] * scale, child->offset[ 1 ] * scale, child->offset[ 2 ] * scale );
}
}

for ( i=0; i<joint->children.size(); i++ )
{
RenderFigure( joint->children[ i ], data, scale );
}

glPopMatrix();
#endif
}

void  BVH::RenderBone( float x0, float y0, float z0, float x1, float y1, float z1)
{
// forward orientation
float dir_x = x1 - x0;
float dir_y = y1 - y0;
float dir_z = z1 - z0;
float bone_length = sqrt(dir_x*dir_x + dir_y*dir_y + dir_z*dir_z);

D3DXVECTOR3 dir(dir_x, dir_y, dir_z);

float  length = D3DXVec3Length(&dir);
//length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );

if ( length < 0.0001 ) {
//dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;
dir = D3DXVECTOR3(0, 0, 1);
length = 1.0f;
}
//dir_x /= length;  dir_y /= length;  dir_z /= length;
D3DXVec3Normalize(&dir, &dir);

// up orientation
//float  up_x, up_y, up_z;
D3DXVECTOR3 up;
up.x = 0.0;
up.y = 1.0;
up.z = 0.0;

// right orientation
//float  side_x, side_y, side_z;
D3DXVECTOR3 side;
//side_x = up_y * dir_z - up_z * dir_y;
//side_y = up_z * dir_x - up_x * dir_z;
//side_z = up_x * dir_y - up_y * dir_x;
D3DXVec3Cross(&side, &up, &dir);

//length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
length = D3DXVec3Length(&side);
if ( length < 0.0001 ) {
//side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;
side = D3DXVECTOR3(1.0, 0.0, 0.0);
length = 1.0f;
}
//side_x /= length;  side_y /= length;  side_z /= length;
D3DXVec3Normalize(&side, &side);

// recompute up again
//up_x = dir_y * side_z - dir_z * side_y;
//up_y = dir_z * side_x - dir_x * side_z;
//up_z = dir_x * side_y - dir_y * side_x;
D3DXVec3Cross(&up, &dir, &side);

D3DXMATRIX m;
m._11 = side.x; m._12 = side.y; m._13 = side.z; m._14 = 0.0f;
m._21 = up.x;   m._22 = up.y;   m._23 = up.z;   m._24 = 0.0f;
m._31 = dir.x;  m._32 = dir.y;  m._33 = dir.z;  m._34 = 0.0f;
m._41 = 0.0f;    m._42 = 0.0f;    m._43 = 0.0f;    m._44 = 1.0f;

//D3DXMatrixTranspose(&m, &m);

D3DXMATRIX resM;

D3DXMatrixIdentity(&resM);

resM *= m;

D3DXMATRIX matTrans;

D3DXMatrixIdentity(&matTrans);

D3DXMatrixTranslation(&matTrans, x0, y0, z0);

resM *= matTrans;

float radius = 0.2;
float slices = 8.0;
float stack = 3.0;

CComPtr<ID3DXMesh> pMesh = NULL;

&pMesh, NULL);

d3d::m_pDevice->SetTransform(D3DTS_WORLD, &resM);
pMesh->DrawSubset(0);

#if 0
GLdouble  dir_x = x1 - x0;
GLdouble  dir_y = y1 - y0;
GLdouble  dir_z = z1 - z0;
GLdouble  bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );

if ( quad_obj == NULL )

// not affecting the original matrix by pushing the original matrix onto the stack
// but still using it
glPushMatrix();

// multiply the current matrix with the translation matrix i.e: result = currentmatrix * trans
glTranslated( x0, y0, z0 );

double  length;
length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
if ( length < 0.0001 ) {
dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;
}
dir_x /= length;  dir_y /= length;  dir_z /= length;

GLdouble  up_x, up_y, up_z;
up_x = 0.0;
up_y = 1.0;
up_z = 0.0;

double  side_x, side_y, side_z;
side_x = up_y * dir_z - up_z * dir_y;
side_y = up_z * dir_x - up_x * dir_z;
side_z = up_x * dir_y - up_y * dir_x;

length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
if ( length < 0.0001 ) {
side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;
}
side_x /= length;  side_y /= length;  side_z /= length;

up_x = dir_y * side_z - dir_z * side_y;
up_y = dir_z * side_x - dir_x * side_z;
up_z = dir_x * side_y - dir_y * side_x;

GLdouble  m[16] = { side_x, side_y, side_z, 0.0,
up_x,   up_y,   up_z,   0.0,
dir_x,  dir_y,  dir_z,  0.0,
0.0,    0.0,    0.0,    1.0 };

// multiply the current matrix with m i.e.: result = current * m
glMultMatrixd( m );

// so the result matrix however is
// result = transm * m * currentm;

GLdouble slices = 8.0;
GLdouble stack = 3.0;

glPopMatrix();
#endif
}

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013164
×