Android Open Source - OpenGlDraw_Android Shape Drawer






From Project

Back to project page OpenGlDraw_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project OpenGlDraw_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.opengldraw.gl;
// w  w w . j  a  v a2s. c om
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLUtils;

import com.example.opengldraw.shapes.FireEngine;
import com.example.opengldraw.utils.ShapeDataInterface;
import com.example.opengldraw.utils.WavefrontObj;

public class ShapeDrawer {

  /** Holds a reference to an Shape - (an object that implements the ShapeData interface) */
  private WavefrontObj myShape;
  private FireEngine fireEngine;
  int drawCount;
  private Context ctx;
  
    private FloatBuffer mColorBuffer;
    
  public ShapeDrawer(Context aCtx) {
    ctx = aCtx;
    /* Create a new desired shape from the com.example.opengldraw.shapes package */
    myShape = new WavefrontObj(ctx);
    myShape.loadObjWithTexture("square.obj");
    drawCount = myShape.indicesCount;
    
// Buffers to be passed to gl*Pointer() functions must be direct, i.e., they must be placed on the native heap where the garbage collector cannot move them.
// Buffers with multi-byte datatypes (e.g., short, int, float) must have their byte order set to native order
    
    ByteBuffer cbb = ByteBuffer.allocateDirect(myShape.getColors().length * 4);
        cbb.order(ByteOrder.nativeOrder());
        mColorBuffer = cbb.asFloatBuffer();
        mColorBuffer.put(myShape.getColors());
        mColorBuffer.position(0);
    }

    public void draw(GL10 gl) {
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
        
        enableTextures(gl);
        /* enable blending for transparency */
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
      gl.glFrontFace(GL10.GL_CW);
      /* back face culling*/
//        gl.glCullFace(GL10.GL_BACK);
//        gl.glEnable(GL10.GL_CULL_FACE);
      
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, WavefrontObj.mVertexBuffer);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
        gl.glNormalPointer(GL10.GL_FLOAT, 0, WavefrontObj.mNormBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLES, drawCount, GL10.GL_UNSIGNED_SHORT, WavefrontObj.mIndexBuffer);
        
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
        
        gl.glDisable(GL10.GL_BLEND);
        disableTextures(gl);
    }

    public ShapeDataInterface getCurrentShape() {
      return myShape;
    }
    
    private void enableTextures(GL10 gl) {
       gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
         gl.glEnable(GL10.GL_TEXTURE_2D);
         
         /* Set the texture file */
         Bitmap bitmap = null;
         if(fireEngine == null) {
           fireEngine = new FireEngine();
           bitmap = fireEngine.getFireBitmat();
         } else {
           bitmap = fireEngine.morphBitmap();
         }
         //bitmap = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.pattern);
         int textures[] = new int[1];
         gl.glGenTextures(1, textures, 0);
         gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, WavefrontObj.mTexBuffer);
         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);
         //gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
         //gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    }
    
    private void disableTextures(GL10 gl) {
       gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
         gl.glDisable(GL10.GL_TEXTURE_2D);
    }
}




Java Source Code List

com.example.opengldraw.MainActivity.java
com.example.opengldraw.gl.GLSurfaceViewDraw.java
com.example.opengldraw.gl.RendererDraw.java
com.example.opengldraw.gl.ShapeDrawer.java
com.example.opengldraw.shapes.FireEngine.java
com.example.opengldraw.shapes.ShapeCube.java
com.example.opengldraw.shapes.ShapeCup10Verts.java
com.example.opengldraw.shapes.ShapeCup64Verts.java
com.example.opengldraw.shapes.ShapeSquare.java
com.example.opengldraw.utils.IndexOrderObj.java
com.example.opengldraw.utils.ShapeDataInterface.java
com.example.opengldraw.utils.WavefrontObj.java