GLSL中的版本兼容性

问题描述 投票:0回答:1

我正在GLSL中从事一个项目(我没有经验)。我目前没有自己编写任何代码,只是试图运行别人的代码。不幸的是,他们使用版本120编写了代码,而我试图在版本330上运行它。注意:我在版本10.15的Mac上运行此代码。这是代码:

# version 330 core
in vec3 mynormal; 
in vec4 myvertex; 

uniform mat4 modelview;

out vec4 fragColor;

uniform vec3 color;

const int numLights = 10; 
uniform bool enablelighting; // are we lighting at all (global).
uniform vec4 lightposn[numLights]; // positions of lights 
uniform vec4 lightcolor[numLights]; // colors of lights
uniform int numused;               // number of lights used  

uniform vec4 ambient; 
uniform vec4 diffuse; 
uniform vec4 specular; 
uniform vec4 emission; 
uniform float shininess; 

vec4 ComputeLight (const in vec3 direction, const in vec4 lightcolor, const in vec3 normal, const in vec3 halfvec, const in vec4 mydiffuse, const in vec4 myspecular, const in float myshininess) {

    float nDotL = max(dot(normal, direction), 0.0);
    vec4 diffuse = mydiffuse * lightcolor * nDotL;

    float nDotH = max(dot(normal, halfvec), 0.0);
    vec4 specular = myspecular * lightcolor * pow(nDotH, myshininess);

    return diffuse + specular;
}

void main (void)
{
    if (enablelighting) {
        const vec3 eyepos = vec3(0,0,0) ;
        vec4 _mypos = gl_ModelViewMatrix * myvertex ;
        vec3 mypos = _mypos.xyz / _mypos.w;
        vec3 eyedir = normalize(eyepos - mypos);

        vec3 _normal = (gl_ModelViewMatrixInverseTranspose*vec4(mynormal,0.0)).xyz ;
        vec3 normal = normalize(_normal);

        gl_FragColor = ambient;
        for (int i = 0; i < numused; ++i) {
            vec3 light_direction;
            if (lightposn[i].w == 0) {
                light_direction = lightposn[i].xyz;
            } else {
                vec3 light_position = lightposn[i].xyz / lightposn[i].w ;
                light_direction = normalize(light_position - mypos); // no attenuation
            }
            vec3 half = normalize (light_direction + eyedir);
            vec4 col = ComputeLight(light_direction, lightcolor[i], normal, half, diffuse, specular, shininess);
            gl_FragColor += col;
        }
    }
    else gl_FragColor = color ;
}

这是我得到的错误:

Compile Error, Log Below
ERROR: 0:60: Use of undeclared identifier 'gl_ModelViewMatrix'
ERROR: 0:61: Use of undeclared identifier '_mypos'
ERROR: 0:61: Use of undeclared identifier '_mypos'
ERROR: 0:63: Use of undeclared identifier 'mypos'
ERROR: 0:64: Use of undeclared identifier 'gl_NormalMatrix'
ERROR: 0:79: Use of undeclared identifier 'mypos'
ERROR: 0:90: Use of undeclared identifier 'mypos'
ERROR: 0:101: Use of undeclared identifier 'mypos'
ERROR: 0:112: Use of undeclared identifier 'mypos'
ERROR: 0:123: Use of undeclared identifier 'mypos'
ERROR: 0:134: Use of undeclared identifier 'mypos'
ERROR: 0:145: Use of undeclared identifier 'mypos'
ERROR: 0:156: Use of undeclared identifier 'mypos'
ERROR: 0:167: Use of undeclared identifier 'mypos'
ERROR: 0:178: Use of undeclared identifier 'mypos'
ERROR: 0:193: Use of undeclared identifier 'eyedirn'
ERROR: 0:196: Use of undeclared identifier 'normal'
ERROR: 0:196: Use of undeclared identifier 'halfy'
ERROR: 0:197: Use of undeclared identifier 'colApp'
ERROR: 0:206: Use of undeclared identifier 'gl_FragColor'
ERROR: 0:208: Use of undeclared identifier 'gl_FragColor'

经过一段时间的搜索后,我意识到这是版本之间兼容性的问题。但是,我对GLSL的了解不足,无法解决此问题。我希望修改此文件以使其运行,而不是尝试自己编写以使其与版本330兼容。谁能给我建议如何修改此代码以为我运行?

opengl glsl shader version fragment-shader
1个回答
0
投票

您不会在真空中“运行GLSL”,除了运行着色器外没有任何上下文。着色器在其设计环境之外无法使用。

您正在使用的着色器是针对固定功能矩阵编写的。这些矩阵在GLSL 3.30中不存在。但是,针对OpenGL 3.3编写的程序也不会提供这些矩阵。它正在使用自己的用户定义数据进行渲染。

如果将着色器移植到其他环境,则您可以控制该环境,或者被告知该环境如何工作。无论哪种方式,您都知道该环境在何处拥有其着色器数据。如果您不知道,那就行不通了。

因此,您现在需要了解的是1.20的内容。然后,您可以用当前着色器环境的数据替换对它们的引用(如果尚未提供,则用所述数据进行扩充)。因此:

  • gl_ModelViewMatrix:这是从模型到照相机的转换矩阵。
  • gl_ModelViewMatrixInverseTranspose:这是上面的逆/转置。
  • gl_FragColor:这是此片段要写入的输出颜色。您应该改为声明layout(location = 0) out vec4 someName;变量并写入该变量。
  • gl_NormalMatrix:这是一个用于转换法线的3x3矩阵。 OpenGL的计算方式是通过计算模型视图矩阵的逆/转置。

所以只需将数据替换在这些位置,就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.