
两个pyOpenGL的小例子
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
global size
size=0.5
#键盘交互有点问题
def keyboard(input="a",x=0,y=0):
global size
if(input=='w'):
size+=1.0
if(input=='s'):
size-=1.0
if(size<0.1):
size=0.1
print(input)
print(size)
glutPostRedisplay()
#通过指定参数缺省值,来设置参数类型
def myMouse(button=0,state=0,x=0,y=0):
if(state==GLUT_DOWN):
print(x,y)
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)#绘制当前帧时,先擦黑板
glRotatef(1, 0, 1, 0)
glutWireTeapot(size)
glFlush()#它是处理OpenGL的渲染流水线,让所有排队中的命令得到执行。
glutInit()#初始化
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)#单缓冲
glutInitWindowPosition(0,0)#设置窗口位置
glutInitWindowSize(400, 400)#设置窗口大小
glutCreateWindow("teaPot")#出现窗口,质变
#回调函数
glutDisplayFunc(drawFunc)#里面是实际的绘图操作
#glutIdleFunc(drawFunc)#可以让OpenGL在闲暇之余,调用一下注册的函数,产生动画
glutKeyboardFunc(keyboard)#键盘交互
glutMouseFunc(myMouse)#鼠标交互
glutMainLoop()
运行结果:鼠标点击,显示点击坐标
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
gluOrtho2D(-1.0, 1.0, -1.0, 1.0)
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
#先画一横一竖两条直线
glBegin(GL_LINES)
glVertex2f(-1.0, 0.0)
glVertex2f(1.0, 0.0)
glVertex2f(0.0, 1.0)
glVertex2f(0.0, -1.0)
glEnd()
#右上部分的3个点
glPointSize(5.0)
glBegin(GL_POINTS)
glColor3f(1.0, 0.0, 0.0)
glVertex2f(0.3, 0.3)
glColor3f(0.0, 1.0, 0.0)
glVertex2f(0.6, 0.6)
glColor3f(0.0, 0.0, 1.0)
glVertex2f(0.9, 0.9)
glEnd()
#左上部分的
glColor3f(1.0, 1.0, 0)
glBegin(GL_QUADS)
glVertex2f(-0.2, 0.2)
glVertex2f(-0.2, 0.5)
glVertex2f(-0.5, 0.5)
glVertex2f(-0.5, 0.2)
glEnd()
#左下
glColor3f(0.0, 1.0, 1.0)
glPolygonMode(GL_FRONT, GL_LINE)
glPolygonMode(GL_BACK, GL_FILL)
glBegin(GL_POLYGON)
glVertex2f(-0.5, -0.1)
glVertex2f(-0.8, -0.3)
glVertex2f(-0.8, -0.6)
glVertex2f(-0.5, -0.8)
glVertex2f(-0.2, -0.6)
glVertex2f(-0.2, -0.3)
glEnd()
#右下
glPolygonMode(GL_FRONT, GL_FILL)
glPolygonMode(GL_BACK, GL_LINE)
glBegin(GL_POLYGON)
glVertex2f(0.5, -0.1)
glVertex2f(0.2, -0.3)
glVertex2f(0.2, -0.6)
glVertex2f(0.5, -0.8)
glVertex2f(0.8, -0.6)
glVertex2f(0.8, -0.3)
glEnd()
glFlush()
glutInit()
glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE)
glutInitWindowSize(400, 400)
glutCreateWindow("Sencond")
glutDisplayFunc(drawFunc)
init()
glutMainLoop()
运行结果,画了一点图
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)