Click here to Skip to main content
15,887,214 members
Articles / Multimedia / DirectX

Loading and Rendering Milkshape 3D Models with Animation and Skinning

Rate me:
Please Sign up or sign in to vote.
4.81/5 (13 votes)
18 May 2011CPOL5 min read 88.5K   4.6K   31  
This article shows how to load Milkshape ms3d binary files, animate and display them with OpenGL
/*
------------------------------------------------------------------------------------------
Author:
Boroş Tiberiu
Administrator Sistem                                                    Tel.: +40745310081
Institutul de Cercetări pentru Inteligenţă Artificială,
Academia Română
Calea 13 Septembrie, Nr. 13, CASA ACADEMIEI, Bucuresti 050711, ROMANIA
Tel.: +40-(0)213188103
Fax: +40-(0)213188142
E-mail: office@racai.ro
Web: http://www.racai.ro
------------------------------------------------------------------------------------------
 */
attribute vec4 position;
attribute vec3 normal;
attribute vec4 weight;
attribute vec4 index;
attribute float numBones;

uniform mat4 matGlobal[100];
uniform mat4 matGlobalSkeleton[100];
uniform vec4 color;
uniform vec4 lightPos;

float DotProduct(in vec4 x, in vec4 y) {
    return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
}

void VectorRotate(in vec4 in1, in mat4 in2, out vec4 rez) {
    rez[0] = DotProduct(in1, in2[0]);
    rez[1] = DotProduct(in1, in2[1]);
    rez[2] = DotProduct(in1, in2[2]);
}

void VectorIRotate(in vec4 in1, in mat4 in2, out vec4 rez) {
    rez[0] = in1[0] * in2[0][0] + in1[1] * in2[1][0] + in1[2] * in2[2][0];
    rez[1] = in1[0] * in2[0][1] + in1[1] * in2[1][1] + in1[2] * in2[2][1];
    rez[2] = in1[0] * in2[0][2] + in1[1] * in2[1][2] + in1[2] * in2[2][2];
}

void VectorTransform(in vec4 in1,in mat4 in2, out vec4 rez) {
    rez[0] = DotProduct(in1, in2[0]) + in2[0][3];
    rez[1] = DotProduct(in1, in2[1]) + in2[1][3];
    rez[2] = DotProduct(in1, in2[2]) + in2[2][3];
}

void VectorITransform(in vec4 in1, in mat4 in2, out vec4 rez) {
    vec4 tmp;
    tmp[0] = in1[0] - in2[0][3];
    tmp[1] = in1[1] - in2[1][3];
    tmp[2] = in1[2] - in2[2][3];
    VectorIRotate(tmp, in2, rez);
}

void main()
{
    vec4 transformedPosition = vec4(0.0);
    vec3 transformedNormal = vec3(0.0);

    vec4 curIndex = index;
    vec4 curWeight = weight;

    for (int i = 0; i < int(numBones); i++)
    {
        mat4 g44 = matGlobal[int(curIndex.x)];
        mat4 s44 = matGlobalSkeleton[int(curIndex.x)];
        vec4 vert = vec4(0);
        vec4 norm = vec4(0);
        vec4 tmpNorm = vec4(0);
        vec4 tmpVert = vec4(0);

        VectorITransform(gl_Vertex, s44, tmpVert);
        VectorTransform(tmpVert, g44, vert);
        vert[3]=1;
        transformedPosition += vert * curWeight.x;

        vec4 preNormal=vec4(normal.xyz,1);
        VectorIRotate(preNormal, s44, tmpNorm);
        VectorRotate(tmpNorm, g44, norm);
        norm[3]=1;
        transformedNormal += vec3(norm.xyz) * curWeight.x;

        // shiftam sa avem urmatoarele valori pentru pondere si indice de os
        curIndex = curIndex.yzwx;
        curWeight = curWeight.yzwx;
    }

    gl_Position = gl_ModelViewProjectionMatrix * transformedPosition;

    transformedNormal = normalize(transformedNormal);
    gl_FrontColor = dot(transformedNormal, lightPos.xyz) * color;
} 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Research Institute For Artificial Intelligence, Ro
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions