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

raycast texture coordinates of a triangle

Last post 9/3/2013 8:13 PM by 49ers94. 5 replies.
  • 1/11/2013 3:19 AM

    raycast texture coordinates of a triangle

    My overall goal is to try to create certain sounds and particle effects based on the texture of the terrain the character is walking on so I started with the ray picking sample at the msdn website below.

    I found a second link(below) that was helpful but wasnt sure how to modify the ray triangle method according to the instructions(modify the RayIntersectsModel method that does the triangle test so it will output the indices of the closest intersecting triangle's vertices -- right now it just outputs the vertex positions. This is in the inner most block of the method, where it stores the information for the closest detected intersection:

    // If so, is it closer than any other previous triangle?  
    if ((closestIntersection == null) || (intersection < closestIntersection))  
    // .... 

    You just need to add an integer output parameter for the method and store the value of the i counter. If RayIntersectsModel returns with a hit, you use i, i+1, and i+2 as the indices into the texcoord list you have stored in the model's tag data. )

    RayIntersectsTriangle(ref ray,
    ref vertices[i],
    ref vertices[i + 1],
    ref vertices[i + 2],
    out intersection);

    // Does the ray intersect this triangle?
    if (intersection != null)
    // If so, is it closer than any other previous triangle?
    if ((closestIntersection == null) ||
    (intersection < closestIntersection))
    // Store the distance to this triangle.
    closestIntersection = intersection;

    // Transform the three vertex positions into world space,
    // and store them into the output vertex parameters.
    Vector3.Transform(ref vertices[i],
    ref modelTransform, out vertex1);

    Vector3.Transform(ref vertices[i + 1],
    ref modelTransform, out vertex2);

    Vector3.Transform(ref vertices[i + 2],
    ref modelTransform, out vertex3);
  • 1/11/2013 5:15 AM In reply to

    Re: raycast texture coordinates of a triangle

    NOTE: i didn't test this, but im interested in this code and think ill try to modify the XNA triangle picking sample to do what you want.

    To the RayIntersectModel() you would add Vector2's to hold the return texture coordinate's

    static float? RayIntersectsModel(

    Ray ray, Model model, Matrix modelTransform,
    out bool insideBoundingSphere,
    out Vector3 vertex1, out Vector3 vertex2,
    out Vector3 vertex3, out Vector2 texCoord1,
    out Vector2 texCoord2, out Vector2 texCoord3)

    Right after this code, at line 368
    // Keep track of the closest triangle we found so far
    // so we can always return the closest one,
    float? closestIntersection = null;

    Add This:

    lookUpIndex = 0; 


    In the For(;;) loop after this code at line 393:
    // Store the distance to this triangle.
    closestIntersection = intersection;

    Add This:
    lookUpIndex  = i;

    At line 409 before the return, add this:
    Vector2[] texCoords = (Vector2[])tagData["TexCoordinates"];
    texCoord1 = texCoords[lookUpIndex + 0];
    texCoord2 = texCoords[lookUpIndex + 1];
    texCoord3 = texCoords[lookUpIndex + 2];


    So you added texture coordinates to the model and have modified the 'TrainglePickingProcessor.cs" to recognize them and include them with the dictionary data referenced by 'model.tag' ?

    I guess the idea is to use these texture coordinates to reference into the texture you have layed over your terrain ?






  • 1/11/2013 5:18 AM In reply to

    Re: raycast texture coordinates of a triangle

    YIKES! i cant get the format right, :-(
  • 1/11/2013 9:23 AM In reply to

    Re: raycast texture coordinates of a triangle

    THANKS!!! for the answer Sick Kreation Ive been posting all over the web for someone to help me
    with that problem, I greatly appreciate the job you guys do Thankyou, now I can finally start experimenting
    with the code again THANKYOU!!!
  • 9/3/2013 6:59 PM In reply to

    Re: raycast texture coordinates of a triangle

    Did you ever get this to work?
  • 9/3/2013 8:13 PM In reply to

    Re: raycast texture coordinates of a triangle

    Hi lighthouse

     Ive tried many approaches since then, at the time I was trying to apply that
     formula to a heightmap(riemers link below) but never got it to work,my problem
    was that I thought that I could use multiple layers of texture coordinates  for one
    mesh but was told outside of the dualtextureeffect(link2 below) it wasnt possible,
    so I went with the tiled terrain in riemers flightsim tutorial(link below)and it worked out
    perfectly. the last link below is what I finally ended up with.


    riemers heightmap link

    dualtexture link

    flightsim floorplan

    tile terrain experiment
Page 1 of 1 (6 posts) Previous Discussion Next Discussion