本文要點在于Python擴展庫pyopengl的應用,關于OpenGL函數參數含義可以查閱有關資料。
import sys
python三角形代碼。from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
python怎么運行自己編寫的程序?class MyPyOpenGLTest:
? ? def __init__(self, width = 640, height = 480, title = b'OpenGL--gradient color'):
? ? ? ? glutInit(sys.argv)
python代碼打錯了刪不掉、? ? ? ? glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
? ? ? ? glutInitWindowSize(width, height)
? ? ? ? self.window = glutCreateWindow(title)
python怎么調整字體大小。? ? ? ? self.InitGL(width, height)
? ? ? ? glutDisplayFunc(self.Draw)
? ? ? ? glutIdleFunc(self.Draw)
turtle簡單好看繪圖代碼,? ? ? ??
? ? #繪圖方法
? ? def Draw(self):
? ? ? ? glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
? ? ? ? glLoadIdentity()
? ? ? ? #平移
? ? ? ? glTranslatef(-2.0, 0.0, -8.0)
? ? ? ? #繪制二維多邊形
? ? ? ? glBegin(GL_POLYGON)
? ? ? ? glColor3f(1.0, 0.0, 0.0)
? ? ? ? glVertex3f(0.0, 3.0, 0.0)
? ? ? ? glColor3f(0.0, 1.0, 0.0)
? ? ? ? glVertex3f(2.0, -2.0, 0.0)
? ? ? ? glColor3f(0.0, 0.0, 1.0)
? ? ? ? glVertex3f(-2.0, -2.0, 0.0)
? ? ? ? glEnd()
? ? ? ? #平移
? ? ? ? glTranslatef(3.5, 1.5, 0.0)
? ? ? ? #縮放
? ? ? ? glScale(0.5, 0.5, 0.5)
? ? ? ??
? ? ? ? #繪制二維多邊形
? ? ? ? glBegin(GL_POLYGON)
? ? ? ? glColor3f(1.0, 1.0, 0.0)
? ? ? ? glVertex3f(0.0, 3.0, 0.0)
? ? ? ? glColor3f(0.0, 1.0, 1.0)
? ? ? ? glVertex3f(2.0, -2.0, 0.0)
? ? ? ? glColor3f(1.0, 0.0, 1.0)
? ? ? ? glVertex3f(-2.0, -2.0, 0.0)
? ? ? ? glEnd()
? ? ? ? glTranslatef(0.0, -4.0, 0.0)
? ? ? ? glScale(0.5, 0.5, 0.5)
? ? ? ? #繞z軸旋轉45度
? ? ? ? glRotatef(45.0, 0.0, 0, 1)
? ? ? ??
? ? ? ? #繪制二維多邊形
? ? ? ? glBegin(GL_POLYGON)
? ? ? ? glColor3f(0, 1.0, 0.5)
? ? ? ? glVertex3f(0.0, 3.0, 0.0)
? ? ? ? glColor3f(0.5, 1.0, 0.0)
? ? ? ? glVertex3f(2.0, -2.0, 0.0)
? ? ? ? glColor3f(1.0, 0.0, 0.5)
? ? ? ? glVertex3f(-2.0, -2.0, 0.0)
? ? ? ? glEnd()
? ? ? ??
? ? ? ? glutSwapBuffers()
? ? ? ??
? ? def InitGL(self, width, height):
? ? ? ? glClearColor(1.0, 1.0, 1.0, 0.0)
? ? ? ? glClearDepth(1.0)
? ? ? ? glDepthFunc(GL_LESS)
? ? ? ? #單色渲染
? ? ? ? #glShadeModel(GL_FLAT)
? ? ? ? #平滑渲染模式
? ? ? ? glShadeModel(GL_SMOOTH)
? ? ? ? glEnable(GL_POINT_SMOOTH)
? ? ? ? glEnable(GL_LINE_SMOOTH)
? ? ? ? glEnable(GL_POLYGON_SMOOTH)
? ? ? ? glHint(GL_POINT_SMOOTH_HINT,GL_NICEST)
? ? ? ? glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
? ? ? ? glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST)
? ? ? ??
? ? ? ? glMatrixMode(GL_PROJECTION)
? ? ? ? glLoadIdentity()
? ? ? ? gluPerspective(45.0, float(width)/float(height), 0.1, 100.0)
? ? ? ? glMatrixMode(GL_MODELVIEW)
? ? ? ??
? ? def MainLoop(self):
? ? ? ? glutMainLoop()
if __name__ == '__main__':
? ? w = MyPyOpenGLTest()
? ? w.MainLoop()
運行效果如圖:
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态