int main(int argc, char** argv) { bool normalize = false; bool flip = false; bool wnormals = false; bool rnormals = false; bool rtexcoords = false; bool closemesh = false; float closedist = 0.0f; float mergedist = -1.0f; Vec3f translation; Vec3f rotation; Vec3f scale(1,1,1); float dilate = 0.0f; int tesselate = 0; bool sphere = false; bool dist = false; int res = 16; int rx=0,ry=0,rz=0; float border = 0.25f; float vsize = 0.0f; ftl::CmdLine cmd("Usage: meshconv [options] mesh.input [mesh.output]"); cmd.opt("normalize",'n',"transform points so that the center is at <0,0,0> and the max coodinate is 1",&normalize); cmd.opt("flip",'f',"flip normals",&flip); cmd.opt("wnormals",'N',"force writing of normals",&wnormals); cmd.opt("rnormals",'O',"remove normals",&rnormals); cmd.opt("rtexcoords",'T',"remove texcoords",&rtexcoords); cmd.opt("close",'c',"close mesh",&closemesh); cmd.opt("close2",'C',"close mesh creating intermediate vertices no further appart than given dist",&closedist); cmd.opt("merge",'m',"merge vertices closer than the given distance",&mergedist); cmd.opt("translate",'t',"translate the mesh",&translation); cmd.opt("rotate",'r',"rotate the mesh using euler angles in degree",&rotation); cmd.opt("scale",'s',"scale the mesh using 3 coefficients",&scale); cmd.opt("dilate",'d',"dilate (i.e. displace vertices of the given distance along their normals)",&dilate); cmd.opt("tesselate",'a',"tesselate (split each edge in 2 resursivly n times)",&tesselate); cmd.opt("sphere",'S',"consider the mesh as a sphere for tesselation",&sphere); cmd.opt("dist",'D',"compute distance field",&dist); cmd.opt("res",'R',"resolution of distance field",&res); cmd.opt("rx",'X',"X resolution of distance field",&rx); cmd.opt("ry",'Y',"Y resolution of distance field",&ry); cmd.opt("rz",'Z',"Z resolution of distance field",&rz); cmd.opt("vsize",'V',"size of each voxel in distance field",&vsize); cmd.opt("border",'B',"distance field border size relative to the object's BBox size (or negative for exact size)",&border); bool error=false; if (!cmd.parse(argc,argv,&error)) return error?1:0; if (cmd.args.size()<1 || cmd.args.size()>2) { std::cerr << cmd.help() << std::endl; return 1; } std::string file_in = cmd.args[0]; std::string file_out; if (cmd.args.size()>=2) file_out = cmd.args[1]; Mesh obj; if (!obj.load(file_in.c_str())) { std::cerr << "Failed to read "<<file_in<<std::endl; return 1; } if (normalize) { BBox bb = obj.calcBBox(); std::cout << "Mesh bbox="<<bb<<std::endl; std::cout << "Normalizing mesh..."<<std::endl; float size = 0; for (int i=0; i<3; i++) { float d = bb.b[i]-bb.a[i]; if (d>size) size=d; } Vec3f center = (bb.a+bb.b)*0.5; float sc = 2/size; Mat4x4f m; m.identity(); m(0,0)=sc; m(0,3) = -center[0]*sc; m(1,1)=sc; m(1,3) = -center[1]*sc; m(2,2)=sc; m(2,3) = -center[2]*sc; for (int i=0; i<obj.nbp(); i++) obj.PP(i) = transform(m,obj.getPP(i)); } if (scale != Vec3f(1,1,1)) { std::cout << "Scaling mesh..."<<std::endl; for (int i=0; i<obj.nbp(); i++) { Vec3f p = obj.getPP(i); p[0] *= scale[0]; p[1] *= scale[1]; p[2] *= scale[2]; obj.PP(i) = p; } } Mat4x4f xform; bool hasXForm = false; xform.identity(); if (rotation != Vec3f(0,0,0)) { std::cout << "Rotating mesh..."<<std::endl; Mat3x3f mat; Quat qx,qy,qz; qx.fromDegreeAngAxis(rotation[0],Vec3f(1,0,0)); qy.fromDegreeAngAxis(rotation[1],Vec3f(0,1,0)); qz.fromDegreeAngAxis(rotation[2],Vec3f(0,0,1)); Quat q = qx*qy*qz; q.toMatrix(&mat); std::cout << "mat = "<<mat<<std::endl; for (int i=0; i<obj.nbp(); i++) { obj.PP(i) = mat*obj.getPP(i); obj.PN(i) = mat*obj.getPN(i); } hasXForm = true; xform = mat; } if (translation != Vec3f(0,0,0)) { std::cout << "Translating mesh..."<<std::endl; for (int i=0; i<obj.nbp(); i++) { obj.PP(i) = obj.getPP(i) + translation; } hasXForm = true; xform[0][3] = translation[0]; xform[1][3] = translation[1]; xform[2][3] = translation[2]; } if (obj.distmap && hasXForm) { //Mat4x4f m; m.invert(xform); std::cout << "distmap mat = "<<xform <<" * " << obj.distmap->mat<<" = "; obj.distmap->mat = xform * obj.distmap->mat; std::cout << obj.distmap->mat<<std::endl; } { BBox bb = obj.calcBBox(); std::cout << "Mesh bbox = "<<bb<<std::endl; std::cout << "Mesh center and radius = "<<(bb.a+bb.b)*0.5<<" "<<(bb.b-bb.a)*0.5 << std::endl; } if (mergedist >= 0.0f) { std::cout << "Merging vertices closer than " << mergedist <<std::endl; obj.mergeVertices(mergedist); } if (flip) { std::cout << "Flipping mesh..."<<std::endl; obj.flipAll(); //obj.calcFlip(); } if (closemesh || closedist != 0.0f) { bool closed = obj.isClosed(); std::cout << "Mesh is "<<(closed?"":"NOT ")<<"closed."<<std::endl; if (!closed) { obj.calcFlip(); std::cout << "Closing mesh..."<<std::endl; if (closedist != 0.0f) { obj.closeDist(closedist); } else //if (closemesh) { obj.close(); } std::cout << "Mesh is "<<(obj.isClosed()?"":"NOT ")<<"closed."<<std::endl; } } if (dilate != 0.0f) obj.dilate(dilate); if (tesselate > 0 || sphere) { std::cout << "Tesselating mesh..."<<std::endl; tesselateMesh(obj, tesselate, sphere); } if (dist) { if (!rx) rx = res; if (!ry) ry = res; if (!rz) rz = res; std::cout << "Flipping mesh..."<<std::endl; obj.calcFlip(); obj.calcEdges(); bool closed = obj.isClosed(); std::cout << "Mesh is "<<(closed?"":"NOT ")<<"closed."<<std::endl; if (!closed) { std::cout << "Closing mesh..."<<std::endl; obj.close(); std::cout << "Mesh is "<<(obj.isClosed()?"":"NOT ")<<"closed."<<std::endl; } BBox bb = obj.calcBBox(); float bsize = (border<0 ? -border : bb.size()*border); Vec3f bbsize = bb.b-bb.a; bbsize[0] += 2*bsize; bbsize[1] += 2*bsize; bbsize[2] += 2*bsize; if (vsize > 0) { rx = (int)ceilf(bbsize[0]/vsize); ry = (int)ceilf(bbsize[1]/vsize); rz = (int)ceilf(bbsize[2]/vsize); } std::cout << "Computing "<<rx<<'x'<<ry<<'x'<<rz<<" DistMap..."<<std::endl; obj.calcDistMap(rx,ry,rz,bsize); } if (wnormals) obj.setAttrib(Mesh::MESH_POINTS_NORMAL,true); if (rnormals) obj.setAttrib(Mesh::MESH_POINTS_NORMAL,false); if (rtexcoords) obj.setAttrib(Mesh::MESH_POINTS_TEXCOORD,false); if (!file_out.empty()) { std::cout << "Saving result..."<<std::endl; obj.save(file_out.c_str()); } return 0; }
int main(int argc, char** argv) { flowvr::InputPort pRotX("rotX"); flowvr::InputPort pRotY("rotY"); flowvr::InputPort pTransX("transX"); flowvr::InputPort pTransY("transY"); flowvr::InputPort pZoom("zoom"); SceneOutputPort pOut("scene"); flowvr::OutputPort pOutMat("matrix"); flowvr::OutputPort pTerrain("terrain"); flowvr::OutputPort pOutFluid("fluidpos"); flowvr::StampInfo terrainN("N",flowvr::TypeArray::create(2,flowvr::TypeInt::create())); flowvr::StampInfo terrainP("P",flowvr::TypeArray::create(2,flowvr::TypeFloat::create())); flowvr::StampInfo terrainS("S",flowvr::TypeArray::create(2,flowvr::TypeFloat::create())); flowvr::StampInfo terrainH("H",flowvr::TypeArray::create(2,flowvr::TypeFloat::create())); pTerrain.stamps->add(&terrainN); pTerrain.stamps->add(&terrainP); pTerrain.stamps->add(&terrainS); pTerrain.stamps->add(&terrainH); std::vector<flowvr::Port*> ports; ports.push_back(&pRotX); ports.push_back(&pRotY); ports.push_back(&pTransX); ports.push_back(&pTransY); ports.push_back(&pZoom); ports.push_back(&pOut); ports.push_back(&pOutMat); ports.push_back(&pOutFluid); ports.push_back(&pTerrain); flowvr::ModuleAPI* module = flowvr::initModule(ports); if (module == NULL) { return 1; } flowvr::render::ChunkRenderWriter scene; ID idP = 0x300; //module->generateID(); ID idT = 0x301; //module->generateID(); ID idVB = 0x302; //module->generateID(); ID idIB = 0x303; //module->generateID(); ID idVS = 0x304; //module->generateID(); ID idPS = 0x305; //module->generateID(); std::cout << "idVS="<<idVS<<std::endl; std::cout << "idPS="<<idPS<<std::endl; std::string ftexture = "images/valley2.jpg"; std::string fheight = "valley2.ter"; if (argc>=2) ftexture = argv[1]; if (argc>=3) fheight = argv[2]; int nx = 257; int ny = 257; TerragenTerrain terrain; if (!terrain.load(fheight)) { module->close(); return 2; } Vec3f position0; Vec3f position(-25,0,0); Vec3f rotation; float zoom = 1; if (cx >= terrain.nx) cx = terrain.nx/2; if (cy >= terrain.ny) cy = terrain.ny/2; if (cx-nx/2<0) nx = cx*2; if (cy-ny/2<0) ny = cy*2; if (cx-nx/2+nx>terrain.nx) nx = (terrain.nx-cx)*2-1; if (cy-ny/2+ny>terrain.ny) ny = (terrain.ny-cy)*2-1; terrain.scale = Vec3f(1.0f,1.0f,2.0f); //terrain.hbase -= 18.033f; terrain.hbase = -terrain.hscale*terrain.height[(cy)*terrain.nx+(cx-25)]; std::cout << "Using data centered @ "<<cx<<"x"<<cy<<" size "<<nx<<"x"<<ny<<" scale "<<terrain.scale.x()<<"x"<<terrain.scale.y()<<"x"<<terrain.scale.z()<<" z="<<terrain.hbase<<"+h*"<<terrain.hscale<<std::endl; if (!scene.loadTexture(idT,ftexture)) scene.addDefaultTexture(idT); int dataType[1] = { Type::Vec3s }; short hmin = 0xffff; short hmax = 0; int xmin=0,xmax=1,ymin=0,ymax=1; // fluid interval { short* height = terrain.height+(cy-ny/2)*terrain.nx+(cx-nx/2); for (int y=0;y<ny;y++) { for (int x=0;x<nx;x++) { if (height[x] < hmin) { hmin = height[x]; xmin=xmax=x; ymin=ymax=y; } else if (height[x] == hmin) { if (x<xmin) xmin=x; else if (x>xmax) xmax=x; if (y<ymin) ymin=y; else if (y>ymax) ymax=y; } if (height[x] > hmax) hmax = height[x]; } height+=terrain.nx; } } std::cout << "height min="<<hmin<<" max="<<hmax<<std::endl; ChunkVertexBuffer* vb = scene.addVertexBuffer(idVB, nx*ny, 1, dataType, BBox(Vec3f(0,0,hmin),Vec3f(nx-1,ny-1,hmax))); { Vec<3,short>* vertex = (Vec<3,short>*)vb->data(); short* height = terrain.height+(cy-ny/2)*terrain.nx+(cx-nx/2); for (int y=0;y<ny;y++) { for (int x=0;x<nx;x++) { vertex->x() = x; vertex->y() = y; vertex->z() = height[x]; ++vertex; } height+=terrain.nx; } } /* std::cout << "Primitive mode: QUAD\n"; ChunkIndexBuffer* ib = scene.addIndexBuffer(idIB, 4*(nx-1)*(ny-1), Type::Int, ChunkIndexBuffer::Quad); { unsigned int* ind = (unsigned int*)ib->data(); for (int y=0;y<ny-1;y++) { for (int x=0;x<nx-1;x++) { ind[0] = (y )*nx+(x ); ind[1] = (y )*nx+(x+1); ind[2] = (y+1)*nx+(x+1); ind[3] = (y+1)*nx+(x ); ind+=4; } } } */ /* std::cout << "Primitive mode: TRIANGLE STRIP\n"; int restart = -1; ChunkIndexBuffer* ib = scene.addIndexBuffer(idIB, 2*(nx)*(ny-1)+(ny-2), Type::Int, ChunkIndexBuffer::TriangleStrip); ib->restart = restart; { unsigned int* ind = (unsigned int*)ib->data(); for (int y=0;y<ny-1;y++) { if (y) *(ind++) = (unsigned int)restart; // start a new strip for (int x=0;x<nx;x++) { ind[0] = (y )*nx+(x ); ind[1] = (y+1)*nx+(x ); ind+=2; } } } */ std::cout << "Primitive mode: TRIANGLE FAN\n"; int restart = -1; int nindex = ((nx-1)/2)*((ny-1)/2)*(10+1) // full circles +((nx&1)?0:((ny-1)/2)*(6+1)) // half circles if nx is even +((ny&1)?0:((nx-1)/2)*(6+1)) // half circles if ny is even +((nx&1 || ny&1)?0: (4+1)) // final quad if both nx and ny are even -1; // last fan does not need a restart index ChunkIndexBuffer* ib = scene.addIndexBuffer(idIB, nindex, Type::Int, ChunkIndexBuffer::TriangleFan); ib->restart = restart; { unsigned int* ind = (unsigned int*)ib->data(); unsigned int* start=ind; for (int y=1;y<ny;y+=2) { for (int x=1;x<nx;x+=2) { if (ind!=start) *(ind++) = (unsigned int)restart; // start a new fan *(ind++) = (y )*nx+(x ); if (y<ny-1) *(ind++) = (y+1)*nx+(x ); if (y<ny-1) *(ind++) = (y+1)*nx+(x-1); *(ind++) = (y )*nx+(x-1); *(ind++) = (y-1)*nx+(x-1); *(ind++) = (y-1)*nx+(x ); if (x<nx-1) *(ind++) = (y-1)*nx+(x+1); if (x<nx-1) *(ind++) = (y )*nx+(x+1); if (x<nx-1 && y<ny-1) *(ind++) = (y+1)*nx+(x+1); if (x<nx-1 && y<ny-1) *(ind++) = (y+1)*nx+(x ); } } } scene.loadVertexShader(idVS, "shaders/terrain_v.cg"); scene.loadPixelShader(idPS, "shaders/terrain_p.cg"); scene.addPrimitive(idP,"Terrain"); //scene.addParam(idP, ChunkPrimParam::ORDER,"",0); scene.addParamID(idP, ChunkPrimParam::VSHADER,"",idVS); scene.addParamID(idP, ChunkPrimParam::PSHADER,"",idPS); scene.addParamID(idP, ChunkPrimParam::VBUFFER_ID,"position",idVB); scene.addParamID(idP, ChunkPrimParam::IBUFFER_ID,"",idIB); scene.addParamEnum(idP, ChunkPrimParam::PARAMVSHADER, "ModelViewProj", ChunkPrimParam::ModelViewProjection); scene.addParam(idP, ChunkPrimParam::PARAMVSHADER, "TextureScale", Vec2f(1.0f/nx,-1.0f/ny)); scene.addParamID(idP, ChunkPrimParam::TEXTURE, "texture", idT); scene.addParam(idP,ChunkPrimParam::TRANSFORM_SCALE,"",Vec3f(terrain.scale.x(),terrain.scale.y(),terrain.hscale*terrain.scale.z())); scene.addParam(idP,ChunkPrimParam::TRANSFORM_POSITION,"",Vec3f(-nx/2*terrain.scale.x(),-ny/2*terrain.scale.y(),terrain.hbase*terrain.scale.z())); scene.put(&pOut); flowvr::MessageWrite mt; mt.data = module->alloc(nx*ny*sizeof(short)); mt.stamps.write(terrainN[0],nx); mt.stamps.write(terrainN[1],ny); mt.stamps.write(terrainP[0],-nx/2*terrain.scale.x()); mt.stamps.write(terrainP[1],-ny/2*terrain.scale.y()); mt.stamps.write(terrainS[0],terrain.scale.x()); mt.stamps.write(terrainS[1],terrain.scale.y()); mt.stamps.write(terrainH[0],terrain.scale.z()*terrain.hbase); mt.stamps.write(terrainH[1],terrain.scale.z()*terrain.hscale); { short* height = terrain.height+(cy-ny/2)*terrain.nx+(cx-nx/2); for (int y=0;y<ny;y++) { memcpy(mt.data.getWrite<short>(y*nx*sizeof(short)), height, nx*sizeof(short)); height+=terrain.nx; } } module->put(&pTerrain, mt); // Fluid position { short fluidh = hmin+(short)(0.3f/terrain.hscale); short* height = terrain.height+(cy-ny/2)*terrain.nx+(cx-nx/2); for (int y=0;y<ny;y++) { for (int x=0;x<nx;x++) { if (height[x] <= fluidh) { if (x<xmin) xmin=x; else if (x>xmax) xmax=x; if (y<ymin) ymin=y; else if (y>ymax) ymax=y; } } height+=terrain.nx; } std::cout << "Fluid pos: <"<<xmin<<','<<ymin<<">-<"<<xmax<<','<<ymax<<") h="<<fluidh<<std::endl; flowvr::MessageWrite m; m.data = module->alloc(sizeof(Mat4x4f)); Mat4x4f& fluid = *m.data.getWrite<Mat4x4f>(); fluid.identity(); // Scale fluid[0][0] = (xmax-xmin+1)*terrain.scale.x(); fluid[1][1] = (ymax-ymin+1)*terrain.scale.y(); fluid[2][2] = terrain.scale.z(); // Position fluid[0][3] = (xmin-nx/2-0.5f)*terrain.scale.x(); fluid[1][3] = (ymin-ny/2-0.5f)*terrain.scale.y(); fluid[2][3] = (terrain.hbase+fluidh*terrain.hscale)*terrain.scale.z(); module->put(&pOutFluid,m); } module->wait(); flowvr::BufferPool pool; if (pRotX.isConnected()) { float rotSpeed = 0.01f; float speed = 0.04f; do { Vec3f depl; depl.x() = getSum(pTransX,position0.x(),speed); depl.y() = -getSum(pTransY,position0.y(),speed); getSum(pRotX,rotation.x(),rotSpeed); getSum(pRotY,rotation.y(),rotSpeed); get(pZoom,zoom); Mat3x3f mrot; Quat qx,qy,qz; qx.fromAngAxis(M_PI/2*rotation.x(),Vec3f(0,0,1)); qy.fromAngAxis(M_PI/2*rotation.y(),Vec3f(1,0,0)); qz.fromAngAxis(M_PI/2*rotation.z(),Vec3f(0,1,0)); Quat q = qz*qy*qx; q.toMatrix(&mrot); Vec3f newPos = position+mrot*depl; newPos.z() = terrain.getHeight(newPos.x(), newPos.y()); //+(zoom+1); position = newPos; Mat3x3f xrot; qx.toMatrix(&xrot); Vec3f pcam = position+xrot*(Vec3f(0,-2,0)*(1+zoom))+Vec3f(0,0,1.5); float zTer = terrain.getHeight(pcam.x(), pcam.y())+1; if (zTer > pcam.z()) pcam.z()=zTer; Mat4x4f m; m.identity(); m = mrot; m(0,3) = pcam[0]; m(1,3) = pcam[1]; m(2,3) = pcam[2]; Mat4x4f mcam; mcam.identity(); Mat3x3f rotcam; Quat qcam; qcam.fromAngAxis(-M_PI/2,Vec3f(1,0,0)); qcam.toMatrix(&rotcam); mcam = rotcam; m = m*mcam; scene.addParam(ID_CAMERA,ChunkPrimParam::TRANSFORM,"",m); mrot.identity(); m = mrot; m(0,3) = position[0]; m(1,3) = position[1]; m(2,3) = position[2]; flowvr::MessageWrite msg; msg.data = pool.alloc(module,sizeof(Mat4x4f)); *msg.data.getWrite<Mat4x4f>() = m; module->put(&pOutMat, msg); scene.put(&pOut); } while (module->wait()); } module->close(); return 0; }