Project

General

Profile

« Previous | Next » 

Revision 951c4af9

Added by Leszek Koltunski about 8 years ago

Around the World: moving the selection rectangle

View differences:

src/main/java/org/distorted/examples/aroundtheworld/AroundTheWorldRendererPicker.java
34 34

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

  
41
	private final FloatBuffer mTriangleVertices;
50
	private final FloatBuffer mTriangleVert, mSquareVert;
42 51

  
43 52
  private int mMVPMatrixH;
44 53
  private int mPositionH;
45 54
	private int mColorH;
46 55

  
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;
56
  private int mHeight, mWidth;
53 57

  
54 58
///////////////////////////////////////////////////////////////////////////////////////////////////
55 59

  
56 60
	AroundTheWorldRendererPicker()
57 61
	  {
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
62
	  Matrix.setIdentityM(mModelMatrix, 0);
61 63

  
62
	            -0.865f,-1.5f, 0.0f,
63
	            0.0f, 0.0f, 0.0f, 1.0f,
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 };
64 68

  
65
	            1.73f, 0.0f, 0.0f,
66
	            0.5f, 0.5f, 0.0f, 1.0f };
69
		mTriangleVert = ByteBuffer.allocateDirect(triangleVertData.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
70
    mTriangleVert.put(triangleVertData).position(0);
67 71

  
68
		mTriangleVertices = ByteBuffer.allocateDirect(triangle1VerticesData.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
    mTriangleVertices.put(triangle1VerticesData).position(0);
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);
70 80
	  }
71 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

  
72 104
///////////////////////////////////////////////////////////////////////////////////////////////////
73 105

  
74 106
	@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
......
190 222

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

  
193 228
		GLES20.glViewport(0, 0, width, height);
194 229

  
195 230
		final float ratio  = (float) width / height;
......
201 236
		final float far    = 2.0f;
202 237

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

  
207 242
///////////////////////////////////////////////////////////////////////////////////////////////////
......
209 244
	@Override public void onDrawFrame(GL10 glUnused)
210 245
	  {
211 246
		GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
247
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
212 248

  
213
    mTriangleVertices.position(mPositionOffset);
214
    GLES20.glVertexAttribPointer(mPositionH, mPositionDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
249
    ///////// 'white-black-yellow' triangle //////
250
    mTriangleVert.position(POSITION_OFFSET);
251
    GLES20.glVertexAttribPointer(mPositionH, POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
215 252
    GLES20.glEnableVertexAttribArray(mPositionH);
253
    mTriangleVert.position(COLOR_OFFSET);
254
    GLES20.glVertexAttribPointer(mColorH, COLOR_DATA_SIZE, GLES20.GL_FLOAT, false, STRIDE_BYTES, mTriangleVert);
255
    GLES20.glEnableVertexAttribArray(mColorH);
216 256

  
217
    mTriangleVertices.position(mColorOffset);
218
    GLES20.glVertexAttribPointer(mColorH, mColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, mTriangleVertices);
257
		GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
258
		GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
259

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

  
221
		GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
222
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
268
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
269
    Matrix.multiplyMM(mMVPSquareMatrix, 0, mProjectionMatrix, 0, mMVPSquareMatrix, 0);
270

  
271
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPSquareMatrix, 0);
272
		GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0,4);
223 273
	  }
224 274
  }
src/main/java/org/distorted/examples/aroundtheworld/AroundTheWorldSurfaceViewPicker.java
64 64
      int x = (int)event.getX();
65 65
      int y = (int)event.getY();
66 66

  
67
      android.util.Log.e("Picker", "touched at x="+x+" y="+y);
67
      int h = mRenderer.getHeight();
68
      int w = mRenderer.getWidth();
69

  
70
      float xf = (x-w/2)*3.0f/h;
71
      float yf =-(y-h/2)*3.0f/h;
72

  
73
      if( yf>-1.5f && yf<1.5f )
74
        {
75
        if( yf>=0 && yf> 1-0.577f*xf )
76
          {
77
          float d = 0.577f*xf + yf;
78
          yf /= d;
79
          xf /= d;
80
          }
81
        else if( yf<=0 && yf< 0.577f*xf-1 )
82
          {
83
          float d = 0.577f*xf - yf;
84
          yf /= d;
85
          xf /= d;
86
          }
87
        else if( xf< -0.865f )
88
          {
89
          yf *= (-0.865f/xf);
90
          xf  =  -0.865f;
91
          }
92

  
93
        mRenderer.move(xf,yf);
94
        }
68 95

  
69 96
      return true;
70 97
      }

Also available in: Unified diff