-
Notifications
You must be signed in to change notification settings - Fork 41
/
pygame_example.py
112 lines (93 loc) · 3.86 KB
/
pygame_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Sofa
import Sofa.SofaGL
import SofaRuntime
import Sofa.Simulation as sim
import os
import time
sofa_directory = os.environ['SOFA_ROOT']
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
display_size = (800, 600)
def init_display(node):
pygame.display.init()
pygame.display.set_mode(display_size, pygame.DOUBLEBUF | pygame.OPENGL)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
Sofa.SofaGL.glewInit()
Sofa.Simulation.initVisual(node)
Sofa.Simulation.initTextures(node)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (display_size[0] / display_size[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def simple_render(rootNode):
"""
Get the OpenGL Context to render an image (snapshot) of the simulation state
"""
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (display_size[0] / display_size[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
cameraMVM = rootNode.camera.getOpenGLModelViewMatrix()
glMultMatrixd(cameraMVM)
Sofa.SofaGL.draw(rootNode)
pygame.display.get_surface().fill((0,0,0))
pygame.display.flip()
def createScene(root):
# Register all the common component in the factory.
SofaRuntime.PluginRepository.addFirstPath(os.path.join(sofa_directory, 'bin'))
root.addObject("RequiredPlugin", name="Sofa.Component")
root.addObject("RequiredPlugin", name="Sofa.GL.Component")
### these are just some things that stay still and move around
# so you know the animation is actually happening
root.gravity = [0, -1., 0]
root.addObject("VisualStyle", displayFlags="showAll")
root.addObject("MeshGmshLoader", name="meshLoaderCoarse",
filename="mesh/liver.msh")
root.addObject("MeshOBJLoader", name="meshLoaderFine",
filename="mesh/liver-smooth.obj")
root.addObject("EulerImplicitSolver")
root.addObject("CGLinearSolver", iterations="200",
tolerance="1e-09", threshold="1e-09")
liver = root.addChild("liver")
liver.addObject("TetrahedronSetTopologyContainer",
name="topo", src="@../meshLoaderCoarse")
liver.addObject("TetrahedronSetGeometryAlgorithms",
template="Vec3d", name="GeomAlgo")
liver.addObject("MechanicalObject",
template="Vec3d",
name="MechanicalModel", showObject="1", showObjectScale="3")
liver.addObject("TetrahedronFEMForceField", name="fem", youngModulus="1000",
poissonRatio="0.4", method="large")
liver.addObject("MeshMatrixMass", massDensity="1")
liver.addObject("FixedConstraint", indices="2 3 50")
visual = liver.addChild("visual")
visual.addObject('MeshOBJLoader', name="meshLoader_0", filename="mesh/liver-smooth.obj", handleSeams="1")
visual.addObject('OglModel', name="VisualModel", src="@meshLoader_0", color='red')
visual.addObject('BarycentricMapping', input="@..", output="@VisualModel", name="visual mapping")
# place light and a camera
root.addObject("LightManager")
root.addObject("DirectionalLight", direction=[0,1,0])
root.addObject("InteractiveCamera", name="camera", position=[0,15, 0],
lookAt=[0,0,0], distance=37,
fieldOfView=45, zNear=0.63, zFar=55.69)
if __name__ == '__main__':
root = Sofa.Core.Node("myroot")
createScene(root)
Sofa.Simulation.init(root)
init_display(root)
try:
while True:
Sofa.Simulation.animate(root, root.getDt())
Sofa.Simulation.updateVisual(root)
simple_render(root)
time.sleep(root.getDt())
except KeyboardInterrupt:
pass