Sample Code for Open GL in Android
Rabu, 18 Juli 2012
0
komentar
Sample Code for Open GL in Android
Now your basic Open GL ES concepts are clear as written in the previous post (http://creativeandroidapps.blogspot.in/2012/07/basics-of-opengl-in-android.html) you are ready to create something with OpenGL on Android in your creative application.
This post is explaining a very popular sample for the Open GL for the new developers to quickly understand the implementation of Open GL functions and classes. For a basic Open GL project you need to create a activity class where you host your GLSurfaceView. The render something on the view we need to implement the GLSurfaceView.Renderer interface.
See here the code of my sample class.
Create a Activity Class
package com.example.sampleopengl;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private MyGLSurfaceView mGLView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new MyGLSurfaceView(this,null);
setContentView(mGLView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
See the highlighted code which you need to add in your activity class after implementing the following two classes.
Now create a class named MyGLSurfaceView which extends the GLSurfaceView to set as the view of activity.
package com.example.sampleopengl;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class MyGLSurfaceView extends GLSurfaceView {
MyGLRenderer mRenderer;
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mRenderer = new MyGLRenderer();
setRenderer(mRenderer);
}
//change the color on touch
public boolean onTouchEvent(final MotionEvent event) {
queueEvent(new Runnable(){
public void run() {
mRenderer.setColor(event.getX() / getWidth(),
event.getY() / getHeight(), 1.0f);
}});
return true;
}
}
See the implementation of the Renderer which will render the color.
package com.example.sampleopengl;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
public class MyGLRenderer implements GLSurfaceView.Renderer {
private float mRed;
private float mGreen;
private float mBlue;
public void setColor(float r, float g, float b) {
mRed = r;
mGreen = g;
mBlue = b;
}
@Override //function draw the opengl graphics
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glClearColor(mRed, mGreen, mBlue, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {
// TODO Auto-generated method stub
gl.glViewport(0, 0, w, h);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
// TODO Auto-generated method stub
}
}
Now you can run the sample code and see the following result as your first sample of Open GL on android.
Open GL - Color 1 |
On touch you will see a color change on the Open GL surface view created in activity.
Open GL - Color 2 |
If you like the post please provide your feedback.
Thanks
Creative Android Apps
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Sample Code for Open GL in Android
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke http://apk-xda.blogspot.com/2012/07/sample-code-for-open-gl-in-android.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar