Advertisement
Guest User

MDL normals

a guest
Oct 20th, 2014
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. void R_BuildFrameNormals (aliashdr_t *hdr, trivertx_t *verts, dtriangle_t *triangles, vertexnormals_t *vnorms)
  2. {
  3. for (int i = 0; i < hdr->numverts; i++)
  4. {
  5. // no normals initially
  6. Vector3Clear (vnorms[i].normal);
  7. vnorms[i].numnormals = 0;
  8. }
  9.  
  10. for (int i = 0; i < hdr->numtris; i++)
  11. {
  12. float triverts[3][3];
  13. float vtemp1[3], vtemp2[3], normal[3];
  14. int *vertindexes = triangles[i].vertindex;
  15.  
  16. // undo the vertex rotation from modelgen.c here too
  17. for (int j = 0; j < 3; j++)
  18. {
  19. triverts[j][0] = (float) verts[vertindexes[j]].v[1] * hdr->scale[1] + hdr->scale_origin[1];
  20. triverts[j][1] = -((float) verts[vertindexes[j]].v[0] * hdr->scale[0] + hdr->scale_origin[0]);
  21. triverts[j][2] = (float) verts[vertindexes[j]].v[2] * hdr->scale[2] + hdr->scale_origin[2];
  22. }
  23.  
  24. // calc the per-triangle normal
  25. Vector3Subtract (vtemp1, triverts[0], triverts[1]);
  26. Vector3Subtract (vtemp2, triverts[2], triverts[1]);
  27. Vector3Cross (normal, vtemp1, vtemp2);
  28. Vector3Normalize (normal);
  29.  
  30. // rotate the normal so the model faces down the positive x axis
  31. float newnormal[3] = {-normal[1], normal[0], normal[2]};
  32.  
  33. // and accumulate it into the calculated normals array
  34. for (int j = 0; j < 3; j++)
  35. {
  36. Vector3Add (vnorms[vertindexes[j]].normal, vnorms[vertindexes[j]].normal, newnormal);
  37. vnorms[vertindexes[j]].numnormals++;
  38. }
  39. }
  40.  
  41. // copy out normals
  42. for (int i = 0; i < hdr->numverts; i++)
  43. {
  44. // numnormals was checked for > 0 in modelgen.c so we shouldn't need to do it again
  45. // here but we do anyway just in case a rogue modder has used a bad modelling tool
  46. if (vnorms[i].numnormals > 0)
  47. {
  48. Vector3Scalef (vnorms[i].normal, vnorms[i].normal, (float) vnorms[i].numnormals);
  49. Vector3Normalize (vnorms[i].normal);
  50. }
  51. else Vector3Set (vnorms[i].normal, 0.0f, 0.0f, 1.0f);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement