gpt4 book ai didi

java - 如何在opengl中旋转3d空间中的单个形状?

转载 作者:行者123 更新时间:2023-12-01 04:11:44 26 4
gpt4 key购买 nike

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

最佳答案

如果为每个需要独立运动的对象提供一个模型 View 矩阵,您就可以实现这一目标。快速绘制/移动一些独立对象的另一个选项是:

对于每个对象:

pushMatrix()

绘制对象

popMatrix()

在模型 View 矩阵中...

绘图方法很大程度上取决于您正在编码的 OpenGL 版本,但上述方法适用于简单绘图。我不是 OpenGL/3D 编程方面的专家,所以,如果您稍等一下,您可能会听到(看到)比我提供的更好的智慧:)

关于java - 如何在opengl中旋转3d空间中的单个形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19864656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com