考虑一个圆 ( − 40) 2 + ( − 40) 2 = 400。将它沿 X 轴逆时针旋转 30 度并沿 Z 轴平移 +20 个单位

Consider a circle ( − 40) 2 + ( − 40) 2 = 400. Rotate it along X- axis counter clockwise 30 degree & translate it along Z- axis for +20 units

为此操作编写 PYOpenGL 代码。

我可以画圆,但是我的旋转和平移代码不起作用。代码正在执行但没有给出正确的结果。帮我解决问题的旋转和平移部分。

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from math import * 


def circle():
    posx, posy = 40,40
    sides = 80    
    radius = 20   
    glBegin(GL_POLYGON)    
    for i in range(100):    
        cosine= radius * cos(i*2*pi/sides) + posx    
        sine  = radius * sin(i*2*pi/sides) + posy    
        glVertex2f(cosine,sine)   
        
    glEnd()
    
    

def iterate():
    glViewport(0, 0, 3000, 3000)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    
  #  glMatrixMode(GL_MODELVIEW)
  #  glLoadIdentity()
  #  glTranslatef(0, 0, -3)
  #  glRotatef(50, 1, 0, 0)
  #  glRotatef(70, 0, 1, 0)
    
    glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()

def showScreen():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(1.0, 0.0, 3.0)
    circle()
    glutSwapBuffers()

glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(400, 400)
glutInitWindowPosition(200, 200)
wind = glutCreateWindow("OpenGL Coding Practice")
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen)
glutMainLoop()

当您沿 z 轴移动圆或旋转圆时,它会被 Orthographic projection 的近平面 (= 0) 和远平面 (= 1) 裁剪。

更改到近平面和远平面的距离(例如 -100 和 100):

glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)

glOrtho(0.0, 500, 0.0, 500, -100, 100)

iterate函数:

def iterate():
    glViewport(0, 0, 3000, 3000)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, 500, 0.0, 500, -100, 100)
    
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()
    glTranslatef(0, 0, 20)
    glRotatef(30, 1, 0, 0)