显示动态平面

Showing a dynamic plane

我写了一个 Python 程序,它连续 returns 4 改变笛卡尔坐标,对齐形成一个可以在任何给定方向的正方形平面;偏航、俯仰或滚动。在 3D 中显示不断更新的平面的最佳方法是什么 space?

注意:这是在 Linux 机器上完成的,如果有任何改变,但我看不出它会怎样。

您可以为此使用 PyOpenGL。

http://pyopengl.sourceforge.net/

可以用pip安装

最简单的方法是使用 "legacy" API 并绘制四边形。

要更改偏航、俯仰和滚动,请使用变换矩阵和 glRotate。

你也可以使用着色器,自己绘制变换矩阵。

https://en.wikipedia.org/wiki/Rotation_matrix

使用 OpenGL 传统绘制纹理平面的示例 API:

import sys
import math

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

def init():
    global image, texName
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH)
    glShadeModel(GL_FLAT)
    glEnable(GL_DEPTH_TEST)

    import Image, numpy
    img = Image.open('flagEn.bmp') # .jpg, .bmp, etc. also work
    img_data = numpy.array(list(img.getdata()), numpy.int8)

    global texture
    texture = glGenTextures(1)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glBindTexture(GL_TEXTURE_2D, texture)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)

def display():
    #global texName
    global texture
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glEnable(GL_TEXTURE_2D)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
    glBindTexture(GL_TEXTURE_2D, texture)
    glBegin(GL_QUADS)
    glTexCoord2f(0, 0)
    glVertex3f(-2, -1, 0)
    glTexCoord2f(0, 10)
    glVertex3f(-2, 1, 0)
    glTexCoord2f(10, 10)
    glVertex3f(0, 1, 0)
    glTexCoord2f(10, 0)
    glVertex3f(0, -1, 0)
    glTexCoord2f(0, 0)
    glVertex3f(1, -1, 0)
    glTexCoord2f(0, 10)
    glVertex3f(1, 1, 0)
    glTexCoord2f(10, 10)
    glVertex3f(1+math.sqrt(2), 1, -math.sqrt(2))
    glTexCoord2f(10, 0)
    glVertex3f(1+math.sqrt(2), -1, -math.sqrt(2))
    glEnd()
    glFlush()
    glDisable(GL_TEXTURE_2D)

def reshape(w, h):
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, w/h, 1.0, 30.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -3.6);

def keyboard(key, x, y):
    pass

glutInit(sys.argv)
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE)
glutInitWindowSize (500, 500)
glutInitWindowPosition (100, 100)
glutCreateWindow ('texture')
init ()
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutKeyboardFunc(keyboard)
glutMainLoop()