Package engine

Source Code of engine.Entity

package engine;
import engine.model.Material;
import engine.model.ObjModel;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
/**
* This class represents the data of an object in three dimensional space
* @author jari <rasaari@gmail.com>
* @author simo <simppak@gmail.com>
*
*/
public class Entity {
  protected Vec3f position;
  private Vec3f rotation;
  private Vec3f scale;
 
  private boolean hidden;
 
  public ObjModel model;
  public Material material;
 
  /**
   * Default constructor
   */
  public Entity(){
    this.position = new Vec3f(0.0f,0.0f,0.0f);
    this.rotation = new Vec3f(0.0f,0.0f,0.0f);
    this.scale = new Vec3f(1.0f,1.0f,1.0f);
  }
 
  /**
   * Set Material to this entity
   * @param ObjModel model
   */
  public void setMaterial(Material material) {
    this.material = material;
  }
 
  /**
   * Set Obj-formatted 3d-model to this entity
   * @param ObjModel model
   */
  public void setModel(ObjModel model) {
    this.model = model;
    this.material = model.getDefaultMaterial();
  }
 
  public void setPos(Vec3f pos) {
    this.position = pos;
  }
 
  public void setPos(float x, float y, float z) {
    this.position.x = x;
    this.position.y = y;
    this.position.z = z;
  }
 
  public void setRot(Vec3f rot) {
    this.rotation = rot;
  }
 
  public void setRot(float x, float y, float z) {
    this.rotation.x = x;
    this.rotation.y = y;
    this.rotation.z = z;
  }
 
  public void setScale(Vec3f scale) {
    this.scale = scale;
  }
  public void setScale(float x, float y, float z) {
    this.scale.set(x, y, z);
  }
 
  public void setScale(float scale) {
    this.scale.set(scale);
  }
  /*
  public void setAlpha(float a){
    this.alpha = Math.max(a,0.0f);
    this.alpha = Math.min(a,1.0f);
  }
 
  public float getAlpha(){
    return this.alpha;
  }
  */
  public void move(float x, float y, float z) {
    this.position.x += x;
    this.position.y += y;
    this.position.z += z;
  }
 
  public void localmove(float x, float y, float z) {
    float xrotrad,yrotrad;
    xrotrad = (float)Math.toRadians(this.rotation.x);
    yrotrad = (float)Math.toRadians(this.rotation.y);
   
    this.move((float)Math.sin(yrotrad)*z, (float)Math.sin(xrotrad)*z, (float)Math.cos(yrotrad)*z);
  }
 
  public void rotate(float x, float y, float z) {
    this.rotation.x += x;
    this.rotation.y += y;
    this.rotation.z += z;
  }
 
  public Vec3f getPos() {
    return this.position; /*new Vec3f(this.position); */
  }
 
  //public Vec3f getPos() { return this.position; }
  public Vec3f getRot() {
    return this.rotation;
  }
 
  public Matrix4f getModelMatrix() {
    Matrix4f mat4 = new Matrix4f();
    mat4.setIdentity();
   
    mat4.translate(new Vector3f(this.position.x,this.position.y,this.position.z));
    mat4.scale(new Vector3f(this.scale.x,this.scale.y,this.scale.z));
    mat4.rotate((float)Math.toRadians(this.rotation.x), new Vector3f(1.0f, 0.0f, 0.0f));
    mat4.rotate((float)Math.toRadians(this.rotation.y), new Vector3f(0.0f, 1.0f, 0.0f));
    mat4.rotate((float)Math.toRadians(this.rotation.z), new Vector3f(0.0f, 0.0f, 1.0f));
   
    //FloatBuffer fl = BufferUtils.createFloatBuffer(16);
    //glGetFloat(GL_MODELVIEW_MATRIX, fl);
    /*
    String str = new String();
    for(int i=0; i<4;i++)
      str += fl.get(i)+" "; 
    str += "\n";
    for(int i=4; i<8;i++)
      str += fl.get(i)+" "; 
    str += "\n";
    for(int i=8; i<12;i++)
      str += fl.get(i)+" ";
   
    str += "\n";
    for(int i=12; i<16;i++)
      str += fl.get(i)+" "; 
    str += "\n";
   
    Matrix4f temp = new Matrix4f();
    Matrix4f.mul(viewMatrix,mat4, temp);
    System.out.println("1:"+str);
    System.out.println("2:"+temp.transpose().toString());
    */
    return mat4;
  }
  public float getScale() { return this.scale.x; }
 
  /**
   * Orients this entity in 3d-space  and calls this entity's model's draw-function.
   * @return returns true if the entity was drawn.
   */
  public boolean exist() {
    if(this.hidden) return true;
    if(this.model == null) return false;
    glPushMatrix();
   
    glTranslatef(this.position.x,this.position.y,this.position.z);
    glRotatef(this.rotation.x,1.0f,0.0f,0.0f);
    glRotatef(this.rotation.y,0.0f,1.0f,0.0f);
    glRotatef(this.rotation.z,0.0f,0.0f,1.0f);
    glScalef(this.scale.x, this.scale.y, this.scale.z);
   
    this.model.draw_glsl(this.material,this.getModelMatrix());
    glPopMatrix();
   
    return true;
  }
 
  public void existDebug() {
    glPushMatrix();
    glTranslatef(this.position.x,this.position.y,this.position.z);
    glRotatef(this.rotation.x,1.0f,0.0f,0.0f);
    glRotatef(this.rotation.y,0.0f,1.0f,0.0f);
    glRotatef(this.rotation.z,0.0f,0.0f,1.0f);
    glScalef(this.scale.x, this.scale.y, this.scale.z);
   
    this.model.drawNormals();
   
    glPopMatrix();
  }
 
  public boolean exist2D(float top, float xrot) {
   
    glPushMatrix();
    glTranslatef(0f,top,900f);
    glRotatef(180f+xrot,1.0f,0.0f,0.0f);
    glScalef(2f,2f,2f);
    this.model.draw_glsl(material,this.getModelMatrix());
    glPopMatrix();
    return true;
  }
}
TOP

Related Classes of engine.Entity

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.