2018MassSpringLab1Fall - dknife/Graduate2017_Autumn GitHub Wiki

ClothObject.py

from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *

import numpy as np
import math

class ClothObject :
    def __init__(self, sizeX, sizeZ, nW, nH):

        self.dx = sizeX / (nW-1)
        self.dz = sizeZ / (nH-1)
        self.dDiag = math.sqrt(self.dx**2.0 + self.dz**2.0)

        self.nW, self.nH = nW, nH
        self.clothSize = (sizeX, sizeZ)
        # vertex array (x,y,z)
        self.verts = np.zeros(shape=(nW * nH, 3))
        self.velocity = np.zeros(shape=(nW*nH, 3))

        # spring links (i, j)
        self.springs = \
            np.zeros(shape=((nW - 1) * nH + (nH - 1) * nW + (nW - 1) * (nH - 1) * 2, 2), 
                     dtype=np.int32)
        # relaxed length of spring : l0
        self.l0 = np.zeros(shape=(len(self.springs),))



        self.numXSprings = (self.nW - 1) * self.nH
        self.numZSprings = (self.nH - 1) * self.nW
        self.numDiagonalSprings = (self.nW - 1) * (self.nH - 1)

        self.resetMassSpring()


    def resetMassSpring(self):

        for i in range(0, self.nW * self.nH):
            self.verts[i] = [(i % self.nW) * self.dx, 1, (int(i / self.nW)) * self.dz]
            self.velocity[i] = [0,0,0]

        for i in range(0, self.numXSprings):
            row = int(i / (self.nW - 1))
            col = i % (self.nW - 1)
            self.springs[i] = [row * self.nW + col, row * self.nW + col + 1]
            self.l0[i] = self.dx

        for i in range(0, self.numZSprings):
            row = int(i / (self.nW))
            col = i % (self.nW)
            self.springs[self.numXSprings + i] = \
                [row * self.nW + col, (row + 1) * self.nW + col]
            self.l0[(self.nW - 1) * self.nH + i] = self.dz

        for i in range(0, self.numDiagonalSprings):
            row = int(i / (self.nW - 1))
            col = i % (self.nW - 1)
            self.springs[self.numXSprings + self.numZSprings + 2 * i] \
                = [row * self.nW + col,(row + 1) * self.nW + col + 1]
            self.l0[self.numXSprings + self.numZSprings + 2 * i] = self.dDiag

            self.springs[self.numXSprings + self.numZSprings + 2 * i + 1] \
                = [row * self.nW + col + 1,(row + 1) * self.nW + col]
            self.l0[self.numXSprings + self.numZSprings + 2 * i + 1] = self.dDiag

    def drawSpring(self):
        glColor3f(1, 1, 1)

        glBegin(GL_LINES)
        for i in range(0, len(self.springs)) :
            idx0 = self.springs[i][0]
            idx1 = self.springs[i][1]
            loc0 = self.verts[idx0]
            loc1 = self.verts[idx1]
            glVertex3fv(loc0)
            glVertex3fv(loc1)
        glEnd()

    def update(self, dt):
        numParticles = self.nW * self.nH
        gravity = np.array([0.0, -9.8, 0.0])

        for i in range (0, numParticles) :
            self.velocity[i] = self.velocity[i] + gravity * dt
            self.verts[i] = self.verts[i] + self.velocity[i]*dt
            #### Simple Euler method for gravity simulation

main.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")
        self.stiffnessSlider = wx.Slider(self, -1, pos=(1030, 180), size=(200,50),
                                         style = wx.SL_HORIZONTAL|wx.SL_AUTOTICKS,
                                  value=0, minValue=-5, maxValue = 5)
        self.stepLabel = wx.StaticText(self, -1, pos=(1030, 250), style=wx.ALIGN_CENTER)
        self.stepLabel.SetLabel("Time Step")
        self.stepSlider = wx.Slider(self, -1, pos=(1030, 280), size=(200, 50),
                                    style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS,
                                  value=0, minValue=-5, maxValue=5)

        self.Bind(wx.EVT_BUTTON, self.OnAnimationButton, self.AnimationButton)
        self.Bind(wx.EVT_BUTTON, self.OnResetButton, self.ResetButton)

    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()



class OpenGLCanvas(glcanvas.GLCanvas):
    def __init__(self, parent) :
        self.initialized = False
        self.size = (1024,720)
        self.aspect_ratio = 1
        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,5,5)
        self.bAnimation = False

    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(0.01)
        self.Refresh()


def main() :
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()


if __name__ == "__main__" :
    main()