Keyframe::Keyframe(map<Vector3f, SharkVertex*, compareVect3> *rawVerts, vector<Quad*> *rawFaces) { //deep copy verts over. map<Vector3f, SharkVertex*, compareVect3>::iterator im; for(im = rawVerts->begin(); im != rawVerts->end(); im++) { pair<Vector3f, SharkVertex*> serk = *im; SharkVertex * d = new SharkVertex(); d->sNormal(serk.second->gNormal()); d->sTransformed(serk.second->gTransformed()); d->sLocal(serk.second->gLocal()); uVertices.insert(pair<Vector3f, SharkVertex*>(serk.first, d)); } //create faces. vector<Quad*>::iterator iq; for(iq = rawFaces->begin(); iq != rawFaces->end(); iq++) { Quad * nRect = new Quad(); for(int i = 0; i < 4; i++) { nRect->sVert(i, uVertices.find((*iq)->gLocalVert(i))->second); } faces.push_back(nRect); } //setNormals createQuads(); }
/*Subroutine of transformHeiararchy(), this function calculates the position and rotation for a given bone segment and * generates quaderlaterals with those transformations */ void Keyframe::segmentMatrixMake( GLfloat segmentRot[], GLfloat segLength[], Mesh *mesh, int segments, glQuaternion *glQuat, int curSegment, MyMat *stackMatrix, float start, float end, int isDownstream) { double rotate = segmentRot[curSegment]; //get rotation of current segment //current shark model segment MyMat Matrix = *stackMatrix; MyMat transrix; transrix.makeTranslate(Vector3f((isDownstream == 2 ? segLength[curSegment] : -segLength[curSegment]), 0, 0)); MyMat secondStack = MyMat(); GLfloat glm[16]; glQuat->CreateFromAxisAngle(0,1,0,isDownstream == 2? -rotate : rotate); glQuat->CreateMatrix(glm); MyMat rotatrix = MyMat(glm[0], glm[4], glm[8], glm[12], glm[1], glm[5],glm[9], glm[13],glm[2],glm[6],glm[10],glm[14],glm[3],glm[7], glm[11],glm[15]); Matrix = Matrix.multRight(rotatrix); //roatation goes before translates Matrix = Matrix.multRight(transrix); //doesnt matter which order translates are done *stackMatrix = Matrix; //advance heirarchy MyMat transrix2; transrix2.makeTranslate(Vector3f(isDownstream == 2? -end : -start, 0, 0)); Matrix = Matrix.multRight(transrix2); //------------------------------------ float center; for(int in = 0; in < mesh->vertCounter; in+=4) { //find center of face center = (mesh->vertList[in].x + mesh->vertList[in+1].x + mesh->vertList[in+2].x + mesh->vertList[in+3].x ) / 4; //draw if face is between start and end if(center >= start && center <= end ) { Quad *curQuad = new Quad(); curQuad->gNormal() = Vector3f(0,0,0); for(int corn = 0; corn < 4; corn++) //corner iteration { SharkVertex *curVert = new SharkVertex(); //current vertex curVert->local = mesh->vertList[in+corn]; //setting untransformed vertex curVert->transformed = Vector3f(Matrix.multVec(mesh->vertList[in+corn], true)); //setting transformed vertex curVert->normal = Vector3f(0,0,0); //normal of the vertex, away from mesh map<Vector3f, SharkVertex*, compareVect3>::iterator findTest = uVertices.find(mesh->vertList[in+corn]); //checking to see if vertex is already in the list. if(findTest == uVertices.end()) { //vertex not in list. Add it to the list and to the quad. uVertices.insert(pair<Vector3f, SharkVertex*>(mesh->vertList[in+corn] , curVert)); curQuad->sVert(corn, curVert); } else //vertex is in the list, so it's added to the existing quad. { delete curVert; curQuad->sVert(corn, (*findTest).second); } curQuad->gNormal() += Matrix.multVec(mesh->normals[in+corn], false); //setting normal of the face }//end corners curQuad->gNormal() /= 4.0; //take average of normals to get the actual normal. curQuad->sBoneNo(curSegment); //record the bone this quad belongs to the most. faces.push_back(curQuad); }//end if }//end quads }
void SharkMesh::buildAOBJ(FILE* readFile) { vector<Vector3f> localVerts = vector<Vector3f>(); //in order to use OBJs as keys, need to remember their order. while(!feof(readFile)) { //tokenize the line identifier. It's only one character and //it should be on the first on the line char identifier = fgetc(readFile); if(ferror(readFile)){ printf("888888888Error reading FILE\n"); exit(-1); } //load in the vertices //v x y z nx ny nz boneName/weight boneName/weight ... if(identifier == 'v') { char cur = fgetc(readFile); //space if(ferror(readFile)) { printf("0Error reading FILE\n"); exit(-1); } while(cur != '\n') { // per line SharkVertex* sv = new SharkVertex(); //location of vertex Vector3f vert; vert.x = atof(nextToken(' ', readFile).c_str()); vert.y = atof(nextToken(' ', readFile).c_str()); vert.z = atof(nextToken(' ', readFile).c_str()); sv->sLocal(vert); Vector3f nor; //normal nor.x = atof(nextToken(' ', readFile).c_str()); nor.y = atof(nextToken(' ', readFile).c_str()); nor.z = atof(nextToken(' ', readFile).c_str()); sv->sNormal(nor*-1.0); //TODO magic. fix blender normals. shouldn't need to reverse them. sv->sTransformed(Vector3f(0,0,0)); //bone / weight repeats fseek(readFile, -1, SEEK_CUR); cur = fgetc(readFile); while(cur != '\n') { string boneName = nextToken('/', readFile); float weight = atof(nextToken(' ', readFile).c_str()); sv->sBonePair(boneName, weight); fseek(readFile, -1, SEEK_CUR); cur = fgetc(readFile); } localVerts.push_back(vert); insertVec(pair<Vector3f, SharkVertex*>(vert, sv)); } } //faces //f ... vertices in mesh ..... else if(identifier == 'f') { char cur = fgetc(readFile); //space if(ferror(readFile)){ printf("4Error reading FILE\n"); exit(-1);} Quad *curQuad = new Quad(); curQuad->sNormal(Vector3f(0,0,0)); //caution. Vertices listed in mesh may not be consistant with other quads int vertex1 = atoi(nextToken(' ', readFile).c_str()); int vertex2 = atoi(nextToken(' ', readFile).c_str()); int vertex3 = atoi(nextToken(' ', readFile).c_str()); int vertex4 = atoi(nextToken(' ', readFile).c_str()); curQuad->sVert(0, gVertex(localVerts[vertex1-1])); curQuad->sVert(1, gVertex(localVerts[vertex2-1])); curQuad->sVert(2, gVertex(localVerts[vertex3-1])); curQuad->sVert(3, gVertex(localVerts[vertex4-1])); curQuad->calcNormal(); pushFace(curQuad); //neighboring quads are to be found later, after parsing is done. fseek(readFile, -1, SEEK_CUR); cur = fgetc(readFile); while(cur != '\n') { cur = fgetc(readFile); if(ferror(readFile)) { printf("5Error reading FILE\n"); exit(-1); } } } else if(identifier == 'b') { return; } } }