Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.lwjgl.opengl.GL11;
- import java.util.ArrayList;
- public class Model {
- //This stores the information for the model, including rotation,
- //location, and data for vertices and faces,
- private ArrayList<int[]> faces;
- private ArrayList<float[]> vertices;
- private ArrayList<Integer> colorInts;
- private double xCoord;
- private double yCoord;
- private double zCoord;
- private float xDegrees;
- private float yDegrees;
- private float zDegrees;
- //Constructor called by the OBJ model parser to store the retrieved model data
- Model(ArrayList<int[]> facesList, ArrayList<float[]> verticesList, ArrayList<Integer> colorInts) {
- this.faces = facesList;
- this.vertices = verticesList;
- this.colorInts = colorInts;
- }
- //This is called to render the model
- public void display() {
- GL11.glTranslated(xCoord, yCoord, zCoord);
- GL11.glRotatef(xDegrees, 1.0f, 0, 0);
- GL11.glRotatef(yDegrees, 0, 1.0f, 0);
- GL11.glRotatef(zDegrees, 0, 0, 1.0f);
- GL11.glPushMatrix();
- //All models used have the faces triangulated when being exported to obj
- GL11.glBegin(GL11.GL_TRIANGLES);
- for (int i=0; i<faces.size(); i++) {
- int[] face = faces.get(i);
- int color = colorInts.get(i);
- //For testing purposes, sets the face color which is determined by the parser when it encounters a new face
- switch (color) {
- case 0: GL11.glColor3f(1.0f, 0, 0);
- break;
- case 1: GL11.glColor3f(0, 1.0f, 0);
- break;
- case 2: GL11.glColor3f(0, 0, 1.0f);
- }
- for (int j=0; j<face.length; j++) {
- float[] vertex = vertices.get((face[j] - 1));
- GL11.glVertex3f((vertex[0]), (vertex[1]), (vertex[2]));
- }
- }
- GL11.glEnd();
- GL11.glPopMatrix();
- }
- //This sets the rotation of the model to the angles specified for each axis
- public void setRotation(float xAngle, float yAngle, float zAngle) {
- float x = xAngle;
- float y = yAngle;
- float z = zAngle;
- if (x >= 360.0f) {
- x += -360.0f;
- }
- if (y >= 360.0f) {
- y += -360.0f;
- }
- if (z >= 360.0f) {
- z += -360.0f;
- }
- this.xDegrees = x;
- this.yDegrees = y;
- this.zDegrees = z;
- }
- //This rotates the displayed model by the specified increment
- public void rotateBy(float xIncrement, float yIncrement, float zIncrement) {
- float x = xDegrees;
- float y = yDegrees;
- float z = zDegrees;
- setRotation((x + xIncrement), (y + yIncrement), (z + zIncrement));
- }
- //This sets the location of the displayed model to the specified coordinates
- public void setLocation(double xLoc, double yLoc, double zLoc) {
- this.xCoord = xLoc;
- this.yCoord = yLoc;
- this.zCoord = zLoc;
- }
- //This moves the displayed model by the specified increments
- public void move(float xLocIncrement, float yLocIncrement, float zLocIncrement) {
- double xLoc = this.xCoord;
- double yLoc = this.yCoord;
- double zLoc = this.zCoord;
- setLocation((xLoc + xLocIncrement), (yLoc + yLocIncrement), (zLoc + zLocIncrement));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment