-1
\$\begingroup\$

How do I add light to my textures using this? I have this

GLfloat ka = 1.0;
glm::vec3 ia = glm::vec3(0.5f, 0.5f, 0.5f);

and in the fragment shader

out vec4 color;

in VS_OUT
{
    vec2 tc;
    vec3 normals;
} 

fs_in;


layout(binding=0)uniform sampler2D tex; 


void main(void)
{
color= texture(tex, fs_in.tc);
}

We were given this example but I don't understand what to do.

out vec4 color;
uniform vec3 materialColor;
uniform vec3 ia; // Ambient colour
uniform float ka; // Ambient constant
void main(void){
color = vec4( ka * ia , 1.0);
}

Help?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Did you try multiplying your illumination vec4 by your texture vec4 to get the combined colour of the two together? This is covered quite extensively in beginner shader tutorials, so it's not clear to me where exactly you're stuck. \$\endgroup\$
    – DMGregory
    Nov 24 '21 at 13:10
  • \$\begingroup\$ No I did not find how to do it, I don't know the syntax or code. Could you type me the code syntax? \$\endgroup\$ Nov 24 '21 at 13:25
  • 1
    \$\begingroup\$ So then you searched "how to multiply in glsl"? (BTW: the multiplication syntax is right there in your third code snippet) \$\endgroup\$
    – DMGregory
    Nov 24 '21 at 13:26
  • \$\begingroup\$ I couldn't find anything, it gives me an error when I multiply it. \$\endgroup\$ Nov 24 '21 at 13:39
0
\$\begingroup\$

Just multiply the two expressions you already have:

color= texture(tex, fs_in.tc) * vec4( ka * ia , 1.0);

This tints the colour of the texture by the colour and intensity of the ambient light.

This is shown in nearly every example shader and definitely in every single "how to get started with glsl shaders" tutorial on the net, so it does not look like you researched this at all. Please take the time to do a bit more legwork and reading before posting a question here in future.

\$\endgroup\$
6
  • \$\begingroup\$ Thanks I'l try it \$\endgroup\$ Nov 24 '21 at 13:31
  • \$\begingroup\$ It did not work, I'm a beginner. \$\endgroup\$ Nov 24 '21 at 13:36
  • \$\begingroup\$ @user17439891 "it did not work" is never a good way to describe a problem; what happens? what do you get instead of the result you want? \$\endgroup\$
    – Vaillancourt
    Nov 24 '21 at 14:36
  • \$\begingroup\$ @Vaillancourt ibb.co/PYCLk6X this is what I get in the output, ignore the first two texture errors \$\endgroup\$ Nov 24 '21 at 14:54
  • \$\begingroup\$ @Vaillancourt I've added this to my fragment shader - uniform vec3 ia; uniform float ka; and the error goes away but now all my textures are displayed black \$\endgroup\$ Nov 24 '21 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .