bool Cterrain::init(const string&heightMapFileName,const Cc3dRect&rect,float heightScale,int quadtreeDepth, Cc3dTexture*texture){ assert(rect.getWidth()==rect.getHeight()); m_heightScale=heightScale; m_quadtreeDepth=quadtreeDepth; m_heightMapFileName=heightMapFileName; //生成mesh Cc3dMesh*mesh=new Cc3dMesh(); mesh->init(); mesh->autorelease(); addMesh(mesh); //读取高程数据--abc readLandMat(); //求高度范围--abc float Hmin=c3d_INF; float Hmax=-c3d_INF; for(int i=0;i<(int)landMat.size();i++){//倒序--abc for(int j=0;j<(int)landMat[i].size();j++){//正序--abc if(landMat[i][j]>Hmax){ Hmax=landMat[i][j]; } if(landMat[i][j]<Hmin){ Hmin=landMat[i][j]; } } }//得到Hmin和Hmax //设置m_range m_range.init(rect.getMinX(), rect.getMaxX(), Hmin, Hmax, rect.getMinY(), rect.getMaxY()); //计算步长--abc int markMatSideLength=pow(2.0,m_quadtreeDepth-1)+1;//do not use 2, should use 2.0, see: http://www.360doc.com/content/11/0826/15/7566064_143451209.shtml gridSize=m_range.getSpanX()/(markMatSideLength-1); //填充法向数据--abc fillNormalMat(); //markmat开辟空间--abc markmat.resize(markMatSideLength); for(int i=0;i<(int)markmat.size();i++){ markmat[i].resize(markMatSideLength); for(int j=0;j<(int)markmat[i].size();j++){ markmat[i][j]=0; } } //制作ground submesh Cc3dSubMesh*submesh=new Cc3dSubMesh(); submesh->autorelease(); submesh->init(); submesh->setTexture(texture); submesh->getIndexVBO()->genBuffers(); this->getMesh()->addSubMesh(submesh); //make submesh makeMesh(); return true; }
bool Cterrain::init(const string&heightMapFileName,const Cc3dRect&rect,float heightScale,int quadtreeDepth, Cc3dTexture*texture){ assert(rect.getWidth()==rect.getHeight()); m_heightScale=heightScale; m_quadtreeDepth=quadtreeDepth; m_heightMapFileName=heightMapFileName; //生成model Cc3dModel*model=new Cc3dModel(); model->init(); model->autorelease(); addModel(model); //读取高程数据 readLandMat(); //求高度范围 float Hmin=c3d_INF; float Hmax=-c3d_INF; for(int i=0;i<(int)landMat.size();i++){//倒序 for(int j=0;j<(int)landMat[i].size();j++){//正序 if(landMat[i][j]>Hmax){ Hmax=landMat[i][j]; } if(landMat[i][j]<Hmin){ Hmin=landMat[i][j]; } } }//得到Hmin和Hmax //设置m_range m_range.init(rect.getMinX(), rect.getMaxX(), Hmin, Hmax, rect.getMinY(), rect.getMaxY()); //计算步长 int markMatSideLength=pow(2,m_quadtreeDepth-1)+1; gridSize=m_range.getSpanX()/(markMatSideLength-1); //填充法向数据 fillNormalMat(); //markmat开辟空间 markmat.resize(markMatSideLength); for(int i=0;i<(int)markmat.size();i++){ markmat[i].resize(markMatSideLength); for(int j=0;j<(int)markmat[i].size();j++){ markmat[i][j]=0; } } //制作ground mesh Cc3dMesh*mesh=new Cc3dMesh(); mesh->autorelease(); mesh->init(); mesh->setTexture(texture); mesh->getIndexVBO()->genBuffers(); this->getModel()->addMesh(mesh); //make mesh makeMesh(); //xxxx下面要去掉 //申请空间不光要考虑矩形,还要考虑三角形 //另外注意一个矩形要拆成两个三角形,所以是六个顶点(而非四个) //矩形最大数量:BMPHEIGHT*BMPWIDTH //矩形分裂出的最大三角形数量:BMPHEIGHT*BMPWIDTH*2 //补洞三角形最大数量:等于mesh网格的非边缘边个数,等于BMPHEIGHT*(BMPWIDTH-1)+BMPWIDTH*(BMPHEIGHT-1) //所以总共有BMPHEIGHT*BMPWIDTH*2+(BMPHEIGHT*(BMPWIDTH-1)+BMPWIDTH*(BMPHEIGHT-1))个三角形 int bmpHeight=(int)landMat.size(); int bmpWidth=(int)landMat[0].size(); this->getModel()->getMeshByIndex(0)->getMeshData()->IDtriList.reserve(bmpHeight*bmpWidth*2+(bmpHeight*(bmpWidth-1)+bmpWidth*(bmpHeight-1))); return true; }