|
|
[求助]大家过年好,我先给大家拜个年。顺便问个问题。我写了一段打开3DS文件的程序,程序的大部分都没有错,但是最后的贴图部分出了问题,请帮忙看看,可能是哪里的问题。

这就是渲染一个3DS文件后的截图,程序中可能出错的代码在下边。
///////////////////////////////////////////////////////////////////////////////////////////////////////
CTrimesh::Read(char *pF)
{
CVector temp_v1,temp_v2,temp_v3;
iFace=0;
ID=*((unsigned short int*)pF);
length=*((unsigned int*)(pF+2));
char *p;
p=pF+6;
while(p<pF+length)
{
switch(*((unsigned short int*)p))
{
case 0x4110: // 顶点列表块
iVertice=*((unsigned short int*)(p+6));
pVertice=new tVertice[iVertice];
pNormal=new CFloatVector[iVertice];
pTexture=new tTexture[iVertice];
for(unsigned int i=0;i<iVertice;i++)
{
pVertice.x=((float*)(p+8))[i*3];
pVertice.z=-((float*)(p+8))[i*3+1];
pVertice.y=((float*)(p+8))[i*3+2];
}
break;
case 0x4120: // 面信息块
iFace=*((unsigned short int*)(p+6));
pFace=new tFace[iFace];
for(unsigned int i=0;i<iFace;i++)
{
pFace.face[0]=*((unsigned short int*)(p+i*8+8));
pFace.face[1]=*((unsigned short int*)(p+i*8+10));
pFace.face[2]=*((unsigned short int*)(p+i*8+12));
temp_v1.x=pVertice[pFace.face[0]].x-pVertice[pFace.face[1]].x;
temp_v1.y=pVertice[pFace.face[0]].y-pVertice[pFace.face[1]].y;
temp_v1.z=pVertice[pFace.face[0]].z-pVertice[pFace.face[1]].z;
temp_v2.x=pVertice[pFace.face[1]].x-pVertice[pFace.face[2]].x;
temp_v2.y=pVertice[pFace.face[1]].y-pVertice[pFace.face[2]].y;
temp_v2.z=pVertice[pFace.face[1]].z-pVertice[pFace.face[2]].z;
temp_v3.CrossVertex(temp_v1,temp_v2);
//这里将面的法线累加到三个顶点上。
pNormal[pFace.face[0]]+=temp_v3;
pNormal[pFace.face[1]]+=temp_v3;
pNormal[pFace.face[2]]+=temp_v3;
}
for(unsigned int i=0;i<iVertice;i++)
{
pNormal.Normalize();
}
if(iFace*8+8<*((unsigned int*)(p+2)))
{
if(*((unsigned short int*)(p+(iFace*8+8)))==0x4130)
{
// 一个物体本来可以绑定多种材质的,这里为了省事,只读取第一个材质的名称。
strcpy(MatName,p+(iFace*8+14));
}
}
break;
case 0x4140:
iTexture=*((unsigned short int*)(p+6));
for(unsigned int i=0;i<iTexture;i++)
{
pTexture.u=((float*)(p+8))[i*2];
pTexture.v=-((float*)(p+8))[i*2+1];
}
break;
}
p+=*((unsigned int*)(p+2));
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void CObject::draw()
{
//启用顶点数组
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, mTrimesh.pVertice );
glNormalPointer( GL_FLOAT, 0, mTrimesh.pNormal );
if(mTrimesh.iTexture)
{
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer( 3, GL_FLOAT, 0, mTrimesh.pTexture );
}
glDrawElements( GL_TRIANGLES, mTrimesh.iFace*3, GL_UNSIGNED_SHORT, mTrimesh.pFace );
} |
|