danke im voraus
Feanor
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import wx
import sys
from wx import glcanvas
from OpenGL.GL import *
from OpenGL.GLUT import *
class MyCanvasBase(glcanvas.GLCanvas):
def __init__(self,parent):
glcanvas.GLCanvas.__init__(self,parent, -1,style=wx.WANTS_CHARS)
self.init = False
#initial mouse position
self.lastx = self.x = 0
self.lasty = self.y = 0
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
def OnSize(self, event):
size = self.size = self.GetClientSize()
if self.GetContext():
self.SetCurrent()
glViewport(0,0,size.width,size.height)
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.lasty, 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 InitGL(self):
glMatrixMode(GL_PROJECTION)
glFrustum(-1.25,1.25,-1.25,1.25,1.0,4.25)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0.0,0.0,-3.6)
self.Bind(wx.EVT_KEY_UP,self.OnKeyUp)
glRotatef(self.y,1.0,0.0,0.0)
glRotatef(self.x,0.0,1.0,0.0)
self.cubea=[ [[None,None,None],[None,None,None],[None,None,None]],
[[None,None,None],[None,None,None],[None,None,None]],
[[None,None,None],[None,None,None],[None,None,None]]]
for i in range(0,3):
for j in range(0,3):
for l in range(0,3):
self.cubea[i-1][j-1][l-1]=( (1.0,1.0,1.0) if (j==2) else (0.3,0.3,0.3),
(1.0,0.0,0.0) if (j==0) else (0.3,0.3,0.3),
(0.0,1.0,0.0) if (i==0) else (0.3,0.3,0.3),
(0.0,0.0,1.0) if (i==2) else (0.3,0.3,0.3),
(1.0,1.0,0.0) if (l==2) else (0.3,0.3,0.3),
(0.0,1.0,1.0) if (l==0) else (0.3,0.3,0.3))
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_LIGHT0)
def OnDraw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBegin(GL_QUADS)
self.drawCubes()
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)
self.SwapBuffers()
def cube(self,x,y,z,s,colorTop,colorBottom,colorLeft,colorRight,colorFront,colorBack):
self.cubea[x][y][z]=(colorTop,colorBottom,colorLeft,colorRight,colorFront,colorBack)
#vorne
glNormal3f(0.0,0.0,1.0)
glColor3fv(colorFront)
glVertex3f(s/2+x,s/2+y,s/2+z)
glVertex3f(-s/2+x,s/2+y,s/2+z)
glVertex3f(-s/2+x,-s/2+y,s/2+z)
glVertex3f(s/2+x,-s/2+y,s/2+z)
#hinten
glNormal3f(0.0,0.0,-1.0)
glColor3fv(colorBack)
glVertex3f(s/2+x,s/2+y,-s/2+z)
glVertex3f(-s/2+x,s/2+y,-s/2+z)
glVertex3f(-s/2+x,-s/2+y,-s/2+z)
glVertex3f(s/2+x,-s/2+y,-s/2+z)
#rechts
glNormal3f(1.0,0.0,0.0)
glColor3fv(colorRight)
glVertex3f(s/2+x,s/2+y,s/2+z)
glVertex3f(s/2+x,s/2+y,-s/2+z)
glVertex3f(s/2+x,-s/2+y,-s/2+z)
glVertex3f(s/2+x,-s/2+y,s/2+z)
#links
glNormal3f(-1.0,0.0,0.0)
glColor3fv(colorLeft)
glVertex3f(-s/2+x,s/2+y,s/2+z)
glVertex3f(-s/2+x,s/2+y,-s/2+z)
glVertex3f(-s/2+x,-s/2+y,-s/2+z)
glVertex3f(-s/2+x,-s/2+y,s/2+z)
#oben
glNormal3f(0.0,1.0,0.0)
glColor3fv(colorTop)
glVertex3f(s/2+x,s/2+y,s/2+z)
glVertex3f(s/2+x,s/2+y,-s/2+z)
glVertex3f(-s/2+x,s/2+y,-s/2+z)
glVertex3f(-s/2+x,s/2+y,s/2+z)
#unten
glNormal3f(0.0,-1.0,0.0)
glColor3fv(colorBottom)
glVertex3f(s/2+x,-s/2+y,s/2+z)
glVertex3f(s/2+x,-s/2+y,-s/2+z)
glVertex3f(-s/2+x,-s/2+y,-s/2+z)
glVertex3f(-s/2+x,-s/2+y,s/2+z)
def drawCubes(self):
for i in range(0,3):
for j in range(0,3):
for l in range(0,3):
self.cube( i-1,
j-1,
l-1,
0.9,
self.cubea[i-1][j-1][l-1][0],
self.cubea[i-1][j-1][l-1][1],
self.cubea[i-1][j-1][l-1][2],
self.cubea[i-1][j-1][l-1][3],
self.cubea[i-1][j-1][l-1][4],
self.cubea[i-1][j-1][l-1][5]
)
def OnKeyUp(self, event):
print "key pressed"
class MainWindow(wx.Frame):
def __init__(self, parent = None, id = -1, title = "Rubik's Cube"):
# Init
wx.Frame.__init__(
self, parent, id, title, size = (400,400),
style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE | wx.WANTS_CHARS
)
# OpenGL
self.control = CubeCanvas(self)
# StatusBar
self.CreateStatusBar()
# Filemenu
filemenu = wx.Menu()
# Filemenu - About
menuitem = filemenu.Append(-1, "&About", "Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, menuitem) # here comes the event-handler
# Filemenu - Separator
filemenu.AppendSeparator()
# Filemenu - Exit
menuitem = filemenu.Append(-1, "E&xit", "Terminate the program")
self.Bind(wx.EVT_MENU, self.OnExit, menuitem) # here comes the event-handler
# Menubar
menubar = wx.MenuBar()
menubar.Append(filemenu,"&File")
self.SetMenuBar(menubar)
# Show
self.Show(True)
def OnAbout(self,event):
message = "A sample editor\n in wxPython"
caption = "About Sample Editor"
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