Wednesday, 6 May 2015

Lesson 6 Android and OpenGL ---- 4

 Android and OpenGL

Here is the code in my onDrawFrame method.
 public void onDrawFrame(GL10 gl) {
 
     //Reset Drawing Surface
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
     gl.glLoadIdentity();
 
     gl.glTranslatef(-4.0f, 5.0f, -20.0f);
     Letters.H.draw(gl);
 
     gl.glTranslatef(2.0f, 0.0f, 0.0f);
     Letters.E.draw(gl);
 
     gl.glTranslatef(1.5f, 0.0f, 0.0f);
     Letters.L.draw(gl);
 
     gl.glTranslatef(1.5f, 0.0f, 0.0f);
     Letters.L.draw(gl);
 
     gl.glTranslatef(1.5f, 0.0f, 0.0f);
     Letters.O.draw(gl);
 
     gl.glTranslatef(-7.0f, -5.0f, 0.0f);
     Letters.W.draw(gl);
 
     gl.glTranslatef(2.5f, 0.0f, 0.0f);
     Letters.O.draw(gl);
 
     gl.glTranslatef(2.0f, 0.0f, 0.0f);
     Letters.R.draw(gl);
 
     gl.glTranslatef(2.0f, 0.0f, 0.0f);
     Letters.L.draw(gl);
 
     gl.glTranslatef(1.5f, 0.0f, 0.0f);
     Letters.D.draw(gl);
 }
The first two lines reset the drawing surface.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
First clearing the surface to the background color, and then loading the identity matrix so no previous commands will affect our next set of commands.
Next I issue this command:
gl.glTranslatef(-4.0f, 5.0f, -20.0f);
This tells OpenGL to translate (move without rotating) the position where the next object will be drawn by (-4, 5, -20) along the x axis, y axis, and z axis respectively. This is the only time I will translate along the Z axis, but I need to make sure we are between the near and far planes that we set to 0.1 and 100. After performing my translation I will call the draw method on the letter H from my enum, causing it to draw itself.
Letters.H.draw(gl);
Now the next line of code will move the position from where we drew the letter H.
gl.glTranslatef(2.0f, 0.0f, 0.0f);
This is why we called gl.glLoadIdentity() at the very beginning of this method. If we had not done so OpenGL would begin where we left off the last time we drew a frame. Now, however, we use this to our advantage since we want to draw our letter E to the right of our letter H. The rest of this method follow this procedure, moving the position and then calling the draw function on the desired letter. I got the numbers used both for the vertices for each letter and for the distance used in the translations through trial and error, so they are based on how I thought the letters should look rather than any system. You should, by all means, try out different numbers to see what results you get. Here is my final result.
Hello World OpenGL
I know there was a lot of new material here, covered very quickly.  If you have any specific questions please leave a comment and I will be happy to answer to the best of my ability.  Also, since we have reached the end of the Hello World series please let me know if there is any specific topic you would like to see covered in a future article.

No comments:

Post a Comment