Guest User

Untitled

a guest
Oct 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. // HelloOpenGL.java
  2.  
  3.  
  4. package com.rvasolutions.helloopengl;
  5.  
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.opengl.GLSurfaceView;
  9. import android.os.Bundle;
  10. import android.view.MotionEvent;
  11.  
  12. public class HelloOpenGL extends Activity {
  13.    
  14.     private GLSurfaceView mGLView;
  15.    
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.        
  20.         // Create a GLSurfaceView instance and set it
  21.         // as the ContentView for this activity
  22.         mGLView = new HelloOpenGLSurfaceView(this);
  23.         setContentView(mGLView);
  24.     }
  25.    
  26.     @Override
  27.     protected void onPause() {
  28.         super.onPause();
  29.         // The following call pauses the rendering thread.
  30.         // If your OpenGL application is memory intensive,
  31.         // you should consider de-allocating objects that
  32.         // consume significant memory here.
  33.         mGLView.onPause();
  34.     }
  35.    
  36.     @Override
  37.     protected void onResume() {
  38.         super.onResume();
  39.         // The following call resumes a paused rendering thread.
  40.         // If you de-allocated graphics objects for onPause()
  41.         // this is a good place to reallocate them.
  42.         mGLView.onResume();
  43.     }    
  44. }
  45.  
  46. class HelloOpenGLSurfaceView extends GLSurfaceView {
  47.    
  48.     private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
  49.     private HelloOpenGLRenderer mRenderer;
  50.     private float mPreviousX;
  51.     private float mPreviousY;
  52.    
  53.     public HelloOpenGLSurfaceView(Context context){
  54.         super(context);
  55.        
  56.         // Create an OpenGL ES 2.0 Context
  57.         setEGLContextClientVersion(2);
  58.        
  59.         // set the mRenderer member
  60.         mRenderer = new HelloOpenGLRenderer();
  61.         setRenderer(mRenderer);
  62.        
  63.         // Render the view only when there is a change
  64.         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
  65.     }
  66.    
  67.     @Override
  68.     public boolean onTouchEvent(MotionEvent e) {
  69.         // MotionEvent reports input details from the touch screen
  70.         // and other input controls. In this case, you are only
  71.         // interested in events where the touch position changed.
  72.        
  73.         float x = e.getX();
  74.         float y = e.getY();
  75.        
  76.         switch(e.getAction()) {
  77.             case MotionEvent.ACTION_MOVE:
  78.                
  79.                 float dx = x - mPreviousX;
  80.                 float dy = y - mPreviousY;
  81.                
  82.                 // reverse direction of rotation above the midline
  83.                 if ( y > getHeight() /2) {
  84.                     dx = dx * -1;
  85.                 }
  86.                
  87.                 // reverse direction of rotation to left of the midline
  88.                 if ( x < getWidth() /2) {
  89.                     dy = dy * -1;
  90.                 }
  91.                
  92.                 mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;
  93.                 requestRender();
  94.         }
  95.        
  96.         mPreviousX = x;
  97.         mPreviousY = y;
  98.         return true;
  99.     }
  100. }
Add Comment
Please, Sign In to add comment