Planeshift
DebugDraw.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 // 1. The origin of this software must not be misrepresented; you must not
11 // claim that you wrote the original software. If you use this software
12 // in a product, an acknowledgment in the product documentation would be
13 // appreciated but is not required.
14 // 2. Altered source versions must be plainly marked as such, and must not be
15 // misrepresented as being the original software.
16 // 3. This notice may not be removed or altered from any source distribution.
17 //
18 
19 #ifndef DEBUGDRAW_H
20 #define DEBUGDRAW_H
21 
22 // Some math headers don't have PI defined.
23 static const float DU_PI = 3.14159265f;
24 
26 {
31 };
32 
35 {
36  virtual ~duDebugDraw() = 0;
37 
38  virtual void depthMask(bool state) = 0;
39 
40  virtual void texture(bool state) = 0;
41 
45  virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f) = 0;
46 
50  virtual void vertex(const float* pos, unsigned int color) = 0;
51 
55  virtual void vertex(const float x, const float y, const float z, unsigned int color) = 0;
56 
60  virtual void vertex(const float* pos, unsigned int color, const float* uv) = 0;
61 
65  virtual void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) = 0;
66 
68  virtual void end() = 0;
69 };
70 
71 inline unsigned int duRGBA(int r, int g, int b, int a)
72 {
73  return ((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16) | ((unsigned int)a << 24);
74 }
75 
76 inline unsigned int duRGBAf(float fr, float fg, float fb, float fa)
77 {
78  unsigned char r = (unsigned char)(fr*255.0f);
79  unsigned char g = (unsigned char)(fg*255.0f);
80  unsigned char b = (unsigned char)(fb*255.0f);
81  unsigned char a = (unsigned char)(fa*255.0f);
82  return duRGBA(r,g,b,a);
83 }
84 
85 unsigned int duIntToCol(int i, int a);
86 void duIntToCol(int i, float* col);
87 
88 inline unsigned int duMultCol(const unsigned int col, const unsigned int d)
89 {
90  const unsigned int r = col & 0xff;
91  const unsigned int g = (col >> 8) & 0xff;
92  const unsigned int b = (col >> 16) & 0xff;
93  const unsigned int a = (col >> 24) & 0xff;
94  return duRGBA((r*d) >> 8, (g*d) >> 8, (b*d) >> 8, a);
95 }
96 
97 inline unsigned int duDarkenCol(unsigned int col)
98 {
99  return ((col >> 1) & 0x007f7f7f) | (col & 0xff000000);
100 }
101 
102 inline unsigned int duLerpCol(unsigned int ca, unsigned int cb, unsigned int u)
103 {
104  const unsigned int ra = ca & 0xff;
105  const unsigned int ga = (ca >> 8) & 0xff;
106  const unsigned int ba = (ca >> 16) & 0xff;
107  const unsigned int aa = (ca >> 24) & 0xff;
108  const unsigned int rb = cb & 0xff;
109  const unsigned int gb = (cb >> 8) & 0xff;
110  const unsigned int bb = (cb >> 16) & 0xff;
111  const unsigned int ab = (cb >> 24) & 0xff;
112 
113  unsigned int r = (ra*(255-u) + rb*u)/255;
114  unsigned int g = (ga*(255-u) + gb*u)/255;
115  unsigned int b = (ba*(255-u) + bb*u)/255;
116  unsigned int a = (aa*(255-u) + ab*u)/255;
117  return duRGBA(r,g,b,a);
118 }
119 
120 inline unsigned int duTransCol(unsigned int c, unsigned int a)
121 {
122  return (a<<24) | (c & 0x00ffffff);
123 }
124 
125 
126 void duCalcBoxColors(unsigned int* colors, unsigned int colTop, unsigned int colSide);
127 
128 void duDebugDrawCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
129  float maxx, float maxy, float maxz, unsigned int col, const float lineWidth);
130 
131 void duDebugDrawBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
132  float maxx, float maxy, float maxz, unsigned int col, const float lineWidth);
133 
134 void duDebugDrawArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
135  const float x1, const float y1, const float z1, const float h,
136  const float as0, const float as1, unsigned int col, const float lineWidth);
137 
138 void duDebugDrawArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
139  const float x1, const float y1, const float z1,
140  const float as0, const float as1, unsigned int col, const float lineWidth);
141 
142 void duDebugDrawCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
143  const float r, unsigned int col, const float lineWidth);
144 
145 void duDebugDrawCross(struct duDebugDraw* dd, const float x, const float y, const float z,
146  const float size, unsigned int col, const float lineWidth);
147 
148 void duDebugDrawBox(struct duDebugDraw* dd, float minx, float miny, float minz,
149  float maxx, float maxy, float maxz, const unsigned int* fcol);
150 
151 void duDebugDrawCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
152  float maxx, float maxy, float maxz, unsigned int col);
153 
154 void duDebugDrawGridXZ(struct duDebugDraw* dd, const float ox, const float oy, const float oz,
155  const int w, const int h, const float size,
156  const unsigned int col, const float lineWidth);
157 
158 
159 // Versions without begin/end, can be used to draw multiple primitives.
160 void duAppendCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
161  float maxx, float maxy, float maxz, unsigned int col);
162 
163 void duAppendBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
164  float maxx, float maxy, float maxz, unsigned int col);
165 
166 void duAppendBoxPoints(struct duDebugDraw* dd, float minx, float miny, float minz,
167  float maxx, float maxy, float maxz, unsigned int col);
168 
169 void duAppendArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
170  const float x1, const float y1, const float z1, const float h,
171  const float as0, const float as1, unsigned int col);
172 
173 void duAppendArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
174  const float x1, const float y1, const float z1,
175  const float as0, const float as1, unsigned int col);
176 
177 void duAppendCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
178  const float r, unsigned int col);
179 
180 void duAppendCross(struct duDebugDraw* dd, const float x, const float y, const float z,
181  const float size, unsigned int col);
182 
183 void duAppendBox(struct duDebugDraw* dd, float minx, float miny, float minz,
184  float maxx, float maxy, float maxz, const unsigned int* fcol);
185 
186 void duAppendCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
187  float maxx, float maxy, float maxz, unsigned int col);
188 
189 
191 {
192  float* m_pos;
193  unsigned int* m_color;
194  int m_size;
195  int m_cap;
196 
197  bool m_depthMask;
198  duDebugDrawPrimitives m_prim;
199  float m_primSize;
200 
201  void resize(int cap);
202 
203 public:
204  duDisplayList(int cap = 512);
205  ~duDisplayList();
206  virtual void depthMask(bool state);
207  virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f);
208  virtual void vertex(const float x, const float y, const float z, unsigned int color);
209  virtual void vertex(const float* pos, unsigned int color);
210  virtual void end();
211  void clear();
212  void draw(struct duDebugDraw* dd);
213 };
214 
215 
216 #endif // DEBUGDRAW_H
virtual void texture(bool state)=0
void duDebugDrawCylinderWire(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, unsigned int col, const float lineWidth)
virtual void vertex(const float *pos, unsigned int color)=0
Submit a vertex.
void duDebugDrawCircle(struct duDebugDraw *dd, const float x, const float y, const float z, const float r, unsigned int col, const float lineWidth)
void duAppendCross(struct duDebugDraw *dd, const float x, const float y, const float z, const float size, unsigned int col)
virtual void depthMask(bool state)=0
void duAppendArc(struct duDebugDraw *dd, const float x0, const float y0, const float z0, const float x1, const float y1, const float z1, const float h, const float as0, const float as1, unsigned int col)
void duCalcBoxColors(unsigned int *colors, unsigned int colTop, unsigned int colSide)
void duAppendBox(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, const unsigned int *fcol)
void duDebugDrawCylinder(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, unsigned int col)
void duAppendCircle(struct duDebugDraw *dd, const float x, const float y, const float z, const float r, unsigned int col)
unsigned int duDarkenCol(unsigned int col)
Definition: DebugDraw.h:97
unsigned int duIntToCol(int i, int a)
unsigned int duMultCol(const unsigned int col, const unsigned int d)
Definition: DebugDraw.h:88
virtual ~duDebugDraw()=0
unsigned int duLerpCol(unsigned int ca, unsigned int cb, unsigned int u)
Definition: DebugDraw.h:102
void duDebugDrawBoxWire(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, unsigned int col, const float lineWidth)
void duDebugDrawGridXZ(struct duDebugDraw *dd, const float ox, const float oy, const float oz, const int w, const int h, const float size, const unsigned int col, const float lineWidth)
void duDebugDrawArc(struct duDebugDraw *dd, const float x0, const float y0, const float z0, const float x1, const float y1, const float z1, const float h, const float as0, const float as1, unsigned int col, const float lineWidth)
void duDebugDrawCross(struct duDebugDraw *dd, const float x, const float y, const float z, const float size, unsigned int col, const float lineWidth)
unsigned int duTransCol(unsigned int c, unsigned int a)
Definition: DebugDraw.h:120
virtual void begin(duDebugDrawPrimitives prim, float size=1.0f)=0
Begin drawing primitives.
Abstract debug draw interface.
Definition: DebugDraw.h:34
void duAppendCylinder(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, unsigned int col)
unsigned int duRGBAf(float fr, float fg, float fb, float fa)
Definition: DebugDraw.h:76
void duDebugDrawBox(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, const unsigned int *fcol)
static const float DU_PI
Definition: DebugDraw.h:23
void duAppendArrow(struct duDebugDraw *dd, const float x0, const float y0, const float z0, const float x1, const float y1, const float z1, const float as0, const float as1, unsigned int col)
void duAppendBoxPoints(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, unsigned int col)
unsigned int duRGBA(int r, int g, int b, int a)
Definition: DebugDraw.h:71
virtual void end()=0
End drawing primitives.
void duAppendBoxWire(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, unsigned int col)
void duDebugDrawArrow(struct duDebugDraw *dd, const float x0, const float y0, const float z0, const float x1, const float y1, const float z1, const float as0, const float as1, unsigned int col, const float lineWidth)
duDebugDrawPrimitives
Definition: DebugDraw.h:25
void duAppendCylinderWire(struct duDebugDraw *dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, unsigned int col)