Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package primitives;
- import java.nio.FloatBuffer;
- import java.util.Arrays;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.util.vector.Matrix2f;
- import org.lwjgl.util.vector.Vector2f;
- public class Shape2D
- {
- protected float xPos;
- protected float yPos;
- protected float[] localVerts;
- protected float[] absoluteVerts;
- protected float[] texCoords = null;
- protected float[] colors = null;
- protected float rotation;
- public void draw(int glMode)
- {
- GL11.glBegin(glMode);
- for (int i = 0; i < absoluteVerts.length / 2; i += 2)
- {
- if (colors == null)
- GL11.glTexCoord2f(texCoords[i * 2], texCoords[i * 2 + 1]);
- else
- GL11.glColor3f(colors[i * 3], colors[i * 3 + 1], colors[i * 3 + 2]);
- GL11.glVertex2f(absoluteVerts[i * 2], absoluteVerts[i * 2 + 1]);
- }
- GL11.glEnd();
- }
- public void drawWithModeBegun()
- {
- for (int i = 0; i < absoluteVerts.length / 2; i += 2)
- {
- if (colors == null)
- GL11.glTexCoord2f(texCoords[i * 2], texCoords[i * 2 + 1]);
- else
- GL11.glColor3f(colors[i * 3], colors[i * 3 + 1], colors[i * 3 + 2]);
- GL11.glVertex2f(absoluteVerts[i * 2], absoluteVerts[i * 2 + 1]);
- }
- }
- public void updateAbsoluteVerticies()
- {
- absoluteVerts = new float[localVerts.length];
- for (int i = 0; i < localVerts.length; i += 2)
- {
- absoluteVerts[i] = xPos + localVerts[i];
- absoluteVerts[i + 1] = yPos + localVerts[i + 1];
- }
- }
- public void setPosition(float newX, float newY)
- {
- xPos = newX;
- yPos = newY;
- }
- public float getXPos()
- {
- return xPos;
- }
- public float getYPos()
- {
- return yPos;
- }
- public void setScale(float xScale, float yScale)
- {
- Matrix2f trans = getScaleMatrix(xScale, yScale);
- for (int i = 0; i < localVerts.length; i += 2)
- {
- Vector2f vec = new Vector2f(localVerts[i], localVerts[i + 1]);
- Vector2f result = Matrix2f.transform(trans, vec, null);
- localVerts[i] = result.x;
- localVerts[i + 1] = result.y;
- }
- }
- public void setRotation(float rotation)
- {
- Matrix2f trans = getRotationMatrix(rotation - this.rotation);
- for (int i = 0; i < localVerts.length; i += 2)
- {
- Vector2f vec = new Vector2f(localVerts[i], localVerts[i + 1]);
- Vector2f result = Matrix2f.transform(trans, vec, null);
- localVerts[i] = result.x;
- localVerts[i + 1] = result.y;
- }
- this.rotation = rotation;
- }
- public void rotate(float rotationChange)
- {
- Matrix2f trans = getRotationMatrix(rotationChange);
- for (int i = 0; i < localVerts.length; i += 2)
- {
- Vector2f vec = new Vector2f(localVerts[i], localVerts[i + 1]);
- Vector2f result = Matrix2f.transform(trans, vec, null);
- localVerts[i] = result.x;
- localVerts[i + 1] = result.y;
- }
- this.rotation += rotationChange;
- }
- private Matrix2f getRotationMatrix(float rotation)
- {
- float cos = (float)Math.cos(rotation);
- float sin = (float)Math.sin(rotation);
- float[] array = {
- cos, -sin,
- sin, cos
- };
- FloatBuffer buf = BufferUtils.createFloatBuffer(array.length);
- buf.put(array);
- buf.flip();
- Matrix2f result = new Matrix2f();
- result.load(buf);
- return result;
- }
- private Matrix2f getScaleMatrix(float xScale, float yScale)
- {
- float[] array = {
- xScale, 0,
- 0, yScale
- };
- FloatBuffer buf = BufferUtils.createFloatBuffer(array.length);
- buf.put(array);
- buf.flip();
- Matrix2f result = new Matrix2f();
- result.load(buf);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement