1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
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
22 static struct vg_shader _shader_lines
=
32 "layout (location=0) in vec3 a_co;"
33 "layout (location=1) in vec4 a_colour;"
39 " vec4 vert_pos = uPv * vec4( a_co, 1.0 );"
40 " s_colour = a_colour;"
41 " gl_Position = vert_pos;"
55 " FragColor = s_colour;"
77 VG_STATIC
void vg_lines_init(void)
79 vg_info( "vg_lines_init\n" );
81 vg_var_push( (struct vg_var
){
83 .data
= &vg_lines
.draw
,
84 .data_type
= k_var_dtype_i32
,
85 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
89 vg_shader_register( &_shader_lines
);
91 vg_acquire_thread_sync();
93 glGenVertexArrays( 1, &vg_lines
.vao
);
94 glGenBuffers( 1, &vg_lines
.vbo
);
95 glBindVertexArray( vg_lines
.vao
);
96 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
98 u32 size
= 50000 * sizeof( struct vg_lines_vert
);
100 vg_lines
.vertex_buffer
=
101 vg_create_linear_allocator(vg_mem
.rtmemory
, size
, VG_MEMORY_REALTIME
);
103 glBufferData( GL_ARRAY_BUFFER
, size
, NULL
, GL_DYNAMIC_DRAW
);
104 glBindVertexArray( vg_lines
.vao
);
108 glVertexAttribPointer(
113 sizeof( struct vg_lines_vert
),
116 glEnableVertexAttribArray( 0 );
118 glVertexAttribPointer(
123 sizeof( struct vg_lines_vert
),
124 (void*)(offsetof( struct vg_lines_vert
, colour
))
126 glEnableVertexAttribArray( 1 );
129 vg_success( "done\n" );
132 vg_release_thread_sync();
133 vg_lines
.allow_input
= 1;
136 VG_STATIC
void vg_lines_drawall( void )
138 glUseProgram( _shader_lines
.id
);
140 glUniformMatrix4fv( glGetUniformLocation( _shader_lines
.id
, "uPv" ),
141 1, GL_FALSE
, (float *)vg
.pv
);
143 glBindVertexArray( vg_lines
.vao
);
144 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
146 u32 bufusage
= vg_linear_get_cur(vg_lines
.vertex_buffer
);
148 glBufferSubData( GL_ARRAY_BUFFER
, 0, bufusage
, vg_lines
.vertex_buffer
);
150 glEnable( GL_BLEND
);
151 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
152 glBlendEquation( GL_FUNC_ADD
);
155 glDrawArrays( GL_LINES
, 0, bufusage
/ sizeof(struct vg_lines_vert
) );
157 glDisable( GL_BLEND
);
158 vg_linear_clear( vg_lines
.vertex_buffer
);
161 VG_STATIC
void vg_line2( line_co from
, line_co to
, u32 fc
, u32 tc
)
163 if( !vg_lines
.allow_input
)
166 u32 size
= 2 * sizeof(struct vg_lines_vert
);
167 struct vg_lines_vert
*v
= vg_linear_alloc( vg_lines
.vertex_buffer
, size
);
169 v3_copy( from
, v
[0].co
);
170 v3_copy( to
, v
[1].co
);
176 VG_STATIC
void vg_line( line_co from
, line_co to
, u32 colour
)
178 vg_line2( from
, to
, colour
, colour
);
181 VG_STATIC
void line_tangent_basis( v3f n
, v3f tx
, v3f ty
)
183 /* Compute tangent basis (box2d) */
184 if( fabsf( n
[0] ) >= 0.57735027f
)
198 v3_cross( n
, tx
, ty
);
201 VG_STATIC
void vg_line_arrow( line_co co
, line_co dir
, float size
, u32 colour
)
203 v3f p1
, tx
, ty
, p2
, p3
;
204 v3_muladds( co
, dir
, size
, p1
);
205 line_tangent_basis( dir
, tx
, ty
);
207 v3_muladds( p1
, dir
, -size
* 0.125f
, p2
);
208 v3_muladds( p2
, ty
, size
* 0.125f
, p3
);
209 v3_muladds( p2
, ty
, -size
* 0.125f
, p2
);
211 vg_line( co
, p1
, colour
);
212 vg_line( p1
, p2
, colour
);
213 vg_line( p1
, p3
, colour
);
216 VG_STATIC
void vg_line_boxf( boxf box
, u32 colour
)
218 v3f p000
, p001
, p010
, p011
, p100
, p101
, p110
, p111
;
220 p000
[0]=box
[0][0];p000
[1]=box
[0][1];p000
[2]=box
[0][2];
221 p001
[0]=box
[0][0];p001
[1]=box
[0][1];p001
[2]=box
[1][2];
222 p010
[0]=box
[0][0];p010
[1]=box
[1][1];p010
[2]=box
[0][2];
223 p011
[0]=box
[0][0];p011
[1]=box
[1][1];p011
[2]=box
[1][2];
225 p100
[0]=box
[1][0];p100
[1]=box
[0][1];p100
[2]=box
[0][2];
226 p101
[0]=box
[1][0];p101
[1]=box
[0][1];p101
[2]=box
[1][2];
227 p110
[0]=box
[1][0];p110
[1]=box
[1][1];p110
[2]=box
[0][2];
228 p111
[0]=box
[1][0];p111
[1]=box
[1][1];p111
[2]=box
[1][2];
230 vg_line( p000
, p001
, colour
);
231 vg_line( p001
, p011
, colour
);
232 vg_line( p011
, p010
, colour
);
233 vg_line( p010
, p000
, colour
);
235 vg_line( p100
, p101
, colour
);
236 vg_line( p101
, p111
, colour
);
237 vg_line( p111
, p110
, colour
);
238 vg_line( p110
, p100
, colour
);
240 vg_line( p100
, p000
, colour
);
241 vg_line( p101
, p001
, colour
);
242 vg_line( p110
, p010
, colour
);
243 vg_line( p111
, p011
, colour
);
246 VG_STATIC
void vg_line_boxf_transformed( m4x3f m
, boxf box
, u32 colour
)
248 v3f p000
, p001
, p010
, p011
, p100
, p101
, p110
, p111
;
250 p000
[0]=box
[0][0];p000
[1]=box
[0][1];p000
[2]=box
[0][2];
251 p001
[0]=box
[0][0];p001
[1]=box
[0][1];p001
[2]=box
[1][2];
252 p010
[0]=box
[0][0];p010
[1]=box
[1][1];p010
[2]=box
[0][2];
253 p011
[0]=box
[0][0];p011
[1]=box
[1][1];p011
[2]=box
[1][2];
255 p100
[0]=box
[1][0];p100
[1]=box
[0][1];p100
[2]=box
[0][2];
256 p101
[0]=box
[1][0];p101
[1]=box
[0][1];p101
[2]=box
[1][2];
257 p110
[0]=box
[1][0];p110
[1]=box
[1][1];p110
[2]=box
[0][2];
258 p111
[0]=box
[1][0];p111
[1]=box
[1][1];p111
[2]=box
[1][2];
260 m4x3_mulv( m
, p000
, p000
);
261 m4x3_mulv( m
, p001
, p001
);
262 m4x3_mulv( m
, p010
, p010
);
263 m4x3_mulv( m
, p011
, p011
);
264 m4x3_mulv( m
, p100
, p100
);
265 m4x3_mulv( m
, p101
, p101
);
266 m4x3_mulv( m
, p110
, p110
);
267 m4x3_mulv( m
, p111
, p111
);
269 vg_line( p000
, p001
, colour
);
270 vg_line( p001
, p011
, colour
);
271 vg_line( p011
, p010
, colour
);
272 vg_line( p010
, p000
, colour
);
274 vg_line( p100
, p101
, colour
);
275 vg_line( p101
, p111
, colour
);
276 vg_line( p111
, p110
, colour
);
277 vg_line( p110
, p100
, colour
);
279 vg_line( p100
, p000
, colour
);
280 vg_line( p101
, p001
, colour
);
281 vg_line( p110
, p010
, colour
);
282 vg_line( p111
, p011
, colour
);
284 vg_line( p000
, p110
, colour
);
285 vg_line( p100
, p010
, colour
);
288 VG_STATIC
void vg_line_cross(v3f pos
,u32 colour
, float scale
)
291 v3_add( (v3f
){ scale
,0.0f
,0.0f
}, pos
, p0
);
292 v3_add( (v3f
){-scale
,0.0f
,0.0f
}, pos
, p1
);
293 vg_line( p0
, p1
, colour
);
294 v3_add( (v3f
){0.0f
, scale
,0.0f
}, pos
, p0
);
295 v3_add( (v3f
){0.0f
,-scale
,0.0f
}, pos
, p1
);
296 vg_line( p0
, p1
, colour
);
297 v3_add( (v3f
){0.0f
,0.0f
, scale
}, pos
, p0
);
298 v3_add( (v3f
){0.0f
,0.0f
,-scale
}, pos
, p1
);
299 vg_line( p0
, p1
, colour
);
302 VG_STATIC
void vg_line_pt3( v3f pt
, float size
, u32 colour
)
306 { pt
[0]-size
, pt
[1]-size
, pt
[2]-size
},
307 { pt
[0]+size
, pt
[1]+size
, pt
[2]+size
}
310 vg_line_boxf( box
, colour
);
313 #endif /* VG_LINES_H */