Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <climits>
- #include <bitset>
- #include <utility>
- #include <vector>
- #include <cmath>
- struct vec3
- {
- float x;
- float y;
- float z;
- };
- void normalize(vec3 &vert)
- {
- float mag = sqrt(pow(vert.x, 2) + pow(vert.y, 2) + pow(vert.z, 2));
- vert.x = vert.x / mag;
- vert.y = vert.y / mag;
- vert.z = vert.z / mag;
- }
- vec3 cross(const vec3 &a, const vec3 &b)
- {
- vec3 res{};
- res.x = a.y * b.z - a.z * b.y;
- res.y = a.z * b.x - a.x * b.z;
- res.z = a.x * b.y - a.y * b.x;
- return res;
- }
- vec3 &operator+=(vec3 &a, const vec3 &b)
- {
- a.x += b.x;
- a.y += b.y;
- a.z += b.z;
- return a;
- }
- vec3 operator-(const vec3 &a, const vec3 &b)
- {
- return {a.x - b.x, a.y - b.y, a.z - b.z};
- }
- void calc_mesh_normals(vec3 *normals, const vec3 *verts, const int *faces, int nverts, int nfaces)
- {
- for (int i = 0; i < nverts; i++)
- {
- normals[i] = {0, 0, 0};
- }
- for (int j = 1; j < nfaces - 1; j++)
- {
- auto p = cross(verts[faces[j - 1]] - verts[faces[j]], verts[faces[j + 1]] - verts[faces[j - 1]]);
- normals[faces[j - 1]] += p;
- normals[faces[j]] += p;
- normals[faces[j + 1]] += p;
- }
- for (int i = 0; i < nverts; i++)
- {
- normalize(normals[i]);
- }
- }
- int main()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement