Planeshift

DebugDraw.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
00003 //
00004 // This software is provided 'as-is', without any express or implied
00005 // warranty.  In no event will the authors be held liable for any damages
00006 // arising from the use of this software.
00007 // Permission is granted to anyone to use this software for any purpose,
00008 // including commercial applications, and to alter it and redistribute it
00009 // freely, subject to the following restrictions:
00010 // 1. The origin of this software must not be misrepresented; you must not
00011 //    claim that you wrote the original software. If you use this software
00012 //    in a product, an acknowledgment in the product documentation would be
00013 //    appreciated but is not required.
00014 // 2. Altered source versions must be plainly marked as such, and must not be
00015 //    misrepresented as being the original software.
00016 // 3. This notice may not be removed or altered from any source distribution.
00017 //
00018 
00019 #ifndef DEBUGDRAW_H
00020 #define DEBUGDRAW_H
00021 
00022 // Some math headers don't have PI defined.
00023 static const float DU_PI = 3.14159265f;
00024 
00025 enum duDebugDrawPrimitives
00026 {
00027         DU_DRAW_POINTS,
00028         DU_DRAW_LINES,
00029         DU_DRAW_TRIS,
00030         DU_DRAW_QUADS,  
00031 };
00032 
00034 struct duDebugDraw
00035 {
00036         virtual ~duDebugDraw() = 0;
00037         
00038         virtual void depthMask(bool state) = 0;
00039 
00040         virtual void texture(bool state) = 0;
00041 
00045         virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f) = 0;
00046 
00050         virtual void vertex(const float* pos, unsigned int color) = 0;
00051 
00055         virtual void vertex(const float x, const float y, const float z, unsigned int color) = 0;
00056 
00060         virtual void vertex(const float* pos, unsigned int color, const float* uv) = 0;
00061         
00065         virtual void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) = 0;
00066         
00068         virtual void end() = 0;
00069 };
00070 
00071 inline unsigned int duRGBA(int r, int g, int b, int a)
00072 {
00073         return ((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16) | ((unsigned int)a << 24);
00074 }
00075 
00076 inline unsigned int duRGBAf(float fr, float fg, float fb, float fa)
00077 {
00078         unsigned char r = (unsigned char)(fr*255.0f);
00079         unsigned char g = (unsigned char)(fg*255.0f);
00080         unsigned char b = (unsigned char)(fb*255.0f);
00081         unsigned char a = (unsigned char)(fa*255.0f);
00082         return duRGBA(r,g,b,a);
00083 }
00084 
00085 unsigned int duIntToCol(int i, int a);
00086 void duIntToCol(int i, float* col);
00087 
00088 inline unsigned int duMultCol(const unsigned int col, const unsigned int d)
00089 {
00090         const unsigned int r = col & 0xff;
00091         const unsigned int g = (col >> 8) & 0xff;
00092         const unsigned int b = (col >> 16) & 0xff;
00093         const unsigned int a = (col >> 24) & 0xff;
00094         return duRGBA((r*d) >> 8, (g*d) >> 8, (b*d) >> 8, a);
00095 }
00096 
00097 inline unsigned int duDarkenCol(unsigned int col)
00098 {
00099         return ((col >> 1) & 0x007f7f7f) | (col & 0xff000000);
00100 }
00101 
00102 inline unsigned int duLerpCol(unsigned int ca, unsigned int cb, unsigned int u)
00103 {
00104         const unsigned int ra = ca & 0xff;
00105         const unsigned int ga = (ca >> 8) & 0xff;
00106         const unsigned int ba = (ca >> 16) & 0xff;
00107         const unsigned int aa = (ca >> 24) & 0xff;
00108         const unsigned int rb = cb & 0xff;
00109         const unsigned int gb = (cb >> 8) & 0xff;
00110         const unsigned int bb = (cb >> 16) & 0xff;
00111         const unsigned int ab = (cb >> 24) & 0xff;
00112         
00113         unsigned int r = (ra*(255-u) + rb*u)/255;
00114         unsigned int g = (ga*(255-u) + gb*u)/255;
00115         unsigned int b = (ba*(255-u) + bb*u)/255;
00116         unsigned int a = (aa*(255-u) + ab*u)/255;
00117         return duRGBA(r,g,b,a);
00118 }
00119 
00120 inline unsigned int duTransCol(unsigned int c, unsigned int a)
00121 {
00122         return (a<<24) | (c & 0x00ffffff);
00123 }
00124 
00125 
00126 void duCalcBoxColors(unsigned int* colors, unsigned int colTop, unsigned int colSide);
00127 
00128 void duDebugDrawCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
00129                                                          float maxx, float maxy, float maxz, unsigned int col, const float lineWidth);
00130 
00131 void duDebugDrawBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
00132                                                 float maxx, float maxy, float maxz, unsigned int col, const float lineWidth);
00133 
00134 void duDebugDrawArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
00135                                         const float x1, const float y1, const float z1, const float h,
00136                                         const float as0, const float as1, unsigned int col, const float lineWidth);
00137 
00138 void duDebugDrawArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
00139                                           const float x1, const float y1, const float z1,
00140                                           const float as0, const float as1, unsigned int col, const float lineWidth);
00141 
00142 void duDebugDrawCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
00143                                            const float r, unsigned int col, const float lineWidth);
00144 
00145 void duDebugDrawCross(struct duDebugDraw* dd, const float x, const float y, const float z,
00146                                           const float size, unsigned int col, const float lineWidth);
00147 
00148 void duDebugDrawBox(struct duDebugDraw* dd, float minx, float miny, float minz,
00149                                         float maxx, float maxy, float maxz, const unsigned int* fcol);
00150 
00151 void duDebugDrawCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
00152                                                  float maxx, float maxy, float maxz, unsigned int col);
00153 
00154 void duDebugDrawGridXZ(struct duDebugDraw* dd, const float ox, const float oy, const float oz,
00155                                            const int w, const int h, const float size,
00156                                            const unsigned int col, const float lineWidth);
00157 
00158 
00159 // Versions without begin/end, can be used to draw multiple primitives.
00160 void duAppendCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
00161                                                   float maxx, float maxy, float maxz, unsigned int col);
00162 
00163 void duAppendBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
00164                                          float maxx, float maxy, float maxz, unsigned int col);
00165 
00166 void duAppendBoxPoints(struct duDebugDraw* dd, float minx, float miny, float minz,
00167                                            float maxx, float maxy, float maxz, unsigned int col);
00168 
00169 void duAppendArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
00170                                  const float x1, const float y1, const float z1, const float h,
00171                                  const float as0, const float as1, unsigned int col);
00172 
00173 void duAppendArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
00174                                    const float x1, const float y1, const float z1,
00175                                    const float as0, const float as1, unsigned int col);
00176 
00177 void duAppendCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
00178                                         const float r, unsigned int col);
00179 
00180 void duAppendCross(struct duDebugDraw* dd, const float x, const float y, const float z,
00181                                    const float size, unsigned int col);
00182 
00183 void duAppendBox(struct duDebugDraw* dd, float minx, float miny, float minz,
00184                                  float maxx, float maxy, float maxz, const unsigned int* fcol);
00185 
00186 void duAppendCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
00187                                           float maxx, float maxy, float maxz, unsigned int col);
00188 
00189 
00190 class duDisplayList : public duDebugDraw
00191 {
00192         float* m_pos;
00193         unsigned int* m_color;
00194         int m_size;
00195         int m_cap;
00196 
00197         bool m_depthMask;
00198         duDebugDrawPrimitives m_prim;
00199         float m_primSize;
00200         
00201         void resize(int cap);
00202         
00203 public:
00204         duDisplayList(int cap = 512);
00205         ~duDisplayList();
00206         virtual void depthMask(bool state);
00207         virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f);
00208         virtual void vertex(const float x, const float y, const float z, unsigned int color);
00209         virtual void vertex(const float* pos, unsigned int color);
00210         virtual void end();
00211         void clear();
00212         void draw(struct duDebugDraw* dd);
00213 };
00214 
00215 
00216 #endif // DEBUGDRAW_H