| 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 1361 2008-09-11 05:50:16Z ArcRiley $ |
|---|
| 19 | |
|---|
| 20 | cdef class Axis (Model) : |
|---|
| 21 | '''soy.models.Axis |
|---|
| 22 | |
|---|
| 23 | This L{models class<soy.models>} renders a "XYZ axis" oriented to each |
|---|
| 24 | body it's attached to. This is useful for debugging. |
|---|
| 25 | ''' |
|---|
| 26 | |
|---|
| 27 | ############################################################################ |
|---|
| 28 | # |
|---|
| 29 | # WindowLoop Functions |
|---|
| 30 | # |
|---|
| 31 | |
|---|
| 32 | cdef void _render(self, soy.bodies.Body _body) : |
|---|
| 33 | cdef VertPC _vert[2] # contains vertex position and color data |
|---|
| 34 | cdef Line _elmt[1] # contains indices to verts to render lines |
|---|
| 35 | cdef gl.GLfloat _mtx[16] |
|---|
| 36 | # |
|---|
| 37 | ###################################### |
|---|
| 38 | # |
|---|
| 39 | # get model's matrix |
|---|
| 40 | # |
|---|
| 41 | _mtx[0] = _body._rotation[0] |
|---|
| 42 | _mtx[1] = _body._rotation[4] |
|---|
| 43 | _mtx[2] = _body._rotation[8] |
|---|
| 44 | _mtx[3] = 0.0 |
|---|
| 45 | _mtx[4] = _body._rotation[1] |
|---|
| 46 | _mtx[5] = _body._rotation[5] |
|---|
| 47 | _mtx[6] = _body._rotation[9] |
|---|
| 48 | _mtx[7] = 0.0 |
|---|
| 49 | _mtx[8] = _body._rotation[2] |
|---|
| 50 | _mtx[9] = _body._rotation[6] |
|---|
| 51 | _mtx[10] = _body._rotation[10] |
|---|
| 52 | _mtx[11] = 0.0 |
|---|
| 53 | _mtx[12] = _body._position[0] |
|---|
| 54 | _mtx[13] = _body._position[1] |
|---|
| 55 | _mtx[14] = _body._position[2] |
|---|
| 56 | _mtx[15] = 1.0 |
|---|
| 57 | # |
|---|
| 58 | ###################################### |
|---|
| 59 | # |
|---|
| 60 | # save Camera matix before transforming to the Axis' matrix |
|---|
| 61 | # |
|---|
| 62 | gl.glPushMatrix() |
|---|
| 63 | gl.glMultMatrixf(_mtx) |
|---|
| 64 | # |
|---|
| 65 | ###################################### |
|---|
| 66 | # |
|---|
| 67 | # Test if we've already setup the axis VBO |
|---|
| 68 | # |
|---|
| 69 | if self._vertBuffer : |
|---|
| 70 | # |
|---|
| 71 | # Since we already setup the buffers during the first _render, now we |
|---|
| 72 | # just need to re-bind them to render again |
|---|
| 73 | # |
|---|
| 74 | gl.glBindBufferARB(gl.GL_ARRAY_BUFFER_ARB, self._vertBuffer) |
|---|
| 75 | gl.glBindBufferARB(gl.GL_ELEMENT_ARRAY_BUFFER_ARB, self._elementBuffer) |
|---|
| 76 | # |
|---|
| 77 | else: |
|---|
| 78 | # |
|---|
| 79 | # This is the first render pass, so we need to setup the VBO |
|---|
| 80 | # |
|---|
| 81 | # First we'll populate _vert |
|---|
| 82 | # |
|---|
| 83 | self._vert[0].px = 0.00 # \ |
|---|
| 84 | self._vert[0].py = 0.00 # } (0.0, 0.0, 0.0) |
|---|
| 85 | self._vert[0].pz = 0.00 # / |
|---|
| 86 | self._vert[0].cr = 1.00 # \ |
|---|
| 87 | self._vert[0].cg = 0.00 # } Red |
|---|
| 88 | self._vert[0].cb = 0.00 # /___________________________ |
|---|
| 89 | self._vert[1].px = 1.00 # \ |
|---|
| 90 | self._vert[1].py = 0.00 # } (1.0, 0.0, 0.0) |
|---|
| 91 | self._vert[1].pz = 0.00 # / |
|---|
| 92 | self._vert[1].cr = 1.00 # \ |
|---|
| 93 | self._vert[1].cg = 0.00 # } Red |
|---|
| 94 | self._vert[1].cb = 0.00 # /___________________________ |
|---|
| 95 | # |
|---|
| 96 | # Next populate _elmt |
|---|
| 97 | # |
|---|
| 98 | self._elmt[0].a = 0 # Line 0: 0-1 |
|---|
| 99 | self._elmt[0].b = 1 # ____________________________ |
|---|
| 100 | # |
|---|
| 101 | # Create new vertex buffer and send _vert |
|---|
| 102 | # |
|---|
| 103 | gl.glGenBuffersARB(1, &self._vertBuffer) |
|---|
| 104 | gl.glBindBufferARB(gl.GL_ARRAY_BUFFER_ARB, self._vertBuffer) |
|---|
| 105 | gl.glBufferDataARB(gl.GL_ARRAY_BUFFER_ARB, sizeof(_vert), _vert, |
|---|
| 106 | gl.GL_STATIC_DRAW_ARB) |
|---|
| 107 | # |
|---|
| 108 | # Do the same for the element buffer |
|---|
| 109 | # |
|---|
| 110 | gl.glGenBuffersARB(1, &self._elmtBuffer) |
|---|
| 111 | gl.glBindBufferARB(gl.GL_ARRAY_BUFFER_ARB, self._elmtBuffer) |
|---|
| 112 | gl.glBufferDataARB(gl.GL_ARRAY_BUFFER_ARB, sizeof(_elmt), _elmt, |
|---|
| 113 | gl.GL_STATIC_DRAW_ARB) |
|---|
| 114 | # |
|---|
| 115 | ###################################### |
|---|
| 116 | # |
|---|
| 117 | # Disable unused pointers |
|---|
| 118 | # |
|---|
| 119 | # Unlike Mesh and most other models, Axis doesn't use normals or textures |
|---|
| 120 | # so we need to turn these off while rendering an Axis |
|---|
| 121 | # |
|---|
| 122 | gl.glDisableClientState(gl.GL_NORMAL_ARRAY) # No Normals |
|---|
| 123 | gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) # No Texture Coords |
|---|
| 124 | # |
|---|
| 125 | ###################################### |
|---|
| 126 | # |
|---|
| 127 | # Enable color pointer |
|---|
| 128 | # |
|---|
| 129 | # To get color data into the vertices without making three separate |
|---|
| 130 | # rendering calls we turn on the color array while rendering |
|---|
| 131 | # |
|---|
| 132 | gl.glEnableClientState(gl.GL_COLOR_ARRAY) # We need to render with color arrays |
|---|
| 133 | # |
|---|
| 134 | gl.glVertexPointer(3, gl.GL_FLOAT, 0, <gl.GLvoid *> 0) |
|---|
| 135 | gl.glColorPointer (3, gl.GL_FLOAT, sizeof(float)*3, <gl.GLvoid *> 0) |
|---|
| 136 | gl.glDisable(gl.GL_CULL_FACE) |
|---|
| 137 | gl.glDisable(gl.GL_DEPTH_TEST) |
|---|
| 138 | gl.glDisable(gl.GL_LIGHTING) |
|---|
| 139 | gl.glDrawElements(gl.GL_LINES, 2, gl.GL_UNSIGNED_BYTE, <gl.GLvoid *> 0) |
|---|
| 140 | gl.glEnable(gl.GL_LIGHTING) |
|---|
| 141 | gl.glEnable(gl.GL_DEPTH_TEST) |
|---|
| 142 | gl.glEnable(gl.GL_CULL_FACE) |
|---|
| 143 | gl.glDisableClientState(gl.GL_COLOR_ARRAY) |
|---|
| 144 | gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) |
|---|
| 145 | gl.glEnableClientState(gl.GL_NORMAL_ARRAY) |
|---|
| 146 | # |
|---|
| 147 | ###################################### |
|---|
| 148 | # |
|---|
| 149 | # return to camera matrix |
|---|
| 150 | # |
|---|
| 151 | gl.glPopMatrix() |
|---|
| 152 | # |
|---|
| 153 | ###################################### |
|---|
| 154 | |
|---|