java - 如何在不使用 glRotateF 内置函数的情况下在 JOGL 中绕 Z 轴旋转图像

标签 java opengl jogl

如何在不使用 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。因此,您必须将旧变量保存在某个地方才能使其工作。这同样适用于所有其他轮换。

其余代码看起来没问题。

关于java - 如何在不使用 glRotateF 内置函数的情况下在 JOGL 中绕 Z 轴旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812922/

相关文章:

java - Spring 独立应用程序中的预定方法

java - 为什么我应该在 httpPost() 之后重启 Wifi

OpenGL 抗锯齿和背景四边形

java - 如何在 JOGL 中旋转圆柱体

java - 我尝试使用 'AlarmManager'设置闹钟,但没有成功,哪里出了问题?

java - 运行 Junit 测试时,为什么日志消息没有写入我的日志文件?

java - 将 OpenGL 与 LWJGL 结合使用不会生成图片

c++ - 在 OpenGL 中计算法线矩阵

eclipse - JOGL 异常 - 在 java.library.path 中找不到胶合剂

java - 如何使用JOGL库执行项目?