Revision 550db260
Added by Leszek Koltunski almost 3 years ago
src/main/java/org/distorted/bandaged/BandagedCreatorRenderer.java | ||
---|---|---|
38 | 38 |
private final BandagedCreatorView mView; |
39 | 39 |
private final DistortedScreen mScreen; |
40 | 40 |
private final Static3D mScale; |
41 |
private final Static3D mMoveWhole; |
|
42 |
private final Static3D[] mMove; |
|
43 |
private final Static3D[] mMoveUnscaled; |
|
41 |
private final Static3D mMoveY; |
|
44 | 42 |
private final BandagedCubit[] mCubits; |
43 |
private float mScaleValue; |
|
45 | 44 |
|
46 | 45 |
static final int COLOR_DEFAULT = 0xffffff55; |
47 | 46 |
static final int COLOR_MARKED = 0xffff0000; |
... | ... | |
95 | 94 |
|
96 | 95 |
mScreen = new DistortedScreen(); |
97 | 96 |
mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f); |
98 |
mScale= new Static3D(1,1,1); |
|
99 |
mMoveWhole = new Static3D(0,0,0); |
|
100 |
|
|
101 |
int len = POSITIONS.length; |
|
102 |
mMove = new Static3D[len]; |
|
103 |
mMoveUnscaled = new Static3D[len]; |
|
104 |
|
|
105 |
for(int i=0; i<len; i++) |
|
106 |
{ |
|
107 |
mMove[i] = new Static3D(0,0,0); |
|
108 |
mMoveUnscaled[i] = new Static3D(0,0,0); |
|
109 |
} |
|
110 |
|
|
111 |
mCubits = createCubits(); |
|
97 |
mScale = new Static3D(1,1,1); |
|
98 |
mMoveY = new Static3D(0,0,0); |
|
99 |
mCubits= createCubits(); |
|
112 | 100 |
} |
113 | 101 |
|
114 | 102 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
120 | 108 |
|
121 | 109 |
for(int c=0; c<len; c++) |
122 | 110 |
{ |
123 |
computeMove(mMoveUnscaled[c],POSITIONS[c]); |
|
124 |
cubits[c] = new BandagedCubit(POSITIONS[c],mQuat1,mQuat2,mMove[c],mMoveWhole,mScale, COLOR_DEFAULT); |
|
111 |
cubits[c] = new BandagedCubit(POSITIONS[c],mQuat1,mQuat2,mMoveY,mScale,COLOR_DEFAULT); |
|
125 | 112 |
} |
126 | 113 |
|
127 | 114 |
return cubits; |
128 | 115 |
} |
129 | 116 |
|
130 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
131 |
|
|
132 |
private void computeMove(Static3D move, float[] position) |
|
133 |
{ |
|
134 |
int numCenters = position.length/3; |
|
135 |
float totalX=0.0f, totalY=0.0f, totalZ=0.0f; |
|
136 |
|
|
137 |
for(int center=0; center<numCenters; center++) |
|
138 |
{ |
|
139 |
totalX += position[3*center ]; |
|
140 |
totalY += position[3*center+1]; |
|
141 |
totalZ += position[3*center+2]; |
|
142 |
} |
|
143 |
|
|
144 |
totalX /= numCenters; |
|
145 |
totalY /= numCenters; |
|
146 |
totalZ /= numCenters; |
|
147 |
|
|
148 |
move.set(totalX,totalY,totalZ); |
|
149 |
} |
|
150 |
|
|
151 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
152 |
|
|
153 |
public BandagedCubit[] getCubits() |
|
154 |
{ |
|
155 |
return mCubits; |
|
156 |
} |
|
157 |
|
|
158 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
159 |
|
|
160 |
public DistortedScreen getScreen() |
|
161 |
{ |
|
162 |
return mScreen; |
|
163 |
} |
|
164 |
|
|
165 | 117 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
166 | 118 |
|
167 | 119 |
@Override |
... | ... | |
177 | 129 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
178 | 130 |
{ |
179 | 131 |
final float Q = SCREEN_RATIO/OBJECT_SIZE; |
180 |
float scale = width<height ? Q*width : Q*height;
|
|
132 |
mScaleValue = width<height ? Q*width : Q*height;
|
|
181 | 133 |
|
182 | 134 |
mScreen.detachAll(); |
183 | 135 |
int len = POSITIONS.length; |
184 | 136 |
|
185 | 137 |
for(int i=0; i<len; i++) |
186 | 138 |
{ |
187 |
float x = mMoveUnscaled[i].get0(); |
|
188 |
float y = mMoveUnscaled[i].get1(); |
|
189 |
float z = mMoveUnscaled[i].get2(); |
|
190 |
|
|
191 |
mMove[i].set(x*scale,y*scale,z*scale); |
|
192 |
|
|
139 |
mCubits[i].scaleMove(mScaleValue); |
|
193 | 140 |
mCubits[i].setTexture(COLOR_DEFAULT); |
194 | 141 |
DistortedNode node = mCubits[i].getNode(); |
195 | 142 |
mScreen.attach(node); |
... | ... | |
198 | 145 |
float RB = BandagedCreatorActivity.RATIO_BAR; |
199 | 146 |
float RS = BandagedCreatorActivity.RATIO_SCROLL; |
200 | 147 |
float yOffset = (0.5f*RB/(1-RS))*height; |
201 |
mMoveWhole.set1(yOffset);
|
|
148 |
mMoveY.set1(yOffset);
|
|
202 | 149 |
|
203 |
mScale.set( scale,scale,scale );
|
|
150 |
mScale.set( mScaleValue,mScaleValue,mScaleValue );
|
|
204 | 151 |
mScreenMin = Math.min(width, height); |
205 | 152 |
mView.setScreenSize(width,height, (int)yOffset); |
206 | 153 |
mScreen.resize(width,height); |
... | ... | |
220 | 167 |
{ |
221 | 168 |
android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() ); |
222 | 169 |
} |
170 |
|
|
171 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
172 |
|
|
173 |
public BandagedCubit[] getCubits() |
|
174 |
{ |
|
175 |
return mCubits; |
|
176 |
} |
|
177 |
|
|
178 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
179 |
|
|
180 |
public DistortedScreen getScreen() |
|
181 |
{ |
|
182 |
return mScreen; |
|
183 |
} |
|
184 |
|
|
185 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
186 |
|
|
187 |
private boolean isAdjacent(float[] pos1, float[] pos2) |
|
188 |
{ |
|
189 |
int len1 = pos1.length/3; |
|
190 |
int len2 = pos2.length/3; |
|
191 |
|
|
192 |
for(int i=0; i<len1; i++) |
|
193 |
for(int j=0; j<len2; j++) |
|
194 |
{ |
|
195 |
float d0 = pos1[3*i ] - pos2[3*j ]; |
|
196 |
float d1 = pos1[3*i+1] - pos2[3*j+1]; |
|
197 |
float d2 = pos1[3*i+2] - pos2[3*j+2]; |
|
198 |
|
|
199 |
if( d0*d0 + d1*d1 + d2*d2 == 1 ) return true; |
|
200 |
} |
|
201 |
|
|
202 |
return false; |
|
203 |
} |
|
204 |
|
|
205 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
206 |
|
|
207 |
public void tryConnectingCubits(int index1, int index2) |
|
208 |
{ |
|
209 |
if( index1!=index2 ) |
|
210 |
{ |
|
211 |
float[] pos1 = mCubits[index1].getPosition(); |
|
212 |
float[] pos2 = mCubits[index2].getPosition(); |
|
213 |
|
|
214 |
if( isAdjacent(pos1,pos2) ) |
|
215 |
{ |
|
216 |
mCubits[index2].prepareJoin(pos1); |
|
217 |
|
|
218 |
mCubits[index1].detach(); |
|
219 |
DistortedNode node1 = mCubits[index1].getNode(); |
|
220 |
mScreen.detach(node1); |
|
221 |
DistortedNode node2 = mCubits[index2].getNode(); |
|
222 |
mScreen.detach(node2); |
|
223 |
mCubits[index2].swapNodes(mScaleValue); |
|
224 |
DistortedNode node3 = mCubits[index2].getNode(); |
|
225 |
mScreen.attach(node3); |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
223 | 229 |
} |
src/main/java/org/distorted/bandaged/BandagedCreatorScreen.java | ||
---|---|---|
25 | 25 |
import org.distorted.helpers.TransparentImageButton; |
26 | 26 |
import org.distorted.main.R; |
27 | 27 |
import org.distorted.main.RubikActivity; |
28 |
import org.distorted.objectlib.helpers.FactoryCubit; |
|
28 | 29 |
|
29 | 30 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
30 | 31 |
|
... | ... | |
90 | 91 |
@Override |
91 | 92 |
public void onClick(View v) |
92 | 93 |
{ |
93 |
|
|
94 |
FactoryCubit factory = FactoryCubit.getInstance(); |
|
95 |
factory.printFaceTransform(); |
|
94 | 96 |
} |
95 | 97 |
}); |
96 | 98 |
} |
src/main/java/org/distorted/bandaged/BandagedCreatorView.java | ||
---|---|---|
37 | 37 |
{ |
38 | 38 |
private final static int DIRECTION_SENSITIVITY= 12; |
39 | 39 |
private int mX, mY; |
40 |
private int mTouchedIndex; |
|
40 |
private int mTouchedIndex1, mTouchedIndex2;
|
|
41 | 41 |
private BandagedCreatorRenderer mRenderer; |
42 | 42 |
private BandagedTouchControl mTouchControl; |
43 | 43 |
private int mScreenWidth, mScreenHeight; |
... | ... | |
54 | 54 |
mX = -1; |
55 | 55 |
mY = -1; |
56 | 56 |
|
57 |
mTouchedIndex1 = -1; |
|
58 |
mTouchedIndex2 = -1; |
|
59 |
|
|
57 | 60 |
if(!isInEditMode()) |
58 | 61 |
{ |
59 | 62 |
BandagedCreatorActivity act = (BandagedCreatorActivity)context; |
... | ... | |
136 | 139 |
case MotionEvent.ACTION_DOWN: float x1 = (x - mScreenWidth*0.5f)/mRenderer.mScreenMin; |
137 | 140 |
float y1 = (mScreenHeight*0.5f - y)/mRenderer.mScreenMin; |
138 | 141 |
|
139 |
mTouchedIndex = mTouchControl.cubitTouched(x1,y1,mRenderer.mQuat2);
|
|
142 |
int index = mTouchControl.cubitTouched(x1,y1,mRenderer.mQuat2);
|
|
140 | 143 |
|
141 |
if( mTouchedIndex<0 )
|
|
144 |
if( index<0 )
|
|
142 | 145 |
{ |
143 | 146 |
mX = x; |
144 | 147 |
mY = y; |
... | ... | |
148 | 151 |
mX = -1; |
149 | 152 |
mY = -1; |
150 | 153 |
|
151 |
mTouchControl.markCubit(mTouchedIndex, BandagedCreatorRenderer.COLOR_MARKED); |
|
154 |
if( mTouchedIndex1<0 ) |
|
155 |
{ |
|
156 |
mTouchedIndex1 = index; |
|
157 |
mTouchControl.markCubit(mTouchedIndex1, BandagedCreatorRenderer.COLOR_MARKED); |
|
158 |
} |
|
159 |
else if( mTouchedIndex1 != index ) |
|
160 |
{ |
|
161 |
mTouchedIndex2 = index; |
|
162 |
mTouchControl.markCubit(mTouchedIndex2, BandagedCreatorRenderer.COLOR_MARKED); |
|
163 |
} |
|
152 | 164 |
} |
153 | 165 |
|
154 | 166 |
break; |
... | ... | |
180 | 192 |
} |
181 | 193 |
break; |
182 | 194 |
|
183 |
case MotionEvent.ACTION_UP : mX = -1; |
|
184 |
mY = -1; |
|
185 |
|
|
186 |
if( mTouchedIndex>=0 ) |
|
195 |
case MotionEvent.ACTION_UP : if( mTouchedIndex2>=0 ) |
|
187 | 196 |
{ |
188 |
mTouchControl.markCubit(mTouchedIndex, BandagedCreatorRenderer.COLOR_DEFAULT); |
|
189 |
mTouchedIndex = -1; |
|
197 |
mTouchControl.markCubit(mTouchedIndex1, BandagedCreatorRenderer.COLOR_DEFAULT); |
|
198 |
mTouchControl.markCubit(mTouchedIndex2, BandagedCreatorRenderer.COLOR_DEFAULT); |
|
199 |
|
|
200 |
mRenderer.tryConnectingCubits(mTouchedIndex1,mTouchedIndex2); |
|
201 |
|
|
202 |
mTouchedIndex1 = -1; |
|
203 |
mTouchedIndex2 = -1; |
|
190 | 204 |
} |
191 | 205 |
|
206 |
mX = -1; |
|
207 |
mY = -1; |
|
208 |
|
|
192 | 209 |
resetQuats(); |
193 | 210 |
break; |
194 | 211 |
} |
src/main/java/org/distorted/bandaged/BandagedCubit.java | ||
---|---|---|
36 | 36 |
{ |
37 | 37 |
private static final Static3D CENTER = new Static3D(0,0,0); |
38 | 38 |
|
39 |
private final DistortedNode mNode;
|
|
39 |
private DistortedNode mNode, mNewNode;
|
|
40 | 40 |
private final DistortedTexture mTexture; |
41 | 41 |
private final DistortedEffects mEffects; |
42 |
private final MeshBase mMesh; |
|
43 |
private final float[] mPosition; |
|
42 |
private final Static3D mMove; |
|
43 |
|
|
44 |
private float mUnscaledX, mUnscaledY, mUnscaledZ; |
|
45 |
private float[] mPosition, mNewPosition; |
|
44 | 46 |
private boolean mIsAttached; |
45 | 47 |
|
48 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
49 |
|
|
50 |
private void computeMove(float[] position) |
|
51 |
{ |
|
52 |
int numCenters = position.length/3; |
|
53 |
mUnscaledX=0.0f; |
|
54 |
mUnscaledY=0.0f; |
|
55 |
mUnscaledZ=0.0f; |
|
56 |
|
|
57 |
for(int center=0; center<numCenters; center++) |
|
58 |
{ |
|
59 |
mUnscaledX += position[3*center ]; |
|
60 |
mUnscaledY += position[3*center+1]; |
|
61 |
mUnscaledZ += position[3*center+2]; |
|
62 |
} |
|
63 |
|
|
64 |
mUnscaledX /= numCenters; |
|
65 |
mUnscaledY /= numCenters; |
|
66 |
mUnscaledZ /= numCenters; |
|
67 |
} |
|
68 |
|
|
46 | 69 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
47 | 70 |
// PUBLIC API |
48 | 71 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
49 | 72 |
|
50 |
public BandagedCubit(float[] position, Static4D quat1, Static4D quat2, Static3D move1, Static3D move2, Static3D scale, int color)
|
|
73 |
public BandagedCubit(float[] position, Static4D quat1, Static4D quat2, Static3D moveY, Static3D scale, int color)
|
|
51 | 74 |
{ |
52 | 75 |
mPosition = position; |
53 | 76 |
mIsAttached = true; |
54 | 77 |
|
78 |
computeMove(mPosition); |
|
79 |
mMove = new Static3D(0,0,0); |
|
80 |
|
|
55 | 81 |
FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance(); |
56 |
mMesh = factory.createMesh(position);
|
|
82 |
MeshBase mesh = factory.createMesh(mPosition);
|
|
57 | 83 |
|
58 | 84 |
mTexture = new DistortedTexture(); |
59 | 85 |
mTexture.setColorARGB(color); |
... | ... | |
61 | 87 |
MatrixEffectScale scaleEffect = new MatrixEffectScale(scale); |
62 | 88 |
MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER); |
63 | 89 |
MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER); |
64 |
MatrixEffectMove move1Effect = new MatrixEffectMove(move1);
|
|
65 |
MatrixEffectMove move2Effect = new MatrixEffectMove(move2);
|
|
90 |
MatrixEffectMove moveSEffect = new MatrixEffectMove(mMove);
|
|
91 |
MatrixEffectMove moveYEffect = new MatrixEffectMove(moveY);
|
|
66 | 92 |
|
67 | 93 |
mEffects = new DistortedEffects(); |
68 | 94 |
mEffects.apply(scaleEffect); |
69 |
mEffects.apply(move1Effect);
|
|
95 |
mEffects.apply(moveSEffect);
|
|
70 | 96 |
mEffects.apply(quat2Effect); |
71 | 97 |
mEffects.apply(quat1Effect); |
72 |
mEffects.apply(move2Effect); |
|
98 |
mEffects.apply(moveYEffect); |
|
99 |
|
|
100 |
mNode = new DistortedNode(mTexture,mEffects,mesh); |
|
101 |
} |
|
102 |
|
|
103 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
104 |
|
|
105 |
public void prepareJoin(float[] position) |
|
106 |
{ |
|
107 |
int len1 = mPosition.length; |
|
108 |
int len2 = position.length; |
|
109 |
|
|
110 |
mNewPosition = new float[len1+len2]; |
|
73 | 111 |
|
74 |
mNode = new DistortedNode(mTexture,mEffects,mMesh); |
|
112 |
System.arraycopy(mPosition, 0, mNewPosition, 0, len1); |
|
113 |
System.arraycopy(position , 0, mNewPosition, len1, len2); |
|
114 |
|
|
115 |
/* |
|
116 |
int l= mPosition.length/3; |
|
117 |
String s=""; |
|
118 |
for(int i=0; i<l; i++) s += (mPosition[3*i]+" "+mPosition[3*i+1]+" "+mPosition[3*i+2]+"\n"); |
|
119 |
android.util.Log.e("D", "pos after joining: \n"+s); |
|
120 |
*/ |
|
121 |
FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance(); |
|
122 |
MeshBase mesh = factory.createMesh(mNewPosition); |
|
123 |
mNewNode = new DistortedNode(mTexture,mEffects,mesh); |
|
124 |
|
|
125 |
computeMove(mNewPosition); |
|
126 |
} |
|
127 |
|
|
128 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
129 |
|
|
130 |
public void swapNodes(float scale) |
|
131 |
{ |
|
132 |
mPosition = mNewPosition; |
|
133 |
mNode = mNewNode; |
|
134 |
mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ); |
|
135 |
} |
|
136 |
|
|
137 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
138 |
|
|
139 |
public void scaleMove(float scale) |
|
140 |
{ |
|
141 |
mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ); |
|
75 | 142 |
} |
76 | 143 |
|
77 | 144 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Progress with BandagedCreator: joining cubits together. Still at least one (probably two) bugs here remain:
1) sometimes some of the walls of the newly creaed joined cubit are incorrectly rotated
2) there is an unpleasant flash when joining