簡體   English   中英

如何在不使用glRotateF內置函數的情況下在JOGL中繞Z軸旋轉圖像

[英]How to rotate an image about the Z Axis in JOGL without using the glRotateF built-in functions

如何在不使用glRotatef函數的情況下在JOGL中繞Z軸旋轉圖像? 我不知道如何獲取z',因為沒有e.getZ()函數。 如何計算z_prime 現在,當我嘗試繞着z軸旋轉時,它就像在開始時繞着z軸旋轉,然后開始繞着y軸旋轉,然后回到z(這很有意義,因為我有z_rot += y_prime 。我怎樣才能繞着z旋轉並計算z'

        public void transform(float[] vertices_in, float[] vertices_out){

            // perform your transformation

            int length = vertices_in.length;
            float[] transformMatrix = 
                {
                   r11, r12, r13, tx,
                   r21, r22, r23, ty,
                   r31, r32, r33, tz,
                   0,   0,   0, 1
                };




            // Fill in the empty verticesout vector
            for(int i = 0; i < vertices_in.length; i++)
            {
                vertices_out[i] = vertices_in[i];
            }
            // loop through and set the new matrix (after translation)
            // because
            // 1 0 0 0
            // 0 1 0 0
            // 0 0 1 0

            // X Rotation
            for(int i = 0; i < vertices_out.length; i += 3)
            {
                vertices_out[i+1] = vertices_out[i+1] * (float)Math.cos(xRot) - vertices_out[i+2] * (float)Math.sin(xRot); //New Y
                vertices_out[i+2] = vertices_out[i+1] * (float)Math.sin(xRot) + vertices_out[i+2] * (float)Math.cos(xRot); //New Z
            }

            // Y Rotation
            for(int i = 0; i < vertices_out.length; i += 3)
            {
                vertices_out[i] = vertices_out[i] * (float)Math.cos(yRot) + vertices_out[i+2] * (float)Math.sin(yRot);
                vertices_out[i+2] = vertices_out[i]*-(float)Math.sin(yRot) + vertices_out[i+2]*(float)Math.cos(yRot);
            }

            // Z Rotation
            for(int i = 0; i < vertices_out.length; i += 3)
            {
                vertices_out[i] = vertices_out[i] * (float)Math.cos(zRot) - vertices_out[i+1] * (float)Math.sin(zRot);
                vertices_out[i+1] = vertices_out[i] * (float)Math.sin(zRot) + vertices_out[i+1] * (float)Math.cos(zRot);
            }

            // Translation & Scaling
            for(int i = 0; i < vertices_in.length; i+=3)
            {
                vertices_out[i] = vertices_out[i] * transformMatrix[0] + transformMatrix[3]; //x'
                vertices_out[i+1] = vertices_out[i+1] * transformMatrix[5] + transformMatrix[7]; //y'
                vertices_out[i+2] = vertices_out[i+2] * transformMatrix[10] + transformMatrix[11]; //z'
            }

        }

@Override
public void mouseDragged(MouseEvent e)
{
        if(rotating)
        {
            float XX = (e.getX()-windowWidth*0.5f)*orthoX/windowWidth;
            float YY = -(e.getY()-windowHeight*0.5f)*orthoX/windowWidth;

            float x_prime = (StartXX - XX);
            float y_prime = (StartYY - YY);

            if(rightMouseClick)
            {
                zRot += y_prime/50;

            }
            else
            {
                xRot += y_prime/50;
                yRot += x_prime/50;
                StartYY = YY;
                StartXX = XX;   
            }

        }
}

所有這些旋轉都使用不一致的中間狀態。 例如(我只是用較短的變量名代替了)。

y = y * (float)Math.cos(xRot) - z * (float)Math.sin(xRot); //New Y
z = y * (float)Math.sin(xRot) + z * (float)Math.cos(xRot); //New Z

第一行是正確的。 但是,第二行在應使用舊y時已使用新y 因此,您必須將舊變量保存在某個地方以使其起作用。 所有其他旋轉同樣如此。

其余代碼看起來還不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM