by Per-Erik Bergman - Android, Embedded, Java
I have started a new updated serie of tutorials on OpenGL ES 2.0 for android. Check them out at: OpenGL ES 2.0
I’m going to write a couple of tutorials on using OpenGL ES on Android phones. The theory of OpenGL ES is the same on different devices so it should be quite easy to convert them to another platform.
I can’t always remember where I found particular info so I might not always be able to give you the right reference. If you feel that I have borrowed stuff from you but have forgotten to add you as a reference, please e-mail me.
In the code examples I will have two different links for each function. The actual function will be linked to the android documentation and after that I will also link the OpenGL documentations. Like this:
|
1 |
gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glClearColor(float, float, float, float)" style="text-decoration: underline">glClearColor</a>(0.0f, 0.0f, 0.0f, 0.5f); // <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glClearColor.xml" style="text-decoration: underline">OpenGL docs.</a> |
So, let’s start.
In this tutorial I will show you how to set up your OpenGL ES view that’s always a good place to start.
Setting up a OpenGL view has never been hard and on Android it is still easy. There really are only two things you need to get started.
GLSurfaceView is a API class in Android 1.5 that helps you write OpenGL ES applications.
If you want to get going fast with your OpenGL ES application this is where you should start.
The only function you need to call on is:
|
1 |
public void setRenderer(GLSurfaceView.Renderer renderer) |
Read more at: GLSurfaceView
GLSurfaceView.Renderer is a generic render interface. In your implementation of this renderer you should put all your calls to render a frame.
There are three functions to implement:
|
1 2 3 4 5 6 7 8 |
// Called when the surface is created or recreated. public void onSurfaceCreated(GL10 gl, EGLConfig config) // Called to draw the current frame. public void onDrawFrame(GL10 gl) // Called when the surface changed size. public void onSurfaceChanged(GL10 gl, int width, int height) |
Here it’s a good thing to setup things that you don’t change so often in the rendering cycle. Stuff like what color to clear the screen with, enabling z-buffer and so on.
Here is where the actual drawing take place.
If your device supports flipping between landscape and portrait you will get a call to this function when it happens. What you do here is setting upp the new ratio.
Read more at: GLSurfaceView.Renderer
First we create our activity, we keep it clean and simple.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package se.jayway.opengl.tutorial; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; public class TutorialPartI extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLSurfaceView view = new GLSurfaceView(this); view.setRenderer(new OpenGLRenderer()); setContentView(view); } } |
Our renderer takes little bit more work to setup, look at it and I will explain the code a bit more.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package se.jayway.opengl.tutorial; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; public class OpenGLRenderer implements Renderer { /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax. * microedition.khronos.opengles.GL10, javax.microedition.khronos. * egl.EGLConfig) */ public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Set the background color to black ( rgba ). gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glClearColor(float, float, float, float)" style="text-decoration: underline">glClearColor</a>(0.0f, 0.0f, 0.0f, 0.5f); // <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glClearColor.xml" style="text-decoration: underline">OpenGL docs.</a> // Enable Smooth Shading, default not really needed. gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glShadeModel(int)" style="text-decoration: underline">glShadeModel</a>(GL10.GL_SMOOTH);// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glShadeModel.xml" style="text-decoration: underline">OpenGL docs.</a> // Depth buffer setup. gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glClearDepthf(float)" style="text-decoration: underline">glClearDepthf</a>(1.0f);// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glClearDepth.xml" style="text-decoration: underline">OpenGL docs.</a> // Enables depth testing. gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glEnable(int)" style="text-decoration: underline">glEnable</a>(GL10.GL_DEPTH_TEST);// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glEnable.xml" style="text-decoration: underline">OpenGL docs.</a> // The type of depth testing to do. gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glDepthFunc(int)" style="text-decoration: underline">glDepthFunc</a>(GL10.GL_LEQUAL);// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glDepthFunc.xml" style="text-decoration: underline">OpenGL docs.</a> // Really nice perspective calculations. gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glHint(int, int)" style="text-decoration: underline">glHint</a>(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glHint.xml" style="text-decoration: underline">OpenGL docs.</a> GL10.GL_NICEST); } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax. * microedition.khronos.opengles.GL10) */ public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glClear(int)" style="text-decoration: underline">glClear</a>(GL10.GL_COLOR_BUFFER_BIT | // <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glClear.xml" style="text-decoration: underline">OpenGL docs.</a> GL10.GL_DEPTH_BUFFER_BIT); } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax. * microedition.khronos.opengles.GL10, int, int) */ public void onSurfaceChanged(GL10 gl, int width, int height) { // Sets the current view port to the new size. gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glViewport(int, int, int, int)" style="text-decoration: underline">glViewport</a>(0, 0, width, height);// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glViewport.xml" style="text-decoration: underline">OpenGL docs.</a> // Select the projection matrix gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glMatrixMode(int)" style="text-decoration: underline">glMatrixMode</a>(GL10.GL_PROJECTION);// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glMatrixMode.xml" style="text-decoration: underline">OpenGL docs.</a> // Reset the projection matrix gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glLoadIdentity()" style="text-decoration: underline">glLoadIdentity</a>();// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glLoadIdentity.xml" style="text-decoration: underline">OpenGL docs.</a> // Calculate the aspect ratio of the window GLU.<a href="http://developer.android.com/reference/android/opengl/GLU.html#gluPerspective(javax.microedition.khronos.opengles.GL10, float, float, float, float)" style="text-decoration: underline">gluPerspective</a>(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f); // Select the modelview matrix gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glMatrixMode(int)" style="text-decoration: underline">glMatrixMode</a>(GL10.GL_MODELVIEW);// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glMatrixMode.xml" style="text-decoration: underline">OpenGL docs.</a> // Reset the modelview matrix gl.<a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html#glLoadIdentity()" style="text-decoration: underline">glLoadIdentity</a>();// <a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/glLoadIdentity.xml" style="text-decoration: underline">OpenGL docs.</a> } } |
Just add this lines in the OpenGLDemo class and you will get fullscreen.
|
1 2 3 4 5 6 7 |
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.<a href="http://developer.android.com/reference/android/app/Activity.html#requestWindowFeature(int)" style="text-decoration: underline">requestWindowFeature</a>(Window.FEATURE_NO_TITLE); // (NEW) getWindow().<a href="http://developer.android.com/reference/android/view/Window.html#setFlags(int, int)" style="text-decoration: underline">setFlags</a>(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW) ... // Previous code. } |
This is pretty much all you need to get your view up and running. If you compile and run it you will see a nice black screen.
The info used in this tutorial is collected from:
Android Developers
OpenGL ES 1.1 Reference Pages
You can download the source for this tutorial here: Tutorial_Part_I.zip
You can also checkout the code from: code.google.com
Next tutorial: OpenGL ES Tutorial for Android – Part II – Building a polygon
I’m an undergraduate student. i am studying opengl for last 2 months. i have to do an academic project on android opengl. i’v 6 months more to do that. Could you please help me giving me some idea which i can choose for project?
[...] OpenGL ES Tutorial for Android – Part I – Setting up the view Referential Integrity using Spring LDAP [...]
Thanks for the tutorial. Can you please this code works for android 2.3
To the point Example… very easy to understand..
Thanks for any other wonderful article. Where else may anyone get that kind of info in such a perfect manner of writing? I have a presentation next week, and I’m at the search for such info.
Thank you for sharing your document
Is it possible to check this code on Android Emulator,Is android emulators supports Opengl , what type of extra effort should i do for that . thankou
tutorial part two doesn’t work. webpage doesn’t open.
http://www.jayway.com/2009/12/04/opengl-es-tutorial-for-android–part-ii-building-a-polygon/
here you have correct link
[...] ES 2.0 Reference Pages GLES20 Tutorial Reference: GLSurfaceView Reference: GLSurfaceView.Renderer OpenGL ES tutorial from FAU student Share this:TwitterFacebookLike this:LikeBe the first to like this [...]
Hi,
GL according to me stands for ” Graphics Library”
Hello,
I am very new for OPENGL.. can you tell me what is gl stands for in each line?
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // OpenGL docs.
// Enable Smooth Shading, default not really needed.
gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs.
// Depth buffer setup.
gl.glClearDepthf(1.0f);// OpenGL docs.
// Enables depth testing.
gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs.
// The type of depth testing to do.
gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs.
// Really nice perspective calculations.
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs.
GL10.GL_NICEST);
I just wanted to thank you for creating this OpenGL ES series. I have found it quite helpful. Please do keep up the great work!
Nice work.. keep it up sir.. ^_^
[...] OpenGL ES Tutorial for Android – Part I – Setting up the view [...]
Thanks for starting this tutorial series…….
thx
How come you don’t extend GLSurfaceView and thus allowing for the @Override of onTouchEvent? Is there another method of calling onTouchEvent?
Hello.
thanks to your tutorial I could publish my first game.
the market is the link:
https://market.android.com/details?id=mbm.main.ReallyPuzzle&feature=search_result#?t=W251bGwsMSwxLDEsIm1ibS5tYWluLlJlYWxseVB1enpsZSJd
happens to have a tutorial on pixel shader on android??
hug.
Log.i(“For your information!”, “Good work :) Thanks”);
[...] (function() { var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0]; s.type = 'text/javascript'; s.async = true; s.src = 'http://widgets.digg.com/buttons.js'; s1.parentNode.insertBefore(s, s1); })(); TweetThis tutorial is original posted at Jayway’s Developer Blog and can be found here: Jayway’s Developer Blog [...]
Hi Per-Erik Bergman,
My name is Dien Trinh. Firstly, forgive me for my bad English. I am developing an App for reading ebook and using harism’s Curl effect codes and I want when the user switches curled page to left or to right a continue picture will display on left or right side instead of current picture on curled page. Example, on the screen are displaying picture 1 on left and picture 2 on right. When I switches curled page to left the screen will display picture 3 and picture 4 instead of pic 2 and pic 3. Can you help me solve the problem or any instruction? Here is the link of my source code. http://www.4shared.com/file/vyLa1szE/curlEffectAdroid.html?
Thanks and Best Regards
I just finish class on graphics. Your tutorial were very help full as I was learning.
Just want say thank you.
Sir, i am trying to draw line by using glsurfaceview when user touches the screen. But i got window coordinates. I want them as glsurfaceview(opengl) coordinate. please help me
[...] Tutorial de OpenGL: Buenísimo tutorial donde se explica como iniciarse en el OpenGL, y en especial para Android. En JayWay nos enseñan desde iniciar la vista OpenGL, hasta aplicar texturas o definir movimientos. GA_googleAddAttr("AdOpt", "1"); GA_googleAddAttr("Origin", "other"); GA_googleAddAttr("theme_bg", "f0f0f0"); GA_googleAddAttr("theme_border", "cccccc"); GA_googleAddAttr("theme_text", "555555"); GA_googleAddAttr("theme_link", "008DCF"); GA_googleAddAttr("theme_url", "008DCF"); GA_googleAddAttr("LangId", "19"); GA_googleAddAttr("Tag", "tutoriales"); GA_googleAddAttr("Tag", "opengl"); GA_googleAddAttr("Tag", "sdk"); GA_googleAddAttr("Tag", "sensores"); GA_googleAddAttr("Tag", "tutorial"); GA_googleFillSlot("wpcom_sharethrough"); Comparte! [...]
thank you so much sir for you accurate tutorials. Very help full
Awesome, this is an excellent tutorial, thanks!
Thank you very much, i wasn’t found tutorial for opengl within a month!
[...] http://www.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/ [...]
Thank you! You are one of the best source i found.
Congratulations.
Thank you very much for supplied tutorials. Much obliged :)
Good job.
[...] http://www.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/ [...]
[...] Open GL ES Programming on Android [...]
Another much easier way to set your application to fullscreen ist by adding 1 line to the AndroidManifest.xml. Just add android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” to the application-tag in the xml file.
Hey, really really really great tutorial!! it`s very simple, i`m starting with opengl and android and its helps me a lot to dont missup with other stuffs. The links on the documentation also helps a lot on the learning process. I`ll continue with the other tutorials you`ve already posted. Thanks a lot!
This was the excellent tutorial.i am having few doubts regarding the fallowing.
How do I bind different textures to to different faces of cube?
coud u please clarify it.
Thanks this is really great.
But I would also like to see a similar example set for NDK.
I was at google I/0 and some of the stuff that they showed on NDK was awesome.
thanxs Per-Erik Bergman…
its work..
but i still really confuse to make graphics for AR using this ..any tutorial for make it ? please ??
thanks.
Hi,
Sorry..i’m new in OpenGL ES..i just wondering, why do we need to include GL10?
And, I found that its quite difficult to look for OpenGL ES in Android in the internet but tons in iPhone…
Hi,
Is it possible to modify the draw method of an object in such a way that it only needs to be drawn once and not every time in the onDraw method? I am having performance issues while drawing lots of pixels for a background image and wondered if the draw method could be changed?
Excellent!
Great tutorial!
How do I bind different textures to to different faces of cube?
I’m trying something like this
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glClientActiveTexture(GL10.GL_TEXTURE0);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
….
gl.glActiveTexture(GL10.GL_TEXTURE5);
gl.glClientActiveTexture(GL10.GL_TEXTURE5);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[5]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glTexEnvx(GL10.GL_TEXTURE_ENV , GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
Thanx you are very nice :)
[...] Часть 1 – Введение [...]
As an openGL programmer on the PC, this was all i needed to get me started on android. However, i’d prefer using C++ over java, so that will be a next thing to figure out on android. But atleast i got started now. Thanks for the info
Nate: You can use en GLSurfaceView as any view so with a frame layout I guess you can add it as a background.
mural: On android we get an .apk file.
shubham: Rendering 2D with OpenGL is a big tutorial itself, it is in the pipeline.
Pach: GL_PROJECTION and GL_MODELVIEW is two different matrix and since we need to “reset” them both we need to do glLoadIdentity on both.
John: The easiest way is to create a camera object that you can steer. When rendering do the camera translation first before you render the square.
[...] ← OpenGL ES Tutorial for Android – Part I – Setting up the view [...]
Hi,
I have followed this tutorial and hve also got a square to appear on screen. I am able to move the square by using up down left and right keys. My question is how can I get a cameraq to “follow” this square when it moves? ie I want the camera to be focused on the square.
[...] Per-Erik Bergman 关于Android下OpenGL ES 的系列教程,图文并茂。 http://www.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/ [...]
dear sir, I do have a question regarding this codes here
// Sets the current view port to the new size.
gl.glViewport(0, 0, width, height);// OpenGL docs.
// Select the projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.
// Reset the projection matrix
gl.glLoadIdentity();// OpenGL docs.
// Calculate the aspect ratio of the window
GLU.gluPerspective(gl, 45.0f,
(float) width / (float) height,
0.1f, 100.0f);
// Select the modelview matrix
gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs.
// Reset the modelview matrix
gl.glLoadIdentity();// OpenGL docs.
why do we have to change the glMatrixMode to GL10.GL_MODELVIEW? and reload the glLoadIdentity?
I hope I would here from your guidance since I’m new to OPENGL and android…
[...] 参考にしたのはこちら「OpenGL ES Tutorial for Android – Part I – Setting up the view」 [...]
Super!!!.
Good Work. Keep it up.
i am facing some error:
[2010-11-20 15:31:55 - Emulator] invalid command-line parameter: partition-size.
[2010-11-20 15:31:55 - Emulator] Hint: use ‘@foo’ to launch a virtual device named ‘foo’.
[2010-11-20 15:31:55 - Emulator] please use -help for more information
*to enable full screen
excellent to get started nice and easy :)
just a note, you also need to include two classes for enable full screen:
import android.view.Window;
import android.view.WindowManager;
great work on the writing, links and selecting the examples. jumping on part II now.
PS: this should be linked from the android manual, where they say it’s not the scope of the glsurfaceview to explain how to do openGL helloworld! think that if you used more androidesque syntax they would consider it :) …just sugar, like @Override, etc
Really appreciate your tutorials, thanks!
Thank u..
thanks
its really nice to start opengl in eclipse
if possible please provide the code for drawing arc in opengl using java
really nice tutorial and Im gonna add ur blog in my blogroll!!
Great tutorial…….
Clear and easy to understand……….
Very good, ThX
Too Good Blog.
Very Clear and Informative.
Thanks for availing this information
By the way thought it could be helpful for anyone since black is the same as the default color. Try this instead:
// Set the background color to RED( rgba ).
gl.glClearColor(255.0f, 0.0f, 0.0f, 0.5f);
Awesome tutorial. Thank you very much.
It’s very helpful. Thanks!
hi friend’s ,I want to draw an 2d image ..how can i do here.
I got pixel position of image ..
Pls reply
Thank in Advance..
Thanx Bro..it’s realy good try.
thnx
Your article is very helpful. Thanks
My doubt is opengl return a .exe file as output in android, similarly in what format the o/p file will be in Android ? May be my question is very silly.
Thanks in advance.
[...] posted at: Jayway Team Blog by Per-Erik [...]
[...] tutorial was all about setting up the GLSurfaceView. Be sure to read it beacuse it’s a really importent one to be able to [...]
any way to use a GLSurfaceView as a background? the idea is to have a normal surfaceview on top of the GLSurfaceView, render the background with opengl, then throw some sprites onto the other view. so as the opengl is just handling the background. I currently have this working fine, except i cannot get the GLSurfaceView to stay in the background.. no amount of view manipulation ie. what order they are in etc, seems to help.
[...] the first article. There’s a link to the next one at the bottom of the first one. http://www.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/ Posted by psinke Filed in Uncategorized Leave a Comment [...]
Thank you for the tutorial, alas some inspiration to start of OpenGL ES :)
Dear Sir, really good articles to Android OpenGL ES!
Thanks for ur sharing!
Hi Per-Erik
Thank you for your tutorial.But My application is not work for a error.It is GLLogger, it show couldn’t load library(Cannot find library).
I don’t know what’s wrong.
Thanks, this is extremely helpful.
I believe there’s a typo on line 220:
s/OpenGLDemo/TutorialPartI
thanks for starting this tutorial series on OpenGL for Android!