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::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::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; }
/* generates the sphere mesh vertices and the corresponding texture coordinates on the circumscribed cube */ void genvertexcoords(void) { int i,j,facenum; float vert1[3], vert2[3], vert3[3]; float tex1[3], tex2[3], tex3[3]; float theta1,theta2,phi1,phi2,dtheta,dphi; dtheta = PI/((float)Ntheta+1); dphi = 2.0*PI/((float)Nphi); link tlist[6]; //set up pointers for linked lists we are about to populate for (facenum=0;facenum<6;facenum++) { //these nodes point to the head of the linked lists for each face; //they merely provide an entry into the linked list and contain no data: triangles_on_face[facenum] = malloc(sizeof *triangles_on_face[facenum]); //set the current pointer to point initially to the heads: tlist[facenum] = triangles_on_face[facenum]; } /* top cap */ for (j=0; j<Nphi; j++) { vert1[0] = 0.0f; vert1[1] = 0.0f; vert1[2] = (float)radius; vert2[0] = (float)radius*sin(dtheta)*cos(j*dphi); vert2[1] = (float)radius*sin(dtheta)*sin(j*dphi); vert2[2] = (float)radius*cos(dtheta); vert3[0] = (float)radius*sin(dtheta)*cos((j+1)*dphi); vert3[1] = (float)radius*sin(dtheta)*sin((j+1)*dphi); vert3[2] = (float)radius*cos(dtheta); projecttocube(vert1,&facenum,tex1); projecttoface(vert2,facenum,tex2); projecttoface(vert3,facenum,tex3); addtriangle(&tlist[facenum],vert1,vert2,vert3,tex1,tex2,tex3); } /* bottom cap */ for (j=0; j<Nphi; j++) { vert1[0] = 0.0f; vert1[1] = 0.0f; vert1[2] = -(float)radius; vert2[0] = (float)radius*sin(PI-dtheta)*cos(j*dphi); vert2[1] = (float)radius*sin(PI-dtheta)*sin(j*dphi); vert2[2] = (float)radius*cos(PI-dtheta); vert3[0] = (float)radius*sin(PI-dtheta)*cos((j+1)*dphi); vert3[1] = (float)radius*sin(PI-dtheta)*sin((j+1)*dphi); vert3[2] = (float)radius*cos(PI-dtheta); projecttocube(vert1,&facenum,tex1); projecttoface(vert2,facenum,tex2); projecttoface(vert3,facenum,tex3); addtriangle(&tlist[facenum],vert1,vert2,vert3,tex1,tex2,tex3); } /* bulk of sphere */ for (i=1; i<Ntheta+1; i++) { for (j=0; j<Nphi; j++) { theta1 = (float)i*dtheta; phi1 = (float)j*dphi; theta2 = (float)(i+1)*dtheta; phi2 = (float)(j+1)*dphi; /* triangle 1 */ vert1[0] = (float)radius*sin(theta1)*cos(phi1); vert1[1] = (float)radius*sin(theta1)*sin(phi1); vert1[2] = (float)radius*cos(theta1); vert2[0] = (float)radius*sin(theta2)*cos(phi1); vert2[1] = (float)radius*sin(theta2)*sin(phi1); vert2[2] = (float)radius*cos(theta2); vert3[0] = (float)radius*sin(theta2)*cos(phi2); vert3[1] = (float)radius*sin(theta2)*sin(phi2); vert3[2] = (float)radius*cos(theta2); projecttocube(vert1,&facenum,tex1); projecttoface(vert2,facenum,tex2); projecttoface(vert3,facenum,tex3); addtriangle(&tlist[facenum],vert1,vert2,vert3,tex1,tex2,tex3); /* triangle 2 */ vert1[0] = (float)radius*sin(theta1)*cos(phi1); vert1[1] = (float)radius*sin(theta1)*sin(phi1); vert1[2] = (float)radius*cos(theta1); vert2[0] = (float)radius*sin(theta2)*cos(phi2); vert2[1] = (float)radius*sin(theta2)*sin(phi2); vert2[2] = (float)radius*cos(theta2); vert3[0] = (float)radius*sin(theta1)*cos(phi2); vert3[1] = (float)radius*sin(theta1)*sin(phi2); vert3[2] = (float)radius*cos(theta1); projecttocube(vert1,&facenum,tex1); projecttoface(vert2,facenum,tex2); projecttoface(vert3,facenum,tex3); addtriangle(&tlist[facenum],vert1,vert2,vert3,tex1,tex2,tex3); } } }
void T3Dmesh::addquad(int i1, int i2, int i3, int i4) { addtriangle(i1,i2,i3); addtriangle(i3,i4,i1); }