Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:20:49+00:00 2026-06-04T18:20:49+00:00

I am currently developing a class for my XNA game whose rendering the lights

  • 0

I am currently developing a class for my XNA game whose rendering the lights on the image. At the time, i have made the source to draw my lightmap, however, the FPS is very low in my source. I know that it is brutally reduced upon looping through each pixel, however, I do not know any other way to get & set each pixel on my Texture in XNA but using the “For” statement?

Current Source:

 public struct Light
    {
        public int Range;
        public int Intensity;
        public Color LightColor;
        public Vector2 LightLocation;

        public Light(int _Range, int _Intensity, Color _LightColor, Vector2 _LightLocation)
        {
            Range = _Range;
            Intensity = _Intensity;
            LightLocation = _LightLocation;
            LightColor = _LightColor;
        }
    }
    public class RenderClass
    {
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern bool MessageBox(IntPtr h, string S, string C, int a);

        public static Texture2D RenderImage(Light[] LightLocations, Texture2D ScreenImage, Viewport v, bool ShadowBack = false)
        {
            Texture2D[] Images = new Texture2D[LightLocations.Count()];
            int curCount = 0;

            /*LOOP THROUGHT EACH LIGHT*/
            foreach (Light LightLocation in LightLocations)
            {
                /*VARIABLES*/
                Color LightColor = LightLocation.LightColor;
                int Range = LightLocation.Range;
                int Intensity = LightLocation.Intensity;

                /*GET COLORS*/
                int Width = v.Width;
                int Height = v.Height;
                Color[] Data = new Color[Width * Height];
                ScreenImage.GetData<Color>(Data);

                /*VARIABLES TO SET COLOR*/
                Color[] SetColorData = new Color[Width * Height];

                /*CIRCEL*/
                int Radius = 15 / 2; // Define range to middle [Radius]
                int Area = (int)Math.PI * (Radius * Radius);

                for (int X = 0; X < Width; X++)
                {
                    for (int Y = 0; Y < Height; Y++)
                    {
                        int Destination = X + Y * Width;

                        #region Light
                        /*GET COLOR*/
                        Color nColor = Data[Destination];

                        /*CREATE NEW COLOR*/
                        Vector2 MiddlePos = new Vector2(LightLocation.LightLocation.X + Radius, LightLocation.LightLocation.Y + Radius);
                        Vector2 CurrentLocation = new Vector2(X, Y);

                        float Distance;
                        Distance = Vector2.Distance(MiddlePos, CurrentLocation);
                        Distance *= 100;
                        Distance /= MathHelper.Clamp(Range, 0, 100);

                        Vector3 newColors = nColor.ToVector3();

                        nColor = new Color(
                            newColors.X,
                            newColors.Y,
                            newColors.Z, 
                            Distance / 100);


                        /*SET COLOR*/
                        SetColorData[Destination] = nColor; // Add to array
                        #endregion
                        #region Shadow
                        #endregion
                    }
                }


                ScreenImage.SetData<Color>(SetColorData);
                Images[curCount] = ScreenImage;
                curCount++;
            }

            return Images[0]; // Temporarily returning the first image of the array.
        }
    }

As you can see, this is a slow and bad method. So I was wondering, is there a better way to get & set each pixel?

Thanks in advance, dotTutorials! =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:20:50+00:00Added an answer on June 4, 2026 at 6:20 pm

    I think that job would be best done in a pixel shader.

    You could create an Effect file that operates over one light at a time.
    XNA uses DX9 so you’ll be limited to 128 constant registers, which I think you can use to squeeze up to three lights.

    So you set your lightmap as a render target, loop through all the lights, set the constant data on your effect, render a render-target-sized quad and in your pixel shader compute your lighting equation.

    In essence something like that:

        // In LoadContent
    RenderTarget2D lightmapRT = new RenderTarget2D(graphics.GraphicsDevice,
                                                     128,
                                                     128,
                                                     false, //No mip-mapping
                                                     SurfaceFormat.Color,
                                                     DepthFormat.Depth24);
    
    // We now render to the lightmap in Render method
    graphics.GraphicsDevice.SetRenderTarget(lightmapRT);
    
    // Lightmap is black by default
    graphics.GraphicsDevice.Clear(Color.Black);
    
    // Use the sprite batch to draw quads with custom shader
    spriteBatch.Begin(0, BlendState.Opaque, null, null, null, lightmapFx);
    
    foreach (var light in lights)
    {
        // Pass the light parameters to the shader
        lightmapFx.Parameters["Viewport"].SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
        lightmapFx.Parameters["Range"].SetValue(light.Range);
        lightmapFx.Parameters["Intensity"].SetValue(light.Intensity);
        lightmapFx.Parameters["LightColor"].SetValue(light.LightColor);
        lightmapFx.Parameters["LightLocation"].SetValue(light.LightLocation);
    
        // Render quad
        spriteBatch.Draw(...);
    }
    spriteBatch.End();
    

    And the FX file would look something like that:

    float Range;
    float Intensity;
    float3 LightColor;
    float2 LightLocation;
    float2 Viewport;
    
    struct VStoPS
    {
        float4 Position : POSITION0;
        float2 TexCoord : TEXCOORD0;
    };
    
    VStoPS VS(in float4 color    : COLOR0,
              in float2 texCoord : TEXCOORD0,
              in float4 position : POSITION0)
    {
        VStoPS vsout = (VStoPS)0;
    
        // Half pixel offset for correct texel centering.
        vsout.Position.xy -= 0.5;
    
        // Viewport adjustment.
        vsout.Position.xy = position.xy / Viewport;
        vsout.Position.xy *= float2(2, -2);
        vsout.Position.xy -= float2(1, -1);
    
        // Pass texcoords as is
        vsout.TexCoord = texCoord;
    
        return vsout;
    }
    
    float4 PS(VStoPS psin)
    {
        // Do calculations here
        // Here I just set it to white
        return float4(1.0f, 1.0f, 1.0f, 1.0f);
    }
    
    technique Main
    {
        pass p0
        {
            VertexShader = compile vs_3_0 VS();
            PixelShader = compile ps_3_0 PS();
        }
    }
    

    Note that this non-tested code and probably full of errors. I leave it up to you to figure out what needs to go in the pixel shader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, In my app that I'm currently developing, I have a class that
I am developing a web form using Visual Web Developer Currently I have class
I'm currently developing a 2D game with C#/XNA. The game's core feature are bullets
I am currently developing an asp.net web app. I have a class which is
I'm developing a 2D overhead shooter game using C# and XNA. I have a
Our team is currently developing a web application, in that we have a class
Im am currently developing an automated test class (running several individual tests on other
I'm currently developing a custom control that derives from CStatic MFC class (Smart Device
Currently developing a PHP framework and have ran into my first problem. I need
Hi LaTeX enthusiasts and TeX programmers! I'm currently developing a single-page document class for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.