Android Open Source - Schooner-3D Game Object






From Project

Back to project page Schooner-3D.

License

The source code is released under:

Apache License

If you think the Android project Schooner-3D 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

/*
 * Copyright 2012 Dan Mercer/*from   ww w .  j ava 2 s . c o  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.supermercerbros.gameengine.objects;

import java.util.HashMap;
import java.util.LinkedList;

import android.opengl.Matrix;

import com.supermercerbros.gameengine.collision.Bounds;
import com.supermercerbros.gameengine.collision.Collider;
import com.supermercerbros.gameengine.collision.Collision;
import com.supermercerbros.gameengine.engine.Engine;
import com.supermercerbros.gameengine.engine.Normals;
import com.supermercerbros.gameengine.engine.shaders.Material;
import com.supermercerbros.gameengine.motion.Movement;
import com.supermercerbros.gameengine.motion.MovementData;
import com.supermercerbros.gameengine.parsers.PreObjectData;

/**
 * Represents a 3D mesh object.
 */
public class GameObject implements Collider {
  public static final String TAG = "GameObject";
  
  /**
   * Contains the indices of the vertices for the elements (i.e. triangles) in
   * this object.
   */
  public final short[] indices;
  
  /**
   * Contains the current object-space coordinates of the vertices used in
   * this </code>GameObject</code>. Every three values represent one vertex.
   */
  public final float[] verts;
  
  /**
   * Contains either the UV coordinates (float pairs) or colors (three floats
   * each) of the vertices in this <code>GameObject</code>.
   * 
   * @see <a href="http://en.wikipedia.org/wiki/UV_mapping">UV Mapping</a>
   *      (Wikipedia)
   */
  public final float[] mtl;
  
  /**
   * Contains the normals of the vertices of this <code>GameObject</code>.
   */
  public final float[] normals;
  
  /**
   * Contains the indices of the vertex pairs that are identical
   * geometrically. Used in normal calculation.
   */
  public final short[][] doubles;
  
  /**
   * The Metadata about this GameObject.
   */
  public final Metadata info;
  
  /**
   * The model transformation matrix for this GameObject
   */
  public final float[] modelMatrix;
  
  public final GameObject parent;
  public final boolean isInstance;
  
  /**
   * The current Movement of the GameObject
   */
  protected Movement motion;
  /**
   * The MovementData containing GameObject-specific information about
   * {@link #motion}
   */
  protected final MovementData motionData;
  
  /**
   * 
   * @param verts
   *            The object-space coordinates of the object's vertices. Every
   *            three values represent one vertex (x-, y-, and z-coordinates).
   * @param indices
   *            The indices of the vertices for the triangles in this object.
   * @param normals
   *            The coordinates of the normal vectors of the vertices.
   * @param mtl
   *            The UV texture coordinates of the triangles.
   * @param mtl
   *            A Material object to use when for rendering
   */
  public GameObject(float[] verts, short[] indices, float[] normals,
      float[] uvs, short[][] doubles, Material mtl) {
    this.verts = verts;
    this.indices = indices;
    this.mtl = uvs;
    this.normals = (normals != null) ? normals : new float[verts.length];
    this.doubles = doubles;
    
    // Instance information
    this.isInstance = false;
    this.parent = null;
    
    // Metadata
    info = new Metadata();
    info.size = indices.length;
    info.count = verts.length / 3;
    info.mtl = mtl;
    
    // Movements
    motionData = new MovementData();
    
    // Collision detection
    collisions = new HashMap<Collision, Collider>();
    
    // Model Matrix
    modelMatrix = new float[16];
    Matrix.setIdentityM(modelMatrix, 0);
    
    // Normals
    if (normals == null) {
      Normals.calculate(this);
    }
  }
  
  public GameObject(PreObjectData data, Material material) {
    this.verts = data.verts;
    this.indices = data.indices;
    this.mtl = data.uvs;
    this.normals = new float[verts.length];
    this.doubles = data.doubles;
    Normals.calculate(this);
    
    // Instance information
    this.parent = data.parent;
    if (parent != null) {
      isInstance = true;
      info = new Metadata(parent.info.bufferLocations);
    } else {
      isInstance = false;
      info = new Metadata();
    }
    
    // Metadata
    info.size = indices.length;
    info.count = verts.length / 3;
    info.mtl = material;
    
    // Movements
    motionData = new MovementData();
    
    // Collision Detection
    collisions = new HashMap<Collision, Collider>();
    
    // Model matrix
    if (data.matrix == null) {
      modelMatrix = new float[16];
      Matrix.setIdentityM(modelMatrix, 0);
    } else {
      modelMatrix = data.matrix;
    }
  }

  /**
   * Returns a LinkedList containing one instance of this GameObject per
   * Material in <code>materials</code>.
   * 
   * @param materials
   *            An array of the Materials to use for the instances.
   * @return a LinkedList of GameObjects, or null if no materials are given.
   */
  public LinkedList<GameObject> instances(Material... materials) {
    if (materials.length == 0) {
      return null;
    }
    LinkedList<GameObject> instances = new LinkedList<GameObject>();
    if (isInstance) {
      for (Material material : materials) {
        final GameObject instance = parent.getInstance(material);
        material.makeProgram();
        instances.add(instance);
      }
    } else {
      for (Material material : materials) {
        final GameObject instance = getInstance(material);
        material.makeProgram();
        instances.add(instance);
      }
    }
    return instances;
  }
  
  public GameObject instance(Material material) {
    if (isInstance) {
      final GameObject instance = parent.getInstance(material);
      material.makeProgram();
      return instance;
    } else {
      final GameObject instance = getInstance(material);
      material.makeProgram();
      return instance;
    }
  }
  
  /**
   * Subclasses should override this to return an instance of that class.
   * @param material
   * @return
   */
  protected GameObject getInstance(Material material) {
    PreObjectData preData = new PreObjectData(verts, indices, mtl, doubles, null, null);
    preData.parent = this;
    return new GameObject(preData, material);
  }

  /**
   * This method is called to tell the object to recalculate its
   * transformation matrix for the given point in time, in milliseconds.
   * 
   * @param time
   *            The time of the frame currently being calculated,
   *            in milliseconds.
   * 
   * @see AnimatedMeshObject#drawMatrix(long)
   */
  public void drawMatrix(long time) {
    synchronized (motionData) {
      if (motion != null) {
        motion.getFrame(this, motionData, time);
      }
    }
  }
  
  /**
   * This method is called to tell the object to update its vertices for the
   * given point in time, in milliseconds.
   * 
   * <p>
   * The default implementation does nothing. To do something with the
   * object-space (local) vertices every frame, override this method in a
   * subclass.
   * </p>
   * 
   * @param time
   *            The time of the frame currently being calculated,
   *            in milliseconds.
   */
  public void drawVerts(long time) {
    // Subclasses can do something here.
  }
  
  /**
   * @return <code>{@link #info}.visible</code>
   */
  public boolean isVisible() {
    return info.isVisible;
  }
  
  /**
   * Sets the visibility of this GameObject. Should only be called from the Engine thread.
   */
  public void setVisible(boolean visible) {
    info.isVisible = visible;
  }
  
  /**
   * Sets and starts the Movement that is used to animate this GameObject's
   * location.
   * 
   * @param time
   *            The current time, in milliseconds.
   * @param duration
   *            The duration of the movement, in milliseconds.
   */
  public void startMovement(Movement motion, long time, long duration) {
    synchronized (motionData) {
      this.motion = motion;
      this.motionData.set(time, duration, modelMatrix);
    }
  }
  
  public void endMovement() {
    synchronized (motionData) {
      this.motion = null;
    }
  }
  
  private final HashMap<Collision, Collider> collisions;
  private Bounds bounds;
  
  @Override
  public Bounds getBounds() {
    return bounds;
  }
  
  public void setBounds(Bounds bounds) {
    this.bounds = bounds;
  }
  
  @Override
  public float[] getMatrix() {
    return modelMatrix;
  }
  
  @Override
  public void clearCollisions() {
    collisions.clear();
  }
  
  @Override
  public void addCollision(Collider other, Collision collision) {
    collisions.put(collision, other);
  }
  
  /**
   * Returns the number of extra matrices used by this GameObject. The default
   * implementation returns zero.
   * 
   * @return The number of extra matrices used by this GameObject.
   */
  public int getExtraMatrixCount() {
    return 0;
  }
  
  public void writeMatrices(float[] matrixArray){
    System.arraycopy(modelMatrix, 0, matrixArray, 0, 16);
  }
  
}




Java Source Code List

com.supermercerbros.gameengine.GameActivity.java
com.supermercerbros.gameengine.GameView.java
com.supermercerbros.gameengine.Schooner3D.java
com.supermercerbros.gameengine.TestActivity.java
com.supermercerbros.gameengine.TestMaterials.java
com.supermercerbros.gameengine.TestObjects.java
com.supermercerbros.gameengine.animation.AnimationData.java
com.supermercerbros.gameengine.animation.Keyframe.java
com.supermercerbros.gameengine.animation.MeshAnimation.java
com.supermercerbros.gameengine.armature.ActionData.java
com.supermercerbros.gameengine.armature.Action.java
com.supermercerbros.gameengine.armature.BinarySkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Bone.java
com.supermercerbros.gameengine.armature.SkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Skeleton.java
com.supermercerbros.gameengine.collision.Bounds.java
com.supermercerbros.gameengine.collision.Collider.java
com.supermercerbros.gameengine.collision.CollisionDetector.java
com.supermercerbros.gameengine.collision.Collision.java
com.supermercerbros.gameengine.collision.DebugListener.java
com.supermercerbros.gameengine.collision.Edge.java
com.supermercerbros.gameengine.collision.Face.java
com.supermercerbros.gameengine.collision.Feature.java
com.supermercerbros.gameengine.collision.Intersection.java
com.supermercerbros.gameengine.collision.Line.java
com.supermercerbros.gameengine.collision.LocalDistMinimum.java
com.supermercerbros.gameengine.collision.Matrix.java
com.supermercerbros.gameengine.collision.OnCollisionCheckFinishedListener.java
com.supermercerbros.gameengine.collision.Plane.java
com.supermercerbros.gameengine.collision.Point.java
com.supermercerbros.gameengine.collision.Polyhedron.java
com.supermercerbros.gameengine.collision.SphereBounds.java
com.supermercerbros.gameengine.collision.Vector.java
com.supermercerbros.gameengine.collision.Vertex.java
com.supermercerbros.gameengine.debug.JankCatcher.java
com.supermercerbros.gameengine.debug.LoopLog.java
com.supermercerbros.gameengine.engine.Camera.java
com.supermercerbros.gameengine.engine.DataPipe.java
com.supermercerbros.gameengine.engine.EGLContextLostHandler.java
com.supermercerbros.gameengine.engine.Engine.java
com.supermercerbros.gameengine.engine.GameRenderer.java
com.supermercerbros.gameengine.engine.Light.java
com.supermercerbros.gameengine.engine.Normals.java
com.supermercerbros.gameengine.engine.RenderData.java
com.supermercerbros.gameengine.engine.Scene.java
com.supermercerbros.gameengine.engine.Time.java
com.supermercerbros.gameengine.engine.shaders.Material.java
com.supermercerbros.gameengine.engine.shaders.Program.java
com.supermercerbros.gameengine.engine.shaders.ShaderLib.java
com.supermercerbros.gameengine.engine.shaders.Shader.java
com.supermercerbros.gameengine.engine.shaders.VertexModifier.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteDispatcher.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteListener.java
com.supermercerbros.gameengine.hud.CoordsConverter.java
com.supermercerbros.gameengine.hud.GameHud.java
com.supermercerbros.gameengine.hud.HudElement.java
com.supermercerbros.gameengine.material.CelShadedMaterial.java
com.supermercerbros.gameengine.material.TexturedMaterial.java
com.supermercerbros.gameengine.math.BezierCurve.java
com.supermercerbros.gameengine.math.Curve.java
com.supermercerbros.gameengine.math.MatrixUtils.java
com.supermercerbros.gameengine.math.Quaternion.java
com.supermercerbros.gameengine.motion.CurveMovement.java
com.supermercerbros.gameengine.motion.MovementData.java
com.supermercerbros.gameengine.motion.Movement.java
com.supermercerbros.gameengine.objects.AnimatedMeshObject.java
com.supermercerbros.gameengine.objects.BasicMaterial.java
com.supermercerbros.gameengine.objects.BonedObject.java
com.supermercerbros.gameengine.objects.GameObject.java
com.supermercerbros.gameengine.objects.Metadata.java
com.supermercerbros.gameengine.parsers.ConstantCurve.java
com.supermercerbros.gameengine.parsers.GameFactory.java
com.supermercerbros.gameengine.parsers.PreBoneData.java
com.supermercerbros.gameengine.parsers.PreObjectData.java
com.supermercerbros.gameengine.parsers.Sch3D.java
com.supermercerbros.gameengine.render.Compositor.java
com.supermercerbros.gameengine.shaders.ProgramSource.java
com.supermercerbros.gameengine.texture.BitmapTexture.java
com.supermercerbros.gameengine.texture.ETC1CompressedTexture.java
com.supermercerbros.gameengine.texture.Texture.java
com.supermercerbros.gameengine.util.BetterDataInputStream.java
com.supermercerbros.gameengine.util.DelayedRunnable.java
com.supermercerbros.gameengine.util.GLES2.java
com.supermercerbros.gameengine.util.IPO.java
com.supermercerbros.gameengine.util.LoopingThread.java
com.supermercerbros.gameengine.util.Toggle.java
com.supermercerbros.gameengine.util.Utils.java