1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
9 SHADER_DEFINE( vg_line_shader
,
12 "layout (location=0) in vec3 a_co;"
15 "layout (location=0) in vec2 a_co;"
17 "layout (location=1) in vec4 a_colour;"
24 " vec4 vert_pos = uPv * vec4( a_co, 1.0 );"
26 " vec4 vert_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
28 " s_colour = a_colour;"
29 " gl_Position = vert_pos;"
39 " FragColor = s_colour;"
60 u32 draw_idx
, cap
, buffer_size
;
64 static void vg_lines_init(void)
66 SHADER_INIT( vg_line_shader
);
68 glGenVertexArrays( 1, &vg_lines
.vao
);
69 glGenBuffers( 1, &vg_lines
.vbo
);
70 glBindVertexArray( vg_lines
.vao
);
72 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
75 vg_lines
.buffer_size
= vg_lines
.cap
* sizeof( struct vg_lines_vert
);
77 glBufferData( GL_ARRAY_BUFFER
, vg_lines
.buffer_size
, NULL
, GL_DYNAMIC_DRAW
);
78 glBindVertexArray( vg_lines
.vao
);
80 glVertexAttribPointer(
82 sizeof( vg_lines
.buffer
[0].co
) / sizeof(float),
85 sizeof( struct vg_lines_vert
),
88 glEnableVertexAttribArray( 0 );
90 glVertexAttribPointer(
95 sizeof( struct vg_lines_vert
),
96 (void*)(offsetof( struct vg_lines_vert
, colour
))
98 glEnableVertexAttribArray( 1 );
99 vg_lines
.buffer
= malloc( vg_lines
.buffer_size
);
102 static void vg_lines_free(void)
104 glDeleteVertexArrays( 1, &vg_lines
.vao
);
105 glDeleteBuffers( 1, &vg_lines
.vbo
);
107 free( vg_lines
.buffer
);
110 static void vg_lines_drawall( float* projection
)
112 SHADER_USE( vg_line_shader
);
119 ( SHADER_UNIFORM( vg_line_shader
, "uPv" ), 1, GL_FALSE
, projection
);
121 glBindVertexArray( vg_lines
.vao
);
122 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
124 glBufferSubData( GL_ARRAY_BUFFER
, 0, vg_lines
.draw_idx
*
125 sizeof(struct vg_lines_vert
), vg_lines
.buffer
);
127 glEnable( GL_BLEND
);
128 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
129 glBlendEquation( GL_FUNC_ADD
);
130 glDrawArrays( GL_LINES
, 0, vg_lines
.draw_idx
);
132 glDisable( GL_BLEND
);
133 vg_lines
.draw_idx
= 0;
136 static void vg_line2( line_co from
, line_co to
, u32 fc
, u32 tc
)
138 struct vg_lines_vert
*v
= &vg_lines
.buffer
[vg_lines
.draw_idx
];
141 v3_copy( from
, v
[0].co
);
142 v3_copy( to
, v
[1].co
);
144 v2_copy( from
, v
[0].co
);
145 v2_copy( to
, v
[1].co
);
151 vg_lines
.draw_idx
+= 2;
154 static void vg_line( line_co from
, line_co to
, u32 colour
)
156 vg_line2( from
, to
, colour
, colour
);
159 static void vg_line_box( line_co min
, line_co max
, u32 colour
)
164 vg_line( min
, (v2f
){min
[0],max
[1]}, colour
);
165 vg_line( (v2f
){min
[0],max
[1]}, max
, colour
);
166 vg_line( max
, (v2f
){max
[0],min
[1]}, colour
);
167 vg_line( (v2f
){max
[0],min
[1]}, min
, colour
);