Toon shader with textures
I'm trying to implement a toon shader with texture using C++ on opengl 3+ but after a week i only got a color toon shade without textures.
vertex file:
#version 330
// Incoming per vertex... position and normal
in vec4 vVertex;
in vec3 vNormal;
smooth out float textureCoordinate;
uniform vec3 vLightPosition;
uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;
void main(void)
{
// Get surface normal in eye coordinates
vec3 vEyeNormal = normalMatr开发者_开发问答ix * vNormal;
// Get vertex position in eye coordinates
vec4 vPosition4 = mvMatrix * vVertex;
vec3 vPosition3 = vPosition4.xyz / vPosition4.w;
// Get vector to light source
vec3 vLightDir = normalize(vLightPosition - vPosition3);
// Dot product gives us diffuse intensity
textureCoordinate = max(0.0, dot(vEyeNormal, vLightDir));
// Don't forget to transform the geometry!
gl_Position = mvpMatrix * vVertex;
}
fragment file:
//
#version 330
uniform sampler1D colorTable;
out vec4 vFragColor;
smooth in float textureCoordinate;
void main(void)
{
vFragColor = texture(colorTable, textureCoordinate);
}
can anyone give me a hand to get this shader working with textures ?
thx
You need another vertex shader input which receives the UV coordinates for each vertex. Forward them to the fragment shader without modifying them directly (using another smooth out
output).
In the fragment shader, you'd typically do something like this:
vFragColor = texture(colorTable, textureCoordinate)
* texture(modelTexture, modelTextureCoordinate);
.. to achieve the desired effect. Since the 1d texture lookup already gives you the kind of sharp lighting that is typical for toon-ish scenes, adding a texture is just multiplying it's color value with the computed light intensity. It looks best if your texture image are too a bit comic style.
精彩评论