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.GLES20;
|
30
|
import android.opengl.GLSurfaceView;
|
31
|
import android.opengl.Matrix;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
class AroundTheWorldRendererPicker implements GLSurfaceView.Renderer
|
36
|
{
|
37
|
private float[] mViewMatrix = new float[16];
|
38
|
private float[] mProjectionMatrix = new float[16];
|
39
|
private float[] mMVPMatrix = new float[16];
|
40
|
|
41
|
private final FloatBuffer mTriangleVertices;
|
42
|
|
43
|
private int mMVPMatrixH;
|
44
|
private int mPositionH;
|
45
|
private int mColorH;
|
46
|
|
47
|
private final int mBytesPerFloat = 4;
|
48
|
private final int mStrideBytes = 7 * mBytesPerFloat;
|
49
|
private final int mPositionOffset = 0;
|
50
|
private final int mPositionDataSize = 3;
|
51
|
private final int mColorOffset = 3;
|
52
|
private final int mColorDataSize = 4;
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
AroundTheWorldRendererPicker()
|
57
|
{
|
58
|
final float[] triangle1VerticesData = {
|
59
|
-0.865f, 1.5f, 0.0f, // x,y,z
|
60
|
1.0f, 1.0f, 1.0f, 1.0f, // r,g,b
|
61
|
|
62
|
-0.865f,-1.5f, 0.0f,
|
63
|
0.0f, 0.0f, 0.0f, 1.0f,
|
64
|
|
65
|
1.73f, 0.0f, 0.0f,
|
66
|
0.5f, 0.5f, 0.0f, 1.0f };
|
67
|
|
68
|
mTriangleVertices = ByteBuffer.allocateDirect(triangle1VerticesData.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
69
|
mTriangleVertices.put(triangle1VerticesData).position(0);
|
70
|
}
|
71
|
|
72
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73
|
|
74
|
@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
75
|
{
|
76
|
GLES20.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
|
77
|
|
78
|
final float eyeX = 0.0f;
|
79
|
final float eyeY = 0.0f;
|
80
|
final float eyeZ = 1.5f;
|
81
|
|
82
|
final float lookX = 0.0f;
|
83
|
final float lookY = 0.0f;
|
84
|
final float lookZ = 0.0f;
|
85
|
|
86
|
final float upX = 0.0f;
|
87
|
final float upY = 1.0f;
|
88
|
final float upZ = 0.0f;
|
89
|
|
90
|
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
|
91
|
|
92
|
final String vertexShader =
|
93
|
"uniform mat4 u_MVPMatrix; \n"
|
94
|
+ "attribute vec4 a_Position; \n"
|
95
|
+ "attribute vec4 a_Color; \n"
|
96
|
+ "varying vec4 v_Color; \n"
|
97
|
|
98
|
+ "void main() \n"
|
99
|
+ " { \n"
|
100
|
+ " v_Color = a_Color; \n"
|
101
|
+ " gl_Position = u_MVPMatrix* a_Position; \n"
|
102
|
+ " } \n";
|
103
|
|
104
|
final String fragmentShader =
|
105
|
"precision mediump float; \n"
|
106
|
+ "varying vec4 v_Color; \n"
|
107
|
|
108
|
+ "void main() \n"
|
109
|
+ " { \n"
|
110
|
+ " gl_FragColor = v_Color; \n"
|
111
|
+ " } \n";
|
112
|
|
113
|
int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
|
114
|
|
115
|
if (vertexShaderHandle != 0)
|
116
|
{
|
117
|
GLES20.glShaderSource(vertexShaderHandle, vertexShader);
|
118
|
GLES20.glCompileShader(vertexShaderHandle);
|
119
|
|
120
|
final int[] compileStatus = new int[1];
|
121
|
GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
|
122
|
|
123
|
if (compileStatus[0] == 0)
|
124
|
{
|
125
|
GLES20.glDeleteShader(vertexShaderHandle);
|
126
|
vertexShaderHandle = 0;
|
127
|
}
|
128
|
}
|
129
|
|
130
|
if (vertexShaderHandle == 0)
|
131
|
{
|
132
|
throw new RuntimeException("Error creating vertex shader.");
|
133
|
}
|
134
|
|
135
|
int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
|
136
|
|
137
|
if (fragmentShaderHandle != 0)
|
138
|
{
|
139
|
GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);
|
140
|
GLES20.glCompileShader(fragmentShaderHandle);
|
141
|
|
142
|
final int[] compileStatus = new int[1];
|
143
|
GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
|
144
|
|
145
|
if (compileStatus[0] == 0)
|
146
|
{
|
147
|
GLES20.glDeleteShader(fragmentShaderHandle);
|
148
|
fragmentShaderHandle = 0;
|
149
|
}
|
150
|
}
|
151
|
|
152
|
if (fragmentShaderHandle == 0)
|
153
|
{
|
154
|
throw new RuntimeException("Error creating fragment shader.");
|
155
|
}
|
156
|
|
157
|
int programHandle = GLES20.glCreateProgram();
|
158
|
|
159
|
if (programHandle != 0)
|
160
|
{
|
161
|
GLES20.glAttachShader(programHandle, vertexShaderHandle);
|
162
|
GLES20.glAttachShader(programHandle, fragmentShaderHandle);
|
163
|
GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
|
164
|
GLES20.glBindAttribLocation(programHandle, 1, "a_Color");
|
165
|
GLES20.glLinkProgram(programHandle);
|
166
|
|
167
|
final int[] linkStatus = new int[1];
|
168
|
GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);
|
169
|
|
170
|
if (linkStatus[0] == 0)
|
171
|
{
|
172
|
GLES20.glDeleteProgram(programHandle);
|
173
|
programHandle = 0;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
if (programHandle == 0)
|
178
|
{
|
179
|
throw new RuntimeException("Error creating program.");
|
180
|
}
|
181
|
|
182
|
mMVPMatrixH = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
|
183
|
mPositionH = GLES20.glGetAttribLocation(programHandle, "a_Position");
|
184
|
mColorH = GLES20.glGetAttribLocation(programHandle, "a_Color");
|
185
|
|
186
|
GLES20.glUseProgram(programHandle);
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
@Override public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
192
|
{
|
193
|
GLES20.glViewport(0, 0, width, height);
|
194
|
|
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;
|
202
|
|
203
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
204
|
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
|
209
|
@Override public void onDrawFrame(GL10 glUnused)
|
210
|
{
|
211
|
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
212
|
|
213
|
mTriangleVertices.position(mPositionOffset);
|
214
|
GLES20.glVertexAttribPointer(mPositionH, mPositionDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
|
215
|
GLES20.glEnableVertexAttribArray(mPositionH);
|
216
|
|
217
|
mTriangleVertices.position(mColorOffset);
|
218
|
GLES20.glVertexAttribPointer(mColorH, mColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
|
219
|
GLES20.glEnableVertexAttribArray(mColorH);
|
220
|
|
221
|
GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
|
222
|
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
|
223
|
}
|
224
|
}
|