#encoding:utf-8
import OpenGL.GL as gl
import OpenGL.GLU as glu
import OpenGL.GLUT as glut
from random import randint
def init():
# 用白色清屏
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
# 使用正投影将世界坐标系二维矩形区域的内容映射到屏幕上。
gl.glMatrixMode(gl.GL_PROJECTION)
glu.gluOrtho2D(0, 400, 0, 400)
def myRandomBitmap(rows, cols, size = (10, 10)):
# 画方格
for row in range(rows):
for col in range(cols):
bit = randint(0, 1000)%2
gl.glColor3f(bit, bit, bit)
gl.glBegin(gl.GL_POLYGON)
gl.glVertex2d(col*size[0], row*size[1])
gl.glVertex2d((col+1)*size[0], row*size[1])
gl.glVertex2d((col+1)*size[0], (row+1)*size[1])
gl.glVertex2d(col*size[0], (row+1)*size[1])
gl.glEnd()
# 画左右分割线
for row in range(rows):
gl.glColor3f(0.5,0.5,0.5)
gl.glBegin(gl.GL_LINES)
gl.glVertex2d(0, row*size[1])
gl.glVertex2d(400, row*size[1])
gl.glVertex2d(row*size[1], 0)
gl.glVertex2d(row*size[1], 400)
gl.glEnd()
def drawFunc():
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
myRandomBitmap(10, 10, (40, 40))
gl.glFlush()
glut.glutInit() # 初始化 GLUT
glut.glutInitDisplayMode(glut.GLUT_RGBA | glut.GLUT_SINGLE) # 设置显示模式
glut.glutInitWindowPosition(400,100) # 设置 top-left 显示窗口的位置
glut.glutInitWindowSize(600, 600) # 设置显示窗口大小,长和宽
glut.glutCreateWindow("Second") # 创建窗口,设置名称
init() # 初始化窗口
glut.glutDisplayFunc(drawFunc) # 将图形送给显示窗口并显示
glut.glutMainLoop() # 显示所有内容,并等待