source: src/models/Shape.pym @ 1212:07172e3bab54

Revision 1212:07172e3bab54, 5.0 KB checked in by Arc Riley <arcriley@…>, 7 years ago (diff)

Ticket #901 :

  • more Pyrex 0.9.8 code updates, these mostly for soy.models
Line 
1# PySoy's models.Shape class
2#
3# Copyright (C) 2006,2007,2008 PySoy Group
4#
5#  This program is free software; you can redistribute it and/or modify
6#  it under the terms of the GNU Affero General Public License as published
7#  by the Free Software Foundation, either version 3 of the License, or
8#  (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU Affero General Public License for more details.
14#
15#  You should have received a copy of the GNU Affero General Public License
16#  along with this program; if not, see http://www.gnu.org/licenses
17#
18# $Id: Shape.pym 1258 2008-05-15 23:54:16Z ArcRiley $
19
20cdef class Shape (Model) :
21  '''soy.models.Shape
22
23    This mesh renders the shape of the body it belongs to.
24  '''
25
26  ############################################################################
27  #
28  # Python functions
29  #
30 
31  def __cinit__(self, material=None,
32                *args, **keywords) :
33    self.material = material
34
35
36  ############################################################################
37  #
38  # Properties
39  #
40
41  property material :
42    def __get__(self) :
43      return self._material
44
45    def __set__(self, value) :
46      if not value :
47        self._material = soy.materials.StainlessSteel()
48      elif isinstance(value, soy.materials.Material) :
49        self._material = value
50      else :
51        raise TypeError('Must provide an instance of soy.materials.Material')
52
53
54  ############################################################################
55  #
56  # WindowLoop Functions
57  #
58
59  cdef void _render(self, soy.bodies.Body _body) :
60    cdef int              _shapeclass
61    #
62    # Don't attempt to render a non-existant shape
63    if _body._shape is None :
64      return
65    #
66    # Grab the shape's class
67    _shapeclass = ode.dGeomGetClass(_body._shape._geomID)
68    #
69    # Bind the material
70    self._material._coreBind()
71    #
72    # Render the specific shape
73    if _shapeclass == ode.dSphereClass :
74      self._renderSphere(_body._shape)
75    elif _shapeclass == ode.dCapsuleClass :
76      self._renderCapsule(_body._shape)
77    elif _shapeclass == ode.dBoxClass :
78      self._renderBox(_body._shape)
79    elif _shapeclass == ode.dRayClass :
80      self._renderRay(_body._shape)
81    #
82    # Unbind the material
83    self._material._coreUnBind()
84
85
86  cdef void _renderSphere(self, soy.shapes.Shape _shape) :
87    cdef gl.GLUquadricObj* _quadric
88    _quadric = gl.gluNewQuadric()
89    gl.gluQuadricNormals(_quadric, gl.GLU_SMOOTH)
90    gl.gluQuadricTexture(_quadric, gl.GL_FALSE)
91    gl.gluSphere(_quadric, _shape._radius(), 32, 32)
92    gl.gluDeleteQuadric(_quadric)
93
94
95  cdef void _renderCapsule(self, soy.shapes.Shape _shape) :
96    cdef ode.dReal r, l
97    cdef gl.GLUquadricObj* _quadric
98    ode.dGeomCapsuleGetParams(_shape._geomID, &r, &l)
99    _quadric = gl.gluNewQuadric()
100    gl.gluQuadricNormals(_quadric, gl.GLU_SMOOTH)
101    gl.gluQuadricTexture(_quadric, gl.GL_FALSE)
102    gl.glPushMatrix()
103    gl.glTranslatef(0.0, 0.0, -l * 0.5)
104    gl.gluCylinder(_quadric, r, r, l, 32, 32)
105    gl.gluSphere(_quadric, r, 32, 32)
106    gl.glPushMatrix()
107    gl.glTranslatef(0.0, 0.0, l)
108    gl.gluSphere(_quadric, r, 32, 32)
109    gl.glPopMatrix()
110    gl.glPopMatrix()
111    gl.gluDeleteQuadric(_quadric)
112
113
114  cdef void _renderBox(self, soy.shapes.Shape _shape) :
115    cdef ode.dVector3 _xyz
116    ode.dGeomBoxGetLengths(_shape._geomID, _xyz)
117    gl.glPushMatrix()
118    gl.glScalef(_xyz[0], _xyz[1], _xyz[2])
119    gl.glBegin(gl.GL_QUADS)
120    gl.glNormal3f( 0.0, 0.0, 1.0)
121    gl.glVertex3f(-0.5, -0.5,  0.5)
122    gl.glVertex3f( 0.5, -0.5,  0.5)
123    gl.glVertex3f( 0.5,  0.5,  0.5)
124    gl.glVertex3f(-0.5,  0.5,  0.5)
125    gl.glNormal3f( 0.0, 0.0,-1.0)
126    gl.glVertex3f(-0.5, -0.5, -0.5)
127    gl.glVertex3f(-0.5,  0.5, -0.5)
128    gl.glVertex3f( 0.5,  0.5, -0.5)
129    gl.glVertex3f( 0.5, -0.5, -0.5)
130    gl.glNormal3f( 0.0, 1.0, 0.0)
131    gl.glVertex3f(-0.5,  0.5, -0.5)
132    gl.glVertex3f(-0.5,  0.5,  0.5)
133    gl.glVertex3f( 0.5,  0.5,  0.5)
134    gl.glVertex3f( 0.5,  0.5, -0.5)
135    gl.glNormal3f( 0.0,-1.0, 0.0)
136    gl.glVertex3f(-0.5, -0.5, -0.5)
137    gl.glVertex3f( 0.5, -0.5, -0.5)
138    gl.glVertex3f( 0.5, -0.5,  0.5)
139    gl.glVertex3f(-0.5, -0.5,  0.5)
140    gl.glNormal3f( 1.0, 0.0, 0.0)
141    gl.glVertex3f( 0.5, -0.5, -0.5)
142    gl.glVertex3f( 0.5,  0.5, -0.5)
143    gl.glVertex3f( 0.5,  0.5,  0.5)
144    gl.glVertex3f( 0.5, -0.5,  0.5)
145    gl.glNormal3f(-1.0, 0.0, 0.0)
146    gl.glVertex3f(-0.5, -0.5, -0.5)
147    gl.glVertex3f(-0.5, -0.5,  0.5)
148    gl.glVertex3f(-0.5,  0.5,  0.5)
149    gl.glVertex3f(-0.5,  0.5, -0.5)
150    gl.glEnd()
151    gl.glPopMatrix()
152
153
154  cdef void _renderRay(self, soy.shapes.Shape _shape) :
155    cdef ode.dVector3 s, d
156    cdef float l
157    ode.dGeomRayGet(_shape._geomID, s, d)
158    l = ode.dGeomRayGetLength(_shape._geomID)
159    gl.glPushAttrib(gl.GL_LIGHTING_BIT)
160    gl.glDisable(gl.GL_LIGHTING)
161    gl.glBegin(gl.GL_LINES)
162    gl.glVertex3f(s[0], s[1], s[2])
163    gl.glVertex3f(s[0] + d[0] * l, s[1] + d[0] * l, s[2] + d[0] * l)
164    gl.glEnd()
165    gl.glPopAttrib()
Note: See TracBrowser for help on using the repository browser.