Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Created July 25, 2013 04:10
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdesl/6076849 to your computer and use it in GitHub Desktop.
Save mattdesl/6076849 to your computer and use it in GitHub Desktop.
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();
}
}
@chenfengkg
Copy link

I try to use it in Table's draw method,but I can't manage to achieve it !
Here is my code:

      @Override
      protected void drawBackground(Batch batch, float parentAlpha, float x, float y) {
               // TODO Auto-generated method stub
              batch.end();
               //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_COLOR_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( PositionArray.detail_x, PositionArray.detail_y,50);

                shapes.end();
                batch.begin();              

                ///////////// Draw sprite(s) to be masked
                //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
                super.drawBackground(batch, parentAlpha, x, y);
                //  batch.draw(grass, 0, 0);
                //end/flush your batch
                Gdx.gl.glDisable(GL10.GL_DEPTH_TEST);
          }                 

Can you give me some advice or some code?
Thank you very much!

@NasserTahani
Copy link

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