Skip to content

Commit 5640daf

Browse files
committed
fix issue: #5,#6 and refactor code slightly
1 parent 19d7988 commit 5640daf

20 files changed

+259
-356
lines changed

.idea/gradle.xml

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PageFlip/src/main/java/com/eschao/android/widget/pageflip/FoldBackVertexProgram.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ public class FoldBackVertexProgram extends VertexProgram {
3535
final static String VAR_MASK_COLOR = "u_maskColor";
3636
final static String VAR_SHADOW_TEXTURE = "u_shadow";
3737

38-
int hShadow;
39-
int hMaskColor;
40-
int hTexXOffset;
38+
int mShadowLoc;
39+
int mMaskColorLoc;
40+
int mTexXOffsetLoc;
4141

4242
public FoldBackVertexProgram() {
4343
super();
4444

45-
hShadow = INVALID_HANDLE;
46-
hMaskColor = INVALID_HANDLE;
47-
hTexXOffset = INVALID_HANDLE;
45+
mShadowLoc = INVALID_GL_HANDLE;
46+
mMaskColorLoc = INVALID_GL_HANDLE;
47+
mTexXOffsetLoc = INVALID_GL_HANDLE;
4848
}
4949

5050
/**
@@ -68,10 +68,11 @@ public FoldBackVertexProgram init(Context context) throws
6868
protected void getVarsLocation() {
6969
super.getVarsLocation();
7070

71-
if (hProgram != 0) {
72-
hShadow = glGetUniformLocation(hProgram, VAR_SHADOW_TEXTURE);
73-
hMaskColor = glGetUniformLocation(hProgram, VAR_MASK_COLOR);
74-
hTexXOffset = glGetUniformLocation(hProgram, VAR_TEXTRUE_OFFSET);
71+
if (mProgramRef != 0) {
72+
mShadowLoc = glGetUniformLocation(mProgramRef, VAR_SHADOW_TEXTURE);
73+
mMaskColorLoc = glGetUniformLocation(mProgramRef, VAR_MASK_COLOR);
74+
mTexXOffsetLoc = glGetUniformLocation(mProgramRef,
75+
VAR_TEXTRUE_OFFSET);
7576
}
7677
}
7778

@@ -81,8 +82,8 @@ protected void getVarsLocation() {
8182
public void delete() {
8283
super.delete();
8384

84-
hShadow = INVALID_HANDLE;
85-
hMaskColor = INVALID_HANDLE;
86-
hTexXOffset = INVALID_HANDLE;
85+
mShadowLoc = INVALID_GL_HANDLE;
86+
mMaskColorLoc = INVALID_GL_HANDLE;
87+
mTexXOffsetLoc = INVALID_GL_HANDLE;
8788
}
8889
}

PageFlip/src/main/java/com/eschao/android/widget/pageflip/FoldBackVertexes.java

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,55 +50,38 @@ public FoldBackVertexes() {
5050
* Set vertex buffer with given mesh count
5151
*
5252
* @param meshCount mesh count
53-
* @return self
5453
*/
55-
public FoldBackVertexes set(int meshCount) {
54+
public void set(int meshCount) {
5655
super.set(meshCount << 1, 4, true);
5756
mNext = 0;
58-
return this;
5957
}
6058

6159
/**
6260
* Set mask alpha
6361
*
6462
* @param alpha mask alpha, value is [0 .. 255]
65-
* @return self
6663
*/
67-
public FoldBackVertexes setMaskAlpha(int alpha) {
64+
public void setMaskAlpha(int alpha) {
6865
if (alpha < 0 || alpha > 255) {
69-
throw new IllegalArgumentException("Alpha: " + alpha + "is out of " +
70-
"[0 .. 255]!");
66+
throw new IllegalArgumentException("Alpha: " + alpha + "is out of "
67+
+ "[0 .. 255]!");
7168
}
7269

7370
mMaskAlpha = alpha / 255.0f;
74-
return this;
7571
}
7672

7773
/**
7874
* set mask alpha
7975
*
8076
* @param alpha mask alpha, value is [0 .. 1]
81-
* @return self
8277
*/
83-
public FoldBackVertexes setMaskAlpha(float alpha) {
78+
public void setMaskAlpha(float alpha) {
8479
if (alpha < 0 || alpha > 1) {
85-
throw new IllegalArgumentException("Alpha: " + alpha + "is out of " +
86-
"[0 .. 1]!");
80+
throw new IllegalArgumentException("Alpha: " + alpha + "is out of "
81+
+ "[0 .. 1]!");
8782
}
8883

8984
mMaskAlpha = alpha;
90-
return this;
91-
}
92-
93-
/**
94-
* Put all data from float array to float buffer
95-
*
96-
* @return self
97-
*/
98-
public FoldBackVertexes toFloatBuffer() {
99-
// fix coordinate x of texture for shadow of fold back
100-
super.toFloatBuffer();
101-
return this;
10285
}
10386

10487
/**
@@ -113,37 +96,37 @@ public void draw(FoldBackVertexProgram program,
11396
Page page,
11497
boolean hasSecondPage,
11598
int gradientShadowId) {
116-
glUniformMatrix4fv(program.hMVPMatrix, 1, false,
99+
glUniformMatrix4fv(program.mMVPMatrixLoc, 1, false,
117100
VertexProgram.MVPMatrix, 0);
118101

119102
// load fold back texture
120103
glBindTexture(GL_TEXTURE_2D, page.getBackTextureID());
121-
glUniform1i(program.hTexture, 0);
104+
glUniform1i(program.mTextureLoc, 0);
122105

123106
// load gradient shadow texture
124107
glActiveTexture(GLES20.GL_TEXTURE1);
125108
glBindTexture(GL_TEXTURE_2D, gradientShadowId);
126-
glUniform1i(program.hShadow, 1);
109+
glUniform1i(program.mShadowLoc, 1);
127110

128111
// set x offset of texture coordinate. In single page mode, the value is
129112
// set 0 to draw the back texture with x coordinate inversely against
130113
// the first texture since they are using the same texture, but in
131114
// double page mode, the back texture is different with the first one,
132115
// it is the next page content texture and should be drawn in the same
133-
// order with the first texture, so the value is set 1. Computing
116+
// order with the first texture, so the value is set 1. For computing
134117
// details, please see the shader script.
135-
glUniform1f(program.hTexXOffset, hasSecondPage ? 1.0f : 0);
118+
glUniform1f(program.mTexXOffsetLoc, hasSecondPage ? 1.0f : 0);
136119

137120
// set mask color and alpha
138-
glUniform4f(program.hMaskColor,
121+
glUniform4f(program.mMaskColorLoc,
139122
page.maskColor[0][0],
140123
page.maskColor[0][1],
141124
page.maskColor[0][2],
142125
hasSecondPage ? 0 : mMaskAlpha);
143126

144127
// draw triangles
145128
drawWith(GL_TRIANGLE_STRIP,
146-
program.hVertexPosition,
147-
program.hTextureCoord);
129+
program.mVertexPosLoc,
130+
program.mTexCoordLoc);
148131
}
149132
}

PageFlip/src/main/java/com/eschao/android/widget/pageflip/GPoint.java renamed to PageFlip/src/main/java/com/eschao/android/widget/pageflip/GLPoint.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616
package com.eschao.android.widget.pageflip;
1717

1818
/**
19-
* GPoint includes (x,y,z) in OpenGL coordinate system and its texture
20-
* coordinates (tX, tY)
19+
* GLPoint includes (x,y,z) in OpenGL coordinate system and its texture
20+
* coordinates (texX, texY)
2121
*
2222
* @author eschao
2323
*/
24-
public final class GPoint {
24+
public final class GLPoint {
2525
// 3D coordinate
2626
float x;
2727
float y;
2828
float z;
2929

3030
// texutre coordinate
31-
float tX;
32-
float tY;
31+
float texX;
32+
float texY;
3333

3434
/**
35-
* Set GPoint with given values
35+
* Set GLPoint with given values
3636
*
3737
* @param x x coordinate
3838
* @param y y coordinate
@@ -44,7 +44,7 @@ public void set(float x, float y, float z, float tX, float tY) {
4444
this.x = x;
4545
this.y = y;
4646
this.z = z;
47-
this.tX = tX;
48-
this.tY = tY;
47+
this.texX = tX;
48+
this.texY = tY;
4949
}
5050
}

PageFlip/src/main/java/com/eschao/android/widget/pageflip/GLProgram.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535

3636
public class GLProgram {
3737

38-
// invalid handle
39-
protected final int INVALID_HANDLE = -1;
38+
// invalid GL getShaderRef including program reference and variable location
39+
protected final int INVALID_GL_HANDLE = -1;
4040

41-
// GLSL program handle
42-
protected int hProgram;
41+
// GLSL program reference
42+
protected int mProgramRef;
4343

4444
// Vertex shader
4545
protected GLShader mVertex;
@@ -48,7 +48,7 @@ public class GLProgram {
4848
protected GLShader mFragment;
4949

5050
public GLProgram() {
51-
hProgram = INVALID_HANDLE;
51+
mProgramRef = INVALID_GL_HANDLE;
5252
mVertex = new GLShader();
5353
mFragment = new GLShader();
5454
}
@@ -76,21 +76,21 @@ public GLProgram init(Context context, int vertexResId, int fragmentResId)
7676
}
7777

7878
// 2. create texture program and link shader
79-
hProgram = glCreateProgram();
80-
if (hProgram == 0) {
79+
mProgramRef = glCreateProgram();
80+
if (mProgramRef == 0) {
8181
mVertex.delete();
8282
mFragment.delete();
8383
throw new PageFlipException("Can't create texture program");
8484
}
8585

8686
// 3. attach vertex and fragment shader
87-
glAttachShader(hProgram, mVertex.handle());
88-
glAttachShader(hProgram, mFragment.handle());
89-
glLinkProgram(hProgram);
87+
glAttachShader(mProgramRef, mVertex.getShaderRef());
88+
glAttachShader(mProgramRef, mFragment.getShaderRef());
89+
glLinkProgram(mProgramRef);
9090

9191
// 4. check shader link status
9292
int[] result = new int[1];
93-
glGetProgramiv(hProgram, GL_LINK_STATUS, result, 0);
93+
glGetProgramiv(mProgramRef, GL_LINK_STATUS, result, 0);
9494
if (result[0] == 0) {
9595
delete();
9696
throw new PageFlipException("Can't link program");
@@ -99,7 +99,7 @@ public GLProgram init(Context context, int vertexResId, int fragmentResId)
9999
// 5. get all variable handles defined in scripts
100100
// subclass should implement getVarsLocation to be responsible for its
101101
// own variables in script
102-
glUseProgram(hProgram);
102+
glUseProgram(mProgramRef);
103103
getVarsLocation();
104104
return this;
105105
}
@@ -111,19 +111,19 @@ public void delete() {
111111
mVertex.delete();
112112
mFragment.delete();
113113

114-
if (hProgram != INVALID_HANDLE) {
115-
glDeleteProgram(hProgram);
116-
hProgram = INVALID_HANDLE;
114+
if (mProgramRef != INVALID_GL_HANDLE) {
115+
glDeleteProgram(mProgramRef);
116+
mProgramRef = INVALID_GL_HANDLE;
117117
}
118118
}
119119

120120
/**
121-
* Get program handle
121+
* Get program GL reference
122122
*
123-
* @return program handle
123+
* @return program GL reference for program
124124
*/
125-
public int handle() {
126-
return hProgram;
125+
public int getProgramRef() {
126+
return mProgramRef;
127127
}
128128

129129
/**

PageFlip/src/main/java/com/eschao/android/widget/pageflip/GLShader.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@
4040
public class GLShader {
4141

4242
private final static String TAG = "GLShader";
43+
private final int INVALID_GL_HANDLE = -1;
4344

44-
// shader handle
45-
int hShader;
45+
// shader object reference
46+
int mShaderRef;
4647

4748
/**
4849
* Default constructor
4950
*/
5051
public GLShader() {
51-
hShader = 0;
52+
mShaderRef = INVALID_GL_HANDLE;
5253
}
5354

5455
/**
@@ -70,24 +71,24 @@ public GLShader compile(Context context, int type, int resId)
7071
}
7172

7273
// create a shader
73-
hShader = glCreateShader(type);
74-
if (hShader != 0) {
74+
mShaderRef = glCreateShader(type);
75+
if (mShaderRef != INVALID_GL_HANDLE) {
7576
// upload shader scripts to GL
76-
glShaderSource(hShader, codes);
77+
glShaderSource(mShaderRef, codes);
7778

7879
// compile shader scripts
79-
glCompileShader(hShader);
80+
glCompileShader(mShaderRef);
8081

8182
// get compile results to check if it is successful
8283
final int[] result = new int[1];
83-
glGetShaderiv(hShader, GL_COMPILE_STATUS, result, 0);
84+
glGetShaderiv(mShaderRef, GL_COMPILE_STATUS, result, 0);
8485
if (result[0] == 0) {
8586
// delete shader if compile is failed
8687
Log.e(TAG, "Can'top compile shader for type: " + type +
8788
"Error: " + glGetError());
8889
Log.e(TAG, "Compile shader error: " +
89-
glGetShaderInfoLog(hShader));
90-
glDeleteShader(hShader);
90+
glGetShaderInfoLog(mShaderRef));
91+
glDeleteShader(mShaderRef);
9192
throw new PageFlipException("Can't compile shader for" +
9293
"type: " + type);
9394
}
@@ -103,19 +104,19 @@ public GLShader compile(Context context, int type, int resId)
103104
* Delete shader
104105
*/
105106
public void delete() {
106-
if (hShader != 0) {
107-
glDeleteShader(hShader);
108-
hShader = 0;
107+
if (mShaderRef != INVALID_GL_HANDLE) {
108+
glDeleteShader(mShaderRef);
109+
mShaderRef = INVALID_GL_HANDLE;
109110
}
110111
}
111112

112113
/**
113-
* Get shader handle
114+
* Get shader object reference
114115
*
115-
* @return shader handle
116+
* @return shader object reference in OpenGL
116117
*/
117-
public int handle() {
118-
return hShader;
118+
public int getShaderRef() {
119+
return mShaderRef;
119120
}
120121

121122
/**

0 commit comments

Comments
 (0)