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
25 enum duDebugDrawPrimitives
26 {
27 DU_DRAW_POINTS,
28 DU_DRAW_LINES,
29 DU_DRAW_TRIS,
30 DU_DRAW_QUADS,
31 };
32
33 /// Abstract debug draw interface.
34 struct duDebugDraw
35 {
36 virtual ~duDebugDraw() = 0;
37
38 virtual void depthMask(bool state) = 0;
39
40 virtual void texture(bool state) = 0;
41
42 /// Begin drawing primitives.
43 /// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives.
44 /// @param size [in] size of a primitive, applies to point size and line width only.
45 virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f) = 0;
46
47 /// Submit a vertex
48 /// @param pos [in] position of the verts.
49 /// @param color [in] color of the verts.
50 virtual void vertex(const float* pos, unsigned int color) = 0;
51
52 /// Submit a vertex
53 /// @param x,y,z [in] position of the verts.
54 /// @param color [in] color of the verts.
55 virtual void vertex(const float x, const float y, const float z, unsigned int color) = 0;
56
57 /// Submit a vertex
58 /// @param pos [in] position of the verts.
59 /// @param color [in] color of the verts.
60 virtual void vertex(const float* pos, unsigned int color, const float* uv) = 0;
61
62 /// Submit a vertex
63 /// @param x,y,z [in] position of the verts.
64 /// @param color [in] color of the verts.
65 virtual void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) = 0;
66
67 /// End drawing primitives.
68 virtual void end() = 0;
69
70 /// Compute a color for given area.
71 virtual unsigned int areaToCol(unsigned int area);
72 };
73
duRGBA(int r,int g,int b,int a)74 inline unsigned int duRGBA(int r, int g, int b, int a)
75 {
76 return ((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16) | ((unsigned int)a << 24);
77 }
78
duRGBAf(float fr,float fg,float fb,float fa)79 inline unsigned int duRGBAf(float fr, float fg, float fb, float fa)
80 {
81 unsigned char r = (unsigned char)(fr*255.0f);
82 unsigned char g = (unsigned char)(fg*255.0f);
83 unsigned char b = (unsigned char)(fb*255.0f);
84 unsigned char a = (unsigned char)(fa*255.0f);
85 return duRGBA(r,g,b,a);
86 }
87
88 unsigned int duIntToCol(int i, int a);
89 void duIntToCol(int i, float* col);
90
duMultCol(const unsigned int col,const unsigned int d)91 inline unsigned int duMultCol(const unsigned int col, const unsigned int d)
92 {
93 const unsigned int r = col & 0xff;
94 const unsigned int g = (col >> 8) & 0xff;
95 const unsigned int b = (col >> 16) & 0xff;
96 const unsigned int a = (col >> 24) & 0xff;
97 return duRGBA((r*d) >> 8, (g*d) >> 8, (b*d) >> 8, a);
98 }
99
duDarkenCol(unsigned int col)100 inline unsigned int duDarkenCol(unsigned int col)
101 {
102 return ((col >> 1) & 0x007f7f7f) | (col & 0xff000000);
103 }
104
duLerpCol(unsigned int ca,unsigned int cb,unsigned int u)105 inline unsigned int duLerpCol(unsigned int ca, unsigned int cb, unsigned int u)
106 {
107 const unsigned int ra = ca & 0xff;
108 const unsigned int ga = (ca >> 8) & 0xff;
109 const unsigned int ba = (ca >> 16) & 0xff;
110 const unsigned int aa = (ca >> 24) & 0xff;
111 const unsigned int rb = cb & 0xff;
112 const unsigned int gb = (cb >> 8) & 0xff;
113 const unsigned int bb = (cb >> 16) & 0xff;
114 const unsigned int ab = (cb >> 24) & 0xff;
115
116 unsigned int r = (ra*(255-u) + rb*u)/255;
117 unsigned int g = (ga*(255-u) + gb*u)/255;
118 unsigned int b = (ba*(255-u) + bb*u)/255;
119 unsigned int a = (aa*(255-u) + ab*u)/255;
120 return duRGBA(r,g,b,a);
121 }
122
duTransCol(unsigned int c,unsigned int a)123 inline unsigned int duTransCol(unsigned int c, unsigned int a)
124 {
125 return (a<<24) | (c & 0x00ffffff);
126 }
127
128
129 void duCalcBoxColors(unsigned int* colors, unsigned int colTop, unsigned int colSide);
130
131 void duDebugDrawCylinderWire(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 duDebugDrawBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
135 float maxx, float maxy, float maxz, unsigned int col, const float lineWidth);
136
137 void duDebugDrawArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
138 const float x1, const float y1, const float z1, const float h,
139 const float as0, const float as1, unsigned int col, const float lineWidth);
140
141 void duDebugDrawArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
142 const float x1, const float y1, const float z1,
143 const float as0, const float as1, unsigned int col, const float lineWidth);
144
145 void duDebugDrawCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
146 const float r, unsigned int col, const float lineWidth);
147
148 void duDebugDrawCross(struct duDebugDraw* dd, const float x, const float y, const float z,
149 const float size, unsigned int col, const float lineWidth);
150
151 void duDebugDrawBox(struct duDebugDraw* dd, float minx, float miny, float minz,
152 float maxx, float maxy, float maxz, const unsigned int* fcol);
153
154 void duDebugDrawCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
155 float maxx, float maxy, float maxz, unsigned int col);
156
157 void duDebugDrawGridXZ(struct duDebugDraw* dd, const float ox, const float oy, const float oz,
158 const int w, const int h, const float size,
159 const unsigned int col, const float lineWidth);
160
161
162 // Versions without begin/end, can be used to draw multiple primitives.
163 void duAppendCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
164 float maxx, float maxy, float maxz, unsigned int col);
165
166 void duAppendBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
167 float maxx, float maxy, float maxz, unsigned int col);
168
169 void duAppendBoxPoints(struct duDebugDraw* dd, float minx, float miny, float minz,
170 float maxx, float maxy, float maxz, unsigned int col);
171
172 void duAppendArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
173 const float x1, const float y1, const float z1, const float h,
174 const float as0, const float as1, unsigned int col);
175
176 void duAppendArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
177 const float x1, const float y1, const float z1,
178 const float as0, const float as1, unsigned int col);
179
180 void duAppendCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
181 const float r, unsigned int col);
182
183 void duAppendCross(struct duDebugDraw* dd, const float x, const float y, const float z,
184 const float size, unsigned int col);
185
186 void duAppendBox(struct duDebugDraw* dd, float minx, float miny, float minz,
187 float maxx, float maxy, float maxz, const unsigned int* fcol);
188
189 void duAppendCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
190 float maxx, float maxy, float maxz, unsigned int col);
191
192
193 class duDisplayList : public duDebugDraw
194 {
195 float* m_pos;
196 unsigned int* m_color;
197 int m_size;
198 int m_cap;
199
200 bool m_depthMask;
201 duDebugDrawPrimitives m_prim;
202 float m_primSize;
203
204 void resize(int cap);
205
206 public:
207 duDisplayList(int cap = 512);
208 ~duDisplayList();
209 virtual void depthMask(bool state);
210 virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f);
211 virtual void vertex(const float x, const float y, const float z, unsigned int color);
212 virtual void vertex(const float* pos, unsigned int color);
213 virtual void end();
214 void clear();
215 void draw(struct duDebugDraw* dd);
216 private:
217 // Explicitly disabled copy constructor and copy assignment operator.
218 duDisplayList(const duDisplayList&);
219 duDisplayList& operator=(const duDisplayList&);
220 };
221
222
223 #endif // DEBUGDRAW_H
224