clray
diff src/mesh.cc @ 20:63a6b46f58a0
fixed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 09 Aug 2010 12:55:40 +0100 |
parents | 8baea9b66b50 |
children | bd6c2b25f6e7 |
line diff
1.1 --- a/src/mesh.cc Mon Aug 09 06:45:57 2010 +0100 1.2 +++ b/src/mesh.cc Mon Aug 09 12:55:40 2010 +0100 1.3 @@ -59,6 +59,8 @@ 1.4 1.5 Vector3() { x = y = z = 0.0; } 1.6 Vector3(float a, float b, float c) { x = a; y = b; z = c; } 1.7 + 1.8 + void normalize() { float len = sqrt(x * x + y * y + z * z); x /= len; y /= len; z /= len; } 1.9 }; 1.10 1.11 struct Vector2 { 1.12 @@ -117,6 +119,9 @@ 1.13 static bool find_file(char *res, int sz, const char *fname, const char *path = ".", const char *mode = "rb"); 1.14 static const char *dirname(const char *str); 1.15 1.16 +static Vector3 operator -(const Vector3 &a, const Vector3 &b); 1.17 +static Vector3 cross(const Vector3 &a, const Vector3 &b); 1.18 + 1.19 static map<string, int> matnames; 1.20 1.21 1.22 @@ -406,13 +411,14 @@ 1.23 1.24 for(size_t i=0; i<obj->f.size(); i++) { 1.25 Face face; 1.26 + Vector3 v[3]; 1.27 1.28 for(int j=0; j<3; j++) { 1.29 obj_face *f = &obj->f[i]; 1.30 1.31 - face.v[j].pos[0] = obj->v[f->v[j]].x; 1.32 - face.v[j].pos[1] = obj->v[f->v[j]].y; 1.33 - face.v[j].pos[2] = obj->v[f->v[j]].z; 1.34 + face.v[j].pos[0] = v[j].x = obj->v[f->v[j]].x; 1.35 + face.v[j].pos[1] = v[j].y = obj->v[f->v[j]].y; 1.36 + face.v[j].pos[2] = v[j].z = obj->v[f->v[j]].z; 1.37 face.v[j].pos[3] = 0.0; 1.38 1.39 int nidx = f->n[j] < 0 ? 0 : f->n[j]; 1.40 @@ -426,9 +432,14 @@ 1.41 face.v[j].tex[1] = obj->vt[tidx].y; 1.42 } 1.43 1.44 - face.normal[0] = face.v[0].normal[0]; 1.45 - face.normal[1] = face.v[1].normal[1]; 1.46 - face.normal[2] = face.v[2].normal[2]; 1.47 + Vector3 a = v[1] - v[0]; 1.48 + Vector3 b = v[2] - v[0]; 1.49 + Vector3 n = cross(a, b); 1.50 + n.normalize(); 1.51 + 1.52 + face.normal[0] = n.x; 1.53 + face.normal[1] = n.y; 1.54 + face.normal[2] = n.z; 1.55 face.normal[3] = 0.0; 1.56 1.57 mesh->faces.push_back(face); 1.58 @@ -729,3 +740,17 @@ 1.59 } 1.60 return buf; 1.61 } 1.62 + 1.63 +static Vector3 operator -(const Vector3 &a, const Vector3 &b) 1.64 +{ 1.65 + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); 1.66 +} 1.67 + 1.68 +static Vector3 cross(const Vector3 &a, const Vector3 &b) 1.69 +{ 1.70 + Vector3 res; 1.71 + res.x = a.y * b.z - a.z * b.y; 1.72 + res.y = a.z * b.x - a.x * b.z; 1.73 + res.z = a.x * b.y - a.y * b.x; 1.74 + return res; 1.75 +}