| 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 1278 2008-05-19 22:57:46Z ArcRiley $ |
|---|
| 19 | |
|---|
| 20 | cdef 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 value is None : |
|---|
| 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 | # Render the specific shape |
|---|
| 70 | if _shapeclass == ode.dSphereClass : |
|---|
| 71 | self._renderSphere(_body._shape) |
|---|
| 72 | elif _shapeclass == ode.dCapsuleClass : |
|---|
| 73 | self._renderCapsule(_body._shape) |
|---|
| 74 | elif _shapeclass == ode.dBoxClass : |
|---|
| 75 | self._renderBox(_body._shape) |
|---|
| 76 | elif _shapeclass == ode.dRayClass : |
|---|
| 77 | self._renderRay(_body._shape) |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | cdef void _renderSphere(self, soy.shapes.Shape _shape) : |
|---|
| 81 | cdef int _pass |
|---|
| 82 | cdef gl.GLUquadricObj* _quadric |
|---|
| 83 | _pass = 0 |
|---|
| 84 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 85 | _pass += 1 |
|---|
| 86 | _quadric = gl.gluNewQuadric() |
|---|
| 87 | gl.gluQuadricNormals(_quadric, gl.GLU_SMOOTH) |
|---|
| 88 | gl.gluQuadricTexture(_quadric, gl.GL_FALSE) |
|---|
| 89 | gl.gluSphere(_quadric, _shape._radius(), 32, 32) |
|---|
| 90 | gl.gluDeleteQuadric(_quadric) |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | cdef void _renderCapsule(self, soy.shapes.Shape _shape) : |
|---|
| 94 | cdef int _pass |
|---|
| 95 | cdef ode.dReal r, l |
|---|
| 96 | cdef gl.GLUquadricObj* _quadric |
|---|
| 97 | _pass = 0 |
|---|
| 98 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 99 | _pass += 1 |
|---|
| 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 int _pass |
|---|
| 118 | cdef ode.dVector3 _xyz |
|---|
| 119 | _pass = 0 |
|---|
| 120 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 121 | _pass += 1 |
|---|
| 122 | ode.dGeomBoxGetLengths(_shape._geomID, _xyz) |
|---|
| 123 | gl.glPushMatrix() |
|---|
| 124 | gl.glScalef(_xyz[0], _xyz[1], _xyz[2]) |
|---|
| 125 | gl.glBegin(gl.GL_QUADS) |
|---|
| 126 | gl.glNormal3f( 0.0, 0.0, 1.0) |
|---|
| 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.glVertex3f(-0.5, 0.5, 0.5) |
|---|
| 131 | gl.glNormal3f( 0.0, 0.0,-1.0) |
|---|
| 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.glVertex3f( 0.5, -0.5, -0.5) |
|---|
| 136 | gl.glNormal3f( 0.0, 1.0, 0.0) |
|---|
| 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.glVertex3f( 0.5, 0.5, -0.5) |
|---|
| 141 | gl.glNormal3f( 0.0,-1.0, 0.0) |
|---|
| 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.glVertex3f(-0.5, -0.5, 0.5) |
|---|
| 146 | gl.glNormal3f( 1.0, 0.0, 0.0) |
|---|
| 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.glVertex3f( 0.5, -0.5, 0.5) |
|---|
| 151 | gl.glNormal3f(-1.0, 0.0, 0.0) |
|---|
| 152 | gl.glVertex3f(-0.5, -0.5, -0.5) |
|---|
| 153 | gl.glVertex3f(-0.5, -0.5, 0.5) |
|---|
| 154 | gl.glVertex3f(-0.5, 0.5, 0.5) |
|---|
| 155 | gl.glVertex3f(-0.5, 0.5, -0.5) |
|---|
| 156 | gl.glEnd() |
|---|
| 157 | gl.glPopMatrix() |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | cdef void _renderRay(self, soy.shapes.Shape _shape) : |
|---|
| 161 | cdef int _pass |
|---|
| 162 | cdef float _l |
|---|
| 163 | cdef ode.dVector3 _s, _d |
|---|
| 164 | _pass = 0 |
|---|
| 165 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 166 | _pass += 1 |
|---|
| 167 | ode.dGeomRayGet(_shape._geomID, _s, _d) |
|---|
| 168 | _l = ode.dGeomRayGetLength(_shape._geomID) |
|---|
| 169 | gl.glPushAttrib(gl.GL_LIGHTING_BIT) |
|---|
| 170 | gl.glDisable(gl.GL_LIGHTING) |
|---|
| 171 | gl.glBegin(gl.GL_LINES) |
|---|
| 172 | gl.glVertex3f(_s[0], _s[1], _s[2]) |
|---|
| 173 | gl.glVertex3f(_s[0] + _d[0] * _l, _s[1] + _d[0] * _l, _s[2] + _d[0] * _l) |
|---|
| 174 | gl.glEnd() |
|---|
| 175 | gl.glPopAttrib() |
|---|