Results 1 to 3 of 3

Thread: GDI+ Performance Issue

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    80

    GDI+ Performance Issue

    I have been trying to achieve a simple 3D engine in VB.NET and it's visually pretty good.
    (Except i still need to add a Z-Buffer or Painter's algorithm to properly show concave shapes)
    Name:  Monkey.png
Views: 370
Size:  11.6 KB
    My problem is when it come to a model with a large amount of triangles(such as the monkey) the fps drop significantly to where is slightly freezes as it's rotating.
    I figured my rotate function creating a new mesh every time might be too much to ask for:
    vb.net Code:
    1. Public Function Rotate_X(ByVal Angle As Single) As Mesh
    2.         Dim Sin_T As Double = Math.Sin(Angle * Math.PI / 180)
    3.         Dim Cos_T As Double = Math.Cos(Angle * Math.PI / 180)
    4.         Dim MSH As New Mesh
    5.         For Each Triangle As TriFace In arr_Faces
    6.             Dim TF As TriFace = Triangle.Clone
    7.             For Each Vertex As PVector In TF.Vertices
    8.                 Dim Y As Double = Vertex.Y
    9.                 Dim Z As Double = Vertex.Z
    10.                 Vertex.Y = CSng(Y * Cos_T - Z * Sin_T)
    11.                 Vertex.Z = CSng(Z * Cos_T + Y * Sin_T)
    12.             Next
    13.             MSH.AddFace(TF)
    14.         Next
    15.         Return MSH
    16.     End Function
    So I instead made a function to fetch Vertices in a mesh and rotate them directly but that was even slower.
    When it comes to models with a smaller amount of face everything runs smoothly though.

    I'm using the fillpath method in in paint event; Should I switch to a bitmap and make my own raster function instead using setpixel ?
    vb.net Code:
    1. Dim new_Points() As Point = {New Point(PTriangle.Vertex_A.X, PTriangle.Vertex_A.Y), New Point(PTriangle.Vertex_B.X, PTriangle.Vertex_B.Y), New Point(PTriangle.Vertex_C.X, PTriangle.Vertex_C.Y)}
    2.                 Dim TrianglePath As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
    3.                 TrianglePath.AddLines(new_Points)
    4.                 TrianglePath.CloseFigure()
    5.                 Dim MyBrush As New Drawing.SolidBrush(TriColor)
    6.                 GFX.FillPath(MyBrush, TrianglePath)

    Or do I just need to use a language with access to the graphics card at this point ?
    I'm kind of lost ... thnx in advance for suggestions.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: GDI+ Performance Issue

    There is a limit to how much graphics you can do without the graphics card. Eventually, you will hit that limit if your graphics get complicated enough. I encountered that limit on one program. The drawing was not going to be fast enough, so I tried a variety of things. The first step was to time everything so that I only focused on the parts that were actually a problem, rather than just the ones I thought could be problems. You seem to have already done that step, though.

    Once I had the key points identified, I optimized them as much as I could, but that will never do more than a few percent improvements. In my case, there was an obvious improvement because I could cache all the images that were being drawn. The big issue I ran into was that I had up to a few hundred items being drawn, and the user could zoom in and out, which could result in those few hundred items all being composed and drawn. Since that wasn't fast enough, I cached all the images at each zoom level such that I only had to redraw the ones that had changed at any time. This was fast, but only worked as long as there weren't more than a handful that had to be redrawn at any point in time. The drawback was that, if I ever had to redraw everything, it was FAR slower. At first, that didn't matter to me, because I rarely had to redraw everything. Once I found that there were too many times when I did have to, the whole approach fell apart.

    At that point, I switched to XNA, which was a framework that made use of the graphics card. Naturally, MS abandoned XNA a few months later, but it lives on in the MonoGame framework.

    My point is that no matter what you do to try to improve the performance with GDI, considering the complexity of the graphics, you will almost certainly exceed what the CPU is capable of, and will need to make use of the graphics card. That doesn't mean abandoning the language, though, as MonoGame will do it, and is .NET. The other option would be WPF, which also uses the graphics card, but that is likely to be a bigger step than MonoGame.
    My usual boring signature: Nothing

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: GDI+ Performance Issue

    This kind of thing should be done on a GPU. The method you're using is utilizing the CPU to perform those transformations. You need to use a library like DirectX or OpenGL which can perform operations on triangles using the graphic adapter's GPU. Graphics operations tend to be highly parallelizable and GPUs excel at parallel operations.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width