~vladh/peony

peony/src/debugdraw.cpp -rw-r--r-- 5.9 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#include "../src_external/glad/glad.h"
#include "shaders.hpp"
#include "util.hpp"
#include "logs.hpp"
#include "debugdraw.hpp"
#include "intrinsics.hpp"


DebugDrawState *debugdraw::g_dds = nullptr;


namespace debugdraw {
  pny_internal void push_vertices(
    DebugDrawState *debug_draw_state,
    DebugDrawVertex vertices[],
    uint32 n_vertices
  ) {
    if(debug_draw_state->n_vertices_pushed + n_vertices > MAX_N_VERTICES) {
      logs::error(
        "Pushed too many DebugDraw vertices, did you forget to call debugdraw::clear()?"
      );
      return;
    }
    range (0, n_vertices) {
      debug_draw_state->vertices[debug_draw_state->n_vertices_pushed + idx] =
        vertices[idx];
    }
    debug_draw_state->n_vertices_pushed += n_vertices;
  }
}


void debugdraw::draw_line(
  DebugDrawState *debug_draw_state,
  v3 start_pos,
  v3 end_pos,
  v4 color
) {
  DebugDrawVertex vertices[2];
  vertices[0] = {start_pos, color};
  vertices[1] = {end_pos, color};
  push_vertices(debug_draw_state, vertices, 2);
}


void debugdraw::draw_ray(
  DebugDrawState *debug_draw_state,
  Ray *ray,
  real32 length,
  v4 color
) {
  v3 end_pos = ray->origin + ray->direction * length;
  v3 x_axis = util::get_orthogonal_vector(&ray->direction);
  v3 z_axis = cross(ray->direction, x_axis);
  real32 chevron_size = 0.2f;
  v3 chevron_1_pos = end_pos + ((-ray->direction + x_axis) * chevron_size);
  v3 chevron_2_pos = end_pos + ((-ray->direction - x_axis) * chevron_size);
  v3 chevron_3_pos = end_pos + ((-ray->direction + z_axis) * chevron_size);
  v3 chevron_4_pos = end_pos + ((-ray->direction - z_axis) * chevron_size);
  draw_line(debug_draw_state, ray->origin, end_pos, color);
  draw_line(debug_draw_state, chevron_1_pos, end_pos, color);
  draw_line(debug_draw_state, chevron_2_pos, end_pos, color);
  draw_line(debug_draw_state, chevron_3_pos, end_pos, color);
  draw_line(debug_draw_state, chevron_4_pos, end_pos, color);
}


void debugdraw::draw_quad(
  DebugDrawState *debug_draw_state,
  v3 p1, // clockwise: top left
  v3 p2, // top right
  v3 p3, // bottom right
  v3 p4, // bottom left
  v4 color
) {
  draw_line(debug_draw_state, p1, p2, color);
  draw_line(debug_draw_state, p2, p3, color);
  draw_line(debug_draw_state, p3, p4, color);
  draw_line(debug_draw_state, p4, p1, color);
}


void debugdraw::draw_box(
  DebugDrawState *debug_draw_state,
  v3 p1, // clockwise top face: top left
  v3 p2, // top right
  v3 p3, // bottom right
  v3 p4, // top left
  v3 p5, // clockwise bottom face: top left
  v3 p6, // top right
  v3 p7, // bottom right
  v3 p8, // top left
  v4 color
) {
  draw_quad(debug_draw_state, p1, p2, p3, p4, color);
  draw_quad(debug_draw_state, p5, p6, p7, p8, color);
  draw_quad(debug_draw_state, p1, p2, p6, p5, color);
  draw_quad(debug_draw_state, p2, p3, p7, p6, color);
  draw_quad(debug_draw_state, p3, p4, p8, p7, color);
  draw_quad(debug_draw_state, p4, p1, p5, p8, color);
}


void debugdraw::draw_obb(
  DebugDrawState *debug_draw_state,
  Obb *obb,
  v4 color
) {
  v3 z_axis = cross(obb->x_axis, obb->y_axis);
  v3 dir1 = obb->x_axis * obb->extents[0];
  v3 dir2 = obb->y_axis * obb->extents[1];
  v3 dir3 = z_axis * obb->extents[2];
  v3 p1 = obb->center - dir1 + dir2 - dir3;
  v3 p2 = obb->center + dir1 + dir2 - dir3;
  v3 p3 = obb->center + dir1 + dir2 + dir3;
  v3 p4 = obb->center - dir1 + dir2 + dir3;
  v3 p5 = obb->center - dir1 - dir2 - dir3;
  v3 p6 = obb->center + dir1 - dir2 - dir3;
  v3 p7 = obb->center + dir1 - dir2 + dir3;
  v3 p8 = obb->center - dir1 - dir2 + dir3;
  draw_quad(debug_draw_state, p1, p2, p3, p4, color);
  draw_quad(debug_draw_state, p5, p6, p7, p8, color);
  draw_quad(debug_draw_state, p1, p2, p6, p5, color);
  draw_quad(debug_draw_state, p2, p3, p7, p6, color);
  draw_quad(debug_draw_state, p3, p4, p8, p7, color);
  draw_quad(debug_draw_state, p4, p1, p5, p8, color);
}


void debugdraw::draw_point(
  DebugDrawState *debug_draw_state,
  v3 position,
  real32 size,
  v4 color
) {
  Obb obb = {
    .center=position,
    .x_axis=v3(1.0f, 0.0f, 0.0f),
    .y_axis=v3(0.0f, 1.0f, 0.0f),
    .extents=v3(size),
  };
  draw_obb(debug_draw_state, &obb, color);
}


void debugdraw::clear(DebugDrawState *debug_draw_state) {
  debug_draw_state->n_vertices_pushed = 0;
}


void debugdraw::render(DebugDrawState *debug_draw_state) {
  glBindVertexArray(debug_draw_state->vao);
  glBindBuffer(GL_ARRAY_BUFFER, debug_draw_state->vbo);
  glBufferData(
    GL_ARRAY_BUFFER,
    VERTEX_SIZE * debug_draw_state->n_vertices_pushed,
    debug_draw_state->vertices,
    GL_STATIC_DRAW
  );

  glUseProgram(debug_draw_state->shader_asset.program);

  glDrawArrays(GL_LINES, 0, debug_draw_state->n_vertices_pushed);
}


void debugdraw::init(
  DebugDrawState* debug_draw_state,
  MemoryPool *memory_pool
) {
  MemoryPool temp_memory_pool = {};

  // Shaders
  {
     shaders::init_shader_asset(
      &debug_draw_state->shader_asset,
      &temp_memory_pool,
      "debugdraw", shaders::Type::standard,
      "debugdraw.vert", "debugdraw.frag", ""
    );
    debug_draw_state->shader_asset.did_set_texture_uniforms = true;
  }

  // VAO
  {
    glGenVertexArrays(1, &debug_draw_state->vao);
    glGenBuffers(1, &debug_draw_state->vbo);
    glBindVertexArray(debug_draw_state->vao);
    glBindBuffer(GL_ARRAY_BUFFER, debug_draw_state->vbo);
    glBufferData(
      GL_ARRAY_BUFFER, VERTEX_SIZE * MAX_N_VERTICES,
      NULL, GL_DYNAMIC_DRAW
    );

    uint32 location;

    // position (vec3)
    location = 0;
    glEnableVertexAttribArray(location);
    glVertexAttribPointer(
      location, 3, GL_FLOAT, GL_FALSE, VERTEX_SIZE, (void*)(0)
    );

    // color (vec4)
    location = 1;
    glEnableVertexAttribArray(location);
    glVertexAttribPointer(
      location, 4, GL_FLOAT, GL_FALSE, VERTEX_SIZE, (void*)(3 * sizeof(real32))
    );
  }

  memory::destroy_memory_pool(&temp_memory_pool);

  debugdraw::g_dds = debug_draw_state;
}