예제 #1
0
        private async Task <List <Pixel> > GetListAsync(Group group, int dx, int dy, int viceVersa, int width, int height, Bgr24Bitmap bitmap)
        {
            return(await Task.Run(() =>
            {
                List <Pixel> pixels = new List <Pixel>();
                int count = 0;
                ZBuffer zBuf = new ZBuffer(width, height);
                Parallel.ForEach(group.Faces, face =>
                {
                    if (IsFaceVisible(group.Vertices, face))
                    {
                        List <Pixel> pixelsForSide = new List <Pixel>();
                        List <Pixel> pixelsInSide = new List <Pixel>();
                        Vertex vertex0, vertex1;
                        int index0, index1;

                        Vector3 lightingVector = new Vector3(0, 0, 1);

                        int ind0 = (face.FaceElements.ElementAt(0).VertexNormalIndex != null) ? (int)face.FaceElements.ElementAt(0).VertexNormalIndex : 1;
                        Vector3 point0Normal = new Vector3(group.VertexNormals[ind0 - 1].X, group.VertexNormals[ind0 - 1].Y, group.VertexNormals[ind0 - 1].Z);

                        int ind1 = (face.FaceElements.ElementAt(1).VertexNormalIndex != null) ? (int)face.FaceElements.ElementAt(1).VertexNormalIndex : 1;
                        Vector3 point1Normal = new Vector3(group.VertexNormals[ind1 - 1].X, group.VertexNormals[ind1 - 1].Y, group.VertexNormals[ind1 - 1].Z);

                        int ind2 = (face.FaceElements.ElementAt(2).VertexNormalIndex != null) ? (int)face.FaceElements.ElementAt(2).VertexNormalIndex : 1;
                        Vector3 point2Normal = new Vector3(group.VertexNormals[ind2 - 1].X, group.VertexNormals[ind2 - 1].Y, group.VertexNormals[ind2 - 1].Z);

                        Color color = Color.FromArgb(255, 255, 0, 0);
                        Color point1Color = Lambert.GetPointColor(point0Normal, lightingVector, color);
                        Color point2Color = Lambert.GetPointColor(point1Normal, lightingVector, color);
                        Color point3Color = Lambert.GetPointColor(point2Normal, lightingVector, color);
                        Color faceColor = PlaneShading.GetAverageColor(point1Color, point2Color, point3Color);

                        for (int i = 0; i < face.FaceElements.Count - 1; i++)
                        {
                            index0 = (face.FaceElements.ElementAt(i).VertexIndex != -2) ? face.FaceElements.ElementAt(i).VertexIndex : (group.Vertices.Count - 1);
                            index1 = (face.FaceElements.ElementAt(i + 1).VertexIndex != -2) ? face.FaceElements.ElementAt(i + 1).VertexIndex : (group.Vertices.Count - 1);
                            vertex0 = group.Vertices.ElementAt(index0);
                            vertex1 = group.Vertices.ElementAt(index1);
                            pixelsForSide.AddRange(Bresenham.GetPixels((int)(vertex0.X + dx), (int)(vertex0.Y * viceVersa + dy), (int)(vertex0.Z), (int)(vertex1.X + dx), (int)(vertex1.Y * viceVersa + dy), (int)(vertex1.Z), windowWidth, windowHeight, bitmap, zBuf, faceColor));
                        }
                        index0 = (face.FaceElements.ElementAt(0).VertexIndex != -2) ? face.FaceElements.ElementAt(0).VertexIndex : (group.Vertices.Count - 1);
                        index1 = (face.FaceElements.ElementAt(face.FaceElements.Count - 1).VertexIndex != -2) ? face.FaceElements.ElementAt(face.FaceElements.Count - 1).VertexIndex : (group.Vertices.Count - 1);
                        vertex0 = group.Vertices.ElementAt(index0);
                        vertex1 = group.Vertices.ElementAt(index1);
                        pixelsForSide.AddRange(Bresenham.GetPixels((int)(vertex0.X + dx), (int)(vertex0.Y *viceVersa + dy), vertex0.Z, (int)(vertex1.X + dx), (int)(vertex1.Y *viceVersa + dy), vertex1.Z, windowWidth, windowHeight, bitmap, zBuf, faceColor));


                        RastAlgorithm.DrawPixelForRasterization(pixelsForSide, bitmap, zBuf, faceColor);
                        //pixels.AddRange(pixelsForSide);
                        //pixelsInSide.AddRange(RastAlgorithm.DrawPixelForRasterization(pixelsForSide, bitmap, zBuf));
                        //pixels.AddRange(pixelsInSide);
                        count++;
                    }
                });
                return pixels;
            }));
        }