Sign in to follow this  
Shamot

GLSL textures and material

Recommended Posts

Hi, I am facing this problem: I have a scene where some objects are textured and some have no texture, only material. When I am rendering this scene with GLSL shaders, I am modulating the final fragment color in the fragment shader with texture color: void main (void) { vec4 color, texel; color = gl_Color; texel = texture2D(texUnit0, gl_TexCoord[0].xy); color *= texel; gl_FragColor = color; } But this gives to non-textured objects some portion of texture color and they are darker :( Is it possible to render textured objects with texture and material and non-textured objects only with material color (just like in a fixed func. rendering)? Thanks a lot.

Share this post


Link to post
Share on other sites
Of course it is, just for example make two shaders :

Textured :


void main (void)
{
vec4 color, texel;
color = gl_Color;
texel = texture2D(texUnit0, gl_TexCoord[0].xy);
color *= texel;
gl_FragColor = color;
}


Not-Textured :


void main (void)
{
vec4 color;
color = gl_Color;
gl_FragColor = color;
}


So, just use different shaders to render different objects.

Share this post


Link to post
Share on other sites
Thaks, I know, that's a solution, but not useful for me. I am using a kind of scene graph - Open Inventor, so the best for me would be to find out in a vertex/fragment shader if a fragment should be textured or not, but how ?

Share this post


Link to post
Share on other sites
Why don't you add one variable... let's say bool UseTexture, and if you set it to true, fragment shader should use textures, if you set it to false it should not use textures.

Share this post


Link to post
Share on other sites
This is another possible solution, but not suitable for me. My scene is created in Open Inventor (high-level 3D graphis extension to OpenGL) and it is a scene graph. Rendering is done through traversing the scene graph from root to children, when OpenGL state is changing.

Your suggested solution is possible, but I would have to change the code inside Open Inventor (that's possible, but I do not want to).

I need some simple solution inside vertex/fragment shader to say, if an object should be textured or not. But I am getting to be worried, this is not possible :(

Share this post


Link to post
Share on other sites
Can't you just bind a 1x1 pixel white texture for objects with no texture?

I am sure there is a more elegant solution though.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi, thanks to all, the solution is here:

if (texUnit0)
doesn't not work, texUnit0 is a texture unit handler and cannot be used like this :(

But the useful trick with white texture by Zymph works !!! (I have placed a 1x1 white texture at the top of the scene graph and I am using

color = gl_Color;
texel = texture2D(texUnit0, gl_TexCoord[0].xy);
color *= texel;

so any non-textured object has correct color now. Thanks all !

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this