a16f5020481a35a7ce3b76048859c144b89d3238
[vg.git] / vg_lines.h
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
2
3 #ifndef VG_LINES_H
4 #define VG_LINES_H
5
6 #define VG_GAME
7 #include "vg/vg.h"
8
9 typedef v3f line_co;
10
11 #define VG__RED 0xff0000ff
12 #define VG__GREEN 0xff00ff00
13 #define VG__BLUE 0xffff0000
14 #define VG__WHITE 0xffffffff
15 #define VG__BLACK 0xff000000
16 #define VG__CLEAR 0x00ffffff
17 #define VG__PINK 0xffff00ff
18 #define VG__YELOW 0xff00ffff
19 #define VG__CYAN 0xffffff00
20 #define VG__NONE 0x00000000
21
22 static struct vg_shader _shader_lines = {
23 .name = "[vg] lines",
24 .link = NULL,
25 .vs = {
26 .orig_file = NULL,
27 .static_src =
28
29 "uniform mat4 uPv;"
30 "layout (location=0) in vec3 a_co;"
31 "layout (location=1) in vec4 a_colour;"
32 ""
33 "out vec4 s_colour;"
34 ""
35 "void main()"
36 "{"
37 " vec4 vert_pos = uPv * vec4( a_co, 1.0 );"
38 " s_colour = a_colour;"
39 " gl_Position = vert_pos;"
40 "}"
41 },
42 .fs = {
43 .orig_file = NULL,
44 .static_src =
45
46 "out vec4 FragColor;"
47 ""
48 "in vec4 s_colour;"
49 ""
50 "void main()"
51 "{"
52 " FragColor = s_colour;"
53 "}"
54 }
55 };
56
57 struct{
58 u32 draw,
59 allow_input;
60
61 struct vg_lines_vert{
62 v3f co;
63 u32 colour;
64 }
65 *vertex_buffer;
66
67 GLuint vao, vbo;
68 }
69 static vg_lines;
70
71 VG_STATIC void vg_lines_init(void)
72 {
73 vg_info( "vg_lines_init\n" );
74
75 vg_console_reg_var( "vg_lines", &vg_lines.draw, k_var_dtype_i32,
76 VG_VAR_CHEAT );
77 vg_shader_register( &_shader_lines );
78
79 vg_acquire_thread_sync();
80 {
81 glGenVertexArrays( 1, &vg_lines.vao );
82 glGenBuffers( 1, &vg_lines.vbo );
83 glBindVertexArray( vg_lines.vao );
84 glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
85
86 u32 size = 50000 * sizeof( struct vg_lines_vert );
87
88 vg_lines.vertex_buffer =
89 vg_create_linear_allocator(vg_mem.rtmemory, size, VG_MEMORY_REALTIME);
90
91 glBufferData( GL_ARRAY_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
92 glBindVertexArray( vg_lines.vao );
93 VG_CHECK_GL_ERR();
94
95 /* Pointers */
96 glVertexAttribPointer(
97 0,
98 3,
99 GL_FLOAT,
100 GL_FALSE,
101 sizeof( struct vg_lines_vert ),
102 (void *)0
103 );
104 glEnableVertexAttribArray( 0 );
105
106 glVertexAttribPointer(
107 1,
108 4,
109 GL_UNSIGNED_BYTE,
110 GL_TRUE,
111 sizeof( struct vg_lines_vert ),
112 (void*)(offsetof( struct vg_lines_vert, colour ))
113 );
114 glEnableVertexAttribArray( 1 );
115
116 VG_CHECK_GL_ERR();
117 vg_success( "done\n" );
118 }
119
120 vg_release_thread_sync();
121 vg_lines.allow_input = 1;
122 }
123
124 VG_STATIC void vg_lines_drawall( void )
125 {
126 glUseProgram( _shader_lines.id );
127
128 glUniformMatrix4fv( glGetUniformLocation( _shader_lines.id, "uPv" ),
129 1, GL_FALSE, (float *)vg.pv );
130
131 glBindVertexArray( vg_lines.vao );
132 glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
133
134 u32 bufusage = vg_linear_get_cur(vg_lines.vertex_buffer);
135
136 glBufferSubData( GL_ARRAY_BUFFER, 0, bufusage, vg_lines.vertex_buffer );
137
138 glEnable( GL_BLEND );
139 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
140 glBlendEquation( GL_FUNC_ADD );
141
142 if( vg_lines.draw )
143 glDrawArrays( GL_LINES, 0, bufusage / sizeof(struct vg_lines_vert) );
144
145 glDisable( GL_BLEND );
146 vg_linear_clear( vg_lines.vertex_buffer );
147 }
148
149 VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc )
150 {
151 if( !vg_lines.allow_input )
152 return;
153
154 u32 size = 2 * sizeof(struct vg_lines_vert);
155 struct vg_lines_vert *v = vg_linear_alloc( vg_lines.vertex_buffer, size );
156
157 v3_copy( from, v[0].co );
158 v3_copy( to, v[1].co );
159
160 v[0].colour = fc;
161 v[1].colour = tc;
162 }
163
164 VG_STATIC void vg_line( line_co from, line_co to, u32 colour )
165 {
166 vg_line2( from, to, colour, colour );
167 }
168
169 VG_STATIC void line_tangent_basis( v3f n, v3f tx, v3f ty )
170 {
171 /* Compute tangent basis (box2d) */
172 if( fabsf( n[0] ) >= 0.57735027f ){
173 tx[0] = n[1];
174 tx[1] = -n[0];
175 tx[2] = 0.0f;
176 }
177 else{
178 tx[0] = 0.0f;
179 tx[1] = n[2];
180 tx[2] = -n[1];
181 }
182
183 v3_normalize( tx );
184 v3_cross( n, tx, ty );
185 }
186
187 VG_STATIC void vg_line_arrow( line_co co, line_co dir, float size, u32 colour )
188 {
189 v3f p1, tx, ty, p2, p3;
190 v3_muladds( co, dir, size, p1 );
191 line_tangent_basis( dir, tx, ty );
192
193 v3_muladds( p1, dir, -size * 0.125f, p2 );
194 v3_muladds( p2, ty, size * 0.125f, p3 );
195 v3_muladds( p2, ty, -size * 0.125f, p2 );
196
197 vg_line( co, p1, colour );
198 vg_line( p1, p2, colour );
199 vg_line( p1, p3, colour );
200 }
201
202 VG_STATIC void vg_line_boxf( boxf box, u32 colour )
203 {
204 v3f p000, p001, p010, p011, p100, p101, p110, p111;
205
206 p000[0]=box[0][0];p000[1]=box[0][1];p000[2]=box[0][2];
207 p001[0]=box[0][0];p001[1]=box[0][1];p001[2]=box[1][2];
208 p010[0]=box[0][0];p010[1]=box[1][1];p010[2]=box[0][2];
209 p011[0]=box[0][0];p011[1]=box[1][1];p011[2]=box[1][2];
210
211 p100[0]=box[1][0];p100[1]=box[0][1];p100[2]=box[0][2];
212 p101[0]=box[1][0];p101[1]=box[0][1];p101[2]=box[1][2];
213 p110[0]=box[1][0];p110[1]=box[1][1];p110[2]=box[0][2];
214 p111[0]=box[1][0];p111[1]=box[1][1];p111[2]=box[1][2];
215
216 vg_line( p000, p001, colour );
217 vg_line( p001, p011, colour );
218 vg_line( p011, p010, colour );
219 vg_line( p010, p000, colour );
220
221 vg_line( p100, p101, colour );
222 vg_line( p101, p111, colour );
223 vg_line( p111, p110, colour );
224 vg_line( p110, p100, colour );
225
226 vg_line( p100, p000, colour );
227 vg_line( p101, p001, colour );
228 vg_line( p110, p010, colour );
229 vg_line( p111, p011, colour );
230 }
231
232 VG_STATIC void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour )
233 {
234 v3f p000, p001, p010, p011, p100, p101, p110, p111;
235
236 p000[0]=box[0][0];p000[1]=box[0][1];p000[2]=box[0][2];
237 p001[0]=box[0][0];p001[1]=box[0][1];p001[2]=box[1][2];
238 p010[0]=box[0][0];p010[1]=box[1][1];p010[2]=box[0][2];
239 p011[0]=box[0][0];p011[1]=box[1][1];p011[2]=box[1][2];
240
241 p100[0]=box[1][0];p100[1]=box[0][1];p100[2]=box[0][2];
242 p101[0]=box[1][0];p101[1]=box[0][1];p101[2]=box[1][2];
243 p110[0]=box[1][0];p110[1]=box[1][1];p110[2]=box[0][2];
244 p111[0]=box[1][0];p111[1]=box[1][1];p111[2]=box[1][2];
245
246 m4x3_mulv( m, p000, p000 );
247 m4x3_mulv( m, p001, p001 );
248 m4x3_mulv( m, p010, p010 );
249 m4x3_mulv( m, p011, p011 );
250 m4x3_mulv( m, p100, p100 );
251 m4x3_mulv( m, p101, p101 );
252 m4x3_mulv( m, p110, p110 );
253 m4x3_mulv( m, p111, p111 );
254
255 vg_line( p000, p001, colour );
256 vg_line( p001, p011, colour );
257 vg_line( p011, p010, colour );
258 vg_line( p010, p000, colour );
259
260 vg_line( p100, p101, colour );
261 vg_line( p101, p111, colour );
262 vg_line( p111, p110, colour );
263 vg_line( p110, p100, colour );
264
265 vg_line( p100, p000, colour );
266 vg_line( p101, p001, colour );
267 vg_line( p110, p010, colour );
268 vg_line( p111, p011, colour );
269
270 vg_line( p000, p110, colour );
271 vg_line( p100, p010, colour );
272 }
273
274 VG_STATIC void vg_line_cross(v3f pos,u32 colour, float scale)
275 {
276 v3f p0, p1;
277 v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 );
278 v3_add( (v3f){-scale,0.0f,0.0f}, pos, p1 );
279 vg_line( p0, p1, colour );
280 v3_add( (v3f){0.0f, scale,0.0f}, pos, p0 );
281 v3_add( (v3f){0.0f,-scale,0.0f}, pos, p1 );
282 vg_line( p0, p1, colour );
283 v3_add( (v3f){0.0f,0.0f, scale}, pos, p0 );
284 v3_add( (v3f){0.0f,0.0f,-scale}, pos, p1 );
285 vg_line( p0, p1, colour );
286 }
287
288 VG_STATIC void vg_line_pt3( v3f pt, float size, u32 colour )
289 {
290 boxf box =
291 {
292 { pt[0]-size, pt[1]-size, pt[2]-size },
293 { pt[0]+size, pt[1]+size, pt[2]+size }
294 };
295
296 vg_line_boxf( box, colour );
297 }
298
299 #endif /* VG_LINES_H */