| 34 |
34 |
|
| 35 |
35 |
class AroundTheWorldRendererPicker implements GLSurfaceView.Renderer
|
| 36 |
36 |
{
|
| 37 |
|
private float[] mModelMatrix = new float[16];
|
| 38 |
37 |
private float[] mViewMatrix = new float[16];
|
| 39 |
38 |
private float[] mProjectionMatrix = new float[16];
|
| 40 |
39 |
private float[] mMVPMatrix = new float[16];
|
| 41 |
40 |
|
| 42 |
41 |
private final FloatBuffer mTriangleVertices;
|
| 43 |
42 |
|
| 44 |
|
private int mMVPMatrixHandle;
|
| 45 |
|
private int mPositionHandle;
|
| 46 |
|
private int mColorHandle;
|
|
43 |
private int mMVPMatrixH;
|
|
44 |
private int mPositionH;
|
|
45 |
private int mColorH;
|
| 47 |
46 |
|
| 48 |
47 |
private final int mBytesPerFloat = 4;
|
| 49 |
48 |
private final int mStrideBytes = 7 * mBytesPerFloat;
|
| ... | ... | |
| 57 |
56 |
AroundTheWorldRendererPicker()
|
| 58 |
57 |
{
|
| 59 |
58 |
final float[] triangle1VerticesData = {
|
| 60 |
|
-0.25f, 0.5f, 0.0f, // x,y,z
|
| 61 |
|
1.0f, 1.0f, 1.0f, 1.0f, // r,g,b
|
|
59 |
-0.865f, 1.5f, 0.0f, // x,y,z
|
|
60 |
1.0f, 1.0f, 1.0f, 1.0f, // r,g,b
|
| 62 |
61 |
|
| 63 |
|
-0.25f, -0.5f, 0.0f,
|
|
62 |
-0.865f,-1.5f, 0.0f,
|
| 64 |
63 |
0.0f, 0.0f, 0.0f, 1.0f,
|
| 65 |
64 |
|
| 66 |
|
0.559f, 0.0f, 0.0f,
|
| 67 |
|
0.3f, 0.3f, 0.0f, 1.0f };
|
|
65 |
1.73f, 0.0f, 0.0f,
|
|
66 |
0.5f, 0.5f, 0.0f, 1.0f };
|
| 68 |
67 |
|
| 69 |
68 |
mTriangleVertices = ByteBuffer.allocateDirect(triangle1VerticesData.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
| 70 |
69 |
mTriangleVertices.put(triangle1VerticesData).position(0);
|
| ... | ... | |
| 74 |
73 |
|
| 75 |
74 |
@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
| 76 |
75 |
{
|
| 77 |
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
76 |
GLES20.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
|
| 78 |
77 |
|
| 79 |
78 |
final float eyeX = 0.0f;
|
| 80 |
79 |
final float eyeY = 0.0f;
|
| ... | ... | |
| 82 |
81 |
|
| 83 |
82 |
final float lookX = 0.0f;
|
| 84 |
83 |
final float lookY = 0.0f;
|
| 85 |
|
final float lookZ = -5.0f;
|
|
84 |
final float lookZ = 0.0f;
|
| 86 |
85 |
|
| 87 |
86 |
final float upX = 0.0f;
|
| 88 |
87 |
final float upY = 1.0f;
|
| ... | ... | |
| 91 |
90 |
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
|
| 92 |
91 |
|
| 93 |
92 |
final String vertexShader =
|
| 94 |
|
"uniform mat4 u_MVPMatrix; \n"
|
| 95 |
|
+ "attribute vec4 a_Position; \n"
|
| 96 |
|
+ "attribute vec4 a_Color; \n"
|
| 97 |
|
+ "varying vec4 v_Color; \n"
|
|
93 |
"uniform mat4 u_MVPMatrix; \n"
|
|
94 |
+ "attribute vec4 a_Position; \n"
|
|
95 |
+ "attribute vec4 a_Color; \n"
|
|
96 |
+ "varying vec4 v_Color; \n"
|
| 98 |
97 |
|
| 99 |
|
+ "void main() \n"
|
| 100 |
|
+ "{ \n"
|
| 101 |
|
+ " v_Color = a_Color; \n"
|
| 102 |
|
+ " gl_Position = u_MVPMatrix* a_Position; \n"
|
| 103 |
|
+ "} \n";
|
|
98 |
+ "void main() \n"
|
|
99 |
+ " { \n"
|
|
100 |
+ " v_Color = a_Color; \n"
|
|
101 |
+ " gl_Position = u_MVPMatrix* a_Position; \n"
|
|
102 |
+ " } \n";
|
| 104 |
103 |
|
| 105 |
104 |
final String fragmentShader =
|
| 106 |
|
"precision mediump float; \n"
|
| 107 |
|
+ "varying vec4 v_Color; \n"
|
|
105 |
"precision mediump float; \n"
|
|
106 |
+ "varying vec4 v_Color; \n"
|
| 108 |
107 |
|
| 109 |
|
+ "void main() \n"
|
| 110 |
|
+ "{ \n"
|
| 111 |
|
+ " gl_FragColor = v_Color; \n"
|
| 112 |
|
+ "} \n";
|
|
108 |
+ "void main() \n"
|
|
109 |
+ " { \n"
|
|
110 |
+ " gl_FragColor = v_Color; \n"
|
|
111 |
+ " } \n";
|
| 113 |
112 |
|
| 114 |
113 |
int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
|
| 115 |
114 |
|
| ... | ... | |
| 180 |
179 |
throw new RuntimeException("Error creating program.");
|
| 181 |
180 |
}
|
| 182 |
181 |
|
| 183 |
|
mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
|
| 184 |
|
mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
|
| 185 |
|
mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");
|
|
182 |
mMVPMatrixH = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
|
|
183 |
mPositionH = GLES20.glGetAttribLocation(programHandle, "a_Position");
|
|
184 |
mColorH = GLES20.glGetAttribLocation(programHandle, "a_Color");
|
| 186 |
185 |
|
| 187 |
186 |
GLES20.glUseProgram(programHandle);
|
| 188 |
187 |
}
|
| ... | ... | |
| 193 |
192 |
{
|
| 194 |
193 |
GLES20.glViewport(0, 0, width, height);
|
| 195 |
194 |
|
| 196 |
|
final float ratio = (float) width / height;
|
| 197 |
|
final float left = -ratio;
|
| 198 |
|
final float right = ratio;
|
| 199 |
|
final float bottom = -1.0f;
|
| 200 |
|
final float top = 1.0f;
|
| 201 |
|
final float near = 1.0f;
|
| 202 |
|
final float far = 10.0f;
|
|
195 |
final float ratio = (float) width / height;
|
|
196 |
final float left =-ratio;
|
|
197 |
final float right = ratio;
|
|
198 |
final float bottom =-1.0f;
|
|
199 |
final float top = 1.0f;
|
|
200 |
final float near = 1.0f;
|
|
201 |
final float far = 2.0f;
|
| 203 |
202 |
|
| 204 |
203 |
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
|
204 |
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
|
| 205 |
205 |
}
|
| 206 |
206 |
|
| 207 |
207 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| ... | ... | |
| 210 |
210 |
{
|
| 211 |
211 |
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
| 212 |
212 |
|
| 213 |
|
Matrix.setIdentityM(mModelMatrix, 0);
|
| 214 |
|
|
| 215 |
213 |
mTriangleVertices.position(mPositionOffset);
|
| 216 |
|
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
|
| 217 |
|
GLES20.glEnableVertexAttribArray(mPositionHandle);
|
|
214 |
GLES20.glVertexAttribPointer(mPositionH, mPositionDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
|
|
215 |
GLES20.glEnableVertexAttribArray(mPositionH);
|
| 218 |
216 |
|
| 219 |
217 |
mTriangleVertices.position(mColorOffset);
|
| 220 |
|
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
|
| 221 |
|
GLES20.glEnableVertexAttribArray(mColorHandle);
|
|
218 |
GLES20.glVertexAttribPointer(mColorH, mColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
|
|
219 |
GLES20.glEnableVertexAttribArray(mColorH);
|
| 222 |
220 |
|
| 223 |
|
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
|
| 224 |
|
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
|
| 225 |
|
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
|
|
221 |
GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
|
| 226 |
222 |
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
|
| 227 |
223 |
}
|
| 228 |
224 |
}
|
Progress with AroundTheWorld