Skip to content
This repository was archived by the owner on Feb 22, 2022. It is now read-only.

Commit 33c963b

Browse files
author
Aphid Mobile
committed
Fix the incorrect z values for shadow in horizontal mode
Issue: #7
1 parent d5ff25e commit 33c963b

File tree

2 files changed

+61
-51
lines changed

2 files changed

+61
-51
lines changed

FlipView/FlipLibrary/src/com/aphidmobile/flip/Card.java

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import static com.aphidmobile.flip.FlipRenderer.*;
2121
import static com.aphidmobile.utils.TextureUtils.*;
2222
import static javax.microedition.khronos.opengles.GL10.*;
23-
24-
import android.util.FloatMath;
23+
import static android.util.FloatMath.*;
2524

2625
import javax.microedition.khronos.opengles.GL10;
2726
import java.nio.FloatBuffer;
@@ -31,6 +30,25 @@ public class Card {
3130
public static final int AXIS_TOP = 0;
3231
public static final int AXIS_BOTTOM = 1;
3332

33+
/**
34+
* The indices of x,y,z for vertices (0, 0), (0, 1), (1, 1), (1, 0)
35+
*/
36+
public static final int X_00 = 0;
37+
public static final int Y_00 = 1;
38+
public static final int Z_00 = 2;
39+
40+
public static final int X_01 = 3;
41+
public static final int Y_01 = 4;
42+
public static final int Z_01 = 5;
43+
44+
public static final int X_11 = 6;
45+
public static final int Y_11 = 7;
46+
public static final int Z_11 = 8;
47+
48+
public static final int X_10 = 9;
49+
public static final int Y_10 = 10;
50+
public static final int Z_10 = 11;
51+
3452
private float cardVertices[];
3553

3654
private short[] indices = {0, 1, 2, 0, 2, 3};
@@ -130,30 +148,28 @@ public void draw(GL10 gl) {
130148
if(orientationVertical){
131149
if (angle > 0) {
132150
if (axis == AXIS_TOP) {
133-
gl.glTranslatef(0, cardVertices[1], 0f);
151+
gl.glTranslatef(0, cardVertices[Y_00], 0f);
134152
gl.glRotatef(-angle, 1f, 0f, 0f);
135-
gl.glTranslatef(0, -cardVertices[1], 0f);
153+
gl.glTranslatef(0, -cardVertices[Y_00], 0f);
136154
} else {
137-
gl.glTranslatef(0, cardVertices[7], 0f);
155+
gl.glTranslatef(0, cardVertices[Y_11], 0f);
138156
gl.glRotatef(angle, 1f, 0f, 0f);
139-
gl.glTranslatef(0, -cardVertices[7], 0f);
157+
gl.glTranslatef(0, -cardVertices[Y_11], 0f);
140158
}
141159
}
142160
} else {
143161
if (angle > 0) {
144162
if (axis == AXIS_TOP) {
145-
gl.glTranslatef(cardVertices[0], 0, 0f);
163+
gl.glTranslatef(cardVertices[X_00], 0, 0f);
146164
gl.glRotatef(-angle, 0f, 1f, 0f);
147-
gl.glTranslatef(-cardVertices[0], 0, 0f);
165+
gl.glTranslatef(-cardVertices[X_00], 0, 0f);
148166
} else {
149-
gl.glTranslatef(cardVertices[6], 0, 0f);
167+
gl.glTranslatef(cardVertices[X_11], 0, 0f);
150168
gl.glRotatef(angle, 0f, 1f, 0f);
151-
gl.glTranslatef(-cardVertices[6], 0, 0f);
169+
gl.glTranslatef(-cardVertices[X_11], 0, 0f);
152170
}
153171
}
154-
155172
}
156-
157173

158174
gl.glVertexPointer(3, GL_FLOAT, 0, vertexBuffer);
159175
gl.glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_SHORT, indexBuffer);
@@ -166,36 +182,33 @@ public void draw(GL10 gl) {
166182
gl.glDisableClientState(GL_TEXTURE_COORD_ARRAY);
167183
gl.glDisable(GL_TEXTURE_2D);
168184
}
169-
185+
186+
float w,h,z;
187+
float[] shadowVertices;
188+
170189
if (angle > 0) {
171190
gl.glDisable(GL_LIGHTING);
172191
gl.glDisable(GL_DEPTH_TEST);
173192

174193
if (axis == AXIS_TOP) {
175-
float w,h,z;
176-
float[] shadowVertices;
177194
if(orientationVertical){
178-
w = cardVertices[9] - cardVertices[0];
179-
h = (cardVertices[1] - cardVertices[4]) * (1f - FloatMath.cos(d2r(angle)));
180-
z = (cardVertices[1] - cardVertices[4]) * FloatMath.sin(d2r(angle));
195+
h = (cardVertices[Y_00] - cardVertices[Y_01]) * (1.0f - cos(d2r(angle)));
196+
z = (cardVertices[Y_00] - cardVertices[Y_01]) * sin(d2r(angle));
181197
shadowVertices = new float[]{
182-
cardVertices[0], h + cardVertices[4], z,
183-
cardVertices[3], cardVertices[4], 0f,
184-
w, cardVertices[7], 0f,
185-
w, h + cardVertices[4], z
198+
cardVertices[X_00], cardVertices[Y_01] + h, z,
199+
cardVertices[X_01], cardVertices[Y_01], 0f,
200+
cardVertices[X_11], cardVertices[Y_11], 0f,
201+
cardVertices[X_10], cardVertices[Y_01] + h, z
186202
};
187-
} else {
188-
w = (cardVertices[9] - cardVertices[0]) * (1f - FloatMath.cos(d2r(angle)));
189-
h = (cardVertices[1] - cardVertices[4]);
190-
z = (cardVertices[1] - cardVertices[4]) * FloatMath.sin(d2r(angle));
191-
203+
} else { //horizontal
204+
w = (cardVertices[X_10] - cardVertices[X_00]) * (1.0f - cos(d2r(angle)));
205+
z = (cardVertices[X_10] - cardVertices[X_00]) * sin(d2r(angle));
192206
shadowVertices = new float[]{
193-
cardVertices[9] - w, cardVertices[1], z,
194-
cardVertices[6] - w, cardVertices[4], z,
195-
cardVertices[6], cardVertices[7], 0f,
196-
cardVertices[9], cardVertices[10], 0f
207+
cardVertices[X_10] - w, cardVertices[Y_00], z,
208+
cardVertices[X_11] - w, cardVertices[Y_01], z,
209+
cardVertices[X_11], cardVertices[Y_11], 0f,
210+
cardVertices[X_10], cardVertices[Y_10], 0f
197211
};
198-
199212
}
200213

201214
float alpha = 1f * (90f - angle) / 90f;
@@ -204,27 +217,23 @@ public void draw(GL10 gl) {
204217
gl.glVertexPointer(3, GL_FLOAT, 0, toFloatBuffer(shadowVertices));
205218
gl.glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_SHORT, indexBuffer);
206219
} else {
207-
float w,h,z;
208-
float[] shadowVertices;
209220
if(orientationVertical){
210-
w = cardVertices[9] - cardVertices[0];
211-
h = (cardVertices[1] - cardVertices[4]) * (1f - FloatMath.cos(d2r(angle)));
212-
z = (cardVertices[1] - cardVertices[4]) * FloatMath.sin(d2r(angle));
221+
h = (cardVertices[Y_00] - cardVertices[Y_01]) * (1f - cos(d2r(angle)));
222+
z = (cardVertices[Y_00] - cardVertices[Y_01]) * sin(d2r(angle));
213223
shadowVertices = new float[]{
214-
cardVertices[0], cardVertices[1], 0f,
215-
cardVertices[3], cardVertices[1] - h, z,
216-
w, cardVertices[1] - h, z,
217-
w, cardVertices[1], 0f
224+
cardVertices[X_00], cardVertices[Y_00], 0f,
225+
cardVertices[X_01], cardVertices[Y_00] - h, z,
226+
cardVertices[X_11], cardVertices[Y_00] - h, z,
227+
cardVertices[X_10], cardVertices[Y_00], 0f
218228
};
219-
} else {
220-
w = (cardVertices[9] - cardVertices[0]) * (1f - FloatMath.cos(d2r(angle)));
221-
h = (cardVertices[1] - cardVertices[4]);
222-
z = (cardVertices[1] - cardVertices[4]) * FloatMath.sin(d2r(angle));
229+
} else { //horizontal
230+
w = (cardVertices[X_10] - cardVertices[X_00]) * (1f - cos(d2r(angle)));
231+
z = (cardVertices[X_10] - cardVertices[X_00]) * sin(d2r(angle));
223232
shadowVertices = new float[]{
224-
cardVertices[0], cardVertices[1], 0f,
225-
cardVertices[3], cardVertices[4], 0f,
226-
cardVertices[0] + w, cardVertices[7], z,
227-
cardVertices[3] + w, cardVertices[10], z
233+
cardVertices[X_00], cardVertices[Y_00], 0f,
234+
cardVertices[X_01], cardVertices[Y_01], 0f,
235+
cardVertices[X_00] + w, cardVertices[Y_11], z,
236+
cardVertices[X_01] + w, cardVertices[Y_10], z
228237
};
229238
}
230239
float alpha = 1f * (90f - angle) / 90f;

FlipView/FlipLibrary/src/com/aphidmobile/flip/GrabIt.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ public static Bitmap takeScreenshot(View view) {
3333
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
3434
Canvas canvas = new Canvas(bitmap);
3535
view.draw(canvas);
36-
//canvas.drawColor(Color.RED, PorterDuff.Mode.LIGHTEN); //XXX: debug option
36+
//canvas.drawColor(Color.RED, PorterDuff.Mode.DARKEN); //XXX: debug option
3737

38-
AphidLog.d("create bitmap %dx%d", width, height);
38+
if (AphidLog.ENABLE_DEBUG)
39+
AphidLog.d("create bitmap %dx%d", width, height);
3940

4041
return bitmap;
4142
} else {

0 commit comments

Comments
 (0)