Unravel Engine C++ Reference
Loading...
Searching...
No Matches
debugdraw.cpp
Go to the documentation of this file.
1#include "debugdraw.h"
2
3#include "debugdraw.h"
4#include <cmath>
5
6namespace gfx
7{
8dd_raii::dd_raii(uint16_t _viewId, bool _depthTestLess, bgfx::Encoder* _encoder)
9{
10 view = _viewId;
11 encoder.begin(_viewId, _depthTestLess, _encoder);
12
13 encoder.setColor(0x00000000);
14
15 draw_billboard(encoder, BGFX_INVALID_HANDLE, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, 1.0f);
16
17 encoder.setColor(0xffffffff);
18}
19
21{
22 encoder.end();
23}
24
26 bgfx::TextureHandle icon_texture,
27 const bx::Vec3& icon_center,
28 const bx::Vec3& camera_pos,
29 const bx::Vec3& camera_look_dir,
30 float half_size)
31{
32 bx::Vec3 toCamera = bx::mul(camera_look_dir, -1.0f);
33
34 // Choose a world-up vector. If the icon-to-camera vector is nearly parallel to the default up, choose a different
35 // one.
36 bx::Vec3 worldUp = {0.0f, 1.0f, 0.0f};
37 if(std::fabs(bx::dot(toCamera, worldUp)) > 0.99f)
38 {
39 worldUp.x = 1.0f;
40 worldUp.y = 0.0f;
41 worldUp.z = 0.0f;
42 }
43
44 // Compute billboard basis:
45 // Right vector = cross(worldUp, toCamera)
46 bx::Vec3 right = bx::cross(worldUp, toCamera);
47 right = bx::normalize(right);
48
49 // Up vector = cross(toCamera, right)
50 bx::Vec3 up = bx::cross(toCamera, right);
51
52 // Build the billboard transform matrix.
53 // This matrix is constructed in column-major order:
54 // - first column: scaled right vector (half-size)
55 // - second column: scaled up vector (half-size)
56 // - third column: forward (toCamera) (unchanged, as a direction)
57 // - fourth column: translation (icon_center)
58 float mtx[16];
59 mtx[0] = up.x * half_size;
60 mtx[1] = up.y * half_size;
61 mtx[2] = up.z * half_size;
62 mtx[3] = 0.0f;
63 mtx[4] = -right.x * half_size;
64 mtx[5] = -right.y * half_size;
65 mtx[6] = -right.z * half_size;
66 mtx[7] = 0.0f;
67 mtx[8] = toCamera.x;
68 mtx[9] = toCamera.y;
69 mtx[10] = toCamera.z;
70 mtx[11] = 0.0f;
71 mtx[12] = icon_center.x;
72 mtx[13] = icon_center.y;
73 mtx[14] = icon_center.z;
74 mtx[15] = 1.0f;
75
76 // Push our billboard transform.
77 dd.pushTransform(mtx);
78
79 // Now draw a unit quad (centered at (0,0,0) with a normal along +Z).
80 // Because the transform has already positioned, rotated, and scaled the quad,
81 // we pass identity parameters to drawQuad.
82 dd.drawQuad(icon_texture, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, 1.0f);
83
84 // Pop the transform to restore previous state.
85 dd.popTransform();
86}
87
88} // namespace gfx
void draw_billboard(DebugDrawEncoder &dd, bgfx::TextureHandle icon_texture, const bx::Vec3 &icon_center, const bx::Vec3 &camera_pos, const bx::Vec3 &camera_look_dir, float half_size)
Definition debugdraw.cpp:25
bgfx::Encoder encoder
Definition graphics.h:38
void drawQuad(const bx::Vec3 &_normal, const bx::Vec3 &_center, float _size)
void pushTransform(const void *_mtx)
dd_raii(uint16_t _viewId, bool _depthTestLess=true, bgfx::Encoder *_encoder=NULL)
Definition debugdraw.cpp:8
view_id view
Definition debugdraw.h:16