我试图使用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);
}
谁能帮我让这个着色器与纹理一起工作?
thx
发布于 2011-04-23 21:17:05
您需要另一个顶点着色器输入,该输入接收每个顶点的UV坐标。将它们转发到片段着色器,而不直接修改它们(使用其他smooth out
输出)。
在片段着色器中,通常会执行如下操作:
vFragColor = texture(colorTable, textureCoordinate)
* texture(modelTexture, modelTextureCoordinate);
。。以达到预期的效果。由于1d纹理查找已经为您提供了典型的卡通场景的锐利照明,因此添加纹理只是将其颜色值与计算的灯光强度相乘。如果你的纹理图像看起来太滑稽一点,效果会更好。
https://stackoverflow.com/questions/5766965
复制相似问题