Changeset 4148 in orxonox.OLD for orxonox/branches/md2_loader/src/lib
- Timestamp:
- May 10, 2005, 5:58:08 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
r4146 r4148 25 25 #include "base_object.h" 26 26 #include <vector> 27 #include <math.h> 27 28 28 29 using namespace std; … … 111 112 }; 112 113 114 115 /* TESTING TESTING TESTING */ 116 /* some mathematical functions that are already implemented by vector.cc class */ 117 118 // magnitude of a normal 119 #define Mag(Normal) (sqrt(Normal.x*Normal.x + Normal.y*Normal.y + Normal.z*Normal.z)) 120 121 // vector between two points 122 CVector3 VectorDiff(CVector3 vPoint1, CVector3 vPoint2) 123 { 124 CVector3 vVector; 125 126 vVector.x = vPoint1.x - vPoint2.x; 127 vVector.y = vPoint1.y - vPoint2.y; 128 vVector.z = vPoint1.z - vPoint2.z; 129 130 return vVector; 131 } 132 133 // adding vectors, returning result 134 CVector3 AddVector(CVector3 vVector1, CVector3 vVector2) 135 { 136 CVector3 vResult; 137 138 vResult.x = vVector2.x + vVector1.x; 139 vResult.y = vVector2.y + vVector1.y; 140 vResult.z = vVector2.z + vVector1.z; 141 142 return vResult; 143 } 144 145 // This divides a vector by a single number (scalar) and returns the result 146 CVector3 DivideVectorByScaler(CVector3 vVector1, float Scaler) 147 { 148 CVector3 vResult; 149 150 vResult.x = vVector1.x / Scaler; 151 vResult.y = vVector1.y / Scaler; 152 vResult.z = vVector1.z / Scaler; 153 154 return vResult; 155 } 156 157 // cross product between 2 vectors 158 CVector3 CrossProduct(CVector3 vVector1, CVector3 vVector2) 159 { 160 CVector3 vCross; 161 vCross.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y)); 162 vCross.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z)); 163 vCross.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)); 164 return vCross; 165 } 166 167 // calculate the normal of a vector 168 CVector3 NormalizeVector(CVector3 vNormal) 169 { 170 double Magnitude; 171 Magnitude = Mag(vNormal); 172 vNormal.x /= (float)Magnitude; 173 vNormal.y /= (float)Magnitude; 174 vNormal.z /= (float)Magnitude; 175 176 return vNormal; 177 } 178 179 113 180 #endif /* _ABSTRACT_MODEL_H */ -
orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc
r4147 r4148 307 307 */ 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++) 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 = 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 } 367 } 310 368 311 369
Note: See TracChangeset
for help on using the changeset viewer.