// // Compute scene vertex normals // public void computeNormals(Scene3ds scene, Scene result) { Point3D vcenter = Point3D.Zero; float vcounter = 0.0f; for (int i = 0; i < scene.Meshes.Count(); i++) { Mesh3ds m = scene.Meshes.ElementAt(i); // Alloc memory Mesh mesh = new Mesh { Name = m.Name }; result.Meshes.Add(mesh); //mesh._numTexCoords = 0; Vector3D[] tmpFaceNormals = new Vector3D[m.faces()]; // Compute face normals for (int fi = 0; fi < m.faces(); fi++) { Face3ds f = m.face(fi); Vertex3ds p0 = m.vertex(f.P0); Vertex3ds p1 = m.vertex(f.P1); Vertex3ds p2 = m.vertex(f.P2); /*// Compute face middle point * mesh.faceMiddlePoint[fi] = new PVector(); * mesh.faceMiddlePoint[fi].x = (p0.X + p1.X + p2.X) / 3.0f; * mesh.faceMiddlePoint[fi].y = (p0.Y + p1.Y + p2.Y) / 3.0f; * mesh.faceMiddlePoint[fi].z = (p0.Z + p1.Z + p2.Z) / 3.0f;*/ Point3D v0 = new Point3D(p0.X, p0.Y, p0.Z); Point3D v1 = new Point3D(p1.X, p1.Y, p1.Z); Point3D v2 = new Point3D(p2.X, p2.Y, p2.Z); Vector3D e0 = v1 - v0; Vector3D e1 = v2 - v0; //mesh.faceNormals[fi] = e1.cross(e0); // save a copy of the unnormalized face normal. used for average vertex normals tmpFaceNormals[fi] = Vector3D.Cross(e1, e0); // normalize face normal //mesh.faceNormals[fi].normalize(); } // // Compute vertex normals.Take average from adjacent face normals.find coplanar faces or get weighted normals. // One could also use the smooth groups from 3ds to compute normals, we'll see about that. // //PVector v = new PVector(); TexCoord3ds tc = new TexCoord3ds(0, 0); for (int vi = 0; vi < m.vertices(); vi++) { Vertex3ds p = m.vertex(vi); vcenter += new Point3D(p.X, p.Y, p.Z); vcounter++; if (m.texCoords() > 0) { tc = m.texCoord(vi); } Vector3D n = Vector3D.Zero; float num = 0; for (int fi = 0; fi < m.faces(); fi++) { Face3ds f = m.face(fi); // Vertex3ds p0 = m.vertex(f.P0); // Vertex3ds p1 = m.vertex(f.P1); // Vertex3ds p2 = m.vertex(f.P2); if (vi == f.P0 || vi == f.P1 || vi == f.P2) { num++; n += tmpFaceNormals[fi]; //mesh.faceNormals[fi] ); } } if (num > 0) { n *= 1.0f / (float)num; } n.Normalize(); mesh.Normals.Add(n); if (FLIPYZ) { Vector3D tmp = mesh.Normals[vi]; mesh.Normals[vi] = new Vector3D(tmp.X, -tmp.Z, tmp.Y); } // Save vertex data if (FLIPYZ) { mesh.Positions.Add(new Point3D(p.X, -p.Z, p.Y)); } else { mesh.Positions.Add(new Point3D(p.X, p.Y, p.Z)); } // Save texcoord data //mesh._numTexCoords = m.texCoords(); if (m.texCoords() > 0) { if (FLIPV) { mesh.TextureCoordinates.Add(new Point3D(tc.U, 1.0f - tc.V, 0)); } else { mesh.TextureCoordinates.Add(new Point3D(tc.U, tc.V, 0)); } } } } if (vcounter > 0.0) { vcenter /= vcounter; } }
private Vertex3ds[] read_POINT_ARRAY() { int verts = ReadUnsignedShort(); Vertex3ds[] v = new Vertex3ds[verts]; for (int n = 0; n < verts; n++) { float x = ReadFloat(); float z = ReadFloat(); float y = ReadFloat(); v[n] = new Vertex3ds(x, y, z); } if (mDecode != null) { mDecode.enter(); mDecode.println("Vertices: " + verts); for (int i = 0; i < verts; i++) { mDecode.println(" " + Utils3ds.intToString(i, 4) + ": " + v[i]); } mDecode.leave(); } return v; }