import sys, time, math, os, random from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * startDir = os.getcwd() camera = PerspCamera() camera.moveBackward(1.5) xRot, yRot = 0,0 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 glVertex3f(x, y, 0) glTexCoord2f(j/(self.cols-1.0), (i+1)/(self.rows-1.0)) y = ((i+1)/(self.rows-1.0)-0.5)*self.height glVertex3f(x, y, 0) 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) camera.apply() glPushMatrix() glRotatef(yRot, 0, 1, 0) glRotatef(xRot, 1, 0, 0) for o in objects: o.draw() glPopMatrix() glutSwapBuffers() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def specialkey(key,x,y): global xRot, yRot if key == GLUT_KEY_LEFT: yRot -= 2.0 elif key == GLUT_KEY_RIGHT: yRot += 2.0 elif key == GLUT_KEY_UP: xRot += 2.0 elif key == GLUT_KEY_DOWN: xRot -= 2.0 def update(dummy): glutTimerFunc(16, update, 0) glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(700,700) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutTimerFunc(1, update, 0) os.chdir(startDir) glEnable(GL_DEPTH_TEST) createObjects() glutMainLoop()