Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pxdebugdraw.h « Graphics « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 577abafb079d7368ef58b9911a1c6212af977f8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//-----------------------------------------------------------------------------
//           Name: pxdebugdraw.h
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once

#include <Math/vec3.h>
#include <Math/vec4.h>
#include <Math/mat4.h>

#include <Graphics/textureref.h>
#include <Graphics/glstate.h>
#include <Graphics/vbocontainer.h>
#include <Graphics/vboringcontainer.h>

#include <Internal/referencecounter.h>
#include <Asset/Asset/texture.h>

#include <opengl.h>

#include <queue>
#include <map>
#include <string>

enum DDLifespan {
    _delete_on_draw,
    _delete_on_update,
    _fade,
    _persistent
};

enum DDFlag {
    _DD_NO_FLAG = 0,
    //Request disabled depth buffer.
    _DD_XRAY = (1<<0),
    _DD_STIPPLE = (1<<1),
    //Means there is a vec4(color) element interleaved in vbo after verts
    _DD_COLOR = (1<<2),
    //Whether or not text should be rendered at a set size or in-world.
    _DD_SCREEN_SPACE = (1<<3),
    //Dashed line segment.
    _DD_DASHED = (1<<4)
};

enum DDElement {
    kUndefined,
    kWire,
    kText,
    kBillboard,
    kRibbon,
    kLine,
    kLines,
    kWireMesh,
    kStippleMesh,
    kPoint,
    kWireSphere,
    kWireBox,
    kCircle,
    kWireCylinder
};

class DebugDrawElement {
        DDLifespan lifespan;
    protected:
        DDFlag flags;
        
    public:
        bool visible;
        DDElement type;
        float fade_amount;
        DebugDrawElement(const DDFlag& _flags ); 
        
        virtual ~DebugDrawElement(); 
        virtual void Draw()=0; 

        //Move the origin of the object.
        virtual bool SetPosition(const vec3& pos);

        inline void SetLifespan(const DDLifespan &_lifespan) 
            {lifespan = _lifespan;}
        inline const DDLifespan& GetLifespan() 
            {return lifespan;}
};

class DebugDrawWire : public DebugDrawElement {

private:
    void DrawVert();
    void DrawVertColor();

protected:
    RC_VBOContainer vbo;
    mat4 transform;
    vec4 color;
    float opacity;

public:
    void DecreaseOpacity(float how_much);
    bool IsInvisible();
    DebugDrawWire(const DDFlag& _flags);
    DebugDrawWire(const vec4 &_color, const DDFlag& _flags);
    DebugDrawWire(const mat4 &_transform, const vec4 &_color, const DDFlag& _flags);
    DebugDrawWire(const RC_VBOContainer& _vbo, const mat4 &_transform, const vec4 &_color, const DDFlag& _flags);
    void Draw() override;
};

class LabelText;
class DebugDrawText : public DebugDrawElement {
        static RC_VBOContainer vbo;
        static const int kBufSize = 256;
        char text[kBufSize];
        vec4 color;
        mat4 transform;
        float scale;
    public:
        void Draw() override;
        DebugDrawText(const vec3 &_position,
                      const float &_scale,
                      const std::string &_content,
                      const DDFlag& _flags,
                      const vec4 & _color);
        ~DebugDrawText() override;
        bool SetPosition(const vec3& position) override;
};


class DebugDrawBillboard: public DebugDrawElement {
public:
    void Draw() override;
    bool SetPosition(const vec3 &position) override;
    void SetScale(float scale);
    DebugDrawBillboard(const TextureRef& ref, const vec3 &position, float scale, const vec4& color, AlphaMode mode);
private: 
    TextureRef ref_;
    vec3 position_;
    vec4 color_;
    float scale_;
    AlphaMode mode_;
};

class DebugDrawRibbon: public DebugDrawElement {
public:
    void Draw() override;
    DebugDrawRibbon(const DDFlag& flags);
    DebugDrawRibbon(const vec3 &start, 
                    const vec3 &end, 
                    const vec4 &start_color, 
                    const vec4 &end_color,
                    const float start_width, 
                    const float end_width,
                    const DDFlag& flags);
    void AddPoint( const vec3 &pos, const vec4 &color, float width );
private: 
    struct RibbonPoint {
        vec3 pos;
        vec4 color;
        float width;
    };
    std::vector<RibbonPoint> ribbon_points;
};

class DebugDrawLine : public DebugDrawWire {
public:
    DebugDrawLine(const vec3 &start, 
        const vec3 &end, 
        const vec4 &start_color,
        const vec4 &end_color,
        const DDFlag& flags = _DD_NO_FLAG);
};

class DebugDrawLines : public DebugDrawWire {
public:
    DebugDrawLines(
        const std::vector<float> &vertices,
        const std::vector<unsigned> &indices,
        const vec4 &color,
        const DDFlag& flags = _DD_NO_FLAG);

    DebugDrawLines(
        const std::vector<vec3> &vertices,
        const vec4 &color,
        const DDFlag& flags = _DD_NO_FLAG);
};

class DebugDrawWireMesh : public DebugDrawWire {
public:
    DebugDrawWireMesh(const RC_VBOContainer& _vbo,
                      const mat4 &_transform,
                      const vec4 &_color);
    ~DebugDrawWireMesh() override;
};

class DebugDrawStippleMesh : public DebugDrawElement {

protected:
    RC_VBOContainer vbo;
    mat4 transform;
    vec4 color;
public:
    DebugDrawStippleMesh(const RC_VBOContainer& _vbo,
                         const mat4 &_transform,
                         const vec4 &_color,
                         const DDFlag& _flags);
    void Draw() override;
};


class DebugDrawPoint : public DebugDrawElement {
        static RC_VBOContainer vbo;
        vec4 color;
        mat4 transform; 
    public:
        void Draw() override;
        DebugDrawPoint(const vec3 &point, 
                      const vec4 &color,
                      const DDFlag& _flags = _DD_NO_FLAG);
};

class DebugDrawWireSphere : public DebugDrawWire {
    private:
        static RC_VBOContainer wire_sphere_vbo;
    public:
        DebugDrawWireSphere(const mat4 _transform,
                            const vec4 &_color );

        DebugDrawWireSphere(const vec3 &position, 
                            const float radius,
                            const vec3 &scale = vec3(1.0f,1.0f,1.0f),
                            const vec4 &color = vec4(1.0f,1.0f,1.0f,1.0f));
        ~DebugDrawWireSphere() override;
};

class DebugDrawCircle : public DebugDrawWire {
private:
    static RC_VBOContainer circle_vert_vbo;
public:
    DebugDrawCircle(const mat4 &transform,
                    const vec4 &color = vec4(1.0f,1.0f,1.0f,1.0f),
                    const DDFlag& flags = _DD_NO_FLAG);
};

class DebugDrawWireCylinder : public DebugDrawWire {
    private:
        static RC_VBOContainer wire_cylinder_vbo;
    public:
        DebugDrawWireCylinder(const vec3 &position, 
                              const float radius,
                              const float height,
                              const vec4 &color);
        ~DebugDrawWireCylinder() override;
};


class DebugDrawWireBox : public DebugDrawWire {
    private:
        static RC_VBOContainer wire_box_vbo;
    public:
        DebugDrawWireBox(const vec3 &position, 
                         const vec3 &dimensions,
                         const vec4 &color);
};

typedef std::map<int,DebugDrawElement*> DebugDrawElementMap;

class TempLines {
public:
    TempLines():vbo(1024*1024, kVBOStream | kVBOFloat){}
    VBORingContainer vbo;
    std::vector<float> verts;
    void Draw();
};

class DebugDraw {
private:
    std::map<std::string, RC_VBOContainer> mesh_edges_map;
    TempLines delete_on_update;
    TempLines delete_on_draw;

    std::queue<int> free_ids;
    int id_index;
    DebugDrawElementMap elements;
    int AddElement( DebugDrawElement* object, const DDLifespan lifespan);
public:
    void Draw();
    void Update(float timestep);
    void Remove(int);
    DebugDrawElement* GetElement(int id);
    int AddLine(const vec3 &start, 
        const vec3 &end, 
        const vec4 &color, 
        const DDLifespan lifespan,
        const DDFlag& flags = _DD_NO_FLAG);
    int AddLine(const vec3 &start, 
        const vec3 &end, 
        const vec4 &start_color, 
        const vec4 &end_color, 
        const DDLifespan lifespan,
        const DDFlag& flags = _DD_NO_FLAG);
    int AddRibbon(const vec3 &start, 
        const vec3 &end, 
        const vec4 &start_color, 
        const vec4 &end_color, 
        const float start_width,
        const float end_width, 
        const DDLifespan lifespan,
        const DDFlag& flags = _DD_NO_FLAG);
    int AddRibbon( const DDLifespan lifespan,
        const DDFlag& flags = _DD_NO_FLAG);
    int AddPoint( const vec3 &point, 
        const vec4 &color, 
        const DDLifespan lifespan, 
        const DDFlag& flags);
    int AddWireSphere(const vec3 &position, 
        const float radius, 
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddWireScaledSphere(const vec3 &position, 
        const float radius, 
        const vec3 &scale,
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddTransformedWireScaledSphere(const mat4 &transform,
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddWireCylinder(const vec3 &position, 
        const float radius, 
        const float height, 
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddWireBox(const vec3 &position, 
        const vec3 &dimensions, 
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddBillboard(const TextureRef &ref, 
        const vec3 &position, 
        float scale, 
        const vec4 &color, 
        AlphaMode mode, 
        const DDLifespan lifespan);
    int AddWireMesh(const std::string &path, 
        const mat4 &transform,
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddLineObject(const RC_VBOContainer &vbo,
        const mat4 &transform,
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddStippleMesh(const RC_VBOContainer &vbo,
        const mat4 &transform,
        const vec4 &color, 
        const DDLifespan lifespan);
    int AddCircle(const mat4 &transform,
                  const vec4 &color, 
                  const DDLifespan lifespan,
                  const DDFlag& flags = _DD_NO_FLAG);
    int AddText(const vec3 &position,
        const std::string &content, 
        const float& scale,
        const DDLifespan lifespan,
        const DDFlag& flags,
        const vec4& color = vec4(1.0f, 1.0f, 1.0f, 1.0f));
    bool SetPosition( int id,
        const vec3 &position );
    bool SetColor( int id,
        const vec4 &color );
    bool SetVisible( int id, bool visible );
    int AddLines( 
        const std::vector<float> &vertices, 
        const std::vector<unsigned> &indices, 
        const vec4 &color, 
        const DDLifespan lifespan, 
        const DDFlag& flags );
    int AddLines( 
        const std::vector<vec3> &vertices, 
        const vec4 &color, 
        const DDLifespan lifespan, 
        const DDFlag& flags );
    void Dispose();
    DebugDraw();
    ~DebugDraw(); 

    static DebugDraw* Instance() {
        static DebugDraw instance;
        return &instance;
    }
};

class DebugDrawAux {
public:
    void Update(float timestep);

    bool visible_sound_spheres;

    static DebugDrawAux* Instance() {
        static DebugDrawAux instance;
        return &instance;
    }
};

DDLifespan LifespanFromInt(int lifespan_int);