• Create Account

## Normals question

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1EarthBanana  GDNet+

1765
Like
0Likes
Like

Posted 14 August 2014 - 11:40 AM

So I have had a strange happening that I can't figure out - its been a day and a half of me trying and so I though I would make a post to see if anyone had any insights.

Our team's artist has made several models in blender - lets just say a tile, a dwarf, a robot, and a bridge. When these items are rendered, the tile has light on the correct side and the other objects do not - they are lit in the back. My first reaction was to flip the normals on the objects and see if the light rendered on the correct side - it does. But - when I draw the normals they appear to be correct.. not sure whats going on. Here is an example - the first bridge is the normals before flipping, and the second is the normals after flipping. The light appears on the correct side after flipping.

(before flipping)

(after flipping)

So if the normals are simply incorrect - thats not a big deal. But the thing is that when I flip the normals by multiplying by negative 1 the result is not exactly correct either. I think that's because the normals need to be reflected over the model's vertical axis rather than flipped. If I do that then the specular lighting is correct. That is, if the normal is pointing from the model to the upper left like \, where the comma is the model, then the new orientation needs to be ,/ rather than '\ if that makes any sense at all.

I don't understand why blender would be exporting models with normals like this which makes me think it has to be a mistake in my code somewhere.

Could it have to do with the coordinate system of the normals? I use assimp for importing and generate normals when they aren't available, but all these models have normals available.

Any insights would be helpful, thanks

### #2Buckeye  GDNet+

10739
Like
3Likes
Like

Posted 14 August 2014 - 12:32 PM

What format are you using for Blender export? Do you create and export all the objects in the same manner? Are you flipping the normals in Blender, or in your program?

From the pictures, it appears the normals are as you want them. If so, then either Blender is exporting them correctly, or you're rendering them incorrectly, or the problem is elsewhere. Are the triangle vertices in the correct winding order? That is, are the normals correct but the triangle is considered a back-face?

Do you set the correct light direction in your shader? If you use a dot product between the normal and the light direction, do you remember to "flip" the light direction? That is, an object should be lit when the normal and light direction are opposing (i.e., the normal points in the direction of the light source).

Have you tried to debug examining actual values which are imported, stuffed into the vertex buffer, sent to the shader and used in the shader? If not, why not?

Edited by Buckeye, 14 August 2014 - 12:37 PM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #3EarthBanana  GDNet+

1765
Like
0Likes
Like

Posted 14 August 2014 - 02:00 PM

What format are you using for Blender export? Do you create and export all the objects in the same manner? Are you flipping the normals in Blender, or in your program?

I use dae from blender - and I was flipping the normals within the engine. That is - I just went through all normals and multiplied by negative 1. There could be some difference in the export formatting from blender - but I don't think so since dae doesn't provide a huge range of exporting options. The artist said they made the bridge in the same manner they made the tiles.

Do you set the correct light direction in your shader? If you use a dot product between the normal and the light direction, do you remember to "flip" the light direction? That is, an object should be lit when the normal and light direction are opposing (i.e., the normal points in the direction of the light source).

Yes - I did flip the direction in the shader when dotting the normal and the direction - the shader code is below.

vec4 getLightInfluence(vec4 lWorldPos, vec3 norm, Material mat)
{
vec3 worldPos = lWorldPos.xyz;

vec4 ambientColor = vec4(light.color, 1.0f) * light.ambientIntensity;
float diffuseFactor = dot(norm,-light.direction);

vec4 diffuseColor  = vec4(0.0, 0.0, 0.0, 0.0);
vec4 specularColor = vec4(0.0, 0.0, 0.0, 0.0);

if (diffuseFactor > 0)
{
diffuseColor = vec4(light.color, 1.0f) * light.diffuseIntensity * diffuseFactor;
vec3 vertexToEye = normalize(camWorldPos - worldPos);
vec3 lightReflect = normalize(reflect(light.direction,norm));
float specularFactor = dot(vertexToEye, lightReflect);
specularFactor = pow(specularFactor, mat.specPower);
if (specularFactor > 0)
specularColor = vec4(mat.specColor, 1.0f) * mat.specIntensity * specularFactor;
}

return ambientColor + shadowFactor*(specularColor + diffuseColor);
}


This is a deferred renderer, and this is the direction light shader which gets all positions, material properties, normals, etc from textures. The direction is passed through a uniform and I have made sure that this direction what it should be.

Have you tried to debug examining actual values which are imported, stuffed into the vertex buffer, sent to the shader and used in the shader? If not, why not?

Well - I did in the sense that the normals drawn in the picture are the imported normals. I just created another VBO with the positions and the positions + normals and drew in GL_LINES mode - the normals look okay so I'm not sure.

Update:

I think I may have found the cause of the problem.. It looks like the artist created a normal map texture for the grass tile and not for any of the other objects.. Let me investigate a bit further but I think the problem is that I put a check for the normal texture in the shader in the wrong place, so that no matter what it is multiplying the normal retrieved from the texture by the mat3(tangent, bitangent, normal) matrix..

What I mean is that the vec3 retrieved from the normal texture is initialized to 1,1,1, and if there was no normal texture I just left it as 1,1,1 however it should just send the normal along with no alterations if there is no normal texture I think

### #4EarthBanana  GDNet+

1765
Like
3Likes
Like

Posted 14 August 2014 - 03:01 PM

So I solved the problem - thanks to a closer look at the shader which was inspired by the above post by Buckeye.

As mentioned at the end of the last post - the problem was within the normal map reading.. but it wasn't exactly what I thought. In the shader I have a couple bools (hasDiffuseMap, hasNormalMap, hasOpacityMap, etc.. ) that are set on a per material basis..

Well in the rendering code I was setting them to true for materials that had it, but not false for materials that didnt.. Since the same GBuffer shader is used for pretty much all materials these bools remained true even when there wasn't a map available.. this resulted in invalid texture reads for the objects that didnt have normal maps

I fixed it by setting the hasMap booleans to true or false for every single material

This fixed a lot of other strange lighting artifacts I was having with specular also

Edited by EarthBanana, 14 August 2014 - 03:02 PM.

1151
Like
0Likes
Like

Posted 15 August 2014 - 02:16 AM

Thanks for sharing the solution. Might I ask about the number of normals in your screenshots? It looks like you've got so many on your bridge supports that you can't actually see them. And the 'road' of the bridge, is that lots of flat polygons?

### #6EarthBanana  GDNet+

1765
Like
0Likes
Like

Posted 15 August 2014 - 11:44 AM

Yeah there really is a lot of normals on the supports - but this model was unrefined and still has the original number of verts. I asked the artist to leave it high vert for now for testing purposes - I want to make sure all the vertex buffers and all my shaders can handle high vert objects. This bridge has 71,000 indices in to 60,000 verts/normals/tangents. Most of which reside in the chain supports.

We are making a hex tile map builder/editor which will can create plugins to go in to our game build and battle, which we are also working on. But anyways - the top of the bridge is fit to hexagons - as the picture below shows