Using light in my shader

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
CheeryLee
Posts: 7
Joined: Wed Jan 06, 2016 10:31 am

Using light in my shader

Post by CheeryLee »

Hello everyone!
I wrote the simple diffuse shader and attached it in my Irrlicht project.

Vertex Shader:

Code: Select all

#version 120
varying vec3 Normal;
varying vec2 TexCoord;
 
void main(void)
{
  gl_Position = ftransform();
  Normal = gl_Normal;
  TexCoord = gl_MultiTexCoord0.xy;
}
Fragment Shader:

Code: Select all

#version 120
uniform vec3 vec_light = vec3 (0.0, 1.0, 1.0);
varying vec3 Normal;
varying vec2 TexCoord;
 
uniform vec4 ambient_color = vec4 (0.1, 0.9, 0.5, 0.0);
uniform float ambient_intensity = 1.0;
 
uniform sampler2D Texture;
 
void main(void)
{  
  gl_FragColor = texture2D(Texture, TexCoord);
  gl_FragColor *= ambient_intensity * dot(Normal, vec_light);
}
And project code:

Code: Select all

IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();
s32 newMaterialType = 0;
newMaterialType = gpu->addHighLevelShaderMaterialFromFiles (vert,frag, "");
map_node->setMaterialType((E_MATERIAL_TYPE)newMaterialType);
map_node->setMaterialFlag(EMF_LIGHTING, true);
But this shader doesn't use Irrlicht light node and therefore doesn't interact with it.
How should I change the shader and project to use light in my scene?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Using light in my shader

Post by CuteAlien »

Did you check-out the shader example? It explains how to pass on parameters from nodes.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Using light in my shader

Post by mongoose7 »

According to the GLSL spec (1.20), you can access gl_LightSource[] for the lights that have been passed to OpenGL. This should include any lights configured in Irrlicht.
CheeryLee
Posts: 7
Joined: Wed Jan 06, 2016 10:31 am

Re: Using light in my shader

Post by CheeryLee »

CuteAlien wrote:Did you check-out the shader example? It explains how to pass on parameters from nodes.
Yes, i did. But... didn't understand. I realized that setPixelShaderConstant method can set vec_light constant in my shader. Could you explain what attributes I should use in this function?
CheeryLee
Posts: 7
Joined: Wed Jan 06, 2016 10:31 am

Re: Using light in my shader

Post by CheeryLee »

The problem has been solved. I read the lesson 10 again and understood what arguments I should use in function.
Thanks for reply.
Post Reply