Project

General

Profile

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

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

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.GLES30;
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 public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
107
	  {
108
		GLES30.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
109

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

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

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

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

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

    
145
		int vertexShaderHandle = GLES30.glCreateShader(GLES30.GL_VERTEX_SHADER);
146

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

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

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

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

    
167
		int fragmentShaderHandle = GLES30.glCreateShader(GLES30.GL_FRAGMENT_SHADER);
168

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

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

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

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

    
189
		int programHandle = GLES30.glCreateProgram();
190

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

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

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

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

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

    
218
    GLES30.glUseProgram(programHandle);
219
	  }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

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

    
228
		GLES30.glViewport(0, 0, width, height);
229

    
230
		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

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
	@Override public void onDrawFrame(GL10 glUnused)
245
	  {
246
		GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
247
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
248

    
249
    ///////// 'white-black-yellow' triangle //////
250
    mTriangleVert.position(POSITION_OFFSET);
251
    GLES30.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
252
    GLES30.glEnableVertexAttribArray(mPositionH);
253
    mTriangleVert.position(COLOR_OFFSET);
254
    GLES30.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
255
    GLES30.glEnableVertexAttribArray(mColorH);
256

    
257
		GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
258
		GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, 3);
259

    
260
    //////// 'current position' square ///////////
261
    mSquareVert.position(POSITION_OFFSET);
262
    GLES30.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
263
    GLES30.glEnableVertexAttribArray(mPositionH);
264
    mSquareVert.position(COLOR_OFFSET);
265
    GLES30.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES30.GL_FLOAT, false, STRIDE_BYTES, mSquareVert);
266
    GLES30.glEnableVertexAttribArray(mColorH);
267

    
268
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
269
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mProjectionMatrix, 0, mMVPSquareMatrix, 0);
270

    
271
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPSquareMatrix, 0);
272
		GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0,4);
273
	  }
274
  }
(4-4/6)