Skip to content

Commit

Permalink
creation of models
Browse files Browse the repository at this point in the history
  • Loading branch information
justinchang1124 authored Sep 12, 2020
1 parent 8911644 commit 119e5da
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 0 deletions.
85 changes: 85 additions & 0 deletions AnimatedModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.util.ArrayList;

/**
* A Model with the ability to support a trailing animation.
*
* @author Justin Chang
*/
public class AnimatedModel extends Model
{
/**
* Master list of all animated models to be rendered.
*/
public static ArrayList<AnimatedModel> pipeline = new ArrayList<AnimatedModel>();

/**
* The number of ticks for which this animation trails. As an example,
* lifeSpan = 20 implies that this model will be followed by 20 fading copies.
*/
public int lifeSpan;

/**
* The starting transparency of the fade operation.
*/
public int alpha_0;

/**
* All model trails corresponding to this AnimatedModel.
*/
private ArrayList<ModelTrail> trails = new ArrayList<ModelTrail>();

/**
* Constructs an empty AnimatedModel at the specified origin. Sets the
* specified binding constants and animation constants.
*/
public AnimatedModel(Vec3 o, boolean s, int life, int alpha)
{
super(o, s);
lifeSpan = life;
alpha_0 = alpha;
}

/**
* Copy constructor.
*/
public AnimatedModel(AnimatedModel o)
{
super(o);
lifeSpan = o.lifeSpan;
alpha_0 = o.alpha_0;
}

/**
* Updates the trails of this animated model.
*/
public void tick()
{
if (lifeSpan > 0 && alpha_0 > -1 && alpha_0 < 256)
{
// Store a new trail at this location.
trails.add(new ModelTrail(this));

for (int j = 0; j < trails.size(); j++)
{
// Decay the lifespan of each trail.
ModelTrail mt = trails.get(j);
mt.tick();

// Remove invisible trails.
if (mt.hidden())
{
trails.remove(mt);
j--;
}
}
}
}

// Adds the model and all corresponding trails to the pipeline.
public void addTo(ArrayList<Triangle> pipeline, boolean shadows)
{
super.addTo(pipeline, shadows);
for (ModelTrail mt : trails)
mt.addTo(pipeline, shadows);
}
}
128 changes: 128 additions & 0 deletions Model.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import java.util.ArrayList;

/**
* A configuration of triangles that can be rendered. All triangles are fixed in
* relation to each other, but the net model can be rotated and displaced.
*
* @author Justin C
*/
public abstract class Model
{
/**
* Master list of all models to be rendered.
*/
public static ArrayList<Model> pipeline = new ArrayList<Model>();

/**
* A local tris that stores all triangles in relation to the localOrigin.
*/
public ArrayList<Triangle> tris = new ArrayList<Triangle>();

/**
* A Vec3 representing the displacement of this Model from the global
* origin.
*/
public Vec3 origin;

/**
* Does this model experience shadows?
*/
public boolean shadowBound;

/**
* Rotational orientations of the entire Model about respective axes.
*/
public double rX, rY, rZ;

/**
* The current orientation matrix. Only updated when necessary.
*/
private Matrix orientMatrix;

/**
* Constructs an empty Model with the specified origin and binding constants.
*/
public Model(Vec3 o, boolean s)
{
origin = o;
shadowBound = s;
}

/**
* Copy constructor.
*/
public Model(Model o)
{
this(new Vec3(o.origin), o.shadowBound);

int len = o.tris.size();

for (int i = 0; i < len; i++)
tris.add(new Triangle(o.tris.get(i)));
}

/**
* Updates the orientation matrix. Only called when necessary.
*/
private void updateOrientMatrix()
{
orientMatrix = Matrix.rotate(1, 0, 0, rX).times(
Matrix.rotate(0, 1, 0, rY).times(Matrix.rotate(0, 0, 1, rZ)));
}

/**
* Orients a local vertex in the global coordinate system.
*/
private Vec3 orient(Vec3 r)
{
return r.itimes(orientMatrix).add(origin);
}

/**
* Orients a local triangle in the global coordinate system.
*/
private Triangle orient(Triangle r)
{
return new Triangle(
orient(r.v1), orient(r.v2), orient(r.v3), r.color, r.shadows);
}

/**
* Adds the oriented version of another model to this model.
*/
public void append(Model o)
{
o.updateOrientMatrix();
for (int i = 0; i < o.tris.size(); i++)
tris.add(o.orient(o.tris.get(i)));
}

/**
* Adds this oriented model to a rendering pipeline.
*/
public void addTo(ArrayList<Triangle> pipeline, boolean shadows)
{
updateOrientMatrix();

if (shadowBound)
for (int i = 0; i < tris.size(); i++)
tris.get(i).shadows = shadows;

for (Triangle t : tris)
pipeline.add(orient(t));
}

/**
* An abbreviated summary that acts as a string representation.
*/
public String toString()
{
return "***MODEL***\nOrigin: " + origin + "\nRotation(x,y,z): " +
new Vec3(rX, rY, rZ) + "\n";
}

/**
* Updates this model. Can be used for movement.
*/
public abstract void tick();
}
76 changes: 76 additions & 0 deletions ModelTrail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.awt.Color;

/**
* A Model whose colors' RGB value linearly fades over time towards black. The
* transparency of each color linearly fades over time from a specified starting
* point alpha_0, given by the AnimatedModel this ModelTrail is based on.
*
* @author Justin Chang
*/
public class ModelTrail extends Model
{
/**
* The current remaining life span of the trail in ticks.
*/
private int currentLifeSpan;

/**
* The original life span of the trail in ticks.
*/
private final int originalLifeSpan;

/**
* The starting transparency of the trail, out of 255.
*/
private final int startingAlpha;

/**
* Creates a ModelTrail for an AnimatedModel.
*/
public ModelTrail(AnimatedModel am)
{
super(am);
startingAlpha = am.alpha_0;
originalLifeSpan = am.lifeSpan;
currentLifeSpan = originalLifeSpan;

// Aligns to given rotation values.
rX = am.rX;
rY = am.rY;
rZ = am.rZ;
}

/**
* Causes the color and transparency to decay.
*/
public void tick()
{
if (currentLifeSpan > 0)
for (int i = 0; i < tris.size(); i++)
{
int r = tris.get(i).color.getRed();
int g = tris.get(i).color.getGreen();
int b = tris.get(i).color.getBlue();

// Linearly fading factors, albeit arbitrary.
double shade = (double) currentLifeSpan / (currentLifeSpan + 1);
double fade = (double) currentLifeSpan / originalLifeSpan;

tris.get(i).color = new Color(
(int) (r * shade),
(int) (g * shade),
(int) (b * shade),
(int) (startingAlpha * fade));
}

currentLifeSpan--;
}

/**
* Returns whether or not the trail is no longer visible.
*/
public boolean hidden()
{
return currentLifeSpan <= 0;
}
}

0 comments on commit 119e5da

Please sign in to comment.