| 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 1375 2008-11-11 05:45:21Z DavidCzech $ |
|---|
| 19 | |
|---|
| 20 | cdef class Shape (Model) : |
|---|
| 21 | '''soy.models.Shape |
|---|
| 22 | |
|---|
| 23 | This :class:`~soy.models.Model` 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 | cdef gl.GLfloat _mtx[16] |
|---|
| 62 | # |
|---|
| 63 | ###################################### |
|---|
| 64 | # |
|---|
| 65 | # Don't attempt to render a non-existant shape |
|---|
| 66 | # |
|---|
| 67 | if _body._shape is None : |
|---|
| 68 | return |
|---|
| 69 | # |
|---|
| 70 | ###################################### |
|---|
| 71 | # |
|---|
| 72 | # save current matrix |
|---|
| 73 | # |
|---|
| 74 | gl.glPushMatrix() |
|---|
| 75 | # |
|---|
| 76 | ###################################### |
|---|
| 77 | # |
|---|
| 78 | # set model's matrix |
|---|
| 79 | # |
|---|
| 80 | _mtx[0] = _body._rotation[0] |
|---|
| 81 | _mtx[1] = _body._rotation[4] |
|---|
| 82 | _mtx[2] = _body._rotation[8] |
|---|
| 83 | _mtx[3] = 0.0 |
|---|
| 84 | _mtx[4] = _body._rotation[1] |
|---|
| 85 | _mtx[5] = _body._rotation[5] |
|---|
| 86 | _mtx[6] = _body._rotation[9] |
|---|
| 87 | _mtx[7] = 0.0 |
|---|
| 88 | _mtx[8] = _body._rotation[2] |
|---|
| 89 | _mtx[9] = _body._rotation[6] |
|---|
| 90 | _mtx[10] = _body._rotation[10] |
|---|
| 91 | _mtx[11] = 0.0 |
|---|
| 92 | _mtx[12] = _body._position[0] |
|---|
| 93 | _mtx[13] = _body._position[1] |
|---|
| 94 | _mtx[14] = _body._position[2] |
|---|
| 95 | _mtx[15] = 1.0 |
|---|
| 96 | gl.glMultMatrixf(_mtx) |
|---|
| 97 | # |
|---|
| 98 | ###################################### |
|---|
| 99 | # |
|---|
| 100 | # Grab the shape's class |
|---|
| 101 | _shapeclass = ode.dGeomGetClass(_body._shape._geomID) |
|---|
| 102 | # |
|---|
| 103 | ###################################### |
|---|
| 104 | # |
|---|
| 105 | # render the specific shape |
|---|
| 106 | # |
|---|
| 107 | if _shapeclass == ode.dSphereClass : |
|---|
| 108 | self._renderSphere(_body._shape) |
|---|
| 109 | elif _shapeclass == ode.dCapsuleClass : |
|---|
| 110 | self._renderCapsule(_body._shape) |
|---|
| 111 | elif _shapeclass == ode.dBoxClass : |
|---|
| 112 | self._renderBox(_body._shape) |
|---|
| 113 | elif _shapeclass == ode.dRayClass : |
|---|
| 114 | self._renderRay(_body._shape) |
|---|
| 115 | # |
|---|
| 116 | ###################################### |
|---|
| 117 | # |
|---|
| 118 | # restore previous matrix |
|---|
| 119 | # |
|---|
| 120 | gl.glPopMatrix() |
|---|
| 121 | # |
|---|
| 122 | ###################################### |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | cdef void _renderSphere(self, soy.shapes.Shape _shape) : |
|---|
| 126 | cdef int _pass |
|---|
| 127 | cdef gl.GLUquadricObj* _quadric |
|---|
| 128 | _pass = 0 |
|---|
| 129 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 130 | _pass += 1 |
|---|
| 131 | _quadric = gl.gluNewQuadric() |
|---|
| 132 | gl.gluQuadricNormals(_quadric, gl.GLU_SMOOTH) |
|---|
| 133 | gl.gluQuadricTexture(_quadric, gl.GL_FALSE) |
|---|
| 134 | gl.gluSphere(_quadric, _shape._radius(), 32, 32) |
|---|
| 135 | gl.gluDeleteQuadric(_quadric) |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | cdef void _renderCapsule(self, soy.shapes.Shape _shape) : |
|---|
| 139 | cdef int _pass |
|---|
| 140 | cdef ode.dReal r, l |
|---|
| 141 | cdef gl.GLUquadricObj* _quadric |
|---|
| 142 | _pass = 0 |
|---|
| 143 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 144 | _pass += 1 |
|---|
| 145 | ode.dGeomCapsuleGetParams(_shape._geomID, &r, &l) |
|---|
| 146 | _quadric = gl.gluNewQuadric() |
|---|
| 147 | gl.gluQuadricNormals(_quadric, gl.GLU_SMOOTH) |
|---|
| 148 | gl.gluQuadricTexture(_quadric, gl.GL_FALSE) |
|---|
| 149 | gl.glPushMatrix() |
|---|
| 150 | gl.glTranslatef(0.0, 0.0, -l * 0.5) |
|---|
| 151 | gl.gluCylinder(_quadric, r, r, l, 32, 32) |
|---|
| 152 | gl.gluSphere(_quadric, r, 32, 32) |
|---|
| 153 | gl.glPushMatrix() |
|---|
| 154 | gl.glTranslatef(0.0, 0.0, l) |
|---|
| 155 | gl.gluSphere(_quadric, r, 32, 32) |
|---|
| 156 | gl.glPopMatrix() |
|---|
| 157 | gl.glPopMatrix() |
|---|
| 158 | gl.gluDeleteQuadric(_quadric) |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | cdef void _renderBox(self, soy.shapes.Shape _shape) : |
|---|
| 162 | cdef int _pass |
|---|
| 163 | cdef ode.dVector3 _xyz |
|---|
| 164 | _pass = 0 |
|---|
| 165 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 166 | _pass += 1 |
|---|
| 167 | ode.dGeomBoxGetLengths(_shape._geomID, _xyz) |
|---|
| 168 | gl.glPushMatrix() |
|---|
| 169 | gl.glScalef(_xyz[0], _xyz[1], _xyz[2]) |
|---|
| 170 | gl.glBegin(gl.GL_QUADS) |
|---|
| 171 | gl.glNormal3f( 0.0, 0.0, 1.0) |
|---|
| 172 | gl.glVertex3f(-0.5, -0.5, 0.5) |
|---|
| 173 | gl.glVertex3f( 0.5, -0.5, 0.5) |
|---|
| 174 | gl.glVertex3f( 0.5, 0.5, 0.5) |
|---|
| 175 | gl.glVertex3f(-0.5, 0.5, 0.5) |
|---|
| 176 | gl.glNormal3f( 0.0, 0.0,-1.0) |
|---|
| 177 | gl.glVertex3f(-0.5, -0.5, -0.5) |
|---|
| 178 | gl.glVertex3f(-0.5, 0.5, -0.5) |
|---|
| 179 | gl.glVertex3f( 0.5, 0.5, -0.5) |
|---|
| 180 | gl.glVertex3f( 0.5, -0.5, -0.5) |
|---|
| 181 | gl.glNormal3f( 0.0, 1.0, 0.0) |
|---|
| 182 | gl.glVertex3f(-0.5, 0.5, -0.5) |
|---|
| 183 | gl.glVertex3f(-0.5, 0.5, 0.5) |
|---|
| 184 | gl.glVertex3f( 0.5, 0.5, 0.5) |
|---|
| 185 | gl.glVertex3f( 0.5, 0.5, -0.5) |
|---|
| 186 | gl.glNormal3f( 0.0,-1.0, 0.0) |
|---|
| 187 | gl.glVertex3f(-0.5, -0.5, -0.5) |
|---|
| 188 | gl.glVertex3f( 0.5, -0.5, -0.5) |
|---|
| 189 | gl.glVertex3f( 0.5, -0.5, 0.5) |
|---|
| 190 | gl.glVertex3f(-0.5, -0.5, 0.5) |
|---|
| 191 | gl.glNormal3f( 1.0, 0.0, 0.0) |
|---|
| 192 | gl.glVertex3f( 0.5, -0.5, -0.5) |
|---|
| 193 | gl.glVertex3f( 0.5, 0.5, -0.5) |
|---|
| 194 | gl.glVertex3f( 0.5, 0.5, 0.5) |
|---|
| 195 | gl.glVertex3f( 0.5, -0.5, 0.5) |
|---|
| 196 | gl.glNormal3f(-1.0, 0.0, 0.0) |
|---|
| 197 | gl.glVertex3f(-0.5, -0.5, -0.5) |
|---|
| 198 | gl.glVertex3f(-0.5, -0.5, 0.5) |
|---|
| 199 | gl.glVertex3f(-0.5, 0.5, 0.5) |
|---|
| 200 | gl.glVertex3f(-0.5, 0.5, -0.5) |
|---|
| 201 | gl.glEnd() |
|---|
| 202 | gl.glPopMatrix() |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | cdef void _renderRay(self, soy.shapes.Shape _shape) : |
|---|
| 206 | cdef int _pass |
|---|
| 207 | cdef float _l |
|---|
| 208 | cdef ode.dVector3 _s, _d |
|---|
| 209 | _pass = 0 |
|---|
| 210 | while self._material._render(_pass, <float*> 24, <float*> 24) : |
|---|
| 211 | _pass += 1 |
|---|
| 212 | ode.dGeomRayGet(_shape._geomID, _s, _d) |
|---|
| 213 | _l = ode.dGeomRayGetLength(_shape._geomID) |
|---|
| 214 | gl.glPushAttrib(gl.GL_LIGHTING_BIT) |
|---|
| 215 | gl.glDisable(gl.GL_LIGHTING) |
|---|
| 216 | gl.glBegin(gl.GL_LINES) |
|---|
| 217 | gl.glVertex3f(_s[0], _s[1], _s[2]) |
|---|
| 218 | gl.glVertex3f(_s[0] + _d[0] * _l, _s[1] + _d[0] * _l, _s[2] + _d[0] * _l) |
|---|
| 219 | gl.glEnd() |
|---|
| 220 | gl.glPopAttrib() |
|---|