XNA running slow when making a texture

Posted by Anthony on Game Development See other posts from Game Development or by Anthony
Published on 2012-12-11T19:38:30Z Indexed on 2012/12/11 23:15 UTC
Read the original article Hit count: 482

Filed under:
|
|

I'm using XNA to test an image analysis algorithm for a robot. I made a simple 3D world that has a grass, a robot, and white lines (that are represent the course). The image analysis algorithm is a modification of the Hough line detection algorithm.

I have the game render 2 camera views to a render target in memory. One camera is a top down view of the robot going around the course, and the second camera is the view from the robot's perspective as it moves along. I take the rendertarget of the robot camera and convert it to a Color[,] so that I can do image analysis on it.

private Color[,] TextureTo2DArray(Texture2D texture, Color[] colors1D, Color[,]   colors2D)
    {
        texture.GetData(colors1D);
        for (int x = 0; x < texture.Width; x++)
        {
            for (int y = 0; y < texture.Height; y++)
            {
                colors2D[x, y] = colors1D[x + (y * texture.Width)];
            }
        }

        return colors2D;
    }

I want to overlay the results of the image analysis on the robot camera view. The first part of the image analysis is finding the white pixels. When I find the white pixels I create a bool[,] array showing which pixels were white and which were black. Then I want to convert it back into a texture so that I can overlay on the robot view.

When I try to create the new texture showing which ones pixels were white, then the game goes super slow (around 10 hz).

Can you give me some pointers as to what to do to make the game go faster. If I comment out this algorithm, then it goes back up to 60 hz.

private Texture2D GenerateTexturesFromBoolArray(bool[,] boolArray,Color[] colorMap, Texture2D textureToModify)
    {            
        for(int i =0;i < screenWidth;i++)
        {
            for(int j =0;j<screenHeight;j++)
            {           
                if (boolArray[i, j] == true)
                {
                    colorMap[i+(j*screenWidth)] = Color.Red;
                }
                else
                {
                    colorMap[i + (j * screenWidth)] = Color.Transparent;
                }                
           }
    }

        textureToModify.SetData<Color>(colorMap);

        return textureToModify;
    }

Each Time I run draw, I must set the texture to null, so that I can modify it.

 public override void Draw(GameTime gameTime)
    {
        Vector2 topRightVector = ((SimulationMain)Game).spriteRectangleManager.topRightVector;
        Vector2 scaleFactor = ((SimulationMain)Game).config.scaleFactorScreenSizeToWindow;

        this.spriteBatch.Begin(); // Start the 2D drawing 
        this.spriteBatch.Draw(this.textureFindWhite, topRightVector, null,
           Color.White, 0, Vector2.Zero, scaleFactor, SpriteEffects.None, 0);
        this.spriteBatch.End(); // Stop drawing. 

        GraphicsDevice.Textures[0] = null;
    }

Thanks for the help,

Anthony G.

© Game Development or respective owner

Related posts about XNA

Related posts about c#