import sys, time, math, os, random from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * startDir = os.getcwd() class TexMesh: def __init__(self, cols=4, rows=4, width=1.0, height=1.0, texture=None): self.cols = cols self.rows = rows self.width = width self.height = height self.texture = texture def draw(self): # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if self.texture: self.texture.apply() glColor4f(1,1,1,1) for i in range(0,self.rows-1): glBegin(GL_TRIANGLE_STRIP) for j in range(0,self.cols): glTexCoord2f(j/(self.cols-1.0), i/(self.rows-1.0)) x = (j/(self.cols-1.0)-0.5)*self.width y = (i/(self.rows-1.0)-0.5)*self.height glVertex2f(x, y) glTexCoord2f(j/(self.cols-1.0), (i+1)/(self.rows-1.0)) y = ((i+1)/(self.rows-1.0)-0.5)*self.height glVertex2f(x, y) glEnd() if self.texture: self.texture.disable() objects = [] def createObjects(): global objects bg = TexMesh(10, 10, texture=Texture2D('flower1.png')) objects.append(bg) def draw(): glClearColor(0, 0.3, 0.5, 0) glLineWidth(3) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) for o in objects: o.draw() glutSwapBuffers() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def update(dummy): glutTimerFunc(16, update, 0) glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE) glutInitWindowSize(700,700) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutTimerFunc(1, update, 0) os.chdir(startDir) createObjects() glutMainLoop()