简体   繁体   English

OpenGL - 顶点或片段着色器中的纹理映射?

[英]OpenGL - Texture mapping in vertex or fragment shader?

Is there texture of an object/triangles mapped in the fragment shader or the vertex shader? 是否在片段着色器或顶点着色器中映射了对象/三角形的纹理?

Whether if it is on the vertex or fragment shader, if you are programming shaders it's something that you have to code yourself right? 无论是在顶点还是片段着色器上,如果您正在编写着色器,那么您必须自己编写代码吗? Without shader you just assinged the tex coords and opengl mapps it without you knowing, but with shaders you have to do it yourself, right? 如果没有着色器,你只需要在不知情的情况下就可以使用它来识别它们,但是使用着色器你必须自己做,对吗?

Usually the texturing happens in the fragment shader. 通常纹理在片段着色器中发生。 This is the place where triangle's fragments gain a color. 这是三角形碎片获得颜色的地方。

Usually in the vertex shader you calculate texture coords (or simply pass them through from vertex attribs without special computation). 通常在顶点着色器中,您可以计算纹理坐标(或者只是从顶点属性传递它们而无需特殊计算)。

I wrote usually because right now you can use textures in all shaders: vertex, geometry, tesselation. 经常写,因为现在你可以在所有着色器中使用纹理:顶点,几何,细分。

See here for further information. 请参阅此处获取更多信息。

This is an example of a fragment shader that implements lighting on GLSL with support of texturing, point and directional lighting (indexed as 0) and eventually fog! 这是一个片段着色器的示例,它在GLSL上实现照明,支持纹理,点和方向照明(索引为0)并最终雾化!

varying vec3 vertex;
varying vec3 normal;
varying vec3 eye;

uniform sampler2D DiffuseMap;

void computeLight(in int i, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular)
{
    ambient += gl_LightSource[i].ambient * gl_FrontMaterial.ambient;

    vec3 light;

    if(gl_LightSource[i].position.w == 1.0)
        light = gl_LightSource[i].position.xyz - vertex;

    else
        light = gl_LightSource[i].position.xyz;

    vec3 lightDiri = normalize(light);

    float NdotL = max(0.0, dot(normal, lightDiri));

    if(NdotL > 0.0)
    {
        float att = 1.0;

        // Calcule l'attinuation dans le cas d'une lumier pointctuelle
        if(gl_LightSource[i].position.w == 1.0)
        {
            float distance = length(light);

            att = 1.0 / (gl_LightSource[i].constantAttenuation
            + gl_LightSource[i].linearAttenuation
            * distance
            + gl_LightSource[i].quadraticAttenuation
            * distance
            * distance);
        }

        float NdotHV = dot(normal, normalize(lightDiri + eye));

        diffuse  += gl_FrontMaterial.diffuse * gl_LightSource[i].diffuse * NdotL * att;
        specular += gl_FrontMaterial.specular * gl_LightSource[i].specular * pow(NdotHV, gl_FrontMaterial.shininess) * NdotL * att;
    }
}

void main()
{
    vec4 ambient = vec4(0);
    vec4 diffuse = vec4(0);
    vec4 specular = vec4(0);

    vec4 finalColor = vec4(0);

    computeLight(0, ambient, diffuse, specular);
    finalColor = ambient + diffuse * shadow;

    finalColor *= gl_Color;
    // *** This is what you asking for !
    finalColor *= texture2D(DiffuseMap, gl_TexCoord[0].st);
    finalColor += specular;

    // Foged ?
    float z = gl_FragCoord.z / gl_FragCoord.w;
    float fogFactor = (gl_Fog.end - z) / (gl_Fog.end - gl_Fog.start);
    fogFactor = clamp(fogFactor, 0.0, 1.0);

    gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor);
    gl_FragColor *= finalColor.aaaa;
}

And the vertex shader 和顶点着色器

varying vec3 vertex;
varying vec3 normal;
varying vec3 eye;

void main()
{
    vertex  = vec3(gl_ModelViewMatrix * gl_Vertex);
    normal  = normalize(gl_NormalMatrix * gl_Normal);
    eye     = -normalize(vertex);

    gl_FrontColor       = gl_Color;
    gl_TexCoord[0].xy   = gl_MultiTexCoord0.xy;
    gl_Position         = ftransform();
    gl_ClipVertex       = gl_ModelViewMatrix*gl_Vertex;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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