3ds多模型---排兵布阵

这一章来排一排模型位置。

定义相关的结构:

typedef struct _boxbian //外框边界
{
	float xmin,xmax,ymin,ymax,zmin,zmax;
}boxbian;

typedef struct _me3DS
{
    string fileName;//文件名
    t3DModel Model;//模型
	CLoad3DS *gothicLoader;//加载器
	float gothicTrans[10];//模型位置,缩放,旋转
	boxbian bian;//外框
}me3DS;

vector<string> names_3ds;//所有模型名
vector<me3DS> me3ds;      // 所有模型
t3DModel *gothicModel;//这个改为指针

搜索3ds文件:

//搜索3ds文件(在文件夹中)
void onFindFilesInDir(char* rootDir, vector<string> &names_3ds)
{
    //srcFiles = (char *)malloc(FILEEMAX * sizeof(char));
    WIN32_FIND_DATA fd;
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));

    HANDLE hFile;
    char tmpPath[256];
    char subPath[256];
    ZeroMemory(tmpPath, 256);
    ZeroMemory(subPath, 256);

    BOOL bRet = TRUE;
    static int nFileSize = 0 ;

    //define the format of the basepath
    strcpy(tmpPath, rootDir);
    if(tmpPath[strlen(tmpPath) -1] != '\\')
    {
        strcat(tmpPath, "\\");
    }
    strcat(tmpPath, "*");

		WCHAR wszPath[MAX_PATH+1];                  // 图片的完整路径 ()
		MultiByteToWideChar(CP_ACP, 0, tmpPath, -1, wszPath, MAX_PATH);    // 从ASCII转换为Unicode
    hFile = FindFirstFile(wszPath, &fd);
    while (hFile != INVALID_HANDLE_VALUE && bRet)
    {
        if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && 
            wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L".."))
        {
            strcpy(subPath, rootDir);
            if(subPath[strlen(subPath) -1] != '\\')
            {
                strcat(subPath, "\\");
            }
			char tmpchar[MAX_PATH+1];
			int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, fd.cFileName, -1, NULL, 0, NULL, NULL); 
			WideCharToMultiByte( CP_ACP, 0, fd.cFileName, -1, tmpchar, lengthOfMbs, NULL, NULL); //宽字符转为多字符
            strcat(subPath, tmpchar);
            onFindFilesInDir(subPath, names_3ds);
        }
        else if(!wcscmp(fd.cFileName, L".") || !wcscmp(fd.cFileName, L".."))
        {
            
        }
        else
        {
			char tmpchar[MAX_PATH+1];
			int lengthOfMbs = WideCharToMultiByte( CP_ACP, 0, fd.cFileName, -1, NULL, 0, NULL, NULL); 
			WideCharToMultiByte( CP_ACP, 0, fd.cFileName, -1, tmpchar, lengthOfMbs, NULL, NULL); //宽字符转为多字符
			string str0=string(tmpchar);
			int loc1,loc2;
			loc1=str0.rfind (".3ds");loc2=str0.rfind (".3DS");//去掉其它文件名
			if(loc1==str0.length() -4 || loc2==str0.length() -4)
				names_3ds.push_back(str0);
        }
        bRet = FindNextFile(hFile, &fd);
    }
    FindClose(hFile);
}

//给定一个文件夹,搜索3ds文件
void FindFiles()
{
		  onFindFilesInDir(srcDir, names_3ds);//搜索文件
		  for (unsigned int i = 0; i < names_3ds.size (); i++)
			{
				cout<<names_3ds[i] <<endl;
				
			}	

}
加载模型并计算外框:

//按名称加载模型
void loadNameModel(string filename)
{
	me3DS *ds=new(me3DS);

	//初始化
	for(int i=0;i<MAX_TEXTURES;i++)
		ds->Model.texture[i]=0;
	ds->Model.numOfMaterials=0;
	ds->Model.numOfObjects=0;

	ds->fileName=filename;//文件名
	ds->gothicLoader=new(CLoad3DS);
	gothicModel=&ds->Model;//显示指向

	char m0[256];
	//sprintf(m0,"Data/3ds/%s",filename);//加上路径
	string m1="Data/3ds/"+filename;
	strcpy(m0, m1.c_str());
	ds->gothicLoader->Import3DS(gothicModel,m0);//载入
	ds->bian= getbian(gothicModel);//获得外框边界
	
	me3ds.push_back(*ds);//存入
}
模型分成两排:

void dsMatrix()//排列
{
	//分成两排
	unsigned int l=me3ds.size ()/2;
	//按x方向排,计算总长
	float xal=0,//x方向
		t,
	zlmax=0;//z最大
	//第一排
	for (unsigned int i = 0; i < l; i++)
	{
		xal+=me3ds[i].bian.xmax-me3ds[i].bian.xmin;
		t=me3ds[i].bian.zmax-me3ds[i].bian.zmin;
		zlmax=(zlmax>t)?zlmax:t;
	}	
	//分配模型地址
	xal=-xal/2;//最左点
	zlmax=-zlmax/2;
	t=xal+gothicTrans[0];printf("%f\n",t);
	for (unsigned int i = 0; i < l; i++)
	{
		
		me3ds[i].gothicTrans[0]=t-me3ds[i].bian.xmin+(me3ds[i].bian.xmax-me3ds[i].bian.xmin)/2;//去掉和原来位置相关性
			t+=(me3ds[i].bian.xmax-me3ds[i].bian.xmin);//下一个位置
		me3ds[i].gothicTrans[1]=gothicTrans[1];
		me3ds[i].gothicTrans[2]=zlmax+-me3ds[i].bian.zmin+(gothicTrans[2]-me3ds[i].bian.zmin)/2;//去掉和原来位置相关性

	}	
	//------------------------------------------------
	//第二排
	for (unsigned int i = l; i < me3ds.size (); i++)
	{
		zlmax=(zlmax>t)?zlmax:t;
	}	
	//分配模型地址
	
	zlmax=zlmax/2;
	t=xal+gothicTrans[0];
	for (unsigned int i = l; i < me3ds.size (); i++)
	{
		
		me3ds[i].gothicTrans[0]=t-me3ds[i].bian.xmin+(me3ds[i].bian.xmax-me3ds[i].bian.xmin)/2;//去掉和原来位置相关性
			t+=(me3ds[i].bian.xmax-me3ds[i].bian.xmin);//下一个位置
		me3ds[i].gothicTrans[1]=gothicTrans[1];
		me3ds[i].gothicTrans[2]=zlmax+-me3ds[i].bian.zmin+(gothicTrans[2]-me3ds[i].bian.zmin)/2;//去掉和原来位置相关性

	}	
}
绘制所有模型:

//绘制所有模型
void drawAllModel()
{
		  for (unsigned int i = 0; i < me3ds.size (); i++)
		{
			gothicModel=&me3ds[i].Model;
			//printf("正在绘制 %d 中的第 %d 个模型\n",names_3ds.size (),i+1);
			float xt1,yt1,zt1;
			xt1=	me3ds[i].gothicTrans[0];
			yt1=     me3ds[i].gothicTrans[1];
			zt1=     me3ds[i].gothicTrans[2];

				glTranslatef(xt1,yt1,zt1);// 放到这个位置
			drawModel(*gothicModel, true, false);//绘模型
				glTranslatef(-xt1,-yt1,-zt1);//还原
		}	
			//printf("绘制完成\n");

}
运行结果:


去掉大家伙后图:


这个效果还是有点差强人意。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值