Project

General

Profile

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

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

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
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
class AroundTheWorldRendererPicker implements GLSurfaceView.Renderer
36
  {
37
	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
	private float[] mViewMatrix       = new float[16];
46
	private float[] mProjectionMatrix = new float[16];
47
	private float[] mMVPTriangleMatrix= new float[16];
48
  private float[] mMVPSquareMatrix  = new float[16];
49

    
50
	private final FloatBuffer mTriangleVert, mSquareVert;
51

    
52
  private int mMVPMatrixH;
53
  private int mPositionH;
54
	private int mColorH;
55

    
56
  private int mHeight, mWidth;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
	AroundTheWorldRendererPicker()
61
	  {
62
	  Matrix.setIdentityM(mModelMatrix, 0);
63

    
64
		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

    
69
		mTriangleVert = ByteBuffer.allocateDirect(triangleVertData.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
70
    mTriangleVert.put(triangleVertData).position(0);
71

    
72
    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
	  }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
	@Override
107
	public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
108
	  {
109
		GLES31.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
110

    
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
		final float lookZ = 0.0f;
118

    
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
			  "uniform mat4 u_MVPMatrix;                \n"
127
		  + "attribute vec4 a_Position;               \n"
128
		  + "attribute vec4 a_Color;                  \n"
129
		  + "varying vec4 v_Color;                    \n"
130

    
131
		  + "void main()                              \n"
132
		  + "  {                                      \n"
133
		  + "  v_Color = a_Color;                     \n"
134
		  + "  gl_Position = u_MVPMatrix* a_Position; \n"
135
		  + "  }                                      \n";
136

    
137
		final String fragmentShader =
138
			  "precision mediump float;     \n"
139
		  + "varying vec4 v_Color;        \n"
140

    
141
		  + "void main()                  \n"
142
		  + "  {                          \n"
143
		  + "  gl_FragColor = v_Color;    \n"
144
		  + "  }                          \n";
145

    
146
		int vertexShaderHandle = GLES31.glCreateShader(GLES31.GL_VERTEX_SHADER);
147

    
148
		if (vertexShaderHandle != 0)
149
		  {
150
			GLES31.glShaderSource(vertexShaderHandle, vertexShader);
151
      GLES31.glCompileShader(vertexShaderHandle);
152

    
153
      final int[] compileStatus = new int[1];
154
			GLES31.glGetShaderiv(vertexShaderHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
155

    
156
			if (compileStatus[0] == 0)
157
  			{
158
				GLES31.glDeleteShader(vertexShaderHandle);
159
				vertexShaderHandle = 0;
160
	  		}
161
		  }
162

    
163
		if (vertexShaderHandle == 0)
164
		  {
165
			throw new RuntimeException("Error creating vertex shader.");
166
		  }
167

    
168
		int fragmentShaderHandle = GLES31.glCreateShader(GLES31.GL_FRAGMENT_SHADER);
169

    
170
		if (fragmentShaderHandle != 0)
171
		  {
172
			GLES31.glShaderSource(fragmentShaderHandle, fragmentShader);
173
      GLES31.glCompileShader(fragmentShaderHandle);
174

    
175
			final int[] compileStatus = new int[1];
176
			GLES31.glGetShaderiv(fragmentShaderHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
177

    
178
			if (compileStatus[0] == 0)
179
			  {
180
				GLES31.glDeleteShader(fragmentShaderHandle);
181
				fragmentShaderHandle = 0;
182
			  }
183
		  }
184

    
185
		if (fragmentShaderHandle == 0)
186
		  {
187
			throw new RuntimeException("Error creating fragment shader.");
188
		  }
189

    
190
		int programHandle = GLES31.glCreateProgram();
191

    
192
		if (programHandle != 0)
193
		  {
194
			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

    
200
			final int[] linkStatus = new int[1];
201
			GLES31.glGetProgramiv(programHandle, GLES31.GL_LINK_STATUS, linkStatus, 0);
202

    
203
			if (linkStatus[0] == 0)
204
			  {
205
				GLES31.glDeleteProgram(programHandle);
206
				programHandle = 0;
207
			  }
208
		  }
209

    
210
		if (programHandle == 0)
211
		  {
212
			throw new RuntimeException("Error creating program.");
213
		  }
214

    
215
    mMVPMatrixH = GLES31.glGetUniformLocation(programHandle, "u_MVPMatrix");
216
    mPositionH  = GLES31.glGetAttribLocation(programHandle, "a_Position");
217
    mColorH     = GLES31.glGetAttribLocation(programHandle, "a_Color");
218

    
219
    GLES31.glUseProgram(programHandle);
220
	  }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
	@Override
225
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
226
	  {
227
	  mHeight = height;
228
	  mWidth  = width;
229

    
230
		GLES31.glViewport(0, 0, width, height);
231

    
232
		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

    
240
		Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
241
		Matrix.multiplyMM(mMVPTriangleMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
242
	  }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
	@Override
247
  public void onDrawFrame(GL10 glUnused)
248
	  {
249
		GLES31.glClear(GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT);
250
    GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
251

    
252
    ///////// 'white-black-yellow' triangle //////
253
    mTriangleVert.position(POSITION_OFFSET);
254
    GLES31.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
255
    GLES31.glEnableVertexAttribArray(mPositionH);
256
    mTriangleVert.position(COLOR_OFFSET);
257
    GLES31.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
258
    GLES31.glEnableVertexAttribArray(mColorH);
259

    
260
		GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
261
		GLES31.glDrawArrays(GLES31.GL_TRIANGLES, 0, 3);
262

    
263
    //////// 'current position' square ///////////
264
    mSquareVert.position(POSITION_OFFSET);
265
    GLES31.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
266
    GLES31.glEnableVertexAttribArray(mPositionH);
267
    mSquareVert.position(COLOR_OFFSET);
268
    GLES31.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES31.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
269
    GLES31.glEnableVertexAttribArray(mColorH);
270

    
271
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
272
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mProjectionMatrix, 0, mMVPSquareMatrix, 0);
273

    
274
    GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPSquareMatrix, 0);
275
		GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0,4);
276
	  }
277
  }
(4-4/6)