由PyOpenGL官方demo NEHE lesson5 修改而来。
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
import numpy as np
NODE= np.array([[0,0,0],
[0.25,0,0],
[0,0.5,0],
[0.25,0.5,0],
[0.,0.,0.25],
[0.25,0,0.25],
[0,0.5,0.25],
[0.25,0.5,0.25]],
dtype=np.float64)
#Node ID 从0开始,步长1为1自增
node_qty = NODE.shape[0] #节点数量
#MAT ID,TYPE ID,4 Nodes' ID
ELEM = np.array([[1,1,3,5,0,1],
[1,1,0,3,2,6],
[1,1,5,4,6,0],
[1,1,5,6,7,3],
[1,1,0,5,3,6]],
dtype=np.integer)
#单元ID 从0开始,步长1为1自增
elem_qty = ELEM.shape[0] #单元数量
X = np.zeros((elem_qty, 4),dtype =np.float64) # 4 :每个单元的节点数
Y = np.zeros((elem_qty, 4),dtype =np.float64) # 4 :每个单元的节点数
Z = np.zeros((elem_qty, 4),dtype =np.float64) # 4 :每个单元的节点数
for i in range(elem_qty):
for j in range(4):
nodeID = ELEM[i,j+2]
X[i,j] = NODE[nodeID,0]
Y[i,j] = NODE[nodeID,1]
Z[i,j] = NODE[nodeID,2]
# Some api in the chain is translating the keystrokes to this octal string
# so instead of saying: ESCAPE = 27, we use the following.
#ESCAPE = '\033'
ESCAPE=27
# Number of the glut window.
window = 0
# inital Rotation angle for the triangle.
rtri = 0
# A general OpenGL initialization function. Sets all of the initial parameters.
def InitGL(Width, Height): # We call this right after our OpenGL window is created.
glClearColor(0.0, 0.0, 0.0, 0.0) # 清除背景颜色并设为黑色
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
glEnable(GL_MULTISAMPLE)#多点采样(可抗锯齿)
glEnable(GL_DEPTH_TEST) # 激活深度测试
glShadeModel(GL_SMOOTH) # 激活颜色渐变渲染
glMatrixMode(GL_PROJECTION)
glLoadIdentity() # 重置投影矩阵
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)#计算窗口长宽比
glMatrixMode(GL_MODELVIEW)
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
def ReSizeGLScene(Width, Height):
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small
Height = 1
glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
# The main drawing function.
def DrawGLScene():
global rtri, rquad
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # 清除屏幕,和深度缓冲
glLoadIdentity() # Reset The View
glTranslatef(-0,0.0,-1.5) # Move Left And Into The Screen
glRotatef(rtri,1.0,1.0,0.0) # Rotate The Pyramid On It's x Axis
glBegin(GL_TRIANGLES)#画三角形
for i in range(elem_qty):
glColor3f(1.0,0.0,0.0) # Red
glVertex3f( X[i,0], Y[i,0], Z[i,0]) # Top Of Triangle (Front)
glColor3f(0.0,1.0,0.0) # Green
glVertex3f( X[i,1], Y[i,1], Z[i,1]) # Left Of Triangle (Front)
glColor3f(0.0,0.0,1.0) # Blue
glVertex3f( X[i,2], Y[i,2], Z[i,2])
glColor3f(0.0,1.0,0.0) # Green
glVertex3f( X[i,1], Y[i,1], Z[i,1]) # Top Of Triangle (Right)
glColor3f(0.0,0.0,1.0) # Blue
glVertex3f( X[i,2], Y[i,2], Z[i,2]) # Left Of Triangle (Right)
glColor3f(0.5,0.5,0.0) # yellow
glVertex3f( X[i,3], Y[i,3], Z[i,3]) # Right
glColor3f(0.0,0.0,1.0) # Blue
glVertex3f( X[i,2], Y[i,2], Z[i,2]) # Top Of Triangle (Back)
glColor3f(0.5,0.5,0.0) # yellow
glVertex3f( X[i,3], Y[i,3], Z[i,3]) # Left Of Triangle (Back)
glColor3f(1.0,0.0,0.0) # red
glVertex3f( X[i,0], Y[i,0], Z[i,0]) # Right Of
glColor3f(0.5,0.5,0.0) # yellow
glVertex3f( X[i,3], Y[i,3], Z[i,3]) # Top Of Triangle (Left)
glColor3f(1.0,0.0,0.0) # red
glVertex3f( X[i,0], Y[i,0], Z[i,0]) # Left Of Triangle (Left)
glColor3f(0.0,1.0,0.0) # Green
glVertex3f( X[i,1], Y[i,1], Z[i,1]) # Right Of Triangle (Left)
glEnd()
glLineWidth(2.0)#设置线宽
glBegin(GL_LINE_LOOP)#画线
glColor3f(1.0,1.0,1.0)#白色(线)
for i in range(elem_qty):
glVertex3f( X[i,0], Y[i,0], Z[i,0]) # Top Of Triangle (Front)
glVertex3f( X[i,1], Y[i,1], Z[i,1]) # Left Of Triangle (Front)
glVertex3f( X[i,2], Y[i,2], Z[i,2])
glVertex3f( X[i,0], Y[i,0], Z[i,0])
glVertex3f( X[i,1], Y[i,1], Z[i,1]) # Top Of Triangle (Right)
glVertex3f( X[i,2], Y[i,2], Z[i,2]) # Left Of Triangle (Right)
glVertex3f( X[i,3], Y[i,3], Z[i,3]) # Right
glVertex3f( X[i,1], Y[i,1], Z[i,1])
glVertex3f( X[i,2], Y[i,2], Z[i,2]) # Top Of Triangle (Back)
glVertex3f( X[i,3], Y[i,3], Z[i,3]) # Left Of Triangle (Back)
glVertex3f( X[i,0], Y[i,0], Z[i,0]) # Right Of
glVertex3f( X[i,2], Y[i,2], Z[i,2])
glVertex3f( X[i,3], Y[i,3], Z[i,3]) # Top Of Triangle (Left)
glVertex3f( X[i,0], Y[i,0], Z[i,0]) # Left Of Triangle (Left)
glVertex3f( X[i,1], Y[i,1], Z[i,1]) # Right Of Triangle (Left)
glVertex3f( X[i,3], Y[i,3], Z[i,3])
glEnd()
#控制转速
rtri += +0.4 # Increase The Rotation Variable For The Triangle
# since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers()
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
def keyPressed(*args):
# If escape is pressed, kill everything.
if args[0] == ESCAPE:
sys.exit()
def main():
global window
glutInit(sys.argv)
# Select type of Display mode:
# Double buffer
# RGBA color
# Alpha components supported
# Depth buffer
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
# get a 640 x 480 window
glutInitWindowSize(1000, 680)
# the window starts at the upper left corner of the screen
glutInitWindowPosition(0, 0)
window = glutCreateWindow("Tetra3d")#OPENGL 窗口标题
glutDisplayFunc(DrawGLScene)
#glutFullScreen()#全屏
# When we are doing nothing, redraw the scene.
glutIdleFunc(DrawGLScene)
# Register the function called when our window is resized.
glutReshapeFunc(ReSizeGLScene)
# Register the function called when the keyboard is pressed.
glutKeyboardFunc(keyPressed)
# Initialize our window.
InitGL(640, 480)
# Start Event Processing Engine
glutMainLoop()
# Print message to console, and kick off the main to get it rolling.
print("Hit ESC key to quit.")
main()
本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!