2013-11-14 69 views
3

我試圖將着色器從#version 110移植到150。我知道,從110移到150時,全局的gl_屬性已棄用,因此必須在着色器中手動指定所有這些屬性。然後指定的屬性必須通過C++調用OpenGL到glGetAttribLocationGLSL從v110移植到v150?

我有點不確定如何處理如gl_MultiTexCoord0gl_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; 
} 

任何人都可以提供一些好的資源,以幫助這些版本之間的移植?

+0

是否有某些原因導致您無法切換到GLSL 4+? – GraphicsMuncher

+0

如果您希望頂點屬性映射到特定位置,而不是依賴GLSL實現來分配它們,您還可以在鏈接之前調用'glBindAttribLocation(...)'。您還需要爲片段着色器輸出執行此操作。 –

回答

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; 
} 

P.S.這個book有一整章致力於不同的GLSL着色器版本之間的這種轉換。