Problem with my normals
Problem with my normals
Problem with my normals
Problem with my normals
Problem with my normals
Problem with my normals Problem with my normals Problem with my normals Problem with my normals Problem with my normals Problem with my normals Problem with my normals Problem with my normals
Problem with my normals Problem with my normals
Problem with my normals
Go Back  Xtreme Visual Basic Talk > > > > Problem with my normals


Reply
 
Thread Tools Display Modes
  #1  
Old 05-13-2006, 04:58 PM
Moohasha's Avatar
Moohasha Moohasha is offline
Centurion
 
Join Date: Nov 2004
Location: Texas
Posts: 165
Default Problem with my normals


I've created a terrain that is a just grid of points (equally spaced in the x and z directions, y determines height), connected by a line list (to avoid blending of light over sharp edges). To calculate the surface normal for each triangle I pass all three vertices (A, B, and C) to a function, form two vectors (vectors AB and AC) take the cross product, and return the unit vector with the same direction as the result. This seems to be working fine in most cases, but I'm getting some undesired results. It's hard to explain, so please view the attached screenshot to see the problem. Every other triangle has a messed up normal in certain directions, but not others. Here is the function I'm using to create a normal, which I then assign to all three vertices of a surface:

Code:
Private Function GetNormal(ByVal A As Vector3, ByVal B As Vector3, ByVal C As Vector3) As Vector3 Dim v1 As New Vector3(B.X - A.X, C.Y - A.Y, C.Z - A.Z) Dim v2 As New Vector3(C.X - A.X, C.Y - A.Y, C.Z - A.Z) Dim result As Vector3 result.X = (v1.Y * v2.Z) - (v1.Z * v2.Y) result.Y = (v1.Z * v2.X) - (v1.X * v2.Z) result.Z = (v1.X * v2.Y) - (v1.Y * v2.X) result.Normalize() Return result End Function
And to answer the obvious question, I believe I am passing the vertices to the function in the correct order, because all the normals are correct when the surface is flat (see screen shot). Any ideas what's causing the problem??
Attached Images
File Type: jpg Terrain.JPG (124.1 KB, 11 views)
__________________
"The whole trouble comes from the fact that there is so much tinkering with software. It is not made in a clean fabrication process, which it should be. What we need, is software engineering." — F.L. Bauer, 1968
Reply With Quote
  #2  
Old 05-14-2006, 12:12 AM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

Half of them seem to be pointing in the wrong direction.

Probably because not all of the triangles vertices are consistently ordered in a clockwise (or counterclockwise) direction.

There are two solutions to a planes normal...
Reply With Quote
  #3  
Old 05-14-2006, 06:17 AM
Iceplug's Avatar
IceplugProblem with my normals Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Note DirectX9 has a function that does the Cross product for you:
result = Vector3.Cross(v1, v2)

The (possibly) easiest solution would be to reorder the vertices when you do your tilemap.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #4  
Old 05-14-2006, 11:41 AM
Moohasha's Avatar
Moohasha Moohasha is offline
Centurion
 
Join Date: Nov 2004
Location: Texas
Posts: 165
Default

I'm pretty sure I ordered all the vertices in the same direction. When I enable culling they either all render or all don't. And besides, it's only half when the "hill" or angle is in a certain direction. None of the surfaces on the right side facing left are lit wrong, only ones facing right.
I've tried passing them to my GetNormals() function in different orders, but that either resulted in no effect (generating the same normal from different vectors), or getting the normal pointing in the opposite direction.
I thought DX9 might have something to calculate the cross product for you. I'll try using that.
__________________
"The whole trouble comes from the fact that there is so much tinkering with software. It is not made in a clean fabrication process, which it should be. What we need, is software engineering." — F.L. Bauer, 1968
Reply With Quote
  #5  
Old 05-14-2006, 11:56 AM
Moohasha's Avatar
Moohasha Moohasha is offline
Centurion
 
Join Date: Nov 2004
Location: Texas
Posts: 165
Default

Once again, it's another PEBKAC error. (Problem Exists Between Keyboard And Chair). If you notice my GetNormal() function, I mixed up B and C when calculating v1. I didn't notice this until I started to replace those two lines with a Vector3.Subctract() function. Fixing that took care of everything. Thanks for the help guys.
__________________
"The whole trouble comes from the fact that there is so much tinkering with software. It is not made in a clean fabrication process, which it should be. What we need, is software engineering." — F.L. Bauer, 1968
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Problem with my normals
Problem with my normals
Problem with my normals Problem with my normals
Problem with my normals
Problem with my normals
Problem with my normals Problem with my normals Problem with my normals Problem with my normals Problem with my normals Problem with my normals Problem with my normals
Problem with my normals
Problem with my normals
 
Problem with my normals
Problem with my normals
 
-->