source: src/models/Shape.pym @ 1211:67d37d97d815

Revision 1211:67d37d97d815, 5.1 KB checked in by Arc Riley <arcriley@…>, 6 years ago (diff)

Ticket #901 :

  • upgrade to Pyrex 0.9.8
  • implement cyclic cimport fix in bodies
  • update API respectively, removing redundant typecasts
  • nogil: in use for bodies
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 1257 2008-05-15 22:19:30Z 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, void* _body) :
60    cdef soy.shapes.Shape _shape
61    cdef int              _shapeclass
62    #
63    # Don't attempt to render a non-existant shape
64    if (<soy.bodies.Body> _body)._shape is None :
65      return
66    #
67    # Grab the body's shape
68    _shape = (<soy.bodies.Body> _body)._shape
69    _shapeclass = ode.dGeomGetClass(_shape._geomID)
70    #
71    # Bind the material
72    self._material._coreBind()
73    #
74    # Render the specific shape
75    if _shapeclass == ode.dSphereClass :
76      self._renderSphere(_shape)
77    elif _shapeclass == ode.dCapsuleClass :
78      self._renderCapsule(_shape)
79    elif _shapeclass == ode.dBoxClass :
80      self._renderBox(_shape)
81    elif _shapeclass == ode.dRayClass :
82      self._renderRay(_shape)
83    #
84    # Unbind the material
85    self._material._coreUnBind()
86
87
88  cdef void _renderSphere(self, soy.shapes.Shape _shape) :
89    cdef gl.GLUquadricObj* _quadric
90    _quadric = gl.gluNewQuadric()
91    gl.gluQuadricNormals(_quadric, gl.GLU_SMOOTH)
92    gl.gluQuadricTexture(_quadric, gl.GL_FALSE)
93    gl.gluSphere(_quadric, _shape._radius(), 32, 32)
94    gl.gluDeleteQuadric(_quadric)
95
96
97  cdef void _renderCapsule(self, soy.shapes.Shape _shape) :
98    cdef ode.dReal r, l
99    cdef gl.GLUquadricObj* _quadric
100    ode.dGeomCapsuleGetParams(_shape._geomID, &r, &l)
101    _quadric = gl.gluNewQuadric()
102    gl.gluQuadricNormals(_quadric, gl.GLU_SMOOTH)
103    gl.gluQuadricTexture(_quadric, gl.GL_FALSE)
104    gl.glPushMatrix()
105    gl.glTranslatef(0.0, 0.0, -l * 0.5)
106    gl.gluCylinder(_quadric, r, r, l, 32, 32)
107    gl.gluSphere(_quadric, r, 32, 32)
108    gl.glPushMatrix()
109    gl.glTranslatef(0.0, 0.0, l)
110    gl.gluSphere(_quadric, r, 32, 32)
111    gl.glPopMatrix()
112    gl.glPopMatrix()
113    gl.gluDeleteQuadric(_quadric)
114
115
116  cdef void _renderBox(self, soy.shapes.Shape _shape) :
117    cdef ode.dVector3 _xyz
118    ode.dGeomBoxGetLengths(_shape._geomID, _xyz)
119    gl.glPushMatrix()
120    gl.glScalef(_xyz[0], _xyz[1], _xyz[2])
121    gl.glBegin(gl.GL_QUADS)
122    gl.glNormal3f( 0.0, 0.0, 1.0)
123    gl.glVertex3f(-0.5, -0.5,  0.5)
124    gl.glVertex3f( 0.5, -0.5,  0.5)
125    gl.glVertex3f( 0.5,  0.5,  0.5)
126    gl.glVertex3f(-0.5,  0.5,  0.5)
127    gl.glNormal3f( 0.0, 0.0,-1.0)
128    gl.glVertex3f(-0.5, -0.5, -0.5)
129    gl.glVertex3f(-0.5,  0.5, -0.5)
130    gl.glVertex3f( 0.5,  0.5, -0.5)
131    gl.glVertex3f( 0.5, -0.5, -0.5)
132    gl.glNormal3f( 0.0, 1.0, 0.0)
133    gl.glVertex3f(-0.5,  0.5, -0.5)
134    gl.glVertex3f(-0.5,  0.5,  0.5)
135    gl.glVertex3f( 0.5,  0.5,  0.5)
136    gl.glVertex3f( 0.5,  0.5, -0.5)
137    gl.glNormal3f( 0.0,-1.0, 0.0)
138    gl.glVertex3f(-0.5, -0.5, -0.5)
139    gl.glVertex3f( 0.5, -0.5, -0.5)
140    gl.glVertex3f( 0.5, -0.5,  0.5)
141    gl.glVertex3f(-0.5, -0.5,  0.5)
142    gl.glNormal3f( 1.0, 0.0, 0.0)
143    gl.glVertex3f( 0.5, -0.5, -0.5)
144    gl.glVertex3f( 0.5,  0.5, -0.5)
145    gl.glVertex3f( 0.5,  0.5,  0.5)
146    gl.glVertex3f( 0.5, -0.5,  0.5)
147    gl.glNormal3f(-1.0, 0.0, 0.0)
148    gl.glVertex3f(-0.5, -0.5, -0.5)
149    gl.glVertex3f(-0.5, -0.5,  0.5)
150    gl.glVertex3f(-0.5,  0.5,  0.5)
151    gl.glVertex3f(-0.5,  0.5, -0.5)
152    gl.glEnd()
153    gl.glPopMatrix()
154
155
156  cdef void _renderRay(self, soy.shapes.Shape _shape) :
157    cdef ode.dVector3 s, d
158    cdef float l
159    ode.dGeomRayGet(_shape._geomID, s, d)
160    l = ode.dGeomRayGetLength(_shape._geomID)
161    gl.glPushAttrib(gl.GL_LIGHTING_BIT)
162    gl.glDisable(gl.GL_LIGHTING)
163    gl.glBegin(gl.GL_LINES)
164    gl.glVertex3f(s[0], s[1], s[2])
165    gl.glVertex3f(s[0] + d[0] * l, s[1] + d[0] * l, s[2] + d[0] * l)
166    gl.glEnd()
167    gl.glPopAttrib()
Note: See TracBrowser for help on using the repository browser.