首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Libgdx模具& ShapeRenderer

Libgdx模具& ShapeRenderer
EN

Stack Overflow用户
提问于 2016-10-17 12:06:49
回答 1查看 900关注 0票数 3

我正试图实现这样的目标:

样本图像

整个屏幕将是黑色的,然后三角形形状的内部是只会出现的部分。

我试过用剪刀,但它是矩形的。

*原始图像来源:tutorial.png

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-19 14:02:51

有几种不同的方法,你可以渲染一个蒙面图像。一种可能的方法是使用深度缓冲区。我编写了一个小方法,它显示了使用ShapeRenderer设置缓冲区的过程,以定义图像的一个三角形区域来呈现和屏蔽其余部分。三角形掩码可以被ShapeRenderer能够呈现的任何其他形状所取代。

代码语言:javascript
复制
// 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中设置深度缓冲区选项,如下所示:

代码语言:javascript
复制
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.depth = 15;
    initialize(new game(), config);
}

glDepthFunc的选项定义如何将掩码应用于纹理。查看OpenGL维基,查看可以传递给函数的参数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40085980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档