所以我尝试只旋转一个对象,我已经阅读了有关如何执行此操作的其他帖子,但所有这些帖子都只是说了这样的话:
1.调用glLoadIdentity();
2. 绘制形状
3.旋转
我已经尝试了他们告诉我做的事情,但似乎对我不起作用?
if (time != faces.size() - 1 && faces.size() != 1){
if (faces.get(time+1).needsIdentity){
GL11.glLoadIdentity();
System.out.println("The not last identity was set!");
}
System.out.println("got identity");
}else{
if (faces.get(faces.size() - 1).needsIdentity){
GL11.glLoadIdentity();
System.out.println("identity set!");
}
System.out.println("got last identity");
}
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(f.clr.red, f.clr.green, f.clr.blue);
GL11.glVertex3f(f.loc.x - f.x, f.loc.y + f.y, f.loc.z + f.z);
GL11.glVertex3f(f.loc.x + f.x, f.loc.y + f.y, f.loc.z + f.z);
GL11.glVertex3f(f.loc.x + f.x, f.loc.y - f.y, f.loc.z + f.z);
GL11.glVertex3f(f.loc.x - f.x, f.loc.y - f.y, f.loc.z + f.z);
GL11.glEnd();
finished();
}
public void finished(){
GL11.glRotatef(rs.rotx, 1F, 0F, 0F);
GL11.glRotatef(rs.roty, 0F, 1F, 0F);
GL11.glRotatef(rs.rotz, 0F, 0F, 1F);
System.out.println("rotated");
}
这是我的代码。
在名为 faces
的数组中有 4 个四边形,其中 3 个有 needsIdentity
false,其中一个是 true,也是我要旋转的那个。
我已经输入了打印行来检查它是否获得了身份,它确实获得了身份。
另请参阅times
每轮添加 1 个。
你能解释一下我到底要在哪里调用glLoadIdentity()
吗? ?
您可能想知道这一点,但它旋转的是我而不是对象。
最佳答案
如果您只想旋转场景的某些部分(在您的情况下,是一个对象),您可以使用 glPushMatrix()
和 glPopMatrix()
。这允许您进行变换(例如平移、旋转和缩放)并在完成绘制要变换的对象后恢复。不用担心 glLoadIdentity()
- 这会重置所有转换。它可能会旋转您的原因是您可能在多次调用 glRotatef(r, x, y, z)
后绘制自己。
//Draw some objects here - not rotated
GL11.glPushMatrix(); //Push the matrix onto the stack
//Rotate the object about to be drawn
GL11.glRotatef(rs.rotx, 1F, 0F, 0F);
GL11.glRotatef(rs.roty, 0F, 1F, 0F);
GL11.glRotatef(rs.rotz, 0F, 0F, 1F);
//Draw the object
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(f.clr.red, f.clr.green, f.clr.blue);
GL11.glVertex3f(f.loc.x - f.x, f.loc.y + f.y, f.loc.z + f.z);
GL11.glVertex3f(f.loc.x + f.x, f.loc.y + f.y, f.loc.z + f.z);
GL11.glVertex3f(f.loc.x + f.x, f.loc.y - f.y, f.loc.z + f.z);
GL11.glVertex3f(f.loc.x - f.x, f.loc.y - f.y, f.loc.z + f.z);
GL11.glEnd();
GL11.glPopMatrix(); //Pop the matrix off of the stack
//Draw some more objects here - not rotated
关于java - lwjgl,仅旋转屏幕上的一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38553084/