Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6
This is the forum to discuss the Wrox book Professional XNA Game Programming: For Xbox 360 and Windows by Benjamin Nitschke; ISBN: 9780470126776
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 11th, 2007, 10:11 AM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Vertex normals

Hello!

Where I can read about the vertex normal computation algorithm mentioned on page 346?
I have not found a mention of this algorithm in the book.

"
// Step 2: Calculate all edge vectors (for normals and tangents)
// This involves quite complicated optimizations and mathematics,
// hard to explain with just a comment. Read my book :D
// Step 3: Calculate normal based on the edges (interpolate
// from 3 cross products we build from our edges).
"

Sergey
  #2 (permalink)  
Old May 12th, 2007, 12:01 AM
Wrox Author
 
Join Date: May 2007
Location: Hannover, , Germany.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to abi.exdream.com
Default

Not sure whats on page 346 because I do not have the book myself. But I'm trying to help.

If it is about loading x models, the normals are already present in the x file, only tangents are recalculated in my TangentVertexProcessor. It is however possible to generate normals if they are not present with the ComputeNormals method of the ModelMesh class there (same way as ComputeTangents is used).

Later in the book normals are calculated for own geometry like the road, the landscape, etc. In most cases, especially in unit tests, the normals are pre-computed and often just point straight up (e.g. for a ground plane). In case of the landscape normal computation, which is probably the most advanced and complex, it works by taking several surrounding landscape points and calculating the vectors from the current landscape point to them and then taking the cross product of them and interpolating several cross products together.

That code and more comments can be found in the Landscape.cs file (line 802-830):

    // Step 1: Calculate position
    int index = x + y * GridWidth;
    Vector3 pos = CalcLandscapePos(x, y, heights);//texData);
    mapHeights[x, y] = pos.Z;
    vertices[index].pos = pos;

    // Step 2: Calculate all edge vectors (for normals and tangents)
    // This involves quite complicated optimizations and mathematics,
    // hard to explain with just a comment. Read my book :D
    Vector3 edge1 = pos - CalcLandscapePos(x, y + 1, heights);//texData);
    Vector3 edge2 = pos - CalcLandscapePos(x + 1, y, heights);//texData);
    Vector3 edge3 = pos - CalcLandscapePos(x - 1, y + 1, heights);//texData);
    Vector3 edge4 = pos - CalcLandscapePos(x + 1, y + 1, heights);//texData);
    Vector3 edge5 = pos - CalcLandscapePos(x - 1, y - 1, heights);//texData);

    // Step 3: Calculate normal based on the edges (interpolate
    // from 3 cross products we build from our edges).
    vertices[index].normal = Vector3.Normalize(
        Vector3.Cross(edge2, edge1) +
        Vector3.Cross(edge4, edge3) +
        Vector3.Cross(edge3, edge5));

More code is also in the Landscape constructor, which smoothes the normals again to make the lighting appear more settle throughout the landscape.

Hope this helps.

http://abi.exdream.com
  #3 (permalink)  
Old May 12th, 2007, 02:28 AM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!!!







Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.