thin font
[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 {
24 .name = "[vg] lines",
25 .link = NULL,
26 .vs =
27 {
28 .orig_file = NULL,
29 .static_src =
30
31 "uniform mat4 uPv;"
32 "layout (location=0) in vec3 a_co;"
33 "layout (location=1) in vec4 a_colour;"
34 ""
35 "out vec4 s_colour;"
36 ""
37 "void main()"
38 "{"
39 " vec4 vert_pos = uPv * vec4( a_co, 1.0 );"
40 " s_colour = a_colour;"
41 " gl_Position = vert_pos;"
42 "}"
43 },
44 .fs =
45 {
46 .orig_file = NULL,
47 .static_src =
48
49 "out vec4 FragColor;"
50 ""
51 "in vec4 s_colour;"
52 ""
53 "void main()"
54 "{"
55 " FragColor = s_colour;"
56 "}"
57 }
58 };
59
60
61 struct
62 {
63 u32 draw,
64 allow_input;
65
66 struct vg_lines_vert
67 {
68 v3f co;
69 u32 colour;
70 }
71 *vertex_buffer;
72
73 GLuint vao, vbo;
74 }
75 static vg_lines;
76
77 VG_STATIC void vg_lines_init(void)
78 {
79 vg_info( "vg_lines_init\n" );
80
81 vg_var_push( (struct vg_var){
82 .name = "vg_lines",
83 .data = &vg_lines.draw,
84 .data_type = k_var_dtype_i32,
85 .opt_i32 = { .min=0, .max=1, .clamp=1 },
86 .persistent = 1
87 });
88
89 vg_shader_register( &_shader_lines );
90
91 vg_acquire_thread_sync();
92 {
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 );
97
98 u32 size = 50000 * sizeof( struct vg_lines_vert );
99
100 vg_lines.vertex_buffer =
101 vg_create_linear_allocator(vg_mem.rtmemory, size, VG_MEMORY_REALTIME);
102
103 glBufferData( GL_ARRAY_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
104 glBindVertexArray( vg_lines.vao );
105 VG_CHECK_GL_ERR();
106
107 /* Pointers */
108 glVertexAttribPointer(
109 0,
110 3,
111 GL_FLOAT,
112 GL_FALSE,
113 sizeof( struct vg_lines_vert ),
114 (void *)0
115 );
116 glEnableVertexAttribArray( 0 );
117
118 glVertexAttribPointer(
119 1,
120 4,
121 GL_UNSIGNED_BYTE,
122 GL_TRUE,
123 sizeof( struct vg_lines_vert ),
124 (void*)(offsetof( struct vg_lines_vert, colour ))
125 );
126 glEnableVertexAttribArray( 1 );
127
128 VG_CHECK_GL_ERR();
129 vg_success( "done\n" );
130 }
131
132 vg_release_thread_sync();
133 vg_lines.allow_input = 1;
134 }
135
136 VG_STATIC void vg_lines_drawall( float* projection )
137 {
138 glUseProgram( _shader_lines.id );
139
140 glUniformMatrix4fv
141 ( glGetUniformLocation( _shader_lines.id, "uPv" ), 1, GL_FALSE, projection );
142
143 glBindVertexArray( vg_lines.vao );
144 glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
145
146 u32 bufusage = vg_linear_get_cur(vg_lines.vertex_buffer);
147
148 glBufferSubData( GL_ARRAY_BUFFER, 0, bufusage, vg_lines.vertex_buffer );
149
150 glEnable( GL_BLEND );
151 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
152 glBlendEquation( GL_FUNC_ADD );
153
154 if( vg_lines.draw )
155 glDrawArrays( GL_LINES, 0, bufusage / sizeof(struct vg_lines_vert) );
156
157 glDisable( GL_BLEND );
158 vg_linear_clear( vg_lines.vertex_buffer );
159 }
160
161 VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc )
162 {
163 if( !vg_lines.allow_input )
164 return;
165
166 u32 size = 2 * sizeof(struct vg_lines_vert);
167 struct vg_lines_vert *v = vg_linear_alloc( vg_lines.vertex_buffer, size );
168
169 v3_copy( from, v[0].co );
170 v3_copy( to, v[1].co );
171
172 v[0].colour = fc;
173 v[1].colour = tc;
174 }
175
176 VG_STATIC void vg_line( line_co from, line_co to, u32 colour )
177 {
178 vg_line2( from, to, colour, colour );
179 }
180
181 VG_STATIC void vg_line_boxf( boxf box, u32 colour )
182 {
183 v3f p000, p001, p010, p011, p100, p101, p110, p111;
184
185 p000[0]=box[0][0];p000[1]=box[0][1];p000[2]=box[0][2];
186 p001[0]=box[0][0];p001[1]=box[0][1];p001[2]=box[1][2];
187 p010[0]=box[0][0];p010[1]=box[1][1];p010[2]=box[0][2];
188 p011[0]=box[0][0];p011[1]=box[1][1];p011[2]=box[1][2];
189
190 p100[0]=box[1][0];p100[1]=box[0][1];p100[2]=box[0][2];
191 p101[0]=box[1][0];p101[1]=box[0][1];p101[2]=box[1][2];
192 p110[0]=box[1][0];p110[1]=box[1][1];p110[2]=box[0][2];
193 p111[0]=box[1][0];p111[1]=box[1][1];p111[2]=box[1][2];
194
195 vg_line( p000, p001, colour );
196 vg_line( p001, p011, colour );
197 vg_line( p011, p010, colour );
198 vg_line( p010, p000, colour );
199
200 vg_line( p100, p101, colour );
201 vg_line( p101, p111, colour );
202 vg_line( p111, p110, colour );
203 vg_line( p110, p100, colour );
204
205 vg_line( p100, p000, colour );
206 vg_line( p101, p001, colour );
207 vg_line( p110, p010, colour );
208 vg_line( p111, p011, colour );
209 }
210
211 VG_STATIC void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour )
212 {
213 v3f p000, p001, p010, p011, p100, p101, p110, p111;
214
215 p000[0]=box[0][0];p000[1]=box[0][1];p000[2]=box[0][2];
216 p001[0]=box[0][0];p001[1]=box[0][1];p001[2]=box[1][2];
217 p010[0]=box[0][0];p010[1]=box[1][1];p010[2]=box[0][2];
218 p011[0]=box[0][0];p011[1]=box[1][1];p011[2]=box[1][2];
219
220 p100[0]=box[1][0];p100[1]=box[0][1];p100[2]=box[0][2];
221 p101[0]=box[1][0];p101[1]=box[0][1];p101[2]=box[1][2];
222 p110[0]=box[1][0];p110[1]=box[1][1];p110[2]=box[0][2];
223 p111[0]=box[1][0];p111[1]=box[1][1];p111[2]=box[1][2];
224
225 m4x3_mulv( m, p000, p000 );
226 m4x3_mulv( m, p001, p001 );
227 m4x3_mulv( m, p010, p010 );
228 m4x3_mulv( m, p011, p011 );
229 m4x3_mulv( m, p100, p100 );
230 m4x3_mulv( m, p101, p101 );
231 m4x3_mulv( m, p110, p110 );
232 m4x3_mulv( m, p111, p111 );
233
234 vg_line( p000, p001, colour );
235 vg_line( p001, p011, colour );
236 vg_line( p011, p010, colour );
237 vg_line( p010, p000, colour );
238
239 vg_line( p100, p101, colour );
240 vg_line( p101, p111, colour );
241 vg_line( p111, p110, colour );
242 vg_line( p110, p100, colour );
243
244 vg_line( p100, p000, colour );
245 vg_line( p101, p001, colour );
246 vg_line( p110, p010, colour );
247 vg_line( p111, p011, colour );
248
249 vg_line( p000, p110, colour );
250 vg_line( p100, p010, colour );
251 }
252
253 VG_STATIC void vg_line_cross(v3f pos,u32 colour, float scale)
254 {
255 v3f p0, p1;
256 v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 );
257 v3_add( (v3f){-scale,0.0f,0.0f}, pos, p1 );
258 vg_line( p0, p1, colour );
259 v3_add( (v3f){0.0f, scale,0.0f}, pos, p0 );
260 v3_add( (v3f){0.0f,-scale,0.0f}, pos, p1 );
261 vg_line( p0, p1, colour );
262 v3_add( (v3f){0.0f,0.0f, scale}, pos, p0 );
263 v3_add( (v3f){0.0f,0.0f,-scale}, pos, p1 );
264 vg_line( p0, p1, colour );
265 }
266
267 VG_STATIC void vg_line_pt3( v3f pt, float size, u32 colour )
268 {
269 boxf box =
270 {
271 { pt[0]-size, pt[1]-size, pt[2]-size },
272 { pt[0]+size, pt[1]+size, pt[2]+size }
273 };
274
275 vg_line_boxf( box, colour );
276 }
277
278 #endif /* VG_LINES_H */