2 * OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20 * Contains a mesh interface.
21 * \file OPC_MeshInterface.h
22 * \author Pierre Terdiman
23 * \date November, 27, 2002
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29 #ifndef __OPC_MESHINTERFACE_H__
30 #define __OPC_MESHINTERFACE_H__
34 const Point* Vertex[3];
36 bool BackfaceCulling(const Point& source)
38 const Point& p0 = *Vertex[0];
39 const Point& p1 = *Vertex[1];
40 const Point& p2 = *Vertex[2];
42 // Compute normal direction
43 Point Normal = (p2 - p1)^(p0 - p1);
46 return (Normal | (source - p0)) >= 0.0f;
50 #ifdef OPC_USE_CALLBACKS
51 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53 * User-callback, called by OPCODE to request vertices from the app.
54 * \param triangle_index [in] face index for which the system is requesting the vertices
55 * \param triangle [out] triangle's vertices (must be provided by the user)
56 * \param user_data [in] user-defined data from SetCallback()
58 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59 typedef void (*RequestCallback) (udword triangle_index, VertexPointers& triangle, void* user_data);
62 class OPCODE_API MeshInterface
65 // Constructor / Destructor
69 inline_ udword GetNbTriangles() const { return mNbTris; }
70 inline_ udword GetNbVertices() const { return mNbVerts; }
71 inline_ void SetNbTriangles(udword nb) { mNbTris = nb; }
72 inline_ void SetNbVertices(udword nb) { mNbVerts = nb; }
74 #ifdef OPC_USE_CALLBACKS
77 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
79 * Callback control: setups object callback. Must provide triangle-vertices for a given triangle index.
80 * \param callback [in] user-defined callback
81 * \param user_data [in] user-defined data
82 * \return true if success
84 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 bool SetCallback(RequestCallback callback, void* user_data);
86 inline_ void* GetUserData() const { return mUserData; }
87 inline_ RequestCallback GetCallback() const { return mObjCallback; }
91 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93 * Pointers control: setups object pointers. Must provide access to faces and vertices for a given object.
94 * \param tris [in] pointer to triangles
95 * \param verts [in] pointer to vertices
96 * \return true if success
98 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99 bool SetPointers(const IndexedTriangle* tris, const Point* verts);
100 inline_ const IndexedTriangle* GetTris() const { return mTris; }
101 inline_ const Point* GetVerts() const { return mVerts; }
103 #ifdef OPC_USE_STRIDE
106 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
109 * \param tri_stride [in] size of a triangle in bytes. The first sizeof(IndexedTriangle) bytes are used to get vertex indices.
110 * \param vertex_stride [in] size of a vertex in bytes. The first sizeof(Point) bytes are used to get vertex position.
111 * \return true if success
113 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
114 bool SetStrides(udword tri_stride=sizeof(IndexedTriangle), udword vertex_stride=sizeof(Point));
115 inline_ udword GetTriStride() const { return mTriStride; }
116 inline_ udword GetVertexStride() const { return mVertexStride; }
120 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122 * Fetches a triangle given a triangle index.
123 * \param vp [out] required triangle's vertex pointers
124 * \param index [in] triangle index
126 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127 inline_ void GetTriangle(VertexPointers& vp, udword index) const
129 #ifdef OPC_USE_CALLBACKS
130 (mObjCallback)(index, vp, mUserData);
132 #ifdef OPC_USE_STRIDE
133 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + index * mTriStride);
134 vp.Vertex[0] = (const Point*)(((ubyte*)mVerts) + T->mVRef[0] * mVertexStride);
135 vp.Vertex[1] = (const Point*)(((ubyte*)mVerts) + T->mVRef[1] * mVertexStride);
136 vp.Vertex[2] = (const Point*)(((ubyte*)mVerts) + T->mVRef[2] * mVertexStride);
138 const IndexedTriangle* T = &mTris[index];
139 vp.Vertex[0] = &mVerts[T->mVRef[0]];
140 vp.Vertex[1] = &mVerts[T->mVRef[1]];
141 vp.Vertex[2] = &mVerts[T->mVRef[2]];
146 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
148 * Remaps client's mesh according to a permutation.
149 * \param nb_indices [in] number of indices in the permutation (will be checked against number of triangles)
150 * \param permutation [in] list of triangle indices
151 * \return true if success
153 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154 inline_ bool RemapClient(udword nb_indices, const udword* permutation) const;
156 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
158 * Checks the mesh interface is valid, i.e. things have been setup correctly.
159 * \return true if valid
161 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162 bool IsValid() const;
164 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166 * Checks the mesh itself is valid.
167 * Currently we only look for degenerate faces.
168 * \return number of degenerate faces
170 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
171 udword CheckTopology() const;
174 udword mNbTris; //!< Number of triangles in the input model
175 udword mNbVerts; //!< Number of vertices in the input model
176 #ifdef OPC_USE_CALLBACKS
178 void* mUserData; //!< User-defined data sent to callback
179 RequestCallback mObjCallback; //!< Object callback
182 const IndexedTriangle* mTris; //!< Array of indexed triangles
183 const Point* mVerts; //!< Array of vertices
184 #ifdef OPC_USE_STRIDE
185 udword mTriStride; //!< Possible triangle stride in bytes [Opcode 1.3]
186 udword mVertexStride; //!< Possible vertex stride in bytes [Opcode 1.3]
191 #endif //__OPC_MESHINTERFACE_H__