PerPixelLighting looks like flat shading

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
porcus
Posts: 149
Joined: Sun May 27, 2007 6:24 pm
Location: Germany

PerPixelLighting looks like flat shading

Post by porcus »

Hi,

I tried to use PerPixelLighting with the following shader (GLSL):

Vertex:

Code: Select all

const stringc vertToonShader =
"varying vec3 normal;"
"void main()"
"{"
"	normal = gl_Normal;"//gl_NormalMatrix * 
"	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;"
"	gl_Position = ftransform();"
"}";
Fragment:

Code: Select all

const stringc fragToonShader =
"uniform sampler2D tex;"
"uniform float texintensity;"
"uniform float fcolorr;"
"uniform float fcolorg;"
"uniform float fcolorb;"
"uniform float lightposx;"
"uniform float lightposy;"
"uniform float lightposz;"
"varying vec3 normal;"
"void main()"
"{"
"	float intensity;"
"	vec4 color;"
"	vec3 n = normalize(-normal);"
""
"	intensity = dot(vec3(lightposx,lightposy,lightposz),n);"
""
"	gl_FragColor = vec4(intensity*fcolorr,intensity*fcolorg,intensity*fcolorb,1.0) + texture2D(tex,gl_TexCoord[0].st) * texintensity;"
"}";
Callback:

Code: Select all

class Shader_Toon_callback: public video::IShaderConstantSetCallBack
{
public:
float texintensity;
float fcolorr;
float fcolorg;
float fcolorb;
float lightposx;
float lightposy;
float lightposz;
   
  virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
  {

    services->setPixelShaderConstant("texintensity", (float*)(&texintensity), 1);
	services->setPixelShaderConstant("fcolorr", (float*)(&fcolorr), 1);
	services->setPixelShaderConstant("fcolorg", (float*)(&fcolorg), 1);
	services->setPixelShaderConstant("fcolorb", (float*)(&fcolorb), 1);
	services->setPixelShaderConstant("lightposx", (float*)(&lightposx), 1);
	services->setPixelShaderConstant("lightposy", (float*)(&lightposy), 1);
	services->setPixelShaderConstant("lightposz", (float*)(&lightposz), 1);


   }
};

[...]

Shader_Toon_callback *callback= new Shader_Toon_callback;

		callback->texintensity=0;
		callback->fcolorr=1;
		callback->fcolorg=1;
		callback->fcolorb=1;
		callback->lightposx=10;
		callback->lightposy=10;
		callback->lightposz=10;

		mtlToonShader = gpu->addHighLevelShaderMaterial(
					vertToonShader.c_str(), "main", video::EVST_VS_1_1,
					fragToonShader.c_str(), "main", video::EPST_PS_1_1,
					callback, video::EMT_SOLID);
		node->setMaterialType((video::E_MATERIAL_TYPE)mtlToonShader);
That's the result:
Image


I wasted already hours of time and I have finally no idea why it looks like flat shading.
I also tried TGM's NormalMapping shader and I get the same artifacts, so I guess it's maybe not
a problem with my code.

I hope someone can help.

greetings,
porcus
Viz_Fuerte
Posts: 91
Joined: Sun Oct 19, 2008 5:29 pm
Location: Valencia (Spain)
Contact:

Post by Viz_Fuerte »

Ummm The code is well.
This problem is due to the extension of the file you used.

This using .3DS .LWO or similar?
These file types have trouble smoothing the vertices, for that reason, they see flat faces.

If you can export to a format like .B3D and really like it looks good (I think)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Could also be the tangent creation of the mesh, as we have had bugs in previous versions of Irrlicht (1.5.1 is the first to have it fixed).
porcus
Posts: 149
Joined: Sun May 27, 2007 6:24 pm
Location: Germany

Post by porcus »

Thanks for the replies!

I updated now from 1.5 to 1.6 and first of all it seemed very good.
But when I used a mesh with tangents (calculated with the mesh manipulator)
the same artifacts are visible like in 1.5 . So PerPixelLighting without
a tangentmesh looks very good but with tangentmesh it looks like the
screenshot above.

Any chance to fix this ?
Viz_Fuerte
Posts: 91
Joined: Sun Oct 19, 2008 5:29 pm
Location: Valencia (Spain)
Contact:

Post by Viz_Fuerte »

This using .3DS .LWO or similar?
These file types have trouble smoothing the vertices, for that reason, they see flat faces.
OMG :lol:

I had the same problem as you, and was solved by changing the file format.
Your code works perfectly.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

porcus wrote:Any chance to fix this ?
Well: 1.5.2 and 1.6.1 have it fixed, of course also 1.7
Post Reply