Android Open Source - block-composer Model






From Project

Back to project page block-composer.

License

The source code is released under:

MIT License

If you think the Android project block-composer 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 bwr.blockcomposer.models;
//  ww  w. j av a  2 s .c  om
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

public class Model {
  private final static int FLOAT_SIZE = 4;
  private final static int CHAR_SIZE = 2;
  
  private int numVertices;
  private int numTriangles;
  
  private FloatBuffer vertexBuffer;
  private FloatBuffer normalBuffer;
  private FloatBuffer textureBuffer;
  private CharBuffer indexBuffer;
  
  private static class TextureCoords {
    public float u,v;
  }
  
  private static class Vertex {
    public float x,y,z;
  }
  
  private static class Triangle {
    public final Vertex[] vertices = new Vertex[3];
    public final Vertex[] normals = new Vertex[3];
    public final TextureCoords[] tcoords = new TextureCoords[3];
  }
  
  public void prerender(GL10 gl) {
    gl.glFrontFace(GL10.GL_CCW);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
    gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer);
    
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
  }
  
  public void render(GL10 gl) {
    gl.glDrawElements(GL10.GL_TRIANGLES, numVertices, GL10.GL_UNSIGNED_SHORT, indexBuffer);
  }
  
  public void postrender(GL10 gl) {
    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);  
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
  }
  
  private static Triangle readTriangle(DataInputStream dataInput) throws IOException {
    Triangle triangle = new Triangle();
    
    for(int i = 0; i < 3; i++) {
      triangle.vertices[i] = new Vertex();
      triangle.vertices[i].x = dataInput.readFloat();
      triangle.vertices[i].y = dataInput.readFloat();
      triangle.vertices[i].z = dataInput.readFloat();
    }
    
    for(int i = 0; i < 3; i++) {
      triangle.normals[i] = new Vertex();
      triangle.normals[i].x = dataInput.readFloat();
      triangle.normals[i].y = dataInput.readFloat();
      triangle.normals[i].z = dataInput.readFloat();
    }
    
    for(int i = 0; i < 3; i++) {
      triangle.tcoords[i] = new TextureCoords();
      triangle.tcoords[i].u = dataInput.readFloat();
      triangle.tcoords[i].v = 1.0f - dataInput.readFloat();
    }
    
    return triangle;
  }
  
  public static Model loadFromResource(Context context, int resourceId) throws IOException {
    Model model = new Model();
    
    DataInputStream dataInput = new DataInputStream(new BufferedInputStream(context.getResources().openRawResource(resourceId)));
    
    model.numTriangles = dataInput.readInt();
    model.numVertices = model.numTriangles * 3;
            
    model.vertexBuffer = ByteBuffer.allocateDirect(model.numVertices*3*FLOAT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
    model.indexBuffer = ByteBuffer.allocateDirect(model.numVertices*CHAR_SIZE).order(ByteOrder.nativeOrder()).asCharBuffer();
    model.normalBuffer = ByteBuffer.allocateDirect(model.numVertices*3*FLOAT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
    model.textureBuffer = ByteBuffer.allocateDirect(model.numVertices*2*FLOAT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
    
        char index = 0; // vertex index
        
    for(int t = 0; t < model.numTriangles; t++) {
      Triangle triangle = readTriangle(dataInput);
      
      for(int v = 0; v < 3; v++) {
        model.vertexBuffer.put(triangle.vertices[v].x);
        model.vertexBuffer.put(triangle.vertices[v].y);
        model.vertexBuffer.put(triangle.vertices[v].z);
        
        model.normalBuffer.put(triangle.normals[v].x);
        model.normalBuffer.put(triangle.normals[v].y);
        model.normalBuffer.put(triangle.normals[v].z);
        
        model.textureBuffer.put(triangle.tcoords[v].u);
        model.textureBuffer.put(triangle.tcoords[v].v);
        
        model.indexBuffer.put(index);
        index++;
      }
    }
    
    model.vertexBuffer.position(0);
    model.indexBuffer.position(0);
        model.normalBuffer.position(0);
    model.textureBuffer.position(0);

    return model;
  }
}




Java Source Code List

bwr.blockcomposer.AnimationFinishedListener.java
bwr.blockcomposer.BlockComposerActivity.java
bwr.blockcomposer.BlockComposerRenderer.java
bwr.blockcomposer.BlockComposer.java
bwr.blockcomposer.Camera.java
bwr.blockcomposer.GameResources.java
bwr.blockcomposer.OnUpdateAction.java
bwr.blockcomposer.gamedata.Block.java
bwr.blockcomposer.gamedata.GameEntity.java
bwr.blockcomposer.gamedata.GameState.java
bwr.blockcomposer.gamedata.LevelDimensions.java
bwr.blockcomposer.gamedata.LevelMask.java
bwr.blockcomposer.gamedata.LevelMetadata.java
bwr.blockcomposer.gamedata.LevelStore.java
bwr.blockcomposer.gamedata.Level.java
bwr.blockcomposer.gamedata.Move.java
bwr.blockcomposer.gamedata.StaticLevelStore.java
bwr.blockcomposer.gamedata.UserLevelStore.java
bwr.blockcomposer.misc.GLUtility.java
bwr.blockcomposer.misc.Timer.java
bwr.blockcomposer.models.AxisRenderer.java
bwr.blockcomposer.models.Model.java
bwr.blockcomposer.models.SelectBoxRenderer.java
bwr.blockcomposer.modes.EditMode.java
bwr.blockcomposer.modes.GameDialogMode.java
bwr.blockcomposer.modes.GameMode.java
bwr.blockcomposer.modes.LevelImportMode.java
bwr.blockcomposer.modes.LevelSelectMode.java
bwr.blockcomposer.modes.LogoDisplayMode.java
bwr.blockcomposer.modes.ModeController.java
bwr.blockcomposer.modes.Mode.java
bwr.blockcomposer.modes.OverlayMode.java
bwr.blockcomposer.types.FlipRotateFloat.java
bwr.blockcomposer.types.FloatValueVector3.java
bwr.blockcomposer.types.FloatValue.java
bwr.blockcomposer.types.IntVector.java
bwr.blockcomposer.types.RotatingFloat.java
bwr.blockcomposer.ui.TextRenderer.java
bwr.blockcomposer.ui.UIElementPicture.java
bwr.blockcomposer.ui.UIElementText.java
bwr.blockcomposer.ui.UIElement.java