public static Matrix3f rotationMatrix(float angle, float x, float y, float z) { angle *= (float)Math.PI/180f; Vector3f axis = new Vector3f(x,y,z); axis.normalise(); float s = (float)Math.sin(angle); float c = (float)Math.cos(angle); float oc = 1.0f - c; Matrix3f mat = new Matrix3f(); mat.m00 = oc * axis.x * axis.x + c; mat.m01 = oc * axis.x * axis.y - axis.z * s; mat.m02 = oc * axis.z * axis .x + axis.y * s; mat.m10 = oc * axis.x * axis.y + axis.z * s; mat.m11 = oc * axis.y * axis.y + c; mat.m12 = oc * axis.y * axis.z - axis.x * s; mat.m20 = oc * axis.z * axis.x - axis.y * s; mat.m21 = oc * axis.y * axis.z + axis.x * s; mat.m22 = oc * axis.z * axis.z + c; return mat; }
public static Matrix3f lookMatrix(Vector3 forward, Vector3 up) { Vector3 vector = forward.normalized(); Vector3 vector2 = up.cross(vector).normalized(); Vector3 vector3 = vector.cross(vector2); Matrix3f matrix = new Matrix3f(); matrix.m00 = vector2.x; matrix.m01 = vector2.y; matrix.m02 = vector2.z; matrix.m10 = vector3.x; matrix.m11 = vector3.y; matrix.m12 = vector3.z; matrix.m20 = vector.x; matrix.m21 = vector.y; matrix.m22 = vector.z; return matrix; }
@Override public void defineParts() { final Matrix4f rotate = new Matrix4f().rotate((float) Math.toRadians(-90), new Vector3f(1, 0, 0)); bodyPartDefinition = Optional.of(new PartDefinition( new Vector3f(0.0f, 5.0f, 2.0f), Matrix4f.mul( createPartMatrix( new Vector3f(12, 20, 10), new Vector3f(0, -1, -6)), rotate, null), new Matrix3f() )); }
@Override public void defineParts() { final Matrix4f rotate = new Matrix4f().rotate((float) Math.toRadians(-90), new Vector3f(1, 0, 0)); bodyPartDefinition = Optional.of(new PartDefinition( new Vector3f(0.0f, 5.0f, 2.0f), Matrix4f.mul( createPartMatrix( new Vector3f(32, 32, 32), new Vector3f(0, -1, offsetY == null ? 0 : offsetY.get())), rotate, null), new Matrix3f() )); }
/** * Add a BakedQuad to the mesh. It will be processed in batch when the model is rendered for the first time. * @param bakedQuads the list of quads to add to the model */ public void addBakedQuads(Matrix4f positionTransform, Matrix3f textureTransform, Collection<BakedQuad> bakedQuads) { if (bakedQuads != null && bakedQuads.size() > 0) { allBakedQuads.add(new FutureQuad<>(bakedQuads, positionTransform, textureTransform)); } }
public PartDefinition(Vector3f rotationPoint, Matrix4f positionTransform, Matrix3f textureTransform) { this.rotationPoint = rotationPoint; this.positionTransform = positionTransform; this.textureTransform = textureTransform; }
@SideOnly(Side.CLIENT) public static Matrix4f matrix3to4(Matrix3f matrix) { Matrix4f mat = new Matrix4f(); mat.m00 = matrix.m00; mat.m01 = matrix.m01; mat.m02 = matrix.m02; mat.m10 = matrix.m10; mat.m11 = matrix.m11; mat.m12 = matrix.m12; mat.m20 = matrix.m20; mat.m21 = matrix.m21; mat.m22 = matrix.m22; return mat; }
public boolean setUniformMatrix(String key, Matrix matrix) { if (shaderID <= 0 || !shaderActive) { return false; } int width; if (matrix instanceof Matrix2f) width = 2; else if (matrix instanceof Matrix3f) width = 3; else if (matrix instanceof Matrix4f) width = 4; else throw new IllegalArgumentException(); FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(width * width); matrix.store(floatBuffer); floatBuffer.position(0); switch (width) { case 2: OpenGlHelper.glUniformMatrix2(getUniformLocation(key), false, floatBuffer); break; case 3: OpenGlHelper.glUniformMatrix3(getUniformLocation(key), false, floatBuffer); break; default: OpenGlHelper.glUniformMatrix4(getUniformLocation(key), false, floatBuffer); break; } return true; }
public void set(Matrix3f m1){ float ww=0.25f*(m1.m00+m1.m11+m1.m22+1.0f); if(ww>=0){ if(ww>=EPS2){ w=(float)Math.sqrt(ww); ww=0.25f/w; x=(m1.m21-m1.m12)*ww; y=(m1.m02-m1.m20)*ww; z=(m1.m10-m1.m01)*ww; return; } }else{ w=x=y=z=1; return; } w=0; ww=-0.5f*(m1.m11+m1.m22); if(ww>=0){ if(ww>=EPS2){ x=(float)Math.sqrt(ww); ww=0.5f/x; y=m1.m10*ww; z=m1.m20*ww; return; } }else{ x=0; y=0; z=1; return; } x=0; ww=0.5f*(1.0f-m1.m22); if(ww>=EPS2){ y=(float)Math.sqrt(ww); z=m1.m21/(2.0f*y); return; } y=0; z=1; }
@Override public ModelQuadruped createModel(ItemStack itemStack, EntityLivingBase entity) { ModelQuadruped quadruped = super.createModel(itemStack, entity); quadruped.body = new ModelRenderer(new ModelSheep1(), 0, 0); quadruped.head = createModelRenderer(getTransforms().getHeadPartDefinition().get()); final EntityMesh mesh = new EntityMesh(quadruped.head); quadruped.head.cubeList.add(mesh); final Matrix4f antenna1Matrix = new Matrix4f(); antenna1Matrix.translate(new Vector3f(1, -3, -4)); antenna1Matrix.scale(new Vector3f(20, -20, 20)); antenna1Matrix.rotate(fifteenDegrees, new Vector3f(1, 0, 0)); antenna1Matrix.rotate(-thirtyDegrees, new Vector3f(0, 0, 1)); antenna1Matrix.rotate(-nintyDegrees, new Vector3f(0, 1, 0)); antenna1Matrix.translate(new Vector3f(-0.5f, -0.5f, -0.5f)); final IBakedModel bakedModelForItem = getBakedModelForItem(itemStack, entity); addBlockModelToEntityMesh(mesh, new PartDefinition( new Vector3f(0, 0, 0), antenna1Matrix, new Matrix3f()), null, bakedModelForItem ); final Matrix4f antenna2Matrix = new Matrix4f(); antenna2Matrix.translate(new Vector3f(-1, -3, -4)); antenna2Matrix.scale(new Vector3f(20, -20, 20)); antenna2Matrix.rotate(fifteenDegrees, new Vector3f(1, 0, 0)); antenna2Matrix.rotate(thirtyDegrees, new Vector3f(0, 0, 1)); antenna2Matrix.rotate(nintyDegrees, new Vector3f(0, 1, 0)); antenna2Matrix.translate(new Vector3f(-0.5f, -0.5f, -0.5f)); addBlockModelToEntityMesh(mesh, new PartDefinition( new Vector3f(0, 0, 0), antenna2Matrix, new Matrix3f()), null, bakedModelForItem ); return quadruped; }
@Override public ModelQuadruped createModel(ItemStack itemStack, EntityLivingBase entity) { ModelQuadruped quadruped = super.createModel(itemStack, entity); final float nintyDegrees = (float) Math.toRadians(90); quadruped.body = new ModelRenderer(new ModelSheep1(), 0, 0); quadruped.head = createModelRenderer(getTransforms().getHeadPartDefinition().get()); final EntityMesh mesh = new EntityMesh(quadruped.head); quadruped.head.cubeList.add(mesh); final Matrix4f antenna1Matrix = new Matrix4f(); antenna1Matrix.translate(new Vector3f(8, 0, -2)); antenna1Matrix.scale(new Vector3f(20, -20, 20)); antenna1Matrix.rotate(-nintyDegrees, new Vector3f(0, 1, 0)); antenna1Matrix.translate(new Vector3f(-0.5f, -0.5f, -0.5f)); final IBakedModel bakedModelForItem = getBakedModelForItem(itemStack, entity); addBlockModelToEntityMesh(mesh, new PartDefinition( new Vector3f(0, 0, 0), antenna1Matrix, new Matrix3f()), null, bakedModelForItem ); final Matrix4f antenna2Matrix = new Matrix4f(); antenna2Matrix.translate(new Vector3f(-8, 0, -2)); antenna2Matrix.scale(new Vector3f(20, -20, 20)); antenna2Matrix.rotate(nintyDegrees, new Vector3f(0, 1, 0)); antenna2Matrix.translate(new Vector3f(-0.5f, -0.5f, -0.5f)); addBlockModelToEntityMesh(mesh, new PartDefinition( new Vector3f(0, 0, 0), antenna2Matrix, new Matrix3f()), null, bakedModelForItem ); return quadruped; }
public void addTexturedQuads(Matrix4f positionTransform, Matrix3f textureTransform, Collection<TexturedQuad> texturedQuads) { if (texturedQuads != null && texturedQuads.size() > 0) { allTexturedQuads.add(new FutureQuad<>(texturedQuads, positionTransform, textureTransform)); } }
public void addTexturedQuads(Matrix4f positionTransform, Matrix3f textureTransform, TexturedQuad... texturedQuads) { if (texturedQuads != null && texturedQuads.length > 0) { allTexturedQuads.add(new FutureQuad<>(Lists.newArrayList(texturedQuads), positionTransform, textureTransform)); } }
@SuppressWarnings("ObjectAllocationInLoop") @Override @SideOnly(Side.CLIENT) public void render(VertexBuffer renderer, float scale) { if (quadList == null) { final List<TexturedQuad> outputQuads = Lists.newArrayList(); for (final FutureQuad<TexturedQuad> texturedQuads : allTexturedQuads) { for (final TexturedQuad texturedQuad : texturedQuads.quads) { final PositionTextureVertex[] newPositions = new PositionTextureVertex[4]; for (int i = 0; i < texturedQuad.vertexPositions.length; i++) { final PositionTextureVertex vertexPosition = texturedQuad.vertexPositions[i]; @SuppressWarnings("NumericCastThatLosesPrecision") final Vector4f position = new Vector4f((float) vertexPosition.vector3D.xCoord, (float) vertexPosition.vector3D.yCoord, (float) vertexPosition.vector3D.zCoord, 1); final Vector3f textureCoords = new Vector3f( vertexPosition.texturePositionX, vertexPosition.texturePositionY, 1); final Vector4f transformedPosition = Matrix4f.transform(texturedQuads.positionTransform, position, null); final Vector3f transformedTexture = Matrix3f.transform(texturedQuads.textureTransform, textureCoords, null); newPositions[i] = new PositionTextureVertex( transformedPosition.getX(), transformedPosition.getY(), transformedPosition.getZ(), transformedTexture.getX(), transformedTexture.getY() ); } outputQuads.add(new TexturedQuad(newPositions)); } } for (final FutureQuad<BakedQuad> bakedQuads : allBakedQuads) { for (final BakedQuad bakedQuad : bakedQuads.quads) { try { final VertexConsumer consumer = new VertexConsumer(bakedQuad.getFormat(), bakedQuads.positionTransform, bakedQuads.textureTransform); bakedQuad.pipe(consumer); outputQuads.add(consumer.getOutputQuad()); } catch (Exception e) { if (!errored) { LogManager.getLogger("ShearMadnessAPI").log(Level.ERROR, "Error creating chiseled model", e); } errored = true; } } } quadList = new TexturedQuad[outputQuads.size()]; quadList = outputQuads.toArray(quadList); } for (final TexturedQuad texturedquad : quadList) { texturedquad.draw(renderer, scale); } }
FutureQuad(Collection<T> quads, Matrix4f positionTransform, Matrix3f textureTransform) { this.quads = quads; this.positionTransform = positionTransform; this.textureTransform = textureTransform; }
VertexConsumer(VertexFormat vertexFormat, Matrix4f positionTransform, Matrix3f textureTransform) { this.vertexFormat = vertexFormat; this.positionTransform = positionTransform; this.textureTransform = textureTransform; }
@Override public void put(int element, float... data) { if (element == 0) { ++currentVertexIndex; } final VertexFormatElement element1 = vertexFormat.getElement(element); switch (element1.getUsage()) { case POSITION: if (data.length < 3) { currentPosition = null; return; } final Vector4f position = new Vector4f(data[0], data[1], data[2], 1); final Vector4f transform = Matrix4f.transform(positionTransform, position, null); currentPosition = new Vec3d(transform.x, transform.y, transform.z); break; case UV: if (data.length < 2) { currentTexture = null; return; } final Vector3f uvs = new Vector3f(data[0], data[1], 1); final Vector3f transformedTexture = Matrix3f.transform(textureTransform, uvs, null); currentTexture = new Vector3f(transformedTexture.x, transformedTexture.y, transformedTexture.z); break; default: break; } if (element == vertexFormat.getElementCount() - 1) { if (currentPosition == null || currentTexture == null) { String vertexInfo = ""; for (final VertexFormatElement vertexFormatElement : vertexFormat.getElements()) { vertexInfo += String.format("Element %d - %s\n", vertexFormatElement.getIndex(), vertexFormatElement.getUsage()); } throw new RuntimeException("Unexpected Vertex Format:\n" + vertexInfo); } vertices[currentVertexIndex] = new PositionTextureVertex(currentPosition, currentTexture.x, currentTexture.y); currentPosition = null; currentTexture = null; } }
public Matrix3f getTextureTransform() { return textureTransform; }
public void setTextureTransform(Matrix3f textureTransform) { this.textureTransform = textureTransform; }
public void defineParts() { final Matrix4f rotate = new Matrix4f().rotate((float) Math.toRadians(-90), new Vector3f(1, 0, 0)); bodyPartDefinition = Optional.of(new PartDefinition( new Vector3f(0.0f, 5.0f, 2.0f), Matrix4f.mul( createPartMatrix( new Vector3f(12, 20, 10), new Vector3f(0, -2, -14)), rotate, null), new Matrix3f() )); headPartDefinition = Optional.of(new PartDefinition( new Vector3f(0.0f, 6.0f, -8.0f), createPartMatrix( new Vector3f(8, 8, 8), new Vector3f(0, -1, -1)), new Matrix3f() )); leg1PartDefinition = Optional.of(new PartDefinition( new Vector3f(-3.0f, 12.0f, 7.0f), createPartMatrix( new Vector3f(5.6f, 7.4f, 5.6f), new Vector3f(0, 3, 0.1f)), new Matrix3f() )); leg2PartDefinition = Optional.of(new PartDefinition( new Vector3f(3.0f, 12.0f, 7.0f), createPartMatrix( new Vector3f(5.6f, 7.4f, 5.6f), new Vector3f(0, 3, 0.1f)), new Matrix3f() )); leg3PartDefinition = Optional.of(new PartDefinition( new Vector3f(-3.0f, 12.0f, -5.0f), createPartMatrix( new Vector3f(5.6f, 7.4f, 5.6f), new Vector3f(0, 3, 0.1f)), new Matrix3f() )); leg4PartDefinition = Optional.of(new PartDefinition( new Vector3f(3.0f, 12.0f, -5.0f), createPartMatrix( new Vector3f(5.6f, 7.4f, 5.6f), new Vector3f(0, 3, 0.1f)), new Matrix3f() )); }
public Vector3 multiply(Matrix3f matrix) { float newX = matrix.m00 * x + matrix.m01 * y + matrix.m02 * z; float newY = matrix.m10 * x + matrix.m11 * y + matrix.m12 * z; float newZ = matrix.m20 * x + matrix.m21 * y + matrix.m22 * z; return new Vector3(newX, newY, newZ); }
public Quaternion(Matrix3f matrix) { this(matrix.m00, matrix.m01, matrix.m02, matrix.m10, matrix.m11, matrix.m12, matrix.m20, matrix.m21, matrix.m22); }
public void set(Matrix3f m) { m.store(matrixBuffer); matrixBuffer.rewind(); }
/** * * @param angle (in degrees) * @param x * @param y * @param z */ public void rotate(float angle, float x, float y, float z){ Matrix3f rotationMatrix = rotationMatrix(angle, x, y, z); for (Vertex vertex : vertices){ Matrix3f.transform(rotationMatrix, vertex.position, vertex.position); } }