XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

How to make a cubemap...and use it.

Last post 8/5/2008 9:23 AM by DynamicSamurai. 2 replies.
  • 8/4/2008 6:34 AM

    How to make a cubemap...and use it.

    Hi All,

    Kinda new to XNA and Computer Graphics in general, trying to do shadow mapping for a point light (as opposed to a directional light). I got it semi working rendering six separate textures, and then projectively texturing them into my main view over six passes - as you can imagine it was incredibly slow (12 passes in all just to get to a lightmap of the scene!!). So I decided that I should use a cubemap instead of 6 separate textures, thus allowing me to do the projective texturing in a single pass as opposed to six.

    Herein lies my problem - I am at a complete loss as to how I actually make the cubemap.

    What I am doing at the moment is something like the following (not at my development machine right now, so treat this more like psuedo code):

     

     

    // Fields  

    RenderTargetCube cubeRT;  
    TextureCube cubeTex;  
    ...  
     
    // Constuctor  
    cubeRT = new RenderTargetCube( device, device.PresentationParameters.BackBufferWidth, SurfaceFormat.Single );  
     
    ...  
     
    // Draw Method  
     
    device.SetRenderTarget(0, cubeRT, CubeFace.PositiveX );  
    drawMySceneUsingShadowMapHLSLEffect(positiveXViewProjectionMatrix);    // I don't really have that method, it is an illustrative example  
     
    device.SetRenderTarget(0, cubeRT, CubeFace.PositiveY );  
    drawMySceneUsingShadowMapHLSLEffect(positiveYViewProjectionMatrix);    // I don't really have that method, it is an illustrative example  
     
    device.SetRenderTarget(0, cubeRT, CubeFace.PositiveZ );  
    drawMySceneUsingShadowMapHLSLEffect(positiveZViewProjectionMatrix);    // I don't really have that method, it is an illustrative example  
     
     // ... and so on for each face.  
     
    device.SetRenderTarget(0, null);  
    cubeTex = cubeRT.GetTexture();  
     
     
     

    After getting that texture I draw the scene again, using my DiffuseLightMap HLSL technique. I'll explain my algorithm:

    The cube texture *should* contain depth maps for each of its views. Working under this assumption, the pixel shader of my DiffuseLightMap shader calcualtes the distance between each pixel and the light, on one hand, and looks up the light's perceived depth to that pixel (calculate vector from pixel to light, and then use that as the second argument to texCUBE( CubeMapSampler, vectorToPixel) ). I just realised that I probably need to normalize that vector - I will have to try that. Anyhow, continuing on. With the two distances calculated, I compare them to decide if the pixel is in shadow or not.

    I have got shadow mapping working for a directional light, so I know that my shadow map shader, and my projective texturing (the latter in principle) shader work. What I don't know is if I am generating the cubemap right, and whether I am consuming it correctly in my projective texturing shader.

    I will try to post the actual code when I get home, but perhaps if someone could post a simple example on using a cube map (needn't be shadow mapping, I am sure that if I can get even a basic cube map for reflections or something working I can extend it to shadow mapping without too much trouble).

    Thanks in advance.

     

    I'm too busy writing computer graphics for my day job to have any games in play-test or peer-review.
    Twitter: twitter/guysherman
    Blog: www.guysherman.com
  • 8/4/2008 2:27 PM In reply to
    • (0)
    • premium membership
    • Posts 86

    Re: How to make a cubemap...and use it.

    Hi DynamicSamurai,

    Try putting device.SetRenderTarget(0, null) after each call to drawMyScene(). On the xbox all rendertargets share a single area of fast memory- calling SetRenderTarget(0, null) tells it to copy from this memory to the location the texture memory is stored. So if you don't call it - you are just overwriting the same piece of memory and never "saving" it. Sorry it is early for me, and no coffee yet, but I hope this makes sense ;-) (Also, FYI, by default render targets behave the same way on the PC, even though this isn't how PC video cards generally work - this is done to keep render target behavior consistent between PC and Xbox)

    Heres a link that describes how this works better than i did: http://blogs.msdn.com/shawnhar/archive/2007/11/21/rendertarget-changes-in-xna-game-studio-2-0.aspx .

    Give that a shot and if you still have problems let me know. I do have code in an old project where I generated cubemaps in designated places in a level to use for reflections. I can dig that up if you are still running into problems.

    Good luck!
    Bryan

  • 8/5/2008 9:23 AM In reply to

    Re: How to make a cubemap...and use it.

    Hi Bryan,

     Thanks for your help - it turned out I was doing that already and my problem was acutally a problem with the view matrices I was using for each face of the cube map.

     So now I have a working cube map, but I am having some HLSL issues, but I don't think this is necessarily the forum for those, so I'll find a more appropriate place to post. Thanks anyhow.

    I'm too busy writing computer graphics for my day job to have any games in play-test or peer-review.
    Twitter: twitter/guysherman
    Blog: www.guysherman.com
Page 1 of 1 (3 items) Previous Next