问题描述
如何在不使用 glRotatef
函数的情况下在JOGL中绕Z轴旋转图像?我不知道如何获取 z',因为没有 e.getZ()
函数.如何计算 z_prime
?现在,当我尝试绕着z轴旋转时,它就像在开始时绕着z轴旋转,然后开始绕着y轴旋转,然后又回到z(这很有意义,因为我有 z_rot += y_prime
.我如何围绕 z 旋转并计算 z'?
How can I rotate an image about the Z axis in JOGL without using glRotatef
function? I don't understand how to get z' since there is no e.getZ()
function. How might one calculate a z_prime
? Right now when I try to rotate about the z axis, it like rotates it about the z axis in the beggining and then begins rotating it about the y axis and then back to the z (which makes sense because i have z_rot += y_prime
. How can I just rotate it about the z and calculate a 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;
}
}
}
推荐答案
所有这些旋转都使用不一致的中间状态.例如(我只是替换了较短的变量名).
All these rotations are using an inconsistent intermediate state. E.g (I just substituted for shorter variable names).
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
.因此,您必须将旧变量保存在某个地方以使其起作用.其他所有轮换也一样.
The first line is correct. However, the second line already uses the new y
when it should use the old one. Hence, you have to save the old variable somewhere to make it work. The same applies to all other rotations.
其余的代码看起来还可以.
The rest of the code looks ok.
这篇关于如何在不使用glRotateF内置函数的情况下在JOGL中绕Z轴旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!