LibGDX Sprite是白色的(但透明度正常)

时间:2015-08-24 14:28:52

标签: opengl libgdx textures alphablending fbo

我正在使用LibGDX开发游戏项目。现在我面临一个我无法理解的问题;让我在解决实际问题之前解释一下我所得到的:

  1. 静态背景C(平铺)
  2. 移动背景B(平铺)
  3. 移动背景A(平铺)
  4. 字符,实体
  5. 墙(平铺)
  6. HUD
  7. 渲染循环如下:

    1. 将前一个列表中的2到5渲染为透明FBO
    2. 将光照贴图渲染到另一个FBO(使用box2dLights)
    3. 使用自定义着色器将两种产品混合在一起
    4. 绘制静态背景
    5. 绘制步骤3中的混合纹理
    6. 绘制HUD
    7. 我面临的问题是我在渲染背景B时获得了一个白色精灵(不是白色块;它更像是一个rgba(1,1,1,x)。然后背景B被混合正确使用光照贴图(渲染步骤3),但我无法获得真正的颜色,只有白色:

      The background B should appear dark blue and with a texture; it appears as a white sprite (blended with the lightmap)

      背景B应呈现深蓝色和纹理;它看起来像一个白色精灵(与光照贴图混合)。在背景A后面,在图片的左侧(以及" SHADOW"文本所在的位置),您仍然可以看到背景B的紫色调,与光照贴图混合的结果,但请注意它没有纹理,在我看来,如果纹理只是纯白色+ alpha。

      渲染代码:

          @Override
          public void render(float delta) {
              Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
              Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      
              /* 1. Update Physics and lights */
              if (worldFixedStep(delta)) {
                  rayHandler.update();
              }
              updateCameras();
              cameraMatrixCopy.set(camera.combined);
              rayHandler.setCombinedMatrix(cameraMatrixCopy.scale(Globals.BOX_TO_WORLD, Globals.BOX_TO_WORLD, 1.0f), camera.position.x,
                      camera.position.y, camera.viewportWidth * camera.zoom * Globals.BOX_TO_WORLD,
                      camera.viewportHeight * camera.zoom * Globals.BOX_TO_WORLD);
      
              rayHandler.render();
              final Texture lightMap = rayHandler.getLightMapTexture();
      
              fbo.begin();
              {
                  Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
                  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      
                  /* 2. Draw the backgrounds */
                  batch.enableBlending();
                  batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
      
                  batch.setProjectionMatrix(bgCamera2.combined); //Background B
                  batch.begin();
                  bTileMapRenderer.setView(bgCamera2);
                  bTileMapRenderer.renderTileLayer(tilesBg2Layer);
                  batch.end();
      
                  batch.setProjectionMatrix(bgCamera1.combined); //Background A
                  batch.begin();
                  bTileMapRenderer.setView(bgCamera1);
                  bTileMapRenderer.renderTileLayer(tilesBg1Layer);
                  batch.end();
      
                  batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); //FIXME this is where to touch to avoid gray alpha'ed borders.
                  batch.setProjectionMatrix(camera.combined);
                  batch.begin();
      
                  (...) //Draw everything else that needs to be blended with the light map
      
                  batch.end();
              }
              fbo.end();
      
              /* 3. Render the static background (background C) */
              batch.setProjectionMatrix(bgCameraStatic.combined);
              batch.disableBlending();
              batch.begin();
              bTileMapRenderer.setView(bgCameraStatic);
              bTileMapRenderer.renderTileLayer(tilesBg3Layer);
              batch.end();
      
              /* 4. Blend the frame buffer's texture with the light map in a fancy way */
              Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
              fboRegion.getTexture().bind();
      
              Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE1);
              lightMap.bind();
      
              Gdx.gl20.glEnable(Gdx.gl20.GL_BLEND);
              Gdx.gl20.glBlendFunc(Gdx.gl20.GL_SRC_ALPHA, Gdx.gl20.GL_ONE_MINUS_SRC_ALPHA);
              lightShader.begin();
              lightShader.setUniformf("ambient_color", bgColor[0], bgColor[1], bgColor[2]);
              lightShader.setUniformi("u_texture0", 0);
              lightShader.setUniformi("u_texture1", 1);
              fullScreenQuad.render(lightShader, GL20.GL_TRIANGLE_FAN, 0, 4);
              lightShader.end();
      
              Gdx.gl20.glDisable(Gdx.gl20.GL_BLEND);
      
              hud.draw();
          }
      

      我无法理解为什么这个背景是白色的,但仍然是它的alpha数据。图像是预乘的alpha纹理,但我再也看不出为什么这会影响颜色渲染。

      非常感谢任何帮助。

      干杯

0 个答案:

没有答案