Advertisement
Guest User

Shape2D class

a guest
Jun 2nd, 2013
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. package primitives;
  2.  
  3. import java.nio.FloatBuffer;
  4. import java.util.Arrays;
  5.  
  6. import org.lwjgl.BufferUtils;
  7. import org.lwjgl.opengl.GL11;
  8. import org.lwjgl.util.vector.Matrix2f;
  9. import org.lwjgl.util.vector.Vector2f;
  10.  
  11. public class Shape2D
  12. {
  13.     protected float xPos;
  14.     protected float yPos;
  15.    
  16.     protected float[] localVerts;
  17.     protected float[] absoluteVerts;
  18.     protected float[] texCoords = null;
  19.     protected float[] colors = null;
  20.    
  21.    
  22.     protected float rotation;
  23.    
  24.    
  25.     public void draw(int glMode)
  26.     {
  27.         GL11.glBegin(glMode);
  28.        
  29.         for (int i = 0; i < absoluteVerts.length / 2; i += 2)
  30.         {
  31.             if (colors == null)
  32.                 GL11.glTexCoord2f(texCoords[i * 2], texCoords[i * 2 + 1]);
  33.             else
  34.                 GL11.glColor3f(colors[i * 3], colors[i * 3 + 1], colors[i * 3 + 2]);
  35.             GL11.glVertex2f(absoluteVerts[i * 2], absoluteVerts[i * 2 + 1]);
  36.         }
  37.        
  38.         GL11.glEnd();
  39.     }
  40.    
  41.     public void drawWithModeBegun()
  42.     {
  43.         for (int i = 0; i < absoluteVerts.length / 2; i += 2)
  44.         {
  45.             if (colors == null)
  46.                 GL11.glTexCoord2f(texCoords[i * 2], texCoords[i * 2 + 1]);
  47.             else
  48.                 GL11.glColor3f(colors[i * 3], colors[i * 3 + 1], colors[i * 3 + 2]);
  49.             GL11.glVertex2f(absoluteVerts[i * 2], absoluteVerts[i * 2 + 1]);
  50.         }
  51.     }
  52.    
  53.    
  54.     public void updateAbsoluteVerticies()
  55.     {
  56.         absoluteVerts = new float[localVerts.length];
  57.        
  58.         for (int i = 0; i < localVerts.length; i += 2)
  59.         {
  60.             absoluteVerts[i] = xPos + localVerts[i];
  61.             absoluteVerts[i + 1] = yPos + localVerts[i + 1];
  62.         }
  63.     }
  64.    
  65.     public void setPosition(float newX, float newY)
  66.     {
  67.         xPos = newX;
  68.         yPos = newY;
  69.     }
  70.    
  71.     public float getXPos()
  72.     {
  73.         return xPos;
  74.     }
  75.    
  76.     public float getYPos()
  77.     {
  78.         return yPos;
  79.     }
  80.    
  81.     public void setScale(float xScale, float yScale)
  82.     {
  83.         Matrix2f trans = getScaleMatrix(xScale, yScale);
  84.        
  85.         for (int i = 0; i < localVerts.length; i += 2)
  86.         {
  87.             Vector2f vec = new Vector2f(localVerts[i], localVerts[i + 1]);
  88.             Vector2f result = Matrix2f.transform(trans, vec, null);
  89.             localVerts[i] = result.x;
  90.             localVerts[i + 1] = result.y;
  91.         }
  92.     }
  93.    
  94.     public void setRotation(float rotation)
  95.     {  
  96.         Matrix2f trans = getRotationMatrix(rotation - this.rotation);
  97.        
  98.         for (int i = 0; i < localVerts.length; i += 2)
  99.         {
  100.             Vector2f vec = new Vector2f(localVerts[i], localVerts[i + 1]);
  101.             Vector2f result = Matrix2f.transform(trans, vec, null);
  102.             localVerts[i] = result.x;
  103.             localVerts[i + 1] = result.y;
  104.         }
  105.         this.rotation = rotation;
  106.     }
  107.    
  108.     public void rotate(float rotationChange)
  109.     {
  110.         Matrix2f trans = getRotationMatrix(rotationChange);
  111.        
  112.         for (int i = 0; i < localVerts.length; i += 2)
  113.         {
  114.             Vector2f vec = new Vector2f(localVerts[i], localVerts[i + 1]);
  115.             Vector2f result = Matrix2f.transform(trans, vec, null);
  116.             localVerts[i] = result.x;
  117.             localVerts[i + 1] = result.y;
  118.         }
  119.         this.rotation += rotationChange;
  120.     }
  121.    
  122.    
  123.     private Matrix2f getRotationMatrix(float rotation)
  124.     {
  125.         float cos = (float)Math.cos(rotation);
  126.         float sin = (float)Math.sin(rotation);
  127.        
  128.         float[] array = {
  129.                 cos, -sin,
  130.                 sin, cos
  131.         };
  132.        
  133.         FloatBuffer buf = BufferUtils.createFloatBuffer(array.length);
  134.         buf.put(array);
  135.         buf.flip();
  136.        
  137.         Matrix2f result = new Matrix2f();
  138.         result.load(buf);
  139.        
  140.         return result;
  141.     }
  142.    
  143.     private Matrix2f getScaleMatrix(float xScale, float yScale)
  144.     {
  145.         float[] array = {
  146.                 xScale, 0,
  147.                 0,      yScale
  148.         };
  149.        
  150.         FloatBuffer buf = BufferUtils.createFloatBuffer(array.length);
  151.         buf.put(array);
  152.         buf.flip();
  153.        
  154.         Matrix2f result = new Matrix2f();
  155.         result.load(buf);
  156.        
  157.         return result;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement