package com.cgvsu.render_engine; import com.cgvsu.math.Matrix4f; import…
- User
package com.cgvsu.render_engine;
import com.cgvsu.math.Matrix4f; import com.cgvsu.math.Point2f; import com.cgvsu.math.Vector3f; import com.cgvsu.math.Vector4f;
import static java.lang.Math.*;
public class GraphicConveyor {
/**
-
Аффинные преобразования
-
Ненавижу матричную логику...
-
@return {@code Matrix4f} - матрица из локальной системы координат в мировую */ public static Matrix4f scaleRotateTranslate(Vector3f rotate, Vector3f scale, Vector3f translate) { Matrix4f r = calculateRotate(rotate.getX(), rotate.getY(), rotate.getZ()); Matrix4f s = calculateScale(scale.getX(), scale.getY(), scale.getZ()); Matrix4f t = calculateTranslate(new Vector3f(translate.getX(), translate.getY(), translate.getZ()));
t.multiply(r); t.multiply(s);
return new Matrix4f(t); }
/**
- Аффинные преобразования - увеличение/уменьшение объекта
- @param vector3f вектор переноса
- @return {@code Matrix4f} - матрица переноса */
public static Matrix4f calculateTranslate(Vector3f vector3f) { return calculateTranslate(new Vector4f(vector3f.getX(), vector3f.getY(), vector3f.getZ(), 1)); }
private static Matrix4f calculateTranslate(Vector4f vector) { Matrix4f res = new Matrix4f(1); for (int i = 0; i < res.getElements().length - 1; i++) { res.setElement(i, 3, vector.getNum(i)); } return res; }
public static Matrix4f Rotate(float alpha, float beta, float gamma) { return calculateRotate(alpha, beta, gamma); }
// мб потом почитаю что такое кватернион и немного переделаю код, пока пусть так будет private static Matrix4f calculateRotate(float alpha, float beta, float gamma) { // вокруг x float[] rotateX = new float[]{ 1, 0, 0, 0, 0, (float) cos(alpha), (float) sin(alpha), 0, 0, (float) -sin(alpha), (float) cos(alpha), 0, 0, 0, 0, 1 }; // вокруг y float[] rotateY = new float[]{ (float) cos(beta), 0, (float) sin(beta), 0, 0, 1, 0, 0, (float) -sin(beta), 0, (float) cos(beta), 0, 0, 0, 0, 1 }; // вокруг z float[] rotateZ = new float[]{ (float) cos(gamma), (float) sin(gamma), 0, 0, (float) -sin(gamma), (float) cos(gamma), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
codeMatrix4f rotateAboutX = new Matrix4f(rotateX); Matrix4f rotateAboutY = new Matrix4f(rotateY); Matrix4f rotateAboutZ = new Matrix4f(rotateZ); // сначала крутим вокруг x, потом y, потом z (да, в реале умножаем сначала Z, потом ыгрик, потом х (а) - плохо) rotateAboutZ.multiply(rotateAboutY); rotateAboutZ.multiply(rotateAboutX); return rotateAboutZ; -
- Assistant