简体   繁体   中英

GLSL texture mapping and coloring in the same shader

I want to write a GLSL Fragment Shader that can do texture mapping and vertex coloring. Is it possible to do both in the one shader?

At the moment, I can do this:

gl_FragColor = texture2D(tex, gl_TexCoord[0].st);

Which causes textured verts to be drawn, but not colored verts. (which looks like this: http://www.tiikoni.com/tis/view/?id=124eb69 )

I can also do this:

gl_FragColor = gl_Color;

Which causes colored verts to be drawn but not textured verts. (Which looks like this: http://www.tiikoni.com/tis/view/?id=5bcd838 )

If I do this:

gl_FragColor = texture2D(tex, gl_TexCoord[0].st) * gl_Color; (which looks like the first code snippet)

Only textured verts get drawn... which is my problem.

What I expect to see is the textured and non-textured verts to all show up, in the last code snippet.

You shouldn't expect that.

There's no such thing as a "textured vert" or a "non-textured vert". There is only a color , whatever that color may be. That's the whole point of shaders: to use arbitrary code to compute a color however you want.

You are computing the color by multiplying the result of a texture fetch with a per-vertex interpolated color. The result will be a combination of the two colors. Namely, the product of them. Therefore, the resultant color will look somewhat like the original texture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM