ClothObject.py
import wx # requires wxPython package
from wx import glcanvas
from OpenGL.GL import *
from OpenGL.GLU import *
import ClothObject
class MyFrame(wx.Frame) :
def __init__(self):
self.size = (1280, 720)
wx.Frame.__init__(self, None, title = "wx frame", size = self.size,
style = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
self.panel = MyPanel(self)
class MyPanel(wx.Panel) :
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.canvas = OpenGLCanvas(self)
self.bAnimation = False
self.resetButton = wx.Button(self, wx.ID_ANY, "Mass-Spring Reset", pos=(1030, 20), size=(200,40), style=0 )
self.animationButton = wx.Button(self, wx.ID_ANY, "Animate/Stop", pos=(1030, 60), size=(200, 40), style=0)
self.stiffnessLabel = wx.StaticText(self, -1, pos=(1030, 150), style=wx.ALIGN_CENTER)
self.stiffnessLabel.SetLabel("Stiffness: " + str(self.canvas.clothObject.stiffness))
self.stiffnessSlider = wx.Slider(self, -1, pos=(1030, 180), size=(200,50),
style = wx.SL_HORIZONTAL|wx.SL_AUTOTICKS,
value=1, minValue=1, maxValue = 30)
self.stepLabel = wx.StaticText(self, -1, pos=(1030, 250), style=wx.ALIGN_CENTER)
self.stepLabel.SetLabel("Time Interval: " + str(self.canvas.stepSize))
self.stepSlider = wx.Slider(self, -1, pos=(1030, 280), size=(200, 50),
style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS,
value=10, minValue=1, maxValue=300)
self.Bind(wx.EVT_BUTTON, self.OnAnimationButton, self.animationButton)
self.Bind(wx.EVT_BUTTON, self.OnResetButton, self.resetButton)
self.Bind(wx.EVT_SLIDER, self.OnStiffnessSlider, self.stiffnessSlider)
self.Bind(wx.EVT_SLIDER, self.OnStepSlider, self.stepSlider)
def OnAnimationButton(self, event) :
if self.bAnimation is False :
self.bAnimation = True
else :
self.bAnimation = False
self.canvas.bAnimation = self.bAnimation
def OnResetButton(self, event) :
self.canvas.clothObject.resetMassSpring()
def OnStiffnessSlider(self, event):
val = event.GetEventObject().GetValue()
stiffness = 2**val
self.stiffnessLabel.SetLabel("Stiffness :" + str(stiffness))
self.canvas.clothObject.stiffness = stiffness
def OnStepSlider(self, event):
val = event.GetEventObject().GetValue()
stepSize = 0.0001*val
self.stepLabel.SetLabel("Time Interval :" + str(stepSize))
self.canvas.stepSize = stepSize
class OpenGLCanvas(glcanvas.GLCanvas):
def __init__(self, parent) :
self.initialized = False
self.size = (1024,720)
self.aspect_ratio = 1
self.stepSize = 0.001
glcanvas.GLCanvas.__init__(self, parent, -1, size = self.size)
self.context = glcanvas.GLContext(self)
self.SetCurrent(self.context)
self.Bind(wx.EVT_PAINT, self.OnDraw)
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.InitGL()
self.clothObject = ClothObject.ClothObject(1,1,10,10)
self.bAnimation = False
def setAnimationFlag(self, bOption):
self.bAnimation = bOption
def resetMesh(self):
self.clothObject.resetMassSpring()
def InitGL(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
self.aspect_ratio = float(self.size[0]) / self.size[1]
gluPerspective(60, self.aspect_ratio, 0.1, 100.0)
glViewport(0,0,self.size[0], self.size[1])
def OnDraw(self, event):
# clear color and depth buffers
if not self.initialized :
self.InitGL()
self.initialized = True
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# position viewer
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(2,2,2, 0,0,0, 0,1,0)
self.clothObject.drawSpring()
self.SwapBuffers()
def OnIdle(self, event):
if self.bAnimation :
self.clothObject.update(self.stepSize)
self.Refresh()
def main() :
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()
if __name__ == "__main__" :
main()