其他分享
首页 > 其他分享> > android – 在LibGDX中将几个纹理传递给着色器

android – 在LibGDX中将几个纹理传递给着色器

作者:互联网

我需要将2个(或更多)纹理传递给一个着色器.此外,我必须在渲染方法中绑定纹理,因为我使用FrameBuffer并尝试在飞行中绑定纹理.所以,我的代码如下:

@Override
public void show () {
      fbo = new FrameBuffer(Format.RGB565, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);

      shader = new ShaderProgram(Gdx.files.internal("shaders/vertex.glsl"), Gdx.files.internal("shaders/test_fragment.glsl"));
    ...
}

@Override
public void render(float delta) {

    fbo.begin();        
    Gdx.graphics.getGL20().glClearColor( 0.0f, 0.0f, 0.0f, 1f );
    Gdx.graphics.getGL20().glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );      

    cam.update();

    spriteBatch.setProjectionMatrix(cam.combined);

    spriteBatch.begin();        
    spriteBatch.draw(someImage, 0, 0, width, height);                   
    spriteBatch.end();
    fbo.end();

    Texture texture = fbo.getColorBufferTexture();

    time+=Gdx.graphics.getDeltaTime();      

    shader.begin();

     shader.setUniformMatrix(u_wview, cam.projection);
     Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE0);
     texture.bind(0);

     shader.setUniformi("u_texture", 0); //passing first texture!!!

     Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE1);       
     tex2.bind(1);
     shader.setUniformi("u_texture2", 1); //passing second texture!!!


    shader.setUniformf(u_res, new Vector2(Gdx.graphics.getWidth(),Gdx.graphics.getHeight())); 
    shader.setUniformf(u_time, time);


    mesh.render(shader, GL20.GL_TRIANGLES);

      shader.end();
}

片段着色器:

#ifdef GL_ES
precision mediump float;
#endif

varying vec2 v_texCoords;
uniform vec2 resolution;
uniform float u_time;
uniform sampler2D u_texture;
uniform sampler2D u_texture2;
uniform float u_temp;
const float pi = 3.14159265;

void main() 
{
vec2 p = gl_FragCoord.xy / resolution.xy;

vec4 i = texture2D(u_texture2, p);
gl_FragColor = texture2D(u_texture, p)+i;
}

如果我只传递一个纹理,一切都还可以.否则,我会得到意想不到的结果,好像第一个纹理(u_texture)是第二个纹理(u_texture).将多个纹理传递给着色器的正确方法是什么?

解决方法:

我必须按照下面的顺序传递纹理:

 texture.bind(1);
 shader.setUniformi("u_texture", 1); //passing first texture!!!

 texture2.bind(0);
 shader.setUniformi("u_texture2", 0); //passing second texture!!! 

标签:android,libgdx,opengl-es-2-0,fragment-shader,pixel-shader
来源: https://codeday.me/bug/20190528/1174570.html