Android Libgdx模具及;成形机

Android Libgdx模具及;成形机,android,libgdx,opengl-es-2.0,Android,Libgdx,Opengl Es 2.0,我正在努力完成这样的事情: protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); config.depth = 15; initialize(new game(), config);

我正在努力完成这样的事情:

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

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

我试着用剪刀,但它是长方形的


*原始图像源:

有几种不同的方法可以渲染遮罩图像。一种可能的方法是使用深度缓冲区。我写了一个小方法,展示了使用ShaperEnder设置缓冲区的过程,它定义了图像的一个三角形区域来渲染和遮罩剩余部分。三角形遮罩可以替换为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);
}
在调用该方法之前,必须首先创建ShaperEnder并设置投影矩阵。您还必须在onCreate方法的android配置中设置深度缓冲区选项,如下所示:

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

glDepthFunc的选项定义如何将遮罩应用于纹理。查看以查看可以传递给函数的参数。

您有很多方法可以达到想要达到的效果。我想到的最简单的方法是使用模具缓冲区(有很多与模具测试相关的教程)。如果你想要一个更一般的答案,这个wiki作为一个掩蔽部分:它只表示剪刀,它的形状是矩形。我知道它不是OpenGL ES,但这应该非常接近:在这个例子中,模具与一个圆一起使用,但是画一个三角形应该更容易。我会开始。谢谢:)如果你成功地写了一些东西,请不要犹豫,如果有东西失败了,请粘贴它,我会尝试在桌面上看一看,它也可以完美地工作!