Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <climits>
  3. #include <bitset>
  4. #include <utility>
  5. #include <vector>
  6. #include <cmath>
  7.  
  8. struct vec3
  9. {
  10.   float x;
  11.   float y;
  12.   float z;
  13. };
  14. void normalize (vec3 & vert)
  15. {
  16.   float mag = sqrt(pow(vert.x, 2) + pow(vert.y, 2) + pow(vert.z, 2));
  17.   vert.x = vert.x / mag;
  18.   vert.y = vert.y / mag;
  19.   vert.z = vert.z / mag;
  20. }
  21. vec3 cross (const vec3 & a, const vec3 & b)
  22. {
  23.   vec3 res{};
  24.   res.x=a.y*b.z-a.z*b.y;
  25.   res.y = a.z*b.x-a.x*b.z;
  26.   res.z = a.x*b.y-a.y*b.x;
  27.   return res;
  28. }
  29. void operator+=(vec3 & a, const vec3 & b)
  30. {
  31.   a.x+=b.x;
  32.   a.y+=b.y;
  33.   a.z+=b.z;
  34. }
  35. void calc_mesh_normals(vec3 *normals, const vec3 *verts, const int *faces, int nverts, int nfaces)
  36. {
  37.   for (int i = 0; i < nverts; i++)
  38.   {
  39.     normals[i] = {0, 0, 0};
  40.   }
  41.   for (int j = 1; j < nfaces-1; j++)
  42.   {
  43.   auto p = cross(verts[faces[j-1]],verts[faces[j+1]]);
  44.   normals[faces[j-1]]+=p;
  45.   normals[faces[j]]+=p;
  46.   normals[faces[j+1]]+=p;
  47.   }
  48.   for(int i = 0; i < nverts; i++)
  49.   {
  50.     normalize(normals[i]);
  51.   }
  52. }
  53.  
  54. int main()
  55. {
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement