65,208
社区成员
发帖
与我相关
我的任务
分享#pragma warning(disable:4200)
#include <stdio.h>
#include <stdlib.h>
typedef unsigned int DWORD;
typedef int _Vertice;
typedef char _Face;
#pragma pack(push,1)
typedef struct //网格模型数据
{
DWORD Vertices;
DWORD nFaces;
union {
_Vertice _vertices[];
_Face _faces[];
} u;
} Mesh;
#pragma pack(pop)
void HexDump(char *buf,int len,int addr) {
int i,j,k;
char binstr[80];
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%08x -",i+addr);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
}
}
int main() {
Mesh *m1,*m2,*m3,*m4;
int offset;
m1=(Mesh *)malloc(sizeof(Mesh));
m1->Vertices=0;
m1->nFaces=0;
HexDump((char *)m1,sizeof(Mesh) ,(int)m1);
printf("m1 end\n\n");
m2=(Mesh *)malloc(sizeof(Mesh)+2*sizeof(_Vertice));
m2->Vertices=2;
m2->nFaces=0;
m2->u._vertices[0]=1;
m2->u._vertices[1]=2;
HexDump((char *)m2,sizeof(Mesh)+2*sizeof(_Vertice) ,(int)m2);
printf("m2 end\n\n");
m3=(Mesh *)malloc(sizeof(Mesh)+2*sizeof(_Face));
m3->Vertices=0;
m3->nFaces=2;
m3->u._faces[0]=1;
m3->u._faces[1]=2;
HexDump((char *)m3,sizeof(Mesh) +2*sizeof(_Face),(int)m3);
printf("m3 end\n\n");
m4=(Mesh *)malloc(sizeof(Mesh)+2*sizeof(_Vertice)+3*sizeof(_Face));
m4->Vertices=2;
m4->nFaces=3;
m4->u._vertices[0]=1;
m4->u._vertices[1]=2;
offset=m4->Vertices*sizeof(_Vertice)/sizeof(_Face);
m4->u._faces[offset+0]=1;
m4->u._faces[offset+1]=2;
m4->u._faces[offset+2]=3;
HexDump((char *)m4,sizeof(Mesh)+2*sizeof(_Vertice)+3*sizeof(_Face),(int)m4);
printf("m4 end\n\n");
free(m4);
free(m3);
free(m2);
free(m1);
return 0;
}
//00374d40 - 00 00 00 00 00 00 00 00 cd cd cd cd ............
//m1 end
//
//00372c48 - 02 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 ................
//00372c58 - cd cd cd cd ....
//m2 end
//
//00372c90 - 00 00 00 00 02 00 00 00 01 02 cd cd cd cd ..............
//m3 end
//
//00372cd8 - 02 00 00 00 03 00 00 00 01 00 00 00 02 00 00 00 ................
//00372ce8 - 01 02 03 cd cd cd cd .......
//m4 end
//
//
typedef struct //网格模型数据
{
DWORD Vertices;
_Vertice _vertices[];
}Mesh_header,Mesh;
typedef struct{
DWORD nFaces;
_Face _faces[];
}Mesh_mid;
Mesh * alloc_Mesh(int m,int n){
Mesh *p =NULL;
try{
p= (Mesh *)malloc(sizeof(Mesh_header) + sizeof(_Vertice)*m + sizeof(Mesh_mid) + sizeof(_Face)*n);
}
{
catch(bad_alloc&)return NULL;
}
if(!p)return NULL;
p-> Vertices =m;
mesh_mid *pMid = (mesh_mid *)((char *)p+sizeof(Mesh_header) + sizeof(_Vertice)*m);
pMid-> nFaces = n;
return p;
}
mesh_mid *getMid(Mesh *p){
if(p==NULL)return NULL;
return (mesh_mid *)( (char *)p+sizeof(Mesh_header) + sizeof(_Vertice)* p-> Vertices) ;
}
typedef struct MESH_S//网格模型数据
{
DWORD dwVertices; // 保存pVertic元素个数
_Vertice*pVertic;
DWORD dwFaces; // 保存pFace元素个数
_Face* pFace;
MESH_S()
{
memset(this,0,sizeof(*this))//初始化
}
~MESH_S()
{
if(pVertic!=NULL)
{
delete[] pVertic;
pVertic=NULL;
}
if(pFace!=NULL)
{
delete[] pFace;
pFace=NULL;
}
}
}Mesh;