void KHalfEdgeMeshPrivate::calculateFaceNormals() { for (Face &f : m_faces) { f.normal = calculateFaceNormal(&f); } }
void Mesh::updateNormal(Vertex *v) { uint vertexIndex; std::vector<glm::uvec3> faces; std::vector<glm::vec3> normals; for (uint i=0; i<indices.size(); i++) { bool included = false; for (uint j=0; j<3; j++) { if(&vertices[indices[i][j]] == v) { faces.push_back(indices[i]); vertexIndex = indices[i][j]; glm::vec3 normal; calculateFaceNormal(normal, i); normals.push_back(normal); included = true; break; } } if(!included) { normals.push_back(glm::vec3(0)); } } //std::cout << "updating normal " << vertexIndex << std::endl; calculateVertexNormal(normals, vertexIndex); }
void Mesh::calculateFaceNormals(std::vector<glm::vec3> &normals) { //std::cout << "calculating face normals.. "; //std::cout.flush(); for (uint i=0; i<indices.size(); i++) { glm::vec3 normal; calculateFaceNormal(normal, i); normals.push_back(normal); } //std::cout << "done." << std::endl; }
void meshModels::cacheEndCap(float vertsX[], float vertsY[], float depth) { vertex_t vertA, vertB, vertC; polygon_t poly; int j = 0; int vCount = model.verts.size(); int pCount = model.polys.size(); poly.a = vCount; // define the first point of our triangle poly.b = vCount + 1; // store initial values that should be correct poly.c = vCount + 2; // establish center point // this should be calculated from bounds vertA.x= 0.0f; vertA.y = 0.0f; vertA.z = depth; //model.verts.reserve(0); model.verts.push_back(vertA); vCount = model.verts.size()+1; int endGon = vCount; // saving this vert id for later // establish the second triangle point along the perimiter vertB.x = vertsX[j]; vertB.y = vertsY[j]; vertB.z = depth; //model.verts.reserve(vCount); model.verts.push_back(vertB); // creating 32 polygons for the end cap for(j = 1; j < 32; ++j) { vCount = model.verts.size(); pCount = model.polys.size(); //establish the final triangle point along the perimiter vertC.x = vertsX[j]; vertC.y = vertsY[j]; vertC.z = depth; // here is where I want to check if my vert exists // if it does we should use that vert. // if not then we can create a new vert in the array int newVert = vertCheck(vertC); if(newVert >= vCount) { //model.verts.reserve(vCount+1); model.verts.push_back(vertC); poly.b = vCount; poly.c = vCount - 1; model.polys.push_back(poly); calculateFaceNormal(pCount); }else{ // set the polygon poly.c = poly.b; poly.b = newVert; bool skip = FALSE; // if the values of vert c xyz and vert b xyz are the // same the face is really a line and can be skipped if (model.verts[poly.c].x == model.verts[newVert].x && model.verts[poly.c].y == model.verts[newVert].y && model.verts[poly.c].z == model.verts[newVert].z) { skip = TRUE; } if(skip == FALSE) { model.polys.push_back(poly); calculateFaceNormal(pCount); } } } vCount = model.verts.size(); pCount = model.polys.size(); poly.b = endGon - 1; poly.c = vCount - 1; model.polys.reserve(pCount + 1); model.polys.push_back(poly); calculateFaceNormal(pCount); }
void meshModels::cacheCylinder(float vertsX[], float vertsY[], float depth) { dir = TRUE; cacheEndCap( vertsX, vertsY, 0.0f); dir = FALSE; cacheEndCap( vertsX, vertsY, depth); dir = FALSE; // objects to build our vertices in vertex_t vertA, vertB, vertC; polygon_t poly; int i = 0; int j; int faceCount; // find out how many verts we have int vcount = model.verts.size(); int endGon = vcount; // and saving the starting vertice index for later //record all three points as a triangle in our structure poly.a = vcount; poly.b = vcount +1; poly.c = vcount +2; // we need to start with two verts because then //we only need to add 2 verts to get 2 new triangles in the for loop // build our first vertex vertA.x = vertsX[i]; vertA.y = vertsY[i]; vertA.z = 0.0f; // push it into our array model.verts.reserve(poly.a); model.verts.push_back(vertA); //build our third vertex vertB.x = vertsX[i]; vertB.y = vertsY[i]; vertB.z = depth; // push it into our array model.verts.reserve(poly.b); model.verts.push_back(vertB); i++; // build a cylinder 2 triangles at a time for( j = 0; j < 62; ++j) { // ensure our array iterator is not too large if(i>=32) i = 0; //record all three points as a triangle in our structure poly.a = vcount; poly.b = vcount +1; poly.c = vcount +2; //build our second vertex vertC.x = vertsX[i]; vertC.y = vertsY[i]; vertC.z = 0.0f; // push it into our array model.verts.reserve(poly.c); model.verts.push_back(vertC); // record the triangle model.polys.reserve(j); model.polys.push_back(poly); faceCount = model.polys.size()-1; calculateFaceNormal(faceCount); j++; // advance to the next polygon // vertex A is really the old vertex b so lets record the change here poly.a = poly.b; vertB.x = vertsX[i]; vertB.y = vertsY[i]; vertB.z = depth; poly.b = model.verts.size(); // set the iterator for the new vertex model.verts.reserve(poly.b); model.verts.push_back(vertB); // VertC is the same and remains unchanged // lets store our triangle model.polys.reserve(j); model.polys.push_back(poly); faceCount = model.polys.size()-1; calculateFaceNormal(faceCount); vcount = vcount +2; i++; } // add our last two triangles using existing verts poly.a = model.verts.size() - 2; poly.b = model.verts.size() - 1; poly.c = endGon; model.polys.reserve(vcount); model.polys.push_back(poly); faceCount = model.polys.size()-1; calculateFaceNormal(faceCount); // add our last two triangles using existing verts poly.a = model.verts.size() - 1; poly.b = endGon + 1; poly.c = endGon; model.polys.reserve(vcount + 1); model.polys.push_back(poly); faceCount = model.polys.size()-1; calculateFaceNormal(faceCount); normalizeVerts(); makeShader(); makeBuffer(); }
sgShadowVolume::sgShadowVolume(sgObject *obj, unsigned int lod) { //Set object and create a mesh object = obj; sgObjectBody *lodstep = object->body; for(int i = 0; i < lod; i++) lodstep = lodstep->nextbody; mesh = new sgMesh; //Get vertex and face count unsigned int vertexnum = 0; unsigned int indexnum = 0; for(int i = 0; i < lodstep->meshs.size(); i++) { vertexnum += lodstep->meshs[i]->vertexnum; indexnum += lodstep->meshs[i]->indexnum; } //Create vertex and face array vertices = new sgVertex[vertexnum]; faces = new sgShadowFace[indexnum/3]; //Init the vertex array vertexnum = 0; for(int i = 0; i < lodstep->meshs.size(); i++) { for(int n = 0; n < lodstep->meshs[i]->vertexnum; n++) { memcpy(&(vertices[vertexnum+n]), lodstep->meshs[i]->vertices+n*lodstep->meshs[i]->vtxsize, sizeof(sgVertex)); } vertexnum += lodstep->meshs[i]->vertexnum; } //Create array of unique edges and initialize the faces std::vector<sgShadowEdge> edgesvec; sgShadowEdge tempedge1; sgShadowEdge tempedge2; sgShadowEdge tempedge3; unsigned int edgeincl1; unsigned int edgeincl2; unsigned int edgeincl3; vertexnum = 0; unsigned int realn = 0; for(int i = 0; i < lodstep->meshs.size(); i++) { unsigned int n = 0; for(n = 0; n < lodstep->meshs[i]->indexnum; n += 3) { tempedge1.verts = vertices; tempedge1.vert1 = lodstep->meshs[i]->indices[n+0]+vertexnum; tempedge1.vert2 = lodstep->meshs[i]->indices[n+1]+vertexnum; tempedge1.counter1 = 1; tempedge1.counter2 = 0; tempedge2.verts = vertices; tempedge2.vert1 = lodstep->meshs[i]->indices[n+1]+vertexnum; tempedge2.vert2 = lodstep->meshs[i]->indices[n+2]+vertexnum; tempedge2.counter1 = 1; tempedge2.counter2 = 0; tempedge3.verts = vertices; tempedge3.vert1 = lodstep->meshs[i]->indices[n+2]+vertexnum; tempedge3.vert2 = lodstep->meshs[i]->indices[n+0]+vertexnum; tempedge3.counter1 = 1; tempedge3.counter2 = 0; calculateFaceNormal(&faces[realn], &vertices[lodstep->meshs[i]->indices[n]+vertexnum], &vertices[lodstep->meshs[i]->indices[n+2]+vertexnum], &vertices[lodstep->meshs[i]->indices[n+1]+vertexnum]); faces[realn].flipped1 = false; faces[realn].flipped2 = false; faces[realn].flipped3 = false; edgeincl1 = -1; edgeincl2 = -1; edgeincl3 = -1; for(int x = 0; x < edgesvec.size(); x++) { if(edgesvec[x] == tempedge1) { edgeincl1 = x; faces[realn].flipped1 = edgesvec[x].lastaddor; if(edgesvec[x].lastaddor) edgesvec[x].counter2 += 1; else edgesvec[x].counter1 += 1; break; } } for(int x = 0; x < edgesvec.size(); x++) { if(edgesvec[x] == tempedge2) { edgeincl2 = x; faces[realn].flipped2 = edgesvec[x].lastaddor; if(edgesvec[x].lastaddor) edgesvec[x].counter2 += 1; else edgesvec[x].counter1 += 1; break; } } for(int x = 0; x < edgesvec.size(); x++) { if(edgesvec[x] == tempedge3) { edgeincl3 = x; faces[realn].flipped3 = edgesvec[x].lastaddor; if(edgesvec[x].lastaddor) edgesvec[x].counter2 += 1; else edgesvec[x].counter1 += 1; break; } } if(edgeincl1 == -1) { edgesvec.push_back(tempedge1); faces[realn].edge1 = edgesvec.size()-1; }else { faces[realn].edge1 = edgeincl1; } if(edgeincl2 == -1) { edgesvec.push_back(tempedge2); faces[realn].edge2 = edgesvec.size()-1; }else { faces[realn].edge2 = edgeincl2; } if(edgeincl3 == -1) { edgesvec.push_back(tempedge3); faces[realn].edge3 = edgesvec.size()-1; }else { faces[realn].edge3 = edgeincl3; } realn += 1; } vertexnum += lodstep->meshs[i]->vertexnum; } edges = new sgShadowEdge[edgesvec.size()]; tempedges = new sgShadowEdge[edgesvec.size()]; memcpy(edges, &edgesvec[0], edgesvec.size()*sizeof(sgShadowEdge)); edgenum = edgesvec.size(); facenum = indexnum/3; }