#!/usr/bin/python from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * import sys, string import vManager import vGeometry import vGeometryHandler import vGeometryGL import vBitmap import vBitmapHandler import vBitmapGL # opengl textures textures = None gwidth = 256 gheight = 192 # parse options if len(sys.argv) > 1: address = sys.argv[1] else: address = "localhost" def load_textures(): global textures # create a nice checkermap ix = 32 iy = 32 image = "" b = chr(50) + chr(50) + chr(50) + chr(255) w = chr(230) + chr(230) + chr(230) + chr(255) for y in range(0, 16): for x in range(0, 16): image = image + b for x in range(0, 16): image = image + w for y in range(0, 16): for x in range(0, 16): image = image + w for x in range(0, 16): image = image + b # opengl fun textures = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, textures) glPixelStorei(GL_UNPACK_ALIGNMENT,1) gluBuild2DMipmaps(GL_TEXTURE_2D,4,ix,iy,GL_RGBA,GL_UNSIGNED_BYTE,image) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glEnable(GL_TEXTURE_2D) #glMatrixMode(GL_TEXTURE) #glScalef(15.0, 15.0, 15.0); glMatrixMode(GL_MODELVIEW) def free_textures(): global textures glDeleteTextures(textures) def init_gl(): global gwidth, gheight glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glShadeModel(GL_FLAT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(gwidth)/float(gheight), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) # smooth lines glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_LINE_SMOOTH); def resize(Width, Height): global gwidth, gheight gwidth = Width gheight = Height if Height == 0: Height = 1 glViewport(0, 0, Width, Height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) def draw_gl(): global xrot, yrot, zrot, texture, gwidth, gheight glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() vBitmapGL.draw_all() vGeometryGL.draw_all() glutSwapBuffers() # not working def draw_text(value, x, y): global gwidth, gheight glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, gwidth or 32, 0.0, gheight or 32) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRasterPos2f(x, y); lines = 0 for character in value: if character == '\n': glRasterPos2f(x, y-(lines*12)) else: glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ord(character)); # (key, x, y) def key_pressed(*args): # octal escape thingie if args[0] == '\033': sys.exit() elif args[0] == 'w': vGeometryGL.wire = not vGeometryGL.wire elif args[0] == 'r': vGeometryGL.rotate = not vGeometryGL.rotate elif args[0] == 's': vGeometryGL.solid = (vGeometryGL.solid + 1) % 3 elif args[0] == 'l': vGeometryGL.light = not vGeometryGL.light elif args[0] == 'p': vGeometryGL.points = not vGeometryGL.points def mouse_motion(x, y): vGeometryGL.yrot = x vGeometryGL.xrot = y def idle(): vManager.callback_update() draw_gl() def main(): global address, gwidth, gheight print "w - draw wire" print "r - auto rotate" print "s - drawmode" print "l - light" print "p - draw points" vGeometryGL.set_handler(vGeometryHandler) vBitmapGL.set_handler(vBitmapHandler) vManager.set_handler(vGeometryHandler) vManager.set_handler(vBitmapHandler) vManager.connect("uname", "pass", address) vManager.callback_update() glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH) glutInitWindowSize(gwidth, gheight) glutInitWindowPosition(0, 0) window = glutCreateWindow("LookAtMe") glutDisplayFunc(draw_gl) glutIdleFunc(idle) glutReshapeFunc(resize) glutKeyboardFunc(key_pressed) glutMotionFunc(mouse_motion) load_textures() init_gl() glutMainLoop() free_textures() vManager.terminate() # go! main()