Normal Problem - Screenshot available

designdb
Unregistered
 
Post: #1
Okay, here's what I've got. I'm working on a 3D terrain generation system, and I just started adding color (as a short-stop towards textures). I'm not ready for textures yet, and I'm using it as a learning project.

One half of my triangles are indicating (as far as I can tell from the color) that they are upside down, or something. Everything is supposed to display as green, but every other triangle is showing up as only very, very light green (not black). I think my normals are being generated properly (it certainly worked for the other triangles).

My normal generation process works by generating 2 lines, one off of point 1 to point 2, and the other off of point 3 to point 2. Then, I find the normal, and "normalize" it to a length of 1.

If code would help, let me know. I can provide, but it's not entirely beautiful as I have yet to encapsulate it properly.

Thanks,
Dave Ramsey
Quote this message in a reply
kberg
Unregistered
 
Post: #2
code and/or a picture of the problem would most likely help...

If you have GL_CULL_FACE enabled and your normals point away from the camera then you should see nothing at all in that spot; an empty hole, not a lightened polygon.
Quote this message in a reply
designdb
Unregistered
 
Post: #3
Here's a screenshot:
[Image: screenshot.jpg?BCpEDx.ASAfcQ5yr]

and here is the code for generating normals:
[SOURCECODE]-(void)buildNormal:(OglTri *)theTriangle
{
//Used for generating Normals
//Needs to be broken back out into it's own method... please
//We will need to be able to adjust the normals on the fly because
// of the terrain deformation and scaling
float x1Component, y1Component, z1Component;
float x2Component, y2Component, z2Component;
float xNormal, yNormal, zNormal;
float normalLength;

//Generate Normal
//p1 to p2
x1Component = (*theTriangle).p1.x-(*theTriangle).p2.x;
y1Component = (*theTriangle).p1.y-(*theTriangle).p2.y;
z1Component = (*theTriangle).p1.z-(*theTriangle).p2.z;

//p3 to p2
x2Component = (*theTriangle).p3.x-(*theTriangle).p2.x;
y2Component = (*theTriangle).p3.y-(*theTriangle).p2.y;
z2Component = (*theTriangle).p3.z-(*theTriangle).p2.z;

//Generate Components of normal vector
xNormal = (y1Component * z2Component) - (y2Component-z1Component);
yNormal = (x1Component * z2Component) - (x2Component-z1Component);
zNormal = (y1Component * x2Component) - (y2Component-x1Component);

//Generate length of normal vector
normalLength = sqrt(xNormal*xNormal + yNormal* yNormal + zNormal*zNormal);

//"Normalize" Normal vector
xNormal = xNormal / normalLength;
yNormal = yNormal / normalLength;
zNormal = zNormal / normalLength;

//Store normal in passed triangle
(*theTriangle).normal.x = xNormal;
(*theTriangle).normal.y = yNormal;
(*theTriangle).normal.z = zNormal;

}
[/SOURCECODE]

If that looks good, and ou think the problem is elsewhere, let me know, and I will post some more code.

Thanks,
Dave Ramsey
Quote this message in a reply
designdb
Unregistered
 
Post: #4
Wow,

I'm screwed Wacko

Aww, I'll figure it our eventually. For reference, it does exist with textures as well. it doesn't have to do (directly) with the coloring code.

Thanks,
Dave Ramsey
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #5
Quote:Originally posted by designdb
Wow,

I'm screwed :wacko:

Aww, I'll figure it our eventually. For reference, it does exist with textures as well. it doesn't have to do (directly) with the coloring code.

Thanks,
Dave Ramsey


well - your screenshot link doesnt work for me - so I cant see what you are talking about. Also dont use separate variables for x1Component, y1Component, z1Component - make proper point and vector structs (or objects) and write functions (or methods) for crossproduct, dotproduct, scalar product, adding, subtracting, etc... You are making your job here so much more difficult and tedious here. No excuses - dont say you were just writing this until you got it working and then you will add fuller structures/objects - that road leads to code you never get around to updating and bugs you never get around to squashing.

That said...

I think your crossproduct formula is incorrect.

[SOURCECODE]//Generate Components of normal vector
xNormal = (y1Component * z2Component) - (y2Component-z1Component);
yNormal = (x1Component * z2Component) - (x2Component-z1Component);
zNormal = (y1Component * x2Component) - (y2Component-x1Component);[/SOURCECODE]

probably should be something like:

[SOURCECODE]//Generate Components of normal vector
xNormal = (y1Component * z2Component) - (z1Component * y2Component);
yNormal = (z1Component * x2Component) - (x1Component * z2Component);
zNormal = (x1Component * y2Component) - (y1Component * x2Component);[/SOURCECODE]

but really this code should be part of your triangle's class (I dont know where you have it now). You should send a message to the triangle to build its normal. This is off the top of my head - but in C++ something like:

(point1, point2, point3, and normal are member variables of your Triangle3 class)
[SOURCECODE]void Triangle3::BuildNormal()
{
Vector3 vector1 = point1 - point2;
Vector3 vector2 = point3 - point2;
normal = (vector1.cross(vector2)).unitize();
}[/SOURCECODE]

Of course if this is for terrain you may want to make normals a part of a Vertex3 class. Then you can use smooth shading instead of faceted.

hth,
Codemattic
Quote this message in a reply
designdb
Unregistered
 
Post: #6
Quote:Originally posted by codemattic
well - your screenshot link doesnt work for me - so I cant see what you are talking about.


I hate Yahoo. I'll have to find another website for documenting my development process. However, your later advice pegged it perfectly.

Quote: No excuses - dont say you were just writing this until you got it working and then you will add fuller structures/objects - that road leads to code you never get around to updating and bugs you never get around to squashing.


Yes, sir, Mr. Codematic, Sir. Point well taken. No excuses (though I will say that I am a hopeless newbie, and am working on it). I'll be real quiet about the part where I'd just finished refactoring just to get to that point. As a matter of fact, you responded a couple of days ago to my thread on NSDocument object references and how to know when your objects are the right size.

You know, a limited code review feature might be very nice for this site. I know you could do it just by tossing the code up there and asking people what they thought, but a particular Zone just for code review, stipulations that the code must be functional (this zone is not for debugging, this zone is for questions of style and approach).

Anyway, just an idea. In a day or two, I'll post a new code sample and you can slam it again until I get it the right size... :-)

Quote:

That said...

I think your crossproduct formula is incorrect.


Yep. Stupid typo. Thanks.

And now my terrain looks beautiful (or, at least, as beautiful as it can look with Polygonal Normals)

Quote:
but really this code should be part of your triangle's class (I dont know where you have it now). You should send a message to the triangle to build its normal.

[snip]

Of course if this is for terrain you may want to make normals a part of a Vertex3 class. Then you can use smooth shading instead of faceted.


Time to go sit in the coffee house and re-factor. Thanks for your help, it is greatly appreciated.
Quote this message in a reply
Tycho
Unregistered
 
Post: #7
I'm not sure using objects for individual vertices and polygons is such a great idea in Objective-C. Just using regular C structs and functions for this would probably make just as much sense, and be faster.
Quote this message in a reply
designdb
Unregistered
 
Post: #8
Oh, I know. My first attempt used objects down to the vertex level, and it was a horrible pain in the butt. This was my second attempt, and while easier, I cetainly did not factor small enough. I''m thinking perhaps objects down to the polygon level, and then structs below. And that still doesn't mean that I can't generalize my methods/functions better.

Or be appropriately specific. I'm currently using a "vertex" for storing color (well, it *is* 3 GLfloats... Rolleyes ) Yeah, I know, I only mentioned it because it is bad.

Dave Ramsey

Or should the objects be down to the level of list of polys, and have the polys on down be structs...

What is the performance loss for creating Obj-C objects vs. comparable structs? And for accessing?
Quote this message in a reply
designdb
Unregistered
 
Post: #9
Here, try this link for a screenshot:
[Image: BadNormal.jpg]

Note: This problem has been solved.
[Image: GoodNormal.jpg]

Much work left to do, but I feel that I'm on the right track

James Ramsey

Yay! Links work Grin
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Normal Mapping Precision on iOS OptimisticMonkey 6 17,221 Apr 13, 2011 11:35 PM
Last Post: OptimisticMonkey
  Getting the Normal for a polygon. Jaden 3 5,769 May 1, 2009 01:47 PM
Last Post: Nosredna
  Vector (Normal) Map blending operations? kelvin 10 6,692 Mar 16, 2007 04:31 PM
Last Post: OneSadCookie
  drawing or displaying vertex normal shru_ani 3 3,550 Oct 29, 2006 06:04 AM
Last Post: shru_ani
  Gouraud vs Normal maps for dinamic terrains sohta 6 4,043 Oct 11, 2006 12:55 AM
Last Post: sohta