这学期开始学计算机图形学基础,课后有个习题让用OpenGL实现折线和矩形的橡皮筋绘制技术,只要求了用右键菜单实现功能的选择。老师嫌有些简单,就说要加上教材上基于键盘实现的代码,可教材上的代码还是要先把鼠标移到一个点上,再用按键确定这个点,这样配合着使用很别扭。我想既然用键盘了,不如直接写个可以完全由键盘控制绘图过程的代码吧。
正好现在我想学下Python,就决定拿这道题开始练手。代码虽然很简单,但写的过程中,一边要学Python的语法,一边又要查OpenGL的库函数,还是挺费精力的。这算是我第一次用Python写代码,结果还算满意,写个博客纪念下吧。
程序用鼠标完成绘图就不用说了。键盘方面 A、D、W、X 用于移动窗口内的点,P用于选定某一点,L、 R、 C 分别为菜单各功能的快捷键,用于选择画折线、画矩形或清除图形。所有按键不区分大小写。为了使程序使用方便,键盘和鼠标没做什么限制,既可以分别使用,也可以结合使用。代码如下:
#!/usr/bin/python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
iMode=1
winWidth = 800; winHeight = 600
num = 0; a = [0]*100; b = [0]*100
w1 = 200; h1 = 500; w2 = 200; h2 = 500
iPointNum = 0; x1 = 200; y1 = 500; x2 = 200; y2 = 500
def ChangeSize(w, h):
global winWidth, winHeight
winWidth = w
winHeight = h
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0.0, winWidth, 0.0, winHeight)
def ProcessMenu(value):
global iMode
iMode = value
glutPostRedisplay()
def DrawPoint(x , y):
glClear(GL_COLOR_BUFFER_BIT)
glPointSize(1.5)
glBegin(GL_POINTS)
glVertex2i(x, y)
glEnd()
glutSwapBuffers()
def Draw():
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0, 0.0, 0.0)
global num, iMode, w1, h1, w2, h2, x1, x2, y1, y2, iPointNum, a, b
#Line
if iMode == 1:
glBegin(GL_LINE_STRIP)
for i in range(0, num):