ClipEntity for GLES2 (not AC)

  ... the case you feel the need for a new feature or want to submit one.

ClipEntity for GLES2 (not AC)

Postby Dantel » Tue Oct 29, 2013 10:49 am

Hi. Since I needed it and all that could be found on the forums were people who also looked for it, I ported the code that can be found here: development/clipping-effect-t2962.html considering/using rkposts and hawaiiantimes work.

No garantee that it is a good solution, just works good enough for me. Maybe there are still people looking for this. Corrections are welcome. Edit: First correction already made ^^.

Here is the class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import org.andengine.engine.camera.Camera;
  2. import org.andengine.entity.Entity;
  3. import org.andengine.entity.IEntity;
  4.  
  5. import org.andengine.opengl.util.GLState;
  6.  
  7. import android.graphics.PointF;
  8.  
  9. import android.opengl.GLES20;
  10.  
  11. public class ClipEntity extends Entity {
  12.  
  13.         // ===========================================================
  14.         // Constants
  15.         // ===========================================================
  16.  
  17.         // ===========================================================
  18.         // Fields
  19.         // ===========================================================
  20.  
  21.         protected int clipX;
  22.         protected int clipY;
  23.         protected int clipW;
  24.         protected int clipH;
  25. ;
  26.         private boolean clippingEnabled = true;
  27.  
  28.         // ===========================================================
  29.         // Constructors
  30.         // ===========================================================
  31.  
  32.         public ClipEntity(int clipX, int clipY, int clipW, int clipH) {
  33.  
  34.                 super(clipX, clipY);
  35.                 this.clipX = clipX;
  36.                 this.clipY = clipY;
  37.                 this.clipW = clipW;
  38.                 this.clipH = clipH;
  39.  
  40.                
  41.                
  42.         }
  43.  
  44.         // ===========================================================
  45.         // Getter & Setter
  46.         // ===========================================================
  47.  
  48.  
  49.         public void setX(int clipX) {
  50.                 this.clipX = clipX;
  51.                 super.setPosition(clipX, getY());
  52.         }
  53.  
  54.  
  55.         public void setY(int clipY) {
  56.                 this.clipY = clipY;
  57.                 super.setPosition(getX(), clipY);
  58.                
  59.         }
  60.        
  61.         public void setPosition(float clipX, float clipY)
  62.         {
  63.                 this.clipX = (int)clipX;
  64.                 this.clipY = (int)clipY;
  65.                 super.setPosition(clipX, clipY);
  66.         }
  67.  
  68.         public int getWidth() {
  69.                 return clipW;
  70.         }
  71.  
  72.         public void setWidth(int clipW) {
  73.                 this.clipW = clipW;
  74.         }
  75.  
  76.         public int getHeight() {
  77.                 return clipH;
  78.         }
  79.  
  80.         public void setHeight(int clipH) {
  81.                 this.clipH = clipH;
  82.         }
  83.  
  84.         public void setClippingEnabled(boolean enabled) {
  85.                 clippingEnabled = enabled;
  86.         }
  87.  
  88.         // ===========================================================
  89.         // Methods for/from SuperClass/Interfaces
  90.         // ===========================================================
  91.  
  92.         @Override
  93.         protected void onManagedDraw(GLState pGL, Camera pCamera) {
  94.                 if (clippingEnabled)
  95.                 {
  96.                         PointF point = localToGlobal(this);
  97.  
  98.                         pGL.pushModelViewGLMatrix();
  99.                         pGL.enableScissorTest();
  100.  
  101.                        
  102.                         float screenRatioX = pCamera.getSurfaceWidth()/pCamera.getWidth();
  103.                         float screenRatioY = pCamera.getSurfaceHeight()/pCamera.getHeight();
  104.                        
  105.                         // Calculation for y position
  106.                         // 1. clip y + global y = global clip y
  107.                         // 2. global clip y + clip height = bottom of clip
  108.                         // 3. camera height - bottom of clip = y position from bottom edge
  109.                         // 4. y position from bottom edge * ratio = y position in display
  110.                         // size for glScissor()
  111.  
  112.                        
  113.                        
  114.                         GLES20.glScissor(
  115.                                         Math.round(((clipX + point.x)) * screenRatioX),
  116.                                         Math.round((pCamera.getHeight() - ((clipY + point.y) + clipH))
  117.                                                         * screenRatioY), Math.round(clipW * screenRatioX),
  118.                                         Math.round(pCamera.getHeight() * screenRatioY));
  119.                        
  120.  
  121.                         super.onManagedDraw(pGL, pCamera);
  122.  
  123.                         pGL.disableScissorTest();
  124.                         pGL.popModelViewGLMatrix();
  125.                 } else
  126.                 {
  127.                         super.onManagedDraw(pGL, pCamera);
  128.                 }
  129.         }
  130.  
  131.         // ===========================================================
  132.         // Methods
  133.         // ===========================================================
  134.  
  135.         public PointF localToGlobal(IEntity entity) {
  136.                 float difX = entity.getX();
  137.                 float difY = entity.getY();
  138.  
  139.                 IEntity parent = entity.getParent();
  140.                 while (parent != null && parent.getParent() != null)
  141.                 {
  142.                         difX += parent.getX();
  143.                         difY += parent.getY();
  144.                         parent = parent.getParent();
  145.                 }
  146.  
  147.                 return new PointF(difX, difY);
  148.         }
  149.  
  150.  
  151.         // ===========================================================
  152.         // Inner and Anonymous Classes
  153.         // ===========================================================
  154.  
  155. }
  156.  
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


Using it is simple, clipX/Y is the desired position of the container on your scene. clipW/H the desired width and height of the container.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                
  2. ClipEntity container = new ClipEntity(clipX, clipY, clipW, clipH);
  3.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


After that add the container to your scene. Stuff that is added to this container will be only visible in the specified area.
Come over to the new AndEngine forum! Don't stay here with all this spam.
andengine-community.com
(alternative domain still works: http://andengine.bbforum.co)
User avatar
Dantel
 
Posts: 246
Joined: Mon Jan 07, 2013 12:00 pm

Re: ClipEntity for GLES2 (not AC)

Postby pep_dj » Thu Jan 09, 2014 10:38 am

I made two modifications: I changed clipX, clipY, clipW and clipH from int to float. And I replaced this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. GLES20.glScissor(
  2.     Math.round(((clipX + point.x)) * screenRatioX),
  3.     Math.round((pCamera.getHeight() - ((clipY + point.y) + clipH)) * screenRatioY),
  4.     Math.round(clipW * screenRatioX),
  5.     Math.round(pCamera.getHeight() * screenRatioY));
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


with this (see last line):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. GLES20.glScissor(
  2.     Math.round(((clipX + point.x)) * screenRatioX),
  3.     Math.round((pCamera.getHeight() - ((clipY + point.y) + clipH)) * screenRatioY),
  4.     Math.round(clipW * screenRatioX),
  5.     Math.round(clipH * screenRatioY));
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Thanks for sharing it. It worked!
Image
pep_dj
 
Posts: 170
Joined: Fri Nov 12, 2010 9:05 pm

Re: ClipEntity for GLES2 (not AC)

Postby retroman » Sun Feb 16, 2014 7:05 pm

Hi, thank you for this.

I attached the clipentity to another moving sprite as a child and this works without problems.

However when I set the camera to chase entity it does not work. Anyone have an idea how to fix this?
retroman
 
Posts: 16
Joined: Sat Oct 13, 2012 5:00 pm

Re: ClipEntity for GLES2 (not AC)

Postby ninja_td » Wed Apr 09, 2014 11:10 am

Try this port:
https://code.google.com/p/randomtitbits ... ntity.java

I tried to emulate the one on the AnchorCenter as much as possible. The only thing still missing is I the pushing and popping of the clipping rectangles. Don't know how it will turn out.
ninja_td
 
Posts: 7
Joined: Sat Nov 24, 2012 4:56 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 7 guests