From 19867cd541b15b0c278bfce767bd7b52135c8238 Mon Sep 17 00:00:00 2001 From: recastro Date: Sun, 29 Jan 2012 11:20:45 +0100 Subject: [PATCH 01/20] Raw implementation of polygons --- .../andengine/entity/primitive/Polygon.java | 500 ++++++++++++++++++ .../andengine/entity/shape/PolygonShape.java | 115 ++++ 2 files changed, 615 insertions(+) create mode 100644 src/org/andengine/entity/primitive/Polygon.java create mode 100644 src/org/andengine/entity/shape/PolygonShape.java diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java new file mode 100644 index 000000000..1939a995f --- /dev/null +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -0,0 +1,500 @@ +package org.andengine.entity.primitive; + +import java.util.ArrayList; + +import org.andengine.engine.camera.Camera; +import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; +import org.andengine.entity.shape.PolygonShape; +import org.andengine.opengl.shader.PositionColorShaderProgram; +import org.andengine.opengl.shader.util.constants.ShaderProgramConstants; +import org.andengine.opengl.util.GLState; +import org.andengine.opengl.vbo.HighPerformanceVertexBufferObject; +import org.andengine.opengl.vbo.IVertexBufferObject; +import org.andengine.opengl.vbo.LowMemoryVertexBufferObject; +import org.andengine.opengl.vbo.VertexBufferObject.DrawType; +import org.andengine.opengl.vbo.VertexBufferObjectManager; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributes; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; + +import android.opengl.GLES20; + +/** + * + * @author Rodrigo Castro + * @since 22:10:11 - 28.01.2012 + */ +public class Polygon extends PolygonShape { + // =========================================================== + // Constants + // =========================================================== + + public static final int VERTEX_INDEX_X = 0; + public static final int VERTEX_INDEX_Y = Rectangle.VERTEX_INDEX_X + 1; + public static final int COLOR_INDEX = Rectangle.VERTEX_INDEX_Y + 1; + + public static final int VERTEX_SIZE = 2 + 1; + + public static final VertexBufferObjectAttributes VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT = new VertexBufferObjectAttributesBuilder(2) + .add(ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION, 2, GLES20.GL_FLOAT, false) + .add(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR, 4, GLES20.GL_UNSIGNED_BYTE, true) + .build(); + + // =========================================================== + // Fields + // =========================================================== + + protected final IPolygonVertexBufferObject mPolygonVertexBufferObject; + protected float[] mVertexX; + protected float[] mVertexY; + protected ArrayList mVertexTriangles; + + // =========================================================== + // Constructors + // =========================================================== + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, pVertexBufferObjectManager, DrawType.STATIC); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + private Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + super(pX, pY, PositionColorShaderProgram.getInstance()); + + mVertexX = pVertexX; + mVertexY = pVertexY; + mVertexTriangles = Triangulate.process(pVertexX, pVertexY); + assert( mVertexTriangles != null ); + assert( mVertexX.length == mVertexY.length ); + + this.mPolygonVertexBufferObject = new HighPerformancePolygonVertexBufferObject(pVertexBufferObjectManager, VERTEX_SIZE * mVertexTriangles.size(), pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); + + this.onUpdateVertices(); + this.onUpdateColor(); + + this.setBlendingEnabled(true); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + public float[] getVertexX() + { + return mVertexX; + } + + public float[] getVertexY() + { + return mVertexY; + } + + public ArrayList getTriangles() + { + return mVertexTriangles; + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public IPolygonVertexBufferObject getVertexBufferObject() { + return this.mPolygonVertexBufferObject; + } + + @Override + protected void preDraw(final GLState pGLState, final Camera pCamera) { + super.preDraw(pGLState, pCamera); + + this.mPolygonVertexBufferObject.bind(pGLState, this.mShaderProgram); + } + + @Override + protected void draw(final GLState pGLState, final Camera pCamera) { + // TODO let the user choose the mode + this.mPolygonVertexBufferObject.draw(GLES20.GL_TRIANGLES, getTriangles().size()); + } + + @Override + protected void postDraw(final GLState pGLState, final Camera pCamera) { + this.mPolygonVertexBufferObject.unbind(pGLState, this.mShaderProgram); + + super.postDraw(pGLState, pCamera); + } + + @Override + protected void onUpdateColor() { + this.mPolygonVertexBufferObject.onUpdateColor(this); + } + + @Override + protected void onUpdateVertices() { + this.mPolygonVertexBufferObject.onUpdateVertices(this); + } + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + + public static interface IPolygonVertexBufferObject extends IVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + public void onUpdateColor(final Polygon pPolygon); + public void onUpdateVertices(final Polygon pPolygon); + } + + public static class HighPerformancePolygonVertexBufferObject extends HighPerformanceVertexBufferObject implements IPolygonVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + // =========================================================== + // Constructors + // =========================================================== + + public HighPerformancePolygonVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { + super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public void onUpdateColor(final Polygon pPolygon) { + final float[] bufferData = this.mBufferData; + + final float packedColor = pPolygon.getColor().getPacked(); + + int nbVertexInTriangles = pPolygon.getTriangles().size(); + for( int i = 0; i < nbVertexInTriangles; i++) + { + // TODO use color per vertex + bufferData[i * Polygon.VERTEX_SIZE + Polygon.COLOR_INDEX] = packedColor; + } + + this.setDirtyOnHardware(); + } + + @Override + public void onUpdateVertices(final Polygon pPolygon) { + final float[] bufferData = this.mBufferData; + + ArrayList vertexTriangles = pPolygon.getTriangles(); + int nbVertexInTriangles = vertexTriangles.size(); + + for( int i = 0; i < nbVertexInTriangles; i++) + { + bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); + bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); + } + + this.setDirtyOnHardware(); + } + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } + + public static class LowMemoryPolygonVertexBufferObject extends LowMemoryVertexBufferObject implements IPolygonVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + // =========================================================== + // Constructors + // =========================================================== + + public LowMemoryPolygonVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { + super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public void onUpdateColor(final Polygon pPolygon) { + + // TODO + this.setDirtyOnHardware(); + } + + @Override + public void onUpdateVertices(final Polygon pPolygon) { + + // TODO + this.setDirtyOnHardware(); + } + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } + + public static class Vector2d + { + + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + private float mX; + private float mY; + + // =========================================================== + // Constructors + // =========================================================== + + public Vector2d(float x, float y) + { + set(x,y); + }; + + // =========================================================== + // Getter & Setter + // =========================================================== + + public float getX() + { + return mX; + } + + public float getY() + { + return mY; + } + + public void set(float x, float y) + { + mX = x; + mY = y; + } + + protected static Vector2d[] floatToList( float pX[], float pY[] ) + { + assert( pX.length == pY.length ); + Vector2d[] vertex = new Vector2d[pX.length]; + for( int i = 0; i < pX.length; i++ ) + { + vertex[i] = new Vector2d(pX[i], pY[i]); + } + return vertex; + } + } + + public static class Triangulate { + // Original code by John W. RATCLIFF + // Java port by Rodrigo Castro + + // COTD Entry submitted by John W. Ratcliff [jratcliff@verant.com] + + // ** THIS IS A CODE SNIPPET WHICH WILL EFFICIEINTLY TRIANGULATE ANY + // ** POLYGON/CONTOUR (without holes) AS A STATIC CLASS. THIS SNIPPET + // ** IS COMPRISED OF 3 FILES, TRIANGULATE.H, THE HEADER FILE FOR THE + // ** TRIANGULATE BASE CLASS, TRIANGULATE.CPP, THE IMPLEMENTATION OF + // ** THE TRIANGULATE BASE CLASS, AND TEST.CPP, A SMALL TEST PROGRAM + // ** DEMONSTRATING THE USAGE OF THE TRIANGULATOR. THE TRIANGULATE + // ** BASE CLASS ALSO PROVIDES TWO USEFUL HELPER METHODS, ONE WHICH + // ** COMPUTES THE AREA OF A POLYGON, AND ANOTHER WHICH DOES AN EFFICENT + // ** POINT IN A TRIANGLE TEST. + // ** SUBMITTED BY JOHN W. RATCLIFF (jratcliff@verant.com) July 22, 2000 + + + /*****************************************************************/ + /** Static class to triangulate any contour/polygon efficiently **/ + /** You should replace Vector2d with whatever your own Vector **/ + /** class might be. Does not support polygons with holes. **/ + /** Uses STL vectors to represent a dynamic array of vertices. **/ + /** This code snippet was submitted to FlipCode.com by **/ + /** John W. Ratcliff (jratcliff@verant.com) on July 22, 2000 **/ + /** I did not write the original code/algorithm for this **/ + /** this triangulator, in fact, I can't even remember where I **/ + /** found it in the first place. However, I did rework it into **/ + /** the following black-box static class so you can make easy **/ + /** use of it in your own code. Simply replace Vector2d with **/ + /** whatever your own Vector implementation might be. **/ + /*****************************************************************/ + + final static float EPSILON = 0.0000000001f; + + // compute area of a contour/polygon + protected static float area(final Vector2d[] contour) + { + + int n = contour.length; + + float A=0.0f; + + for(int p=n-1,q=0; q= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f)); + }; + + protected static boolean Snip(final Vector2d[] contour,int u,int v,int w,int n,int[] V) + { + int p; + float Ax, Ay, Bx, By, Cx, Cy, Px, Py; + + Ax = contour[V[u]].getX(); + Ay = contour[V[u]].getY(); + + Bx = contour[V[v]].getX(); + By = contour[V[v]].getY(); + + Cx = contour[V[w]].getX(); + Cy = contour[V[w]].getY(); + + if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) + return false; + + for (p=0;p process(final float[] pX, final float[] pY) + { + + final Vector2d[] contour = Vector2d.floatToList(pX, pY); + + /* allocate and initialize list of Vertices in polygon */ + int n = contour.length; + if ( n < 3 ) + return null; + + int[] V = new int[n]; + ArrayList result = new ArrayList(n); + /* we want a counter-clockwise polygon in V */ + + if ( 0.0f < area(contour) ) + for (int v=0; v2; ) + { + /* if we loop, it is probably a non-simple polygon */ + if (0 >= (count--)) + { + //** Triangulate: ERROR - probable bad polygon! + return null; + } + + /* three consecutive vertices in current polygon, */ + int u = v ; if (nv <= u) u = 0; /* previous */ + v = u+1; if (nv <= v) v = 0; /* new v */ + int w = v+1; if (nv <= w) w = 0; /* next */ + if ( Snip(contour,u,v,w,nv,V) ) + { + int a,b,c,s,t; + + /* true names of the vertices */ + a = V[u]; b = V[v]; c = V[w]; + /* output Triangle */ + result.add( contour[a] ); + result.add( contour[b] ); + result.add( contour[c] ); + + /* remove v from remaining polygon */ + for(s=v,t=v+1;t Date: Sun, 29 Jan 2012 14:22:20 +0100 Subject: [PATCH 02/20] Polygons' vertices may be changed on the go with the help of updateVertices --- .../andengine/entity/primitive/Polygon.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java index 1939a995f..c5e4f63a1 100644 --- a/src/org/andengine/entity/primitive/Polygon.java +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -43,7 +43,7 @@ public class Polygon extends PolygonShape { // =========================================================== // Fields // =========================================================== - + protected final IPolygonVertexBufferObject mPolygonVertexBufferObject; protected float[] mVertexX; protected float[] mVertexY; @@ -68,13 +68,14 @@ private Polygon(final float pX, final float pY, final float[] pVertexX, float[] mVertexX = pVertexX; mVertexY = pVertexY; + assert( mVertexX.length == mVertexY.length ); + mVertexTriangles = Triangulate.process(pVertexX, pVertexY); assert( mVertexTriangles != null ); - assert( mVertexX.length == mVertexY.length ); this.mPolygonVertexBufferObject = new HighPerformancePolygonVertexBufferObject(pVertexBufferObjectManager, VERTEX_SIZE * mVertexTriangles.size(), pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); - - this.onUpdateVertices(); + + onUpdateVertices(); this.onUpdateColor(); this.setBlendingEnabled(true); @@ -94,6 +95,21 @@ public float[] getVertexY() return mVertexY; } + public void updateVertices( float[] pVertexX, float[] pVertexY ) + { + mVertexX = pVertexX; + mVertexY = pVertexY; + assert( mVertexX.length == mVertexY.length ); + + int oldVertexCount = mVertexTriangles.size(); + + mVertexTriangles = Triangulate.process(pVertexX, pVertexY); + assert( mVertexTriangles != null ); + assert( oldVertexCount >= mVertexTriangles.size() ); + + onUpdateVertices(); + } + public ArrayList getTriangles() { return mVertexTriangles; From b39286cdfe99d6e3a124892e5f215388a6cf969f Mon Sep 17 00:00:00 2001 From: recastro Date: Sun, 29 Jan 2012 16:57:18 +0100 Subject: [PATCH 03/20] Fixed comment --- src/org/andengine/entity/primitive/Polygon.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java index c5e4f63a1..1d2d2119c 100644 --- a/src/org/andengine/entity/primitive/Polygon.java +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -18,6 +18,7 @@ import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; import android.opengl.GLES20; +import android.util.Log; /** * @@ -451,7 +452,7 @@ protected static boolean Snip(final Vector2d[] contour,int u,int v,int w,int n,i return true; } - // triangulate a contour/polygon, places results in STL vector + // triangulate a contour/polygon, places results in a list // as series of triangles. public static ArrayList process(final float[] pX, final float[] pY) { @@ -488,9 +489,15 @@ public static ArrayList process(final float[] pX, final float[] pY) } /* three consecutive vertices in current polygon, */ - int u = v ; if (nv <= u) u = 0; /* previous */ - v = u+1; if (nv <= v) v = 0; /* new v */ - int w = v+1; if (nv <= w) w = 0; /* next */ + int u = v ; + if (nv <= u) + u = 0; /* previous */ + v = u+1; + if (nv <= v) + v = 0; /* new v */ + int w = v+1; + if (nv <= w) + w = 0; /* next */ if ( Snip(contour,u,v,w,nv,V) ) { int a,b,c,s,t; From 457f45c53d19a1d5c6c16635fe4daf326aa82579 Mon Sep 17 00:00:00 2001 From: recastro Date: Sun, 29 Jan 2012 16:57:52 +0100 Subject: [PATCH 04/20] Fixed comment --- src/org/andengine/entity/primitive/Polygon.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java index 1d2d2119c..abbda210d 100644 --- a/src/org/andengine/entity/primitive/Polygon.java +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -18,7 +18,6 @@ import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; import android.opengl.GLES20; -import android.util.Log; /** * From 3a81267fe102d5f507df0efd5c2acd007ea5f67b Mon Sep 17 00:00:00 2001 From: recastro Date: Mon, 30 Jan 2012 13:53:34 +0100 Subject: [PATCH 05/20] Polygon may be allocated with a ratio size\n Will not crash if updated vertices can't be triangulated => Default : keep and draw the old triangles --- .../andengine/entity/primitive/Polygon.java | 59 +++++++++++++++---- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java index abbda210d..0b0efbf2d 100644 --- a/src/org/andengine/entity/primitive/Polygon.java +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -18,6 +18,7 @@ import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; import android.opengl.GLES20; +import android.util.Log; /** * @@ -29,12 +30,15 @@ public class Polygon extends PolygonShape { // Constants // =========================================================== - public static final int VERTEX_INDEX_X = 0; - public static final int VERTEX_INDEX_Y = Rectangle.VERTEX_INDEX_X + 1; - public static final int COLOR_INDEX = Rectangle.VERTEX_INDEX_Y + 1; + protected static final int VERTEX_INDEX_X = 0; + protected static final int VERTEX_INDEX_Y = Rectangle.VERTEX_INDEX_X + 1; + protected static final int COLOR_INDEX = Rectangle.VERTEX_INDEX_Y + 1; - public static final int VERTEX_SIZE = 2 + 1; + protected static final int VERTEX_SIZE = 2 + 1; + public static final float VERTEX_SIZE_DEFAULT_RATIO = 1.f; + public static final float VERTEX_SIZE_EXTRA_RATIO = 1.3f; + public static final VertexBufferObjectAttributes VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT = new VertexBufferObjectAttributesBuilder(2) .add(ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION, 2, GLES20.GL_FLOAT, false) .add(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR, 4, GLES20.GL_UNSIGNED_BYTE, true) @@ -48,6 +52,7 @@ public class Polygon extends PolygonShape { protected float[] mVertexX; protected float[] mVertexY; protected ArrayList mVertexTriangles; + protected int mCapacity; // =========================================================== // Constructors @@ -57,13 +62,21 @@ public class Polygon extends PolygonShape { * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pVertexX, pVertexY, pVertexBufferObjectManager, DrawType.STATIC); + this(pX, pY, pVertexX, pVertexY, VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, DrawType.STATIC); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, vertexSizeRatio, pVertexBufferObjectManager, DrawType.STATIC); } + /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - private Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + private Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { super(pX, pY, PositionColorShaderProgram.getInstance()); mVertexX = pVertexX; @@ -73,7 +86,8 @@ private Polygon(final float pX, final float pY, final float[] pVertexX, float[] mVertexTriangles = Triangulate.process(pVertexX, pVertexY); assert( mVertexTriangles != null ); - this.mPolygonVertexBufferObject = new HighPerformancePolygonVertexBufferObject(pVertexBufferObjectManager, VERTEX_SIZE * mVertexTriangles.size(), pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); + mCapacity = (int) (vertexSizeRatio * VERTEX_SIZE * mVertexTriangles.size()); + this.mPolygonVertexBufferObject = new HighPerformancePolygonVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); onUpdateVertices(); this.onUpdateColor(); @@ -95,19 +109,38 @@ public float[] getVertexY() return mVertexY; } - public void updateVertices( float[] pVertexX, float[] pVertexY ) + /** + * + * @param pVertexX + * @param pVertexY + * @return true if vertices were correctly updated + * false otherwise + */ + public boolean updateVertices( float[] pVertexX, float[] pVertexY ) { mVertexX = pVertexX; mVertexY = pVertexY; assert( mVertexX.length == mVertexY.length ); - int oldVertexCount = mVertexTriangles.size(); - - mVertexTriangles = Triangulate.process(pVertexX, pVertexY); - assert( mVertexTriangles != null ); - assert( oldVertexCount >= mVertexTriangles.size() ); + ArrayList vertexTriangles = Triangulate.process(pVertexX, pVertexY); + if( vertexTriangles == null ) + { + Log.e("AndEngine", "Error: Polygon - Polygon can't be triangulated. Will not update vertices"); + return false; + } + else if (mCapacity < vertexTriangles.size()) + { + Log.e("AndEngine", "Error: Polygon - Not enough space to accomodate extra triangles"); + return false; + } + else + { + mVertexTriangles = vertexTriangles; + } onUpdateVertices(); + + return true; } public ArrayList getTriangles() From c8d1a473f2e774a927aed6cd6931fc0f61f71f95 Mon Sep 17 00:00:00 2001 From: recastro Date: Mon, 30 Jan 2012 23:08:57 +0100 Subject: [PATCH 06/20] Added PolygonBase for common Polygon code. Added new PolygonLine primitive. --- .../andengine/entity/primitive/PolyLine.java | 127 ++++++ .../andengine/entity/primitive/Polygon.java | 287 +------------ .../entity/primitive/PolygonBase.java | 404 ++++++++++++++++++ 3 files changed, 539 insertions(+), 279 deletions(-) create mode 100644 src/org/andengine/entity/primitive/PolyLine.java create mode 100644 src/org/andengine/entity/primitive/PolygonBase.java diff --git a/src/org/andengine/entity/primitive/PolyLine.java b/src/org/andengine/entity/primitive/PolyLine.java new file mode 100644 index 000000000..17e7bb080 --- /dev/null +++ b/src/org/andengine/entity/primitive/PolyLine.java @@ -0,0 +1,127 @@ +package org.andengine.entity.primitive; + +import org.andengine.engine.camera.Camera; +import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; +import org.andengine.opengl.util.GLState; +import org.andengine.opengl.vbo.VertexBufferObject.DrawType; +import org.andengine.opengl.vbo.VertexBufferObjectManager; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; + +/** + * + * @author Rodrigo Castro + * @since 22:54:17 - 30.01.2012 + */ +public class PolyLine extends PolygonBase { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + protected float[] mVertexX; + protected float[] mVertexY; + private float mLineWidth; + + // =========================================================== + // Constructors + // =========================================================== + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, DrawMode.GL_LINE_LOOP); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode) { + this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, pDrawMode, DrawType.STATIC); + } + + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { + super(pX, pY, buildVector2dList(pVertexX, pVertexY), PolygonBase.VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, pDrawMode, pDrawType); + + mVertexX = pVertexX; + mVertexY = pVertexY; + mLineWidth = pLineWidth; + assert( mVertexX.length == mVertexY.length ); + + onUpdateVertices(); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + public float[] getVertexX() + { + return mVertexX; + } + + public float[] getVertexY() + { + return mVertexY; + } + + public float getLineWidth() + { + return mLineWidth; + } + + public void setLineWidth( float pLineWidth ) + { + mLineWidth = pLineWidth; + } + + /** + * + * @param pVertexX + * @param pVertexY + * @return true if vertices were correctly updated + * false otherwise + */ + public boolean updateVertices( float[] pVertexX, float[] pVertexY ) + { + mVertexX = pVertexX; + mVertexY = pVertexY; + assert( mVertexX.length == mVertexY.length ); + + return this.updateVertices(buildVector2dList(pVertexX, pVertexY)); + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + protected void preDraw(final GLState pGLState, final Camera pCamera) { + super.preDraw(pGLState, pCamera); + + pGLState.lineWidth(this.mLineWidth); + + } + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== +} diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java index 0b0efbf2d..18a3ffa2d 100644 --- a/src/org/andengine/entity/primitive/Polygon.java +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -2,22 +2,11 @@ import java.util.ArrayList; -import org.andengine.engine.camera.Camera; import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; -import org.andengine.entity.shape.PolygonShape; -import org.andengine.opengl.shader.PositionColorShaderProgram; -import org.andengine.opengl.shader.util.constants.ShaderProgramConstants; -import org.andengine.opengl.util.GLState; -import org.andengine.opengl.vbo.HighPerformanceVertexBufferObject; -import org.andengine.opengl.vbo.IVertexBufferObject; -import org.andengine.opengl.vbo.LowMemoryVertexBufferObject; import org.andengine.opengl.vbo.VertexBufferObject.DrawType; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; -import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributes; -import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; -import android.opengl.GLES20; import android.util.Log; /** @@ -25,48 +14,31 @@ * @author Rodrigo Castro * @since 22:10:11 - 28.01.2012 */ -public class Polygon extends PolygonShape { +public class Polygon extends PolygonBase { // =========================================================== // Constants // =========================================================== - protected static final int VERTEX_INDEX_X = 0; - protected static final int VERTEX_INDEX_Y = Rectangle.VERTEX_INDEX_X + 1; - protected static final int COLOR_INDEX = Rectangle.VERTEX_INDEX_Y + 1; - - protected static final int VERTEX_SIZE = 2 + 1; - - public static final float VERTEX_SIZE_DEFAULT_RATIO = 1.f; - public static final float VERTEX_SIZE_EXTRA_RATIO = 1.3f; - - public static final VertexBufferObjectAttributes VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT = new VertexBufferObjectAttributesBuilder(2) - .add(ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION, 2, GLES20.GL_FLOAT, false) - .add(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR, 4, GLES20.GL_UNSIGNED_BYTE, true) - .build(); - // =========================================================== // Fields // =========================================================== - protected final IPolygonVertexBufferObject mPolygonVertexBufferObject; protected float[] mVertexX; protected float[] mVertexY; - protected ArrayList mVertexTriangles; - protected int mCapacity; // =========================================================== // Constructors // =========================================================== /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, DrawType.STATIC); } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, vertexSizeRatio, pVertexBufferObjectManager, DrawType.STATIC); @@ -76,23 +48,14 @@ public Polygon(final float pX, final float pY, final float[] pVertexX, float[] p /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - private Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { - super(pX, pY, PositionColorShaderProgram.getInstance()); + public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + super(pX, pY, Triangulate.process(pVertexX, pVertexY), vertexSizeRatio, pVertexBufferObjectManager, DrawMode.GL_TRIANGLES, pDrawType); mVertexX = pVertexX; mVertexY = pVertexY; assert( mVertexX.length == mVertexY.length ); - - mVertexTriangles = Triangulate.process(pVertexX, pVertexY); - assert( mVertexTriangles != null ); - mCapacity = (int) (vertexSizeRatio * VERTEX_SIZE * mVertexTriangles.size()); - this.mPolygonVertexBufferObject = new HighPerformancePolygonVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); - onUpdateVertices(); - this.onUpdateColor(); - - this.setBlendingEnabled(true); } // =========================================================== @@ -122,71 +85,19 @@ public boolean updateVertices( float[] pVertexX, float[] pVertexY ) mVertexY = pVertexY; assert( mVertexX.length == mVertexY.length ); - ArrayList vertexTriangles = Triangulate.process(pVertexX, pVertexY); - if( vertexTriangles == null ) + ArrayList vertices = Triangulate.process(pVertexX, pVertexY); + if( vertices == null ) { Log.e("AndEngine", "Error: Polygon - Polygon can't be triangulated. Will not update vertices"); return false; } - else if (mCapacity < vertexTriangles.size()) - { - Log.e("AndEngine", "Error: Polygon - Not enough space to accomodate extra triangles"); - return false; - } - else - { - mVertexTriangles = vertexTriangles; - } - - onUpdateVertices(); - - return true; - } - - public ArrayList getTriangles() - { - return mVertexTriangles; + return this.updateVertices(vertices); } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== - @Override - public IPolygonVertexBufferObject getVertexBufferObject() { - return this.mPolygonVertexBufferObject; - } - - @Override - protected void preDraw(final GLState pGLState, final Camera pCamera) { - super.preDraw(pGLState, pCamera); - - this.mPolygonVertexBufferObject.bind(pGLState, this.mShaderProgram); - } - - @Override - protected void draw(final GLState pGLState, final Camera pCamera) { - // TODO let the user choose the mode - this.mPolygonVertexBufferObject.draw(GLES20.GL_TRIANGLES, getTriangles().size()); - } - - @Override - protected void postDraw(final GLState pGLState, final Camera pCamera) { - this.mPolygonVertexBufferObject.unbind(pGLState, this.mShaderProgram); - - super.postDraw(pGLState, pCamera); - } - - @Override - protected void onUpdateColor() { - this.mPolygonVertexBufferObject.onUpdateColor(this); - } - - @Override - protected void onUpdateVertices() { - this.mPolygonVertexBufferObject.onUpdateVertices(this); - } - // =========================================================== // Methods // =========================================================== @@ -194,188 +105,6 @@ protected void onUpdateVertices() { // =========================================================== // Inner and Anonymous Classes // =========================================================== - - public static interface IPolygonVertexBufferObject extends IVertexBufferObject { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Methods - // =========================================================== - - public void onUpdateColor(final Polygon pPolygon); - public void onUpdateVertices(final Polygon pPolygon); - } - - public static class HighPerformancePolygonVertexBufferObject extends HighPerformanceVertexBufferObject implements IPolygonVertexBufferObject { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - // =========================================================== - // Constructors - // =========================================================== - - public HighPerformancePolygonVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { - super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - @Override - public void onUpdateColor(final Polygon pPolygon) { - final float[] bufferData = this.mBufferData; - - final float packedColor = pPolygon.getColor().getPacked(); - - int nbVertexInTriangles = pPolygon.getTriangles().size(); - for( int i = 0; i < nbVertexInTriangles; i++) - { - // TODO use color per vertex - bufferData[i * Polygon.VERTEX_SIZE + Polygon.COLOR_INDEX] = packedColor; - } - - this.setDirtyOnHardware(); - } - - @Override - public void onUpdateVertices(final Polygon pPolygon) { - final float[] bufferData = this.mBufferData; - - ArrayList vertexTriangles = pPolygon.getTriangles(); - int nbVertexInTriangles = vertexTriangles.size(); - - for( int i = 0; i < nbVertexInTriangles; i++) - { - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); - } - - this.setDirtyOnHardware(); - } - - // =========================================================== - // Methods - // =========================================================== - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - } - - public static class LowMemoryPolygonVertexBufferObject extends LowMemoryVertexBufferObject implements IPolygonVertexBufferObject { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - // =========================================================== - // Constructors - // =========================================================== - - public LowMemoryPolygonVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { - super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - @Override - public void onUpdateColor(final Polygon pPolygon) { - - // TODO - this.setDirtyOnHardware(); - } - - @Override - public void onUpdateVertices(final Polygon pPolygon) { - - // TODO - this.setDirtyOnHardware(); - } - - // =========================================================== - // Methods - // =========================================================== - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - } - - public static class Vector2d - { - - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - private float mX; - private float mY; - - // =========================================================== - // Constructors - // =========================================================== - - public Vector2d(float x, float y) - { - set(x,y); - }; - - // =========================================================== - // Getter & Setter - // =========================================================== - - public float getX() - { - return mX; - } - - public float getY() - { - return mY; - } - - public void set(float x, float y) - { - mX = x; - mY = y; - } - - protected static Vector2d[] floatToList( float pX[], float pY[] ) - { - assert( pX.length == pY.length ); - Vector2d[] vertex = new Vector2d[pX.length]; - for( int i = 0; i < pX.length; i++ ) - { - vertex[i] = new Vector2d(pX[i], pY[i]); - } - return vertex; - } - } public static class Triangulate { // Original code by John W. RATCLIFF diff --git a/src/org/andengine/entity/primitive/PolygonBase.java b/src/org/andengine/entity/primitive/PolygonBase.java new file mode 100644 index 000000000..0676c570d --- /dev/null +++ b/src/org/andengine/entity/primitive/PolygonBase.java @@ -0,0 +1,404 @@ +package org.andengine.entity.primitive; + +import java.util.ArrayList; + +import org.andengine.engine.camera.Camera; +import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; +import org.andengine.entity.shape.PolygonShape; +import org.andengine.opengl.shader.PositionColorShaderProgram; +import org.andengine.opengl.shader.util.constants.ShaderProgramConstants; +import org.andengine.opengl.util.GLState; +import org.andengine.opengl.vbo.HighPerformanceVertexBufferObject; +import org.andengine.opengl.vbo.IVertexBufferObject; +import org.andengine.opengl.vbo.LowMemoryVertexBufferObject; +import org.andengine.opengl.vbo.VertexBufferObject.DrawType; +import org.andengine.opengl.vbo.VertexBufferObjectManager; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributes; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; + +import android.opengl.GLES20; +import android.util.Log; + +/** + * + * @author Rodrigo Castro + * @since 22:43:05 - 30.01.2012 + */ +public abstract class PolygonBase extends PolygonShape { + // =========================================================== + // Constants + // =========================================================== + + protected static final int VERTEX_INDEX_X = 0; + protected static final int VERTEX_INDEX_Y = VERTEX_INDEX_X + 1; + protected static final int COLOR_INDEX = VERTEX_INDEX_Y + 1; + + protected static final int VERTEX_SIZE = 2 + 1; + + public static final float VERTEX_SIZE_DEFAULT_RATIO = 1.f; + public static final float VERTEX_SIZE_EXTRA_RATIO = 1.3f; + + public static final VertexBufferObjectAttributes VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT = new VertexBufferObjectAttributesBuilder(2) + .add(ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION, 2, GLES20.GL_FLOAT, false) + .add(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR, 4, GLES20.GL_UNSIGNED_BYTE, true) + .build(); + + // =========================================================== + // Fields + // =========================================================== + + protected final IPolygonBaseVertexBufferObject mPolygonVertexBufferObject; + protected ArrayList mVertices; + protected int mCapacity; + protected DrawMode mDrawMode; + + // =========================================================== + // Constructors + // =========================================================== + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + protected PolygonBase(final float pX, final float pY, ArrayList pVertices, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawMode pDrawMode, final DrawType pDrawType) { + super(pX, pY, PositionColorShaderProgram.getInstance()); + + mDrawMode = pDrawMode; + mVertices = pVertices; + mCapacity = (int) (vertexSizeRatio * VERTEX_SIZE * pVertices.size()); + this.mPolygonVertexBufferObject = new HighPerformancePolygonBaseVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); + + onUpdateVertices(); + this.onUpdateColor(); + + this.setBlendingEnabled(true); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + /** + * + * @param pVertexX + * @param pVertexY + * @return true if vertices were correctly updated + * false otherwise + */ + protected boolean updateVertices( ArrayList pVertices ) + { + + if (mCapacity < pVertices.size()) + { + Log.e("AndEngine", "Error: Polygon - Not enough space to accomodate extra vertices"); + return false; + } + else + { + mVertices = pVertices; + } + + onUpdateVertices(); + + return true; + } + + protected ArrayList getVertices() + { + return mVertices; + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public IPolygonBaseVertexBufferObject getVertexBufferObject() { + return this.mPolygonVertexBufferObject; + } + + @Override + protected void preDraw(final GLState pGLState, final Camera pCamera) { + super.preDraw(pGLState, pCamera); + + this.mPolygonVertexBufferObject.bind(pGLState, this.mShaderProgram); + } + + @Override + protected void draw(final GLState pGLState, final Camera pCamera) { + this.mPolygonVertexBufferObject.draw(mDrawMode.getUsage(), getVertices().size()); + } + + @Override + protected void postDraw(final GLState pGLState, final Camera pCamera) { + this.mPolygonVertexBufferObject.unbind(pGLState, this.mShaderProgram); + + super.postDraw(pGLState, pCamera); + } + + @Override + protected void onUpdateColor() { + this.mPolygonVertexBufferObject.onUpdateColor(this); + } + + @Override + protected void onUpdateVertices() { + this.mPolygonVertexBufferObject.onUpdateVertices(this); + } + + // =========================================================== + // Methods + // =========================================================== + + protected static ArrayList buildVector2dList(float[] pVertexX, float[] pVertexY) + { + assert( pVertexX.length == pVertexY.length ); + + ArrayList vertices = new ArrayList(pVertexX.length); + for( int i = 0; i < pVertexX.length; i++) + { + Vector2d v = new Vector2d(pVertexX[i], pVertexY[i]); + vertices.add(v); + } + return vertices; + } + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + + public static interface IPolygonBaseVertexBufferObject extends IVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + public void onUpdateColor(final PolygonBase pPolygon); + public void onUpdateVertices(final PolygonBase pPolygon); + } + + public static class HighPerformancePolygonBaseVertexBufferObject extends HighPerformanceVertexBufferObject implements IPolygonBaseVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + // =========================================================== + // Constructors + // =========================================================== + + public HighPerformancePolygonBaseVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { + super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public void onUpdateColor(final PolygonBase pPolygon) { + final float[] bufferData = this.mBufferData; + + final float packedColor = pPolygon.getColor().getPacked(); + + int nbVertexInTriangles = pPolygon.getVertices().size(); + for( int i = 0; i < nbVertexInTriangles; i++) + { + // TODO use color per vertex + bufferData[i * Polygon.VERTEX_SIZE + Polygon.COLOR_INDEX] = packedColor; + } + + this.setDirtyOnHardware(); + } + + @Override + public void onUpdateVertices(final PolygonBase pPolygon) { + final float[] bufferData = this.mBufferData; + + ArrayList vertexTriangles = pPolygon.getVertices(); + int nbVertexInTriangles = vertexTriangles.size(); + + for( int i = 0; i < nbVertexInTriangles; i++) + { + bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); + bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); + } + + this.setDirtyOnHardware(); + } + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } + + public static class LowMemoryPolygonBaseVertexBufferObject extends LowMemoryVertexBufferObject implements IPolygonBaseVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + // =========================================================== + // Constructors + // =========================================================== + + public LowMemoryPolygonBaseVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { + super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public void onUpdateColor(final PolygonBase pPolygon) { + + // TODO + this.setDirtyOnHardware(); + } + + @Override + public void onUpdateVertices(final PolygonBase pPolygon) { + + // TODO + this.setDirtyOnHardware(); + } + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } + + protected static class Vector2d + { + + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + private float mX; + private float mY; + + // =========================================================== + // Constructors + // =========================================================== + + public Vector2d(float x, float y) + { + set(x,y); + }; + + // =========================================================== + // Getter & Setter + // =========================================================== + + public float getX() + { + return mX; + } + + public float getY() + { + return mY; + } + + public void set(float x, float y) + { + mX = x; + mY = y; + } + + protected static Vector2d[] floatToList( float pX[], float pY[] ) + { + assert( pX.length == pY.length ); + Vector2d[] vertex = new Vector2d[pX.length]; + for( int i = 0; i < pX.length; i++ ) + { + vertex[i] = new Vector2d(pX[i], pY[i]); + } + return vertex; + } + } + + public static enum DrawMode { + // =========================================================== + // Elements + // =========================================================== + + GL_POINTS(GLES20.GL_POINTS), + GL_LINE_STRIP(GLES20.GL_LINE_STRIP), + GL_LINE_LOOP(GLES20.GL_LINE_LOOP), + GL_LINES(GLES20.GL_LINES), + GL_TRIANGLE_STRIP(GLES20.GL_TRIANGLE_STRIP), + GL_TRIANGLE_FAN(GLES20.GL_TRIANGLE_FAN), + GL_TRIANGLES(GLES20.GL_TRIANGLES); + + + // =========================================================== + // Constants + // =========================================================== + + private final int mUsage; + + // =========================================================== + // Fields + // =========================================================== + + // =========================================================== + // Constructors + // =========================================================== + + private DrawMode(final int pUsage) { + this.mUsage = pUsage; + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + public int getUsage() { + return this.mUsage; + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } +} From 282e49af4cfb7a3ba40c80b72d3076f7b7052de9 Mon Sep 17 00:00:00 2001 From: recastro Date: Tue, 31 Jan 2012 13:28:12 +0100 Subject: [PATCH 07/20] New Ellipse primitive Added Ellipse primitive -> based on Polyline so it can't be filled Removed useless float[] mVertexX,Y --- .../andengine/entity/primitive/Ellipse.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/org/andengine/entity/primitive/Ellipse.java diff --git a/src/org/andengine/entity/primitive/Ellipse.java b/src/org/andengine/entity/primitive/Ellipse.java new file mode 100644 index 000000000..2d5fbce03 --- /dev/null +++ b/src/org/andengine/entity/primitive/Ellipse.java @@ -0,0 +1,113 @@ +package org.andengine.entity.primitive; + +import java.util.ArrayList; + +import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; +import org.andengine.opengl.vbo.VertexBufferObject.DrawType; +import org.andengine.opengl.vbo.VertexBufferObjectManager; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; + +/** + * + * @author Rodrigo Castro + * @since 16:47:01 - 31.01.2012 + */ +public class Ellipse extends PolyLine { + // =========================================================== + // Constants + // =========================================================== + + static final int LOW_RESOLUTION = 15; + static final int MEDIUM_RESOLUTION = 30; + static final int HIGH_RESOLUTION = 50; + static final int DEFAULT_RESOLUTION = HIGH_RESOLUTION; + + // =========================================================== + // Fields + // =========================================================== + + protected final int mResolution; + + // =========================================================== + // Constructors + // =========================================================== + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pRadiusA, pRadiusB, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pRadiusA, pRadiusB, pLineWidth, DEFAULT_RESOLUTION, pVertexBufferObjectManager); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, DrawMode.GL_LINE_LOOP); + } + + public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode) { + this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, pDrawMode, DrawType.STATIC); + } + + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { + super(pX, pY, buildEllipseVertices(pRadiusA, pRadiusB, pResolution), pLineWidth, pVertexBufferObjectManager, pDrawMode, pDrawType); + + mResolution = pResolution; + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + /** + * + * @param pRadiusA + * @param pRadiusB + * @return true if vertices were correctly updated + * false otherwise + */ + public boolean setRadius( float pRadiusA, float pRadiusB ) + { + return this.updateVertices(buildEllipseVertices(pRadiusA, pRadiusB, mResolution)); + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + private static ArrayList buildEllipseVertices(float pRadiusA, float pRadiusB, int pResolution) { + + ArrayList vertices = new ArrayList(pResolution); + + for( int i = 0; i < pResolution; i++) + { + double theta = 2. * Math.PI * (double)i / (double) pResolution; + float x = (float) ( (double)pRadiusA * Math.cos( theta )); + float y = (float) ( (double)pRadiusB * Math.sin( theta )); + + vertices.add(new Vector2d(x, y)); + } + + return vertices; + } + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== +} \ No newline at end of file From cea94cb7977a6b72978e4f5e277cbc1f150f3c89 Mon Sep 17 00:00:00 2001 From: recastro Date: Wed, 1 Feb 2012 12:48:24 +0100 Subject: [PATCH 08/20] Added missing changes in previous commit : new Constructors --- .../andengine/entity/primitive/PolyLine.java | 40 +++++++++---------- .../andengine/entity/primitive/Polygon.java | 4 +- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/org/andengine/entity/primitive/PolyLine.java b/src/org/andengine/entity/primitive/PolyLine.java index 17e7bb080..f8be8bf19 100644 --- a/src/org/andengine/entity/primitive/PolyLine.java +++ b/src/org/andengine/entity/primitive/PolyLine.java @@ -1,5 +1,7 @@ package org.andengine.entity.primitive; +import java.util.ArrayList; + import org.andengine.engine.camera.Camera; import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; import org.andengine.opengl.util.GLState; @@ -21,8 +23,6 @@ public class PolyLine extends PolygonBase { // Fields // =========================================================== - protected float[] mVertexX; - protected float[] mVertexY; private float mLineWidth; // =========================================================== @@ -32,21 +32,21 @@ public class PolyLine extends PolygonBase { /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { + public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager); } /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) { + public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, DrawMode.GL_LINE_LOOP); } /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode) { + public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode) { this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, pDrawMode, DrawType.STATIC); } @@ -55,12 +55,20 @@ public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { - super(pX, pY, buildVector2dList(pVertexX, pVertexY), PolygonBase.VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, pDrawMode, pDrawType); + this(pX, pY, buildVector2dList(pVertexX, pVertexY), PolygonBase.VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, pDrawMode, pDrawType); + + mLineWidth = pLineWidth; + + onUpdateVertices(); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public PolyLine(final float pX, final float pY, final ArrayList pVertices, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { + super(pX, pY, pVertices, PolygonBase.VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, pDrawMode, pDrawType); - mVertexX = pVertexX; - mVertexY = pVertexY; mLineWidth = pLineWidth; - assert( mVertexX.length == mVertexY.length ); onUpdateVertices(); } @@ -69,16 +77,6 @@ public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] // Getter & Setter // =========================================================== - public float[] getVertexX() - { - return mVertexX; - } - - public float[] getVertexY() - { - return mVertexY; - } - public float getLineWidth() { return mLineWidth; @@ -98,9 +96,7 @@ public void setLineWidth( float pLineWidth ) */ public boolean updateVertices( float[] pVertexX, float[] pVertexY ) { - mVertexX = pVertexX; - mVertexY = pVertexY; - assert( mVertexX.length == mVertexY.length ); + assert( pVertexX.length == pVertexY.length ); return this.updateVertices(buildVector2dList(pVertexX, pVertexY)); } diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java index 18a3ffa2d..e089bec15 100644 --- a/src/org/andengine/entity/primitive/Polygon.java +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -33,14 +33,14 @@ public class Polygon extends PolygonBase { /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { + public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, DrawType.STATIC); } /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager) { + public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, vertexSizeRatio, pVertexBufferObjectManager, DrawType.STATIC); } From e7ba05269810d23fae2ba83ac9d79a76f3982e08 Mon Sep 17 00:00:00 2001 From: recastro Date: Mon, 6 Feb 2012 21:37:47 +0100 Subject: [PATCH 09/20] Merged && fixed build --- src/org/andengine/entity/primitive/PolygonBase.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/andengine/entity/primitive/PolygonBase.java b/src/org/andengine/entity/primitive/PolygonBase.java index 0676c570d..575c3e465 100644 --- a/src/org/andengine/entity/primitive/PolygonBase.java +++ b/src/org/andengine/entity/primitive/PolygonBase.java @@ -6,7 +6,7 @@ import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; import org.andengine.entity.shape.PolygonShape; import org.andengine.opengl.shader.PositionColorShaderProgram; -import org.andengine.opengl.shader.util.constants.ShaderProgramConstants; +import org.andengine.opengl.shader.constants.ShaderProgramConstants; import org.andengine.opengl.util.GLState; import org.andengine.opengl.vbo.HighPerformanceVertexBufferObject; import org.andengine.opengl.vbo.IVertexBufferObject; @@ -209,7 +209,7 @@ public HighPerformancePolygonBaseVertexBufferObject(final VertexBufferObjectMana public void onUpdateColor(final PolygonBase pPolygon) { final float[] bufferData = this.mBufferData; - final float packedColor = pPolygon.getColor().getPacked(); + final float packedColor = pPolygon.getColor().getFloatPacked(); int nbVertexInTriangles = pPolygon.getVertices().size(); for( int i = 0; i < nbVertexInTriangles; i++) From 408fb0266bba2310a2be61aa2d083031fb0f147b Mon Sep 17 00:00:00 2001 From: recastro Date: Fri, 10 Feb 2012 19:02:57 +0100 Subject: [PATCH 10/20] Renamed Polygon -> Polygon2 to avoid merge conflict --- .../andengine/entity/primitive/Polygon.java | 284 ------------------ .../entity/primitive/PolygonBase.java | 8 +- 2 files changed, 4 insertions(+), 288 deletions(-) delete mode 100644 src/org/andengine/entity/primitive/Polygon.java diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java deleted file mode 100644 index e089bec15..000000000 --- a/src/org/andengine/entity/primitive/Polygon.java +++ /dev/null @@ -1,284 +0,0 @@ -package org.andengine.entity.primitive; - -import java.util.ArrayList; - -import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; -import org.andengine.opengl.vbo.VertexBufferObject.DrawType; -import org.andengine.opengl.vbo.VertexBufferObjectManager; -import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; - -import android.util.Log; - -/** - * - * @author Rodrigo Castro - * @since 22:10:11 - 28.01.2012 - */ -public class Polygon extends PolygonBase { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - protected float[] mVertexX; - protected float[] mVertexY; - - // =========================================================== - // Constructors - // =========================================================== - - /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. - */ - public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pVertexX, pVertexY, VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, DrawType.STATIC); - } - - /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. - */ - public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pVertexX, pVertexY, vertexSizeRatio, pVertexBufferObjectManager, DrawType.STATIC); - } - - - /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. - */ - public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { - super(pX, pY, Triangulate.process(pVertexX, pVertexY), vertexSizeRatio, pVertexBufferObjectManager, DrawMode.GL_TRIANGLES, pDrawType); - - mVertexX = pVertexX; - mVertexY = pVertexY; - assert( mVertexX.length == mVertexY.length ); - - onUpdateVertices(); - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - public float[] getVertexX() - { - return mVertexX; - } - - public float[] getVertexY() - { - return mVertexY; - } - - /** - * - * @param pVertexX - * @param pVertexY - * @return true if vertices were correctly updated - * false otherwise - */ - public boolean updateVertices( float[] pVertexX, float[] pVertexY ) - { - mVertexX = pVertexX; - mVertexY = pVertexY; - assert( mVertexX.length == mVertexY.length ); - - ArrayList vertices = Triangulate.process(pVertexX, pVertexY); - if( vertices == null ) - { - Log.e("AndEngine", "Error: Polygon - Polygon can't be triangulated. Will not update vertices"); - return false; - } - return this.updateVertices(vertices); - } - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - // =========================================================== - // Methods - // =========================================================== - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - - public static class Triangulate { - // Original code by John W. RATCLIFF - // Java port by Rodrigo Castro - - // COTD Entry submitted by John W. Ratcliff [jratcliff@verant.com] - - // ** THIS IS A CODE SNIPPET WHICH WILL EFFICIEINTLY TRIANGULATE ANY - // ** POLYGON/CONTOUR (without holes) AS A STATIC CLASS. THIS SNIPPET - // ** IS COMPRISED OF 3 FILES, TRIANGULATE.H, THE HEADER FILE FOR THE - // ** TRIANGULATE BASE CLASS, TRIANGULATE.CPP, THE IMPLEMENTATION OF - // ** THE TRIANGULATE BASE CLASS, AND TEST.CPP, A SMALL TEST PROGRAM - // ** DEMONSTRATING THE USAGE OF THE TRIANGULATOR. THE TRIANGULATE - // ** BASE CLASS ALSO PROVIDES TWO USEFUL HELPER METHODS, ONE WHICH - // ** COMPUTES THE AREA OF A POLYGON, AND ANOTHER WHICH DOES AN EFFICENT - // ** POINT IN A TRIANGLE TEST. - // ** SUBMITTED BY JOHN W. RATCLIFF (jratcliff@verant.com) July 22, 2000 - - - /*****************************************************************/ - /** Static class to triangulate any contour/polygon efficiently **/ - /** You should replace Vector2d with whatever your own Vector **/ - /** class might be. Does not support polygons with holes. **/ - /** Uses STL vectors to represent a dynamic array of vertices. **/ - /** This code snippet was submitted to FlipCode.com by **/ - /** John W. Ratcliff (jratcliff@verant.com) on July 22, 2000 **/ - /** I did not write the original code/algorithm for this **/ - /** this triangulator, in fact, I can't even remember where I **/ - /** found it in the first place. However, I did rework it into **/ - /** the following black-box static class so you can make easy **/ - /** use of it in your own code. Simply replace Vector2d with **/ - /** whatever your own Vector implementation might be. **/ - /*****************************************************************/ - - final static float EPSILON = 0.0000000001f; - - // compute area of a contour/polygon - protected static float area(final Vector2d[] contour) - { - - int n = contour.length; - - float A=0.0f; - - for(int p=n-1,q=0; q= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f)); - }; - - protected static boolean Snip(final Vector2d[] contour,int u,int v,int w,int n,int[] V) - { - int p; - float Ax, Ay, Bx, By, Cx, Cy, Px, Py; - - Ax = contour[V[u]].getX(); - Ay = contour[V[u]].getY(); - - Bx = contour[V[v]].getX(); - By = contour[V[v]].getY(); - - Cx = contour[V[w]].getX(); - Cy = contour[V[w]].getY(); - - if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) - return false; - - for (p=0;p process(final float[] pX, final float[] pY) - { - - final Vector2d[] contour = Vector2d.floatToList(pX, pY); - - /* allocate and initialize list of Vertices in polygon */ - int n = contour.length; - if ( n < 3 ) - return null; - - int[] V = new int[n]; - ArrayList result = new ArrayList(n); - /* we want a counter-clockwise polygon in V */ - - if ( 0.0f < area(contour) ) - for (int v=0; v2; ) - { - /* if we loop, it is probably a non-simple polygon */ - if (0 >= (count--)) - { - //** Triangulate: ERROR - probable bad polygon! - return null; - } - - /* three consecutive vertices in current polygon, */ - int u = v ; - if (nv <= u) - u = 0; /* previous */ - v = u+1; - if (nv <= v) - v = 0; /* new v */ - int w = v+1; - if (nv <= w) - w = 0; /* next */ - if ( Snip(contour,u,v,w,nv,V) ) - { - int a,b,c,s,t; - - /* true names of the vertices */ - a = V[u]; b = V[v]; c = V[w]; - /* output Triangle */ - result.add( contour[a] ); - result.add( contour[b] ); - result.add( contour[c] ); - - /* remove v from remaining polygon */ - for(s=v,t=v+1;t pVerti mDrawMode = pDrawMode; mVertices = pVertices; mCapacity = (int) (vertexSizeRatio * VERTEX_SIZE * pVertices.size()); - this.mPolygonVertexBufferObject = new HighPerformancePolygonBaseVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); + this.mPolygonVertexBufferObject = new HighPerformancePolygonBaseVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon2.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); onUpdateVertices(); this.onUpdateColor(); @@ -215,7 +215,7 @@ public void onUpdateColor(final PolygonBase pPolygon) { for( int i = 0; i < nbVertexInTriangles; i++) { // TODO use color per vertex - bufferData[i * Polygon.VERTEX_SIZE + Polygon.COLOR_INDEX] = packedColor; + bufferData[i * Polygon2.VERTEX_SIZE + Polygon2.COLOR_INDEX] = packedColor; } this.setDirtyOnHardware(); @@ -230,8 +230,8 @@ public void onUpdateVertices(final PolygonBase pPolygon) { for( int i = 0; i < nbVertexInTriangles; i++) { - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); + bufferData[i * Polygon2.VERTEX_SIZE + Polygon2.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); + bufferData[i * Polygon2.VERTEX_SIZE + Polygon2.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); } this.setDirtyOnHardware(); From 5fc8561cce4ba500a1840121c65313d6c01c71d5 Mon Sep 17 00:00:00 2001 From: recastro Date: Sat, 11 Feb 2012 10:54:20 +0100 Subject: [PATCH 11/20] Ellipse and PolyLine use Mesh, renamed Polygon2 -> Polygon --- .../andengine/entity/primitive/Ellipse.java | 11 +- src/org/andengine/entity/primitive/Mesh.java | 20 ++ .../andengine/entity/primitive/PolyLine.java | 24 +- .../andengine/entity/primitive/Polygon.java | 284 ++++++++++++++++++ .../entity/primitive/PolygonBase.java | 8 +- 5 files changed, 324 insertions(+), 23 deletions(-) create mode 100644 src/org/andengine/entity/primitive/Polygon.java diff --git a/src/org/andengine/entity/primitive/Ellipse.java b/src/org/andengine/entity/primitive/Ellipse.java index 2d5fbce03..101dd99bc 100644 --- a/src/org/andengine/entity/primitive/Ellipse.java +++ b/src/org/andengine/entity/primitive/Ellipse.java @@ -50,7 +50,7 @@ public Ellipse(final float pX, final float pY, final float pRadiusA, final float * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, DrawMode.GL_LINE_LOOP); + this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, DrawMode.LINE_LOOP); } public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode) { @@ -78,10 +78,12 @@ public Ellipse(final float pX, final float pY, final float pRadiusA, final float * @return true if vertices were correctly updated * false otherwise */ + /* public boolean setRadius( float pRadiusA, float pRadiusB ) { return this.updateVertices(buildEllipseVertices(pRadiusA, pRadiusB, mResolution)); } + */ // =========================================================== // Methods for/from SuperClass/Interfaces @@ -91,9 +93,9 @@ public boolean setRadius( float pRadiusA, float pRadiusB ) // Methods // =========================================================== - private static ArrayList buildEllipseVertices(float pRadiusA, float pRadiusB, int pResolution) { + private static float[] buildEllipseVertices(float pRadiusA, float pRadiusB, int pResolution) { - ArrayList vertices = new ArrayList(pResolution); + float[] vertices = new float[VERTEX_SIZE * pResolution]; for( int i = 0; i < pResolution; i++) { @@ -101,7 +103,8 @@ private static ArrayList buildEllipseVertices(float pRadiusA, float pR float x = (float) ( (double)pRadiusA * Math.cos( theta )); float y = (float) ( (double)pRadiusB * Math.sin( theta )); - vertices.add(new Vector2d(x, y)); + vertices[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_X] = x; + vertices[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_Y] = y; } return vertices; diff --git a/src/org/andengine/entity/primitive/Mesh.java b/src/org/andengine/entity/primitive/Mesh.java index 715a4e0e6..a5ef089fa 100644 --- a/src/org/andengine/entity/primitive/Mesh.java +++ b/src/org/andengine/entity/primitive/Mesh.java @@ -65,6 +65,13 @@ public Mesh(final float pX, final float pY, final float[] pBufferData, final int public Mesh(final float pX, final float pY, final float[] pBufferData, final int pVertexCount, final DrawMode pDrawMode, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { this(pX, pY, pVertexCount, pDrawMode, new HighPerformanceMeshVertexBufferObject(pVertexBufferObjectManager, pBufferData, pVertexCount, pDrawType, true, Mesh.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT)); } + + /** + * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Mesh(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final DrawMode pDrawMode, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + this(pX, pY, pVertexX.length, pDrawMode, new HighPerformanceMeshVertexBufferObject(pVertexBufferObjectManager, buildVertexList(pVertexX, pVertexY), pVertexX.length, pDrawType, true, Mesh.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT)); + } public Mesh(final float pX, final float pY, final int pVertexCount, final DrawMode pDrawMode, final IMeshVertexBufferObject pMeshVertexBufferObject) { super(pX, pY, PositionColorShaderProgram.getInstance()); @@ -155,6 +162,19 @@ public boolean collidesWith(final IShape pOtherShape) { // Methods // =========================================================== + protected static float[] buildVertexList(float[] pVertexX, float[] pVertexY) + { + assert( pVertexX.length == pVertexY.length ); + + float[] vertices = new float[VERTEX_SIZE * pVertexX.length]; + for( int i = 0; i < pVertexX.length; i++) + { + vertices[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_X] = pVertexX[i]; + vertices[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_Y] = pVertexY[i]; + } + return vertices; + } + // =========================================================== // Inner and Anonymous Classes // =========================================================== diff --git a/src/org/andengine/entity/primitive/PolyLine.java b/src/org/andengine/entity/primitive/PolyLine.java index f8be8bf19..6407f2f65 100644 --- a/src/org/andengine/entity/primitive/PolyLine.java +++ b/src/org/andengine/entity/primitive/PolyLine.java @@ -14,7 +14,7 @@ * @author Rodrigo Castro * @since 22:54:17 - 30.01.2012 */ -public class PolyLine extends PolygonBase { +public class PolyLine extends Mesh { // =========================================================== // Constants // =========================================================== @@ -40,7 +40,7 @@ public PolyLine(final float pX, final float pY, final float[] pVertexX, final fl * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, DrawMode.GL_LINE_LOOP); + this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, DrawMode.LINE_LOOP); } /** @@ -54,23 +54,17 @@ public PolyLine(final float pX, final float pY, final float[] pVertexX, final fl /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public PolyLine(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { - this(pX, pY, buildVector2dList(pVertexX, pVertexY), PolygonBase.VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, pDrawMode, pDrawType); - - mLineWidth = pLineWidth; - - onUpdateVertices(); + public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { + this(pX, pY, buildVertexList(pVertexX, pVertexY), pLineWidth, pVertexBufferObjectManager, pDrawMode, pDrawType); } /** * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public PolyLine(final float pX, final float pY, final ArrayList pVertices, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { - super(pX, pY, pVertices, PolygonBase.VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, pDrawMode, pDrawType); + public PolyLine(final float pX, final float pY, final float[] pBufferData, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { + super(pX, pY, pBufferData, pBufferData.length / VERTEX_SIZE, pDrawMode, pVertexBufferObjectManager, pDrawType); mLineWidth = pLineWidth; - - onUpdateVertices(); } // =========================================================== @@ -94,12 +88,12 @@ public void setLineWidth( float pLineWidth ) * @return true if vertices were correctly updated * false otherwise */ - public boolean updateVertices( float[] pVertexX, float[] pVertexY ) + /*public boolean updateVertices( float[] pVertexX, float[] pVertexY ) { assert( pVertexX.length == pVertexY.length ); - return this.updateVertices(buildVector2dList(pVertexX, pVertexY)); - } + return this.updateVertices(buildVertexList(pVertexX, pVertexY)); + }*/ // =========================================================== // Methods for/from SuperClass/Interfaces diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java new file mode 100644 index 000000000..e089bec15 --- /dev/null +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -0,0 +1,284 @@ +package org.andengine.entity.primitive; + +import java.util.ArrayList; + +import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; +import org.andengine.opengl.vbo.VertexBufferObject.DrawType; +import org.andengine.opengl.vbo.VertexBufferObjectManager; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; + +import android.util.Log; + +/** + * + * @author Rodrigo Castro + * @since 22:10:11 - 28.01.2012 + */ +public class Polygon extends PolygonBase { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + protected float[] mVertexX; + protected float[] mVertexY; + + // =========================================================== + // Constructors + // =========================================================== + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, DrawType.STATIC); + } + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, vertexSizeRatio, pVertexBufferObjectManager, DrawType.STATIC); + } + + + /** + * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + super(pX, pY, Triangulate.process(pVertexX, pVertexY), vertexSizeRatio, pVertexBufferObjectManager, DrawMode.GL_TRIANGLES, pDrawType); + + mVertexX = pVertexX; + mVertexY = pVertexY; + assert( mVertexX.length == mVertexY.length ); + + onUpdateVertices(); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + public float[] getVertexX() + { + return mVertexX; + } + + public float[] getVertexY() + { + return mVertexY; + } + + /** + * + * @param pVertexX + * @param pVertexY + * @return true if vertices were correctly updated + * false otherwise + */ + public boolean updateVertices( float[] pVertexX, float[] pVertexY ) + { + mVertexX = pVertexX; + mVertexY = pVertexY; + assert( mVertexX.length == mVertexY.length ); + + ArrayList vertices = Triangulate.process(pVertexX, pVertexY); + if( vertices == null ) + { + Log.e("AndEngine", "Error: Polygon - Polygon can't be triangulated. Will not update vertices"); + return false; + } + return this.updateVertices(vertices); + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + + public static class Triangulate { + // Original code by John W. RATCLIFF + // Java port by Rodrigo Castro + + // COTD Entry submitted by John W. Ratcliff [jratcliff@verant.com] + + // ** THIS IS A CODE SNIPPET WHICH WILL EFFICIEINTLY TRIANGULATE ANY + // ** POLYGON/CONTOUR (without holes) AS A STATIC CLASS. THIS SNIPPET + // ** IS COMPRISED OF 3 FILES, TRIANGULATE.H, THE HEADER FILE FOR THE + // ** TRIANGULATE BASE CLASS, TRIANGULATE.CPP, THE IMPLEMENTATION OF + // ** THE TRIANGULATE BASE CLASS, AND TEST.CPP, A SMALL TEST PROGRAM + // ** DEMONSTRATING THE USAGE OF THE TRIANGULATOR. THE TRIANGULATE + // ** BASE CLASS ALSO PROVIDES TWO USEFUL HELPER METHODS, ONE WHICH + // ** COMPUTES THE AREA OF A POLYGON, AND ANOTHER WHICH DOES AN EFFICENT + // ** POINT IN A TRIANGLE TEST. + // ** SUBMITTED BY JOHN W. RATCLIFF (jratcliff@verant.com) July 22, 2000 + + + /*****************************************************************/ + /** Static class to triangulate any contour/polygon efficiently **/ + /** You should replace Vector2d with whatever your own Vector **/ + /** class might be. Does not support polygons with holes. **/ + /** Uses STL vectors to represent a dynamic array of vertices. **/ + /** This code snippet was submitted to FlipCode.com by **/ + /** John W. Ratcliff (jratcliff@verant.com) on July 22, 2000 **/ + /** I did not write the original code/algorithm for this **/ + /** this triangulator, in fact, I can't even remember where I **/ + /** found it in the first place. However, I did rework it into **/ + /** the following black-box static class so you can make easy **/ + /** use of it in your own code. Simply replace Vector2d with **/ + /** whatever your own Vector implementation might be. **/ + /*****************************************************************/ + + final static float EPSILON = 0.0000000001f; + + // compute area of a contour/polygon + protected static float area(final Vector2d[] contour) + { + + int n = contour.length; + + float A=0.0f; + + for(int p=n-1,q=0; q= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f)); + }; + + protected static boolean Snip(final Vector2d[] contour,int u,int v,int w,int n,int[] V) + { + int p; + float Ax, Ay, Bx, By, Cx, Cy, Px, Py; + + Ax = contour[V[u]].getX(); + Ay = contour[V[u]].getY(); + + Bx = contour[V[v]].getX(); + By = contour[V[v]].getY(); + + Cx = contour[V[w]].getX(); + Cy = contour[V[w]].getY(); + + if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) + return false; + + for (p=0;p process(final float[] pX, final float[] pY) + { + + final Vector2d[] contour = Vector2d.floatToList(pX, pY); + + /* allocate and initialize list of Vertices in polygon */ + int n = contour.length; + if ( n < 3 ) + return null; + + int[] V = new int[n]; + ArrayList result = new ArrayList(n); + /* we want a counter-clockwise polygon in V */ + + if ( 0.0f < area(contour) ) + for (int v=0; v2; ) + { + /* if we loop, it is probably a non-simple polygon */ + if (0 >= (count--)) + { + //** Triangulate: ERROR - probable bad polygon! + return null; + } + + /* three consecutive vertices in current polygon, */ + int u = v ; + if (nv <= u) + u = 0; /* previous */ + v = u+1; + if (nv <= v) + v = 0; /* new v */ + int w = v+1; + if (nv <= w) + w = 0; /* next */ + if ( Snip(contour,u,v,w,nv,V) ) + { + int a,b,c,s,t; + + /* true names of the vertices */ + a = V[u]; b = V[v]; c = V[w]; + /* output Triangle */ + result.add( contour[a] ); + result.add( contour[b] ); + result.add( contour[c] ); + + /* remove v from remaining polygon */ + for(s=v,t=v+1;t pVerti mDrawMode = pDrawMode; mVertices = pVertices; mCapacity = (int) (vertexSizeRatio * VERTEX_SIZE * pVertices.size()); - this.mPolygonVertexBufferObject = new HighPerformancePolygonBaseVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon2.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); + this.mPolygonVertexBufferObject = new HighPerformancePolygonBaseVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); onUpdateVertices(); this.onUpdateColor(); @@ -215,7 +215,7 @@ public void onUpdateColor(final PolygonBase pPolygon) { for( int i = 0; i < nbVertexInTriangles; i++) { // TODO use color per vertex - bufferData[i * Polygon2.VERTEX_SIZE + Polygon2.COLOR_INDEX] = packedColor; + bufferData[i * Polygon.VERTEX_SIZE + Polygon.COLOR_INDEX] = packedColor; } this.setDirtyOnHardware(); @@ -230,8 +230,8 @@ public void onUpdateVertices(final PolygonBase pPolygon) { for( int i = 0; i < nbVertexInTriangles; i++) { - bufferData[i * Polygon2.VERTEX_SIZE + Polygon2.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); - bufferData[i * Polygon2.VERTEX_SIZE + Polygon2.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); + bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); + bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); } this.setDirtyOnHardware(); From e846f7fc09299306a0bd47e563d95834518aa824 Mon Sep 17 00:00:00 2001 From: recastro Date: Sat, 11 Feb 2012 10:57:12 +0100 Subject: [PATCH 12/20] Removed unused imports --- src/org/andengine/entity/primitive/Ellipse.java | 2 -- src/org/andengine/entity/primitive/PolyLine.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/org/andengine/entity/primitive/Ellipse.java b/src/org/andengine/entity/primitive/Ellipse.java index 101dd99bc..fdeb0d187 100644 --- a/src/org/andengine/entity/primitive/Ellipse.java +++ b/src/org/andengine/entity/primitive/Ellipse.java @@ -1,7 +1,5 @@ package org.andengine.entity.primitive; -import java.util.ArrayList; - import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; import org.andengine.opengl.vbo.VertexBufferObject.DrawType; import org.andengine.opengl.vbo.VertexBufferObjectManager; diff --git a/src/org/andengine/entity/primitive/PolyLine.java b/src/org/andengine/entity/primitive/PolyLine.java index 6407f2f65..1d955ef9a 100644 --- a/src/org/andengine/entity/primitive/PolyLine.java +++ b/src/org/andengine/entity/primitive/PolyLine.java @@ -1,7 +1,5 @@ package org.andengine.entity.primitive; -import java.util.ArrayList; - import org.andengine.engine.camera.Camera; import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; import org.andengine.opengl.util.GLState; From 2206081ee38961d79013bcf37f4ce9fcf3e495f5 Mon Sep 17 00:00:00 2001 From: recastro Date: Sat, 11 Feb 2012 11:30:38 +0100 Subject: [PATCH 13/20] Added feature to update PolyLine vertices --- src/org/andengine/entity/primitive/Mesh.java | 13 +++++--- .../andengine/entity/primitive/PolyLine.java | 30 +++++++++---------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/org/andengine/entity/primitive/Mesh.java b/src/org/andengine/entity/primitive/Mesh.java index a5ef089fa..c0e362364 100644 --- a/src/org/andengine/entity/primitive/Mesh.java +++ b/src/org/andengine/entity/primitive/Mesh.java @@ -166,13 +166,18 @@ protected static float[] buildVertexList(float[] pVertexX, float[] pVertexY) { assert( pVertexX.length == pVertexY.length ); - float[] vertices = new float[VERTEX_SIZE * pVertexX.length]; + float[] bufferData = new float[VERTEX_SIZE * pVertexX.length]; + updateVertexList(pVertexX, pVertexY, bufferData); + return bufferData; + } + + protected static void updateVertexList(float[] pVertexX, float[] pVertexY, float[] pBufferData) + { for( int i = 0; i < pVertexX.length; i++) { - vertices[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_X] = pVertexX[i]; - vertices[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_Y] = pVertexY[i]; + pBufferData[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_X] = pVertexX[i]; + pBufferData[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_Y] = pVertexY[i]; } - return vertices; } // =========================================================== diff --git a/src/org/andengine/entity/primitive/PolyLine.java b/src/org/andengine/entity/primitive/PolyLine.java index 1d955ef9a..0f19b82d3 100644 --- a/src/org/andengine/entity/primitive/PolyLine.java +++ b/src/org/andengine/entity/primitive/PolyLine.java @@ -78,20 +78,6 @@ public void setLineWidth( float pLineWidth ) { mLineWidth = pLineWidth; } - - /** - * - * @param pVertexX - * @param pVertexY - * @return true if vertices were correctly updated - * false otherwise - */ - /*public boolean updateVertices( float[] pVertexX, float[] pVertexY ) - { - assert( pVertexX.length == pVertexY.length ); - - return this.updateVertices(buildVertexList(pVertexX, pVertexY)); - }*/ // =========================================================== // Methods for/from SuperClass/Interfaces @@ -108,7 +94,21 @@ protected void preDraw(final GLState pGLState, final Camera pCamera) { // =========================================================== // Methods // =========================================================== - + + /** + * + * @param pVertexX + * @param pVertexY + * @return true if vertices were correctly updated + * false otherwise + */ + public void updateVertices( float[] pVertexX, float[] pVertexY ) + { + assert( pVertexX.length == pVertexY.length ); + updateVertexList(pVertexX, pVertexY, getBufferData()); + onUpdateVertices(); + } + // =========================================================== // Inner and Anonymous Classes // =========================================================== From 960e3ea1e7d6a6916047e32e028ac11be75fcf1f Mon Sep 17 00:00:00 2001 From: recastro Date: Mon, 13 Feb 2012 13:15:42 +0100 Subject: [PATCH 14/20] Fixed javadoc --- src/org/andengine/entity/primitive/Ellipse.java | 9 ++++----- src/org/andengine/entity/primitive/PolyLine.java | 11 +++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/org/andengine/entity/primitive/Ellipse.java b/src/org/andengine/entity/primitive/Ellipse.java index fdeb0d187..4849bb9c3 100644 --- a/src/org/andengine/entity/primitive/Ellipse.java +++ b/src/org/andengine/entity/primitive/Ellipse.java @@ -1,6 +1,5 @@ package org.andengine.entity.primitive; -import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; import org.andengine.opengl.vbo.VertexBufferObject.DrawType; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; @@ -31,21 +30,21 @@ public class Ellipse extends PolyLine { // =========================================================== /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pRadiusA, pRadiusB, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager); } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pRadiusA, pRadiusB, pLineWidth, DEFAULT_RESOLUTION, pVertexBufferObjectManager); } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pRadiusA, pRadiusB, pLineWidth, pResolution, pVertexBufferObjectManager, DrawMode.LINE_LOOP); @@ -57,7 +56,7 @@ public Ellipse(final float pX, final float pY, final float pRadiusA, final float /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Ellipse(final float pX, final float pY, final float pRadiusA, final float pRadiusB, final float pLineWidth, final int pResolution, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { super(pX, pY, buildEllipseVertices(pRadiusA, pRadiusB, pResolution), pLineWidth, pVertexBufferObjectManager, pDrawMode, pDrawType); diff --git a/src/org/andengine/entity/primitive/PolyLine.java b/src/org/andengine/entity/primitive/PolyLine.java index 0f19b82d3..054d2a6c4 100644 --- a/src/org/andengine/entity/primitive/PolyLine.java +++ b/src/org/andengine/entity/primitive/PolyLine.java @@ -1,7 +1,6 @@ package org.andengine.entity.primitive; import org.andengine.engine.camera.Camera; -import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; import org.andengine.opengl.util.GLState; import org.andengine.opengl.vbo.VertexBufferObject.DrawType; import org.andengine.opengl.vbo.VertexBufferObjectManager; @@ -28,21 +27,21 @@ public class PolyLine extends Mesh { // =========================================================== /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager); } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) { this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, DrawMode.LINE_LOOP); } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode) { this(pX, pY, pVertexX, pVertexY, pLineWidth, pVertexBufferObjectManager, pDrawMode, DrawType.STATIC); @@ -50,14 +49,14 @@ public PolyLine(final float pX, final float pY, final float[] pVertexX, final fl /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public PolyLine(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { this(pX, pY, buildVertexList(pVertexX, pVertexY), pLineWidth, pVertexBufferObjectManager, pDrawMode, pDrawType); } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public PolyLine(final float pX, final float pY, final float[] pBufferData, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, DrawMode pDrawMode, final DrawType pDrawType) { super(pX, pY, pBufferData, pBufferData.length / VERTEX_SIZE, pDrawMode, pVertexBufferObjectManager, pDrawType); From 3321f8f41e40f9d459412c9056253c82942c490b Mon Sep 17 00:00:00 2001 From: recastro Date: Tue, 14 Feb 2012 18:04:55 +0100 Subject: [PATCH 15/20] Started mesh uv mapping --- src/org/andengine/entity/primitive/Mesh.java | 74 +++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/src/org/andengine/entity/primitive/Mesh.java b/src/org/andengine/entity/primitive/Mesh.java index bdcc9fc70..66de32d3c 100644 --- a/src/org/andengine/entity/primitive/Mesh.java +++ b/src/org/andengine/entity/primitive/Mesh.java @@ -1,11 +1,15 @@ package org.andengine.entity.primitive; +import java.security.spec.MGF1ParameterSpec; + import org.andengine.engine.camera.Camera; import org.andengine.entity.shape.IShape; import org.andengine.entity.shape.RectangularShape; import org.andengine.entity.shape.Shape; +import org.andengine.entity.sprite.Sprite; import org.andengine.opengl.shader.PositionColorShaderProgram; import org.andengine.opengl.shader.constants.ShaderProgramConstants; +import org.andengine.opengl.texture.region.ITextureRegion; import org.andengine.opengl.util.GLState; import org.andengine.opengl.vbo.HighPerformanceVertexBufferObject; import org.andengine.opengl.vbo.IVertexBufferObject; @@ -47,10 +51,20 @@ public class Mesh extends Shape { protected final IMeshVertexBufferObject mMeshVertexBufferObject; private int mVertexCountToDraw; private int mDrawMode; + protected ITextureRegion mTextureRegion; // =========================================================== // Constructors // =========================================================== + + /** + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public Mesh(final float pX, final float pY, final float[] pBufferData, final int pVertexCount, final DrawMode pDrawMode, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pBufferData, pVertexCount, pDrawMode, pVertexBufferObjectManager, DrawType.STATIC); + mTextureRegion = pTextureRegion; + this.onUpdateTextureCoordinates(); + } /** * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. @@ -100,6 +114,10 @@ public void setVertexCountToDraw(final int pVertexCountToDraw) { public void setDrawMode(final DrawMode pDrawMode) { this.mDrawMode = pDrawMode.mDrawMode; } + + public ITextureRegion getTextureRegion() { + return this.mTextureRegion; + } // =========================================================== // Methods for/from SuperClass/Interfaces @@ -113,7 +131,11 @@ public IMeshVertexBufferObject getVertexBufferObject() { @Override protected void preDraw(final GLState pGLState, final Camera pCamera) { super.preDraw(pGLState, pCamera); - + + // Check if polygon uses a texture + if( mTextureRegion != null) + this.mTextureRegion.getTexture().bind(pGLState); + this.mMeshVertexBufferObject.bind(pGLState, this.mShaderProgram); } @@ -138,6 +160,10 @@ protected void onUpdateColor() { protected void onUpdateVertices() { this.mMeshVertexBufferObject.onUpdateVertices(this); } + + protected void onUpdateTextureCoordinates() { + this.mMeshVertexBufferObject.onUpdateTextureCoordinates(this); + } @Override @Deprecated @@ -196,6 +222,7 @@ public static interface IMeshVertexBufferObject extends IVertexBufferObject { public float[] getBufferData(); public void onUpdateColor(final Mesh pMesh); public void onUpdateVertices(final Mesh pMesh); + public void onUpdateTextureCoordinates(final Mesh pMesh); } public static class HighPerformanceMeshVertexBufferObject extends HighPerformanceVertexBufferObject implements IMeshVertexBufferObject { @@ -246,6 +273,51 @@ public void onUpdateVertices(final Mesh pMesh) { this.setDirtyOnHardware(); } + + @Override + public void onUpdateTextureCoordinates(final Mesh pMesh) { + final float[] bufferData = this.mBufferData; + + final ITextureRegion textureRegion = pMesh.getTextureRegion(); // TODO Optimize with field access? + + final float u; + final float v; + final float u2; + final float v2; + + u = textureRegion.getU(); + u2 = textureRegion.getU2(); + v = textureRegion.getV(); + v2 = textureRegion.getV2(); + + if(textureRegion.isRotated()) { + bufferData[0 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u2; + bufferData[0 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v; + + bufferData[1 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u; + bufferData[1 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v; + + bufferData[2 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u2; + bufferData[2 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v2; + + bufferData[3 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u; + bufferData[3 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v2; + } else { + bufferData[0 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u; + bufferData[0 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v; + + bufferData[1 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u; + bufferData[1 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v2; + + bufferData[2 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u2; + bufferData[2 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v; + + bufferData[3 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_U] = u2; + bufferData[3 * Sprite.VERTEX_SIZE + Sprite.TEXTURECOORDINATES_INDEX_V] = v2; + } + + this.setDirtyOnHardware(); + } // =========================================================== // Methods From 3bd446d6671f1296ad2f286414651cfc7a310beb Mon Sep 17 00:00:00 2001 From: recastro Date: Tue, 14 Feb 2012 19:48:13 +0100 Subject: [PATCH 16/20] Polygon uses Mesh instead of PolygonBase --- .../andengine/entity/primitive/Polygon.java | 246 +++++------------- 1 file changed, 58 insertions(+), 188 deletions(-) diff --git a/src/org/andengine/entity/primitive/Polygon.java b/src/org/andengine/entity/primitive/Polygon.java index e089bec15..4ea71262e 100644 --- a/src/org/andengine/entity/primitive/Polygon.java +++ b/src/org/andengine/entity/primitive/Polygon.java @@ -1,24 +1,29 @@ package org.andengine.entity.primitive; import java.util.ArrayList; +import java.util.List; -import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; +import org.andengine.extension.physics.box2d.util.triangulation.EarClippingTriangulator; import org.andengine.opengl.vbo.VertexBufferObject.DrawType; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; import android.util.Log; +import com.badlogic.gdx.math.Vector2; + /** * * @author Rodrigo Castro * @since 22:10:11 - 28.01.2012 */ -public class Polygon extends PolygonBase { +public class Polygon extends Mesh { // =========================================================== // Constants // =========================================================== + private static final float VERTEX_SIZE_DEFAULT_RATIO = 1.f; + // =========================================================== // Fields // =========================================================== @@ -26,34 +31,37 @@ public class Polygon extends PolygonBase { protected float[] mVertexX; protected float[] mVertexY; + protected static EarClippingTriangulator mTriangulator = new EarClippingTriangulator(); + // =========================================================== // Constructors // =========================================================== /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pVertexX, pVertexY, VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, DrawType.STATIC); + this(pX, pY, pVertexX, pVertexY, pVertexBufferObjectManager, DrawType.STATIC); } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link PolygoonBase#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public Polygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pVertexX, pVertexY, vertexSizeRatio, pVertexBufferObjectManager, DrawType.STATIC); + public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + this(pX, pY, buildVertexList(mTriangulator.computeTriangles(buildListOfVector2(pVertexX, pVertexY))), VERTEX_SIZE_DEFAULT_RATIO, pVertexBufferObjectManager, pDrawType ); + + assert( mVertexX.length == mVertexY.length ); + mVertexX = pVertexX; + mVertexY = pVertexY; } /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + * Uses a default {@link HighPerformanceMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ - public Polygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { - super(pX, pY, Triangulate.process(pVertexX, pVertexY), vertexSizeRatio, pVertexBufferObjectManager, DrawMode.GL_TRIANGLES, pDrawType); - - mVertexX = pVertexX; - mVertexY = pVertexY; - assert( mVertexX.length == mVertexY.length ); + public Polygon(final float pX, final float pY, final float[] pBufferData, final float sizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + super(pX, pY, (int) ((pBufferData.length / VERTEX_SIZE)* sizeRatio), DrawMode.TRIANGLES, + new HighPerformanceMeshVertexBufferObject(pVertexBufferObjectManager, pBufferData, (int) ((pBufferData.length / VERTEX_SIZE )*sizeRatio), pDrawType, true, Mesh.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT)); onUpdateVertices(); } @@ -85,13 +93,17 @@ public boolean updateVertices( float[] pVertexX, float[] pVertexY ) mVertexY = pVertexY; assert( mVertexX.length == mVertexY.length ); - ArrayList vertices = Triangulate.process(pVertexX, pVertexY); - if( vertices == null ) + List verticesVectors = mTriangulator.computeTriangles(buildListOfVector2(pVertexX, pVertexY)); + if( verticesVectors.size() == 0 ) { Log.e("AndEngine", "Error: Polygon - Polygon can't be triangulated. Will not update vertices"); return false; } - return this.updateVertices(vertices); + + updateVertices(verticesVectors, getBufferData()); + onUpdateVertices(); + + return true; } // =========================================================== @@ -102,183 +114,41 @@ public boolean updateVertices( float[] pVertexX, float[] pVertexY ) // Methods // =========================================================== - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - - public static class Triangulate { - // Original code by John W. RATCLIFF - // Java port by Rodrigo Castro - - // COTD Entry submitted by John W. Ratcliff [jratcliff@verant.com] - - // ** THIS IS A CODE SNIPPET WHICH WILL EFFICIEINTLY TRIANGULATE ANY - // ** POLYGON/CONTOUR (without holes) AS A STATIC CLASS. THIS SNIPPET - // ** IS COMPRISED OF 3 FILES, TRIANGULATE.H, THE HEADER FILE FOR THE - // ** TRIANGULATE BASE CLASS, TRIANGULATE.CPP, THE IMPLEMENTATION OF - // ** THE TRIANGULATE BASE CLASS, AND TEST.CPP, A SMALL TEST PROGRAM - // ** DEMONSTRATING THE USAGE OF THE TRIANGULATOR. THE TRIANGULATE - // ** BASE CLASS ALSO PROVIDES TWO USEFUL HELPER METHODS, ONE WHICH - // ** COMPUTES THE AREA OF A POLYGON, AND ANOTHER WHICH DOES AN EFFICENT - // ** POINT IN A TRIANGLE TEST. - // ** SUBMITTED BY JOHN W. RATCLIFF (jratcliff@verant.com) July 22, 2000 - + protected static List buildListOfVector2(float[] pX, float [] pY ) + { + assert(pX.length == pY.length ); + ArrayList vectors = new ArrayList( pX.length ); - /*****************************************************************/ - /** Static class to triangulate any contour/polygon efficiently **/ - /** You should replace Vector2d with whatever your own Vector **/ - /** class might be. Does not support polygons with holes. **/ - /** Uses STL vectors to represent a dynamic array of vertices. **/ - /** This code snippet was submitted to FlipCode.com by **/ - /** John W. Ratcliff (jratcliff@verant.com) on July 22, 2000 **/ - /** I did not write the original code/algorithm for this **/ - /** this triangulator, in fact, I can't even remember where I **/ - /** found it in the first place. However, I did rework it into **/ - /** the following black-box static class so you can make easy **/ - /** use of it in your own code. Simply replace Vector2d with **/ - /** whatever your own Vector implementation might be. **/ - /*****************************************************************/ - - final static float EPSILON = 0.0000000001f; - - // compute area of a contour/polygon - protected static float area(final Vector2d[] contour) + for( int i = 0; i < pX.length; i++ ) { - - int n = contour.length; - - float A=0.0f; - - for(int p=n-1,q=0; q= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f)); - }; - - protected static boolean Snip(final Vector2d[] contour,int u,int v,int w,int n,int[] V) - { - int p; - float Ax, Ay, Bx, By, Cx, Cy, Px, Py; - - Ax = contour[V[u]].getX(); - Ay = contour[V[u]].getY(); - - Bx = contour[V[v]].getX(); - By = contour[V[v]].getY(); - - Cx = contour[V[w]].getX(); - Cy = contour[V[w]].getY(); - - if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) - return false; - - for (p=0;p process(final float[] pX, final float[] pY) - { - - final Vector2d[] contour = Vector2d.floatToList(pX, pY); - - /* allocate and initialize list of Vertices in polygon */ - int n = contour.length; - if ( n < 3 ) - return null; - - int[] V = new int[n]; - ArrayList result = new ArrayList(n); - /* we want a counter-clockwise polygon in V */ - - if ( 0.0f < area(contour) ) - for (int v=0; v2; ) - { - /* if we loop, it is probably a non-simple polygon */ - if (0 >= (count--)) - { - //** Triangulate: ERROR - probable bad polygon! - return null; - } + protected static float[] buildVertexList(List vertices ) + { - /* three consecutive vertices in current polygon, */ - int u = v ; - if (nv <= u) - u = 0; /* previous */ - v = u+1; - if (nv <= v) - v = 0; /* new v */ - int w = v+1; - if (nv <= w) - w = 0; /* next */ - if ( Snip(contour,u,v,w,nv,V) ) - { - int a,b,c,s,t; - - /* true names of the vertices */ - a = V[u]; b = V[v]; c = V[w]; - /* output Triangle */ - result.add( contour[a] ); - result.add( contour[b] ); - result.add( contour[c] ); - - /* remove v from remaining polygon */ - for(s=v,t=v+1;t vertices, float[] pBufferData) { + int i = 0; + for( Vector2 vertex : vertices ) + { + pBufferData[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_X] = vertex.x; + pBufferData[(i * Mesh.VERTEX_SIZE) + Mesh.VERTEX_INDEX_Y] = vertex.y; + i++; } } + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } From 14e3b16bab045f5987143dbae619a6768bbbb132 Mon Sep 17 00:00:00 2001 From: recastro Date: Wed, 15 Feb 2012 13:29:13 +0100 Subject: [PATCH 17/20] added experimental textured polygon --- .../entity/primitive/TexturedMesh.java | 401 ++++++++++++++++++ .../entity/primitive/TexturedPolygon.java | 172 ++++++++ 2 files changed, 573 insertions(+) create mode 100644 src/org/andengine/entity/primitive/TexturedMesh.java create mode 100644 src/org/andengine/entity/primitive/TexturedPolygon.java diff --git a/src/org/andengine/entity/primitive/TexturedMesh.java b/src/org/andengine/entity/primitive/TexturedMesh.java new file mode 100644 index 000000000..4abb7c3c2 --- /dev/null +++ b/src/org/andengine/entity/primitive/TexturedMesh.java @@ -0,0 +1,401 @@ +package org.andengine.entity.primitive; + +import org.andengine.engine.camera.Camera; +import org.andengine.entity.shape.IShape; +import org.andengine.entity.shape.RectangularShape; +import org.andengine.entity.shape.Shape; +import org.andengine.opengl.shader.PositionColorTextureCoordinatesShaderProgram; +import org.andengine.opengl.shader.constants.ShaderProgramConstants; +import org.andengine.opengl.texture.region.ITextureRegion; +import org.andengine.opengl.util.GLState; +import org.andengine.opengl.vbo.HighPerformanceVertexBufferObject; +import org.andengine.opengl.vbo.IVertexBufferObject; +import org.andengine.opengl.vbo.VertexBufferObject.DrawType; +import org.andengine.opengl.vbo.VertexBufferObjectManager; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributes; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; +import org.andengine.util.debug.Debug; +import org.andengine.util.exception.MethodNotSupportedException; + +import android.opengl.GLES20; +import android.renderscript.Mesh; + +/** + * (c) Zynga 2012 + * + * @author Nicolas Gramlich + * @since 16:44:50 - 09.02.2012 + */ +public class TexturedMesh extends Shape { + // =========================================================== + // Constants + // =========================================================== + + public static final int VERTEX_INDEX_X = 0; + public static final int VERTEX_INDEX_Y = TexturedMesh.VERTEX_INDEX_X + 1; + public static final int COLOR_INDEX = TexturedMesh.VERTEX_INDEX_Y + 1; + public static final int TEXTURECOORDINATES_INDEX_U = TexturedMesh.COLOR_INDEX + 1; + public static final int TEXTURECOORDINATES_INDEX_V = TexturedMesh.TEXTURECOORDINATES_INDEX_U + 1; + + + public static final int VERTEX_SIZE = 5; + + public static final VertexBufferObjectAttributes VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT = new VertexBufferObjectAttributesBuilder(3) + .add(ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION, 2, GLES20.GL_FLOAT, false) + .add(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR, 4, GLES20.GL_UNSIGNED_BYTE, true) + .add(ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES_LOCATION, ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES, 2, GLES20.GL_FLOAT, false) + .build(); + + // =========================================================== + // Fields + // =========================================================== + + protected final ITexturedMeshVertexBufferObject mMeshVertexBufferObject; + private int mVertexCountToDraw; + private int mDrawMode; + protected ITextureRegion mTextureRegion; + + // =========================================================== + // Constructors + // =========================================================== + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedMesh(final float pX, final float pY, final float[] pBufferData, final int pVertexCount, final DrawMode pDrawMode, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pBufferData, pVertexCount, pDrawMode, null, pVertexBufferObjectManager, DrawType.STATIC); + } + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedMesh(final float pX, final float pY, final float[] pBufferData, final int pVertexCount, final DrawMode pDrawMode, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pBufferData, pVertexCount, pDrawMode, pTextureRegion, pVertexBufferObjectManager, DrawType.STATIC); + } + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedMesh(final float pX, final float pY, final float[] pBufferData, final int pVertexCount, final DrawMode pDrawMode, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + this(pX, pY, pBufferData, pVertexCount, pDrawMode, null, pVertexBufferObjectManager, pDrawType); + } + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedMesh(final float pX, final float pY, final float[] pBufferData, final int pVertexCount, final DrawMode pDrawMode, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + this(pX, pY, pVertexCount, pDrawMode, pTextureRegion, new HighPerformanceTexturedMeshVertexBufferObject(pVertexBufferObjectManager, pBufferData, pVertexCount, pDrawType, true, TexturedMesh.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT)); + } + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedMesh(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final DrawMode pDrawMode, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + this(pX, pY, pVertexX.length, pDrawMode, pTextureRegion, new HighPerformanceTexturedMeshVertexBufferObject(pVertexBufferObjectManager, buildVertexList(pVertexX, pVertexY), pVertexX.length, pDrawType, true, TexturedMesh.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT)); + } + + public TexturedMesh(final float pX, final float pY, final int pVertexCount, final DrawMode pDrawMode, final ITextureRegion pTextureRegion, final ITexturedMeshVertexBufferObject pMeshVertexBufferObject) { + super(pX, pY, PositionColorTextureCoordinatesShaderProgram.getInstance()); + + this.mDrawMode = pDrawMode.getDrawMode(); + this.mTextureRegion = pTextureRegion; + this.mMeshVertexBufferObject = pMeshVertexBufferObject; + this.mVertexCountToDraw = pVertexCount; + + if( pTextureRegion != null) + { + this.setBlendingEnabled(true); + this.initBlendFunction(pTextureRegion); + this.onUpdateTextureCoordinates(); + + } + + this.onUpdateVertices(); + this.onUpdateColor(); + + this.mMeshVertexBufferObject.setDirtyOnHardware(); + + this.setBlendingEnabled(true); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + public float[] getBufferData() { + return this.mMeshVertexBufferObject.getBufferData(); + } + + public void setVertexCountToDraw(final int pVertexCountToDraw) { + this.mVertexCountToDraw = pVertexCountToDraw; + } + + public void setDrawMode(final DrawMode pDrawMode) { + this.mDrawMode = pDrawMode.mDrawMode; + } + + public ITextureRegion getTextureRegion() { + return this.mTextureRegion; + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public ITexturedMeshVertexBufferObject getVertexBufferObject() { + return this.mMeshVertexBufferObject; + } + + @Override + public void reset() { + super.reset(); + + this.initBlendFunction(this.getTextureRegion().getTexture()); + } + + @Override + protected void preDraw(final GLState pGLState, final Camera pCamera) { + super.preDraw(pGLState, pCamera); + + this.mTextureRegion.getTexture().bind(pGLState); + + this.mMeshVertexBufferObject.bind(pGLState, this.mShaderProgram); + } + + @Override + protected void draw(final GLState pGLState, final Camera pCamera) { + this.mMeshVertexBufferObject.draw(this.mDrawMode, this.mVertexCountToDraw); + } + + @Override + protected void postDraw(final GLState pGLState, final Camera pCamera) { + this.mMeshVertexBufferObject.unbind(pGLState, this.mShaderProgram); + + super.postDraw(pGLState, pCamera); + } + + @Override + protected void onUpdateColor() { + this.mMeshVertexBufferObject.onUpdateColor(this); + } + + @Override + protected void onUpdateVertices() { + this.mMeshVertexBufferObject.onUpdateVertices(this); + } + + protected void onUpdateTextureCoordinates() { + this.mMeshVertexBufferObject.onUpdateTextureCoordinates(this); + } + + @Override + @Deprecated + public boolean contains(final float pX, final float pY) { + throw new MethodNotSupportedException(); + } + + @Override + public boolean collidesWith(final IShape pOtherShape) { + if(pOtherShape instanceof Line) { + // TODO + return false; + } else if(pOtherShape instanceof RectangularShape) { + // TODO + return false; + } else { + return false; + } + } + + // =========================================================== + // Methods + // =========================================================== + + protected static float[] buildVertexList(float[] pVertexX, float[] pVertexY) + { + assert( pVertexX.length == pVertexY.length ); + + float[] bufferData = new float[TexturedMesh.VERTEX_SIZE * pVertexX.length]; + updateVertexList(pVertexX, pVertexY, bufferData); + return bufferData; + } + + protected static void updateVertexList(float[] pVertexX, float[] pVertexY, float[] pBufferData) + { + for( int i = 0; i < pVertexX.length; i++) + { + pBufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.VERTEX_INDEX_X] = pVertexX[i]; + pBufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.VERTEX_INDEX_Y] = pVertexY[i]; + } + } + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + + public static interface ITexturedMeshVertexBufferObject extends IVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + public float[] getBufferData(); + public void onUpdateColor(final TexturedMesh pMesh); + public void onUpdateVertices(final TexturedMesh pMesh); + public void onUpdateTextureCoordinates(final TexturedMesh pMesh); + } + + public static class HighPerformanceTexturedMeshVertexBufferObject extends HighPerformanceVertexBufferObject implements ITexturedMeshVertexBufferObject { + // =========================================================== + // Constants + // =========================================================== + + // =========================================================== + // Fields + // =========================================================== + + private final int mVertexCount; + + // =========================================================== + // Constructors + // =========================================================== + + public HighPerformanceTexturedMeshVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final float[] pBufferData, final int pVertexCount, final DrawType pDrawType, final boolean pAutoDispose, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { + super(pVertexBufferObjectManager, pBufferData, pDrawType, pAutoDispose, pVertexBufferObjectAttributes); + + this.mVertexCount = pVertexCount; + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + @Override + public void onUpdateColor(final TexturedMesh pMesh) { + final float[] bufferData = this.mBufferData; + + final float packedColor = pMesh.getColor().getFloatPacked(); + + for(int i = 0; i < this.mVertexCount; i++) { + bufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.COLOR_INDEX] = packedColor; + } + + this.setDirtyOnHardware(); + } + + @Override + public void onUpdateVertices(final TexturedMesh pMesh) { + /* Since the buffer data is managed from the caller, we just mark the buffer data as dirty. */ + + this.setDirtyOnHardware(); + } + + @Override + public void onUpdateTextureCoordinates(final TexturedMesh pMesh) { + final float[] bufferData = this.mBufferData; + + final ITextureRegion textureRegion = pMesh.getTextureRegion(); // TODO Optimize with field access? + + float textureWidth = textureRegion.getWidth(); + float textureHeight = textureRegion.getHeight(); + + // x0 is mapped to u0 + // y0 is mapped to v0 + + // TODO get initial mapping + float x0 = 0; // pMesh.getX0(); + float y0 = 0; //pMesh.getY0(); + + + for(int i = 0; i < this.mVertexCount; i++) { + float x = bufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.VERTEX_INDEX_X]; + float y = bufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.VERTEX_INDEX_Y]; + + float u = (x - x0) / textureWidth; + float v = (y - y0) / textureHeight; + + Debug.d("u = " + u ); + Debug.d("v = " + v ); + + Debug.d("x = " + x ); + Debug.d("y = " + y ); + + bufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.TEXTURECOORDINATES_INDEX_U] = u; + bufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.TEXTURECOORDINATES_INDEX_V] = v; + } + + Debug.d("v ---" ); + + this.setDirtyOnHardware(); + } + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } + + public static enum DrawMode { + // =========================================================== + // Elements + // =========================================================== + + POINTS(GLES20.GL_POINTS), + LINE_STRIP(GLES20.GL_LINE_STRIP), + LINE_LOOP(GLES20.GL_LINE_LOOP), + LINES(GLES20.GL_LINES), + TRIANGLE_STRIP(GLES20.GL_TRIANGLE_STRIP), + TRIANGLE_FAN(GLES20.GL_TRIANGLE_FAN), + TRIANGLES(GLES20.GL_TRIANGLES); + + // =========================================================== + // Constants + // =========================================================== + + public final int mDrawMode; + + // =========================================================== + // Fields + // =========================================================== + + // =========================================================== + // Constructors + // =========================================================== + + private DrawMode(final int pDrawMode) { + this.mDrawMode = pDrawMode; + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + public int getDrawMode() { + return this.mDrawMode; + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + } +} diff --git a/src/org/andengine/entity/primitive/TexturedPolygon.java b/src/org/andengine/entity/primitive/TexturedPolygon.java new file mode 100644 index 000000000..dc5afb372 --- /dev/null +++ b/src/org/andengine/entity/primitive/TexturedPolygon.java @@ -0,0 +1,172 @@ +package org.andengine.entity.primitive; + +import java.util.ArrayList; +import java.util.List; + +import org.andengine.extension.physics.box2d.util.triangulation.EarClippingTriangulator; +import org.andengine.opengl.texture.region.ITextureRegion; +import org.andengine.opengl.vbo.VertexBufferObject.DrawType; +import org.andengine.opengl.vbo.VertexBufferObjectManager; +import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; + +import android.renderscript.Mesh; +import android.util.Log; + +import com.badlogic.gdx.math.Vector2; + +/** + * + * @author Rodrigo Castro + * @since 22:10:11 - 28.01.2012 + */ +public class TexturedPolygon extends TexturedMesh { + // =========================================================== + // Constants + // =========================================================== + + private static final float VERTEX_SIZE_DEFAULT_RATIO = 1.f; + + // =========================================================== + // Fields + // =========================================================== + + protected float[] mVertexX; + protected float[] mVertexY; + + protected static EarClippingTriangulator mTriangulator = new EarClippingTriangulator(); + + // =========================================================== + // Constructors + // =========================================================== + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedPolygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, pVertexBufferObjectManager, DrawType.STATIC); + } + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedPolygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + this(pX, pY, buildVertexList(mTriangulator.computeTriangles(buildListOfVector2(pVertexX, pVertexY))), VERTEX_SIZE_DEFAULT_RATIO, null, pVertexBufferObjectManager, pDrawType ); + + assert( mVertexX.length == mVertexY.length ); + mVertexX = pVertexX; + mVertexY = pVertexY; + } + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedPolygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) { + this(pX, pY, pVertexX, pVertexY, pTextureRegion, pVertexBufferObjectManager, DrawType.STATIC); + } + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedPolygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + this(pX, pY, buildVertexList(mTriangulator.computeTriangles(buildListOfVector2(pVertexX, pVertexY))), VERTEX_SIZE_DEFAULT_RATIO, pTextureRegion, pVertexBufferObjectManager, pDrawType ); + + assert( mVertexX.length == mVertexY.length ); + mVertexX = pVertexX; + mVertexY = pVertexY; + } + + + /** + * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. + */ + public TexturedPolygon(final float pX, final float pY, final float[] pBufferData, final float sizeRatio, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { + super(pX, pY, (int) ((pBufferData.length / VERTEX_SIZE)* sizeRatio), DrawMode.TRIANGLES, + pTextureRegion, new HighPerformanceTexturedMeshVertexBufferObject(pVertexBufferObjectManager, pBufferData, (int) ((pBufferData.length / VERTEX_SIZE )*sizeRatio), pDrawType, true, TexturedMesh.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT)); + } + + // =========================================================== + // Getter & Setter + // =========================================================== + + public float[] getVertexX() + { + return mVertexX; + } + + public float[] getVertexY() + { + return mVertexY; + } + + /** + * + * @param pVertexX + * @param pVertexY + * @return true if vertices were correctly updated + * false otherwise + */ + public boolean updateVertices( float[] pVertexX, float[] pVertexY ) + { + mVertexX = pVertexX; + mVertexY = pVertexY; + assert( mVertexX.length == mVertexY.length ); + + List verticesVectors = mTriangulator.computeTriangles(buildListOfVector2(pVertexX, pVertexY)); + if( verticesVectors.size() == 0 ) + { + Log.e("AndEngine", "Error: Polygon - Polygon can't be triangulated. Will not update vertices"); + return false; + } + + updateVertices(verticesVectors, getBufferData()); + onUpdateVertices(); + + return true; + } + + // =========================================================== + // Methods for/from SuperClass/Interfaces + // =========================================================== + + // =========================================================== + // Methods + // =========================================================== + + protected static List buildListOfVector2(float[] pX, float [] pY ) + { + assert(pX.length == pY.length ); + ArrayList vectors = new ArrayList( pX.length ); + + for( int i = 0; i < pX.length; i++ ) + { + // TODO avoid using new + Vector2 v = new Vector2( pX[i], pY[i]); + vectors.add(v); + } + + return vectors; + } + + protected static float[] buildVertexList(List vertices ) + { + + float[] bufferData = new float[VERTEX_SIZE * vertices.size()]; + updateVertices( vertices, bufferData ); + return bufferData; + } + + protected static void updateVertices(List vertices, float[] pBufferData) { + int i = 0; + for( Vector2 vertex : vertices ) + { + pBufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.VERTEX_INDEX_X] = vertex.x; + pBufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.VERTEX_INDEX_Y] = vertex.y; + i++; + } + } + + // =========================================================== + // Inner and Anonymous Classes + // =========================================================== + +} From 53872ed3bd0c8f66c1f11722c7897d8b5caa4ef4 Mon Sep 17 00:00:00 2001 From: recastro Date: Wed, 15 Feb 2012 13:32:10 +0100 Subject: [PATCH 18/20] added experimental textured polygon support --- .../entity/primitive/PolygonBase.java | 6 +++--- .../entity/primitive/TexturedPolygon.java | 18 ------------------ 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/src/org/andengine/entity/primitive/PolygonBase.java b/src/org/andengine/entity/primitive/PolygonBase.java index 575c3e465..8a8985377 100644 --- a/src/org/andengine/entity/primitive/PolygonBase.java +++ b/src/org/andengine/entity/primitive/PolygonBase.java @@ -215,7 +215,7 @@ public void onUpdateColor(final PolygonBase pPolygon) { for( int i = 0; i < nbVertexInTriangles; i++) { // TODO use color per vertex - bufferData[i * Polygon.VERTEX_SIZE + Polygon.COLOR_INDEX] = packedColor; + bufferData[i * VERTEX_SIZE + COLOR_INDEX] = packedColor; } this.setDirtyOnHardware(); @@ -230,8 +230,8 @@ public void onUpdateVertices(final PolygonBase pPolygon) { for( int i = 0; i < nbVertexInTriangles; i++) { - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); + bufferData[i * VERTEX_SIZE + VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); + bufferData[i * VERTEX_SIZE + VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); } this.setDirtyOnHardware(); diff --git a/src/org/andengine/entity/primitive/TexturedPolygon.java b/src/org/andengine/entity/primitive/TexturedPolygon.java index dc5afb372..37c7dd887 100644 --- a/src/org/andengine/entity/primitive/TexturedPolygon.java +++ b/src/org/andengine/entity/primitive/TexturedPolygon.java @@ -39,24 +39,6 @@ public class TexturedPolygon extends TexturedMesh { // Constructors // =========================================================== - /** - * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. - */ - public TexturedPolygon(final float pX, final float pY, final float[] pVertexX, final float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager) { - this(pX, pY, pVertexX, pVertexY, pVertexBufferObjectManager, DrawType.STATIC); - } - - /** - * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. - */ - public TexturedPolygon(final float pX, final float pY, final float[] pVertexX, float[] pVertexY, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) { - this(pX, pY, buildVertexList(mTriangulator.computeTriangles(buildListOfVector2(pVertexX, pVertexY))), VERTEX_SIZE_DEFAULT_RATIO, null, pVertexBufferObjectManager, pDrawType ); - - assert( mVertexX.length == mVertexY.length ); - mVertexX = pVertexX; - mVertexY = pVertexY; - } - /** * Uses a default {@link HighPerformanceTexturedMeshVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Mesh#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. */ From 685e7039c6ad0ede06c9e883e9ec03b598e015ba Mon Sep 17 00:00:00 2001 From: recastro Date: Tue, 28 Feb 2012 23:07:21 +0100 Subject: [PATCH 19/20] Removed unused files --- .../entity/primitive/PolygonBase.java | 404 ------------------ .../andengine/entity/shape/PolygonShape.java | 115 ----- 2 files changed, 519 deletions(-) delete mode 100644 src/org/andengine/entity/primitive/PolygonBase.java delete mode 100644 src/org/andengine/entity/shape/PolygonShape.java diff --git a/src/org/andengine/entity/primitive/PolygonBase.java b/src/org/andengine/entity/primitive/PolygonBase.java deleted file mode 100644 index 575c3e465..000000000 --- a/src/org/andengine/entity/primitive/PolygonBase.java +++ /dev/null @@ -1,404 +0,0 @@ -package org.andengine.entity.primitive; - -import java.util.ArrayList; - -import org.andengine.engine.camera.Camera; -import org.andengine.entity.primitive.Rectangle.HighPerformanceRectangleVertexBufferObject; -import org.andengine.entity.shape.PolygonShape; -import org.andengine.opengl.shader.PositionColorShaderProgram; -import org.andengine.opengl.shader.constants.ShaderProgramConstants; -import org.andengine.opengl.util.GLState; -import org.andengine.opengl.vbo.HighPerformanceVertexBufferObject; -import org.andengine.opengl.vbo.IVertexBufferObject; -import org.andengine.opengl.vbo.LowMemoryVertexBufferObject; -import org.andengine.opengl.vbo.VertexBufferObject.DrawType; -import org.andengine.opengl.vbo.VertexBufferObjectManager; -import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttribute; -import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributes; -import org.andengine.opengl.vbo.attribute.VertexBufferObjectAttributesBuilder; - -import android.opengl.GLES20; -import android.util.Log; - -/** - * - * @author Rodrigo Castro - * @since 22:43:05 - 30.01.2012 - */ -public abstract class PolygonBase extends PolygonShape { - // =========================================================== - // Constants - // =========================================================== - - protected static final int VERTEX_INDEX_X = 0; - protected static final int VERTEX_INDEX_Y = VERTEX_INDEX_X + 1; - protected static final int COLOR_INDEX = VERTEX_INDEX_Y + 1; - - protected static final int VERTEX_SIZE = 2 + 1; - - public static final float VERTEX_SIZE_DEFAULT_RATIO = 1.f; - public static final float VERTEX_SIZE_EXTRA_RATIO = 1.3f; - - public static final VertexBufferObjectAttributes VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT = new VertexBufferObjectAttributesBuilder(2) - .add(ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION, 2, GLES20.GL_FLOAT, false) - .add(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR, 4, GLES20.GL_UNSIGNED_BYTE, true) - .build(); - - // =========================================================== - // Fields - // =========================================================== - - protected final IPolygonBaseVertexBufferObject mPolygonVertexBufferObject; - protected ArrayList mVertices; - protected int mCapacity; - protected DrawMode mDrawMode; - - // =========================================================== - // Constructors - // =========================================================== - - /** - * Uses a default {@link HighPerformanceRectangleVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Rectangle#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. - */ - protected PolygonBase(final float pX, final float pY, ArrayList pVertices, final float vertexSizeRatio, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawMode pDrawMode, final DrawType pDrawType) { - super(pX, pY, PositionColorShaderProgram.getInstance()); - - mDrawMode = pDrawMode; - mVertices = pVertices; - mCapacity = (int) (vertexSizeRatio * VERTEX_SIZE * pVertices.size()); - this.mPolygonVertexBufferObject = new HighPerformancePolygonBaseVertexBufferObject(pVertexBufferObjectManager, mCapacity, pDrawType, true, Polygon.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT); - - onUpdateVertices(); - this.onUpdateColor(); - - this.setBlendingEnabled(true); - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - /** - * - * @param pVertexX - * @param pVertexY - * @return true if vertices were correctly updated - * false otherwise - */ - protected boolean updateVertices( ArrayList pVertices ) - { - - if (mCapacity < pVertices.size()) - { - Log.e("AndEngine", "Error: Polygon - Not enough space to accomodate extra vertices"); - return false; - } - else - { - mVertices = pVertices; - } - - onUpdateVertices(); - - return true; - } - - protected ArrayList getVertices() - { - return mVertices; - } - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - @Override - public IPolygonBaseVertexBufferObject getVertexBufferObject() { - return this.mPolygonVertexBufferObject; - } - - @Override - protected void preDraw(final GLState pGLState, final Camera pCamera) { - super.preDraw(pGLState, pCamera); - - this.mPolygonVertexBufferObject.bind(pGLState, this.mShaderProgram); - } - - @Override - protected void draw(final GLState pGLState, final Camera pCamera) { - this.mPolygonVertexBufferObject.draw(mDrawMode.getUsage(), getVertices().size()); - } - - @Override - protected void postDraw(final GLState pGLState, final Camera pCamera) { - this.mPolygonVertexBufferObject.unbind(pGLState, this.mShaderProgram); - - super.postDraw(pGLState, pCamera); - } - - @Override - protected void onUpdateColor() { - this.mPolygonVertexBufferObject.onUpdateColor(this); - } - - @Override - protected void onUpdateVertices() { - this.mPolygonVertexBufferObject.onUpdateVertices(this); - } - - // =========================================================== - // Methods - // =========================================================== - - protected static ArrayList buildVector2dList(float[] pVertexX, float[] pVertexY) - { - assert( pVertexX.length == pVertexY.length ); - - ArrayList vertices = new ArrayList(pVertexX.length); - for( int i = 0; i < pVertexX.length; i++) - { - Vector2d v = new Vector2d(pVertexX[i], pVertexY[i]); - vertices.add(v); - } - return vertices; - } - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - - public static interface IPolygonBaseVertexBufferObject extends IVertexBufferObject { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Methods - // =========================================================== - - public void onUpdateColor(final PolygonBase pPolygon); - public void onUpdateVertices(final PolygonBase pPolygon); - } - - public static class HighPerformancePolygonBaseVertexBufferObject extends HighPerformanceVertexBufferObject implements IPolygonBaseVertexBufferObject { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - // =========================================================== - // Constructors - // =========================================================== - - public HighPerformancePolygonBaseVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { - super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - @Override - public void onUpdateColor(final PolygonBase pPolygon) { - final float[] bufferData = this.mBufferData; - - final float packedColor = pPolygon.getColor().getFloatPacked(); - - int nbVertexInTriangles = pPolygon.getVertices().size(); - for( int i = 0; i < nbVertexInTriangles; i++) - { - // TODO use color per vertex - bufferData[i * Polygon.VERTEX_SIZE + Polygon.COLOR_INDEX] = packedColor; - } - - this.setDirtyOnHardware(); - } - - @Override - public void onUpdateVertices(final PolygonBase pPolygon) { - final float[] bufferData = this.mBufferData; - - ArrayList vertexTriangles = pPolygon.getVertices(); - int nbVertexInTriangles = vertexTriangles.size(); - - for( int i = 0; i < nbVertexInTriangles; i++) - { - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_X] = vertexTriangles.get(i).getX(); - bufferData[i * Polygon.VERTEX_SIZE + Polygon.VERTEX_INDEX_Y] = vertexTriangles.get(i).getY(); - } - - this.setDirtyOnHardware(); - } - - // =========================================================== - // Methods - // =========================================================== - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - } - - public static class LowMemoryPolygonBaseVertexBufferObject extends LowMemoryVertexBufferObject implements IPolygonBaseVertexBufferObject { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - // =========================================================== - // Constructors - // =========================================================== - - public LowMemoryPolygonBaseVertexBufferObject(final VertexBufferObjectManager pVertexBufferObjectManager, final int pCapacity, final DrawType pDrawType, final boolean pManaged, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) { - super(pVertexBufferObjectManager, pCapacity, pDrawType, pManaged, pVertexBufferObjectAttributes); - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - @Override - public void onUpdateColor(final PolygonBase pPolygon) { - - // TODO - this.setDirtyOnHardware(); - } - - @Override - public void onUpdateVertices(final PolygonBase pPolygon) { - - // TODO - this.setDirtyOnHardware(); - } - - // =========================================================== - // Methods - // =========================================================== - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - } - - protected static class Vector2d - { - - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - private float mX; - private float mY; - - // =========================================================== - // Constructors - // =========================================================== - - public Vector2d(float x, float y) - { - set(x,y); - }; - - // =========================================================== - // Getter & Setter - // =========================================================== - - public float getX() - { - return mX; - } - - public float getY() - { - return mY; - } - - public void set(float x, float y) - { - mX = x; - mY = y; - } - - protected static Vector2d[] floatToList( float pX[], float pY[] ) - { - assert( pX.length == pY.length ); - Vector2d[] vertex = new Vector2d[pX.length]; - for( int i = 0; i < pX.length; i++ ) - { - vertex[i] = new Vector2d(pX[i], pY[i]); - } - return vertex; - } - } - - public static enum DrawMode { - // =========================================================== - // Elements - // =========================================================== - - GL_POINTS(GLES20.GL_POINTS), - GL_LINE_STRIP(GLES20.GL_LINE_STRIP), - GL_LINE_LOOP(GLES20.GL_LINE_LOOP), - GL_LINES(GLES20.GL_LINES), - GL_TRIANGLE_STRIP(GLES20.GL_TRIANGLE_STRIP), - GL_TRIANGLE_FAN(GLES20.GL_TRIANGLE_FAN), - GL_TRIANGLES(GLES20.GL_TRIANGLES); - - - // =========================================================== - // Constants - // =========================================================== - - private final int mUsage; - - // =========================================================== - // Fields - // =========================================================== - - // =========================================================== - // Constructors - // =========================================================== - - private DrawMode(final int pUsage) { - this.mUsage = pUsage; - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - public int getUsage() { - return this.mUsage; - } - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - // =========================================================== - // Methods - // =========================================================== - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - } -} diff --git a/src/org/andengine/entity/shape/PolygonShape.java b/src/org/andengine/entity/shape/PolygonShape.java deleted file mode 100644 index a227eb84d..000000000 --- a/src/org/andengine/entity/shape/PolygonShape.java +++ /dev/null @@ -1,115 +0,0 @@ -/** - * - */ -package org.andengine.entity.shape; - -import org.andengine.engine.camera.Camera; -import org.andengine.entity.primitive.Line; -import org.andengine.opengl.shader.ShaderProgram; - -/** - * - * @author Rodrigo Castro - * @since 22:10:11 - 28.01.2012 - */ -public abstract class PolygonShape extends Shape { - // =========================================================== - // Constants - // =========================================================== - - // =========================================================== - // Fields - // =========================================================== - - // =========================================================== - // Constructors - // =========================================================== - - public PolygonShape(float pX, float pY, ShaderProgram pShaderProgram) { - super(pX, pY, pShaderProgram); - // TODO Auto-generated constructor stub - - this.resetRotationCenter(); - this.resetScaleCenter(); - this.resetSkewCenter(); - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - public void setBaseSize() { - // TODO - } - - @Override - public boolean isCulled(final Camera pCamera) { - // TODO - return true; - } - - @Override - public void reset() { - super.reset(); - this.setBaseSize(); - - this.resetRotationCenter(); - this.resetSkewCenter(); - this.resetScaleCenter(); - } - - @Override - public boolean contains(final float pX, final float pY) { - // TODO - return false; - } - - @Override - public float[] getSceneCenterCoordinates() { - // TODO test this - return this.convertLocalToSceneCoordinates(mX, mY); - } - - @Override - public boolean collidesWith(final IShape pOtherShape) { - if(pOtherShape instanceof RectangularShape) { - // TODO - return false; - } else if(pOtherShape instanceof Line) { - // TODO - return false; - } else { - return false; - } - } - - // =========================================================== - // Methods - // =========================================================== - - public void resetRotationCenter() { - this.mRotationCenterX = mX; - this.mRotationCenterY = mY; - } - - public void resetScaleCenter() { - // TODO test this - this.mScaleCenterX = mX; - this.mScaleCenterY = mY; - } - - public void resetSkewCenter() { - // TODO test this - this.mSkewCenterX = mX; - this.mSkewCenterY = mY; - } - - // =========================================================== - // Inner and Anonymous Classes - // =========================================================== - -} From 4e3eb34fa8b3ce1b3f4725d632c844c0e11ea3da Mon Sep 17 00:00:00 2001 From: recastro Date: Tue, 28 Feb 2012 23:11:44 +0100 Subject: [PATCH 20/20] Fixed compiler error --- src/org/andengine/entity/primitive/TexturedMesh.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/andengine/entity/primitive/TexturedMesh.java b/src/org/andengine/entity/primitive/TexturedMesh.java index 4abb7c3c2..7666c810e 100644 --- a/src/org/andengine/entity/primitive/TexturedMesh.java +++ b/src/org/andengine/entity/primitive/TexturedMesh.java @@ -283,7 +283,7 @@ public HighPerformanceTexturedMeshVertexBufferObject(final VertexBufferObjectMan public void onUpdateColor(final TexturedMesh pMesh) { final float[] bufferData = this.mBufferData; - final float packedColor = pMesh.getColor().getFloatPacked(); + final float packedColor = pMesh.getColor().getABGRPackedFloat(); for(int i = 0; i < this.mVertexCount; i++) { bufferData[(i * TexturedMesh.VERTEX_SIZE) + TexturedMesh.COLOR_INDEX] = packedColor;