Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void FWstorage::mesh::calcNormals()
- {
- if (normList.size == 0 && normList.data == NULL){
- if ((vertList.size == 0) || (meshList.size == 0)) { throw(FW_ERROR_UNKNOWN); return; }
- // 1.
- array_t<vec3float> perMeshNormals;
- array_t<vec3float> normVectors;
- _MEMORY_SET_ROW_ perMeshNormals.insert(meshList.size);
- _MEMORY_SET_ROW_ normVectors.insert(meshList.size);
- _MEMORY_SET_ROW_ normList.insert(vertList.size);
- vec3float a, b, c, v1, v2, tmpv;
- for (unsigned int i=0; i<meshList.size; i++){
- a = vertList[meshList[i].x];
- b = vertList[meshList[i].y];
- c = vertList[meshList[i].z];
- v1 = a-b; //AB
- v2 = a-c; //AC
- tmpv = v1.crossProduct(v2);
- perMeshNormals[i] = tmpv;
- }
- // 2.
- int norm_index = 0;
- for(register unsigned int i=0; i<vertList.size; i++) { // and again ...
- for (register unsigned int l=0; l<meshList.size; l++){
- if ((meshList[l].x == i) || (meshList[l].y == i) || (meshList[l].z == i)){
- normVectors[norm_index] = perMeshNormals[l];
- norm_index++;
- }
- }
- while (norm_index > 1){
- norm_index = ceil((float)norm_index/2.0);
- for (register int j=0, k=0 ; k<norm_index; (j+=2, k++)){ // i>>j !{
- if (j+1 > norm_index){
- normVectors[k] = normVectors[j]; // leave as it was before
- } else {
- v1 = normVectors[j];
- v2 = normVectors[j+1];
- tmpv = v1+v2; // sum only only, normalization later.
- normVectors[k] = tmpv;
- }
- }
- }
- normVectors[0] /= normVectors[0].vlen();
- normList[i] = normVectors[0];
- norm_index = 0;
- }
- _MEMORY_SET_ROW_ perMeshNormals.clear();
- _MEMORY_SET_ROW_ normVectors.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement