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