0
101 Oct 08, 2004 at 12:58

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.

#### 5 Replies

0
101 Oct 08, 2004 at 13:29

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 =)

0
101 Oct 08, 2004 at 16:36

@NeZbiE

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 =)

[snapback]12526[/snapback]

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

0
101 Oct 08, 2004 at 18:23

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.

0
101 Oct 09, 2004 at 07:37

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

0
101 Oct 09, 2004 at 15:55

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