【问题标题】:LibGDX ShaderProgram is not compiled on Android deviceLibGDX ShaderProgram 未在 Android 设备上编译
【发布时间】:2024-01-10 06:02:01
【问题描述】:

我正在使用 LibGDX 并且有一个简单的片段着色器:

#ifdef GL_ES
#define mediump lowp
precision mediump float;
#else
#define lowp 
#endif

uniform sampler2D u_texture;
uniform vec2 iResolution; 
uniform float iGlobalTime; 
uniform float glow; 


void main(void)
{
vec2 uv = gl_FragCoord.xy / iResolution.xy;
//uv.y = 1-uv.y; // THIS LINE EVOKES AN ERROR!!!

uv.y += (sin((uv.x + (iGlobalTime * 0.2)) * 10.0) * 0.02)+0.02;

vec4 color = vec4(glow,glow,glow,1);        
vec4 texColor = texture2D(u_texture, uv)*color;
gl_FragColor = texColor;
}

它在 PC 上运行良好,但在 Android 设备上此着色器未编译。如果我评论该行(在示例中评论了一个),一切都会好起来的。

我如何使用它:

mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(),    VertexAttribute.TexCoords(0));
     mesh.setVertices(new float[] 
     {0.5f, 0.5f, 0, 1, 1, 1, 1, 0, 1,
     0.5f, -0.5f, 0, 1, 1, 1, 1, 1, 1,
     -0.5f, -0.5f, 0, 1, 1, 1, 1, 1, 0,
     -0.5f, 0.5f, 0, 1, 1, 1, 1, 0, 0});         
     mesh.setIndices(new short[] {0, 1, 2, 2, 3, 0});

     this.texture = texture;
     shader = new ShaderProgram(Gdx.files.internal("shaders/default.vertex"),
           Gdx.files.internal("shaders/glowwave.fragment"));

渲染方法:

texture.bind();
    shader.begin();

    shader.setUniformMatrix("u_worldView", localCam.projection);
    shader.setUniformi("u_texture", 0);
    shader.setUniformf("iGlobalTime", time);
    shader.setUniformf("iResolution", new Vector2(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()+15));
    shader.setUniformf("glow", glow);
    mesh.render(shader, GL20.GL_TRIANGLES);
    shader.end();

可能是什么原因?在我看来,问题出在预处理器上,尽管我可能是错的。

【问题讨论】:

    标签: android libgdx shader fragment-shader


    【解决方案1】:
    ERROR: 0:17: '-' :  Wrong operand types. No operation '-' exists that takes a left-hand operand of type 'const int' and a right operand of type 'float' (and there is no acceptable conversion)
    ERROR: 0:17: 'assign' :  cannot convert from 'int' to 'float'
    ERROR: 2 compilation errors. No code generated.
    

    试试这个:使用 1.0 而不是 1 uv.y = 1.0 - uv.y; // 这条线引发了一个错误!!!

    【讨论】:

    • 感谢您的快速回答!它帮助了我!