简体   繁体   中英

How to properly color in C# GDI+?

I have this assignment where I need to create a Rotating 3D Castle with Color using C# GDI+. I crammed and decided to download an already-built program here: https://code.msdn.microsoft.com/windowsdesktop/3D-Modeling-using-GDI-b93937b9

I replaced the shape in VectorShape.cs with my vertices, edges and faces. Also, I've put some additional code to make the shape colored.

for (int i = 0; i < 6; i++)
        {
            Point[] point2D = new Point[m_face[i].Length]; //I added this
            //iterate each edges in that face
            for (int j = 0; j < m_face[i].Length; j++)
            {

                int k=m_face[i][j];

                GraphicsState _s1 = g.Save();
                GraphicsContainer a = g.BeginContainer();
                Point3d p1 =m_vertex[m_edges[k][0]];
                Point3d p2 = m_vertex[m_edges[k][1]];
                double[] _p1 = ProjMatrix.ApplyTransform(p1.X,p1.Y,p1.Z, 1);
                double[] _p2 = ProjMatrix.ApplyTransform(p2.X, p2.Y, p2.Z, 1);
                P1.X = (int)_p1[0]; P1.Y = -(int)_p1[1];
                P2.X = (int)_p2[0]; P2.Y = -(int)_p2[1];

                point2D[j].X = P2.X; //I added this
                point2D[j].Y = P2.Y; //I added this
                g.DrawLine(m_pen,P1, P2);
                g.ResetTransform();
                g.EndContainer(a); 
                g.Restore(_s1);
            }
            g.FillPolygon(Brushes.Gray, point2D); //I added this.
        }

Both the front and back faces are okay but when rendering the sides, the top and the bottom face, the coloring looked like these: Badly Colored Side Badly Colored Castle

I've tried to color the cube that came with the program. The output was similar. The front and back faces were alright. The other faces were bad. So, I think the problem is the code where I downloaded it. I've already tried using similar GDI+ programs but this is the only one where you can manually put vertices, edges and faces. I also can't build a program from scratch because I've got little knowledge about 3D stuff, honestly.

尝试使用Antialiasing SmoothingMode

mygraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM