Exemplo n.º 1
0
        public void DrawFace(WavefrontObject meshObj, WavefrontFace face, Shader shader)
        {
            //WavefontFace是一个triangle fan
            //可能会有多个三角形;这些三角形共用第一个顶点做v0
            if (face.Vertices.Count < 3)
            {
                Debug.Assert(false);
                return;
            }
            Vertex   vertex0 = Vertex.FromWavefrontVertex(meshObj, face.Vertices[0]);
            VSOutput vClip0  = shader.VertShader(vertex0);

            context.statics.vertexCount += 1;

            for (int i = 1; i + 1 < face.Vertices.Count; i += 1)
            {
                Vertex vertex1 = Vertex.FromWavefrontVertex(meshObj, face.Vertices[i]);
                Vertex vertex2 = Vertex.FromWavefrontVertex(meshObj, face.Vertices[i + 1]);
                //顶点着色器
                VSOutput vClip1 = shader.VertShader(vertex1);
                VSOutput vClip2 = shader.VertShader(vertex2);
                context.statics.vertexCount += 2;
                DrawTriangle(vClip0, vClip1, vClip2);
            }
        }
Exemplo n.º 2
0
        //材质
        //public static Material mat = new Material(new Color(0, 0, 0.1f), 0.1f, new Color(0.3f, 0.3f, 0.3f), new Color(1, 1, 1), 99);


        public static WavefrontObject ToWavefrontObject()
        {
            WavefrontObject obj = new WavefrontObject();

            for (int i = 0; i < CubeTestData.pointList.Length; ++i)
            {
                Vector3 point = CubeTestData.pointList[i];
                obj.Positions.Add(point);
            }
            for (int i = 0; i < CubeTestData.normals.Length; ++i)
            {
                Vector3 normal = CubeTestData.normals[i];
                obj.Normals.Add(normal);
            }
            for (int i = 0; i < CubeTestData.uvs.Length; ++i)
            {
                Vector2 uv = CubeTestData.uvs[i];
                obj.Texcoords.Add(uv);
            }
            for (int i = 0; i < CubeTestData.vertColors.Length; ++i)
            {
                Vector4 color = new Vector4(CubeTestData.vertColors[i], 1);
                obj.Colors.Add(color);
            }

            //处理三角形
            WavefrontFaceGroup faceGroup = new WavefrontFaceGroup();

            obj.Groups.Add(faceGroup);
            for (int i = 0; i + 2 < CubeTestData.indexs.Length; i += 3)
            {
                WavefrontFace face = new WavefrontFace();
                faceGroup.Faces.Add(face);
                int             index0 = i;
                int             index1 = i + 1;
                int             index2 = i + 2;
                int             p0     = CubeTestData.indexs[i];
                int             p1     = CubeTestData.indexs[i + 1];
                int             p2     = CubeTestData.indexs[i + 2];
                WavefrontVertex v0     = new WavefrontVertex(p0 + 1, index0 + 1, index0 + 1);
                WavefrontVertex v1     = new WavefrontVertex(p1 + 1, index1 + 1, index1 + 1);
                WavefrontVertex v2     = new WavefrontVertex(p2 + 1, index2 + 1, index2 + 1);
                v0.Color = index0 + 1;
                v1.Color = index1 + 1;
                v2.Color = index2 + 1;
                face.Vertices.Add(v0);
                face.Vertices.Add(v1);
                face.Vertices.Add(v2);
            }
            return(obj);
        }