Java 用JOGL加密

Java 用JOGL加密,java,opengl,homography,projection-matrix,Java,Opengl,Homography,Projection Matrix,我正在努力基于显示器的4个角应用投影变换,如下所示: 我的见解是通过首先计算透视变换,然后将矩阵乘以投影变换,来调整投影矩阵 由于这个原因,我能够找到投影变换矩阵,但我得到的结果不是我期望的结果 以下是我的一些代码: public void reshape(final GLAutoDrawable drawable, final int arg1, final int arg2, final int width, final int height) {

我正在努力基于显示器的4个角应用投影变换,如下所示:

我的见解是通过首先计算透视变换,然后将矩阵乘以投影变换,来调整投影矩阵

由于这个原因,我能够找到投影变换矩阵,但我得到的结果不是我期望的结果

以下是我的一些代码:

    public void reshape(final GLAutoDrawable drawable, final int arg1, final int arg2, final int width,
            final int height) {
        // Get the OpenGL graphics context
        if (width <= 0 || height <= 0) {
            return;
        }
        final GL2 gl = drawable.getContext().getGL().getGL2();
        // Set the viewport (display area) to cover the entire window
        gl.glViewport(0, 0, width, height);
        // Enable the model view - any new transformations will affect the
        // model-view matrix
        gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
        gl.glLoadIdentity(); // reset
        // perspective view
        gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
        gl.glLoadIdentity();
        // Only if zoomFit... camera.resetCamera(data.getEnvWidth(),
        // data.getEnvHeight(), data.isOutput3D());
        updatePerspective(gl);
    }

    public final void updatePerspective(final GL2 gl) {
        final int height = getDrawable().getSurfaceHeight();
        final double aspect = (double) getDrawable().getSurfaceWidth() / (double) (height == 0 ? 1 : height);

        final double maxDim = getMaxEnvDim();

        if (!data.isOrtho()) {
            try {
                final double zNear = maxDim / 1000;
                double fW, fH;
                // final double fovY = 45.0d;
                final double fovY = this.data.getCameralens();
                if (aspect > 1.0) {
                    fH = FastMath.tan(fovY / 360 * Math.PI) * zNear;
                    fW = fH * aspect;
                } else {
                    fW = FastMath.tan(fovY / 360 * Math.PI) * zNear;
                    fH = fW / aspect;
                }
                gl.glFrustum(-fW, fW, -fH, fH, zNear, maxDim * 10);
            } catch (final BufferOverflowException e) {
                System.out.println("Buffer overflow exception");
            }
        } else {
            if (aspect >= 1.0) {
                ((GL2ES1) gl).glOrtho(-maxDim * aspect, maxDim * aspect, -maxDim, maxDim, maxDim * 10, -maxDim * 10);
            } else {
                ((GL2ES1) gl).glOrtho(-maxDim, maxDim, -maxDim / aspect, maxDim / aspect, maxDim, -maxDim);
            }
            gl.glTranslated(0d, 0d, maxDim * 0.05);
        }

        // keystoning
        double[][] srcPoints = {
                {0,0,0},
                {1,0,0},
                {1,1,0},
                {0,1,0}
        };
        double[][] dstPoints = {
                {X1,Y1,0},
                {X2,Y2,0},
                {X3,Y3,0},
                {X4,Y4,0}
        };
        double[] keystoneMatrix = getDoubleArray(getHomography(srcPoints, dstPoints));
        gl.glMultMatrixd(keystoneMatrix,0);

        camera.animate();
    }

public static Matrix4d getHomography(double[][] srcPoints, double[][] dstPoints) {

        double sx1 = srcPoints[0][0];
        double sy1 = srcPoints[0][1];
        double sx2 = srcPoints[1][0];
        double sy2 = srcPoints[1][1];
        double sx3 = srcPoints[2][0];
        double sy3 = srcPoints[2][1];
        double sx4 = srcPoints[3][0];
        double sy4 = srcPoints[3][1];

        double dx1 = dstPoints[0][0];
        double dy1 = dstPoints[0][1];
        double dx2 = dstPoints[1][0];
        double dy2 = dstPoints[1][1];
        double dx3 = dstPoints[2][0];
        double dy3 = dstPoints[2][1];
        double dx4 = dstPoints[3][0];
        double dy4 = dstPoints[3][1];

        // 1) resolve the following equation for src and dst :
        //      | x1  x2  x3 |   | lamda |   | x4 |
        //      | y1  y2  y3 | . |  mu   | = | y4 |
        //      | 1   1   1  |   |  to   |   | 1  |

        // for src :

        double src_to = ( (sx4 - sx1)*(sy2 - sy1) - (sy4 - sy1)*(sx2 - sx1) ) /
                ( (sx3 - sx1)*(sy2 - sy1) - (sy3 - sy1)*(sx2 - sx1) );
        double src_mu = (sx4 - sx1 - src_to*(sx3 - sx1)) / (sx2 - sx1);
        double src_lamda = 1 - src_mu - src_to;     

        // for dst :

        double dst_to = ( (dx4 - dx1)*(dy2 - dy1) - (dy4 - dy1)*(dx2 - dx1) ) /
                ( (dx3 - dx1)*(dy2 - dy1) - (dy3 - dy1)*(dx2 - dx1) );
        double dst_mu = (dx4 - dx1 - dst_to*(dx3 - dx1)) / (dx2 - dx1);
        double dst_lamda = 1 - dst_mu - dst_to;

        // 2) scale the columns by the coefficients just computed :
        //  | lamda*x1  mu*x2  to*x3  |
        //  | lamda*y1  mu*y2  to*y3  |
        //  | lamda     mu     to     |

        Matrix3d A = new Matrix3d();
        A.m00 = src_lamda * sx1;
        A.m01 = src_mu * sx2;
        A.m02 = src_to * sx3;
        A.m10 = src_lamda * sy1;
        A.m11 = src_mu * sy2;
        A.m12 = src_to * sy3;
        A.m20 = src_lamda;
        A.m21 = src_mu;
        A.m22 = src_to;

        Matrix3d B = new Matrix3d();
        B.m00 = dst_lamda * dx1;
        B.m01 = dst_mu * dx2;
        B.m02 = dst_to * dx3;
        B.m10 = dst_lamda * dy1;
        B.m11 = dst_mu * dy2;
        B.m12 = dst_to * dy3;
        B.m20 = dst_lamda;
        B.m21 = dst_mu;
        B.m22 = dst_to;

        // 3) compute Ainvert

        Matrix3d Ainv = (Matrix3d) A.clone();
        Ainv.invert();

        // 4) compute C = B . Ainvert

        Matrix3d C = (Matrix3d) B.clone();
        C.mul(Ainv);

        // 5) compute the final matrix

        Matrix4d Result = new Matrix4d();
        Result.m00 = C.m00;
        Result.m01 = C.m01;
        Result.m02 = 0;
        Result.m03 = C.m02;
        Result.m10 = C.m10;
        Result.m11 = C.m11;
        Result.m12 = 0;
        Result.m13 = C.m12;
        Result.m20 = 0;
        Result.m21 = 0;
        Result.m22 = 1;
        Result.m23 = 0;
        Result.m30 = C.m20;
        Result.m31 = C.m21;
        Result.m32 = 0;
        Result.m33 = C.m22;

        return Result;
    }
public void重塑(最终glautodraw可绘制、最终int arg1、最终int arg2、最终int width、,
最终整数高度){
//获取OpenGL图形上下文
如果(宽度=1.0){
((GL2ES1)gl).glOrtho(-maxDim*纵横比,maxDim*纵横比,-maxDim,maxDim,maxDim*10,-maxDim*10);
}否则{
((GL2ES1)gl).glOrtho(-maxDim,maxDim,-maxDim/aspect,maxDim/aspect,maxDim,-maxDim);
}
gl.GLD(0d,0d,最大值*0.05);
}
//密钥存储
双[][]点={
{0,0,0},
{1,0,0},
{1,1,0},
{0,1,0}
};
双[][]点={
{X1,Y1,0},
{X2,Y2,0},
{X3,Y3,0},
{X4,Y4,0}
};
double[]keystenematrix=getDoubleArray(getHomography(srcPoints,dstPoints));
gl.glmultmatricxd(keystenematrix,0);
camera.animate();
}
公共静态矩阵x4d GetMonography(双[][]srcPoints,双[][]dstPoints){
双sx1=SRC点[0][0];
双sy1=SRC点[0][1];
双sx2=SRC点[1][0];
双sy2=SRC点[1][1];
双sx3=src点[2][0];
双sy3=SRC点[2][1];
双sx4=SRC点[3][0];
双sy4=SRC点[3][1];
双dx1=DST点[0][0];
双dy1=DST点[0][1];
双dx2=DST点[1][0];
双dy2=DST点[1][1];
双dx3=DST点[2][0];
双dy3=DST点[2][1];
双dx4=DST点[3][0];
双dy4=DST点[3][1];
//1)求解以下src和dst方程:
//| x1 x2 x3 | lamda | x4|
//| y1 y2 y3 |.| mu |=| y4|
//| 1 | |至| | 1|
//对于src:
双src_to=((sx4-sx1)*(sy2-sy1)-(sy4-sy1)*(sx2-sx1))/
((sx3-sx1)*(sy2-sy1)-(sy3-sy1)*(sx2-sx1));
双src_mu=(sx4-sx1-src_to*(sx3-sx1))/(sx2-sx1);
双src_lamda=1-src_mu-src_to;
//对于dst:
双dst_to=((dx4-dx1)*(dy2-dy1)-(dy4-dy1)*(dx2-dx1))/
((dx3-dx1)*(dy2-dy1)-(dy3-dy1)*(dx2-dx1));
双dst_μ=(dx4-dx1-dst_至*(dx3-dx1))/(dx2-dx1);
双dst_lamda=1-dst_mu-dst_to;
//2)根据刚刚计算的系数缩放柱:
//|拉姆达*x1亩*x2至*x3|
//|拉姆达*y1亩*y2至*y3|
//|拉姆达穆托|
Matrix3d A=新的Matrix3d();
A.m00=src_lamda*sx1;
A.m01=src_mu*sx2;
A.m02=src_至*sx3;
A.m10=src_lamda*sy1;
A.m11=src_mu*sy2;
A.m12=src_至*sy3;
A.m20=src_lamda;
A.m21=src_mu;
A.m22=src_至;
Matrix3d B=新的Matrix3d();
B.m00=dst_lamda*dx1;
B.m01=dst_mu*dx2;
B.m02=dst_至*dx3;
B.m10=dst_lamda*dy1;
B.m11=dst_mu*dy2;
B.m12=dst_至*dy3;
B.m20=dst_lamda;
B.m21=dst_μ;
B.m22=dst_至;
//3)计算Ainvert
Matrix3d Ainv=(Matrix3d)A.clone();
Ainv.inverse();
//4)计算C=B.Ainvert
Matrix3d C=(Matrix3d)B.clone();
C.mul(Ainv);
//5)计算最终矩阵
Matrix4d结果=新Matrix4d();
结果m00=C.m00;
Result.m01=C.m01;
结果:m02=0;
结果m03=C.m02;
结果:m10=C.m10;
Result.m11=C.m11;
结果:1.m12=0;
结果1.m13=C.m12;
结果:m20=0;
结果:m21=0;
结果:m22=1;
结果:m23=0;
结果:m30=C.m20;
结果m31=C.m21;
结果:m32=0;
结果m33=C.m22;
返回结果;
}

我一定错过了什么,但我不知道。。。提前感谢您的帮助

“基于显示器的4个角应用投影变换”这到底是什么意思?同样,你的图表也不清楚你想做什么。我只是添加了一张图片让它更清楚。我的结果看起来更像是“仿射变换”而不是“投影变换”,但我不明白为什么。。。(cf)看看你所期望的,我想说你只想渲染到一个纹理,并将该纹理映射到该多边形上