3

我正在尝试将着色器#version 110150. 我知道当从全局属性迁移110150全局gl_属性时,所有这些属性都必须在着色器中手动指定。然后必须通过 C++ 通过调用 OpenGL 来设置指定的属性glGetAttribLocation

我有点不确定如何处理诸如gl_MultiTexCoord0和之类的事情gl_TexCoord

这里只是我试图移植#version 110到的着色器之一150

//  blinn_phong.glsl
[vert]

#version 110

varying vec3 normal;

void main()
{
    normal = normalize(gl_NormalMatrix * gl_Normal);

    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;    
}

[frag]

#version 110

uniform sampler2D colorMap;
uniform float materialAlpha;

varying vec3 normal;

void main()
{   
    vec3 n = normalize(normal);

    float nDotL = max(0.0, dot(n, gl_LightSource[0].position.xyz));
    float nDotH = max(0.0, dot(normal, vec3(gl_LightSource[0].halfVector)));
    float power = (nDotL == 0.0) ? 0.0 : pow(nDotH, gl_FrontMaterial.shininess);

    vec4 ambient = gl_FrontLightProduct[0].ambient;
    vec4 diffuse = gl_FrontLightProduct[0].diffuse * nDotL;
    vec4 specular = gl_FrontLightProduct[0].specular * power;
    vec4 color = gl_FrontLightModelProduct.sceneColor + ambient + diffuse + specular;

    gl_FragColor = color * texture2D(colorMap, gl_TexCoord[0].st);
    gl_FragColor.a = materialAlpha;
}

谁能提供一些好的资源来帮助在这些版本之间进行移植?

4

1 回答 1

2

您必须设置自己的制服、属性、变量和片段着色器输出。代码将变得类似于:

[vert]

#version 150 core

uniform mat3 in_NormalMatrix;
uniform mat4 in_ModelViewProjectionMatrix;

in vec3 in_Normal;
in vec4 in_Vertex;
in vec2 in_MultiTexCoord;

out vec3 normal;
out vec2 texcoord;

void main()
{
    normal = normalize(in_NormalMatrix * in_Normal);

    gl_Position = in_ModelViewProjectionMatrix * in_Vertex;
    texcoord = in_MultiTexCoord;    
}

[frag]

#version 150

uniform sampler2D colorMap;
uniform float materialAlpha;

struct sLightSource
{
   vec3 position;
   vec3 halfVector;
   ...
}

uniform sLightSource u_LightSource;

in vec3 normal;
in vec2 texcoord;

out vec4 out_FragColor;

void main()
{   
    vec3 n = normalize(normal);

    float nDotL = max(0.0, dot(n, u_LightSource.position.xyz));
    float nDotH = max(0.0, dot(normal, vec3(u_LightSource.halfVector)));
    ...
    out_FragColor = color * texture(colorMap, texcoord);
    out_FragColor.a = materialAlpha;
}

PS 这本书有一整章专门讨论不同 GLSL 着色器版本之间的这种转换。

于 2013-11-14T12:30:59.990 回答