Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 2 (30 posts) 1 2 Next >

Odd BumpMap effect

Last post 9/7/2007 5:24 AM by Randomchaos Ltd. 29 replies.
  • 9/2/2007 6:06 PM

    Odd BumpMap effect

    I have implemented a bump map shader from a book by Wolfgang Engle, it all kind of works fine now that I get the model to load it's tangent data in my model content pipeline code. But it only seems to render correctly when I have my terrain object in the scene, the terrain object also uses a similar bump map technique but as it is a dynamic model these tangents and normals are generated by me at run time.

    Can anyone give me some clues as to why my bump effect renders oddly when my terrain object is not present? I have posted this up on another forum and as I don't have access to my server at the moment I am goint to link it here so you can see the effect I am getting. You will see the two images at the botom of my post.

    The terrain object and it's shader do not alter any render states..

    Thanks in advance, I have had a nightmare with normal and bump mapping right from the start with XNA, be good to finaly put this one to bed.

  • 9/3/2007 1:30 AM In reply to

    Re: Odd BumpMap effect

    You might be changing the render state through the shader. I know that I've had to change the order I draw things, or set renderstates between drawing different things.

    Although it is tough to say without having code. If you want to post a sample it might help.

  • 9/3/2007 4:17 AM In reply to

    Re: Odd BumpMap effect

    Well I am pretty sure the shader does not alter any render states, in both the bump and terrain shader, but here they are anyway.

    (I am not posting them in the code segment as it takes an age for them to get through the validation proces)

    BumpMap Shader:

    float4x4 wvp : WorldViewProjection;
    float4x4 world : World;
    float3 LightPosition : POSITION
    <
     string Object = "DirectionalLight";
        string Space = "World";
    > = {1.0f, -1.0f, 1.0f};
    float3 EyePosition : CAMERAPOSITION;

    struct VS_OUTPUT
    {
     float4 Pos : POSITION;
     float2 Tex : TEXCOORD0;
     float4 Light : TEXCOORD1;
     float3 lView : TEXCOORD2;
    };

    VS_OUTPUT VS(float4 Pos : POSITION,float2 Tex : TEXCOORD,float3 Normal : NORMAL,float3 Tangent : TANGENT)
    {
     VS_OUTPUT Out = (VS_OUTPUT)0;
      
     Out.Pos = mul(Pos,wvp);
     
     float3x3 worldToTangentSpace;
     worldToTangentSpace[0] = mul(Tangent,world);
     worldToTangentSpace[1] = mul(cross(Tangent,Normal),world);
     worldToTangentSpace[2] = mul(Normal,world);
     
     Out.Tex = Tex;
     
     float4 PosWorld = mul(Pos,world);
     
     Out.Light.xyz = mul(worldToTangentSpace,LightPosition);
     Out.Light.w = 1;
     Out.lView = mul(worldToTangentSpace,EyePosition - PosWorld);
     
     return Out;
    }

    texture ColorMap : RCMaterialParameter
    <
     string Object = "ColorMap";
     string UIName = "ColorMap";
    >;
    sampler ColorMapSampler = sampler_state
    {
     Texture = <ColorMap>;
    // MinFilter = Linear;
    // MagFilter = Linear;
    // MipFilter = Linear;
     magfilter = LINEAR;
     minfilter = POINT;
     mipfilter = NONE;
     AddressU = mirror;
     AddressV = mirror;
    };

    texture BumpMap : RCMaterialParameter
    <
     string Object = "BumpMap";
     string UIName = "BumpMap";
    >;
    sampler BumpMapSampler = sampler_state
    {
     Texture = <BumpMap>;
     MinFilter = Linear;
     MagFilter = Linear;
     MipFilter = Linear;
     AddressU = mirror;
     AddressV = mirror;
    };
    struct PixelToFrame
    {
        float4 Color : COLOR0;
    };

    PixelToFrame PS(float2 Tex : TEXCOORD0,float4 Light : TEXCOORD1,float3 lView : TEXCOORD2) : COLOR
    {
     PixelToFrame Out = (PixelToFrame)0;
     
     float4 Color = tex2D(ColorMapSampler,Tex);
     float3 Normal = (2 * (tex2D(BumpMapSampler,Tex))) - 1.0;
     
     float4 LightDir = normalize(Light);
     float3 ViewDir = normalize(lView);
     
     float Diffuse = saturate(dot(Normal,LightDir));
     float Reflect = normalize(2 * Diffuse * Normal - LightDir);
     
     float Specular = min(pow(saturate(dot(Reflect,ViewDir)),3),Color.w);
      
     Out.Color = 0.2 * Color + Color * Diffuse + Specular;

     return Out;
    }

    technique BumpMapShader
    {
        pass P0
        {
            Sampler[0] = (ColorMapSampler);  
            Sampler[1] = (BumpMapSampler);  
       
            VertexShader = compile vs_2_0 VS();
            PixelShader  = compile ps_2_0 PS();
        }
    }

     

    And this is the terrain shader:

    //////////////////////////////////////////////////////////////////////////////
    //                   //
    // NemoKradBumpTerrain.fx Terrain shader by C.Humphrey 02/05/2007          //
    //                   //
    // This shader is based on terrain shaders by Riemer and Frank Luna.       //
    //  Riemer:
    http://www.riemers.net           //
    //  Frank Luna:
    http://www.moon-labs.com         //
    //                   //
    // 
    http://randomchaos.co.uk            //
    // 
    http://randomchaosuk.blogspot.com          //
    //                   //
    // 25/06/2007 - Adde bumpmap functionlaity         //
    //                   //
    //////////////////////////////////////////////////////////////////////////////

    float4x4 wvp : WorldViewProjection;
    float4x4 world : World;
    float3 LightPosition : LightDirection;
    float3 EyePosition : CAMERAPOSITION;

    // Do we want to use bump map feature?
    bool bumpOn = true;

    // Texture size moifier 1 = no change.
    float tileSizeMod = 1;

    // Terrain Textures.
    texture  LayerMap0;
    texture  LayerMap1;
    texture  LayerMap2;
    texture  LayerMap3;

    // Terrain Normals for above texture.
    texture BumpMap0;
    texture BumpMap1;
    texture BumpMap2;
    texture BumpMap3;

    // Normal samplers
    sampler BumpMap0Sampler = sampler_state
    {
     Texture = <BumpMap0>;
     MinFilter = Linear;
     MagFilter = Linear;
     MipFilter = Linear;
     AddressU = mirror;
     AddressV = mirror;
    };

    sampler BumpMap1Sampler = sampler_state
    {
     Texture = <BumpMap1>;
     MinFilter = Linear;
     MagFilter = Linear;
     MipFilter = Linear;
     AddressU = mirror;
     AddressV = mirror;
    };

    sampler BumpMap2Sampler = sampler_state
    {
     Texture = <BumpMap2>;
     MinFilter = Linear;
     MagFilter = Linear;
     MipFilter = Linear;
     AddressU = mirror;
     AddressV = mirror;
    };

    sampler BumpMap3Sampler = sampler_state
    {
     Texture = <BumpMap3>;
     MinFilter = Linear;
     MagFilter = Linear;
     MipFilter = Linear;
     AddressU = mirror;
     AddressV = mirror;
    };

    // Texture Samplers
    sampler LayerMap0Sampler = sampler_state
    {
        Texture   = <LayerMap0>;
        MinFilter = LINEAR;
        MagFilter = LINEAR;
        MipFilter = LINEAR;
        AddressU  = WRAP;
        AddressV  = WRAP;
    };

    sampler LayerMap1Sampler = sampler_state
    {
     Texture   = <LayerMap1>;
     MinFilter = LINEAR;
     MagFilter = LINEAR;
     MipFilter = LINEAR;
     AddressU  = WRAP;
        AddressV  = WRAP;
    };

    sampler LayerMap2Sampler = sampler_state
    {
        Texture   = <LayerMap2>;
        MinFilter = LINEAR;
     MagFilter = LINEAR;
     MipFilter = LINEAR;
     AddressU  = WRAP;
        AddressV  = WRAP;
    };
    sampler LayerMap3Sampler = sampler_state
    {
        Texture   = <LayerMap3>;
        MinFilter = LINEAR;
     MagFilter = LINEAR;
     MipFilter = LINEAR;
     AddressU  = WRAP;
        AddressV  = WRAP;
    };

    // Vertex Shader input structure.
    struct VS_INPUT
    {
     float4 posL         : POSITION0;
        float3 normalL      : NORMAL0;
        float4 tiledTexC    : TEXCOORD0;
        float4 TextureWeights : TEXCOORD1;
        float3 Tangent : TANGENT;
    };

    // Vertex Shader output structure/Pixel Shaer input structure
    struct VS_OUTPUT
    {
     float4 posH         : POSITION0;
        float  shade        : TEXCOORD0;
        float4 tiledTexC    : TEXCOORD1; 
        float4 TextureWeights : TEXCOORD2;  
     float4 Light : TEXCOORD3;
     float3 lView : TEXCOORD4;
    };

    // Vetex Shader
    VS_OUTPUT BumpVS(VS_INPUT input)
    {
     // Clean the output structure.
     VS_OUTPUT Out = (VS_OUTPUT)0;
      
     // Calculate tangent space.
     float3x3 worldToTangentSpace;
     worldToTangentSpace[0] = mul(input.Tangent,world);
     worldToTangentSpace[1] = mul(cross(input.Tangent,input.normalL),world);
     worldToTangentSpace[2] = mul(input.normalL,world); 
     
     // Get world pos for texture and normal.
     float4 PosWorld = mul(input.posL,world); 
     
     // Get light position.
     Out.Light.xyz = LightPosition;
     Out.Light.w = 1;
     
     // Set position for pixel shader
        Out.posH  = mul(input.posL, wvp);        
       
        // Set lighting.
        Out.shade = saturate(saturate(dot(input.normalL, normalize(LightPosition))));   
       
        // Set view direction for normals.
     Out.lView = mul(worldToTangentSpace,EyePosition - Out.posH); 
       
        // Set tile TexCoord.
        Out.tiledTexC = input.tiledTexC * tileSizeMod;
      
     // Set Texture weight.
        Out.TextureWeights = input.TextureWeights;   
     
     return Out;
    }
    // Output to screen.
    struct PixelToFrame
    {
        float4 Color : COLOR0;
    };

    // Pixel shader.
    PixelToFrame BumpPS(VS_OUTPUT input) : COLOR
    {
     // Clean output structure.
     PixelToFrame Out = (PixelToFrame)0; 
     
     // Get pixel color.
     float4 Col = tex2D(LayerMap0Sampler, input.tiledTexC)*input.TextureWeights.x;
        Col += tex2D(LayerMap1Sampler, input.tiledTexC)*input.TextureWeights.y;
        Col += tex2D(LayerMap2Sampler, input.tiledTexC)*input.TextureWeights.z;
        Col += tex2D(LayerMap3Sampler, input.tiledTexC)*input.TextureWeights.w;
       
        // Set light directon amd view direction.
        float4 LightDir = normalize(input.Light);
     float3 ViewDir = normalize(input.lView);
     
     // Get prominent normal. 
     float2 nearTextureCoords = input.tiledTexC*3;
     float3 Normal;
     Normal = tex2D(BumpMap0Sampler,nearTextureCoords)*input.TextureWeights.x;
     Normal += tex2D(BumpMap1Sampler,nearTextureCoords)*input.TextureWeights.y;
     Normal += tex2D(BumpMap2Sampler,nearTextureCoords)*input.TextureWeights.z;
     Normal += tex2D(BumpMap3Sampler,nearTextureCoords)*input.TextureWeights.w; 
     Normal = 2 * Normal - 1.0; 
      
     // Set diffuse, reflection and specular effect for Normal.
     float Diffuse = saturate(dot(Normal,LightDir)); 
     float Reflect = normalize(2 * Diffuse * Normal - LightDir);
     float Specular = min(pow(saturate(dot(Reflect,ViewDir)),3),Col.w); 
     
     float4 final;
     
     // Do color calculation depending if bump feature is on or off.
     if(bumpOn)
      final = (0.2 * Col * (Diffuse + Specular)) * (input.shade * 12);
     else
      final = Col * input.shade;

     Out.Color = final;
     
     return Out;
    }

    technique Terrain_MultiTex
    {
        pass P0
        {
      vertexShader = compile vs_2_0 BumpVS();
            pixelShader  = compile ps_2_0 BumpPS();
        }   
    }

    As you can see there are no alterations to the render states in the shaders and the bump map technique is the same. Thanks for taking a look. 

  • 9/3/2007 5:49 AM In reply to

    Re: Odd BumpMap effect

    Just had another dig around..

    I can get the bad bump effect with the terrain object loading if I have a terrain map that is 46 X 46 (or greater) it still works fine, but 45 X 45 and I get the issue... This comes out at 2116 verts, now this does not leap out to me as one of those magic numbers, but I have been on holiday and my brain is probably not fully engaged....

    any one got any ideas??

  • 9/3/2007 3:17 PM In reply to

    Re: Odd BumpMap effect

    Shamefull bump....

    Anyone got any ideas, this is killing me :(

  • 9/4/2007 3:31 AM In reply to

    Re: Odd BumpMap effect

    I don't see any screen shots in that forum that you linked too.

    I doubt that its the cause of your problem, but why are you setting the samplers directly in your bump shader technique?

    Sampler[0] = (ColorMapSampler);
    Sampler[1] = (BumpMapSampler);

    I don't see any reason for doing that in the code that you've showed.

    Cheers,
    Leaf.

  • 9/4/2007 4:47 AM In reply to

    Re: Odd BumpMap effect

    Ahh, that is from Wolfgangs code for the effect. It is not explained in the book. I have removed it and it has no effect on my issue.

    Here are the images in another link:

    Good Bump Effect (when terrain object is in play and greater than 45X45)

    Bad Bump Effect (When terrain is not in use or less than 46X46)

     

    Thanks for taking a look.

  • 9/4/2007 11:56 AM In reply to

    Re: Odd BumpMap effect

    Maybe I'm being dim, but those screenshots look very similar to me?

    To narrow down where things are different, try using PIX. Take a single frame capture from each of the two situations, run two copies of PIX side by side, and examine all the GPU state at the point when the object you are interested in is drawn. That makes it easy to see what piece of state is different between the two.
  • 9/4/2007 4:40 PM In reply to

    Re: Odd BumpMap effect

    Thanks Shawn I will give that a go.

    There is a difference in the imeges, the bad render has poor quality in the detail and (you an't see it in the shots) where the light changes from light to the dark side, in the bad render it is sharp where as in the good render it is nice and smooth.

    I have put 2 new shots up

    GoodBump2 - In this shot you can see the line between light and dark indicated by the white arrows (do you love my free hand?) I have put an ellipse showng an area behind the line on the shadow side where some of the bump is being lit, as it should be.

    BadBump2 - Again I show the line between light and shadow, you can see this is a much more defined line and again an ellipse to show where the the bump is NOT being lit.

    It is better to see when the thing is running as I have the planet rotating and so it shows the issue up better.

    I have just had a quick look at the sample off this site for normal mapping and it runs a treat, but I notice that you are manipulating the normal images in the custome pipeline and that the images have to be associated with the mesh. I am going to try and extract these texture manipulation items and see if I can get them to work in my stuff. The only trouble is I dont want to have to associate my textures in the mesh as I want one mesh to represent n items that have different textures. So what I am thinking of doing is puting my normal maps through this new content pipeline and see how this goes.

    Could one of you guys explain why the normals are manipulated in the pipeline and not just applied, or am I missing the point with this sample??

    If you guys have any other ideas I am all ears :)

    Also, I have had nothing but bother with bump/normal maps in XNA and I am wondering if anyone else has had eithr the same issues or found it difficult to implement, if you have do let me know I would love to see your issues. Their seems to be lots of bits and pices that are not obvious to the noobie 3D developer. My first issue was not knowing that X files did not come with tangent data and that it had to be configured in the custom pipeline, I now recalc the tangent, normal and binormal of my meshs in the custom pipeline, be nice to have them all hammered out :)

  • 9/4/2007 5:32 PM In reply to

    Re: Odd BumpMap effect

    Now this is odd, having applied the NormalMapTextureProcessor to my custom content pipeline the bumped shphere (when terrain is in play) looks much better, and I did not noticec it before but the light angle I was setting was coming out wrong and this, thanks to this proessor has been fixed. However, if I now remove the terrain from play or drop the size of the terrain to 45X45 or less I now get NO bump effect.

    I am now totaly stumped as to what could be going on here... If no one has any other ideas I am going to try PIX and try and see what is changing. Personaly I dont think it is anything to do with the states as if it was it would be consistent regardless of the terrain size.

    I can see that if I want bump effects in my stuff I am going to have to have a small patch of terrain apositioned at new Vector3(-10000,-10000,-10000); :(

    Would it be possible for me to put up a sample projet and one fo you guys have a look at it? This truly is anoying the pants off me...

    Thanks in advance.

  • 9/5/2007 4:17 PM In reply to

    Re: Odd BumpMap effect

    Right, had a go with PIX and snaped 2 shots, now there is a lot of data there, is there a nice easy way to compare differences from wahc frame snapshot?

    Also, can anyone answer my questions above as to why in the normal map samle you are altering the format of the normal map in the custome pipeline?

    Thanks.

  • 9/5/2007 4:42 PM In reply to

    Re: Odd BumpMap effect

    PIX actually has a function to compare bitmaps built in - File/Compare Images. Of course your app needs to be totally predictable so that if you grab frame X from app1 and app2 everything is in the same positions (otehr than the differences you expect to see of course)
  • 9/6/2007 5:25 AM In reply to

    Re: Odd BumpMap effect

    Sorry if this is a daft question, but how will comparing two images tell me what the render states are at the time of capture??

    The PIX method I am using to discover the render states at the time of capture is "A single-frame cature of Direct3D whenever F12 is pressed". So what I am doing is hitting F12 before I display the terrain and then again after, this is giving me two frames to compare. Before the terrain is displayed I am getting the bad render, once it is displayed I get the good bump render and this can be clearly seen at run time. However, when I take a look at the images (render tab of the details window)that PIX has for the two frames they look identical, what is going on there?!?!

    So back to my question, is there a nice quick way to compare the differences in render states between the two frames I have from the PIX application?

  • 9/6/2007 7:06 AM In reply to

    Re: Odd BumpMap effect

    The render tab (and most other details tabs) updates according to what event you have selected, you'll need to make sure you select an event towards the end of each frame that you are examining, or after the draw call that you are interested in.

    To see the render states, bring up the device details tab by double clicking on the blue hex address at the left of most events - from there you can examine various render states. By clicking on events you can see the render states at different times - I would suggest switching between the draw events for your 'good' and 'bad' version. If they are very far apart in the events view then you can save the render state reports as html and view them side by side (perhaps you could do a diff/compare on the html files).

    Cheers,
    Leaf.
  • 9/6/2007 9:36 AM In reply to

    Re: Odd BumpMap effect

    Took a look at the render at the end of each frame, the images are identical. I have just ran the code on my desktop wich has  a much better graphics card and the effect is even worse when the terrain is not in play. It is the same effect I used to get way back at the start of my bump shader issues. I figure that the tangents are all screwed up, even though I have the MeshHelper stuff generating them in the custom content pipelie. My terrain object has it's tangents calculated manualy by me and I am guessing, for what ever reason once these tangents go throught the pipleine it seems to put it all right again, even if I then remove the terrain from the draw the mesh stays good, if I stop and start the app again the bad render comes back until the terrain is drawn again.

    Thanks to all for your help, I guess XNA and bump/normal mapping and me just dont go together at the moment... :(

  • 9/6/2007 10:33 AM In reply to

    Re: Odd BumpMap effect

    I know, I am like a thorn in the side of this forum, but I would really like to undertand why my bump/normal mapping is not behaving as expected.

    I have created a project that is displaying my issue, if one of you good people out there could just spend 10 minutes or so having a look and seeing the behavior. I have put the source here for down load. If you just execute it there is some on screen text that will show you how to create the issue and move the camera so you can have a good look.

    It has my shaders in it along with some basic textures to be bumped, I have also added my custom pipeline to this download so you can see hoe I am generating the tangents for the mesh.

    I have set the terrain to be at height 0 all over so as not to have to include a height map.

    Thanks, in advance, again...

  • 9/6/2007 11:11 AM In reply to

    Re: Odd BumpMap effect

    Chazsoft:

    Sorry if this is a daft question, but how will comparing two images tell me what the render states are at the time of capture??

    It isn't, it won't and I need to read replies more carefully ;-)

  • 9/6/2007 11:30 AM In reply to

    Re: Odd BumpMap effect

    I'm not 100% sure how to recreate this... my guess is that pressing Q & T should have some effect on the drawing and I see nothing (though  also don't see any terrain appear). This is on an Nvidia 8800 GTX if thats makes any difference.

    I did get some shader compiler warnings

    Warning 3 Warning compiling ...\BumpIssue\Content\Shaders\Planet.fx: warning X3083: Truncating 3-vector to size 1 .....BumpIssue\Content\Shaders\Planet.fx 91 1 BumpIssue

  • 9/6/2007 12:25 PM In reply to

    Re: Odd BumpMap effect

    Are you sure you are using the same sphere model? Just take out the all the bump map and texture, I feel that the light is much smoother on the GoodExample. And I guess you need to output your highmaps (good and bad) and compare it to the preset. Or you know, export the good result to a high map apply it and see if it still look good.

    Take out the texture.

    And sorry, I am totally noob on this.

  • 9/6/2007 12:54 PM In reply to

    Re: Odd BumpMap effect

    Your sphere mesh has no tangents because your custom model processor does not work. You shouldn't assume that there is only one NodeContent and that it is a MeshContent. You should walk the scene hierarchy looking for MeshContent nodes and converting each one.

    That it looked good when you were rendering the terrain was pure fluke - it was just different random data for the vertex shader to read.

    Not related to the issue but...

    You're converting the normal map to a normalized texture format, so you don't need to scale and bias the normal that you read from the texture in the pixel shader.

    You shouldn't create vertex declarations over and over again in your terrain rendering method.

    Cheers,
    Leaf.





  • 9/6/2007 2:14 PM In reply to

    Re: Odd BumpMap effect

    Thank you all for taking a look at this.

    Leaf, thanks for the correction of the tangent creation stuff, I will take a look and try and fix that tonight, I hope this fix's it :) As to the terrain class, this is a very trimmed down version of it and it builds the vertex each frame as in the full class it is self culling, thats to say depending on the culling depth you give the class it will not render terrain outside of that and so you can have a massive terrain map and it still flys along over 60FPS (depending on your culling distance). Probably didn't explain that to well, but that is why it is like that, should have moded it for this example.

    Oh and thanks for the explination of the normal map manipulation.

    Again, thanks to you all it was driving me mad this :) I will take a look at how the tangents are built in the sample normal map and alter my pipeline to be the same.

  • 9/6/2007 2:26 PM In reply to

    Re: Odd BumpMap effect

    If you hit T the terrain is drawn bellow the starting point of the camera so if you use the arrow keys to rotate the camera you will see the bumped terrain bellow you. The textures may not have changed, as Leaf pointed out, the reason why it got drawn ok when the terrain was there was total fluke the data might not be landing in the same location in memory on you machine and so it is not giving the illusion of a good render.
  • 9/6/2007 2:37 PM In reply to

    Re: Odd BumpMap effect

    Leaf,

    I have altered my GenerateTangents method in the custom pipeline to this:

    MeshContent mesh = input as MeshContent;

    if (mesh != null)

    {

    MeshHelper.CalculateTangentFrames(mesh,

    VertexChannelNames.TextureCoordinate(0),

    VertexChannelNames.Tangent(0),

    VertexChannelNames.Binormal(0));

    }

    foreach (NodeContent child in input.Children)

    {

    GenerateTangents(child);

    }

    Which is pretty much 100% taken from the normal map sample from here, and yet my model is still not rendering correctly. Can you maybe pop a sample up here on how it should be done, you know me, it never sinks in if there is no example to look at..

  • 9/6/2007 3:10 PM In reply to

    Re: Odd BumpMap effect

    Answer
    Reply Quote
    The corrected model processor:

    namespace CustomContentPipeline
    {
    [ContentProcessor]
    public class MyModelProcessor : ModelProcessor
    {
    double minX = double.MaxValue;
    double minY = double.MaxValue;
    double minZ = double.MaxValue;
    double maxX = double.MinValue;
    double maxY = double.MinValue;
    double maxZ = double.MinValue;

    List<BoundingBox> boxes = new List<BoundingBox>();
    List<List<Vector3>> MeshVerts = new List<List<Vector3>>();
    object[] ModelData = new object[2];

    // Bounding Box's
    private void CheckNode(NodeContent content)
    {
    foreach (NodeContent o in content.Children)
    {
    if (o is MeshContent)
    {
    // Get VertData
    GetAllVerticies((MeshContent)o);

    BoundingBox bb = new BoundingBox();

    minX = double.MaxValue;
    minY = double.MaxValue;
    minZ = double.MaxValue;
    maxX = double.MinValue;
    maxY = double.MinValue;
    maxZ = double.MinValue;

    MeshContent mesh = (MeshContent)o;

    foreach (Vector3 basev in mesh.Positions)
    {
    Vector3 v = basev;

    if (v.X < minX)
    minX = v.X;

    if (v.Y < minY)
    minY = v.Y;

    if (v.Z < minZ)
    minZ = v.Z;

    if (v.X > maxX)
    maxX = v.X;

    if (v.Y > maxY)
    maxY = v.Y;

    if (v.Z > maxZ)
    maxZ = v.Z;
    }

    double lenX = maxX - minX;
    double lenZ = maxZ - minZ;
    double lenY = maxY - minY;

    bb.Min = new Vector3((float)minX, (float)minY, (float)minZ);
    bb.Max = new Vector3((float)maxX, (float)maxY, (float)maxZ);

    boxes.Add(bb);
    }
    else
    CheckNode(o);
    }
    }
    // Vertex positions
    private void GetAllVerticies(MeshContent mesh)
    {
    for (int g = 0; g < mesh.Geometry.Count; g++)
    {
    GeometryContent geometry = mesh.Geometry[g];
    List<Vector3> temp = new List<Vector3>();
    for (int ind = 0; ind < geometry.Indices.Count; ind++)
    {
    // Transforms all of my verticies to local space.
    Vector3 position = Vector3.Transform(geometry.Vertices.Positions[geometry.Indices[ind]], mesh.AbsoluteTransform);
    temp.Add(position);
    }
    MeshVerts.Add(temp);
    }
    }
    // Tangents.
    private void GenerateTangents(NodeContent input, ContentProcessorContext context)
    {
    MeshContent mesh = input as MeshContent;

    if (mesh != null)
    {
    MeshHelper.CalculateTangentFrames(mesh,
    VertexChannelNames.TextureCoordinate(0),
    VertexChannelNames.Tangent(0),
    VertexChannelNames.Binormal(0));

    }

    foreach (NodeContent child in input.Children)
    {
    GenerateTangents(child, context);
    }
    }

    // Normals
    private void GenerateNormals(NodeContent input, ContentProcessorContext context)
    {
    MeshContent mesh = input as MeshContent;

    if (mesh != null)
    {
    MeshHelper.CalculateNormals(mesh, true);
    }

    foreach (NodeContent child in input.Children)
    {
    GenerateNormals(child, context);
    }
    }

    public override ModelContent Process(NodeContent input, ContentProcessorContext context)
    {
    // Calculate Mesh Tangents.
    GenerateNormals(input, context);

    // Calculate Mesh Normals.
    GenerateTangents(input, context);

    // Setup bounding box data.
    CheckNode(input);

    ModelData[0] = boxes;
    ModelData[1] = MeshVerts;

    ModelContent basemodel = base.Process(input, context);
    basemodel.Tag = ModelData;
    return basemodel;
    }
    }
    }

    That produces the correct data now.

    Cheers,
    Leaf.
  • 9/6/2007 3:32 PM In reply to

    Re: Odd BumpMap effect

    Dear Leaf,

    You are (to me) a GOD in the land of XNA!! This issue has been breaking my heart... You will have to arrange a meeting of the XNA UK User Group, and I will happily buy you your drinks ALL night!

    Oh, in case you did not realise, this fixed it :)

     

    For the record, how did you know the tangent data was built badly and that once you made the mods it would be correct?

Page 1 of 2 (30 posts) 1 2 Next > Previous Discussion Next Discussion