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