【问题标题】:Libgdx Stencil & ShapeRendererLibgdx 模板和 ShapeRenderer
【发布时间】:2016-10-17 12:06:49
【问题描述】:

我正在尝试完成这样的事情:

sample image

整个屏幕会是黑色的,那么三角形的内部是只会出现的部分。

我尝试使用 SCISSOR,但它的形状是矩形。

*原图来源:https://www.html5rocks.com/static/images/screenshots/casestudies/onslaught/controls_tutorial.png

【问题讨论】:

  • 你有很多方法可以达到你想要达到的效果。我想到的最简单的方法是使用模板缓冲区(有很多与模板测试相关的教程)。如果您想要更一般的答案,请将此 wiki 作为屏蔽部分:github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Masking
  • 只表示剪刀,是矩形的。
  • 我知道它不是 OpenGL ES,但这应该很接近:en.wikibooks.org/wiki/OpenGL_Programming/Stencil_buffer 在示例中,模板与圆形一起使用,但绘制三角形应该更容易。
  • 我会解决的。谢谢:)
  • 如果你写的成功了,如果写不成功,请不要犹豫粘贴,我试试看

标签: android libgdx opengl-es-2.0


【解决方案1】:

有几种不同的方法可以渲染蒙版图像。一种可能的方法是使用深度缓冲区。我编写了一个小方法,该方法显示了使用 ShapeRenderer 设置缓冲区的过程,以定义图像的三角形区域以渲染和屏蔽其余部分。三角形遮罩可以替换为 ShapeRenderer 能够渲染的任何其他形状。

// For a 2D image use an OrthographicCamera
OrthographicCamera cam = new OrthographicCamera();
ShapeRenderer shapes = new ShapeRenderer();
cam.setToOrtho(true, screenWidth, screenHeight);
shapes.setProjectionMatrix(cam.combined);

private void renderStencilImage(float runTime){
    // Clear the buffer
    Gdx.gl.glClearDepthf(1.0f);
    Gdx.gl.glClear(GL30.GL_DEPTH_BUFFER_BIT);
    // Disable writing to frame buffer and
    // Set up the depth test
    Gdx.gl.glColorMask(false, false, false, false);
    Gdx.gl.glDepthFunc(GL20.GL_LESS);
    Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
    Gdx.gl.glDepthMask(true);
    //Here add your mask shape rendering code i.e. rectangle
    //triangle, or other polygonal shape mask
    shapes.begin(ShapeRenderer.ShapeType.Filled);
    shapes.setColor(1f, 1f, 1f, 0.5f);
    shapes.triangle(x1,y1,x2,y2,x3,y3);
    shapes.end();
    // Enable writing to the FrameBuffer
    // and set up the texture to render with the mask
    // applied
    Gdx.gl.glColorMask(true, true, true, true);
    Gdx.gl.glDepthMask(true);
    Gdx.gl.glDepthFunc(GL20.GL_EQUAL);
    // Here add your texture rendering code
    batcher.begin();
    renderFrame(runTime);
    batcher.end();
    // Ensure depth test is disabled so that depth
    // testing is not run on other rendering code.
    Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
}

在调用该方法之前,必须先创建一个 ShapeRenderer 并设置投影矩阵。您还必须在 onCreate 方法的 android 配置中设置深度缓冲区选项,如下所示:

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.depth = 15;
    initialize(new game(), config);
}

glDepthFunc 的选项定义蒙版如何应用于纹理。查看OpenGL wiki 以查看可以传递给函数的参数。

【讨论】:

  • 在桌面上也能完美运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
相关资源
最近更新 更多