NeHe Android Ports
As Android supports OpenGL ES, I wanted to provide the basic and probably most read OpenGL tutorials available for the Android platform. The NeHe OpenGL tutorials are what every OpenGL starter is beginning with or pointed at. The set of nearly 50 tutorials give an overview about OpenGL functions and principles and are easy to understand and to be converted to the own needs. They are already available in many versions, many different programming languages (e.g. C#, D, Java) as well as for many frameworks (e.g. JOGL, REALBasic).
I ported/am porting the original tutorials to the Android platform, adapted to the specifics and requirements and in a somehow more Object-Oriented way. Some specifics and requirements given from the OpenGL ES Android implementation require changes such as
- glBegin/glEnd is a common way to immediately draw something to the OpenGL screen and permanently used in the NeHe tutorials. But as these are not supported on the Android platform, everything will be handled by Arrays (Vertex, Indices, Colors, Textures Coordinates). Anyway, we are still immediately drawing these, but for future tasks and buffer binding on GL11 supporting devices, this is the basic way to go, therefore the change should be seen as an advantage (Anyway, glBegin and End will drop out of the OpenGL Specification).
- GL_QUADS are not available. Therefore, in correlation with the used Vertex and Index Arrays most things will be drawn with GL_TRIANGLES, which again is no direct disadvantage.
and some more things. Overall, I try to keep everything as near as possible to the NeHe source and comments, so that everybody can use these sources with the description of the original tutorials. Additional elements and structures added for a better utilization, visibility, style and organisation are commented by myself here on this page as well as in the source code itself. If questions occur, please comment to this page.
Please note that the source is a mere port and is not necessarily best practice or for best performance. It is just a plain port to show how to accomplish the original intend on the Android platform. This source has been developed and tested with the Android 1.5r3 SDK in an Eclipse 3.5 environment. All ran fine on the provided emulator.
Not every little aspect of Android or Java is explained. You should now how the basics work to understand the lessons.
In this very first tutorial you will get a quick overview where to get all you need (of course, depending on your personal preferences you have to adapt the tutorial) to start developing Android applications.
In this first OpenGL tutorial you will see the basic structure to initiate an OpenGL View on an Android system. It contains four classes: One Runner, to base the activity on (will be nearly the same for each lesson), the OpenGL View and the two objects which are drawn. Both objects are properly separated but resemble the same structure to be drawn by vertex buffers. As mentioned, this should be the preferred way and could also be used as an initial idea for a later game entity design structure.
Note: As JoshuaFalken pointed out it is to note that by creating the GLSurfaceView and setting the renderer the drawing process is continously. This means you do not have to make any loop yourself. While in most cases this is what you want, to draw all the time. If you need or want to do it on your own you can set the render mode to RENDERMODE_WHEN_DIRTY. By that you would have to requestRender on the GLSurfaceView by yourself. You can read about it here.
Changes regarding NeHe: The biggest changes to the original tutorial come from the Android platform and classes structure. Everything is separated into its own classes (Renderer, Triangle and Square), as well as the objects are drawn by Vertex Buffers (see remark above). The init, resize and draw methods are nearly the same but they refer to the respective instanced objects of the Triangle and Square. In addition to fit the normal up-side screen layout I changed the location of both to be among each other.
In this lesson you add colors to the objects. The triangle gets colored with with a gradient and the Square is colored in blue.
Changes to previous: The changes are only made in both object classes, Triangle and Square. In Sqaure.java only one line has been added to the draw method, which defines the color before drawing. The Triangle.java does not only draw one color but many. These are again defined by values in a buffer like the vertices. The values are based on RGB and luminance. In the draw method this buffer is also added like the vertex buffer and the mapped colors are drawn according to their order.
Now that we have colored Triangles and Squares we need to spin these round n’ round. Whoohi^^’!!! In this tutorial you will add rotation to both objects.
Changes to previous: Only the method onDrawFrame(GL10 gl) in Lesson04.java has been altered. Before we draw the Triangle and Square we rotate the drawing matrix. A dynamic value, which is increased in each drawing round ensures that our objects start spinning. Between both drawing procedures we reset the matrix by calling gl.glLoadIdentity(). This is very important, otherwise the following translation and rotation is based on the position set through the previous translation and rotation. Just try and comment that line. Funny results ^^°!
In this tutorial you will enter another dimension: The THIRD! based on our Triangle and Square you will create the three-dimensional representation of these. Therefore, we change the Triangle to a Pyramid and the Square to a Cube.
Changes to previous: The biggest changes are done to our objects. We converted and extended both to three-dimensional representations. If you have a look at the Cube and Pyramid class you will notice that they resemble the structure of the Square and Triangle. We just added additional vertices and indices to the Cube. It is important to notice the difference between e.g. Square and Cube to understand how to draw and how to create objects. The Cube uses as mentioned indices to gl.glDrawElements itself. To understand the indices it is important to understand a cube. A cube has 8 vertices, 12 edges and 6 faces. All faces share different vertices and this is what we want to use to our advantage. If we draw a cube we would have to put every vertex in the array and so that the drawing function can follow the vertices to conclude in surfaces. Now, we just define the 8 vertices a cube has overall and put them in “drawing order” through our indices. The values in the index buffer are to be seen as pointer to the defined vertices. 0 for the first, 1 for the second and so on.
Note: Please note that the Pyramid has no “base”. In the original NeHe tutorial this was not needed because of the perspective. But as I altered the code to fit the normal screen layout you can see into the Pyramid. I was too lazy to add the floor but you could add it to test and see if you understood the lesson.
In this tutorial we will concentrate and continue with the Cube. We step away from the psychedelic colors and add a texture. In honour of the original tutorials I keep the original texture.
Changes to previous: Some small changes are in Lesson06.java where there were some variables added, 2D textures are enabled (gl.glEnable(GL10.GL_TEXTURE_2D)) and the texture is loaded in the Cube class. The biggest change experience the Cube.java. First of all the color array was replaced by the texture array, which contains the texture mapping coordinates. Therefore, instead of the color array the texture array is enabled in the draw method (gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY)). You will notice much more vertices than before. This results from the fact that we do not want to color them but to texture them. Now, if we use vertices for more than one face the texture mapping becomes inconsistent. Therefore, each “real” face vertex has been defined and the according texture mapping with one mapping per vertex. There are possible cases where texture mapping with shared vertices can work but not with this cube. The biggest extension to the Cube class is the loadGLTexture method. It combines the OpenGL texture generation with the Android resource loading possibilities and shows best the advantages and possibilities Android can provide for such a development.
In this tutorial we change our texture, add texture filters, lighting AND controls.
Changes to previous: Several changes have been made to the Run, Cube and Lesson07 classes. The Lesson07.java now defines light arrays for ambient, diffuse and position values and converts these into the buffers in the constructor. What can be noticed here also is that the constructor sets itself as renderer, what was done before in the Run class. If you look up in the class you will see that I changed the class from a Renderer to a full GLSurfaceView, Renderer and some InputListener. The Run class now only instances the Lesson07 and sets it as content view. I have changed this because of the input functionality in this lesson. It is quicker and easier to implement a change of values in the same class these are used and drawn. It is no best practice as you should really use a real input manager for yourself, but to keep it simple to understand and near to the original I chose this way. When the surface is created a OpenGL light is created. This can be enabled through input and a flag queried in the draw method. The Cube class is nearly the same but it got another array (yes, another one): Normals! Please refer to the Wikipedia entry for further information about normals. We need them, otherwise OpenGL would not know how to work with the light if it hits the surface. The buffer is enabled and added as the other ones. Everything common here. But the Texture loading method has changed a bit as different filters are now created: Nearest, Linear and Mipmapped. Nearest and Linear are just different in flags but the mipmapping needs some changes. Please refer to this post of me, where I described it in more detail. But overall the probably biggest change to the NeHe tutorial are the controls.
Controls: As first lesson this one has controls to control what should happen on the screen. The emulator has a keyboard but I wanted to also use this lesson to give away some InputHandler basics. Therefore, I converted all controls of the original tutorial to the Touchscreen and D+Pad. The controls are:
- D+Pad: Acceleration in the respective direction
- D+Pad Button: Change the filter
- Touchscreen: Control the Box by spinning it according to your movement on the touchscreen
- Upper Touchscreen: From left to right you can zoom in and out by moving on the screen
- Lower Touchscreen: By clicking in the lower part you can enable and disable the lighting
The implementation is done in the Lesson07 class as mentioned. It is pretty straight forward if you know the listener principles in Java. We catch key and touch events and interpret these to set the according variables in our code (that is why I wanted to have Renderer and Listener in one class). It should be self-explanatory, but a very important point is done in the constructor. By setting it focusable in touchscreen mode and requesting the focus the D+Pad is catched for that Activity. Always ensure this.
In this tutorial we again change the texture and enable blending for the textures.
Changes to previous: There are not that many changes to this lesson as it only sets and enables blending in the Lesson08 class with the according flags and method to blend in-between textures.
Controls: The controls are the same as in the previous lesson but the lower touchscreen has been split into left and right
- Lower Touchscreen LEFT: By clicking you can enable and disable the blending
- Lower Touchscreen RIGHT: By clicking you can enable and disable the lighting
This tutorial slightly leaves the previous tutorials and shows how to move and blend textured objects in our 3D space.
Changes to previous: Basically, you will find much same code as before, as I kept the basic structure to have everything as clear as possible. Two bigger changes have been made: Now, there is a Stars and a Star class. The Star.java is basically the textured object class representation of one single star. It has the vertices and texture coordinates, handles to draw itself. The Stars class is instanced and called by our renderer and initiates the star texture once, sets random colors and iterates through all stars. This can show how a possible “world” representation could call its sub-objects. In the Lesson09 class you can change the number of stars or for example change glBlendFunc to see which effects occur. This may help to understand blending.
Controls: The controls are just:
- Lower Touchscreen LEFT: By clicking you can enable and disable the blending (just looks funny ^^)
- Lower Touchscreen RIGHT: By clicking you can enable and disable twinkling
This tutorial is different from the ones before as it does not only show a geometric object but allows you to move through a 3D world.
Changes: The changes here are basically additions. The drawing and rendering is pretty similar to the NeHe Android Port structure before. But this time, we do not define the vertices etc. by hand, but read these from a text file. Therefore, the World class is a representation of a World, based on what has been read from the world.txt file. The biggest additions are made here, in the World class. The text file is interpreted and the controls are moved here. The controls are similar to the original but converted to the Android platform.
Controls: The controls are moving by pressing the D-Pad or your arrow keys on the keyboard. In addition you can look up and down and turn around by scrolling on the touchscreen. The texture filter can be changed with the D-Pad Center button and by pressing
- Lower Touchscreen LEFT: By clicking you can enable and disable the blending (just looks funny ^^)
This tutorial is based on tutorial 07 and adds fog.
Changes to lesson 07/08: The changes to Lesson 07/08 are minimal and are just in the Lesson16 class. In the beginning we define specific fog color, mode and filter variables. In onSurfaceCreated() we set the OpenGL Fog settings and in draw check which fog mode filter should be used. The listener onTouchEvent() now checks for presses in the lower left and changes the fog mode accordingly.
Controls: The controls are the same as in Lesson 07/08 but
- Lower Touchscreen LEFT: By clicking you can switch the fog mode
…more to come…
I hope these ports can help at least some people trying to figure out the platform. If you like it, have questions, are mad at me or you have ideas for improvement, please let me know. The comments are open!
First I want to say thanks for the tutorials, they are extremely helpful!
But now for some errata:
In your tutorials, you refer to the 4th float of each vertex in the color buffers as ‘luminosity’. It’s actually alpha, which refers to transparency. Changing that vertex won’t make it any darker or brighter.
@Thomas Dignan
at the end there I meant value, not vertex.
Hi
Thanks for the hint. You are absolutely right that it is the alpha. Maybe in my “german foolishness” I just wanted to sound cooler because alpha is maybe not directly understood, but transparency would be the better alternative.
Thanks for that
Cheers
I LOVE YOU!
Would be great to see some tutorial about loading some 3D files/scenes etc. But any new tutorial will be very appreciated. Thanks for these
LadTy
I love you lol. As he said. On my way to making my first game.. it might be 3D
This is so cool..i have been looking for android opengl es code for newbies for days..
This really helped me out! My sincere thanks!
This really helped me out! thanks a lot!
Thanks for converting these tutorials to Android. That is quite an accomplishment! I wonder why the NeHe site does not include these alongside the other languages? I have been a big fan of the NeHe tutorials for years. When I first started Android development I looked your site, but I didn’t hover the mouse over the links to the Android code and instead when I clicked on the blue links they sent me to the NeHe site and I downloaded the java tutorials which didn’t work. Stupid me! For the last few days I’ve been banging my head against some of the crappy ApiDemos trying to get them to work and came across your site again and this time found the real code, thank heavens. Also, the names on the icons need to be shorter because the all say the same thing on my LG phone:
Nehe
Android Le…
Keep up the good work!
P.S: Also It would be awesome if anyone could convert NeHe’s lesson 36 to Android. That code really shows how bad OpenGL drivers have gotten lately…
Excellent tutorials, really good as a starting point in Android’s OpenGL ES programming.
Excellent opengl tutorial !!!
it is awesome tutorial, thanks for it
but i have a very deep problem in compressed texture usage,
can you help
Hi Sam
Dont know if I can help. You first have to state what your issue is.
Regards
Hi NeA
Thank you very much for you reply
i publish the problem here http://stackoverflow.com/quest.....come-white
Please if you can help
Thanks a lot! Really.
Hi. Thanks for the tutorial. However, when I am running the project files all I get is an android emulator with ‘Android_’ splashed on the virtual screen. There’s no graphics element displayed. No error report has been delivered by eclipse. Can you suggest a solution? Thanks in advance.
thank`s for lessons archives
Hello sir,
can you please tell me how can i change the texture image dynamically?
Thanks in advance,
sandesh
Many thanks,
This is really useful, haven’t been over the NeHe examples in a few years and this was just what i was looking for.
Thanks for Sharing
Jim
Hi, thanks for the great tutorials. in the examples for the phone the cubes continue to rotate a bit after the user releases the touchpad. What method was used to simulate this? Any chance of a quick example. Thanks
@Ed
Since the “onDrawFrame” method is continuously being called so is:
rtri += 0.2f;
rquad -= 0.15f;
Since these are in the rotations:
gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
The cube is constantly rotating. See source.
??????????????
wodetianna
Thanks for these. Very helpful.
i myself use the mosync for programming opengl for android..
do you have any problems with the render to texture example?
i have tried to make my own but i have huge problems when drawing back the blurbuffer
to the screen.. there is a lot of weird errors in the texture..
it is supposed to be 32bit on my.Samsung Galaxy S2..
On my old Samsung Galaxy S i have no errors..
Anyone have any ideas what it might be?
Hi!
I see ths tutorial is written for OpenGL ES 1.1.
I’m just getting into Android OpenGL development and I can not find the differences between OpenGL ES 1.1 or 2.0.
Which one is better? Which one is worst? In 2.0 exists shaders which i don’t understand and drive me nuts…
Maybe i can try to undestand this tutorials and then adapt them to OPENGL 2.0….
Sincerely i don’t know what to do at all….
Hi Sergio
In general I wrote them for OpenGL ES 1.1 because at that time it was the Android State-of-the-Art (not even JIT ^^’). In general ES1.1 and ES2.0 are totally different. 2.0 follows a different approach (the shader approach as you noted). Besides that it has several additional functions and options not even available or emulated in 1.1. You can pretty much say 1.1 is old and becoming outdated and 2.0 is for the future as it also is more oriented to be similar/compatible with the “normal” OpenGL branch and development.
I want to add some NeHe ports based on OpenGL ES 2.0 but I am totally out of time at the moment.
But in general I would support maybe for you if are totally new to get into these tutorials and then try to port these or even try other things. A good start would also be to have a look at iPhone tutorials as these support ES2.0 for a longer time already.
I know it may seem a little complicated because it is a pretty big paradigm shift, but if you want to continue in that direction you have to adept ES2.0.
Cheers
I have been waiting for these NeHe tutorials on android platform.Thanks a million….
u r great! thanx alot .
but i m finding a code to draw a sphare or torus king of designs.if u can show some code for tht too,ll be even greater.
This is going to sound really dumb, but how do I open these projects? I can create my own android projects from scratch, but don’t know how to open these once unzipped. thanks
ok ignore last post, managed to open it (In eclipse go to File->Import. Select ‘Existing projects into Workspace’ under the general tab and press next. Then Select Archive File and choose the zip file you downloaded.)
These are all nice tutorials, but if they build on Android 1.5, then they are not much help for OpenGL ES 2.0.
Now I realize it would be a lot of work to rewrite them in 2.0, so instead of asking for that, could you at least give us some nice guidelines for how to do the same things in 2.0? Perhaps with references to 2.0 sites and a list of hints on how to do the translation?
thanks for sharing, i will start android app development.
Can any one give an example on how to add a glow effect to any of these examples? any help would be much appreciated, thanks
Wow, as soon as I saw NeHe in google search I remembered the old tutorials I had read a few years ago. I’ve been wanting to learn android development. Thanks a ton.
NeHe is the best…
Your tutorial are very useful, thanks so much.
I expect your more tutorials in the future.
Come on!
Can you port Lesson11(Flag) too?
thanks a lot~ it really help~
thanks a lot~~
It is too useful to the beginners. if possible please provide some sample game tutorial..
It is too useful to Beginners, If possible please provide sample game tutorial.
ty very much. this is actually the first thing that really helps me.
the source code is awesome and understandable.
Why isn’t this up on the original NeHe Page ?
Thank you so much for this tutorial..!! I am beginner in openGL and I found it so helpful..
I´m wondering… Do you have an Augmented reality example on android.. Something for begginers…??
Hi, what’s the official way to use OpenGL for a live wallpaper?
Without using GLWallpaperService by Robert Green because its quite buggy
http://www.rbgrn.net/content/3.....wallpapers
thanx
Dude. I am so glad that google direct me to ur web page . it has solve a hug problem of mine working with Open Gl and was trying to port the openGL SDL on android but ur lesson 10 made it all.. Bless you
Um do you think u could add anons modles like people plz cause then its boring but still fun confusing huh oh and not bad ones I’m still 12 years old so no bad ones k