gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > No Translation [Solved]

#124841 - relpats_eht - Mon Apr 09, 2007 3:03 am

I have been attempting to make the switch from software to hardware matrices on my model loader and have come across a bit of a problem: the resulting image seems to be ignoring all premultiplication translation.

Previously, I had been transforming each vertex through the matrix of its corresponding joint in software, which, simplified, was something like this:
Code:
Vector newVertex(curVert->location);
newVertex.Transform(matrix->GetMatrix());
F32* vert = newVertex.GetVector();

glTranslate3f32(vert[0].v, vert[1].v, vert[2].v);
glVertex3v16(0, 0, 0);
glTranslate3f32(-vert[0].v, -vert[1].v, -vert[2].v);


The above worked just fine, but I am trying to move past that as it doesn't work well for normals and is slower than using hardware. Therefore, I now have code that looks like this:
Code:
Matrix* matrix = &joint[curVert->boneId]->final;
F32* m = matrix->GetMatrix();
m4x4 glM;

glM.m[0]=m[0].v; glM.m[1]=m[1].v; glM.m[2]=m[2].v; glM.m[3]=m[3].v;
glM.m[4]=m[4].v; glM.m[5]=m[5].v; glM.m[6]=m[6].v; glM.m[7]=m[7].v;
glM.m[8]=m[8].v; glM.m[9]=m[9].v; glM.m[10]=m[10].v; glM.m[11]=m[11].v;
glM.m[12]=m[12].v; glM.m[13]=m[13].v; glM.m[14]=m[14].v; glM.m[15]=m[15].v;

glPushMatrix();
glMultMatrix4x4(&glM);

glTranslate3f32(curVert->location[0].v, curVert->location[1].v, curVert->location[2].v);
glVertex3v16(0, 0, 0);
glTranslate3f32(-curVert->location[0].v, -curVert->location[1].v, -curVert->location[2].v);

glPopMatrix(1);


But the result is incorrect. It seems that any translation, but not rotation, done before the call to glMultMatrix is lost in the final result. I do not know if and to what extent the final model is deformed, as I cannot see the whole thing without translation.

Thus, I ask, could anyone point out just what I am doing wrong? I think my matrices are stored in the same manner and those on the DS (translation on the bottom row) and I seem to be currently unable to figure this out.

** Yes, I know this code is horrible and inefficient. Step one is making things work, step two is aesthetics and optimization.
_________________
- relpats_eht


Last edited by relpats_eht on Wed Apr 11, 2007 2:43 am; edited 1 time in total

#124884 - gabebear - Mon Apr 09, 2007 5:17 pm

What Matrix Mode are you in? Be warned that glGetFixed() will change the matrix mode behind your back, so reset your mode whenever you call that function.

#124900 - relpats_eht - Mon Apr 09, 2007 8:50 pm

I am in model view mode, which I do call again after every glGetFixed statement.
_________________
- relpats_eht

#124998 - relpats_eht - Tue Apr 10, 2007 8:17 pm

I most likely is a good idea to give a bit more information on the subject.
I have confirmed via adjusting the matrix multiplication by adding to the Z translational field (14) that the model is being drawn completely correctly, although always at the position 0, 0, 0.

Anyway, here is the complete set of graphical code that is called each frame. What am I doing wrong that is causing all translation to be lost while rotation preserved?

Code:
glReset();
GFX_CLEAR_COLOR = BIT(16) | RGB15(0, 0, 0); // Bit 16 is the alpha bit.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspectivef32(50, F32(F32(inttof32(256))/F32(inttof32(192))).v, floattof32(0.1f), inttof32(100)); // 50 = 35 degrees

glLight(0, RGB15(16,16,16), 0, 1023, 0); // 1023 ~= -1 in v10
glLight(1, RGB15(24,24,24), 1023, 0, 0);

glMatrixMode(GL_MODELVIEW);

glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE | POLY_FORMAT_LIGHT0 | POLY_FORMAT_LIGHT1 | POLY_TOON_SHADING);

glTranslatef(x, y, z);
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
glRotatef(zrot, 0.0f, 0.0f, 1.0f);

int8 matIndex;
for(int m=0; m<numMeshes; m++){
   Mesh* curMesh = mesh[m];

   matIndex = curMesh->materialIndex;
   if(matIndex >= 0){
      glMaterialf(GL_AMBIENT, material[matIndex]->ambient);
      glMaterialf(GL_DIFFUSE, material[matIndex]->diffuse);
      glMaterialf(GL_SPECULAR, material[matIndex]->specular);
      glMaterialf(GL_EMISSION, material[matIndex]->emissive);

      if(material[matIndex]->texture.GetID() > 0){
         glMatrixMode(GL_TEXTURE);
         glLoadIdentity();
         glMatrixMode(GL_MODELVIEW);

         glEnable(GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, id);
      }else{
         glDisable(GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, 0);
      }
   }else{
      glDisable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, 0);
   }

   glBegin(GL_TRIANGLES);
      for(int t=0; t<curMesh->numTriangles; t++){
         Triangle* curTri = &curMesh->triangle[t];

         for(int v=0; v<3; v++){
            Vertex* curVert = &curMesh->vertex[curTri->vertexIndex[v]];

            glTexCoord1i(curVert->tex);
            Matrix* matrix = &joint[curVert->boneId]->final;
            F32* m = matrix->GetMatrix();
            m4x4 glM;

            glM.m[0]=m[0].v; glM.m[1]=m[1].v; glM.m[2]=m[2].v; glM.m[3]=m[3].v;
            glM.m[4]=m[4].v; glM.m[5]=m[5].v; glM.m[6]=m[6].v; glM.m[7]=m[7].v;
            glM.m[8]=m[8].v; glM.m[9]=m[9].v; glM.m[10]=m[10].v; glM.m[11]=m[11].v;
            glM.m[12]=m[12].v; glM.m[13]=m[13].v; glM.m[14]=m[14].v; glM.m[15]=m[15].v;

            glPushMatrix();
            glMultMatrix4x4(&glM);

            glNormal(NORMAL_PACK(curTri->normal[v][0].Fix().v, curTri->normal[v][1].Fix().v, curTri->normal[v][2].Fix().v));

            glTranslate3f32(curVert->location[0].v, curVert->location[1].v, curVert->location[2].v);
            glVertex3v16(0, 0, 0);

            glPopMatrix(1);
         }
      }
   glEnd();
}

glFlush();
swiWaitForVBlank();

_________________
- relpats_eht

#125062 - relpats_eht - Wed Apr 11, 2007 2:42 am

Never mind, I have found the problem. For reasons I am still unsure or, the final element of the my matrices were being set to 1, rather than 4096. All now works as expected.
_________________
- relpats_eht

#125063 - HyperHacker - Wed Apr 11, 2007 2:49 am

That sounds like a bug in converting to/from fixed-point.
_________________
I'm a PSP hacker now, but I still <3 DS.

#125065 - relpats_eht - Wed Apr 11, 2007 3:01 am

Actually, it was a bug in assuming I converted to/from fixed point. I simply forgot how my classes worked, it has been so long since I have worked with the DS.
_________________
- relpats_eht