Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HelloOpenGL.java
- package com.rvasolutions.helloopengl;
- import android.app.Activity;
- import android.content.Context;
- import android.opengl.GLSurfaceView;
- import android.os.Bundle;
- import android.view.MotionEvent;
- public class HelloOpenGL extends Activity {
- private GLSurfaceView mGLView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Create a GLSurfaceView instance and set it
- // as the ContentView for this activity
- mGLView = new HelloOpenGLSurfaceView(this);
- setContentView(mGLView);
- }
- @Override
- protected void onPause() {
- super.onPause();
- // The following call pauses the rendering thread.
- // If your OpenGL application is memory intensive,
- // you should consider de-allocating objects that
- // consume significant memory here.
- mGLView.onPause();
- }
- @Override
- protected void onResume() {
- super.onResume();
- // The following call resumes a paused rendering thread.
- // If you de-allocated graphics objects for onPause()
- // this is a good place to reallocate them.
- mGLView.onResume();
- }
- }
- class HelloOpenGLSurfaceView extends GLSurfaceView {
- private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
- private HelloOpenGLRenderer mRenderer;
- private float mPreviousX;
- private float mPreviousY;
- public HelloOpenGLSurfaceView(Context context){
- super(context);
- // Create an OpenGL ES 2.0 Context
- setEGLContextClientVersion(2);
- // set the mRenderer member
- mRenderer = new HelloOpenGLRenderer();
- setRenderer(mRenderer);
- // Render the view only when there is a change
- setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
- }
- @Override
- public boolean onTouchEvent(MotionEvent e) {
- // MotionEvent reports input details from the touch screen
- // and other input controls. In this case, you are only
- // interested in events where the touch position changed.
- float x = e.getX();
- float y = e.getY();
- switch(e.getAction()) {
- case MotionEvent.ACTION_MOVE:
- float dx = x - mPreviousX;
- float dy = y - mPreviousY;
- // reverse direction of rotation above the midline
- if ( y > getHeight() /2) {
- dx = dx * -1;
- }
- // reverse direction of rotation to left of the midline
- if ( x < getWidth() /2) {
- dy = dy * -1;
- }
- mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;
- requestRender();
- }
- mPreviousX = x;
- mPreviousY = y;
- return true;
- }
- }
Add Comment
Please, Sign In to add comment