Was working on a live wallpaper and I wanted to do something like I’ve done in the past when developing OpenGL for windows with floating spheres circling the camera – Sphere Demo – Unfortunately, my use of some glut features way back in 2004 are considered a crutch in OpenGL ES development. Anyway, because things like this are hard to find on the internet, this is how you can create a sphere in opengl-es!
FloatBuffer strip, texture; float radius; int stacks, slices; boolean wire; /** Our texture pointer */ private int[] textures = new int[1]; public Sphere(int stacks, int slices, float radius, boolean wire) { this.stacks = stacks; this.slices = slices; this.radius = radius; this.wire = wire; unitSphere(stacks, slices); } public void draw(GL10 gl) { //Draw out your triangle strips here } protected void unitSphere(int stacks, int slices) { float drho = (float)(Math.PI / stacks); float dtheta = (float)(2.0 * Math.PI / slices); // Calculate the triangle strip for the sphere body int triangleStripVertexCount = (slices + 1) * 2 * stacks; float[] stripVertices = new float[triangleStripVertexCount * 3]; float[] textureVertices = new float[20000]; int index = 0; int indexTex = 0; for (int i = 0; i < stacks; i++) { float rho = i * drho; for (int j = 0; j <= slices; j++) { float theta = (j == slices) ? 0.0f : j * dtheta; float x = (float)(this.radius * -Math.sin(theta) * Math.sin(rho)); float y = (float)(this.radius * Math.cos(theta) * Math.sin(rho)); float z = (float)(this.radius * Math.cos(rho)); // TODO: Implement texture mapping if texture used // TXTR_COORD(s, t); stripVertices[index++] = x; stripVertices[index++] = y; stripVertices[index++] = z; float s = (i + rho) / stacks; float t = (j) / slices; textureVertices[indexTex++] = s; textureVertices[indexTex++] = t; x = (float)(this.radius * -Math.sin(theta) * Math.sin(rho + drho)); y = (float)(this.radius * Math.cos(theta) * Math.sin(rho + drho)); z = (float)(this.radius * Math.cos(rho + drho)); float sx = (float)(-Math.sin(theta) * Math.sin(rho + drho)); float sy = (float)(Math.cos(theta) * Math.sin(rho + drho)); float sz = (float)(Math.cos(rho + drho)); // TODO: Implement texture mapping if texture used // TXTR_COORD(s, t); stripVertices[index++] = x; stripVertices[index++] = y; stripVertices[index++] = z; s = (i) / stacks; t = (j + rho + drho) / slices; textureVertices[indexTex++] = s; textureVertices[indexTex++] = t; } } ByteBuffer bb = ByteBuffer.allocateDirect(stripVertices.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer ib = bb.asFloatBuffer(); ib.put(stripVertices); ib.position(0); strip = ib; bb = ByteBuffer.allocateDirect(textureVertices.length * 4); bb.order(ByteOrder.nativeOrder()); ib = bb.asFloatBuffer(); ib.put(textureVertices); ib.position(0); texture = ib; }
Oh I was adding in texture support, but I got distracted and stopped working on it (since it wasn’t needed in this particular project and I keep getting delayed in my tower defense mission). It currently doesn’t work, soooo, what are you waiting for, go add in texture support…
Here use this to speed along the journey:
public void loadGLTexture(GL10 gl, Context context) { //Get the texture from the Android resource directory InputStream is = context.getResources().openRawResource(R.drawable.logo); Bitmap bitmap = null; try { //BitmapFactory is an Android graphics utility for images bitmap = BitmapFactory.decodeStream(is); } finally { //Always clear and close try { is.close(); is = null; } catch (IOException e) { } } //Generate there texture pointer gl.glGenTextures(1, textures, 0); //Create Linear Filtered Texture and bind it to texture gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); //Clean up bitmap.recycle(); }
Ok now just add a “logo.png” into your res folder* and build a textured sphere*
*Some assembly required.
In the previous tutorial, the code had the camera tracking to the player, while that may work as a sample for the TMX engine, we will want a little more “user” control for our purposes. So, I just thought I would add a block of code that, when combined with the downloaded code in the previous post, will allow you to move around the tile map. Now we’re free to look around at the map (which is important for a tower defense game) while the action continues in another location. So how do we achieve this amazing feat? Easy!
Just go ahead and remove the line:
this.mBoundChaseCamera.setChaseEntity(player);
and then plug the below code directly into the TMXAndEngineLesson.java file within the “public Scene onCreateScene()” method.
scene.setOnSceneTouchListener(new IOnSceneTouchListener() { private float mTouchX; private float mTouchY; private float mTouchOffsetX; private float mTouchOffsetY; @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pTouchEvent) { if (pTouchEvent.getAction() == MotionEvent.ACTION_DOWN) { mTouchX = pTouchEvent.getMotionEvent().getX(); mTouchY = pTouchEvent.getMotionEvent().getY(); } else if (pTouchEvent.getAction() == MotionEvent.ACTION_MOVE) { float newX = pTouchEvent.getMotionEvent().getX(); float newY = pTouchEvent.getMotionEvent().getY(); mTouchOffsetX = (newX - mTouchX) * 2; mTouchOffsetY = (newY - mTouchY) * 2; float newScrollX = mBoundChaseCamera.getCenterX() - mTouchOffsetX; float newScrollY = mBoundChaseCamera.getCenterY() - mTouchOffsetY; mBoundChaseCamera.setCenter(newScrollX, newScrollY); mTouchX = newX; mTouchY = newY; } return true; } });
[UPDATE: Just found a site that did the work of describing how to setup AndEngine with GLES 2.0 here - for those who have never setup AndEngine before this could be useful.]
It’s interesting to note (based on my experience) that there are far fewer game engines available for the Android as there are for the iPhone. Fortunately, while there aren’t as many options, the products themselves are very much comparable.
Currently, this week, I’ve been playing around with the AndEngine, Cocos2d for Android and the Libgdx. Each of them has their own strengths and weaknesses, but one thing I’ve noticed instantly is that there are steeeeeep learning curves just to begin development.
Since I’m putting together a tower defense tutorial for android, I’ve decided to just post a few samples (usually from the test suite) that each of the game engine are packed with.
To Start – below is an example of setting up the AndEngine (GLES 2.0) and displaying a simple TMX file and moving an NPC around the screen with camera tracking.
You’ll notice the first thing about this download… it’s 5 megs after I’ve attached all the required jar file (the author recommends though that you use the andengine as an android library not as a jar on the andengine forum.
If you decided to pull all the source down from Nicolas Gramlich git repository, you looking at about 44 megs of files and that’s not including the AndroidTestExamples files.
Even after downloading all those files you have to go through a hugely pain linking all the projects together – “Skaulius” who goes by “RealMayo” on the AndEngine Forum has put together a walk through that runs about 15 min for the setup. Now granted AndEngine has moved to support OpenGL ES 2.0+ and it was just started this past Dec ’11.
Anyway as I’ve said I’ve taken the liberty to just compile the jar files and post the simplest of Tile map examples…
[FINAL NOTE: Depending on whether you've updated your Emulator recently you may only be able to run this off your phone. See more about the issue and solving it here]
Android AndEngine TMX (Tile Map) Example - Source Download (90)What do you think, what’s your experience been with the andEngine? Have you played with the new GLES 2.0? Have you had issues getting the emulator to work?
So you have a great game and would like more people to know about it? Maybe you want to get a little more exposure about your mobile experience and skill or just want to give back to the android gaming community? Whatever the reason we can use your help!
How-To-Help: First, contact me at info (at) androidgametutorials (dot) com. Then start by building off an existing tutorial on the site or maybe come up with a new tutorial all your own! Make it as long or short as you want… Or just show off a segment of your compilable code. You get full credit for that tutorial and it will be displayed on androidgametutorials.com for all to see including a side bar of “Contributors” as well.
I don’t know what could be better for someone trying to show off their chops in the mobile game space or get a little more spotlight on the game they’ve made!
(more…)
Since 1.6 you’ve been able to use Gestures, but in a game it is that much more important to have the fundamentals down. From a match-3 mechanic to an angry birds game, gestures are a key user mechanic that is *required* to keeping your games playable. I can’t tell you how many games I’m stopped playing because some development shop didn’t have an easy enough gesture system, but it’s probably a lot…
To this end, I’m posting a simple example that will allow you to see all the potential gestures that you can do (besides custom gestures). Nothing complicated, but something I think is useful.
I’m showing you a screen shot, so you don’t think this is some huge download with loads of graphics… it’s not… I just thought I would break it out from the larger tutorial I am working on right now
USAGE: Just click on the screen and swipe your finger around.
Android Game Gestures - Source Download (72)
Nehe’s lessons 16 converted to an Android Live Wallpaper….
This Tutorial covers using OpenGL ES Fog with a Textured Cube…
[Editor: In an effort to get NeHe lesson 16 out for Android Live Wallpaper, I am going to forgo the formality of actually explaining the code and some of the code speaks for itself... If there is a desire for more information, just post a comment and I'll put the tutorial on my list.]
Download and Source Code after the break…
Nehe’s lessons 11 converted to an Android Live Wallpaper….
This Tutorial covers using OpenGL ES to manipulate the individual vertices. This is based on a “Flag” tutorial, that I thought was more interesting in a landscape view. Therefore, this specific tutorial is incomplete, but very useful…
[Editor: In an effort to get NeHe lesson 11 out for Android Live Wallpaper, I am going to forgo the formality of actually explaining the code and some of the code speaks for itself... If there is a desire for more information, just post a comment and I'll put the tutorial on my list.]
Download and Source Code after the break…
Nehe’s lessons 9 converted to an Android Live Wallpaper….
This Tutorial covers using OpenGL ES and particle effects…
[Editor: In an effort to get NeHe lesson 9 out for Android Live Wallpaper, I am going to forgo the formality of actually explaining the code and some of the code speaks for itself... If there is a desire for more information, just post a comment and I'll put the tutorial on my list.]
Download and Source Code after the break…
Nehe’s lessons 8 converted to an Android Live Wallpaper….
This Tutorial covers using OpenGL ES Blending with a Textured Cube…
[Editor: In an effort to get NeHe lesson 8 out for Android Live Wallpaper, I am going to forgo the formality of actually explaining the code and some of the code speaks for itself... If there is a desire for more information, just post a comment and I'll put the tutorial on my list.]
Download and Source Code after the break…
Nehe’s lessons 7 converted to an Android Live Wallpaper….
This Tutorial covers using OpenGL ES Lighting with a Textured Cube…
[Editor: In an effort to get NeHe lesson 7 out for Android Live Wallpaper, I am going to forgo the formality of actually explaining the code and some of the code speaks for itself... If there is a desire for more information, just post a comment and I'll put the tutorial on my list.]
Download and Source Code after the break…

Recent Comments: