Guest User

Model Renderer

a guest
Oct 20th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. import org.lwjgl.opengl.GL11;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Model {
  6.     //This stores the information for the model, including rotation,
  7.     //location, and data for vertices and faces,
  8.     private ArrayList<int[]> faces;
  9.     private ArrayList<float[]> vertices;
  10.     private ArrayList<Integer> colorInts;
  11.     private double xCoord;
  12.     private double yCoord;
  13.     private double zCoord;
  14.     private float xDegrees;
  15.     private float yDegrees;
  16.     private float zDegrees;
  17.  
  18.     //Constructor called by the OBJ model parser to store the retrieved model data
  19.     Model(ArrayList<int[]> facesList, ArrayList<float[]> verticesList, ArrayList<Integer> colorInts) {
  20.         this.faces = facesList;
  21.         this.vertices = verticesList;
  22.         this.colorInts = colorInts;
  23.     }
  24.  
  25.     //This is called to render the model
  26.     public void display() {
  27.         GL11.glTranslated(xCoord, yCoord, zCoord);
  28.         GL11.glRotatef(xDegrees, 1.0f, 0, 0);
  29.         GL11.glRotatef(yDegrees, 0, 1.0f, 0);
  30.         GL11.glRotatef(zDegrees, 0, 0, 1.0f);
  31.  
  32.         GL11.glPushMatrix();
  33.  
  34.         //All models used have the faces triangulated when being exported to obj
  35.         GL11.glBegin(GL11.GL_TRIANGLES);
  36.         for (int i=0; i<faces.size(); i++) {
  37.             int[] face = faces.get(i);
  38.             int color = colorInts.get(i);
  39.  
  40.             //For testing purposes, sets the face color which is determined by the parser when it encounters a new face
  41.             switch (color) {
  42.                 case 0: GL11.glColor3f(1.0f, 0, 0);
  43.                     break;
  44.                 case 1: GL11.glColor3f(0, 1.0f, 0);
  45.                     break;
  46.                 case 2: GL11.glColor3f(0, 0, 1.0f);
  47.             }
  48.  
  49.             for (int j=0; j<face.length; j++) {
  50.                 float[] vertex = vertices.get((face[j] - 1));
  51.                 GL11.glVertex3f((vertex[0]), (vertex[1]), (vertex[2]));
  52.             }
  53.         }
  54.         GL11.glEnd();
  55.         GL11.glPopMatrix();
  56.     }
  57.  
  58.     //This sets the rotation of the model to the angles specified for each axis
  59.     public void setRotation(float xAngle, float yAngle, float zAngle) {
  60.         float x = xAngle;
  61.         float y = yAngle;
  62.         float z = zAngle;
  63.         if (x >= 360.0f) {
  64.             x += -360.0f;
  65.         }
  66.         if (y >= 360.0f) {
  67.             y += -360.0f;
  68.         }
  69.         if (z >= 360.0f) {
  70.             z += -360.0f;
  71.         }
  72.  
  73.         this.xDegrees = x;
  74.         this.yDegrees = y;
  75.         this.zDegrees = z;
  76.     }
  77.  
  78.     //This rotates the displayed model by the specified increment
  79.     public void rotateBy(float xIncrement, float yIncrement, float zIncrement) {
  80.         float x = xDegrees;
  81.         float y = yDegrees;
  82.         float z = zDegrees;
  83.         setRotation((x + xIncrement), (y + yIncrement), (z + zIncrement));
  84.     }
  85.  
  86.     //This sets the location of the displayed model to the specified coordinates
  87.     public void setLocation(double xLoc, double yLoc, double zLoc) {
  88.         this.xCoord = xLoc;
  89.         this.yCoord = yLoc;
  90.         this.zCoord = zLoc;
  91.     }
  92.  
  93.     //This moves the displayed model by the specified increments
  94.     public void move(float xLocIncrement, float yLocIncrement, float zLocIncrement) {
  95.         double xLoc = this.xCoord;
  96.         double yLoc = this.yCoord;
  97.         double zLoc = this.zCoord;
  98.  
  99.         setLocation((xLoc + xLocIncrement), (yLoc + yLocIncrement), (zLoc + zLocIncrement));
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment