Skip to content

Commit

Permalink
Optional vertex modulation in draw API
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyGaul committed Jun 30, 2024
1 parent 37df19e commit c0b26d5
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 45 deletions.
3 changes: 3 additions & 0 deletions docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ This is a list of all functions in Cute Framework organized by categories. This
- [cf_draw_quad2](/draw/cf_draw_quad2.md)
- [cf_draw_quad_fill](/draw/cf_draw_quad_fill.md)
- [cf_draw_quad_fill2](/draw/cf_draw_quad_fill2.md)
- [cf_draw_set_vertex_callback](/draw/cf_draw_set_vertex_callback.md)
- [cf_draw_sprite](/draw/cf_draw_sprite.md)
- [cf_draw_tri](/draw/cf_draw_tri.md)
- [cf_draw_tri_fill](/draw/cf_draw_tri_fill.md)
Expand All @@ -373,10 +374,12 @@ This is a list of all functions in Cute Framework organized by categories. This
- [cf_render_settings_push_viewport](/draw/cf_render_settings_push_viewport.md)
- [cf_render_settings_set_atlas_dimensions](/draw/cf_render_settings_set_atlas_dimensions.md)
- [cf_render_to](/draw/cf_render_to.md)
- [CF_VertexFn](/draw/cf_vertexfn.md)


### structs
- [CF_TemporaryImage](/draw/cf_temporaryimage.md)
- [CF_Vertex](/draw/cf_vertex.md)


## graphics
Expand Down
34 changes: 34 additions & 0 deletions docs/draw/cf_draw_set_vertex_callback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[](../header.md ':include')

# cf_draw_set_vertex_callback

Category: [draw](/api_reference?id=draw)
GitHub: [cute_draw.h](https://github.com/RandyGaul/cute_framework/blob/master/include/cute_draw.h)
---

An optional callback for modifying vertices before they are sent to the GPU.

```cpp
void cf_draw_set_vertex_callback(CF_VertexFn* vertex_fn);
```
## Remarks
Setup this callback to apply per-vertex modulations for implementing advanced graphical effects.
`Count` is always a multiple of three, as this function always processes large batched arrays of
triangles. Since all shapes are rendered with signed-distance functions, most shapes merely generate
a single quad, so you may find triangle counts lower than originally anticipated.
Call [cf_draw_set_vertex_callback](/draw/cf_draw_set_vertex_callback.md) to setup your callback.
There is no adjecancy info provided. If you need to know which triangles connect to others you
should probably redesign your feature to not require adjecancy information, or use your own custom
rendering solution. With a custom solution you may use low-level graphics in cute_graphics.h, where
any adjacency info can be controlled 100% by you a-priori.
## Related Pages
[CF_Vertex](/draw/cf_vertex.md)
[CF_VertexFn](/draw/cf_vertexfn.md)
cf_draw_push_vertex_callback
cf_draw_pop_vertex_callback
38 changes: 38 additions & 0 deletions docs/draw/cf_vertex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[](../header.md ':include')

# CF_Vertex

Category: [draw](/api_reference?id=draw)
GitHub: [cute_draw.h](https://github.com/RandyGaul/cute_framework/blob/master/include/cute_draw.h)
---

The full vertex layout CF uses just before sending verts to the GPU.

Struct Members | Description
--- | ---
`CF_V2 p` | World space position.
`CF_V2 posH` | "Homogenous" position transformed by the camera.
`CF_V2 a, b, c` | For internal use -- used in signed-distance functions for rendering shapes.
`CF_V2 uv` | For internal use -- used for sprite rendering.
`CF_Pixel color` | Color for rendering shapes (ignored for sprites).
`float radius` | For internal use -- Used for applying "chubbiness" factor for shapes, or radii on circle/capsule.
`float stroke` | For internal use -- Used for shape rendering for border style stroke rendering (no fill).
`float aa` | For internal use -- Factor for the size of antialiasing.
`uint8_t type` | For internal use -- The type of shape to be rendered, used by the signed-distance functions within CF's internal fragment shader.
`uint8_t alpha` | Used for the alpha-component (transparency).
`uint8_t fill` | For internal use -- Whether or not to render shapes as filled or strokedx.
`uint8_t not_used` | For internal use -- Reserved for a future purpose, simply fulfills byte alignment for now.
`CF_Color attributes` | Four general purpose floats passed into custom user shaders.

## Remarks

You may fill in vertices via callback by `cf_draw_push_vertex_callback`. See [CF_VertexFn](/draw/cf_vertexfn.md).
This is useful when you need to fill in unique `attributes` per-vertex, or modify any other
bits of the vertex before rendering. This could be used to implement features like dynamically
generated UV's for shape slicing, or complex lighting systems.

## Related Pages

cf_draw_pop_vertex_callback
[CF_VertexFn](/draw/cf_vertexfn.md)
cf_draw_push_vertex_callback
33 changes: 33 additions & 0 deletions docs/draw/cf_vertexfn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[](../header.md ':include')

# CF_VertexFn

Category: [draw](/api_reference?id=draw)
GitHub: [cute_draw.h](https://github.com/RandyGaul/cute_framework/blob/master/include/cute_draw.h)
---

An optional callback for modifying vertices before they are sent to the GPU.

```cpp
typedef void (CF_VertexFn)(CF_Vertex* verts, int count);
```
## Remarks
Setup this callback to apply per-vertex modulations for implementing advanced graphical effects.
`Count` is always a multiple of three, as this function always processes large batched arrays of
triangles. Since all shapes are rendered with signed-distance functions, most shapes merely generate
a single quad, so you may find triangle counts lower than originally anticipated.
Call [cf_draw_set_vertex_callback](/draw/cf_draw_set_vertex_callback.md) to setup your callback.
There is no adjecancy info provided. If you need to know which triangles connect to others you
should probably redesign your feature to not require adjecancy information, or use your own custom
rendering solution. With a custom solution you may use low-level graphics in cute_graphics.h, where
any adjacency info can be controlled 100% by you a-priori.
## Related Pages
[CF_Vertex](/draw/cf_vertex.md)
cf_draw_pop_vertex_callback
cf_draw_push_vertex_callback
91 changes: 91 additions & 0 deletions include/cute_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,97 @@ CF_API CF_Color CF_CALL cf_draw_pop_vertex_attributes();
*/
CF_API CF_Color CF_CALL cf_draw_peek_vertex_attributes();

/**
* @struct CF_Vertex
* @category draw
* @brief The full vertex layout CF uses just before sending verts to the GPU.
* @remarks You may fill in vertices via callback by `cf_draw_push_vertex_callback`. See `CF_VertexFn`.
* This is useful when you need to fill in unique `attributes` per-vertex, or modify any other
* bits of the vertex before rendering. This could be used to implement features like dynamically
* generated UV's for shape slicing, or complex lighting systems.
* @related CF_Vertex CF_VertexFn cf_draw_push_vertex_callback cf_draw_pop_vertex_callback
*/
typedef struct CF_Vertex
{
/* @member World space position. */
CF_V2 p;

/* @member "Homogenous" position transformed by the camera. */
CF_V2 posH;

/* @member For internal use -- used in signed-distance functions for rendering shapes. */
CF_V2 a, b, c;

/* @member For internal use -- used for sprite rendering. */
CF_V2 uv;

/* @member Color for rendering shapes (ignored for sprites). */
CF_Pixel color;

/* @member For internal use -- Used for applying "chubbiness" factor for shapes, or radii on circle/capsule. */
float radius;

/* @member For internal use -- Used for shape rendering for border style stroke rendering (no fill). */
float stroke;

/* @member For internal use -- Factor for the size of antialiasing. */
float aa;

/* @member For internal use -- The type of shape to be rendered, used by the signed-distance functions within CF's internal fragment shader. */
uint8_t type;

/* @member Used for the alpha-component (transparency). */
uint8_t alpha;

/* @member For internal use -- Whether or not to render shapes as filled or strokedx. */
uint8_t fill;

/* @member For internal use -- Reserved for a future purpose, simply fulfills byte alignment for now. */
uint8_t not_used;

/* @member Four general purpose floats passed into custom user shaders. */
CF_Color attributes;
} CF_Vertex;
// @end

/**
* @function CF_VertexFn
* @category draw
* @brief An optional callback for modifying vertices before they are sent to the GPU.
* @remarks Setup this callback to apply per-vertex modulations for implementing advanced graphical effects.
* `Count` is always a multiple of three, as this function always processes large batched arrays of
* triangles. Since all shapes are rendered with signed-distance functions, most shapes merely generate
* a single quad, so you may find triangle counts lower than originally anticipated.
*
* Call `cf_draw_set_vertex_callback` to setup your callback.
*
* There is no adjecancy info provided. If you need to know which triangles connect to others you
* should probably redesign your feature to not require adjecancy information, or use your own custom
* rendering solution. With a custom solution you may use low-level graphics in cute_graphics.h, where
* any adjacency info can be controlled 100% by you a-priori.
* @related CF_Vertex CF_VertexFn cf_draw_push_vertex_callback cf_draw_pop_vertex_callback
*/
typedef void (CF_VertexFn)(CF_Vertex* verts, int count);

/**
* @function cf_draw_set_vertex_callback
* @category draw
* @brief An optional callback for modifying vertices before they are sent to the GPU.
* @remarks Setup this callback to apply per-vertex modulations for implementing advanced graphical effects.
* `Count` is always a multiple of three, as this function always processes large batched arrays of
* triangles. Since all shapes are rendered with signed-distance functions, most shapes merely generate
* a single quad, so you may find triangle counts lower than originally anticipated.
*
* Call `cf_draw_set_vertex_callback` to setup your callback.
*
* There is no adjecancy info provided. If you need to know which triangles connect to others you
* should probably redesign your feature to not require adjecancy information, or use your own custom
* rendering solution. With a custom solution you may use low-level graphics in cute_graphics.h, where
* any adjacency info can be controlled 100% by you a-priori.
* @related CF_Vertex CF_VertexFn cf_draw_push_vertex_callback cf_draw_pop_vertex_callback
*/
CF_API void CF_CALL cf_draw_set_vertex_callback(CF_VertexFn* vertex_fn);

/**
* @function cf_make_font
* @category text
Expand Down
Loading

0 comments on commit c0b26d5

Please sign in to comment.