gpt4 book ai didi

python - 如何使用 OpenGL 随时更改旋转半径

转载 作者:行者123 更新时间:2023-12-01 07:37:12 25 4
gpt4 key购买 nike

我有一张 map ,我想围绕具有一定半径的点旋转。我设法做到了这一点,但我的问题是我有时想在旅途中改变半径并相应地更新相机

pygame.init()
display = (1700, 1000)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(50, (display[0] / display[1]), 0.1, 5000)

glTranslatef(1, 1, -(radius/2))
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glRotatef(10, 1, 0, 0) # i gave it 10 degrees for the camera to look at
# the ground
gluLookAt(offset_x - radius / 2, height, offset_z - radius / 2,
offset_x, 0, offset_z,
0, 1, 0)

# offset_x,offset_z are the center of the circle,
# and the distance of rotation is the radius.
# This gets me to the point i want to be, everything is good so far.

while True:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()

if not move:
glTranslatef(offset_x, 0, offset_z)
glRotatef(rotation_angle, 0, 1, 0)
glTranslatef(-offset_x, 0, -offset_z)
rotation_angle += 0.5
else:
glTranslatef(offset_x, 0, offset_z)
glRotatef(rotation_angle, 0, 1, 0)
glTranslatef(-offset_x, 0, -offset_z)
#### this rotates around the point at the start.
#### the radius and the view distance is perfect.

现在我从文本文件中获取新的半径,并且我想更改 View 以从新的半径距离围绕新的半径中心旋转。我有这个“if”知道半径何时改变,但我不明白如何使用 glTranslatef() 或 gluLookAt() 来更改围绕新点的旋转。

    #####################################################
if radius_changed:
glTranslatef(0, 0, radius - old_radius_copy) # this isnt working

radius_changed = False
else:
glTranslatef(0, 0, radius - old_radius_copy) # this isnt working
######################################################


glCallList(obj.gl_list)
DrawBuffer(bufferObj, noPoints, noCirclePoints, noCrossPoints)
glPopMatrix()

最佳答案

使用 glScalef() 将得到正确的结果,如下所示:

    if radius_changed:
delta = float(old_radius_copy) / float(radius)
print delta
scale_param_for_radius_change *= delta
glTranslatef(offset_x, 0, offset_z)
glScalef(scale_param_for_radius_change, 1, scale_param_for_radius_change)
glTranslatef(-offset_x, 0, -offset_z)
old_radius_copy = radius
radius_changed = False
else:
glTranslatef(offset_x, 0, offset_z)
glScalef(scale_param_for_radius_change, 1, scale_param_for_radius_change)
glTranslatef(-offset_x, 0, -offset_z)

关于python - 如何使用 OpenGL 随时更改旋转半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56932668/

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