Changeset 4149 in orxonox.OLD
- Timestamp:
- May 10, 2005, 6:04:16 PM (19 years ago)
- Location:
- orxonox/branches/md2_loader/src/lib/graphics/importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/md2_loader/src/lib/graphics/importer/abstract_model.h
r4148 r4149 119 119 #define Mag(Normal) (sqrt(Normal.x*Normal.x + Normal.y*Normal.y + Normal.z*Normal.z)) 120 120 121 // vector between two points 122 CVector3 VectorDiff(CVector3 vPoint1, CVector3 vPoint2) 123 { 124 CVector3 vVector; 121 class MathHelp { 125 122 126 vVector.x = vPoint1.x - vPoint2.x; 127 vVector.y = vPoint1.y - vPoint2.y; 128 vVector.z = vPoint1.z - vPoint2.z; 123 public: 124 // vector between two points 125 static CVector3 VectorDiff(CVector3 vPoint1, CVector3 vPoint2) 126 { 127 CVector3 vVector; 129 128 130 return vVector; 131 } 129 vVector.x = vPoint1.x - vPoint2.x; 130 vVector.y = vPoint1.y - vPoint2.y; 131 vVector.z = vPoint1.z - vPoint2.z; 132 132 133 // adding vectors, returning result 134 CVector3 AddVector(CVector3 vVector1, CVector3 vVector2) 135 { 136 CVector3 vResult; 133 return vVector; 134 } 135 136 // adding vectors, returning result 137 static CVector3 AddVector(CVector3 vVector1, CVector3 vVector2) 138 { 139 CVector3 vResult; 137 140 138 139 140 141 vResult.x = vVector2.x + vVector1.x; 142 vResult.y = vVector2.y + vVector1.y; 143 vResult.z = vVector2.z + vVector1.z; 141 144 142 143 }145 return vResult; 146 } 144 147 145 // This divides a vector by a single number (scalar) and returns the result146 CVector3 DivideVectorByScaler(CVector3 vVector1, float Scaler)147 {148 148 // This divides a vector by a single number (scalar) and returns the result 149 static CVector3 DivideVectorByScaler(CVector3 vVector1, float Scaler) 150 { 151 CVector3 vResult; 149 152 150 151 152 153 vResult.x = vVector1.x / Scaler; 154 vResult.y = vVector1.y / Scaler; 155 vResult.z = vVector1.z / Scaler; 153 156 154 155 }157 return vResult; 158 } 156 159 157 // cross product between 2 vectors158 CVector3 CrossProduct(CVector3 vVector1, CVector3 vVector2)159 {160 161 162 163 164 165 }160 // cross product between 2 vectors 161 static CVector3 CrossProduct(CVector3 vVector1, CVector3 vVector2) 162 { 163 CVector3 vCross; 164 vCross.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y)); 165 vCross.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z)); 166 vCross.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)); 167 return vCross; 168 } 166 169 167 // calculate the normal of a vector168 CVector3 NormalizeVector(CVector3 vNormal)169 {170 171 172 173 174 170 // calculate the normal of a vector 171 static CVector3 NormalizeVector(CVector3 vNormal) 172 { 173 double Magnitude; 174 Magnitude = Mag(vNormal); 175 vNormal.x /= (float)Magnitude; 176 vNormal.y /= (float)Magnitude; 177 vNormal.z /= (float)Magnitude; 175 178 176 177 }178 179 return vNormal; 180 } 181 }; 179 182 180 183 #endif /* _ABSTRACT_MODEL_H */ -
orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc
r4148 r4149 308 308 void MD2Loader::computeNormals(t3DModel *pModel) 309 309 { 310 CVector3 vVector1, vVector2, vNormal, vPoly[3]; 311 312 if( unlikely(pModel->numOfObjects <= 0)) 313 return; 314 /* now computing face normals: this means just averaging the vertex normals of a face */ 315 /* so for every object: */ 316 for(int index = 0; index < pModel->numOfObjects; index++) 317 { 318 t3DObject *pObject = &(pModel->objectList[index]); 319 320 /* allocate all the memory we need to calculate the normals */ 321 CVector3 *pNormals = new CVector3 [pObject->numOfFaces]; 322 CVector3 *pTempNormals = new CVector3 [pObject->numOfFaces]; 323 pObject->pNormals = new CVector3 [pObject->numOfVerts]; 324 325 for(int i=0; i < pObject->numOfFaces; i++) 310 CVector3 vVector1, vVector2, vNormal, vPoly[3]; 311 312 if( unlikely(pModel->numOfObjects <= 0)) 313 return; 314 /* now computing face normals: this means just averaging the vertex normals of a face */ 315 /* so for every object: */ 316 for(int index = 0; index < pModel->numOfObjects; index++) 317 { 318 t3DObject *pObject = &(pModel->objectList[index]); 319 320 /* allocate all the memory we need to calculate the normals */ 321 CVector3 *pNormals = new CVector3 [pObject->numOfFaces]; 322 CVector3 *pTempNormals = new CVector3 [pObject->numOfFaces]; 323 pObject->pNormals = new CVector3 [pObject->numOfVerts]; 324 325 for(int i=0; i < pObject->numOfFaces; i++) 326 { 327 /* cache the points to make coding easier :) */ 328 vPoly[0] = pObject->pVerts[pObject->pFaces[i].vertIndex[0]]; 329 vPoly[1] = pObject->pVerts[pObject->pFaces[i].vertIndex[1]]; 330 vPoly[2] = pObject->pVerts[pObject->pFaces[i].vertIndex[2]]; 331 332 vVector1 = MathHelp::VectorDiff(vPoly[0], vPoly[2]); 333 vVector2 = MathHelp::VectorDiff(vPoly[2], vPoly[1]); 334 335 vNormal = MathHelp::CrossProduct(vVector1, vVector2); 336 pTempNormals[i] = vNormal; 337 vNormal = MathHelp::NormalizeVector(vNormal); 338 339 pNormals[i] = vNormal; 340 } 341 342 /* now calculating vertex normals */ 343 CVector3 vSum = {0.0, 0.0, 0.0}; 344 CVector3 vZero = vSum; 345 int shared=0; 346 347 for (int i = 0; i < pObject->numOfVerts; i++) 348 { 349 for (int j = 0; j < pObject->numOfFaces; j++) 350 { 351 if (pObject->pFaces[j].vertIndex[0] == i || 352 pObject->pFaces[j].vertIndex[1] == i || 353 pObject->pFaces[j].vertIndex[2] == i) 326 354 { 327 /* cache the points to make coding easier :) */ 328 vPoly[0] = pObject->pVerts[pObject->pFaces[i].vertIndex[0]]; 329 vPoly[1] = pObject->pVerts[pObject->pFaces[i].vertIndex[1]]; 330 vPoly[2] = pObject->pVerts[pObject->pFaces[i].vertIndex[2]]; 331 332 vVector1 = VectorDiff(vPoly[0], vPoly[2]); 333 vVector2 = VectorDiff(vPoly[2], vPoly[1]); 334 335 vNormal = CrossProduct(vVector1, vVector2); 336 pTempNormals[i] = vNormal; 337 vNormal = NormalizeVector(vNormal); 338 339 pNormals[i] = vNormal; 340 } 341 342 /* now calculating vertex normals */ 343 CVector3 vSum = {0.0, 0.0, 0.0}; 344 CVector3 vZero = vSum; 345 int shared=0; 346 347 for (i = 0; i < pObject->numOfVerts; i++) 348 { 349 for (int j = 0; j < pObject->numOfFaces; j++) 350 { 351 if (pObject->pFaces[j].vertIndex[0] == i || 352 pObject->pFaces[j].vertIndex[1] == i || 353 pObject->pFaces[j].vertIndex[2] == i) 354 { 355 vSum = AddVector(vSum, pTempNormals[j]); 356 shared++; } 357 } 358 pObject->pNormals[i] = DivideVectorByScaler(vSum, float(-shared)); 359 pObject->pNormals[i] = NormalizeVector(pObject->pNormals[i]); 360 361 vSum = vZero; 362 shared = 0; 363 } 364 delete [] pTempNormals; 365 delete [] pNormals; 366 } 355 vSum = MathHelp::AddVector(vSum, pTempNormals[j]); 356 shared++; } 357 } 358 pObject->pNormals[i] = MathHelp::DivideVectorByScaler(vSum, float(-shared)); 359 pObject->pNormals[i] = MathHelp::NormalizeVector(pObject->pNormals[i]); 360 361 vSum = vZero; 362 shared = 0; 363 } 364 delete [] pTempNormals; 365 delete [] pNormals; 366 } 367 367 } 368 368
Note: See TracChangeset
for help on using the changeset viewer.