带有纹理的卡通着色器

发布于 2024-11-02 18:41:59 字数 1180 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌能量女王 2024-11-09 18:41:59

您需要另一个顶点着色器输入来接收每个顶点的 UV 坐标。将它们转发到片段着色器,而不直接修改它们(使用另一个smooth out输出)。

在片段着色器中,您通常会执行以下操作:

vFragColor = texture(colorTable, textureCoordinate) 
    * texture(modelTexture, modelTextureCoordinate);

.. 以达到所需的效果。由于一维纹理查找已经为您提供了卡通场景中典型的锐利照明,因此添加纹理只需将其颜色值乘以计算出的光强度即可。如果你的纹理图像有点漫画风格,它看起来最好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文