| 1 | # PySoy's models.Axis 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: Axis.pym 1384 2008-12-01 06:26:35Z ArcRiley $ |
|---|
| 19 | |
|---|
| 20 | cdef class Axis (Model) : |
|---|
| 21 | '''soy.models.Axis |
|---|
| 22 | |
|---|
| 23 | This :class:`~soy.model.Model` renders a "XYZ axis" oriented to each |
|---|
| 24 | body it's attached to. This is useful for debugging. |
|---|
| 25 | ''' |
|---|
| 26 | def __cinit__(self, size=1.0, *args, **kw): |
|---|
| 27 | self._size = size |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | ############################################################################ |
|---|
| 31 | # |
|---|
| 32 | # Properties |
|---|
| 33 | # |
|---|
| 34 | |
|---|
| 35 | property size: |
|---|
| 36 | def __get__(self): |
|---|
| 37 | return self._size |
|---|
| 38 | |
|---|
| 39 | def __set__(self, value): |
|---|
| 40 | if value == 0.0 : |
|---|
| 41 | raise ValueError('Cannot set size to 0.0') |
|---|
| 42 | self._size = value |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | ############################################################################ |
|---|
| 46 | # |
|---|
| 47 | # WindowLoop Functions |
|---|
| 48 | # |
|---|
| 49 | |
|---|
| 50 | cdef void _render(self, soy.bodies.Body _body) : |
|---|
| 51 | cdef VertPC _vert[20] # contains vertex position and color data |
|---|
| 52 | cdef Line _elmt[20] # contains indices to verts to render lines |
|---|
| 53 | cdef gl.GLfloat _mtx[16] |
|---|
| 54 | # |
|---|
| 55 | ###################################### |
|---|
| 56 | # |
|---|
| 57 | # get model's matrix |
|---|
| 58 | # |
|---|
| 59 | _mtx[0] = _body._rotation[0] |
|---|
| 60 | _mtx[1] = _body._rotation[4] |
|---|
| 61 | _mtx[2] = _body._rotation[8] |
|---|
| 62 | _mtx[3] = 0.0 |
|---|
| 63 | _mtx[4] = _body._rotation[1] |
|---|
| 64 | _mtx[5] = _body._rotation[5] |
|---|
| 65 | _mtx[6] = _body._rotation[9] |
|---|
| 66 | _mtx[7] = 0.0 |
|---|
| 67 | _mtx[8] = _body._rotation[2] |
|---|
| 68 | _mtx[9] = _body._rotation[6] |
|---|
| 69 | _mtx[10] = _body._rotation[10] |
|---|
| 70 | _mtx[11] = 0.0 |
|---|
| 71 | _mtx[12] = _body._position[0] |
|---|
| 72 | _mtx[13] = _body._position[1] |
|---|
| 73 | _mtx[14] = _body._position[2] |
|---|
| 74 | _mtx[15] = 1 / self._size |
|---|
| 75 | # |
|---|
| 76 | ###################################### |
|---|
| 77 | # |
|---|
| 78 | # save Camera matix before transforming to the Axis' matrix |
|---|
| 79 | # |
|---|
| 80 | gl.glPushMatrix() |
|---|
| 81 | gl.glMultMatrixf(_mtx) |
|---|
| 82 | #gl.glScalef(self._size,self._size,self._size) |
|---|
| 83 | # |
|---|
| 84 | ###################################### |
|---|
| 85 | # |
|---|
| 86 | # Test if we've already setup the axis VBO |
|---|
| 87 | # |
|---|
| 88 | if self._vertBuffer : |
|---|
| 89 | # |
|---|
| 90 | # Since we already setup the buffers during the first _render, now we |
|---|
| 91 | # just need to re-bind them to render again |
|---|
| 92 | # |
|---|
| 93 | gl.glBindBufferARB(gl.GL_ARRAY_BUFFER_ARB, self._vertBuffer) |
|---|
| 94 | gl.glBindBufferARB(gl.GL_ELEMENT_ARRAY_BUFFER_ARB, self._elmtBuffer) |
|---|
| 95 | # |
|---|
| 96 | else: |
|---|
| 97 | # |
|---|
| 98 | # This is the first render pass, so we need to setup the VBO |
|---|
| 99 | # |
|---|
| 100 | # First we'll populate _vert |
|---|
| 101 | # |
|---|
| 102 | # X Axis. |
|---|
| 103 | _vert[0].px = 0.00 # \ Vertex 0 |
|---|
| 104 | _vert[0].py = 0.00 # } (0.0, 0.0, 0.0) |
|---|
| 105 | _vert[0].pz = 0.00 # / |
|---|
| 106 | _vert[0].cr = 1.00 # \ |
|---|
| 107 | _vert[0].cg = 0.00 # } Red |
|---|
| 108 | _vert[0].cb = 0.00 # /___________________________ |
|---|
| 109 | _vert[1].px = 1.00 # \ Vertex 1 |
|---|
| 110 | _vert[1].py = 0.00 # } (1.0, 0.0, 0.0) |
|---|
| 111 | _vert[1].pz = 0.00 # / |
|---|
| 112 | _vert[1].cr = 1.00 # \ |
|---|
| 113 | _vert[1].cg = 0.00 # } Red |
|---|
| 114 | _vert[1].cb = 0.00 # /___________________________ |
|---|
| 115 | |
|---|
| 116 | _vert[2].px = 1.00 # \ Vertex 2 |
|---|
| 117 | _vert[2].py = 0.30 # } (1.0, 0.3, 0.0) |
|---|
| 118 | _vert[2].pz = 0.00 # / |
|---|
| 119 | _vert[2].cr = 1.00 # \ |
|---|
| 120 | _vert[2].cg = 0.00 # } Red |
|---|
| 121 | _vert[2].cb = 0.00 # /___________________________ |
|---|
| 122 | _vert[3].px = 1.30 # \ Vertex 3 |
|---|
| 123 | _vert[3].py = 0.00 # } (1.3, 0.0, 0.0) |
|---|
| 124 | _vert[3].pz = 0.00 # / |
|---|
| 125 | _vert[3].cr = 1.00 # \ |
|---|
| 126 | _vert[3].cg = 0.00 # } Red |
|---|
| 127 | _vert[3].cb = 0.00 # /___________________________ |
|---|
| 128 | |
|---|
| 129 | _vert[4].px = 1.00 # \ Vertex 4 |
|---|
| 130 | _vert[4].py = -.30 # } (1.0, -0.3, 0.0) |
|---|
| 131 | _vert[4].pz = 0.00 # / |
|---|
| 132 | _vert[4].cr = 1.00 # \ |
|---|
| 133 | _vert[4].cg = 0.00 # } Red |
|---|
| 134 | _vert[4].cb = 0.00 # /___________________________ |
|---|
| 135 | |
|---|
| 136 | # Y Axis |
|---|
| 137 | _vert[5]= _vert[0] # Vertex 5 is the same as vertex 0 |
|---|
| 138 | _vert[5].cr = 0.0 # Except its not Red. |
|---|
| 139 | _vert[5].cg = 1.0 # Its green. |
|---|
| 140 | |
|---|
| 141 | _vert[6].px = 0.00 # \ Vertex 6 |
|---|
| 142 | _vert[6].py = 1.00 # } (0.0, 1.0, 0.0) |
|---|
| 143 | _vert[6].pz = 0.00 # / |
|---|
| 144 | _vert[6].cr = 0.00 # \ |
|---|
| 145 | _vert[6].cg = 1.00 # } Red |
|---|
| 146 | _vert[6].cb = 0.00 # /___________________________ |
|---|
| 147 | _vert[7].px = 0.30 # \ Vertex 7 |
|---|
| 148 | _vert[7].py = 1.00 # } (0.3, 1.0, 0.0) |
|---|
| 149 | _vert[7].pz = 0.00 # / |
|---|
| 150 | _vert[7].cr = 0.00 # \ |
|---|
| 151 | _vert[7].cg = 1.00 # } Red |
|---|
| 152 | _vert[7].cb = 0.00 # /___________________________ |
|---|
| 153 | |
|---|
| 154 | _vert[8].px = 0.00 # \ Vertex 8 |
|---|
| 155 | _vert[8].py = 1.30 # } (0.0, 1.3, 0.0) |
|---|
| 156 | _vert[8].pz = 0.00 # / |
|---|
| 157 | _vert[8].cr = 0.00 # \ |
|---|
| 158 | _vert[8].cg = 1.00 # } Red |
|---|
| 159 | _vert[8].cb = 0.00 # /___________________________ |
|---|
| 160 | _vert[9].px = -.30 # \ Vertex 9 |
|---|
| 161 | _vert[9].py = 1.00 # } (-0.3, 1.0, 0.0) |
|---|
| 162 | _vert[9].pz = 0.00 # / |
|---|
| 163 | _vert[9].cr = 0.00 # \ |
|---|
| 164 | _vert[9].cg = 1.00 # } Red |
|---|
| 165 | _vert[9].cb = 0.00 # /___________________________ |
|---|
| 166 | |
|---|
| 167 | # Z Axis |
|---|
| 168 | _vert[10] = _vert[0] |
|---|
| 169 | _vert[10].cr = 0.00 # \ |
|---|
| 170 | _vert[10].cb = 1.00 # /___________________________ |
|---|
| 171 | |
|---|
| 172 | _vert[11].px = 0.00 # \ Vertex 11 |
|---|
| 173 | _vert[11].py = 0.00 # } (0.0, 1.3, 0.0) |
|---|
| 174 | _vert[11].pz = 1.00 # / |
|---|
| 175 | _vert[11].cr = 0.00 # \ |
|---|
| 176 | _vert[11].cg = 0.00 # } Red |
|---|
| 177 | _vert[11].cb = 1.00 # /___________________________ |
|---|
| 178 | |
|---|
| 179 | _vert[12].px = 0.30 # \ Vertex 12 |
|---|
| 180 | _vert[12].py = 0.00 # } (-0.3, 1.0, 0.0) |
|---|
| 181 | _vert[12].pz = 1.00 # / |
|---|
| 182 | _vert[12].cr = 0.00 # \ |
|---|
| 183 | _vert[12].cg = 0.00 # } Red |
|---|
| 184 | _vert[12].cb = 1.00 # /___________________________ |
|---|
| 185 | |
|---|
| 186 | _vert[13].px = 0.00 # \ Vertex 13 |
|---|
| 187 | _vert[13].py = 0.00 # } (0.0, 1.3, 0.0) |
|---|
| 188 | _vert[13].pz = 1.30 # / |
|---|
| 189 | _vert[13].cr = 0.00 # \ |
|---|
| 190 | _vert[13].cg = 0.00 # } Red |
|---|
| 191 | _vert[13].cb = 1.00 # /___________________________ |
|---|
| 192 | _vert[14].px = -.30 # \ Vertex 14 |
|---|
| 193 | _vert[14].py = 0.00 # } (-0.3, 1.0, 0.0) |
|---|
| 194 | _vert[14].pz = 1.00 # / |
|---|
| 195 | _vert[14].cr = 0.00 # \ |
|---|
| 196 | _vert[14].cg = 0.00 # } Red |
|---|
| 197 | _vert[14].cb = 1.00 # /___________________________ |
|---|
| 198 | |
|---|
| 199 | # TODO optional tags. I left them out because they're not all that useful |
|---|
| 200 | # and take up another 15+ verticies.. maybe when we can define verticies more easily. |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | # |
|---|
| 204 | # Next populate _elmt |
|---|
| 205 | # |
|---|
| 206 | _elmt[0].a = 0 # Line 0: 0-1 |
|---|
| 207 | _elmt[0].b = 1 # ____________________________ |
|---|
| 208 | |
|---|
| 209 | _elmt[1].a = 2 # Line 1: 2-4 |
|---|
| 210 | _elmt[1].b = 4 # ____________________________ |
|---|
| 211 | |
|---|
| 212 | _elmt[2].a = 2 # Line 2: 2-3 |
|---|
| 213 | _elmt[2].b = 3 # ____________________________ |
|---|
| 214 | |
|---|
| 215 | _elmt[3].a = 3 # Line 3: 3-4 |
|---|
| 216 | _elmt[3].b = 4 # ____________________________ |
|---|
| 217 | |
|---|
| 218 | _elmt[4].a = 5 # Line 4: 5-6 |
|---|
| 219 | _elmt[4].b = 6 # ____________________________ |
|---|
| 220 | |
|---|
| 221 | _elmt[5].a = 7 # Line 5: 7-9 |
|---|
| 222 | _elmt[5].b = 9 # ____________________________ |
|---|
| 223 | |
|---|
| 224 | _elmt[6].a = 8 # Line 6: 8-9 |
|---|
| 225 | _elmt[6].b = 9 # ____________________________ |
|---|
| 226 | |
|---|
| 227 | _elmt[7].a = 8 # Line 7: 8-7 |
|---|
| 228 | _elmt[7].b = 7 # ____________________________ |
|---|
| 229 | |
|---|
| 230 | _elmt[8].a = 10 # Line 8: 10-11 |
|---|
| 231 | _elmt[8].b = 11 # ____________________________ |
|---|
| 232 | |
|---|
| 233 | _elmt[9].a = 14 # Line 9: 14-12 |
|---|
| 234 | _elmt[9].b = 12 # ____________________________ |
|---|
| 235 | |
|---|
| 236 | _elmt[10].a = 13 # Line 10: 13-12 |
|---|
| 237 | _elmt[10].b = 12 # ____________________________ |
|---|
| 238 | |
|---|
| 239 | _elmt[11].a = 13 # Line 11: 13-14 |
|---|
| 240 | _elmt[11].b = 14 # ____________________________ |
|---|
| 241 | |
|---|
| 242 | # |
|---|
| 243 | # Create new vertex buffer and send _vert |
|---|
| 244 | # |
|---|
| 245 | gl.glGenBuffersARB(1, &self._vertBuffer) |
|---|
| 246 | gl.glBindBufferARB(gl.GL_ARRAY_BUFFER_ARB, self._vertBuffer) |
|---|
| 247 | gl.glBufferDataARB(gl.GL_ARRAY_BUFFER_ARB, sizeof(_vert), _vert, |
|---|
| 248 | gl.GL_STATIC_DRAW_ARB) |
|---|
| 249 | # |
|---|
| 250 | # Do the same for the element buffer |
|---|
| 251 | # |
|---|
| 252 | gl.glGenBuffersARB(1, &self._elmtBuffer) |
|---|
| 253 | gl.glBindBufferARB(gl.GL_ELEMENT_ARRAY_BUFFER_ARB, self._elmtBuffer) |
|---|
| 254 | gl.glBufferDataARB(gl.GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(_elmt), _elmt, |
|---|
| 255 | gl.GL_STATIC_DRAW_ARB) |
|---|
| 256 | |
|---|
| 257 | # |
|---|
| 258 | ###################################### |
|---|
| 259 | # |
|---|
| 260 | # Disable unused pointers |
|---|
| 261 | # |
|---|
| 262 | # Unlike Mesh and most other models, Axis doesn't use normals or textures |
|---|
| 263 | # so we need to turn these off while rendering an Axis |
|---|
| 264 | # |
|---|
| 265 | gl.glDisableClientState(gl.GL_NORMAL_ARRAY) # No Normals |
|---|
| 266 | gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) # No Texture Coords |
|---|
| 267 | # |
|---|
| 268 | ###################################### |
|---|
| 269 | # |
|---|
| 270 | # Enable color pointer |
|---|
| 271 | # |
|---|
| 272 | # To get color data into the vertices without making three separate |
|---|
| 273 | # rendering calls we turn on the color array while rendering |
|---|
| 274 | # |
|---|
| 275 | gl.glEnableClientState(gl.GL_COLOR_ARRAY) # We need to render with color arrays |
|---|
| 276 | # |
|---|
| 277 | gl.glVertexPointer(3, gl.GL_FLOAT, sizeof(VertPC), <gl.GLvoid *> 0) |
|---|
| 278 | gl.glColorPointer (3, gl.GL_FLOAT, sizeof(VertPC), <gl.GLvoid *> 0+12) |
|---|
| 279 | gl.glDisable(gl.GL_CULL_FACE) |
|---|
| 280 | gl.glDisable(gl.GL_DEPTH_TEST) |
|---|
| 281 | gl.glDisable(gl.GL_LIGHTING) |
|---|
| 282 | gl.glDrawElements(gl.GL_LINES, 24, gl.GL_UNSIGNED_BYTE, <gl.GLvoid *> 0) |
|---|
| 283 | gl.glEnable(gl.GL_LIGHTING) |
|---|
| 284 | gl.glEnable(gl.GL_DEPTH_TEST) |
|---|
| 285 | gl.glEnable(gl.GL_CULL_FACE) |
|---|
| 286 | gl.glDisableClientState(gl.GL_COLOR_ARRAY) |
|---|
| 287 | gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) |
|---|
| 288 | gl.glEnableClientState(gl.GL_NORMAL_ARRAY) |
|---|
| 289 | # |
|---|
| 290 | ###################################### |
|---|
| 291 | # |
|---|
| 292 | # return to camera matrix |
|---|
| 293 | # |
|---|
| 294 | gl.glPopMatrix() |
|---|
| 295 | # |
|---|
| 296 | ###################################### |
|---|
| 297 | |
|---|