# primitive_drawer.h ## Index
`enum tm_primitive_drawer_type`
`struct tm_primitive_drawer_buffer_t`
`enum tm_primitive_drawer_result`
`enum tm_primitive_drawer_depth_test`

`struct tm_primitive_drawer_api`
`stroke_lines()`
`stroke_polyline()`
`stroke_triangles()`
`stroke_rect()`
`stroke_ellipse()`
`stroke_cuboid()`
`stroke_frustum()`
`stroke_axis()`
`stroke_sphere()`
`stroke_cone()`
`stroke_octasphere()`
`stroke_capsule_profile()`
`fill_triangles()`
`fill_plane()`
`reserve()`
`grow()`

`tm_primitive_drawer_api_version`
`tm_primitive_renderer_o`

`struct tm_primitive_renderer_api`
`create()`
`destroy()`
`render()`

`tm_primitive_renderer_api_version`
## API
### `enum tm_primitive_drawer_type`
~~~c enum tm_primitive_drawer_type { // Lines, each line is represented by a start and end position. TM_PRIMITIVE_DRAWER_LINES = 0, // Sphere primitive. TM_PRIMITIVE_DRAWER_SPHERE, // Cone primitive. TM_PRIMITIVE_DRAWER_CONE, // Triangles TM_PRIMITIVE_DRAWER_TRIANGLES, // Filled triangles TM_PRIMITIVE_DRAWER_FILLED_TRIANGLES }; ~~~
### `struct tm_primitive_drawer_buffer_t`
Primitive and vertex buffer to draw into. ~~~c typedef struct tm_primitive_drawer_buffer_t { uint8_t *buffer; // Number of used bytes. uint32_t bytes; // Number of allocated bytes. uint32_t bytes_allocated; } tm_primitive_drawer_buffer_t; ~~~
### `enum tm_primitive_drawer_result`
Return values from the primitive drawer functions. The caller can use this to resize the buffers appropriately to have room for the drawn primitives. ~~~c typedef enum tm_primitive_drawer_result { TM_PRIMITIVE_DRAWER__OK, TM_PRIMITIVE_DRAWER__PBUFFER_FULL = 0x1, TM_PRIMITIVE_DRAWER__VBUFFER_FULL = 0x2, } tm_primitive_drawer_result; ~~~
### `enum tm_primitive_drawer_depth_test`
~~~c typedef enum tm_primitive_drawer_depth_test { // Renders with depth testing disabled. TM_PRIMITIVE_DRAWER_DEPTH_TEST_DISABLED = 0, // Renders with depth testing enabled. TM_PRIMITIVE_DRAWER_DEPTH_TEST_ENABLED = 1, // Renders in two passes, pixels passing the depth test will render with an alpha scale of 1.f, // pixels failing the depth test will render with an alpha scale of 0.25. TM_PRIMITIVE_DRAWER_DEPTH_TEST_DIMMED = 2, TM_PRIMITIVE_DRAWER_DEPTH_TEST_MAX_DEPTH_TEST } tm_primitive_drawer_depth_test; ~~~
### `struct tm_primitive_drawer_api`
#### `stroke_lines()` ~~~c tm_primitive_drawer_result (*stroke_lines)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, const tm_vec3_t *vertices, uint32_t num_lines, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes pairs of lines from the `vertices` buffers. `vertices` should contain `2 * num_lines` vertices. #### `stroke_polyline()` ~~~c tm_primitive_drawer_result (*stroke_polyline)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, const tm_vec3_t *vertices, uint32_t num_vertices, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a single connected line from the `vertices`. `num_vertices - 1` line segments will be drawn. #### `stroke_triangles()` ~~~c tm_primitive_drawer_result (*stroke_triangles)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, const tm_vec3_t *vertices, uint32_t num_vertices, const uint32_t *indices, uint32_t num_indices, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes the triangles from the buffers. #### `stroke_rect()` ~~~c tm_primitive_drawer_result (*stroke_rect)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, float width, float height, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a rectangle (origin is center). #### `stroke_ellipse()` ~~~c tm_primitive_drawer_result (*stroke_ellipse)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, float width, float height, uint32_t divisions, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a ellipse #### `stroke_cuboid()` ~~~c tm_primitive_drawer_result (*stroke_cuboid)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, tm_vec3_t lower_bound, tm_vec3_t upper_bound, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a cuboid. #### `stroke_frustum()` ~~~c tm_primitive_drawer_result (*stroke_frustum)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, float left_plane, float right_plane, float bottom_plane, float top_plane, float near_plane, float far_plane, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a frustum. #### `stroke_axis()` ~~~c tm_primitive_drawer_result (*stroke_axis)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, float line_length, float line_width, tm_color_srgb_t custom_colors[3], tm_primitive_drawer_depth_test depth_test); ~~~ Strokes an axis. If `custom_colors` is `NULL`, hard coded default colors will be used instead. #### `stroke_sphere()` ~~~c tm_primitive_drawer_result (*stroke_sphere)(tm_primitive_drawer_buffer_t *pbuffer, const tm_mat44_t *tm, float radius, uint32_t segments, uint32_t parts, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a sphere. #### `stroke_cone()` ~~~c tm_primitive_drawer_result (*stroke_cone)(tm_primitive_drawer_buffer_t *pbuffer, const tm_mat44_t *tm, float length, float radius, uint32_t segments, uint32_t bars, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a cone. The center of the cone base is placed at `tm` and the cone points in the `z` direction of the `tm` matrix. #### `stroke_octasphere()` ~~~c tm_primitive_drawer_result (*stroke_octasphere)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, float r, tm_vec3_t d, uint32_t n, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes an octasphere. The center of the octasphere is at `tm`. `r` specifies the sphere radius and `d` the separation between the sphere quadrants along each axis. `n` is the number of subdivisions of the octahedron edge. Reference: https://prideout.net/blog/octasphere/ #### `stroke_capsule_profile()` ~~~c tm_primitive_drawer_result (*stroke_capsule_profile)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, float radius, float half_height, uint32_t n, tm_color_srgb_t color, float line_width, tm_primitive_drawer_depth_test depth_test); ~~~ Strokes a capsule profile #### `fill_triangles()` ~~~c tm_primitive_drawer_result (*fill_triangles)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, const tm_vec3_t *vertices, uint32_t num_vertices, const uint32_t *indices, uint32_t num_indices, tm_color_srgb_t color, tm_primitive_drawer_depth_test depth_test); ~~~ Fills triangles. #### `fill_plane()` ~~~c tm_primitive_drawer_result (*fill_plane)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, const tm_mat44_t *tm, tm_vec3_t lower_bound, tm_vec3_t upper_bound, tm_color_srgb_t color, tm_primitive_drawer_depth_test depth_test); ~~~ Fills a plane. #### `reserve()` ~~~c void (*reserve)(tm_primitive_drawer_buffer_t *pbuffer, uint32_t p_bytes, tm_primitive_drawer_buffer_t *vbuffer, uint32_t v_bytes, struct tm_allocator_i *allocator); ~~~ Makes sure buffers have room for at least the specified number of bytes. Buffers grow geometrically. #### `grow()` ~~~c void (*grow)(tm_primitive_drawer_buffer_t *pbuffer, tm_primitive_drawer_buffer_t *vbuffer, tm_primitive_drawer_result res, struct tm_allocator_i *allocator); ~~~ Grows buffers based on the value of `res`. If `res` is `TM_PRIMITIVE_DRAWER__PBUFFER_FULL`, the pbuffer is grown, if res is `TM_PRIMITIVE_DRAWER__VBUFFER_FULL`, the vbuffer is grown.
### `tm_primitive_drawer_api_version`
~~~c #define tm_primitive_drawer_api_version ~~~
### `tm_primitive_renderer_o`
~~~c typedef struct tm_primitive_renderer_o tm_primitive_renderer_o; ~~~
### `struct tm_primitive_renderer_api`
#### `create()` ~~~c struct tm_primitive_renderer_o *(*create)(struct tm_allocator_i *allocator, struct tm_renderer_resource_command_buffer_o *res_buf); ~~~ #### `destroy()` ~~~c void (*destroy)(struct tm_primitive_renderer_o *inst, struct tm_renderer_resource_command_buffer_o *res_buf); ~~~ #### `render()` ~~~c void (*render)(struct tm_primitive_renderer_o *inst, const tm_primitive_drawer_buffer_t *pbuffer, const tm_primitive_drawer_buffer_t *vbuffer, struct tm_shader_repository_o *shader_repository, tm_strhash_t shader_name, const struct tm_shader_system_context_o *shader_context, uint64_t sort_key, struct tm_renderer_resource_command_buffer_o *res_buf, struct tm_renderer_command_buffer_o *cmd_buf); ~~~
### `tm_primitive_renderer_api_version`
~~~c #define tm_primitive_renderer_api_version ~~~