Advertisement

glsl - material, texture and light relationship

Started by March 24, 2018 04:51 AM
4 comments, last by too_many_stars 6 years, 10 months ago

Hello Everyone,

I am having a difficult time coming up with a relationship between light, material, and a texture inside a shader.

I have a model that has the following properties.

1.) texture

2.) material

and a world light source.

If the light is on, inside the fragment shader, I do the following

vec3 light_frag = phongADS( material );

gl_FragColor = vec4( light_frag , 1 ) * texture( sampler , uv );

This simply calculates the light fragment based on the material with an ambient, diffuse and specular component and the multiplies this by the texture fragment

 

However, when the light is off, and I am only dealing with a material and a texture

what is the relationship between the two?

Does one just take for example the diffuse component of the material like so...

gl_FragColor = vec4( material.diffuse , 1 ) * texture( sampler , uv );

 

More generally, my question is this:

Given a model ( say an AABB ) with a material, a texture and a light source, what is the proper per fragment calculation ( if light is on or off ) that takes all three into account

 

Thanks,

 

Mike

I would say, no lights, therefore no materials. So simply use your texture.

But if you really want to use a material, then yes, the diffuse one is the one which corresponds the most to what you want to do.

However, all of this depends on your materials. And textures are materials. The "default" one is normally the diffuse texture which has to be mixed with the object material, if any.

So if your materials (and therefore textures) are made for a lit environment, you might end with something not that beautiful or at least not as expected.

Advertisement

Thanks for the response Silence@Sid,

The reason I want material with a texture is if I simply want to change the texture color. I guess I could introduce another RGB variable just to change the texture color but that seems like a waste of memory.

Mike

On 24/03/2018 at 3:56 PM, too_many_stars said:

Thanks for the response Silence@Sid,

The reason I want material with a texture is if I simply want to change the texture color. I guess I could introduce another RGB variable just to change the texture color but that seems like a waste of memory.

Mike

In that case you can simply send a uniform. This is small and will be fast enough too. The only issue with this is that you'll have the same color for all your mesh or model.

Another solution would be to use a single short integer attribute for the color, and choose the right color by doing a texture lookup from a 1D texture within the shader.

Hope that could help.

Thanks for the response Silence, I do like the lookup idea. It's going on the to do list. 

Mike

This topic is closed to new replies.

Advertisement