# Problems calculating true normals

5 replies to this topic

### #1MTB

New Member

• Members
• 3 posts

Posted 08 October 2004 - 12:58 PM

I've made this function to calculate true normals:

Bvertex Bmap::RetNormal(int w, int h) {

Bvertex n1, n2, n3, n, face1;
if (w < 0) w = 0; if (h < 0) h = 0;

n1 = CreateVertex(w, h);
n2 = CreateVertex(w - step, h);
n3 = CreateVertex(w - step, h + step);
n = bFunctions::Normal(n1, n2, n3);

n1 = CreateVertex(w, h);
n2 = CreateVertex(w - step, h + step);
n3 = CreateVertex(w, h + step);
face1 = bFunctions::Normal(n1, n2, n3);

n1 = CreateVertex(w, h);
n2 = CreateVertex(w, h + step);
n3 = CreateVertex(w + step, h + step);
face1 = bFunctions::Normal(n1, n2, n3);

n1 = CreateVertex(w, h);
n2 = CreateVertex(w + step, h + step);
n3 = CreateVertex(w + step, h);
face1 = bFunctions::Normal(n1, n2, n3);

n1 = CreateVertex(w, h);
n2 = CreateVertex(w + step, h);
n3 = CreateVertex(w + step, h - step);
face1 = bFunctions::Normal(n1, n2, n3);

n1 = CreateVertex(w, h);
n2 = CreateVertex(w + step, h - step);
n3 = CreateVertex(w, h - step);
face1 = bFunctions::Normal(n1, n2, n3);

n1 = CreateVertex(w, h);
n2 = CreateVertex(w, h - step);
n3 = CreateVertex(w - step, h + step);
face1 = bFunctions::Normal(n1, n2, n3);

n1 = CreateVertex(w, h);
n2 = CreateVertex(w - step, h - step);
n3 = CreateVertex(w - step, h);
face1 = bFunctions::Normal(n1, n2, n3);

n = bFunctions::Normalize(n);
return n;
}


I'm sorry for the less comments.. :)

But it doesn't work, i get a kind of weird lighting. I call it in this way:
LoadNormalCoord(w, h, &faces[0], &faces[1]);	// Load the right vertexes into the memory
/*--- Render first triangle ---*/
face = faces[0];      // Put in face the first face

glNormal(RetNormal(w, h));
glVertex(face.v1);      // Render top-left part

glNormal(RetNormal(w+1, h));
glVertex(face.v2);      // Render top-right part

glNormal(RetNormal(w+1, h+1));
glVertex(face.v3);      // Render down-right part

/*--- Render second triangle ---*/

face = faces[1];      // Put in face the second face

glNormal(RetNormal(w, h));
glVertex(face.v1);      // Render top-left part again

glNormal(RetNormal(w+1, h+1));
glVertex(face.v2);      // Render down-right part again

glNormal(RetNormal(w, h+1));
glVertex(face.v3);      // Render down-left part



What am i doing wrong? The calculation of normals doesn't contain any errors (it works with flat normals). BTW I'm working in openGL.

### #2NeZbiE

Member

• Members
• 61 posts

Posted 08 October 2004 - 01:29 PM

I assume you are talking about vertex normals, considering your actual rendering code.

For flat shading, simple triangle normals suffice. When it comes to true smooth shading however, instead of passing on the normal of a polygon "face" to glNormal, you need to pass on the "average" of the normals of all polygon "faces" sharing that common vertex. Hope this helps and is clear enough =)

Anyways, I'm off to bed, it's already 10:30 Am here =)

### #3MTB

New Member

• Members
• 3 posts

Posted 08 October 2004 - 04:36 PM

NeZbiE said:

I assume you are talking about vertex normals, considering your actual rendering code.

For flat shading, simple triangle normals suffice. When it comes to true smooth shading however, instead of passing on the normal of a polygon "face" to glNormal, you need to pass on the "average" of the normals of all polygon "faces" sharing that common vertex. Hope this helps and is clear enough =)

Anyways, I'm off to bed, it's already 10:30 Am here =)

I knew that already, and that's what my code should have to do :), but the problem is that it doesn't work :wacko: .

Senior Member

• Members
• 785 posts

Posted 08 October 2004 - 06:23 PM

perhaps your AddVertex code is wrong, or your calculation for which w h values go with the next two verticies are wrong. It is difficult to tell from here.
Jesse Coyle

### #5MTB

New Member

• Members
• 3 posts

Posted 09 October 2004 - 07:37 AM

This is my code to add a vertex:
void AddVertex(Bvertex* v1, Bvertex v2) {
v1->x += v2.x;
v1->y += v2.y;
v1->z += v2.z;
}

and calculation of w and h with the next two vertices:
w, h    w + step, h
|________|
|\       |
|  \     |
|    \   |
|      \ |
|_______\|_____w + step, h + step
|
w, h + step


Senior Member

• Members
• 785 posts

Posted 09 October 2004 - 03:55 PM

Can you show us a screenshot of this weird lighting? Upload it to ImageShack.us and you can post it then.
Jesse Coyle

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users