possible normals problem

Started by
6 comments, last by limesX 6 years ago

Hi guys.

Trying to solve this for a few days now but getting nowhere.

I am having trouble with correctly displaying model when using diffuse and ambient light combination.

In the picture you can see 2 larger cubes, top one using only ambient light, and bottom one using ambient + diffuse combination

Smaller cube is just for showing where light is located.

Can someone help me understand why is cube rendered the way it is? I was thinking problems with normals. I tried several different normal calculations and none of them work.

Or maybe its not problem with the normals? I didnt want to post any code until maybe someone just give me a clue what might be happening?

thanx

pic1.jpg

Advertisement

Without code we can just guess so you should post it.

To debug yourself, visualize the normals as small vectors with wireframe display, so you can quickly see if they are wrong. (Looks they are wrong, but it can be anything, like messing things up when formatting the data for GPU, bug in shader, etc...)

In the bottom cube there appears to be a triangle between the front top left, front bottom right and rear bottom left vertices. Unless you put that in your model deliberately, your vertex indices may be incorrect, and maybe causing the shading issues.

thx for quick info. i try to render everthing in wireframe including normals for each vertex. then I post back with some screenshot. i see that little triangle which apperas to be there, but i think it just "appears" to be there cause of colors. I post back with wireframe and normals.... 

 

Hi. As you can see in the screen shot the normals are the problem. I tried all calculations I could found online and cant get correct results. Maybe can you help. I tried different model formats and different parsers for them. I will post here the .x file format for my model exported from blender.

    Mesh { // Cube mesh
      8;
       1.000000; 1.000000;-1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000;-1.000000;,
       1.000000; 0.999999; 1.000000;,
       0.999999;-1.000001; 1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000; 1.000000; 1.000000;;
      12;
      3;0,3,1;,
      3;4,5,7;,
      3;0,1,4;,
      3;1,2,5;,
      3;3,7,2;,
      3;4,7,0;,
      3;3,2,1;,
      3;5,6,7;,
      3;1,5,4;,
      3;2,6,5;,
      3;7,6,2;,
      3;7,3,0;;
      MeshNormals { // Cube normals
        12;
        -0.000000;-0.000000;-1.000000;,
         0.000000; 0.000000; 1.000000;,
         1.000000; 0.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000;-0.000000;-1.000000;,
         0.000000;-0.000000; 1.000000;,
         1.000000;-0.000001; 0.000000;,
        -0.000000;-1.000000; 0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;;
        12;
        3;0,0,0;,
        3;1,1,1;,
        3;2,2,2;,
        3;3,3,3;,
        3;4,4,4;,
        3;5,5,5;,
        3;6,6,6;,
        3;7,7,7;,
        3;8,8,8;,
        3;9,9,9;,
        3;10,10,10;,
        3;11,11,11;;
      } // End of Cube normals

So, if I got this correct first number 8 defines 8 vertices with x,y,z coordinates. Next number 12 are indexes, like 3;0,3,1;, means 3 vertex, with index 0, 3 and 1 which gives single face(triangle).

Then we have mesh normals, 12 normals with x,y,z. Then normal indexes: 12 indexes means 12 faces (triangles). so  3;0,0,0;, means 3 normals for 1 face.

so now some help with vertex normal calculation: 

here is what i do:

for every vertex

        for every face

             if vertex is in this face

                      vertexnormal += face normal

after that i divide each vertex normal with how many time was repeted and normalize it.

it doesnt work. help pls.....

pic1.jpg

Lawnjelly is right i think, you probably have an indexing problem too, or culling / winding order problem. Be sure to enable backface culling and depth buffer.

Here is an improved normals generation algo:

for each vertex   

   vertexNormal = (0,0,0);

      for each vertex triangle

         vertexNormal += triangleNormal * triangle area // area weighted normals

   vertexNormal.Normalize() // notice there's no division necessary if you normalize

 

But yours should work as well and since it does not, be sure triangle normals work. (visualize them too to be sure you index correctly)

However, for this cube model, centerd at the origin, you could simple use: vertexNormal = vertexPosition.Normalized() for perfect results and reference.

 

 

 

Finally some code to calculate triangle normal and area:

vec v = (triangle[1] - triangle[0] ).Cross( triangle[2] - triangle[0] );
float len = v.Length();
vec triangleNormal = v / len;
float triangleArea = len * 0.5f;

 

 

Finally I think it is working. And suddenly even my old normal calculations work. I think when I was cleaning my code I had some mistake with setting stride size prior rendering but I am not sure anymore.

Here are the screenshots. I think its lighting the cube the way it should be now.

If someone need code for this, I will post it.

Thank you guys for your help......

And the screenshots..... Green vertex normals and red face normals.....

 

pic1.jpg

pic2.jpg

This topic is closed to new replies.

Advertisement