|
Tiny3D
|


Go to the source code of this file.
Data Structures | |
| struct | __attribute__ |
| struct | T3DViewport |
| struct | T3DInitParams |
Macros | |
| #define | T3D_VERTEX_CACHE_SIZE 70 |
Functions | |
| void | t3d_init (T3DInitParams params) |
| Initializes the tiny3d library. | |
| void | t3d_destroy (void) |
| Destroys the tiny3d library. | |
| void | t3d_frame_start (void) |
| Starts a new frame, this will setup some default states. | |
| void | t3d_screen_clear_color (color_t color) |
| Clears the entire screen with a given color. | |
| void | t3d_screen_clear_depth () |
| Clears the entire depth buffer with a fixed value (0xFFFC) | |
| static T3DViewport | t3d_viewport_create () |
| static T3DViewport | t3d_viewport_create_buffered (uint16_t count) |
| static void | t3d_viewport_destroy (T3DViewport *viewport) |
| void | t3d_viewport_attach (T3DViewport *viewport) |
| T3DViewport * | t3d_viewport_get () |
| static void | t3d_viewport_set_area (T3DViewport *viewport, int32_t x, int32_t y, int32_t width, int32_t height) |
| void | t3d_viewport_set_perspective (T3DViewport *viewport, float fov, float aspectRatio, float near, float far) |
| void | t3d_viewport_set_projection (T3DViewport *viewport, float fov, float near, float far) |
| void | t3d_viewport_set_ortho (T3DViewport *viewport, float left, float right, float bottom, float top, float near, float far) |
| static void | t3d_viewport_set_w_normalize (T3DViewport *viewport, float near, float far) |
| void | t3d_viewport_look_at (T3DViewport *viewport, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up) |
| void | t3d_viewport_set_view_matrix (T3DViewport *viewport, const T3DMat4 *mat) |
| void | t3d_viewport_set_projection_matrix (T3DViewport *viewport, const T3DMat4 *mat) |
| void | t3d_viewport_calc_viewspace_pos (T3DViewport *viewport, T3DVec3 *out, const T3DVec3 *pos) |
| void | t3d_tri_draw (uint32_t v0, uint32_t v1, uint32_t v2) |
| Draws a single triangle, referencing loaded vertices. | |
| void | t3d_tri_draw_unindexed (uint32_t baseIndex, uint32_t triCount) |
| void | t3d_quad_draw_unindexed (uint32_t baseIndex, uint32_t quadCount) |
| void | t3d_tri_draw_strip (int16_t *indexBuff, int count) |
| void | t3d_tri_draw_strip_and_sync (int16_t *indexBuff, int count) |
| static void | t3d_tri_sync () |
| void | t3d_matrix_set (const T3DMat4FP *mat, bool doMultiply) |
| void | t3d_matrix_push (const T3DMat4FP *mat) |
| void | t3d_matrix_pop (int count) |
| void | t3d_matrix_push_pos (int count) |
| void | t3d_matrix_set_proj (const T3DMat4FP *mat) |
| void | t3d_vert_load (const T3DVertPacked *vertices, uint32_t offset, uint32_t count) |
| void | t3d_light_set_ambient (const uint8_t *color) |
| void | t3d_light_set_directional (int index, const uint8_t *color, const T3DVec3 *dir) |
| void | t3d_light_set_point (int index, const uint8_t *color, const T3DVec3 *pos, float size, bool ignoreNormals) |
| void | t3d_light_set_count (int count) |
| void | t3d_light_set_exposure (float exposure) |
| void | t3d_fog_set_range (float near, float far) |
| static void | t3d_fog_set_enabled (bool isEnabled) |
| uint16_t | t3d_vert_pack_normal (const T3DVec3 *normal) |
| void | t3d_state_set_drawflags (enum T3DDrawFlags drawFlags) |
| void | t3d_state_set_depth_offset (int16_t offset) |
| void | t3d_state_set_alpha_to_tile (bool enable) |
| void | t3d_state_set_vertex_fx (enum T3DVertexFX func, int16_t arg0, int16_t arg1) |
| void | t3d_state_set_vertex_fx_scale (uint16_t scale) |
| void | t3d_segment_set (uint8_t segmentId, void *address) |
| static void * | t3d_segment_placeholder (uint8_t segmentId) |
| static void * | t3d_segment_address (uint8_t segmentId, void *ptr) |
| void | t3d_indexbuffer_convert (int16_t indices[], int count) |
| static int16_t * | t3d_vertbuffer_get_pos (T3DVertPacked vert[], int idx) |
| static int16_t * | t3d_vertbuffer_get_uv (T3DVertPacked vert[], int idx) |
| static uint32_t * | t3d_vertbuffer_get_color (T3DVertPacked vert[], int idx) |
| static uint8_t * | t3d_vertbuffer_get_rgba (T3DVertPacked vert[], int idx) |
| static uint16_t * | t3d_vertbuffer_get_norm (T3DVertPacked vert[], int idx) |
Variables | |
| uint32_t | T3D_RSP_ID |
|
inlinestatic |
Enables or disables fog, this can be set independently from the range.
| isEnabled |
| void t3d_fog_set_range | ( | float | near, |
| float | far ) |
Sets the range of the fog. To disable fog, use 't3d_fog_disable' or set 'near' and 'far' to 0. Note: in order for fog itself to work, make sure to setup the needed RSPQ commands.
| near | start of the fog effect |
| far | end of the fog effect |
| void t3d_indexbuffer_convert | ( | int16_t | indices[], |
| int | count ) |
Converts an index buffer for triangle strips from indices to encoded DMEM pointers. This is necessary in order for 't3d_tri_draw_strip' to work. Internally this data will be DMA'd by the ucode later on.
Format: The input data is expected to be an array of local indices (0-69) as s16 values. The first 3 values define the initial triangle. Every index afterwards will extend the strip by one triangle (winding order flipped). Degenerate triangles are NOT supported, since it is always more efficient to use a restart flag. To start a new strip, set the MSB of the next index, so: 'idx | (1<<15)' meaning the flagged value and the 2 following values will form a new triangle, re-starting the strip. Since restarting does not need a special index value, non-stripped indices (triangle lists) can be encoded too without overhead.
Examples: (original triangles -> expected input of this function) [4,5,6] [0,1,2] [7,8,9] -> [4,5,6,(0x8000|0),1,2,(0x8000|7),8,9] [0,1,2] [3,0,2] [0,3,4] [5,0,4] -> [1,2,0,3,0,4,5]
| indices | index buffer of local indices, will be modified in-place |
| count | index count |
| void t3d_init | ( | T3DInitParams | params | ) |
Initializes the tiny3d library.
| params | settings to configure the library |
| void t3d_light_set_ambient | ( | const uint8_t * | color | ) |
Sets the global ambient light color. This color is always active and applied to all objects. To disable the ambient light, set the color to black.
| color | color in RGBA8 format |
| void t3d_light_set_count | ( | int | count | ) |
Sets the amount of active lights (excl. ambient light). Note that the ambient light does not count towards this limit and is always applied.
| count | amount of lights (0-6) |
| void t3d_light_set_directional | ( | int | index, |
| const uint8_t * | color, | ||
| const T3DVec3 * | dir ) |
Sets a directional light. You can set up to 7 directional lights, the amount can be set with 't3d_light_set_count'. Note that directional and point lights share the same space.
| index | index (0-6) |
| color | color in RGBA8 format |
| dir | direction vector |
| void t3d_light_set_exposure | ( | float | exposure | ) |
Sets the final color exposure. This will be applied to the combined light + vertex color as a simple scaling factor. Any value above 1.0 after scaling will be clamped. This setting can be used to implement a simple form of HDR.
Note that negative values are allowed and will invert the color to some extend.
| exposure | factor, 1.0 by default |
| void t3d_light_set_point | ( | int | index, |
| const uint8_t * | color, | ||
| const T3DVec3 * | pos, | ||
| float | size, | ||
| bool | ignoreNormals ) |
Sets a point light. You can set up to 7 point lights, the amount can be set with 't3d_light_set_count'. Note that point and directional lights share the same space.
The position is expected to be in world-space, and will be transformed internally. Before calling this function, make sure to have a viewport attached.
The size argument is roughly the maximum radius in world-space the light will cover. Note that due to precision limitations and the fact lighting happens per vertex, the size is not exact.
| index | index (0-6) |
| color | color in RGBA8 format |
| pos | position in world-space |
| size | distance, in world-space |
| ignoreNormals | if true, the light will only check the distance, not the angle (useful for cutouts) |
| void t3d_matrix_pop | ( | int | count | ) |
Pops the current matrix from the stack.
| count | how many matrices to pop |
| void t3d_matrix_push | ( | const T3DMat4FP * | mat | ) |
Multiplies a matrix with the current stack position, then pushes it onto the stack.
| mat | address to load matrix from |
| void t3d_matrix_push_pos | ( | int | count | ) |
Moves the stack pos. without changing a matrix or causing re-calculations. This should only be used in preparation for 't3d_matrix_set' calls.
E.g. instead of multiple push/pop combis, use:
This is the most efficient way to set multiple matrices at the same level.
| count | relative change (matrix count), should usually be 1 |
| void t3d_matrix_set | ( | const T3DMat4FP * | mat, |
| bool | doMultiply ) |
Directly loads a matrix, overwriting the current stack position.
With 'doMultiply' set to false, this lets you completely replace the current matrix. With 'doMultiply' set to true, it serves as a faster version of a pop+push combination.
| mat | address to load matrix from |
| doMultiply | if true, the matrix will be multiplied with the previous stack entry |
| void t3d_matrix_set_proj | ( | const T3DMat4FP * | mat | ) |
Sets the projection matrix, this is stored outside of the matrix stack.
| mat | address to load matrix from |
| void t3d_quad_draw_unindexed | ( | uint32_t | baseIndex, |
| uint32_t | quadCount ) |
Draws multiple quads, assuming sequential indices. For example with baseIndex=4 and quadCount=2, we would get -> 4,5,6 7,6,5, 8,9,10, 11,10,9 This is effectively performing an un-indexed draw of quads, but without the overhead of loading an index-buffer. This method is faster than going through 't3d_tri_draw_strip'.
| baseIndex | first index |
| triCount | amount of quads to draw |
|
inlinestatic |
Converts an address into a segmented one. If used in a vertex/matrix load, it will cause the segment table to be used and the address in there to be added on top. To set entries in the segment table use 't3d_segment_set'.
| segmentId | id (1-7) |
| ptr | pointer, can be NULL for absolute addressing |
|
inlinestatic |
Creates a dummy address to be used for vertex/matrix loads. This will cause the address in the segment table to be used instead. If you need relative addressing instead, use 't3d_segment_address'
| segmentId | id (1-7) |
| void t3d_segment_set | ( | uint8_t | segmentId, |
| void * | address ) |
Sets a new address in the segment table. This acts as a base-address for addresses in matrices/vertices with a matching segment index. Segment 0 is reserved and always has a zero value.
| segmentId | id (1-7) |
| address | base RDRAM address |
| void t3d_state_set_alpha_to_tile | ( | bool | enable | ) |
Enables or disables the alpha-to-tile feature. This will cause the 3 MSBs of the input vertex alpha to be stored as the base tile. Which is later used during triangle draws to define what TEX0 is (by default TILE0). This feature is completely free and adds no extra time.
This can be useful if you can preload a tile-set into TMEM containing multiple textures. By setting up distinct tiles you can still take advantage of repeating UVs. Each triangle can then select its own tile without a costly texture or state switches inbetween.
NOTE: since a triangle can only have one tile, it is expected that all vertices have the same tile set. Otherwise it is considered undefined behavior. Furthermore, When loading vertices it is expected that both vertices in the interleaved struct must contain the same tile value.
| enable | true to enable for future vertex loads, false to disable. |
| void t3d_state_set_depth_offset | ( | int16_t | offset | ) |
Offsets the final screen-space depth by a fixed amount. This can be used as an alternative to decals by drawing normally with an offset instead. E.g.: t3d_state_set_depth_offset(-0x40);
| offset | relative offset, negative value to pull depth closer. Pass '0' to reset. |
| void t3d_state_set_drawflags | ( | enum T3DDrawFlags | drawFlags | ) |
Sets various draw flags, this will affect how triangles are drawn.
| drawFlags |
| void t3d_state_set_vertex_fx | ( | enum T3DVertexFX | func, |
| int16_t | arg0, | ||
| int16_t | arg1 ) |
Sets a function for vertex effects. To disable it, set the function to 'T3D_VERTEX_FX_NONE'. The arg0/arg1 values are stored ín DMEM and are used by the ucode.
The meaning of those arguments depends on the type:
| func | vertex effect function |
| arg0 | first argument |
| arg1 | second argument |
| void t3d_state_set_vertex_fx_scale | ( | uint16_t | scale | ) |
Overrides the scale factor for some vertex effects. This is currently only used by the 'T3D_VERTEX_FX_SPHERICAL_UV' function to create an offset based on screen-space position. This is automatically calculated by the ucode when setting the screen-size. However if you see distortions near the edge of the screen, you can set this manually.
| scale | scale factor |
| void t3d_tri_draw | ( | uint32_t | v0, |
| uint32_t | v1, | ||
| uint32_t | v2 ) |
Draws a single triangle, referencing loaded vertices.
| v0 | vertex index 0 |
| v1 | vertex index 1 |
| v2 | vertex index 2 |
| void t3d_tri_draw_strip | ( | int16_t * | indexBuff, |
| int | count ) |
Draws a strip of triangles by loading an index buffer. Note that this data must be in an internal format, so use 't3d_indexbuffer_convert' to convert it first. The docs of 't3d_indexbuffer_convert' also describe the format of the input data.
The data behind 'indexBuff' will be DMA'd by the ucode, so it must be aligned to 8 bytes, and persist in memory until the triangles are drawn. The target location of the DMA in DMEM is shared with the vertex cache, aligned to the end. Make sure that there is enough space left to load the indices to not corrupt the vertices. E.g. if you loaded 68 vertices, you have 2 slots free or 36*2 bytes, meaning you can load 36 indices. Note that due to alignment reasons, a safety margin of 4 indices should be added if the free vertex count is odd.
The built-in model format will use this function internally, if you plan on manually using it for model data, check out 'tools/gltf_importer/src/optimizer/meshOptimizer.cpp' for an algorithm to do so.
| indexBuff | index buffer to load |
| count | amount of indices to load |
| void t3d_tri_draw_strip_and_sync | ( | int16_t * | indexBuff, |
| int | count ) |
Combined t3d_tri_draw_strip + t3d_tri_sync. See individual functions for more details.
| indexBuff | index buffer to load |
| count | amount of indices to load |
| void t3d_tri_draw_unindexed | ( | uint32_t | baseIndex, |
| uint32_t | triCount ) |
Draws multiple triangles, assuming sequential indices. For example with baseIndex=4 and triCount=2, we would get -> 4,5,6 7,8,9 This is effectively performing an un-indexed draw of triangles, but without the overhead of loading an index-buffer. This method is faster than going through 't3d_tri_draw_strip'.
| baseIndex | first index |
| triCount | amount of triangles to draw |
|
inlinestatic |
Syncs pending triangles. This needs to be called after triangles where drawn and a different overlay which also generates RDP commands will be used. (e.g. RDPQ)
| void t3d_vert_load | ( | const T3DVertPacked * | vertices, |
| uint32_t | offset, | ||
| uint32_t | count ) |
Loads a vertex buffer with a given size, this can then be used to draw triangles.
| vertices | vertex buffer |
| offset | offset in the target buffer (0-68) |
| count | how many vertices to load (1-70) |
| uint16_t t3d_vert_pack_normal | ( | const T3DVec3 * | normal | ) |
Packs a floating-point normal into the internal 5.6.5 format.
| normal | normal vector |
|
inlinestatic |
Returns the pointer to the color (as a u32) of a vertex in a buffer
| vert | vertex buffer |
| idx | vertex index |
|
inlinestatic |
Returns the pointer to the packed normal of a vertex in a buffer
| vert | vertex buffer |
| idx | vertex index |
|
inlinestatic |
Returns the pointer to a position of a vertex in a buffer
| vert | vertex buffer |
| idx | vertex index |
|
inlinestatic |
Returns the pointer to the color (as a u8[4]) of a vertex in a buffer
| vert | vertex buffer |
| idx | vertex index |
|
inlinestatic |
Returns the pointer to the UV of a vertex in a buffer
| vert | vertex buffer |
| idx | vertex index |
| void t3d_viewport_attach | ( | T3DViewport * | viewport | ) |
Uses the given viewport for further rendering and applies its settings. This will set the visible screen-rect, and apply the camera and projection matrix.
Note that you may have to re-apply directional lights if you are using multiple viewports, as they are depend on the view matrix.
| viewport | viewport, pointer must be valid until the next t3d_viewport_attach call |
| void t3d_viewport_calc_viewspace_pos | ( | T3DViewport * | viewport, |
| T3DVec3 * | out, | ||
| const T3DVec3 * | pos ) |
Calculates the view-space position of a given world-space position. This will also handle offset viewports (e.g. for splitscreens)
| viewport | viewport to calculate for |
| out | output screen-space vector |
| pos | input world-space position |
|
inlinestatic |
Creates a viewport struct, this only creates a struct and doesn't change any setting. Most likely you want to update the camera and projection matrix over time, in which case you should use 't3d_viewport_create_buffered' instead.
Once done, free the viewports internal data via 't3d_viewport_destroy'. (Note that for compatibility reasons, this is not strictly necessary unless buffered)
|
inlinestatic |
Same as 't3d_viewport_create' but buffered. This will allow you to change matrices over time without running into race-conditions with the RSP.
Once done, free the viewports internal data via 't3d_viewport_destroy'.
| count | amount of buffered matrices (usually amount of framebuffers) |
|
inlinestatic |
Destroys a viewport and frees allocated internal data.
Note that since you still hold the struct itself, any further use of it is undefined behavior. To create a new viewport overwrite the struct with a new one from 't3d_viewport_create' instead.
| viewport | viewport to destroy |
| T3DViewport * t3d_viewport_get | ( | ) |
Returns the currently attached viewport.
| void t3d_viewport_look_at | ( | T3DViewport * | viewport, |
| const T3DVec3 * | eye, | ||
| const T3DVec3 * | target, | ||
| const T3DVec3 * | up ) |
Sets a new camera position and direction for the given viewport. The view matrix gets auto. applied at the next t3d_viewport_attach call.
| eye | camera position |
| target | camera target/look-at |
| up | camera up vector, expected to be {0,1,0} by default |
|
inlinestatic |
Convenience function to set the area of a viewport.
| viewport | |
| x | position (0 by default) |
| y | |
| width | size (display width by default) |
| height |
| void t3d_viewport_set_ortho | ( | T3DViewport * | viewport, |
| float | left, | ||
| float | right, | ||
| float | bottom, | ||
| float | top, | ||
| float | near, | ||
| float | far ) |
Sets an orthographic projection matrix for the given viewport. The matrix gets auto. applied at the next t3d_viewport_attach call.
| viewport | |
| left | |
| right | |
| bottom | |
| top | |
| near | near plane distance |
| far | far plane distance (should be >=40 to avoid depth-precision issues) |
| void t3d_viewport_set_perspective | ( | T3DViewport * | viewport, |
| float | fov, | ||
| float | aspectRatio, | ||
| float | near, | ||
| float | far ) |
Updates the projection matrix of the given viewport using a perspective projection. The proj. matrix gets auto. applied at the next t3d_viewport_attach call.
| viewport | |
| fov | fov in radians |
| aspectRatio | aspect ratio (= width / height) |
| near | near plane distance |
| far | far plane distance (should be >=40 to avoid depth-precision issues) |
| void t3d_viewport_set_projection | ( | T3DViewport * | viewport, |
| float | fov, | ||
| float | near, | ||
| float | far ) |
Updates the projection matrix of the given viewport. The proj. matrix gets auto. applied at the next t3d_viewport_attach call.
NOTE: if you need a custom aspect ratio, use 't3d_viewport_set_perspective' instead.
| viewport | |
| fov | fov in radians |
| near | near plane distance |
| far | far plane distance (should be >=40 to avoid depth-precision issues) |
| void t3d_viewport_set_projection_matrix | ( | T3DViewport * | viewport, |
| const T3DMat4 * | mat ) |
Sets a new projection matrix for the given viewport. If you have a perspective or orthographic projection prefer using 't3d_viewport_set_projection' or 't3d_viewport_set_ortho'.
| viewport | viewport to set for |
| mat | new projection matrix |
| void t3d_viewport_set_view_matrix | ( | T3DViewport * | viewport, |
| const T3DMat4 * | mat ) |
Sets a new view (aka camera) matrix for the given viewport. If you have a look-at based camera prefer using 't3d_viewport_look_at'.
| viewport | viewport to set for |
| mat | new view matrix |
|
inlinestatic |
Sets the normalization factor for W in the ucode. NOTE: this gets called automatically by t3d_viewport_set_projection. You only need to call this if you want to provide your own projection matrix.
| viewport | |
| near | |
| far |