示例#1
0
        public static IShape CreateShape(Transform o2w, Transform w2o, bool reverseOrientation, ParameterSet parameters, Dictionary<string, ITexture<double>> floatTextures, Dictionary<string, ITexture<Spectrum>> spectrumTextures)
        {
            int nvi = 0, npi = 0, nuvi = 0, nsi = 0, nni = 0;
            int[] vi = parameters.FindInt ("indices", ref nvi);
            Point[] P = parameters.FindPoint ("P", ref npi);
            double[] uvs = parameters.FindDouble ("uv", ref nuvi);
            if (uvs == null)
                uvs = parameters.FindDouble ("st", ref nuvi);
            bool discardDegnerateUVs = parameters.FindOneBool ("discarddegenerateUVs", false);
            // XXX should complain if uvs aren't an array of 2...
            if (uvs != null)
            {
                if (nuvi < 2 * npi)
                {
                    uvs = null;
                }
            }
            if (vi == null || P == null)
                return null;
            Vector[] S = parameters.FindVector ("S", ref nsi);
            if (S != null && nsi != npi)
            {

                S = null;
            }
            Normal[] N = parameters.FindNormal ("N", ref nni);
            if (N != null && nni != npi)
            {
                N = null;
            }
            if (discardDegnerateUVs && uvs != null && N != null)
            {
                // if there are normals, check for bad uv's that
                // give degenerate mappings; discard them if so
                int vp = 0;
                for (int i = 0; i < nvi; i += 3,vp += 3)
                {
                    double area = 0.5 * ((P[vi[vp + 0]] - P[vi[vp + 1]]) % (P[vi[vp + 2]] - P[vi[vp + 1]])).Length;
                    if (area < 1E-07)
                        continue;
                    // ignore degenerate tris.
                    if ((uvs[2 * vi[vp + 0]] == uvs[2 * vi[vp + 1]] && uvs[2 * vi[vp + 0] + 1] == uvs[2 * vi[vp + 1] + 1]) || (uvs[2 * vi[vp + 1]] == uvs[2 * vi[vp + 2]] && uvs[2 * vi[vp + 1] + 1] == uvs[2 * vi[vp + 2] + 1]) || (uvs[2 * vi[vp + 2]] == uvs[2 * vi[vp + 0]] && uvs[2 * vi[vp + 2] + 1] == uvs[2 * vi[vp + 0] + 1]))
                    {
                        uvs = null;
                        break;
                    }
                }
            }
            for (int i = 0; i < nvi; ++i)
                if (vi[i] >= npi)
                {
                    return null;
                }

            ITexture<double> alphaTex = null;
            string alphaTexName = parameters.FindTexture ("alpha");
            if (alphaTexName != "")
            {
                if (floatTextures.ContainsKey (alphaTexName))
                    alphaTex = floatTextures[alphaTexName];
            } else if (parameters.FindOneDouble ("alpha", 1.0) == 0.0)
                alphaTex = new ConstantTexture<double> (0.0);
            return new TriangleMesh (o2w, w2o, reverseOrientation, nvi / 3, npi, vi, P, N, S, uvs,
            alphaTex);
        }