Animate part of the object using Lib3s

40 views
Skip to first unread message

truyenle

unread,
Jul 28, 2011, 11:35:31 AM7/28/11
to lib3ds
have been trying to figure out is how to animate part of an object (a
man) using lib3ds. I mean I want to animate the man is walking but
don’t know where to start and I couldn’t find any post for this.

I have a little bit difficult in understand void
lib3ds_file_eval(Lib3dsFile *file, float t); and apply to my
application since I’m very new to Lib3ds and .3ds objects.

So I have an 3ds object, and here is the function to load the object

Lib3dsFile * pModel = lib3ds_file_open(“path_to_the_object”);

if( pModel == NULL ) {
cout << "Failed to load model " << endl;
return false;
}

I’m using OpenGL to display the lists for the model, so I need to
build a function to generate the display list
GLuint mnDisplayList[0] = _GenerateDisplayList( pModel); // polygon

The code of _GenerateDisplayList( pModel) is below but when I got the
mnDispalyList[0], I just can render the object as by calling
glCallList( mnDisplayList[0] ); as follows:

glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLfloat tmp[]={0.5, 0.5, 0.5, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, tmp);
glLightfv(GL_LIGHT0, GL_DIFFUSE, tmp);
GLfloat tmp2[]={1.0, 0.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, tmp2);
GLfloat tmp3[]={1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tmp3);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glCallList( mnDisplayList[0] );


Here is the code of the _GenerateDisplayList() function
GLuint _GenerateDisplayList( Lib3dsFile * pModel)
{
assert( pModel );

//some defaults
static GLfloat ambiant[4] = {0.2f, 0.2f, 0.2f, 1.0f};
static GLfloat diffuse[4] = {0.8f, 0.8f, 0.8f, 1.0f};
static GLfloat spectal[4] = {0.0f, 0.0f, 0.0f, 1.0f};

GLuint list = glGenLists(1);

if ( list !=0 )
{
glNewList( list , GL_COMPILE);

// Loop through every mesh
for(int mm = 0; mm < pModel->nmeshes; mm++ )
{
Lib3dsMesh * mesh = pModel->meshes[mm];
Vector3D * normals = new Vector3D[ 3* mesh->nfaces];

//calculate the normals
lib3ds_mesh_calculate_vertex_normals( mesh, normals );

glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);

// Begin drawing
glBegin( GL_TRIANGLES );

// Go through all of the faces (polygons) of the object and draw
them
for( int ff = 0; ff < mesh->nfaces; ff++ )
{
Lib3dsFace & face = mesh->faces[ff];

glColor3ub(255, 255, 255);

if( face.material >= 0 )
{
Lib3dsMaterial * material = pModel-
>materials[ face.material ];

glMaterialfv(GL_FRONT, GL_AMBIENT, material->ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material->diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material->specular);
float s = pow(2.0f, 10.0f*material->shininess); // make
shininess an openGL value
if( s > 128.0f ) {
s = 128.0f;
}
glMaterialf(GL_FRONT, GL_SHININESS, s);

///@TODO fix the color, material and light settings.
glColor3fv( material->diffuse );
}
else // assign default material values
{
glMaterialfv(GL_FRONT, GL_AMBIENT, ambiant);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, spectal);
}

// Go through each corner of the triangle and draw it.
for( int vv = 0; vv < 3; vv++ )
{
// Get the index for each point of the face
int index = face.index[vv];

// Give OpenGL the normal for this vertex.
Vector3D & v3Norm = normals[ 3*ff + vv ];
glNormal3fv( v3Norm );

// Draw in the current vertex of the object (Corner of
current face)
glVertex3fv( mesh->vertices[ index ] );
}
}

glEnd();

}

glEndList();
}

return list;
}

So where should I use this lib3ds_file_eval() function to get the
animation runnig? I just can’t figure out. Sorry for my newbie in this
libraries that this question might be stupid.
Reply all
Reply to author
Forward
0 new messages