Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRendererPicker.java @ fe59d375

1 248a2782 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
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
	@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
107
	  {
108 41a81a14 Leszek Koltunski
		GLES30.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
109 248a2782 Leszek Koltunski
110
		final float eyeX = 0.0f;
111
		final float eyeY = 0.0f;
112
		final float eyeZ = 1.5f;
113
114
		final float lookX = 0.0f;
115
		final float lookY = 0.0f;
116 80508ef5 Leszek Koltunski
		final float lookZ = 0.0f;
117 248a2782 Leszek Koltunski
118
		final float upX = 0.0f;
119
		final float upY = 1.0f;
120
		final float upZ = 0.0f;
121
122
		Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
123
124
		final String vertexShader =
125 80508ef5 Leszek Koltunski
			  "uniform mat4 u_MVPMatrix;                \n"
126
		  + "attribute vec4 a_Position;               \n"
127
		  + "attribute vec4 a_Color;                  \n"
128
		  + "varying vec4 v_Color;                    \n"
129 248a2782 Leszek Koltunski
130 80508ef5 Leszek Koltunski
		  + "void main()                              \n"
131
		  + "  {                                      \n"
132
		  + "  v_Color = a_Color;                     \n"
133
		  + "  gl_Position = u_MVPMatrix* a_Position; \n"
134
		  + "  }                                      \n";
135 248a2782 Leszek Koltunski
136
		final String fragmentShader =
137 80508ef5 Leszek Koltunski
			  "precision mediump float;     \n"
138
		  + "varying vec4 v_Color;        \n"
139 248a2782 Leszek Koltunski
140 80508ef5 Leszek Koltunski
		  + "void main()                  \n"
141
		  + "  {                          \n"
142
		  + "  gl_FragColor = v_Color;    \n"
143
		  + "  }                          \n";
144 248a2782 Leszek Koltunski
145 41a81a14 Leszek Koltunski
		int vertexShaderHandle = GLES30.glCreateShader(GLES30.GL_VERTEX_SHADER);
146 248a2782 Leszek Koltunski
147
		if (vertexShaderHandle != 0)
148
		  {
149 41a81a14 Leszek Koltunski
			GLES30.glShaderSource(vertexShaderHandle, vertexShader);
150
      GLES30.glCompileShader(vertexShaderHandle);
151 248a2782 Leszek Koltunski
152
      final int[] compileStatus = new int[1];
153 41a81a14 Leszek Koltunski
			GLES30.glGetShaderiv(vertexShaderHandle, GLES30.GL_COMPILE_STATUS, compileStatus, 0);
154 248a2782 Leszek Koltunski
155
			if (compileStatus[0] == 0)
156
  			{
157 41a81a14 Leszek Koltunski
				GLES30.glDeleteShader(vertexShaderHandle);
158 248a2782 Leszek Koltunski
				vertexShaderHandle = 0;
159
	  		}
160
		  }
161
162
		if (vertexShaderHandle == 0)
163
		  {
164
			throw new RuntimeException("Error creating vertex shader.");
165
		  }
166
167 41a81a14 Leszek Koltunski
		int fragmentShaderHandle = GLES30.glCreateShader(GLES30.GL_FRAGMENT_SHADER);
168 248a2782 Leszek Koltunski
169
		if (fragmentShaderHandle != 0)
170
		  {
171 41a81a14 Leszek Koltunski
			GLES30.glShaderSource(fragmentShaderHandle, fragmentShader);
172
      GLES30.glCompileShader(fragmentShaderHandle);
173 248a2782 Leszek Koltunski
174
			final int[] compileStatus = new int[1];
175 41a81a14 Leszek Koltunski
			GLES30.glGetShaderiv(fragmentShaderHandle, GLES30.GL_COMPILE_STATUS, compileStatus, 0);
176 248a2782 Leszek Koltunski
177
			if (compileStatus[0] == 0)
178
			  {
179 41a81a14 Leszek Koltunski
				GLES30.glDeleteShader(fragmentShaderHandle);
180 248a2782 Leszek Koltunski
				fragmentShaderHandle = 0;
181
			  }
182
		  }
183
184
		if (fragmentShaderHandle == 0)
185
		  {
186
			throw new RuntimeException("Error creating fragment shader.");
187
		  }
188
189 41a81a14 Leszek Koltunski
		int programHandle = GLES30.glCreateProgram();
190 248a2782 Leszek Koltunski
191
		if (programHandle != 0)
192
		  {
193 41a81a14 Leszek Koltunski
			GLES30.glAttachShader(programHandle, vertexShaderHandle);
194
      GLES30.glAttachShader(programHandle, fragmentShaderHandle);
195
			GLES30.glBindAttribLocation(programHandle, 0, "a_Position");
196
			GLES30.glBindAttribLocation(programHandle, 1, "a_Color");
197
			GLES30.glLinkProgram(programHandle);
198 248a2782 Leszek Koltunski
199
			final int[] linkStatus = new int[1];
200 41a81a14 Leszek Koltunski
			GLES30.glGetProgramiv(programHandle, GLES30.GL_LINK_STATUS, linkStatus, 0);
201 248a2782 Leszek Koltunski
202
			if (linkStatus[0] == 0)
203
			  {
204 41a81a14 Leszek Koltunski
				GLES30.glDeleteProgram(programHandle);
205 248a2782 Leszek Koltunski
				programHandle = 0;
206
			  }
207
		  }
208
209
		if (programHandle == 0)
210
		  {
211
			throw new RuntimeException("Error creating program.");
212
		  }
213
214 41a81a14 Leszek Koltunski
    mMVPMatrixH = GLES30.glGetUniformLocation(programHandle, "u_MVPMatrix");
215
    mPositionH  = GLES30.glGetAttribLocation(programHandle, "a_Position");
216
    mColorH     = GLES30.glGetAttribLocation(programHandle, "a_Color");
217 248a2782 Leszek Koltunski
218 41a81a14 Leszek Koltunski
    GLES30.glUseProgram(programHandle);
219 248a2782 Leszek Koltunski
	  }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223
	@Override public void onSurfaceChanged(GL10 glUnused, int width, int height)
224
	  {
225 951c4af9 Leszek Koltunski
	  mHeight = height;
226
	  mWidth  = width;
227
228 41a81a14 Leszek Koltunski
		GLES30.glViewport(0, 0, width, height);
229 248a2782 Leszek Koltunski
230 80508ef5 Leszek Koltunski
		final float ratio  = (float) width / height;
231
		final float left   =-ratio;
232
		final float right  = ratio;
233
		final float bottom =-1.0f;
234
		final float top    = 1.0f;
235
		final float near   = 1.0f;
236
		final float far    = 2.0f;
237 248a2782 Leszek Koltunski
238
		Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
239 951c4af9 Leszek Koltunski
		Matrix.multiplyMM(mMVPTriangleMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
240 248a2782 Leszek Koltunski
	  }
241
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
244
	@Override public void onDrawFrame(GL10 glUnused)
245
	  {
246 41a81a14 Leszek Koltunski
		GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
247
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
248 248a2782 Leszek Koltunski
249 951c4af9 Leszek Koltunski
    ///////// 'white-black-yellow' triangle //////
250
    mTriangleVert.position(POSITION_OFFSET);
251 41a81a14 Leszek Koltunski
    GLES30.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
252
    GLES30.glEnableVertexAttribArray(mPositionH);
253 951c4af9 Leszek Koltunski
    mTriangleVert.position(COLOR_OFFSET);
254 41a81a14 Leszek Koltunski
    GLES30.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
255
    GLES30.glEnableVertexAttribArray(mColorH);
256 248a2782 Leszek Koltunski
257 41a81a14 Leszek Koltunski
		GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
258
		GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, 3);
259 951c4af9 Leszek Koltunski
260
    //////// 'current position' square ///////////
261
    mSquareVert.position(POSITION_OFFSET);
262 41a81a14 Leszek Koltunski
    GLES30.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
263
    GLES30.glEnableVertexAttribArray(mPositionH);
264 951c4af9 Leszek Koltunski
    mSquareVert.position(COLOR_OFFSET);
265 41a81a14 Leszek Koltunski
    GLES30.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
266
    GLES30.glEnableVertexAttribArray(mColorH);
267 248a2782 Leszek Koltunski
268 951c4af9 Leszek Koltunski
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
269
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mProjectionMatrix, 0, mMVPSquareMatrix, 0);
270
271 41a81a14 Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPSquareMatrix, 0);
272
		GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0,4);
273 248a2782 Leszek Koltunski
	  }
274
  }