2017-03-20 183 views
2

我遇到了在我的树莓上构建这个问题。即使我强制使用OpenGL ES,并将GLSL版本强制为1.0 es,该着色器代码也可以在其他几台计算机上使用。GLSL ES顶点着色器错误

将#version字符串以编程方式添加到每个着色器。

顶点着色器:

#version 100 
#ifdef GL_ES 
precision mediump float; 
precision mediump int; 
#endif 

#if (__VERSION__ > 120) 
#define IN in 
#define OUT out 
#else 
#define IN attribute 
#define OUT varying 
#endif // __VERSION 

#define MAX_LIGHTS 8 

struct SLight 
    { 
    vec3 Position; 
    vec3 DiffuseColor; 
    float Intensity; 
    float ConstantAttenuation; 
    float LinearAttenuation; 
    float ExponentialAttenuation; 
    float CutoffDistance; 
    float CutoffIntensity; 
    }; 
struct SMaterial 
    { 
    vec3 Specular, Diffuse, Ambient; 
    float Shininess; 
    }; 

uniform SLight Lights[MAX_LIGHTS]; 
uniform int LightCount; 
uniform SMaterial Material; 

struct SAmbientLight 
    { 
    vec3 Color; 
    float Intensity; 
    }; 

uniform mat4 ModelMatrix, ViewMatrix, MVPMatrix; 
uniform SAmbientLight AmbientLight; 

IN vec3 VertexPosition, VertexNormal; 
IN vec2 VertexTexCoord; 
IN vec4 VertexColor; 

OUT vec2 PerVertex_TexCoord; 
OUT vec4 PerVertex_Color; 
OUT vec3 PerVertex_ViewSpaceNormal, PerVertex_ViewVector; 
OUT vec4 PerVertex_LightVectors[MAX_LIGHTS]; 

uniform bool ColoringEnabled, TexturingEnabled, LightingEnabled; 

vec4 ViewSpaceLightPositions[MAX_LIGHTS]; 
void main() 
    { 
    mat4 ObjectToViewMatrix = ViewMatrix * ModelMatrix; 
    vec4 ViewSpaceCoordinate = ObjectToViewMatrix * vec4 (VertexPosition, 1.0f); 

    // Calculate normal in view-space 
    PerVertex_ViewSpaceNormal = mat3 (ObjectToViewMatrix) * VertexNormal; 

    // Calculate light position in view-space 
    for (int cont = 0; cont < LightCount; ++cont) 
     ViewSpaceLightPositions[cont] = (ViewMatrix * vec4 (Lights[cont].Position, 1.0f)); 

    // Calculate vectors from the lights to this vertex 
    for (int cont = 0; cont < LightCount; ++cont) 
     PerVertex_LightVectors[cont] = ViewSpaceLightPositions[cont] - ViewSpaceCoordinate; 

    // Calculate view vector 
    PerVertex_ViewVector = -ViewSpaceCoordinate.xyz; 

    gl_Position = MVPMatrix * vec4 (VertexPosition, 1.0f); 
    PerVertex_TexCoord = VertexTexCoord; 
    if (ColoringEnabled) 
     PerVertex_Color = VertexColor; 
    } 

片段着色器:

#version 100 
#ifdef GL_ES 
precision mediump float; 
precision mediump int; 
#endif 

#if (__VERSION__ > 120) 
#define IN in 
#else 
#define IN varying 
#endif // __VERSION __ 

#if (__VERSION__ > 330) 
#define texture2D texture 
#endif 

#if (__VERSION__ >= 300) 
#define FRAG_OUTPUT FragOutput 
out vec4 FragOutput; 
#else 
#define FRAG_OUTPUT gl_FragColor 
#endif 

#define MAX_LIGHTS 8 

struct SLight 
    { 
    vec3 Position; 
    vec3 DiffuseColor; 
    float Intensity; 
    float ConstantAttenuation; 
    float LinearAttenuation; 
    float ExponentialAttenuation; 
    float CutoffDistance; 
    float CutoffIntensity; 
    }; 
struct SMaterial 
    { 
    vec3 Specular, Diffuse, Ambient; 
    float Shininess; 
    }; 

uniform SLight Lights[MAX_LIGHTS]; 
uniform int LightCount; 
uniform SMaterial Material; 

struct SAmbientLight 
    { 
    vec3 Color; 
    float Intensity; 
    }; 

uniform mat4 ModelMatrix, ViewMatrix, MVPMatrix; 
uniform SAmbientLight AmbientLight; 

IN vec2 PerVertex_TexCoord; 
IN vec4 PerVertex_Color; 
IN vec3 PerVertex_ViewSpaceNormal, PerVertex_ViewVector; 
IN vec4 PerVertex_LightVectors[MAX_LIGHTS]; 

uniform bool ColoringEnabled, TexturingEnabled, LightingEnabled; 
uniform sampler2D TextureSampler; 

vec4 CalculateLights (void) 
    { 
    vec4 LightResult; 
    LightResult = vec4 (0.0f, 0.0f, 0.0f, 1.0f); 
    vec3 N = normalize (PerVertex_ViewSpaceNormal); 
    vec3 V = normalize (PerVertex_ViewVector); 
    for (int cont = 0; cont < LightCount; ++cont) 
     { 
     float Distance = length (PerVertex_LightVectors[cont]); 
     if (Distance > Lights[cont].CutoffDistance) 
      continue; 

     // Normalize the incoming N, L and V vectors 
     vec3 L = normalize (PerVertex_LightVectors[cont]).xyz; 
     vec3 H = normalize (L + V); 

     // Compute the diffuse and specular components for each fragment 
     vec3 diffuse = max (dot (N, L), 0.0f) * Material.Diffuse * Lights[cont].DiffuseColor /** Lights[cont].Intensity*/; 
     vec3 specular = pow (max (dot (N, H), 0.0f), Material.Shininess) * Material.Specular; 

     // Compute attenuation 
     float Attenuation = Lights[cont].ConstantAttenuation + Lights[cont].LinearAttenuation * Distance + Lights[cont].ExponentialAttenuation * pow (Distance, 2.0f); 

     // Final color contribution from this light 
     vec3 LightContribution = vec3 (diffuse + specular)/Attenuation; 
     if (length (LightContribution) < Lights[cont].CutoffIntensity) 
      continue; 
     LightResult += vec4 (LightContribution, 1.0f); 
//  LightResult += vec4 (diffuse + specular, 1.0f); 
     } 
    LightResult += vec4 (AmbientLight.Color * AmbientLight.Intensity * Material.Ambient, 1.0f); 
    return LightResult; 
    } 

void main() 
    { 
    vec4 FragmentOriginalColor; 
    if (TexturingEnabled) 
     FragmentOriginalColor = texture2D (TextureSampler, PerVertex_TexCoord); 
    else if (ColoringEnabled) 
     FragmentOriginalColor = PerVertex_Color; 
    else 
     FragmentOriginalColor = vec4 (Material.Diffuse, 1.0f); 

    if (LightingEnabled) 
     FragmentOriginalColor *= CalculateLights(); 
    if (FragmentOriginalColor.a == 0.0) 
     discard; 

    FRAG_OUTPUT = FragmentOriginalColor; 
    } 

这是我的应用程序的输出:

DEBUG: Video driver 1: RPI 
DEBUG: Video driver 2: dummy 
DEBUG: Current video driver: RPI 
INFO: Initializing OpenGLES2 
INFO: Initializing OpenGLES2 
DEBUG: Reported GL version string : OpenGL ES 2.0 
DEBUG: Reported GLSL version string : OpenGL ES GLSL ES 1.00 
DEBUG: Parsed GLSL version 1.0 es 
glGetError 0x500 
glGetError 0x500 
DEBUG: OpenGL version 2.0 es 
DEBUG: GLSL version 1.0 es 
DEBUG: Created window 0x160a960 
DEBUG: GL program info log length 9 
DEBUG: vertex shader ID 1 successfully compiled 
DEBUG: GL program info log length 9 
DEBUG: fragment shader ID 2 successfully compiled 
DEBUG: Created shader program 3 
DEBUG: GL program info log length 56 
ERROR: Shader program 3 link error. ERROR:LEX/PARSE-1 (vertex shader, line 61) Syntax error 

线61似乎是内部主要的第一行,两个矩阵相乘的地方。我究竟做错了什么?

+0

如果您在这里没有得到答案,请尝试在[computergraphics.se]询问。 –

回答

0

我试着用PVRShaderEditor编译GLES的顶点着色器(来自Power VR SDK,有用的工具来检查GLES相关的错误,它允许像在iPhone上一样编译着色器)。

它报告的三个错误:

ERROR: 0:62: 'f' : suffix for floats requires language version 300 
ERROR: 0:69: 'f' : suffix for floats requires language version 300 
ERROR: 0:78: 'f' : suffix for floats requires language version 300 

所以你只需要1.0全部更换1.0f在顶点和片段着色器,问题应该得到解决,似乎是在GLES2.0不支持后缀f 。

请注意,它似乎有一个Pi上的错误,导致编译错误显示在程序链接时,而他们应该在着色器编译时显示......不知道为什么你得到的错误信息没有更多的细节。 ..

+0

可能就是这样。错误开始越来越远。不幸的是我现在无法检查,因为我的rpi完全拒绝使用简单的索引数组VBO,出于某种原因,所以我实际上无法呈现任何东西。 –