Project

General

Profile

« Previous | Next » 

Revision 9361b337

Added by Leszek Koltunski almost 8 years ago

Provide support to add any class derived from DistortedObject to DistortedNode.

View differences:

src/main/java/org/distorted/library/DistortedBitmap.java
78 78
     this(bmp.getWidth(), bmp.getHeight(), gridSize); 
79 79
     setBitmap(bmp);
80 80
     }
81
   
81

  
82 82
///////////////////////////////////////////////////////////////////////////////////////////////////
83 83
/**
84
 * Copy constructor used to create a DistortedBitmap based on various parts of another Bitmap.
84
 * Copy constructor used to create a DistortedBitmap based on various parts of another object.
85 85
 * <p>
86 86
 * Whatever we do not clone gets created just like in the default constructor.
87
 *    
88
 * @param db    Source DistortedBitmap to create our object from
87
 *
88
 * @param db    Source object to create our object from
89 89
 * @param flags A bitmask of values specifying what to copy.
90 90
 *              For example, CLONE_BITMAP | CLONE_MATRIX.
91 91
 */
92 92
   public DistortedBitmap(DistortedBitmap db, int flags)
93 93
     {
94
     initializeEffectLists(db,flags);  
95
      
96
     mID = DistortedObjectList.add(this);
97
        
98
     mSizeX = db.mSizeX;
99
     mSizeY = db.mSizeY;
100
     mSizeZ = db.mSizeZ;
101
     mSize  = db.mSize;
102
     mGrid  = db.mGrid;
103
       
104
     if( (flags & Distorted.CLONE_BITMAP) != 0 ) 
105
       {
106
       mTextureDataH = db.mTextureDataH;
107
 	    mBmp          = db.mBmp;
108
 	    mBitmapSet    = db.mBitmapSet;
109
 	    }
110
     else
111
       {
112
       mTextureDataH   = new int[1];
113
       mTextureDataH[0]= 0;
114
       mBitmapSet      = new boolean[1];
115
       mBitmapSet[0]   = false;
116
       mBmp            = new Bitmap[1];
117
       mBmp[0]         = null;
118
       
119
       if( Distorted.isInitialized() ) resetTexture();
120
       }
94
     super(db,flags);
121 95
     }
122 96

  
123 97
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedCubes.java
1 1
package org.distorted.library;
2 2

  
3
import android.graphics.Bitmap;
4

  
5 3
///////////////////////////////////////////////////////////////////////////////////////////////////
6 4
/**
7
 * Instance of this class represents a connected, flat set of cubes optionally textured as a whole.
5
 * Instance of this class represents a flat set of cubes optionally textured as a whole.
8 6
 * (a subset of a NxMx1 cuboid build with 1x1x1 cubes, i.e. the MxNx1 cuboid with arbitrary cubes missing)
9 7
 * <p>
10 8
 * General idea is as follows:
......
30 28
   
31 29
///////////////////////////////////////////////////////////////////////////////////////////////////
32 30
// PUBLIC API
33
/////////////////////////////////////////////////////////////////////////////////////////////////// 
34
 
31
///////////////////////////////////////////////////////////////////////////////////////////////////
35 32
/**
36 33
 * Creates internal memory representation of a cuboid subset.
37
 * 
34
 *
38 35
 * @param cols Integer helping to parse the next parameter.
39 36
 * @param desc String describing the subset of a MxNx1 cuboid that we want to create.
40 37
 *             Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
41
 *             
38
 *
42 39
 *             For example, (cols=2, desc="111010") describes the following shape:
43
 *             
40
 *
44 41
 *             XX
45 42
 *             X
46 43
 *             X
47
 *             
44
 *
48 45
 *             whereas (cols=2,desc="110001") describes
49
 *             
46
 *
50 47
 *             XX
51
 *              
48
 *
52 49
 *              X
53
 *              
50
 *
54 51
 * @param gridSize size, in pixels, of the single 1x1x1 cube our cuboid is built from
55
 *  
56
 */   
57
   public DistortedCubes(int cols, String desc, int gridSize) 
58
     {
59
     this(cols,desc,gridSize,false);
60
     }
61

  
62
/**
63 52
 * @param frontOnly Only create the front wall or side and back as well?
64 53
 */
65
   
66
   public DistortedCubes(int cols, String desc, int gridSize, boolean frontOnly) 
67
     {
68
     int Rs = 0;
69
     int Cs = 0;
54
 public DistortedCubes(int cols, String desc, int gridSize, boolean frontOnly)
55
   {
56
   int Rs = 0;
57
   int Cs = 0;
70 58
     
71
     if( cols>0 )
72
       {
73
       int reallen = desc.length();
74
       int len = reallen;
59
   if( cols>0 )
60
     {
61
     int reallen = desc.length();
62
     int len = reallen;
75 63

  
76
       if( (reallen/cols)*cols != reallen )
77
         {
78
         len = ((reallen/cols)+1)*cols; 
79
         for(int i=reallen; i<len; i++) desc += "0";
80
         }
64
     if( (reallen/cols)*cols != reallen )
65
       {
66
       len = ((reallen/cols)+1)*cols;
67
       for(int i=reallen; i<len; i++) desc += "0";
68
       }
81 69
    
82
       if( desc.indexOf("1")>=0 )
83
         {
84
         Cs = cols;
85
         Rs = len/cols;
86
         }
70
     if( desc.indexOf("1")>=0 )
71
       {
72
       Cs = cols;
73
       Rs = len/cols;
87 74
       }
88
     
89
     mSizeX= gridSize*Cs;
90
     mSizeY= gridSize*Rs;
91
     mSizeZ= frontOnly ? 0 : gridSize;
92
     mGrid = new GridCubes(cols,desc, frontOnly);
93
     initializeData(gridSize);
94 75
     }
95
   
76
     
77
   mSizeX= gridSize*Cs;
78
   mSizeY= gridSize*Rs;
79
   mSizeZ= frontOnly ? 0 : gridSize;
80
   mGrid = new GridCubes(cols,desc, frontOnly);
81
   initializeData(gridSize);
82
   }
83

  
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
/**
86
 * Convenience constructor
87
 */
88
 public DistortedCubes(int cols, String desc, int gridSize)
89
   {
90
   this(cols,desc,gridSize,false);
91
   }
92

  
96 93
///////////////////////////////////////////////////////////////////////////////////////////////////
97 94
/**
98 95
 * Copy constructor used to create a DistortedCubes based on various parts of another object.
99 96
 * <p>
100 97
 * Whatever we do not clone gets created just like in the default constructor.
101
 *    
98
 *
102 99
 * @param dc    Source object to create our object from
103 100
 * @param flags A bitmask of values specifying what to copy.
104 101
 *              For example, CLONE_BITMAP | CLONE_MATRIX.
105 102
 */
106
   public DistortedCubes(DistortedCubes dc, int flags)
107
     {
108
     initializeEffectLists(dc,flags);  
109
      
110
     mID = DistortedObjectList.add(this);
111
        
112
     mSizeX = dc.mSizeX;
113
     mSizeY = dc.mSizeY;
114
     mSizeZ = dc.mSizeZ;
115
     mSize  = dc.mSize;
116
     mGrid  = dc.mGrid;
117
       
118
     if( (flags & Distorted.CLONE_BITMAP) != 0 ) 
119
       {
120
       mTextureDataH = dc.mTextureDataH;
121
       mBmp          = dc.mBmp;
122
       mBitmapSet    = dc.mBitmapSet;
123
       }
124
     else
125
       {
126
       mTextureDataH   = new int[1];
127
       mTextureDataH[0]= 0;
128
       mBitmapSet      = new boolean[1];
129
       mBitmapSet[0]   = false;
130
       mBmp            = new Bitmap[1];
131
       mBmp[0]         = null;
132
       
133
       if( Distorted.isInitialized() ) resetTexture(); 
134
       }
135
     }
103
 public DistortedCubes(DistortedCubes dc, int flags)
104
   {
105
   super(dc,flags);
106
   }
136 107

  
137 108
///////////////////////////////////////////////////////////////////////////////////////////////////   
138
   }
109
 }
src/main/java/org/distorted/library/DistortedNode.java
18 18
  private static final int TEXTURE_FAILED_TO_CREATE = -1;   
19 19
  private static final int TEXTURE_NOT_CREATED_YET  = -2;   
20 20
   
21
  private DistortedBitmap mBitmap;
21
  private DistortedObject mObject;
22 22
  private NodeData mData;
23 23
  
24 24
  private DistortedNode mParent;
25 25
  private ArrayList<DistortedNode> mChildren;
26 26
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
27 27
  
28
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<ArrayList<Long>,NodeData>();
28
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
29 29
  private static long mNextNodeID =0;
30 30

  
31 31
  //////////////////////////////////////////////////////////////////
......
124 124
    {
125 125
    if( mNumChildren[0]<=0 )
126 126
      {
127
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBitmap.mTextureDataH[0]); 
127
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
128 128
      
129 129
      if( mData.mProjection!=null )
130 130
        {
......
144 144
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
145 145
        GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
146 146
      
147
        if( mBitmap.mBitmapSet[0] )
147
        if( mObject.mBitmapSet[0] )
148 148
          {
149
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBitmap.mTextureDataH[0]);        
150
          mBitmap.drawNoEffectsPriv(mData.mProjection);
149
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
150
          mObject.drawNoEffectsPriv(mData.mProjection);
151 151
          }
152 152
      
153 153
        synchronized(this)
......
163 163
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mData.mTextureID);   // this is safe because we must have called createFBO() above before.     
164 164
      }
165 165
    
166
    mBitmap.drawPriv(currTime, dp);
166
    mObject.drawPriv(currTime, dp);
167 167
    }
168 168
  
169 169
///////////////////////////////////////////////////////////////////////////////////////////////////
......
189 189

  
190 190
  private ArrayList<Long> generateIDList()
191 191
    {
192
    ArrayList<Long> ret = new ArrayList<Long>();
192
    ArrayList<Long> ret = new ArrayList<>();
193 193
     
194
    ret.add( mNumChildren[0]>0 ? mBitmap.getBitmapID() : mBitmap.getID() );
194
    ret.add( mNumChildren[0]>0 ? mObject.getBitmapID() : mObject.getID() );
195 195
    DistortedNode node;
196 196
   
197 197
    for(int i=0; i<mNumChildren[0]; i++)
......
220 220
      if( newList.size()>1 && mData.mProjection==null )
221 221
        {     
222 222
        mData.mProjection = new DistortedProjection(true);
223
        mData.mProjection.onSurfaceChanged(mBitmap.getWidth(), mBitmap.getHeight());
223
        mData.mProjection.onSurfaceChanged(mObject.getWidth(), mObject.getHeight());
224 224
        mData.mFramebufferID = 0;
225 225
        mData.mTextureID = TEXTURE_NOT_CREATED_YET;
226 226
        }
......
260 260
/**
261 261
 * Constructs new Node of the Tree.
262 262
 *     
263
 * @param bmp DistortedBitmap to put into the new Node.
263
 * @param obj DistortedObject to put into the new Node.
264 264
 */
265
  public DistortedNode(DistortedBitmap bmp)
265
  public DistortedNode(DistortedObject obj)
266 266
    {
267
    mBitmap = bmp;
267
    mObject = obj;
268 268
    mParent = null;
269 269
    mChildren = null;
270 270
    mNumChildren = new int[1];
271 271
    mNumChildren[0] = 0;
272 272
   
273
    ArrayList<Long> list = new ArrayList<Long>();
274
    list.add(bmp.getID());
273
    ArrayList<Long> list = new ArrayList<>();
274
    list.add(obj.getID());
275 275
      
276 276
    mData = mMapNodeID.get(list);
277 277
   
......
298 298
 */
299 299
  public DistortedNode(DistortedNode node, int flags)
300 300
    {
301
    mParent = null;  
302
    mBitmap = new DistortedBitmap(node.mBitmap, flags);  
303
   
304
    if( (flags & Distorted.CLONE_CHILDREN) != 0 ) 
301
    mParent = null;
302

  
303
    if( node.mObject instanceof DistortedBitmap)
304
      mObject = new DistortedBitmap( (DistortedBitmap)node.mObject, flags);
305
    else if( node.mObject instanceof DistortedCubes)
306
      mObject = new DistortedCubes( (DistortedCubes)node.mObject, flags);
307

  
308
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
305 309
      {
306 310
      mChildren = node.mChildren;
307 311
      mNumChildren = node.mNumChildren;
......
338 342
    {
339 343
    ArrayList<Long> prev = generateIDList(); 
340 344
   
341
    if( mChildren==null ) mChildren = new ArrayList<DistortedNode>(2);
345
    if( mChildren==null ) mChildren = new ArrayList<>(2);
342 346
     
343 347
    node.mParent = this;
344 348
    mChildren.add(node);
......
358 362
    {
359 363
    ArrayList<Long> prev = generateIDList(); 
360 364
      
361
    if( mChildren==null ) mChildren = new ArrayList<DistortedNode>(2);  
365
    if( mChildren==null ) mChildren = new ArrayList<>(2);
362 366
    DistortedNode node = new DistortedNode(bmp);
363 367
    node.mParent = this;
364 368
    mChildren.add(node);
......
400 404
/**
401 405
 * Removes the first occurrence of a specified child from the list of children of our Node.
402 406
 * 
403
 * @param bmp The DistortedBitmap to remove.
407
 * @param obj DistortedObject to remove.
404 408
 * @return <code>true</code> if the child was successfully removed.
405 409
 */
406
  public synchronized boolean detach(DistortedBitmap bmp)
410
  public synchronized boolean detach(DistortedObject obj)
407 411
    {
408
    long id = bmp.getID();
412
    long id = obj.getID();
409 413
    DistortedNode node;
410 414
   
411 415
    for(int i=0; i<mNumChildren[0]; i++)
412 416
      {
413 417
      node = mChildren.get(i);
414 418
     
415
      if( node.mBitmap.getID()==id )
419
      if( node.mObject.getID()==id )
416 420
        {
417 421
        ArrayList<Long> prev = generateIDList();   
418 422
     
......
468 472
 
469 473
///////////////////////////////////////////////////////////////////////////////////////////////////
470 474
/**
471
 * Returns the DistortedBitmap object that's in the Node.
475
 * Returns the DistortedObject object that's in the Node.
472 476
 * 
473
 * @return The DistortedBitmap contained in the Node.
477
 * @return The DistortedObject contained in the Node.
474 478
 */
475
  public DistortedBitmap getBitmap()
479
  public DistortedObject getObject()
476 480
    {
477
    return mBitmap;  
481
    return mObject;
478 482
    }
479 483
  
480 484
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedObject.java
27 27
    protected Bitmap[] mBmp= null; // 
28 28
    int[] mTextureDataH;           // have to be shared among all the cloned Objects
29 29
    boolean[] mBitmapSet;          // 
30
 
30

  
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

  
33
  /**
34
   * Default empty constructor so that derived classes can call it
35
   */
36
    public DistortedObject()
37
      {
38

  
39
      }
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
  /**
43
   * Copy constructor used to create a DistortedObject based on various parts of another object.
44
   * <p>
45
   * Whatever we do not clone gets created just like in the default constructor.
46
   *
47
   * @param dc    Source object to create our object from
48
   * @param flags A bitmask of values specifying what to copy.
49
   *              For example, CLONE_BITMAP | CLONE_MATRIX.
50
   */
51
    public DistortedObject(DistortedObject dc, int flags)
52
      {
53
      initializeEffectLists(dc,flags);
54

  
55
      mID = DistortedObjectList.add(this);
56

  
57
      mSizeX = dc.mSizeX;
58
      mSizeY = dc.mSizeY;
59
      mSizeZ = dc.mSizeZ;
60
      mSize  = dc.mSize;
61
      mGrid  = dc.mGrid;
62

  
63
      if( (flags & Distorted.CLONE_BITMAP) != 0 )
64
        {
65
        mTextureDataH = dc.mTextureDataH;
66
        mBmp          = dc.mBmp;
67
        mBitmapSet    = dc.mBitmapSet;
68
        }
69
      else
70
        {
71
        mTextureDataH   = new int[1];
72
        mTextureDataH[0]= 0;
73
        mBitmapSet      = new boolean[1];
74
        mBitmapSet[0]   = false;
75
        mBmp            = new Bitmap[1];
76
        mBmp[0]         = null;
77

  
78
        if( Distorted.isInitialized() ) resetTexture();
79
        }
80
      }
81

  
31 82
///////////////////////////////////////////////////////////////////////////////////////////////////
32 83

  
33 84
    protected void initializeData(int size)
src/main/java/org/distorted/library/DistortedObjectList.java
10 10
final class DistortedObjectList 
11 11
  {
12 12
  private static long id =0;  
13
  private static HashMap<Long,DistortedObject> mBitmaps = new HashMap<Long,DistortedObject>();
13
  private static HashMap<Long,DistortedObject> mBitmaps = new HashMap<>();
14 14
  
15 15
///////////////////////////////////////////////////////////////////////////////////////////////////
16 16

  

Also available in: Unified diff