Debian Codegraph @

DebugUtils/Include/DebugDraw.h

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