【问题标题】:Using deferred rendering, getting blank output使用延迟渲染,得到空白输出
【发布时间】:2015-03-11 16:47:50
【问题描述】:

所以我正在使用 Java/LibGDX,我正在尝试设置延迟渲染的基础知识,即将实际游戏艺术渲染到 FBO 的一个颜色缓冲区,并将相应的法线渲染到另一个颜色缓冲区FBO。

(本质上,我想通过使用多个渲染目标来创建thisthis。)

我的问题是我的最终输出是空白的,好像没有渲染任何内容或渲染不正确。


我的着色器(顶点和片段)

我有点确定这是可行的,因为如果我只是完全正常地渲染一些精灵并在屏幕上启用它(不是 FBO),它们会渲染。

#version 150

in vec4 a_position;
in vec4 a_color;
in vec2 a_texCoord0;

uniform mat4 u_projTrans;

out vec4 v_color;
out vec2 v_texCoords;

void main()
{
   v_color = a_color;
   v_color.a = v_color.a * (255.0/254.0);
   v_texCoords = a_texCoord0;
   gl_Position =  u_projTrans * a_position;
}



#version 150

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

in LOWP vec4 v_color;
in vec2 v_texCoords;

uniform sampler2D u_texture;
uniform sampler2D u_normal;

out vec4 fragColor;

void main()
{
    gl_FragData[0] = v_color * texture(u_texture, v_texCoords);
    gl_FragData[1] = texture(u_normal, v_texCoords);
}

以下代码在渲染循环中。

基本思想是我正在将 FBO 的两个颜色缓冲区与 glDrawBuffers 绑定。然后我绑定两个纹理单元,游戏美术和普通游戏美术。我上面的着色器应该接受这个并将游戏艺术输出到一个颜色缓冲区,并将相应的正常艺术输出到另一个。

// Position the camera.
_cameraRef.position.set(_stage.getWidth() * 0.5f, _stage.getHeight() * 0.5f, 0);

// Update the camera, SpriteBatch, and map renderer.
_cameraRef.update();
_spriteBatch.setProjectionMatrix(_cameraRef.combined);
_mapRenderer.setView(_cameraRef);       

// Bind the color texture units of the FBO (multiple-render-targets).
Gdx.gl30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, _gBufferFBOHandle);
IntBuffer intBuffer = BufferUtils.newIntBuffer(2);
intBuffer.put(GL30.GL_COLOR_ATTACHMENT0);
intBuffer.put(GL30.GL_COLOR_ATTACHMENT1);
Gdx.gl30.glDrawBuffers(2, intBuffer);

// Draw!
Gdx.gl30.glClearColor(0, 0, 0, 1);
Gdx.gl30.glClear(GL30.GL_COLOR_BUFFER_BIT);

_spriteBatch.setShader(_shaderGBuffer);
_spriteBatch.begin();

_tilesAtlasNormal.bind(1); // Using multiple texture units to draw art and normals at same time to the two color buffers of the FBO.
_tilesAtlas.bind(0);
_mapRenderer.renderTileLayer(_mapLayerBackground);

_spriteBatch.end();
_spriteBatch.setShader(null);

// Bind the default FBO.
Gdx.gl30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);

// Draw the contents of the FBO onto the screen to see what it looks like.
Gdx.gl30.glClearColor(0, 0, 0, 1);
Gdx.gl30.glClear(GL30.GL_COLOR_BUFFER_BIT);

_spriteBatch.begin();
_spriteBatch.draw(_gBufferTexture1, 0.0f, 0.0f);  // <-- This texture is blank? It's the texture that was just rendered to above.
_spriteBatch.end();

就像我说的,最终输出是空白的。我确定我创建的 FBO 是有效的,因为我检查了 glCheckFramebufferStatus。

只是当我从该 FBO 获取颜色纹理并将它们绘制到屏幕上时,它们是空白的。我不知道我哪里出错了。

感谢任何意见。

【问题讨论】:

  • 着色器编译器是否在信息日志中为您提供了任何信息?在片段着色器中声明out 变量,同时写入gl_FragData [n] 不应该工作。可能会拯救您的一件事是您从未真正写过fragColor,因此该变量可能已被消除,但它们仍然不应该同时存在于同一个着色器中。
  • 没有来自日志的信息,它似乎编译也没有错误。当我以后有机会时,我会尝试删除 out 变量,看看它是否有帮助。

标签: java opengl libgdx deferred-rendering deferred-shading


【解决方案1】:

上次我检查了 gl30 并没有真正准备好在 LibGDX 中的黄金时间。

也就是说,我已经完全实现了你想要的,但是在 gl20.即使你有其他理由使用 gl30,也许你会发现我的实现很有用sourcevideo

【讨论】:

    【解决方案2】:

    不要忘记倒带您的 intbuffer:

    IntBuffer intBuffer = BufferUtils.newIntBuffer(2);
    intBuffer.put(GL30.GL_COLOR_ATTACHMENT0);
    intBuffer.put(GL30.GL_COLOR_ATTACHMENT1);
    
    intBuffer.rewind();
    

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多