带有纹理的卡通着色器
我正在尝试在 opengl 3+ 上使用 C++ 实现带有纹理的香椿着色器,但一周后我只得到了没有纹理的彩色香椿着色器。
顶点文件:
#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 = normalMatrix * 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;
}
片段文件:
//
#version 330
uniform sampler1D colorTable;
out vec4 vFragColor;
smooth in float textureCoordinate;
void main(void)
{
vFragColor = texture(colorTable, textureCoordinate);
}
任何人都可以帮我让这个着色器处理纹理吗?
谢谢
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 = normalMatrix * 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要另一个顶点着色器输入来接收每个顶点的 UV 坐标。将它们转发到片段着色器,而不直接修改它们(使用另一个
smooth out
输出)。在片段着色器中,您通常会执行以下操作:
.. 以达到所需的效果。由于一维纹理查找已经为您提供了卡通场景中典型的锐利照明,因此添加纹理只需将其颜色值乘以计算出的光强度即可。如果你的纹理图像有点漫画风格,它看起来最好。
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:
.. 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.