81b009150b4791518087ad5cb130965229d6dc8f
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 SHADER_DEFINE( vg_line_shader
,
6 "layout (location=0) in vec3 a_co;"
7 "layout (location=1) in vec4 a_colour;"
14 " vec4 vert_pos = uPv * vec4( a_co, 1.0 );"
15 " s_colour = a_colour;"
16 " gl_Position = vert_pos;"
26 " FragColor = s_colour;"
42 u32 draw_idx
, cap
, buffer_size
;
47 static void vg_lines_init(void)
49 SHADER_INIT( vg_line_shader
);
51 glGenVertexArrays( 1, &vg_lines
.vao
);
52 glGenBuffers( 1, &vg_lines
.vbo
);
53 glBindVertexArray( vg_lines
.vao
);
55 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
58 vg_lines
.buffer_size
= vg_lines
.cap
* sizeof( struct vg_lines_vert
);
60 glBufferData( GL_ARRAY_BUFFER
, vg_lines
.buffer_size
, NULL
, GL_DYNAMIC_DRAW
);
61 glBindVertexArray( vg_lines
.vao
);
63 glVertexAttribPointer(
65 sizeof( vg_lines
.buffer
[0].co
) / sizeof(float),
68 sizeof( struct vg_lines_vert
),
71 glEnableVertexAttribArray( 0 );
73 glVertexAttribPointer(
78 sizeof( struct vg_lines_vert
),
79 (void*)(offsetof( struct vg_lines_vert
, colour
))
81 glEnableVertexAttribArray( 1 );
82 vg_lines
.buffer
= malloc( vg_lines
.buffer_size
);
85 static void vg_lines_free(void)
87 glDeleteVertexArrays( 1, &vg_lines
.vao
);
88 glDeleteBuffers( 1, &vg_lines
.vbo
);
90 free( vg_lines
.buffer
);
93 static void vg_lines_drawall(void)
95 SHADER_USE( vg_line_shader
);
96 glUniformMatrix4fv( SHADER_UNIFORM( vg_line_shader
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
98 glBindVertexArray( vg_lines
.vao
);
99 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
101 glBufferSubData( GL_ARRAY_BUFFER
, 0, vg_lines
.draw_idx
* sizeof(struct vg_lines_vert
), vg_lines
.buffer
);
102 glEnable( GL_BLEND
);
103 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
104 glBlendEquation( GL_FUNC_ADD
);
106 glDrawArrays( GL_LINES
, 0, vg_lines
.draw_idx
);
108 glDisable( GL_BLEND
);
109 vg_lines
.draw_idx
= 0;
112 static void vg_line2( vec3 from
, vec3 to
, u32 fc
, u32 tc
)
114 struct vg_lines_vert
*v
= vg_lines
.buffer
+ vg_lines
.draw_idx
;
115 glm_vec3_copy( from
, v
[0].co
);
116 glm_vec3_copy( to
, v
[1].co
);
120 vg_lines
.draw_idx
+= 2;
123 static void vg_line( vec3 from
, vec3 to
, u32 colour
)
125 vg_line2( from
, to
, colour
, colour
);