Python -- OpenGL

1.画犹他茶壶

 

Python3下,glutCreateWindow()的参数必须编码

 

 1 from OpenGL.GL import *
 2 from OpenGL.GLU import *
 3 from OpenGL.GLUT import *
 4 
 5 def drawFunc():
 6     glClear(GL_COLOR_BUFFER_BIT)
 7     glRotatef(0.5, 1, 1, 0)
 8     glutWireTeapot(0.5)
 9     # glutSolidTeapot(0.5)
10     glFlush()
11 
12 glutInit()
13 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
14 glutInitWindowSize(400, 400)
15 glutCreateWindow("第一个OpenGL程序--犹他茶壶".encode('cp936'))
16 glutDisplayFunc(drawFunc)
17 glutIdleFunc(drawFunc)
18 glutMainLoop()

2.画基本图形

 1 from OpenGL.GL import *
 2 from OpenGL.GLU import *
 3 from OpenGL.GLUT import *
 4 
 5 def init():
 6     glClearColor(0.0, 0.0, 0.0, 1.0)
 7     gluOrtho2D(-1.0, 1.0, -1.0, 1.0)
 8 def drawFunc():
 9     glClear(GL_COLOR_BUFFER_BIT)
10 
11     glBegin(GL_LINES)
12     glVertex2f(-1.0, 0.0)
13     glVertex2f(1.0, 0.0)
14     glVertex2f(0.0, 1.0)
15     glVertex2f(0.0, -1.0)
16     glEnd()
17 
18     glPointSize(5.0)
19     glBegin(GL_POINTS)
20     glColor3f(1.0, 0.0, 0.0)
21     glVertex2f(0.3, 0.3)
22     glColor3f(0.0, 1.0, 0.0)
23     glVertex2f(0.6, 0.6)
24     glColor3f(0.0, 0.0, 1.0)
25     glVertex2f(0.9, 0.9)
26     glEnd()
27 
28     glColor3f(1.0, 1.0, 0)
29     glBegin(GL_QUADS)
30     glVertex2f(-0.2, 0.2)
31     glVertex2f(-0.2, 0.5)
32     glVertex2f(-0.5, 0.5)
33     glVertex2f(-0.5, 0.2)
34     glEnd()
35 
36     glColor3f(0.0, 1.0, 1.0)
37     glPolygonMode(GL_FRONT, GL_LINE)
38     glPolygonMode(GL_BACK, GL_FILL)
39     glBegin(GL_POLYGON)
40     glVertex2f(-0.5, -0.1)
41     glVertex2f(-0.8, -0.3)
42     glVertex2f(-0.8, -0.6)
43     glVertex2f(-0.5, -0.8)
44     glVertex2f(-0.2, -0.6)
45     glVertex2f(-0.2, -0.3)
46     glEnd()
47 
48     glPolygonMode(GL_FRONT, GL_FILL)
49     glPolygonMode(GL_BACK, GL_LINE)
50     glBegin(GL_POLYGON)
51     glVertex2f(0.5, -0.1)
52     glVertex2f(0.2, -0.3)
53     glVertex2f(0.2, -0.6)
54     glVertex2f(0.5, -0.8)
55     glVertex2f(0.8, -0.6)
56     glVertex2f(0.8, -0.3)
57     glEnd()
58 
59     glFlush()
60 
61 glutInit()
62 glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE)
63 glutInitWindowSize(400, 400)
64 glutInitWindowPosition(500, 300)
65 glutCreateWindow('Second--图形测试'.encode('cp936'))
66 
67 glutDisplayFunc(drawFunc)
68 init()
69 glutMainLoop()

3.画抛物线

 1 from OpenGL.GL import *
 2 from OpenGL.GLU import *
 3 from OpenGL.GLUT import *
 4 import numpy
 5 import sys
 6 
 7 def init():
 8     glClearColor(1.0, 1.0, 1.0, 1.0)
 9     gluOrtho2D(-5.0, 5.0, -5.0, 5.0)
10 def plotfunc():
11     glClear(GL_COLOR_BUFFER_BIT)
12     glPointSize(3.0)
13 
14     glColor3f(1.0, 1.0, 0.0)
15     glBegin(GL_LINES)
16     glVertex2f(-5.0, 0.0)
17     glVertex2f(5.0, 0.0)
18     glVertex2f(0.0, 5.0)
19     glVertex2f(0.0, -5.0)
20     glEnd()
21 
22     glColor3f(0.0, 0.0, 0.0)
23     glBegin(GL_POINTS)
24     # for x in (i*0.1 for i in range(-50, 50)):
25     for x in numpy.arange(-5.0, 5.0, 0.1):
26         y = x*x
27         glVertex2f(x, y)
28     glEnd()
29 
30     glFlush()
31 
32 def main():
33     glutInit(sys.argv)
34     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
35     glutInitWindowPosition(50, 50)
36     glutInitWindowSize(400, 400)
37     glutCreateWindow('抛物线'.encode('cp936'))
38     glutDisplayFunc(plotfunc)
39     init()
40     glutMainLoop()
41 
42 main()

4.CPU杀手

 1 from OpenGL.GL import *
 2 from OpenGL.GLU import *
 3 from OpenGL.GLUT import *
 4 from numpy import *
 5 import sys
 6 
 7 global W, H, R
 8 (W, H, R) = (501, 500, 10.0)
 9 
10 def init():
11     glClearColor(1.0, 1.0, 1.0, 1.0)
12 
13 def drawfunc():
14     glClear(GL_COLOR_BUFFER_BIT)
15     glColor3f(0.0, 0.0, 0.0)
16     glBegin(GL_POINTS)
17     for x in arange(-R, R, 0.04):
18         print('%.1f%%' % ((R+x)/(R+R)*100))
19         for y in arange(-R, R, 0.04):
20             r = cos(x) + sin(y)
21             glColor3f(cos(y*r), cos(x*y*r), sin(x*r))
22             glVertex2f(x, y)
23     print('100%!!')
24     glEnd()
25     glFlush()
26 
27 def reshape(w, h):
28     if h<=0:
29         h = 1
30     glViewport(0, 0, w, h)
31     glMatrixMode(GL_PROJECTION)
32     glLoadIdentity()
33     if w<= h:
34         gluOrtho2D(-R, R, -R*h/w, R*h/w)
35     else:
36         gluOrtho2D(-R*w/h, R*w/h, -R, R)
37     glMatrixMode(GL_MODELVIEW)
38     glLoadIdentity()
39 
40 def keyboard(key, x, y):
41     if key == chr(27) or key == 'q': #Esc is 27
42         sys.exit()
43 
44 def main():
45     glutInit(sys.argv)
46     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
47     glutInitWindowPosition(0, 0)
48     glutInitWindowSize(W, H)
49     glutCreateWindow('OpenGL的艺术'.encode('cp936'))
50     glutReshapeFunc(reshape)
51     glutDisplayFunc(drawfunc)
52     # glutKeyboardFunc(keyboard)
53     init()
54     glutMainLoop()
55 
56 main()

转载于:https://www.cnblogs.com/baijifeilong/p/3706818.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值