# How do I draw a scene with 2 nested frames

I have been trying for long time to figure out this: I have loaded a model from a directx file (I am using opengl and Java) the model have a hierarchical system of nested reference frames (there are not bones). There are just 2 frames, one of them is called x3ds_Torso and it has a child frame called x3ds_Arm_01. Each one of them has a mesh. The thing is that I can't draw the arm connected to the body. Sometimes the body is in the center of the screen and the arm is at the top. Sometimes they are both in the center. I know that I have to multiply the matrix transformation of every frame by its parent frame starting from the top to the bottom and after that I have to multiply every vertex of every mesh by its final transformation matrix. So I have this:

public void calculeFinalMatrixPosition(Bone boneParent, Bone bone) {
System.out.println("-->" + bone.name);
if (boneParent != null) {
bone.matrixCombined = bone.matrixTransform.multiply(boneParent.matrixCombined);
} else {
bone.matrixCombined = bone.matrixTransform;
}

bone.matrixFinal = bone.matrixCombined;
for (Bone childBone : bone.boneChilds) {
calculeFinalMatrixPosition(bone, childBone);
}


}

Then I have to multiply every vertex of the mesh:

public void transformVertex(Bone bone) {

for (Iterator<Mesh> iterator = meshes.iterator(); iterator.hasNext();) {
Mesh mesh = iterator.next();

if (mesh.boneName.equals(bone.name)) {
float[] vertex = new float[4];
double[] newVertex = new double[3];
if (mesh.skinnedVertexBuffer == null) {
mesh.skinnedVertexBuffer = new FloatDataBuffer(
mesh.numVertices, 3);
}
mesh.vertexBuffer.buffer.rewind();
while (mesh.vertexBuffer.buffer.hasRemaining()) {
vertex[0] = mesh.vertexBuffer.buffer.get();
vertex[1] = mesh.vertexBuffer.buffer.get();
vertex[2] = mesh.vertexBuffer.buffer.get();
vertex[3] = 1;

newVertex = bone.matrixFinal.transpose().multiply(vertex);
mesh.skinnedVertexBuffer.buffer.put(((float) newVertex[0]));
mesh.skinnedVertexBuffer.buffer.put(((float) newVertex[1]));
mesh.skinnedVertexBuffer.buffer.put(((float) newVertex[2]));

}

mesh.vertexBuffer = new FloatDataBuffer(
mesh.numVertices, 3);
mesh.skinnedVertexBuffer.buffer.rewind();
mesh.vertexBuffer.buffer.put(mesh.skinnedVertexBuffer.buffer);

}

}

for (Bone childBone : bone.boneChilds) {

transformVertex(childBone);

}


}

I know this is not the more efficient code but by now I just want to understand exactly how a hierarchical model is organized and how I can draw it on the screen.

• What do you mean by "frame"? I've never heard it in an OpenGL context. Do you mean viewports? Why don't you place the objects relative to each other in the same viewport coordinates and render them in order? – opatut Nov 11 '12 at 0:42
• Can't see anything wrong with the code. You could try swapping the multiplication in calculeFinalMatrixPosition, inverting the final matrix and possibly removing the transposing. – msell Nov 12 '12 at 5:22

Ok. This is what I mean by frames, I'm talking about a Directx file (.x extension) which contains something like this:

Frame x3ds_Torso {
FrameTransformMatrix {
1.000000, 0.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000, 0.000000,
0.000000, 0.000000, 1.000000, 0.000000,
0.000001, 25.891449, -0.982366, 1.000000;;
}
Mesh Torso {
//Vertex list
}

Frame x3ds_Arm_01 {
FrameTransformMatrix {
1.000000, 0.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000, 0.000000,
0.000000, 0.000000, 1.000000, 0.000000,
-10.000001, 2.436886, 0.982366, 1.000000;;
}
Mesh Arm_01 {
//Vertex list
}


}

As you can see every frame is a node in the hierarchical system and each one of them can contain meshes and a transformation matrix which define the position of mesh in the model space. I already know how to load this structure and when the file just contain only one mesh there is no problem, the problem comes when there is more than on mesh, in the above example there is a body (frame x3ds_Torso) and its child the arm (x3ds_Arm_01), because the arm should be drawn connected to the body but this is no happening.

• Ok. I got it solved. The problem wasn't in any of the methods that I have mentioned in here. In fact I didn't need use them at all ( at least by now ). The real problem was in the render method because I wasn't using glPushMatrix and glPopMatrix in the right way. Any way I thank you for taking time out to answer my question. I am sure that you are really busy and I appreciate you taking the time to respond. – Guido Javier Granobles Pizarro Nov 14 '12 at 1:15