void T3Dmesh::generateallvertexnormals() { int i,j,k; Tvec3d v; T4color cl; T2textureidx tidx(0,0); if (hasvertexnormals) return; calcnormals(); for (i=0; i<triangles.G_count(); i++) for (j=0; j<3; j++) { k=triangles[i].vertices[j]; ASSERT(k>=0); ASSERT(k<vertices.G_count()); if (vertices_normals[k].getsize()<0.000001) { v=vertices[k];vertices.add(Tvec3d(v.x,v.y,v.z)); v=triangles[i].facenormal;vertices_normals.add(Tvec3d(v.x,v.y,v.z)); vertices_idx.add(vertices_idx[k]); cl=vertices_colL[k];vertices_colL.add(T4color(cl.r,cl.g,cl.b,cl.a)); cl=vertices_colR[k];vertices_colR.add(T4color(cl.r,cl.g,cl.b,cl.a)); tidx=vertices_texcoord[k];vertices_texcoord.add(T2textureidx(tidx.tx1,tidx.tx2)); triangles[i].vertices[j]=vertices.G_count()-1; trianglesindices[3*i+j]=vertices.G_count()-1; } } calcedges(); hasvertexnormals=true; }
void mesh(Mesh* mymesh){ register int i; vecp v; matp m; int *ip,calculatedisplaykoordinates=1; i=mymesh->mode; if ((!mymesh->normalsinitialized) &&((i==MESHONESIDED)||(i==SQUAREMESHONESIDED))) { calcnormals(&mymesh->Garray[0],1); mymesh->normalsinitialized=1; /*calculatedisplaykoordinates=0*/ } ip=mymesh->transf2dc; meq(mymesh->mc2wcmn,getmc2wcmn()); meq(mymesh->mc2wcm,getmc2wcm()); veq(mymesh->mc2wcv,getmc2wcv()); m=mymesh->mc2wcm; v=mymesh->mc2wcv; for (i=0;i<mymesh->xdim*mymesh->ydim;i++){ veq(mymesh->tva[i],vsum(matvecmul(m,mymesh->va[i]),v)); *(ip++)=0; } if (calculatedisplaykoordinates) for (i=0;i<mymesh->xdim*mymesh->ydim;i++) wc2dc(&mymesh->points[i].x,&mymesh->points[i].y,mymesh->tva[i]); meq(mymesh->mc2wcmn,getmc2wcmn()); for (i=0;i<(mymesh->xdim-1)*(mymesh->ydim-1);i++)insertGobject(&mymesh->Garray[i]); }
void T3Dmesh::copyfrom(const CSGShape &shape) { reset(); Titemarray<CSGVector> tmp_normals; for (int i=0; i<shape.G_vertexcount(); i++) { addvertex(&Tvec3d(shape.G_vertex(i).x,shape.G_vertex(i).y,shape.G_vertex(i).z),i); tmp_normals.add(CSGVector(0,0,0)); vertices_label[vertices.G_count()-1]=-1; } for (int i=0; i<shape.G_facecount(); i++) { const CSGFace &face=shape.G_face(i); CSGVertex* vs[3]; const CSGVector *ns[3]; int idxs[3]; vs[0]=face.v1;vs[1]=face.v2;vs[2]=face.v3; ns[0]=&face.n1;ns[1]=&face.n2;ns[2]=&face.n3; for (int j=0; j<3; j++) { int oidx=vs[j]->idx;idxs[j]=oidx; bool makenew=false; if ((vertices_label[oidx]>=0)&&(face.label!=vertices_label[oidx])) makenew=true; if (!ns[j]->IsZero()) { if (!tmp_normals[oidx].IsZero()) { if (!ns[j]->IsSame(tmp_normals[oidx])) makenew=true; } } if (makenew) { Tvec3d v0=vertices[oidx]; idxs[j]=addvertex(&v0,oidx); vertices_label[idxs[j]]=face.label; vertices_normals[idxs[j]].set(ns[j]->x,ns[j]->y,ns[j]->z); } else { tmp_normals[oidx]=*ns[j]; vertices_label[oidx]=face.label; vertices_normals[oidx].set(ns[j]->x,ns[j]->y,ns[j]->z); } } addtriangle(idxs[0],idxs[1],idxs[2]); } hasvertexnormals=true; calcnormals(); calcedges(); hasowncolors=false; }
void T3Dmesh::warp(Tspacewarper &w) { generateallvertexnormals(); Tvertex vin,vout; Tvector nin,nout; for (int i=0; i<vertices.G_count(); i++) { vin.Set3(vertices[i].x,vertices[i].y,vertices[i].z); w.warpvertex(&vin,&vout); vertices[i]=vout; nin.Set(vertices_normals[i].x,vertices_normals[i].y,vertices_normals[i].z); w.warpnormal(&vin,&nin,&nout); vertices_normals[i]=nout; } calcnormals(); }
void T3Dmesh::createsphere(Tvec3d ¢, double radius, int resol) { int i,j,i1,i2,j1,j2; double al1,al2; Tvec3d norm,vertex; int ct=0; for (i=0; i<=2*resol-1; i++) { al1=(i*1.0)/resol*Pi; for (j=0; j<=resol; j++) { al2=(j*1.0)/resol*Pi; norm.x=(V3DFLOAT)(sin(al2)*cos(al1)); norm.y=(V3DFLOAT)(sin(al2)*sin(al1)); norm.z=(V3DFLOAT)(cos(al2)); vertex.lincomb(cent,norm,1,radius); addvertex(&vertex,ct,&norm,al1/(2*Pi)+0.5,1.0-al2/Pi); ct++; } } for (i=0; i<=2*resol-1; i++) { i1=i;i2=(i+1)%(2*resol); for (j=0; j<resol; j++) { j1=j; j2=j+1; addtriangle(i1*(resol+1)+j2,i2*(resol+1)+j2,i2*(resol+1)+j1); addtriangle(i1*(resol+1)+j2,i2*(resol+1)+j1,i1*(resol+1)+j1); } } calcnormals(); calcedges(); hasowncolors=false; }
void T3Dmesh::Make_stripbased(int irowsize, bool iclosed) { int i,j,i1,i2,j1,j2; calccenter(); ASSERT(hasvertexnormals); if (!hasvertexnormals) throw QError(_text("Strip based mesh should have vertex normals defined")); if (irowsize==0) return; strips_rowsize=irowsize; strips_closed=iclosed; int colsize=vertices.G_count()/irowsize; int colsize2=colsize-1; if (strips_closed) { for (j=0; j<strips_rowsize; j++) vertices_idx[(colsize-1)*strips_rowsize+j]=vertices_idx[j]; } for (i=0; i<colsize2; i++) { i1=i;i2=(i+1)%colsize; for (j=0; j<strips_rowsize-1; j++) { j1=j; j2=j+1; addtriangle(i1*strips_rowsize+j2,i2*strips_rowsize+j2,i2*strips_rowsize+j1); addtriangle(i1*strips_rowsize+j2,i2*strips_rowsize+j1,i1*strips_rowsize+j1); } } calcnormals(); calcedges(); }
void T3Dmesh::subtriangle(double maxdist) { int v1,v2,tr1,tr2,vnew,maxidx,vtr1,vtr2; Tvec3d newnorm; generateallvertexnormals(); calcedges(); maxidx=0; for (int i=0; i<vertices_idx.G_count(); i++) if (vertices_idx[i]>maxidx) maxidx=vertices_idx[i]; for (int edgenr=0; edgenr<edges.G_count();) { v1=edges[edgenr].vertices[0]; v2=edges[edgenr].vertices[1]; tr1=edges[edgenr].triangles[0]; tr2=edges[edgenr].triangles[1]; if (Tvec3d::distance(vertices[v1],vertices[v2])>maxdist) { vnew=vertices.G_count(); vertices.add(Tvec3d((vertices[v1].x+vertices[v2].x)/2,(vertices[v1].y+vertices[v2].y)/2,(vertices[v1].z+vertices[v2].z)/2)); vertices_colL.add(T4color((vertices_colL[v1].r+vertices_colL[v2].r)/2,(vertices_colL[v1].g+vertices_colL[v2].g)/2,(vertices_colL[v1].b+vertices_colL[v2].b)/2,(vertices_colL[v1].a+vertices_colL[v2].a)/2)); vertices_colR.add(T4color((vertices_colR[v1].r+vertices_colR[v2].r)/2,(vertices_colR[v1].g+vertices_colR[v2].g)/2,(vertices_colR[v1].b+vertices_colR[v2].b)/2,(vertices_colR[v1].a+vertices_colR[v2].a)/2)); vertices_texcoord.add(T2textureidx((vertices_texcoord[v1].tx1+vertices_texcoord[v2].tx1)/2,(vertices_texcoord[v1].tx2+vertices_texcoord[v2].tx2)/2)); maxidx++;vertices_idx.add(maxidx); newnorm.lincomb(vertices_normals[v1],vertices_normals[v2],1,1);newnorm.norm(); vertices_normals.add(Tvec3d(newnorm.x,newnorm.y,newnorm.z)); ASSERT(tr1>=0); vtr1=-1; if (triangles[tr1].vertices[0]==v1) vtr1=0; if (triangles[tr1].vertices[1]==v1) vtr1=1; if (triangles[tr1].vertices[2]==v1) vtr1=2; ASSERT(vtr1>=0); vtr2=(vtr1+1)%3; ASSERT(triangles[tr1].vertices[vtr2]==v2); // triangles[tr1].vertices[vtr2]=vnew;trianglesindices[3*tr1+vtr2]=vnew; // addtriangle(vnew,v2,v1); if (tr2>=0) { vtr1=-1; if (triangles[tr2].vertices[0]==v1) vtr1=0; if (triangles[tr2].vertices[1]==v1) vtr1=1; if (triangles[tr2].vertices[2]==v1) vtr1=2; ASSERT(vtr1>=0); vtr2=(vtr1+2)%3; ASSERT(triangles[tr2].vertices[vtr2]==v2); // triangles[tr2].vertices[vtr2]=vnew;trianglesindices[3*tr2+vtr2]=vnew; // addtriangle(vnew,v2,v1); } edges[edgenr].vertices[1]=vnew; edgenr++; //... ondoenbaar ingewikkeld omwille van verschillende vertices met shared index! } else edgenr++; } calcnormals(); // calcedges(); }