M
M
mIka012021-02-15 21:04:33
3D
mIka01, 2021-02-15 21:04:33

How to optimize image rendering code?

Hello, help optimize and refine (in your opinion).
Foreword, watched the video from the Vectozavr channel. I did not succeed ( I did as in the article ). Decided on the theory to do and did. However, the next video was about textures. He used the libraries I decided without. It came out like that.

help me please

Graphics a = pictureBox1.CreateGraphics();
            base.OnResize(e);
            Bitmap bmp = new Bitmap("C:\\(*Путь)\\1.png");
            int[,] Data = new int[,] {
                    {0,0,100,100,  0,0,100,100,  45,45,0, 200},
                    {0,0,100,100,  0,0,100,100,  45,45,45, 0},
                };
            /*
             * 0,1,2,3 - 4 координаты на плоскости.
             * 4,5,6,7 - 4 координаты на текстуре.
             * 8,9,10 - 3 координаты углы поворота.
             * 11 - 1 координаты смещение по z
             * 
             */

            int x_1 = 0;
            int y_1 = 0;
            while (true) {
                int x = Cursor.Position.X;
                int y = Cursor.Position.Y;

                if ((x_1 != x) && (y_1 != y))
                {
                    x_1 = x;
                    y_1 = y;
                    a.Clear(Color.White);
                    for (int n = 0; n < Data.GetLength(0); n++)
                    {
                        int[,] texture = new int[(Data[n, 2] - Data[n, 0] + 1) * (Data[n, 3] - Data[n, 1] + 1), 6];

                        int u = 0;
                        for (int j = Data[n, 4]; j < Data[n, 6]; j++)
                        {
                            for (int i = Data[n, 5]; i < Data[n, 7]; i++)
                            {
                                Color s1 = bmp.GetPixel(j, i);

                                texture[u, 3] = s1.R;
                                texture[u, 4] = s1.G;
                                texture[u, 5] = s1.B;

                                u++;
                            }
                        }

                        u = 0;
                        for (int j = Data[n, 0]; j < Data[n, 2]; j++)
                        {
                            for (int i = Data[n, 1]; i < Data[n, 3]; i++)
                            {

                                texture[u, 0] = j;
                                texture[u, 1] = i;

                                u++;
                            }
                        }

                        for (int i = 0; i < u; i++)
                        {
                            texture[i, 2] = Data[n, 11];
                        }

                        for (int i = 0; i < u; i++)
                        {
                            turn.X(texture[i, 0], texture[i, 1], texture[i, 2], Data[n, 8], out texture[i, 0], out texture[i, 1], out texture[i, 2]);
                        }
                        for (int i = 0; i < u; i++)
                        {
                            turn.Y(texture[i, 0], texture[i, 1], texture[i, 2], Data[n, 9], out texture[i, 0], out texture[i, 1], out texture[i, 2]);
                        }
                        for (int i = 0; i < u; i++)
                        {
                            turn.Z(texture[i, 0], texture[i, 1], texture[i, 2], Data[n, 10], out texture[i, 0], out texture[i, 1], out texture[i, 2]);
                        }

                        for (int i = 0; i < u; i++)
                        {
                            turn.X(texture[i, 0], texture[i, 1], texture[i, 2], x, out texture[i, 0], out texture[i, 1], out texture[i, 2]);
                        }
                        for (int i = 0; i < u; i++)
                        {
                            turn.Y(texture[i, 0], texture[i, 1], texture[i, 2], y, out texture[i, 0], out texture[i, 1], out texture[i, 2]);
                        }
                        for (int i = 0; i < u; i++)
                        {
                            turn.Z(texture[i, 0], texture[i, 1], texture[i, 2], 0, out texture[i, 0], out texture[i, 1], out texture[i, 2]);
                        }



                        int h = pictureBox1.ClientSize.Height;
                        int w = pictureBox1.ClientSize.Width;

                        for (int i = 0; i < u; i++)
                        {
                            texture[i, 0] = texture[i, 0] + (w / 2);
                            texture[i, 1] = texture[i, 1] + (h / 2);
                        }

                        for (int j = 0; j < u; j++)
                        {
                            int q = Convert.ToInt32(Math.Sqrt(Math.Pow(texture[j, 0], 2) + Math.Pow(texture[j, 1], 2) + Math.Pow(texture[j, 2], 2)));
                            int r, g, b;
                            r = texture[j, 3] - q / 20;
                            g = texture[j, 4] - q / 20;
                            b = texture[j, 5] - q / 20;
                            if (r < 0) { r = 0; }
                            if (g < 0) { g = 0; }
                            if (b < 0) { b = 0; }
                            SolidBrush brush = new SolidBrush(Color.FromArgb(r, g, b));
                            a.FillRectangle(brush, texture[j, 0], texture[j, 1], 2, 2);
                        }





                    }
                }
            }
.
Class.
class turn
    {
        public static void X(int x, int y, int z, int l, out int x1, out int y1, out int z1)
        {
            double q = l * (Math.PI / 180.0);
            x1 = x;
            y1 = Convert.ToInt32(y * Math.Cos(q) + z * Math.Sin(q));
            z1 = Convert.ToInt32((-1) * y * Math.Sin(q) + z * Math.Cos(q));
        }
        public static void Y(int x, int y, int z, int l, out int x1, out int y1, out int z1)
        {
            double q = l * (Math.PI / 180.0);
            x1 = Convert.ToInt32(x * Math.Cos(q) + z * Math.Sin(q));
            y1 = y;
            z1 = Convert.ToInt32((-1) * x * Math.Sin(q) + z * Math.Cos(q));
        }
        public static void Z(int x, int y, int z, int l, out int x1, out int y1, out int z1)
        {
            double q = l * (Math.PI / 180.0);
            x1 = Convert.ToInt32(x * Math.Cos(q) - y * Math.Sin(q));
            y1 = Convert.ToInt32((1) * x * Math.Sin(q) + y * Math.Cos(q));
            z1 = z;
        }
    }


The image used 602ab69a9bfc1908625477.pngis (but you can use any).

How can this be optimized?
Thanks in advance for your constructive response.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
youngMaster, 2021-02-25
@mIka01

First, get rid of the use of bmp.GetPixel on every frame.
This part needs to be taken out before the rendering cycle:

//
  int[,] texture = new int[(Data[n, 2] - Data[n, 0] + 1) * (Data[n, 3] - Data[n, 1] + 1), 6];

  int u = 0;
  for (int j = Data[n, 4]; j < Data[n, 6]; j++)
  {
      for (int i = Data[n, 5]; i < Data[n, 7]; i++)
      {
          Color s1 = bmp.GetPixel(j, i);

          texture[u, 3] = s1.R;
          texture[u, 4] = s1.G;
          texture[u, 5] = s1.B;

          u++;
      }
  }

Without testing, it's hard to say, but most likely this is the slowest part of your code.
Although the last cycle with output to the screen is also quite slow, it just cannot be accelerated in Sharpe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question