GLSurfaceView in android

It used for 2D and 3D Graphic animation , gaming and all those app which required 2D or 3D Graphic
Here we Simply Introduce skeleton of GLSurfaceView. Create New Project The Activity Class Look like the following
public class GLActivity extends Activity
 {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
               setContentView(R.Layout.main);
}

This is our main activity onCreate() method. Now we make out project GLSurfaceView based Application
So we Do this
public class GLActivity extends Activity
private GLSurfaceView glView;
 {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
               glView = new GLSurfaceView(this);
               glView.setRenderer(new GLRenderer());
               setContentView(glView)
}

Now We add the Renerer Class Which we used in GLSurfceView
class GLRenderer implements GLSurfaceView.Renderer {

public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
//First Time called.
}

public void onSurfaceChanged(GL10 gl, int w, int h)
{
gl.glViewport(0, 0, w, h);
}

public void onDrawFrame(GL10 gl)
{
// This method called Continuously... to update View
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}

Now We want to pause and Resume the GLSurfaceView When Activity is Change state;
// to pause View
glView.onPause();
// to Resume View
glView.onResume();

Complete Skeleton of GLSurfaceView
package com.example.android.apis.graphics;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class ClearActivity extends Activity {
private GLSurfaceView glView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glView = new GLSurfaceView(this);
glView.setRenderer(new GLRenderer());
setContentView(mGLView);
}

@Override
protected void onPause() {
super.onPause();
glView.onPause();
}

@Override
protected void onResume() {
super.onResume();
glView.onResume();
}


}

class GLRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Do nothing special.
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}

public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
}

Now your Simple GLSurfaceView are Completed
Now we add Touch Event to GLSurfaceView
package com.google.android.ClearTest;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

public class GLActivity extends Activity {
private MyGLSurfaceView glView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glView = new MyGLSurfaceView(this);
setContentView(glLView);
}

@Override
protected void onPause() {
super.onPause();
glView.onPause();
}

@Override
protected void onResume() {
super.onResume();
glView.onResume();
}


}

class MyGLSurfaceView extends GLSurfaceView {
GlRenderer glRenderer;
public MyGLSurfaceView(Context context) {
super(context);
glRenderer = GLRenderer();
setRenderer(glRenderer);
}

public boolean onTouchEvent(final MotionEvent event) {
queueEvent(new Runnable(){
public void run() {
glRenderer.setColor(event.getX() / getWidth(),
event.getY() / getHeight(), 1.0f);
}});
return true;
}


}

class GLRenderer implements GLSurfaceView.Renderer {
private float redColor;
private float greenColor;
private float blueColor;
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Do nothing special.
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}

public void onDrawFrame(GL10 gl) {
gl.glClearColor(redColor, greenColor, blueColor, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}

public void setColor(float r, float g, float b) {
redColor = r;
greenColor = g;
blueColor = b;
}


}
If you want to Debug OPenGL ES program use this befor setting render

setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);
glRenderer = new GLRenderer();

Post a Comment

0 Comments