rendered paste body#!/usr/bin/env python# -*- coding: iso-8859-1 -*-#url: http://showmedo.com/videotutorials/video?name=1510030&fromSeriesID=151#url: http://steinsoft.net/index.php?site=Programming/Code%20Snippets/OpenGL/gluperspectiveimport wximport sysfrom wx import glcanvasfrom OpenGL.GL import *from OpenGL.GLU import *class MyCanvasBase(glcanvas.GLCanvas): def __init__(self, parent): glcanvas.GLCanvas.__init__(self, parent, -1, attribList=[wx.glcanvas.WX_GL_DOUBLEBUFFER]) self.parent = parent #just for safe keeping self.init = False # initial mouse position self.lastx = self.x = 30 self.lasty = self.y = 30 self.size = None self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown) self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp) self.Bind(wx.EVT_MOTION, self.OnMouseMotion) def OnEraseBackground(self, event): pass # Do nothing, to avoid flashing on MSW. def OnSize(self, event): size = self.size = self.GetClientSize() if self.GetContext(): self.SetCurrent() #glViewport(0, 0, size.width, size.height) glViewport(0, 0, 300, 300) event.Skip() def OnPaint(self, event): dc = wx.PaintDC(self) self.SetCurrent() if not self.init: self.InitGL() self.init = True self.OnDraw() def OnMouseDown(self, evt): self.CaptureMouse() self.x, self.y = self.lastx, self.lasty = evt.GetPosition() def OnMouseUp(self, evt): self.ReleaseMouse() def OnMouseMotion(self, evt): if evt.Dragging() and evt.LeftIsDown(): self.lastx, self.lasty = self.x, self.y self.x, self.y = evt.GetPosition() self.Refresh(False)class CubeCanvas(MyCanvasBase): def old_InitGL(self): # set viewing projection glMatrixMode(GL_PROJECTION) #glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0) width = self.parent.GetSize().width height = self.parent.GetSize().height glLoadIdentity() gluPerspective(45.0, width/float(height), 0.1, 100.0) #gluPerspective(45.0, 1.0, 0.1, 100.0) # position viewer glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0.0, 0.0, -2.0) # position object glRotatef(self.y, 1.0, 0.0, 0.0) glRotatef(self.x, 0.0, 1.0, 0.0) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) def InitGL(self): glClearColor(0, 0, 0, 1) glClearDepth(1) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) glEnable(GL_COLOR_MATERIAL) width = self.parent.GetSize().width height = self.parent.GetSize().height topleft_x, topleft_y, bottomright_x, bottomright_y = 0, 0, width, height glViewport(topleft_x, topleft_y, bottomright_x-topleft_x, bottomright_y-topleft_y) glMatrixMode(GL_PROJECTION) glLoadIdentity() view_angle = 45 ratio_w_h = float(float(float(bottomright_x)-float(topleft_x)) / float(float(bottomright_y)-float(topleft_y))) clip_close = 0.1 clip_far = 200 gluPerspective(view_angle, ratio_w_h, clip_close, clip_far) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def old_OnDraw(self): # clear color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #glLoadIdentity() # draw six faces of a cube glBegin(GL_QUADS) glNormal3f( 0.0, 0.0, 1.0) glVertex3f( 0.5, 0.5, 0.5) glVertex3f(-0.5, 0.5, 0.5) glVertex3f(-0.5,-0.5, 0.5) glVertex3f( 0.5,-0.5, 0.5) glNormal3f( 0.0, 0.0,-1.0) glVertex3f(-0.5,-0.5,-0.5) glVertex3f(-0.5, 0.5,-0.5) glVertex3f( 0.5, 0.5,-0.5) glVertex3f( 0.5,-0.5,-0.5) glNormal3f( 0.0, 1.0, 0.0) glVertex3f( 0.5, 0.5, 0.5) glVertex3f( 0.5, 0.5,-0.5) glVertex3f(-0.5, 0.5,-0.5) glVertex3f(-0.5, 0.5, 0.5) glNormal3f( 0.0,-1.0, 0.0) glVertex3f(-0.5,-0.5,-0.5) glVertex3f( 0.5,-0.5,-0.5) glVertex3f( 0.5,-0.5, 0.5) glVertex3f(-0.5,-0.5, 0.5) glNormal3f( 1.0, 0.0, 0.0) glVertex3f( 0.5, 0.5, 0.5) glVertex3f( 0.5,-0.5, 0.5) glVertex3f( 0.5,-0.5,-0.5) glVertex3f( 0.5, 0.5,-0.5) glNormal3f(-1.0, 0.0, 0.0) glVertex3f(-0.5,-0.5,-0.5) glVertex3f(-0.5,-0.5, 0.5) glVertex3f(-0.5, 0.5, 0.5) glVertex3f(-0.5, 0.5,-0.5) glEnd() #if self.size is None: # self.size = self.GetClientSize() #w, h = self.size #w = max(w, 1.0) #h = max(h, 1.0) #xScale = 180.0 / w #yScale = 180.0 / h #glRotatef((self.y - self.lasty) * yScale, 1.0, 0.0, 0.0); #glRotatef((self.x - self.lastx) * xScale, 0.0, 1.0, 0.0); glPushMatrix() glTranslate(0.0, 0.0, -2.0) glPopMatrix() self.SwapBuffers() def OnDraw(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.InitGL() glLoadIdentity() glTranslatef(0,0,-5) glRotatef(50, 0, 1, 0) glColor4f(1, 0, 0, 1) # draw six faces of a cube glBegin(GL_QUADS) glNormal3f( 0.0, 0.0, 1.0) glVertex3f( 0.5, 0.5, 0.5) glVertex3f(-0.5, 0.5, 0.5) glVertex3f(-0.5,-0.5, 0.5) glVertex3f( 0.5,-0.5, 0.5) glNormal3f( 0.0, 0.0,-1.0) glVertex3f(-0.5,-0.5,-0.5) glVertex3f(-0.5, 0.5,-0.5) glVertex3f( 0.5, 0.5,-0.5) glVertex3f( 0.5,-0.5,-0.5) glNormal3f( 0.0, 1.0, 0.0) glVertex3f( 0.5, 0.5, 0.5) glVertex3f( 0.5, 0.5,-0.5) glVertex3f(-0.5, 0.5,-0.5) glVertex3f(-0.5, 0.5, 0.5) glNormal3f( 0.0,-1.0, 0.0) glVertex3f(-0.5,-0.5,-0.5) glVertex3f( 0.5,-0.5,-0.5) glVertex3f( 0.5,-0.5, 0.5) glVertex3f(-0.5,-0.5, 0.5) glNormal3f( 1.0, 0.0, 0.0) glVertex3f( 0.5, 0.5, 0.5) glVertex3f( 0.5,-0.5, 0.5) glVertex3f( 0.5,-0.5,-0.5) glVertex3f( 0.5, 0.5,-0.5) glNormal3f(-1.0, 0.0, 0.0) glVertex3f(-0.5,-0.5,-0.5) glVertex3f(-0.5,-0.5, 0.5) glVertex3f(-0.5, 0.5, 0.5) glVertex3f(-0.5, 0.5,-0.5) glEnd() glFlush() self.SwapBuffers() #glPushMatrix() #glTranslate(0.0, 0.0, -2.0) #glPopMatrix()class ConeCanvas(MyCanvasBase): def InitGL( self ): glMatrixMode(GL_PROJECTION) # camera frustrum setup glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0) glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0]) glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0]) glMaterial(GL_FRONT, GL_SHININESS, 50.0) glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0]) glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]) glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # position viewer glMatrixMode(GL_MODELVIEW) # position viewer glTranslatef(0.0, 0.0, -2.0); # glutInit(sys.argv) def OnDraw(self): # clear color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # use a fresh transformation matrix glPushMatrix() # position object #glTranslate(0.0, 0.0, -2.0) glRotate(30.0, 1.0, 0.0, 0.0) glRotate(30.0, 0.0, 1.0, 0.0) glTranslate(0, -1, 0) glRotate(250, 1, 0, 0) glutSolidCone(0.5, 1, 30, 5) glPopMatrix() glRotatef((self.y - self.lasty), 0.0, 0.0, 1.0); glRotatef((self.x - self.lastx), 1.0, 0.0, 0.0); # push into visible buffer self.SwapBuffers()class MainWindow(wx.Frame): def __init__(self, parent = None, id = -1, title = "PyOpenGL Example 1"): # Init wx.Frame.__init__( self, parent, id, title, size = (600,600), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE ) # TextCtrl # self.control = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE) #self.control = ConeCanvas(self) box = wx.BoxSizer(wx.HORIZONTAL) #self.cone_canvas = ConeCanvas(self) self.cube_canvas = CubeCanvas(self) #box.Add(self.cone_canvas, 1, wx.EXPAND) box.Add(self.cube_canvas, 1, wx.EXPAND) self.SetSizer(box) #self.SetAutoLayout(True) #default # StatusBar self.CreateStatusBar() self.SetStatusText("supercad9000") # Filemenu filemenu = wx.Menu() self.Layout() # Filemenu - About menuitem = filemenu.Append(-1, "&About", "Information about this program") self.Bind(wx.EVT_MENU, self.OnAbout, menuitem) # Filemenu - Separator filemenu.AppendSeparator() # Filemenu - Exit menuitem = filemenu.Append(-1, "E&xit", "Terminate the program") self.Bind(wx.EVT_MENU, self.OnExit, menuitem) # Menubar menubar = wx.MenuBar() menubar.Append(filemenu,"&File") self.SetMenuBar(menubar) # Show self.Show(True) def OnAbout(self,event): message = "Using PyOpenGL in wxPython" caption = "About PyOpenGL Example" wx.MessageBox(message, caption, wx.OK) def OnExit(self,event): self.Close(True) # Close the frame.app = wx.PySimpleApp()frame = MainWindow()app.MainLoop()# destroying the objects, so that this script works more than once in IDLEdieses Beispiel#del frame#del app