1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.examples.aroundtheworld;
|
21
|
|
22
|
import java.nio.ByteBuffer;
|
23
|
import java.nio.ByteOrder;
|
24
|
import java.nio.FloatBuffer;
|
25
|
|
26
|
import javax.microedition.khronos.egl.EGLConfig;
|
27
|
import javax.microedition.khronos.opengles.GL10;
|
28
|
|
29
|
import android.opengl.GLES31;
|
30
|
import android.opengl.GLSurfaceView;
|
31
|
import android.opengl.Matrix;
|
32
|
|
33
|
import org.distorted.library.helpers.MatrixHelper;
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
|
37
|
class AroundTheWorldRendererPicker implements GLSurfaceView.Renderer
|
38
|
{
|
39
|
private static final int BYTES_PER_FLOAT = 4;
|
40
|
private static final int STRIDE_BYTES = 7 * BYTES_PER_FLOAT;
|
41
|
private static final int POSITION_OFFSET = 0;
|
42
|
private static final int POSITION_DATA_SIZE = 3;
|
43
|
private static final int COLOR_OFFSET = 3;
|
44
|
private static final int COLOR_DATA_SIZE = 4;
|
45
|
|
46
|
private final float[] mModelMatrix = new float[16];
|
47
|
private final float[] mViewMatrix = new float[16];
|
48
|
private final float[] mProjectionMatrix = new float[16];
|
49
|
private final float[] mMVPTriangleMatrix= new float[16];
|
50
|
private final float[] mMVPSquareMatrix = new float[16];
|
51
|
private final float[] mTmpMatrix = new float[16];
|
52
|
|
53
|
private final FloatBuffer mTriangleVert, mSquareVert;
|
54
|
|
55
|
private int mMVPMatrixH;
|
56
|
private int mPositionH;
|
57
|
private int mColorH;
|
58
|
|
59
|
private int mHeight, mWidth;
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
AroundTheWorldRendererPicker()
|
64
|
{
|
65
|
MatrixHelper.setIdentity(mModelMatrix);
|
66
|
|
67
|
final float[] triangleVertData = {
|
68
|
-0.865f, 1.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, // x,y,z, r,g,b,a
|
69
|
-0.865f,-1.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
70
|
1.730f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f, 1.0f };
|
71
|
|
72
|
mTriangleVert = ByteBuffer.allocateDirect(triangleVertData.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
73
|
mTriangleVert.put(triangleVertData).position(0);
|
74
|
|
75
|
final float[] squareVertData = {
|
76
|
-0.15f, 0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
77
|
-0.15f,-0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
78
|
0.15f, 0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
79
|
0.15f,-0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f };
|
80
|
|
81
|
mSquareVert = ByteBuffer.allocateDirect(squareVertData.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
82
|
mSquareVert.put(squareVertData).position(0);
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
public void move(float x, float y)
|
88
|
{
|
89
|
mModelMatrix[12] = x;
|
90
|
mModelMatrix[13] = y;
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
public int getHeight()
|
96
|
{
|
97
|
return mHeight;
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
public int getWidth()
|
103
|
{
|
104
|
return mWidth;
|
105
|
}
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108
|
|
109
|
@Override
|
110
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
111
|
{
|
112
|
GLES31.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
|
113
|
|
114
|
final float eyeX = 0.0f;
|
115
|
final float eyeY = 0.0f;
|
116
|
final float eyeZ = 1.5f;
|
117
|
|
118
|
final float lookX = 0.0f;
|
119
|
final float lookY = 0.0f;
|
120
|
final float lookZ = 0.0f;
|
121
|
|
122
|
final float upX = 0.0f;
|
123
|
final float upY = 1.0f;
|
124
|
final float upZ = 0.0f;
|
125
|
|
126
|
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
|
127
|
|
128
|
final String vertexShader =
|
129
|
"uniform mat4 u_MVPMatrix; \n"
|
130
|
+ "attribute vec4 a_Position; \n"
|
131
|
+ "attribute vec4 a_Color; \n"
|
132
|
+ "varying vec4 v_Color; \n"
|
133
|
|
134
|
+ "void main() \n"
|
135
|
+ " { \n"
|
136
|
+ " v_Color = a_Color; \n"
|
137
|
+ " gl_Position = u_MVPMatrix* a_Position; \n"
|
138
|
+ " } \n";
|
139
|
|
140
|
final String fragmentShader =
|
141
|
"precision mediump float; \n"
|
142
|
+ "varying vec4 v_Color; \n"
|
143
|
|
144
|
+ "void main() \n"
|
145
|
+ " { \n"
|
146
|
+ " gl_FragColor = v_Color; \n"
|
147
|
+ " } \n";
|
148
|
|
149
|
int vertexShaderHandle = GLES31.glCreateShader(GLES31.GL_VERTEX_SHADER);
|
150
|
|
151
|
if (vertexShaderHandle != 0)
|
152
|
{
|
153
|
GLES31.glShaderSource(vertexShaderHandle, vertexShader);
|
154
|
GLES31.glCompileShader(vertexShaderHandle);
|
155
|
|
156
|
final int[] compileStatus = new int[1];
|
157
|
GLES31.glGetShaderiv(vertexShaderHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
|
158
|
|
159
|
if (compileStatus[0] == 0)
|
160
|
{
|
161
|
GLES31.glDeleteShader(vertexShaderHandle);
|
162
|
vertexShaderHandle = 0;
|
163
|
}
|
164
|
}
|
165
|
|
166
|
if (vertexShaderHandle == 0)
|
167
|
{
|
168
|
throw new RuntimeException("Error creating vertex shader.");
|
169
|
}
|
170
|
|
171
|
int fragmentShaderHandle = GLES31.glCreateShader(GLES31.GL_FRAGMENT_SHADER);
|
172
|
|
173
|
if (fragmentShaderHandle != 0)
|
174
|
{
|
175
|
GLES31.glShaderSource(fragmentShaderHandle, fragmentShader);
|
176
|
GLES31.glCompileShader(fragmentShaderHandle);
|
177
|
|
178
|
final int[] compileStatus = new int[1];
|
179
|
GLES31.glGetShaderiv(fragmentShaderHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
|
180
|
|
181
|
if (compileStatus[0] == 0)
|
182
|
{
|
183
|
GLES31.glDeleteShader(fragmentShaderHandle);
|
184
|
fragmentShaderHandle = 0;
|
185
|
}
|
186
|
}
|
187
|
|
188
|
if (fragmentShaderHandle == 0)
|
189
|
{
|
190
|
throw new RuntimeException("Error creating fragment shader.");
|
191
|
}
|
192
|
|
193
|
int programHandle = GLES31.glCreateProgram();
|
194
|
|
195
|
if (programHandle != 0)
|
196
|
{
|
197
|
GLES31.glAttachShader(programHandle, vertexShaderHandle);
|
198
|
GLES31.glAttachShader(programHandle, fragmentShaderHandle);
|
199
|
GLES31.glBindAttribLocation(programHandle, 0, "a_Position");
|
200
|
GLES31.glBindAttribLocation(programHandle, 1, "a_Color");
|
201
|
GLES31.glLinkProgram(programHandle);
|
202
|
|
203
|
final int[] linkStatus = new int[1];
|
204
|
GLES31.glGetProgramiv(programHandle, GLES31.GL_LINK_STATUS, linkStatus, 0);
|
205
|
|
206
|
if (linkStatus[0] == 0)
|
207
|
{
|
208
|
GLES31.glDeleteProgram(programHandle);
|
209
|
programHandle = 0;
|
210
|
}
|
211
|
}
|
212
|
|
213
|
if (programHandle == 0)
|
214
|
{
|
215
|
throw new RuntimeException("Error creating program.");
|
216
|
}
|
217
|
|
218
|
mMVPMatrixH = GLES31.glGetUniformLocation(programHandle, "u_MVPMatrix");
|
219
|
mPositionH = GLES31.glGetAttribLocation(programHandle, "a_Position");
|
220
|
mColorH = GLES31.glGetAttribLocation(programHandle, "a_Color");
|
221
|
|
222
|
GLES31.glUseProgram(programHandle);
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
@Override
|
228
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
229
|
{
|
230
|
mHeight = height;
|
231
|
mWidth = width;
|
232
|
|
233
|
GLES31.glViewport(0, 0, width, height);
|
234
|
|
235
|
final float ratio = (float) width / height;
|
236
|
final float left =-ratio;
|
237
|
final float right = ratio;
|
238
|
final float bottom =-1.0f;
|
239
|
final float top = 1.0f;
|
240
|
final float near = 1.0f;
|
241
|
final float far = 2.0f;
|
242
|
|
243
|
MatrixHelper.frustum(mProjectionMatrix, left, right, bottom, top, near, far);
|
244
|
MatrixHelper.multiply(mMVPTriangleMatrix, mProjectionMatrix, mViewMatrix);
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
@Override
|
250
|
public void onDrawFrame(GL10 glUnused)
|
251
|
{
|
252
|
GLES31.glClear(GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT);
|
253
|
GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
|
254
|
|
255
|
///////// 'white-black-yellow' triangle //////
|
256
|
mTriangleVert.position(POSITION_OFFSET);
|
257
|
GLES31.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
|
258
|
GLES31.glEnableVertexAttribArray(mPositionH);
|
259
|
mTriangleVert.position(COLOR_OFFSET);
|
260
|
GLES31.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
|
261
|
GLES31.glEnableVertexAttribArray(mColorH);
|
262
|
|
263
|
GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
|
264
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLES, 0, 3);
|
265
|
|
266
|
//////// 'current position' square ///////////
|
267
|
mSquareVert.position(POSITION_OFFSET);
|
268
|
GLES31.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
|
269
|
GLES31.glEnableVertexAttribArray(mPositionH);
|
270
|
mSquareVert.position(COLOR_OFFSET);
|
271
|
GLES31.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
|
272
|
GLES31.glEnableVertexAttribArray(mColorH);
|
273
|
|
274
|
MatrixHelper.multiply(mTmpMatrix, mViewMatrix, mModelMatrix);
|
275
|
MatrixHelper.multiply(mMVPSquareMatrix, mProjectionMatrix, mTmpMatrix );
|
276
|
|
277
|
GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPSquareMatrix, 0);
|
278
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0,4);
|
279
|
}
|
280
|
}
|