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 > Messed texture loading md2

#61727 - Webez - Tue Nov 22, 2005 10:09 pm

Hi.

Let's see if someone can help me this time (it seems I ask boring questions). I'm doing a direct md2 loader for nds. I put .md2 and .pcx files in the data directory so they can be converted to .bin. The .pcx is 256x256, 8 bits. The model shows ok but there is something wrong with the texture mapping.

This piece of code shows how textcoords are loaded from md2

Code:
   for (i=0;i<md2Header->numTexCoords;i++){
      memcpy(m_pTexCoords, &file[indice+(i*sizeof(tMd2TexCoord))], sizeof(tMd2TexCoord));   
      mesh->maps.push_back(new DS_TexMap(intot16(m_pTexCoords->u),intot16(256-m_pTexCoords->v)));
      
   }


This is how texcoords are used

Code:
glTexCoord2t16(mesh->maps[mesh->mapIndex[i]->indexA]->u,mesh->maps[mesh->mapIndex[i]->indexA]->v);


And texture is loaded like this

Code:
   sImage file; 
   int texture,sizeX,sizeY;

   loadPCX((u8*)name, &file);

   image8to16(&file);

   switch (file.height){
      case 8: sizeX= TEXTURE_SIZE_8;
            break;
      case 16: sizeX= TEXTURE_SIZE_16;
            break;
      case 32: sizeX= TEXTURE_SIZE_32;
            break;
      case 64: sizeX= TEXTURE_SIZE_64;
            break;
      case 128: sizeX= TEXTURE_SIZE_128;
            break;
      case 256: sizeX= TEXTURE_SIZE_256;
            break;            
      case 512: sizeX= TEXTURE_SIZE_512;
            break;            
      case 1024: sizeX= TEXTURE_SIZE_1024;
            break;
      default: return 0;
   }

   switch (file.width){
      case 8: sizeY= TEXTURE_SIZE_8;
            break;
      case 16: sizeY= TEXTURE_SIZE_16;
            break;
      case 32: sizeY= TEXTURE_SIZE_32;
            break;
      case 64: sizeY= TEXTURE_SIZE_64;
            break;
      case 128: sizeY= TEXTURE_SIZE_128;
            break;
      case 256: sizeY= TEXTURE_SIZE_256;
            break;            
      case 512: sizeY= TEXTURE_SIZE_512;
            break;            
      case 1024: sizeY= TEXTURE_SIZE_1024;
            break;
      default: return 0;
   }

   glGenTextures(1, &texture);
   glBindTexture(0, texture);

   glTexImage2D(0, 0, GL_RGB, sizeX , sizeY, 0, TEXGEN_TEXCOORD, file.data8);

   imageDestroy(&file);




I also have upload a demo that shows the problem.

http://www.4shared.com/file/506245/312bee26/md2.html

Thanks

#61821 - duencil - Thu Nov 24, 2005 12:34 am

what's the range of your texture coords? Discounting tiling they should be scaled to 0-255 for your 256x256 texture ), not 0-1

#61866 - Webez - Thu Nov 24, 2005 10:21 am

I was using the texture coords as you get them from the md2 file (because as far as I know they are 0-255). The only thing I do is flipping v coord as it is done in all loaders I have seen.

#61908 - duencil - Thu Nov 24, 2005 10:16 pm

Webez wrote:
I was using the texture coords as you get them from the md2 file (because as far as I know they are 0-255). The only thing I do is flipping v coord as it is done in all loaders I have seen.


Right, I wasn't aware of the md2 file format specs. Anyway if your data's coming in in the range 0-255 and you want to flip the v, it should be 255-v, not 256. That's probably not your issue, as most of your uvs would be more or less correct. What are you seeing?

obvious question, you're using a temporary pointer to read the text coords to in the loading (m_pTexCoords). What is that, a member variable pointing to a temporary variable? If its an array, its no problem, but it is pointing to something, right?

#61909 - Webez - Thu Nov 24, 2005 10:38 pm

I have already changed 256 to 255 and it was the same as before. As for the other thing I use a pointer to load values into a vector. I have checked that those values are the same I have kept in the vector when drawing the model

This exactly how it is quake coords are loaded once I have read the header

Code:
   tMd2TexCoord *m_pTexCoords = new tMd2TexCoord;
   indice=md2Header->offsetTexCoords;   

   for (i=0;i<md2Header->numTexCoords;i++){
      memcpy(m_pTexCoords, &file[indice+(i*sizeof(tMd2TexCoord))], sizeof(tMd2TexCoord));   
      mesh->maps.push_back(new DS_TexMap(intot16(m_pTexCoords->u),intot16(255-m_pTexCoords->v)));
   }
delete m_pTexCoords;



The class where I keep data

Code:
class DS_Mesh 
{
public:
   std::vector <DS_Vertex *> vertex;
   std::vector <DS_Index *> polIndex;
   std::vector <DS_Index *> mapIndex;
   std::vector <DS_TexMap *> maps;
         ........

For drawing models

Code:
      glBegin(GL_TRIANGLE);
      for(unsigned int i = 0; i < mesh->polIndex.size(); i++){
         glTexCoord2t16(mesh->maps[mesh->mapIndex[i]->indexA]->u,mesh->maps[mesh->mapIndex[i]->indexA]->v);
         glVertex3v16(mesh->vertex[mesh->polIndex[i]->indexA+frame]->x, mesh->vertex[mesh->polIndex[i]->indexA+frame]->y, mesh->vertex[mesh->polIndex[i]->indexA+frame]->z );
         glTexCoord2t16(mesh->maps[mesh->mapIndex[i]->indexB]->u,mesh->maps[mesh->mapIndex[i]->indexB]->v);
         glVertex3v16(mesh->vertex[mesh->polIndex[i]->indexB+frame]->x, mesh->vertex[mesh->polIndex[i]->indexB+frame]->y, mesh->vertex[mesh->polIndex[i]->indexB+frame]->z );
         glTexCoord2t16(mesh->maps[mesh->mapIndex[i]->indexC]->u,mesh->maps[mesh->mapIndex[i]->indexC]->v);
         glVertex3v16(mesh->vertex[mesh->polIndex[i]->indexC+frame]->x, mesh->vertex[mesh->polIndex[i]->indexC+frame]->y, mesh->vertex[mesh->polIndex[i]->indexC+frame]->z );   
      }
glEnd();



Thanks

#62015 - Webez - Sat Nov 26, 2005 2:59 pm

Finally I have found the problem. I have had to rotate the texture 90? left beacuse u,v coords in libnds are not the same as u,v coords in opengl.