Changeset 14073
- Timestamp:
- 11/30/12 15:18:38 (12 years ago)
- Location:
- issm/trunk-jpl/src/android/ISSM_Visual/src/com/example/issm_visual
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/android/ISSM_Visual/src/com/example/issm_visual/ISSMVisual.java
r13958 r14073 3 3 4 4 import android.app.Activity; 5 import android.content.Context; 6 import android.opengl.GLES20; 5 import android.content.DialogInterface.OnClickListener; 7 6 import android.opengl.GLSurfaceView; 8 import android.opengl.GLSurfaceView.Renderer;9 7 import android.os.Bundle; 10 import android.util.AttributeSet;11 8 import android.view.Menu; 12 9 import android.view.View; 13 import android.view.View.OnClickListener;14 10 import android.widget.Button; 15 11 import android.widget.FrameLayout; 16 import android.widget.RelativeLayout;17 12 import android.widget.SeekBar; 18 13 import android.widget.SeekBar.OnSeekBarChangeListener; 19 14 import android.widget.TextView; 20 import android.widget.Toast;21 import java.nio.ByteBuffer;22 import java.nio.ByteOrder;23 import java.nio.FloatBuffer;24 import java.nio.ShortBuffer;25 import javax.microedition.khronos.egl.EGLConfig;26 import javax.microedition.khronos.opengles.GL10;27 15 28 16 public class ISSMVisual extends Activity implements OnSeekBarChangeListener 29 17 { 30 18 private SeekBar bar; 31 private TextView txtStatus,txtValue;19 private TextView /*txtStatus,*/ txtValue; 32 20 private GLSurfaceView mGLView; 33 21 private FrameLayout frame; 22 34 23 @Override 35 public void onCreate(Bundle savedInstanceState) { 24 public void onCreate(Bundle savedInstanceState) 25 { 36 26 super.onCreate(savedInstanceState); 37 27 setContentView(R.layout.activity_issmvisual); … … 40 30 bar.setOnSeekBarChangeListener(this); // set seekbar listener 41 31 txtValue = (TextView)findViewById(R.id.value); 42 txtStatus = (TextView)findViewById(R.id.status); 32 Button reset = (Button)findViewById(R.id.button1); 33 reset.setOnClickListener(new View.OnClickListener() 34 { 35 public void onClick(View v) 36 { 37 frame.removeView(mGLView); 38 mGLView = new MyGLSurfaceView(ISSMVisual.this); 39 frame.addView(mGLView); 40 } 41 }); 42 //txtStatus = (TextView)findViewById(R.id.status); 43 43 44 44 mGLView = new MyGLSurfaceView(this); … … 46 46 frame.addView(mGLView); 47 47 } 48 49 50 @Override 51 public void onProgressChanged(SeekBar seekBar, int val, 48 public void onProgressChanged(SeekBar seekBar, int val, 52 49 boolean fromUser) 53 50 { 54 51 // change progress text label with current Seekbar value 55 txtValue.setText(" Value:" + val);56 txtStatus.setText("Status: changing");52 txtValue.setText("alpha value = " + val); 53 //txtStatus.setText("Status: changing"); 57 54 } 58 55 59 @Override 60 public void onStartTrackingTouch(SeekBar seekBar) 56 public void onStartTrackingTouch(SeekBar seekBar) {/*txtStatus.setText("Status: Starting to track touch");*/} 57 58 public void onStopTrackingTouch(SeekBar seekBar) 61 59 { 62 txtStatus.setText("Status: Starting to track touch"); 63 60 seekBar.setSecondaryProgress(seekBar.getProgress()); // set the shade of the previous value. 61 62 //txtStatus.setText("Status: Ended tracking touch"); 63 frame.removeView(mGLView); 64 mGLView = new MyGLSurfaceView(this); 65 frame.addView(mGLView); 64 66 } 65 67 66 68 @Override 67 p ublic void onStopTrackingTouch(SeekBar seekBar)69 protected void onPause() 68 70 { 69 seekBar.setSecondaryProgress(seekBar.getProgress()); // set the shade of the previous value.70 txtStatus.setText("Status: Ended tracking touch");71 }72 73 @Override74 protected void onPause() {75 71 super.onPause(); 76 72 // The following call pauses the rendering thread. … … 82 78 83 79 @Override 84 protected void onResume() { 80 protected void onResume() 81 { 85 82 super.onResume(); 86 83 // The following call resumes a paused rendering thread. … … 90 87 } 91 88 92 93 89 @Override 94 90 public boolean onCreateOptionsMenu(Menu menu) -
issm/trunk-jpl/src/android/ISSM_Visual/src/com/example/issm_visual/MyGLRenderer.java
r13958 r14073 25 25 import android.opengl.GLES20; 26 26 import android.opengl.GLSurfaceView; 27 28 public class MyGLRenderer implements GLSurfaceView.Renderer { 29 30 private Triangle mTriangle1, mTriangle2, mTriangle3; 31 32 @Override 33 public void onSurfaceCreated(GL10 unused, EGLConfig config) { 34 27 import android.opengl.Matrix; 28 29 public class MyGLRenderer implements GLSurfaceView.Renderer 30 { 31 public volatile float mAngle; 32 33 /** 34 * Store the model matrix. This matrix is used to move models from object space (where each model can be thought 35 * of being located at the center of the universe) to world space. 36 */ 37 private float[][] mModelMatrix; 38 39 /** 40 * Store the view matrix. This can be thought of as our camera. This matrix transforms world space to eye space; 41 * it positions things relative to our eye. 42 */ 43 private float[][] mViewMatrix; 44 45 /** Store the projection matrix. This is used to project the scene onto a 2D viewport. */ 46 private float[][] mProjectionMatrix; 47 48 /** Allocate storage for the final combined matrix. This will be passed into the shader program. */ 49 private float[][] mMVPMatrix; 50 51 /** This will be used to pass in the transformation matrix. */ 52 private int mMatrixHandle; 53 54 private FloatBuffer[] triangleVert; 55 56 private float[][] triangleData2DArr; 57 58 private int mPositionHandle; 59 60 private int mColorHandle; 61 62 /** How many bytes per float. */ 63 private final int mBytesPerFloat = 4; 64 65 /** How many elements per vertex. */ 66 private final int mStrideBytes = 7 * mBytesPerFloat; 67 68 /** Offset of the position data. */ 69 private final int mPositionOffset = 0; 70 71 /** Size of the position data in elements. */ 72 private final int mPositionDataSize = 3; 73 74 /** Offset of the color data. */ 75 private final int mColorOffset = 3; 76 77 /** Size of the color data in elements. */ 78 private final int mColorDataSize = 4; 79 80 public MyGLRenderer(float[][] vertices) 81 { 82 triangleData2DArr = new float[vertices.length][21]; 83 triangleVert = new FloatBuffer[vertices.length]; 84 85 for (int i = 0; i < vertices.length; i++) 86 { 87 for (int j = 0; j < 21; j++) 88 triangleData2DArr[i][j] = vertices[i][j]; 89 } 90 91 // initialize vertex byte buffer for shape coordinates 92 for (int i = 0; i < vertices.length; i++) 93 { 94 triangleVert[i] = ByteBuffer.allocateDirect(triangleData2DArr[i].length * mBytesPerFloat) 95 .order(ByteOrder.nativeOrder()).asFloatBuffer(); 96 triangleVert[i].put(triangleData2DArr[i]).position(0); 97 } 98 } 99 100 public void onSurfaceCreated(GL10 unused, EGLConfig config) 101 { 35 102 // Set the background frame color 36 GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 37 38 mTriangle1 = new Triangle( 39 0.0f, 0.6f, 0.0f, // top 40 -0.5f, -0.3f, 0.0f, // bottom left 41 0.5f, -0.3f, 0.0f, 42 0.0f, 0.0f, 1.0f, 1.0f); // bottom right 43 //mTriangle = new Triangle(); 44 45 mTriangle2 = new Triangle( 46 -0.5f, -0.3f, 0.0f, 47 0.0f, -1.0f, 0.0f, 48 0.5f, -0.3f, 0.0f, 49 1.0f, 0.0f, 0.0f, 1.0f); 50 51 mTriangle3 = new Triangle( 52 0.0f, 0.6f, 0.0f, 53 0.5f, -0.3f, 0.0f, 54 1.0f, 0.6f, 0.0f, 55 1.0f, 1.0f, 0.0f, 1.0f); 56 } 57 58 @Override 59 public void onDrawFrame(GL10 unused) { 60 61 // Draw background color 62 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 63 64 // Draw triangle 65 mTriangle1.draw(); 66 mTriangle2.draw(); 67 mTriangle3.draw(); 68 } 69 70 @Override 71 public void onSurfaceChanged(GL10 unused, int width, int height) { 103 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 104 105 // Position the eye behind the origin. 106 final float eyeX = 0.0f; 107 final float eyeY = 0.0f; 108 final float eyeZ = 1.5f; 109 110 // We are looking toward the distance 111 final float lookX = 0.0f; 112 final float lookY = 0.0f; 113 final float lookZ = -5.0f; 114 115 // Set our up vector. This is where our head would be pointing were we holding the camera. 116 final float upX = 0.0f; 117 final float upY = 1.0f; 118 final float upZ = 0.0f; 119 120 // Set the view matrix. This matrix can be said to represent the camera position. 121 // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and 122 // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose. 123 mViewMatrix = new float[triangleVert.length][16]; 124 125 for (int i = 0; i < triangleVert.length; i++) 126 Matrix.setLookAtM(mViewMatrix[i], 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); 127 128 final String vertexShader = 129 "uniform mat4 u_MVPMatrix; \n" // A constant representing the combined model/view/projection matrix. 130 + "attribute vec4 a_Position; \n" // Per-vertex position information we will pass in. 131 + "attribute vec4 a_Color; \n" // Per-vertex color information we will pass in. 132 + "varying vec4 v_Color; \n" // This will be passed into the fragment shader. 133 + "void main() \n" // The entry point for our vertex shader. 134 + "{ \n" 135 + " v_Color = a_Color; \n" // Pass the color through to the fragment shader. 136 // It will be interpolated across the triangle. 137 + " gl_Position = u_MVPMatrix \n" // gl_Position is a special variable used to store the final position. 138 + " * a_Position; \n" // Multiply the vertex by the matrix to get the final point in 139 + "} \n"; // normalized screen coordinates. 140 141 final String fragmentShader = 142 "precision mediump float; \n" // Set the default precision to medium. We don't need as high of a // precision in the fragment shader. 143 + "varying vec4 v_Color; \n" // This is the color from the vertex shader interpolated across the 144 // triangle per fragment. 145 + "void main() \n" // The entry point for our fragment shader. 146 + "{ \n" 147 + " gl_FragColor = v_Color; \n" // Pass the color directly through the pipeline. 148 + "} \n"; 149 150 151 int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER); 152 // Pass in the shader source. 153 GLES20.glShaderSource(vertexShaderHandle, vertexShader); 154 155 // Compile the shader. 156 GLES20.glCompileShader(vertexShaderHandle); 157 158 // Get the compilation status. 159 final int[] compileStatus1 = new int[1]; 160 GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus1, 0); 161 162 // If the compilation failed, delete the shader. 163 if (compileStatus1[0] == 0) 164 { 165 GLES20.glDeleteShader(vertexShaderHandle); 166 vertexShaderHandle = 0; 167 } 168 169 170 int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER); 171 // Pass in the shader source. 172 GLES20.glShaderSource(fragmentShaderHandle, fragmentShader); 173 174 // Compile the shader. 175 GLES20.glCompileShader(fragmentShaderHandle); 176 177 // Get the compilation status. 178 final int[] compileStatus2 = new int[1]; 179 GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus2, 0); 180 181 // If the compilation failed, delete the shader. 182 if (compileStatus2[0] == 0) 183 { 184 GLES20.glDeleteShader(fragmentShaderHandle); 185 fragmentShaderHandle = 0; 186 } 187 188 189 int mProgram = GLES20.glCreateProgram(); 190 // Bind the vertex shader to the program. 191 GLES20.glAttachShader(mProgram, vertexShaderHandle); 192 193 // Bind the fragment shader to the program. 194 GLES20.glAttachShader(mProgram, fragmentShaderHandle); 195 196 // Bind attributes 197 GLES20.glBindAttribLocation(mProgram, 0, "a_Position"); 198 GLES20.glBindAttribLocation(mProgram, 1, "a_Color"); 199 200 // Link the two shaders together into a program. 201 GLES20.glLinkProgram(mProgram); 202 203 // Get the link status. 204 final int[] linkStatus = new int[1]; 205 GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0); 206 207 // If the link failed, delete the program. 208 if (linkStatus[0] == 0) 209 { 210 GLES20.glDeleteProgram(mProgram); 211 mProgram = 0; 212 } 213 214 for (int i = 0; i < triangleVert.length; i++) 215 { 216 mMatrixHandle = GLES20.glGetUniformLocation(mProgram, "u_MVPMatrix"); 217 mPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_Position"); 218 mColorHandle = GLES20.glGetAttribLocation(mProgram, "a_Color"); 219 } 220 221 // Add program to OpenGL environment 222 GLES20.glUseProgram(mProgram); 223 } 224 225 public void onSurfaceChanged(GL10 unused, int width, int height) 226 { 72 227 // Adjust the viewport based on geometry changes, 73 228 // such as screen rotation 74 229 GLES20.glViewport(0, 0, width, height); 75 } 76 77 public static int loadShader(int type, String shaderCode){ 78 79 // create a vertex shader type (GLES20.GL_VERTEX_SHADER) 80 // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) 81 int shader = GLES20.glCreateShader(type); 82 83 // add the source code to the shader and compile it 84 GLES20.glShaderSource(shader, shaderCode); 85 GLES20.glCompileShader(shader); 86 87 return shader; 230 231 // Create a new perspective projection matrix. The height will stay the same 232 // while the width will vary as per aspect ratio. 233 final float ratio = (float) width / height; 234 235 mProjectionMatrix = new float[triangleVert.length][16]; 236 for (int i = 0; i < triangleVert.length; i++) 237 Matrix.frustumM(mProjectionMatrix[i], 0, -ratio*1.4f, ratio*1.4f, -1.4f, 1.4f, 1.4f, 14f); 238 } 239 240 public void onDrawFrame(GL10 glUnused) 241 { 242 // Draw background color 243 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 244 245 mModelMatrix = new float[triangleVert.length][16]; 246 // Draw the triangle facing straight on. 247 for (int i = 0; i < triangleVert.length; i++) 248 Matrix.setIdentityM(mModelMatrix[i], 0); 249 250 mMVPMatrix = new float[triangleVert.length][16]; 251 draw(triangleVert); 252 253 } 254 255 private void draw(final FloatBuffer[] aTriangleBuffer) 256 { 257 for (int i = 0; i < aTriangleBuffer.length; i++) 258 { 259 // Pass in the position information 260 aTriangleBuffer[i].position(mPositionOffset); 261 GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 262 mStrideBytes, aTriangleBuffer[i]); 263 264 GLES20.glEnableVertexAttribArray(mPositionHandle); 265 266 267 // Pass in the color information 268 aTriangleBuffer[i].position(mColorOffset); 269 GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 270 mStrideBytes, aTriangleBuffer[i]); 271 272 GLES20.glEnableVertexAttribArray(mColorHandle); 273 274 Matrix.setRotateM(mModelMatrix[i], 0, mAngle, 0, 0, -1.0f); 275 276 // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix 277 // (which currently contains model * view). 278 Matrix.multiplyMM(mMVPMatrix[i], 0, mViewMatrix[i], 0, mModelMatrix[i], 0); 279 280 // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix 281 // (which now contains model * view * projection). 282 Matrix.multiplyMM(mMVPMatrix[i], 0, mProjectionMatrix[i], 0, mMVPMatrix[i], 0); 283 284 GLES20.glUniformMatrix4fv(mMatrixHandle, 1, false, mMVPMatrix[i], 0); 285 286 // Draw the triangle 287 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 288 289 // Disable vertex array 290 GLES20.glDisableVertexAttribArray(mPositionHandle); 291 } 88 292 } 89 293 90 294 } 91 92 class Triangle93 {94 95 private final String vertexShaderCode =96 "attribute vec4 vPosition;" +97 "void main() {" +98 " gl_Position = vPosition;" +99 "}";100 101 private final String fragmentShaderCode =102 "precision mediump float;" +103 "uniform vec4 vColor;" +104 "void main() {" +105 " gl_FragColor = vColor;" +106 "}";107 108 private final FloatBuffer vertexBuffer;109 private final int mProgram;110 private int mPositionHandle;111 private int mColorHandle;112 113 // number of coordinates per vertex in this array114 static final int COORDS_PER_VERTEX = 3;115 float triangleCoords[] ; /*){ // in counterclockwise order:116 0.0f, 0.6f, 0.0f, // top117 -0.5f, -0.3f, 0.0f, // bottom left118 0.5f, -0.3f, 0.0f}; */ // bottom right119 120 /* -0.55f, -0.35f, 0.0f,121 0.0f, -1.2f, 0.0f,122 0.55f, -0.35f, 0.0f123 };*/124 125 private final int vertexCount = 9 / COORDS_PER_VERTEX;126 private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex127 128 // Set color with red, green, blue and alpha (opacity) values129 float color[];130 //private final FloatBuffer colorBuffer;131 private final int colorCount = 4/COORDS_PER_VERTEX;132 133 public Triangle()134 {135 // initialize vertex byte buffer for shape coordinates136 ByteBuffer bb = ByteBuffer.allocateDirect(137 // (number of coordinate values * 4 bytes per float)138 triangleCoords.length * 4);139 // use the device hardware's native byte order140 bb.order(ByteOrder.nativeOrder());141 142 // create a floating point buffer from the ByteBuffer143 vertexBuffer = bb.asFloatBuffer();144 // add the coordinates to the FloatBuffer145 vertexBuffer.put(triangleCoords);146 // set the buffer to read the first coordinate147 vertexBuffer.position(0);148 149 /*ByteBuffer bb1 = ByteBuffer.allocateDirect(color.length*4);150 bb1.order(ByteOrder.nativeOrder());151 colorBuffer = bb1.asFloatBuffer();152 colorBuffer.put(color);153 colorBuffer.position(0);*/154 155 //GLES20.glEnableVertexAttribArray(1);156 //GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, colorBuffer);157 158 // prepare shaders and OpenGL program159 int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,160 vertexShaderCode);161 int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,162 fragmentShaderCode);163 164 mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program165 GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program166 GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program167 GLES20.glLinkProgram(mProgram); // create OpenGL program executables168 }169 public Triangle(float x1, float y1, float z1,170 float x2, float y2, float z2,171 float x3, float y3, float z3,172 float c1, float c2, float c3, float c4)173 {174 triangleCoords = new float[9];175 color = new float[4];176 triangleCoords[0] = x1;177 triangleCoords[1] = y1;178 triangleCoords[2] = z1;179 triangleCoords[3] = x2;180 triangleCoords[4] = y2;181 triangleCoords[5] = z2;182 triangleCoords[6] = x3;183 triangleCoords[7] = y3;184 triangleCoords[8] = z3;185 color[0] = c1;186 color[1] = c2;187 color[2] = c3;188 color[3] = c4;189 // initialize vertex byte buffer for shape coordinates190 ByteBuffer bb = ByteBuffer.allocateDirect(191 // (number of coordinate values * 4 bytes per float)192 triangleCoords.length * 4);193 // use the device hardware's native byte order194 bb.order(ByteOrder.nativeOrder());195 196 // create a floating point buffer from the ByteBuffer197 vertexBuffer = bb.asFloatBuffer();198 // add the coordinates to the FloatBuffer199 vertexBuffer.put(triangleCoords);200 // set the buffer to read the first coordinate201 vertexBuffer.position(0);202 203 /*ByteBuffer bb1 = ByteBuffer.allocateDirect(color.length*4);204 bb1.order(ByteOrder.nativeOrder());205 colorBuffer = bb1.asFloatBuffer();206 colorBuffer.put(color);207 colorBuffer.position(0);*/208 209 // prepare shaders and OpenGL program210 int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,211 vertexShaderCode);212 int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,213 fragmentShaderCode);214 215 mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program216 GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program217 GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program218 GLES20.glLinkProgram(mProgram); // create OpenGL program executables219 }220 221 public void draw()222 {223 // Add program to OpenGL environment224 GLES20.glUseProgram(mProgram);225 226 // get handle to vertex shader's vPosition member227 mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");228 229 // Enable a handle to the triangle vertices230 GLES20.glEnableVertexAttribArray(mPositionHandle);231 232 // Prepare the triangle coordinate data233 GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,234 GLES20.GL_FLOAT, false,235 vertexStride, vertexBuffer);236 237 // get handle to fragment shader's vColor member238 mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");239 240 // Set color for drawing the triangle241 //GLES20.glUniform4fv(mColorHandle, 0, color, 0);242 //GLES20.glVertexAttrib4f(0, 0.5f, 0.5f, 0.0f, 1.0f);243 //GLES20.glVertexAttribPointer(mPositionHandle, 4, GLES20.GL_FLOAT, false, stride, offset)244 GLES20.glUniform4fv(mColorHandle, 1, color, 0);245 // Draw the triangle246 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);247 }248 } -
issm/trunk-jpl/src/android/ISSM_Visual/src/com/example/issm_visual/MyGLSurfaceView.java
r13958 r14073 3 3 import android.content.Context; 4 4 import android.opengl.GLSurfaceView; 5 import android.view.MotionEvent; 5 6 6 7 7 class MyGLSurfaceView extends GLSurfaceView { 8 9 public MyGLSurfaceView(Context context) { 8 class MyGLSurfaceView extends GLSurfaceView 9 { 10 private MyGLRenderer mRend; 11 public MyGLSurfaceView(Context context) 12 { 10 13 super(context); 11 14 12 15 // Create an OpenGL ES 2.0 context. 13 16 setEGLContextClientVersion(2); 17 18 final int NMAX_TRIANGLES = 2000; 19 final int MAX_VERTICES = 21; 20 21 float f[][] = new float[NMAX_TRIANGLES][MAX_VERTICES]; 22 int n, random; 23 for (int i = 0; i < f.length; i++) 24 { 25 for (int j = 0; j < MAX_VERTICES; j++) 26 { 27 if (j == 6 || j == 13 || j == 20) continue; 28 n = 1; 29 random = (int)(Math.floor((Math.random()*10)+1)); 30 if (random <= 5) n = -1; 31 32 f[i][j] = (float)(Math.random()*n); 33 } 34 } 14 35 36 mRend = new MyGLRenderer(f); 37 15 38 // Set the Renderer for drawing on the GLSurfaceView 16 setRenderer( new MyGLRenderer());39 setRenderer(mRend); 17 40 18 41 // Render the view only when there is a change in the drawing data 19 42 setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 20 43 } 44 45 private final float TOUCH_SCALE_FACTOR = 180.0f / 320; 46 private float mPreviousX; 47 48 private float mPreviousY; 49 @Override 50 public boolean onTouchEvent(MotionEvent e) { 51 // MotionEvent reports input details from the touch screen 52 // and other input controls. In this case, you are only 53 // interested in events where the touch position changed. 54 55 float x = e.getX(); 56 float y = e.getY(); 57 58 switch (e.getAction()) { 59 case MotionEvent.ACTION_MOVE: 60 61 float dx = x - mPreviousX; 62 float dy = y - mPreviousY; 63 64 // reverse direction of rotation above the mid-line 65 if (y > getHeight() / 2) { 66 dx = dx * -1 ; 67 } 68 69 // reverse direction of rotation to left of the mid-line 70 if (x < getWidth() / 2) { 71 dy = dy * -1 ; 72 } 73 74 mRend.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR; // = 180.0f / 320 75 requestRender(); 76 } 77 78 mPreviousX = x; 79 mPreviousY = y; 80 return true; 81 } 21 82 }
Note:
See TracChangeset
for help on using the changeset viewer.