所以我对opengl和创建3d形状还很陌生。因此,对于我的示例,我有两个正方形,一个正方形的高度/宽度2为中心,其原点坐标为(0,0,-10),另一个正方形位于窗口的最左侧。我正在尝试沿x-z平面旋转位于原点的正方形,而不旋转位于屏幕最左侧的正方形。我的解决方法是将中心正方形的每个xyz坐标保存到一个变量,然后创建一种使用cos(theta)行为沿x-z平面旋转正方形的方法。我的代码有效,但是我认为这是一种可怕的方法,因为必须已经创建了一些更有效的方法来执行相同的功能。我看了glRotatef(),但是据我了解,这只会旋转我的相机视图,最终将同时旋转中间的正方形和最左边的正方形,而我只想旋转中间的正方形。是否有其他方法可以轻松地在3d空间中旋转单个2d形状?

如果相关,我将自己制作的旋转代码包括在中间正方形中:(顺便说一句,蓝色类只是我制作的具有平方坐标和cos(theta)的圆度的类)

if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {

            blue.setCircle(blue.getCircle()+1f);//getCircle is initially zero and gets incremented by 1 for everytime the program loops with the user holding the left button.

            blue.setXfrontTR((float)Math.cos(Math.toRadians(blue.getCircle())));//Changing top-right x coordinate of the middle square
            blue.setZfrontTR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f)))); //Changing top-right z coordinate of the middle square.

            blue.setXfrontTL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
            blue.setZfrontTL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing top-left x,z coordinates

            blue.setXfrontBL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
            blue.setZfrontBL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing bottom-left x,z coordinates

            blue.setXfrontBR((float)Math.cos(Math.toRadians(blue.getCircle())));
            blue.setZfrontBR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f))));//Changing bottom-right x-z coordinates
}

最佳答案

如果为需要独立运动的每个对象提供模型视图矩阵,则可以实现此目的。快速绘制/移动一些独立对象的另一种方法是:

对于每个对象:

pushMatrix()

绘制对象

popMatrix()

而在模型视图矩阵中...

绘制方法在很大程度上取决于您要编码的OpenGL版本,但以上内容适用于简单的绘制。我不是OpenGL / 3D编程专家,所以,如果您稍等片刻,您可能会听到(见)比我提供的更好的知识:)

07-24 09:49
查看更多