1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
# Soya 3D
# Copyright (C) 2004 Jean-Baptiste LAMY -- jiba@tuxfamily.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# A collider for a Shape
cdef class _TriMesh(GeomObject):
"""Parent class for all TriMesh-derived geoms"""
cdef dTriMeshDataID _data
cdef double *_last_transformation
def __new__(self, parent, space, *a, **kw):
self._data = dGeomTriMeshDataCreate()
self._last_transformation = <double*>malloc(16 * sizeof(double))
def __dealloc__(self):
dGeomTriMeshDataDestroy(self._data)
free(self._last_transformation)
# cdef void begin_round(self):
# cdef dReal *R, *P
#
# R = dGeomGetRotation(self.gid)
# P = dGeomGetPosition(self.gid)
#
# memcpy(self._last_transformation, R, 12 * sizeof(dReal))
# memcpy(&self._last_transformation[12], P, 4 * sizeof(dReal))
#
# dGeomTriMeshDataSet(self._data, TRIMESH_LAST_TRANSFORMATION, <void*>self._last_transformation)
#
# GeomObject.begin_round(self)
cdef class _GeomShape(_TriMesh):
"""Shape collider for Soya.
Shapes can collide with primitives (box, sphere, capped cylinder, ray),
but not with other shapes or with terrains.
The axis-aligned bounding box (AABB) is calculated from the bounding
sphere of the shape.
Shapes *must* be made of only triangles!
"""
cdef readonly _soya._SimpleShape shape
cdef float *_normals
cdef int *_indices
def __new__(self, _soya._World parent, SpaceBase space, _soya._SimpleShape shape=None):
cdef _soya.ShapeFace face
cdef float *normals
cdef int *indices
cdef int i
cdef dSpaceID sid
if shape is None:
shape = parent._shape
self.shape = shape
self._normals = <float*>malloc(shape._nb_faces * sizeof(float) * 3)
self._indices = <int*>malloc(shape._nb_faces * sizeof(int) * 3)
if space is None:
sid = NULL
else:
sid = space.sid
normals = self._normals
indices = self._indices
# Extract the normals from the shape while making sure all the
# faces are triangles
for i from 0 <= i < shape._nb_faces:
face = shape._faces[i]
if not face.option & _soya.FACE_TRIANGLE:
raise ValueError, "All faces in a shape for a GeomShape must be triangles (for now)"
memcpy(normals, shape._values + face.normal, 3 * sizeof(float))
normals = normals + 3
indices[0] = shape._vertex_coords[face.v[0]] / 3
indices[1] = shape._vertex_coords[face.v[1]] / 3
indices[2] = shape._vertex_coords[face.v[2]] / 3
#print indices[0], indices[1], indices[2], shape._nb_coords
indices = indices + 3
#print <long>shape._coords, shape._nb_coords, <long>self._indices, shape._nb_faces * 3, <long>normals
dGeomTriMeshDataBuildSingle1(self._data, shape._coords, 3 * sizeof(float), shape._nb_coords, self._indices, shape._nb_faces * 3, 3 * sizeof(int), NULL) #<void*>normals)
self.gid = dCreateTriMesh(sid, self._data, NULL, NULL, NULL)
def __dealloc__(self):
free(self._normals)
free(self._indices)
def placeable(self):
return True
cdef class _GeomLand(_TriMesh):
"""Land collider for Soya.
"""
cdef readonly _soya._Land land
#cdef float *_normals
cdef int *_indices
def __new__(self, _soya._Land land, SpaceBase space):
cdef float *normals
cdef int *indices
cdef int i, nvertices, ntris, v1, v2, v3, v4
cdef dSpaceID sid
if space is None:
sid = NULL
else:
sid = space.sid
self.land = land
nvertices = land._nb_vertex_width * land._nb_vertex_depth
ntris = ((land._nb_vertex_width - 1) * (land._nb_vertex_depth - 1)) * 2
#self._vertices = <float*>malloc(nvertices * sizeof(float) * 3)
#self._normals = <float*>malloc(ntris * sizeof(float) * 3)
self._indices = <int*>malloc(ntris * sizeof(int) * 3)
indices = self._indices
for j from 0 <= j < land._nb_vertex_depth - 1:
for i from 0 <= i < land._nb_vertex_width - 1:
v1 = i + j * land._nb_vertex_width
v2 = i + 1 + j * land._nb_vertex_width
v3 = i + 1 + (j + 1) * land._nb_vertex_width
v4 = i + (j + 1) * land._nb_vertex_width
# Land uses diamonds
if ((i & 1) and (j & 1)) or ((not (i * 1)) and (not (j & 1))):
# Down and to the right
indices[0] = v4
indices[1] = v3
indices[2] = v1
indices[3] = v2
indices[4] = v1
indices[5] = v3
else:
# Down and to the left
indices[0] = v1
indices[1] = v4
indices[2] = v2
indices[3] = v3
indices[4] = v2
indices[5] = v4
indices = indices + 6
dGeomTriMeshDataBuildSingle1(self._data, land._vertices[0].coord, sizeof(land._vertices[0]), nvertices, self._indices, ntris * 3, 3 * sizeof(int), NULL) #<void*>land._normals)
self.gid = dCreateTriMesh(sid, self._data, NULL, NULL, NULL)
def __dealloc__(self):
free(self._indices)
def __init__(self, _soya._Land land, SpaceBase space=None):
GeomObject.__init__(self, land._parent, space)
def placeable(self):
return True
|