use of com.badlogic.gdx.math.Affine2 in project libgdx by libgdx.
the class CpuSpriteBatch method drawAdjustedUV.
private void drawAdjustedUV(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, float u, float v, float u2, float v2, boolean flipX, boolean flipY) {
if (!drawing)
throw new IllegalStateException("CpuSpriteBatch.begin must be called before draw.");
if (texture != lastTexture)
switchTexture(texture);
else if (idx == vertices.length)
super.flush();
// bottom left and top right corner points relative to origin
final float worldOriginX = x + originX;
final float worldOriginY = y + originY;
float fx = -originX;
float fy = -originY;
float fx2 = width - originX;
float fy2 = height - originY;
// scale
if (scaleX != 1 || scaleY != 1) {
fx *= scaleX;
fy *= scaleY;
fx2 *= scaleX;
fy2 *= scaleY;
}
// construct corner points, start from top left and go counter clockwise
final float p1x = fx;
final float p1y = fy;
final float p2x = fx;
final float p2y = fy2;
final float p3x = fx2;
final float p3y = fy2;
final float p4x = fx2;
final float p4y = fy;
float x1;
float y1;
float x2;
float y2;
float x3;
float y3;
float x4;
float y4;
// rotate
if (rotation != 0) {
final float cos = MathUtils.cosDeg(rotation);
final float sin = MathUtils.sinDeg(rotation);
x1 = cos * p1x - sin * p1y;
y1 = sin * p1x + cos * p1y;
x2 = cos * p2x - sin * p2y;
y2 = sin * p2x + cos * p2y;
x3 = cos * p3x - sin * p3y;
y3 = sin * p3x + cos * p3y;
x4 = x1 + (x3 - x2);
y4 = y3 - (y2 - y1);
} else {
x1 = p1x;
y1 = p1y;
x2 = p2x;
y2 = p2y;
x3 = p3x;
y3 = p3y;
x4 = p4x;
y4 = p4y;
}
x1 += worldOriginX;
y1 += worldOriginY;
x2 += worldOriginX;
y2 += worldOriginY;
x3 += worldOriginX;
y3 += worldOriginY;
x4 += worldOriginX;
y4 += worldOriginY;
if (flipX) {
float tmp = u;
u = u2;
u2 = tmp;
}
if (flipY) {
float tmp = v;
v = v2;
v2 = tmp;
}
Affine2 t = adjustAffine;
vertices[idx + 0] = t.m00 * x1 + t.m01 * y1 + t.m02;
vertices[idx + 1] = t.m10 * x1 + t.m11 * y1 + t.m12;
vertices[idx + 2] = color;
vertices[idx + 3] = u;
vertices[idx + 4] = v;
vertices[idx + 5] = t.m00 * x2 + t.m01 * y2 + t.m02;
vertices[idx + 6] = t.m10 * x2 + t.m11 * y2 + t.m12;
vertices[idx + 7] = color;
vertices[idx + 8] = u;
vertices[idx + 9] = v2;
vertices[idx + 10] = t.m00 * x3 + t.m01 * y3 + t.m02;
vertices[idx + 11] = t.m10 * x3 + t.m11 * y3 + t.m12;
vertices[idx + 12] = color;
vertices[idx + 13] = u2;
vertices[idx + 14] = v2;
vertices[idx + 15] = t.m00 * x4 + t.m01 * y4 + t.m02;
vertices[idx + 16] = t.m10 * x4 + t.m11 * y4 + t.m12;
vertices[idx + 17] = color;
vertices[idx + 18] = u2;
vertices[idx + 19] = v;
idx += Sprite.SPRITE_SIZE;
}
use of com.badlogic.gdx.math.Affine2 in project libgdx by libgdx.
the class CpuSpriteBatch method drawAdjusted.
private void drawAdjusted(TextureRegion region, float width, float height, Affine2 transform) {
if (!drawing)
throw new IllegalStateException("CpuSpriteBatch.begin must be called before draw.");
if (region.texture != lastTexture)
switchTexture(region.texture);
else if (idx == vertices.length)
super.flush();
Affine2 t = transform;
// construct corner points
float x1 = t.m02;
float y1 = t.m12;
float x2 = t.m01 * height + t.m02;
float y2 = t.m11 * height + t.m12;
float x3 = t.m00 * width + t.m01 * height + t.m02;
float y3 = t.m10 * width + t.m11 * height + t.m12;
float x4 = t.m00 * width + t.m02;
float y4 = t.m10 * width + t.m12;
// v must be flipped
float u = region.u;
float v = region.v2;
float u2 = region.u2;
float v2 = region.v;
t = adjustAffine;
vertices[idx + 0] = t.m00 * x1 + t.m01 * y1 + t.m02;
vertices[idx + 1] = t.m10 * x1 + t.m11 * y1 + t.m12;
vertices[idx + 2] = color;
vertices[idx + 3] = u;
vertices[idx + 4] = v;
vertices[idx + 5] = t.m00 * x2 + t.m01 * y2 + t.m02;
vertices[idx + 6] = t.m10 * x2 + t.m11 * y2 + t.m12;
vertices[idx + 7] = color;
vertices[idx + 8] = u;
vertices[idx + 9] = v2;
vertices[idx + 10] = t.m00 * x3 + t.m01 * y3 + t.m02;
vertices[idx + 11] = t.m10 * x3 + t.m11 * y3 + t.m12;
vertices[idx + 12] = color;
vertices[idx + 13] = u2;
vertices[idx + 14] = v2;
vertices[idx + 15] = t.m00 * x4 + t.m01 * y4 + t.m02;
vertices[idx + 16] = t.m10 * x4 + t.m11 * y4 + t.m12;
vertices[idx + 17] = color;
vertices[idx + 18] = u2;
vertices[idx + 19] = v;
idx += Sprite.SPRITE_SIZE;
}
use of com.badlogic.gdx.math.Affine2 in project libgdx by libgdx.
the class CpuSpriteBatch method drawAdjusted.
private void drawAdjusted(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise) {
if (!drawing)
throw new IllegalStateException("CpuSpriteBatch.begin must be called before draw.");
if (region.texture != lastTexture)
switchTexture(region.texture);
else if (idx == vertices.length)
super.flush();
// bottom left and top right corner points relative to origin
final float worldOriginX = x + originX;
final float worldOriginY = y + originY;
float fx = -originX;
float fy = -originY;
float fx2 = width - originX;
float fy2 = height - originY;
// scale
if (scaleX != 1 || scaleY != 1) {
fx *= scaleX;
fy *= scaleY;
fx2 *= scaleX;
fy2 *= scaleY;
}
// construct corner points, start from top left and go counter clockwise
final float p1x = fx;
final float p1y = fy;
final float p2x = fx;
final float p2y = fy2;
final float p3x = fx2;
final float p3y = fy2;
final float p4x = fx2;
final float p4y = fy;
float x1;
float y1;
float x2;
float y2;
float x3;
float y3;
float x4;
float y4;
// rotate
if (rotation != 0) {
final float cos = MathUtils.cosDeg(rotation);
final float sin = MathUtils.sinDeg(rotation);
x1 = cos * p1x - sin * p1y;
y1 = sin * p1x + cos * p1y;
x2 = cos * p2x - sin * p2y;
y2 = sin * p2x + cos * p2y;
x3 = cos * p3x - sin * p3y;
y3 = sin * p3x + cos * p3y;
x4 = x1 + (x3 - x2);
y4 = y3 - (y2 - y1);
} else {
x1 = p1x;
y1 = p1y;
x2 = p2x;
y2 = p2y;
x3 = p3x;
y3 = p3y;
x4 = p4x;
y4 = p4y;
}
x1 += worldOriginX;
y1 += worldOriginY;
x2 += worldOriginX;
y2 += worldOriginY;
x3 += worldOriginX;
y3 += worldOriginY;
x4 += worldOriginX;
y4 += worldOriginY;
float u1, v1, u2, v2, u3, v3, u4, v4;
if (clockwise) {
u1 = region.u2;
v1 = region.v2;
u2 = region.u;
v2 = region.v2;
u3 = region.u;
v3 = region.v;
u4 = region.u2;
v4 = region.v;
} else {
u1 = region.u;
v1 = region.v;
u2 = region.u2;
v2 = region.v;
u3 = region.u2;
v3 = region.v2;
u4 = region.u;
v4 = region.v2;
}
Affine2 t = adjustAffine;
vertices[idx + 0] = t.m00 * x1 + t.m01 * y1 + t.m02;
vertices[idx + 1] = t.m10 * x1 + t.m11 * y1 + t.m12;
vertices[idx + 2] = color;
vertices[idx + 3] = u1;
vertices[idx + 4] = v1;
vertices[idx + 5] = t.m00 * x2 + t.m01 * y2 + t.m02;
vertices[idx + 6] = t.m10 * x2 + t.m11 * y2 + t.m12;
vertices[idx + 7] = color;
vertices[idx + 8] = u2;
vertices[idx + 9] = v2;
vertices[idx + 10] = t.m00 * x3 + t.m01 * y3 + t.m02;
vertices[idx + 11] = t.m10 * x3 + t.m11 * y3 + t.m12;
vertices[idx + 12] = color;
vertices[idx + 13] = u3;
vertices[idx + 14] = v3;
vertices[idx + 15] = t.m00 * x4 + t.m01 * y4 + t.m02;
vertices[idx + 16] = t.m10 * x4 + t.m11 * y4 + t.m12;
vertices[idx + 17] = color;
vertices[idx + 18] = u4;
vertices[idx + 19] = v4;
idx += Sprite.SPRITE_SIZE;
}
use of com.badlogic.gdx.math.Affine2 in project libgdx by libgdx.
the class Affine2Test method create.
@Override
public void create() {
Vector2 trn = new Vector2(30, 50);
float rot = 35;
float cos = (float) Math.cos(MathUtils.degreesToRadians * rot);
float sin = (float) Math.sin(MathUtils.degreesToRadians * rot);
Vector2 scl = new Vector2(0.42f, 1.19f);
Vector2 shear = new Vector2(0.35f, 0.71f);
Matrix3 mat1 = new Matrix3();
Matrix3 mat2 = new Matrix3();
Affine2 afn1 = new Affine2();
Affine2 afn2 = new Affine2();
// check setter - identity
checkEqual(mat1, new float[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 });
checkEqual(mat1, mat2.idt());
checkEqual(mat1, afn1);
checkEqual(mat1, afn1.idt());
// check setter - translation
mat1.setToTranslation(trn);
checkEqual(mat1, new float[] { 1, 0, 0, 0, 1, 0, trn.x, trn.y, 1 });
afn1.setToTranslation(trn);
checkEqual(mat1, afn1);
// check setter - scale
mat1.setToScaling(scl);
checkEqual(mat1, new float[] { scl.x, 0, 0, 0, scl.y, 0, 0, 0, 1 });
afn1.setToScaling(scl);
checkEqual(mat1, afn1);
// check setter - rotation
mat1.setToRotation(rot);
checkEqual(mat1, new float[] { cos, sin, 0, -sin, cos, 0, 0, 0, 1 });
afn1.setToRotation(rot);
checkEqual(mat1, afn1);
mat1.setToRotationRad(MathUtils.degreesToRadians * rot);
checkEqual(mat1, afn1);
afn1.setToRotationRad(MathUtils.degreesToRadians * rot);
checkEqual(mat1, afn1);
// check setter - shearing
afn1.setToShearing(shear);
checkEqual(mat1.set(afn1), new float[] { 1, shear.y, 0, shear.x, 1, 0, 0, 0, 1 });
// check setter - translation x rotation x scale
afn1.setToTrnRotScl(trn, rot, scl);
afn2.setToTrnRotRadScl(trn, MathUtils.degreesToRadians * rot, scl);
checkEqual(afn1, afn2);
afn2.setToTranslation(trn).rotate(rot).scale(scl);
checkEqual(afn1, afn2);
// check setter - translation x scale
afn1.setToTrnRotScl(trn, 0, scl);
afn2.setToTrnScl(trn, scl);
checkEqual(afn1, afn2);
// check post-multiplication
mat1.idt().scale(scl).rotate(rot).translate(trn).mul(mat2.set(afn2.setToShearing(shear)));
afn1.idt().scale(scl).rotate(rot).translate(trn).shear(shear);
checkEqual(mat1, afn1);
afn1.idt().mul(afn2.setToScaling(scl)).mul(afn2.setToRotation(rot)).mul(afn2.setToTranslation(trn)).mul(afn2.setToShearing(shear));
checkEqual(mat1, afn1);
// check pre-multiplication
afn1.idt().preShear(shear).preTranslate(trn).preRotate(rot).preScale(scl);
checkEqual(mat1, afn1);
afn1.idt().preMul(afn2.setToShearing(shear)).preMul(afn2.setToTranslation(trn)).preMul(afn2.setToRotation(rot)).preMul(afn2.setToScaling(scl));
checkEqual(mat1, afn1);
mat1.set(afn2.setToShearing(shear)).trn(trn).mulLeft(mat2.setToRotation(rot)).mulLeft(mat2.setToScaling(scl));
checkEqual(mat1, afn1);
// check determinant and inverse
checkEqual(mat1.det(), afn1.det());
check(afn1.det() == (afn1.m00 * afn1.m11 - afn1.m01 * afn1.m10));
mat1.inv();
afn2.set(afn1).inv();
checkEqual(mat1, afn2);
checkEqual(afn1.det(), 1 / afn2.det());
// check for exception when trying to invert singular matrices
boolean didThrow = false;
afn1.setToShearing(1, 1);
try {
afn1.inv();
} catch (GdxRuntimeException e) {
didThrow = true;
}
check(didThrow);
System.out.println("All tests passed.");
}
use of com.badlogic.gdx.math.Affine2 in project libgdx by libgdx.
the class CpuSpriteBatch method drawAdjusted.
private void drawAdjusted(Texture texture, float[] spriteVertices, int offset, int count) {
if (!drawing)
throw new IllegalStateException("CpuSpriteBatch.begin must be called before draw.");
if (texture != lastTexture)
switchTexture(texture);
Affine2 t = adjustAffine;
int copyCount = Math.min(vertices.length - idx, count);
do {
count -= copyCount;
while (copyCount > 0) {
float x = spriteVertices[offset];
float y = spriteVertices[offset + 1];
// x
vertices[idx] = t.m00 * x + t.m01 * y + t.m02;
// y
vertices[idx + 1] = t.m10 * x + t.m11 * y + t.m12;
// color
vertices[idx + 2] = spriteVertices[offset + 2];
// u
vertices[idx + 3] = spriteVertices[offset + 3];
// v
vertices[idx + 4] = spriteVertices[offset + 4];
idx += Sprite.VERTEX_SIZE;
offset += Sprite.VERTEX_SIZE;
copyCount -= Sprite.VERTEX_SIZE;
}
if (count > 0) {
super.flush();
copyCount = Math.min(vertices.length, count);
}
} while (count > 0);
}
Aggregations