Created
July 25, 2013 04:10
-
-
Save mattdesl/6076849 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mdesl.line2dx.test; | |
import com.badlogic.gdx.ApplicationListener; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.GL10; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; | |
public class MaskTest1 implements ApplicationListener { | |
SpriteBatch batch; | |
OrthographicCamera cam; | |
Texture grass; | |
ShapeRenderer shapes; | |
@Override | |
public void create() { | |
batch = new SpriteBatch(); | |
cam = new OrthographicCamera(); | |
grass = new Texture("data/grass.png"); | |
shapes = new ShapeRenderer(); | |
} | |
@Override | |
public void resize(int width, int height) { | |
cam.setToOrtho(false, width, height); | |
batch.setProjectionMatrix(cam.combined); | |
} | |
void drawMaskShape() { | |
} | |
@Override | |
public void render() { | |
//1. clear screen | |
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); | |
//2. clear our depth buffer with 1.0 | |
Gdx.gl.glClearDepthf(1f); | |
Gdx.gl.glClear(GL10.GL_DEPTH_BUFFER_BIT); | |
//3. set the function to LESS | |
Gdx.gl.glDepthFunc(GL10.GL_LESS); | |
//4. enable depth writing | |
Gdx.gl.glEnable(GL10.GL_DEPTH_TEST); | |
//5. Enable depth writing, disable RGBA color writing | |
Gdx.gl.glDepthMask(true); | |
Gdx.gl.glColorMask(false, false, false, false); | |
///////////// Draw mask shape(s) | |
//6. render your primitive shapes | |
shapes.begin(ShapeType.Filled); | |
shapes.setColor(1f, 0f, 0f, 0.5f); | |
shapes.circle(50, 50, 50); | |
shapes.setColor(0f, 1f, 0f, 0.5f); | |
shapes.rect(50, 50, 100, 100); | |
shapes.end(); | |
///////////// Draw sprite(s) to be masked | |
batch.begin(); | |
//8. Enable RGBA color writing | |
// (SpriteBatch.begin() will disable depth mask) | |
Gdx.gl.glColorMask(true, true, true, true); | |
//9. Make sure testing is enabled. | |
Gdx.gl.glEnable(GL10.GL_DEPTH_TEST); | |
//10. Now depth discards pixels outside our masked shapes | |
Gdx.gl.glDepthFunc(GL10.GL_EQUAL); | |
//push to the batch | |
batch.draw(grass, 0, 0); | |
//end/flush your batch | |
batch.end(); | |
} | |
@Override | |
public void pause() { | |
} | |
@Override | |
public void resume() { | |
} | |
@Override | |
public void dispose() { | |
batch.dispose(); | |
grass.dispose(); | |
shapes.dispose(); | |
} | |
} |
Is it possible to use depth buffer for 3D objcets and model batch? For instance, masking a box with another one. Please take a look at this question on stackoverflow if there is any idea!!! Thanks. http://stackoverflow.com/questions/43758031/mask-part-of-a-model-with-another-one-in-libgdx/43807796#43807796
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I try to use it in Table's draw method,but I can't manage to achieve it !
Here is my code:
Can you give me some advice or some code?
Thank you very much!