片段着色器中的“错误的预处理程序指令”?

时间:2019-06-13 23:48:28

标签: opengl glsl

我正在制作2个着色器(一个.frag文件),并且收到一条错误消息,指出“错误:0:83:”:语法错误:预处理程序指令不正确”。当我按确定时,它不会显示并使模型不出现,并且我认为这会使其他着色器无法正常工作。奇怪的是,仅显示图像,但不显示模型。为什么会这样?

着色器1:

#version 120
#define PI 3.14159265

varying vec3 position;
varying vec3 normal;
varying vec4 color;
varying vec2 textureCoord;
varying vec2 imageCoord;

uniform float time;
uniform float beat;
uniform vec2 resolution;
uniform vec2 textureSize;
uniform vec2 imageSize;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 perspectiveMatrix;
uniform mat4 textureMatrix;
uniform sampler2D sampler0;
uniform sampler2D sampler1;

vec2 img2tex( vec2 v ) { return v / textureSize * imageSize; }



float amount = 0.3;

float random(float x) 
{ 
    return fract(sin(x) * 10000.);          
}

float noise(vec2 p) 
{
    return random(p.x + p.y * 10000.);            
}

vec2 sw(vec2 p) { return vec2(floor(p.x), floor(p.y)); }
vec2 se(vec2 p) { return vec2(ceil(p.x), floor(p.y)); }
vec2 nw(vec2 p) { return vec2(floor(p.x), ceil(p.y)); }
vec2 ne(vec2 p) { return vec2(ceil(p.x), ceil(p.y)); }

float smoothNoise(vec2 p) 
{
    vec2 interp = smoothstep(0., 1., fract(p));
    float s = mix(noise(sw(p)), noise(se(p)), interp.x);
    float n = mix(noise(nw(p)), noise(ne(p)), interp.x);
    return mix(s, n, interp.y);        
}

float fractalNoise(vec2 p) 
{
    float x = 0.;
    x += smoothNoise(p      );
    x += smoothNoise(p * 2. ) / 2.;
    x += smoothNoise(p * 4. ) / 4.;
    x += smoothNoise(p * 8. ) / 8.;
    x += smoothNoise(p * 16.) / 16.;
    x /= 1. + 1./2. + 1./4. + 1./8. + 1./16.;
    return x;            
}

float movingNoise(vec2 p) 
{ 
    float x = fractalNoise(p + time);
    float y = fractalNoise(p - time);
    return fractalNoise(p + vec2(x, y));    
}

float nestedNoise(vec2 p) 
{    
    float x = movingNoise(p);
    float y = movingNoise(p + 100.);
    return movingNoise(p + vec2(x, y));    
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord.xy / imageSize.xy;
    float n = nestedNoise(uv * 6.) * 1.0;
    float offset = mix(0.0, 2.0, amount);

    #if NORM_FOCUS
      vec2 offsetVector = normalize(vec2(0.5, 0.5) - uv) * (n * offset);
    #else
      vec2 offsetVector = (vec2(0.5, 0.5) - uv) * (n * offset);
    #endif

    fragColor = texture2D(sampler0, img2tex(uv) + offsetVector);
}

void main()
{
    mainImage(gl_FragColor.rgba, gl_FragCoord.xy);
}

着色器2:

#version 120
#define PI 3.14159265

varying vec3 position;
varying vec3 normal;
varying vec4 color;
varying vec2 textureCoord;
varying vec2 imageCoord;

uniform float time;
uniform float beat;
uniform vec2 resolution;
uniform vec2 textureSize;
uniform vec2 imageSize;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 perspectiveMatrix;
uniform mat4 textureMatrix;
uniform sampler2D sampler0;
uniform sampler2D sampler1;

vec2 img2tex( vec2 v ) { return v / textureSize * imageSize; }



float amount = 0.9;

float random(float x) 
{ 
    return fract(sin(x) * 10000.);          
}

float noise(vec2 p) 
{
    return random(p.x + p.y * 10000.);            
}

vec2 sw(vec2 p) { return vec2(floor(p.x), floor(p.y)); }
vec2 se(vec2 p) { return vec2(ceil(p.x), floor(p.y)); }
vec2 nw(vec2 p) { return vec2(floor(p.x), ceil(p.y)); }
vec2 ne(vec2 p) { return vec2(ceil(p.x), ceil(p.y)); }

float smoothNoise(vec2 p) 
{
    vec2 interp = smoothstep(0., 1., fract(p));
    float s = mix(noise(sw(p)), noise(se(p)), interp.x);
    float n = mix(noise(nw(p)), noise(ne(p)), interp.x);
    return mix(s, n, interp.y);        
}

float fractalNoise(vec2 p) 
{
    float x = 0.;
    x += smoothNoise(p      );
    x += smoothNoise(p * 2. ) / 2.;
    x += smoothNoise(p * 4. ) / 4.;
    x += smoothNoise(p * 8. ) / 8.;
    x += smoothNoise(p * 16.) / 16.;
    x /= 1. + 1./2. + 1./4. + 1./8. + 1./16.;
    return x;            
}

float movingNoise(vec2 p) 
{ 
    float x = fractalNoise(p + time);
    float y = fractalNoise(p - time);
    return fractalNoise(p + vec2(x, y));    
}

float nestedNoise(vec2 p) 
{    
    float x = movingNoise(p);
    float y = movingNoise(p + 100.);
    return movingNoise(p + vec2(x, y));    
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord.xy / imageSize.xy;
    float n = nestedNoise(uv * 6.) * 1.0;
    float offset = mix(0.0, 2.0, amount);

    #if NORM_FOCUS
      vec2 offsetVector = normalize(vec2(0.5, 0.5) - uv) * (n * offset);
    #else
      vec2 offsetVector = (vec2(0.5, 0.5) - uv) * (n * offset);
    #endif

    fragColor = texture2D(sampler0, img2tex(uv) + offsetVector);
}

void main()
{
    mainImage(gl_FragColor.rgba, gl_FragCoord.xy);
}

我希望它能工作,但是没有。

1 个答案:

答案 0 :(得分:2)

您在第一个片段着色器中具有此功能:

#if NORM_FOCUS  // line 83
  vec2 offsetVector = normalize(vec2(0.5, 0.5) - uv) * (n * offset);
#else
  vec2 offsetVector = (vec2(0.5, 0.5) - uv) * (n * offset);
#endif

在没有任何其他代码的情况下,我看不到NORM_FOCUS#define放在任何地方,这违反了以下规定(GLSL 1.20 Specification,第7页):

  

#if#elif之后的表达式仅限于使用文字整数常量以及defined运算符使用的标识符的表达式。

所以#define NORM_FOCUS或切换到#ifdef NORM_FOCUS