This is Part II of the Android OpenGL-Tutorial powered by brendan.d.burns
:larrow: previous....................................................next
:larrow: previous....................................................next
:idea: In this tutorial we are referring to the GLTutorialOne.java from brendans AndroidGL-Tutorial-Package (Download from Google-Code).
The code here is partially altered for a bit easier understanding.
:idea:
Download that package, to run that specific Tutorial.

The code here is partially altered for a bit easier understanding.
:idea:
What you will learn: This tutorial will explain how to prepare a OpenGL ES 'View' to allow drawing of primitives later.
Preparing a blank screen
In our constructor, we place a call to glClearColor(...). This is used to specify what color the window should be cleared with. The function takes 4 parameters. These parameters represent the RGBA color values and can range from 0.0f to 1.0f.
The first three parameters indicate your red, green and blue values. The larger the value, the brighter the color. If all values are 0.0f, you get black. If all values are 1, you get white.
The last value is the alpha value. This is used for transparency. 1.0f is completely opaque and 0.0f is completely transparent.
The code below sets the clearing color to black.
Using java Syntax Highlighting
- public class GLTutorialOne extends View {
- protected OpenGLContext myGLContext;
- protected GL10 myGL;
- public GLTutorialOne(Context c) {
- super(c);
- /* Create a OpenGLContext
- * In this case there is no
- * need to save it, but in other
- * cases it will be reused. */
- this.myGLContext = new OpenGLContext(0);
- /* Store the GL interface that will handle
- * the actual graphical operations */
- this.myGL = (GL10)this.myGLContext.getGL();
- /* Set the color to be uses when clearing
- * Params: Red, Green, Blue, Alpha
- * All from (0.0f up to 1.0f)
- * Below we use a android-green =) */
- this.myGL.glClearColor(0.0f, 0.8f, 0.0f, 0.0f);
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
We start off by first clearing the color buffer. This is achieved by using the glClear function. This function accepts one parameter being which buffers you want to clear. Other buffers will be discussed at a later stage. For now, you only need to know about the color buffer. This is specified by using the GL_COLOR_BUFFER_BIT flag. This causes the screen to be cleared to the color (android-green) specified above.
Using java Syntax Highlighting
- protected void onDraw(Canvas canvas) {
- /* Associate the glContext with the
- * canvas it should draw on */
- this.myGLContext.waitNative(canvas, this);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
waitNative() makes sure all pending native drawing calls have completed and binds this view's surface to the specified context.
Using java Syntax Highlighting
- /* waitNative() makes sure all pending
- * native drawing calls have completed
- * and binds this passed canvas to
- * the specified context(here: this). */
- this.myGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Thats it, when you run this application it shows a background with the clearColor we defined above. 

Full Source:
Using java Syntax Highlighting
- package edu.union;
- import javax.microedition.khronos.opengles.GL10;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.OpenGLContext;
- import android.view.View;
- public class GLTutorialOne extends View {
- protected OpenGLContext myGLContext;
- protected GL10 myGL;
- public GLTutorialOne(Context c) {
- super(c);
- /* Create a OpenGLContext
- * In this case there is no
- * need to save it, but in other
- * cases it will be reused. */
- this.myGLContext = new OpenGLContext(0);
- /* Store the GL interface that will handle
- * the actual graphical operations */
- this.myGL = (GL10)this.myGLContext.getGL();
- /* Set the color to be uses when clearing
- * Params: Red, Green, Blue, Alpha
- * All from (0.0f up to 1.0f)
- * Below we use a android-green =) */
- this.myGL.glClearColor(0.0f, 0.8f, 0.0f, 0.0f);
- }
- protected void onDraw(Canvas canvas) {
- /* waitNative() makes sure all pending
- * native drawing calls have completed
- * and binds this passed canvas to
- * the specified context(here: this). */
- this.myGLContext.waitNative(canvas, this);
- /* This call will finally cause our
- * canvas to be cleared to the color
- * we passed in the constructor */
- this.myGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Regards,
plusminus







