Kohi Game Engine
geometry.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "math/math_types.h"
4 #include "strings/kname.h"
5 #include "utils/kcolour.h"
6 
8 #define GEOMETRY_NAME_MAX_LENGTH 256
9 
14 typedef enum kgeometry_type {
36 
41 typedef struct kgeometry {
44 
54 
60  void* vertices;
63 
69  void* indices;
73 
74 typedef enum grid_orientation {
89 
99 KAPI void geometry_generate_normals(u32 vertex_count, vertex_3d* vertices, u32 index_count, u32* indices);
100 
110 KAPI void geometry_generate_tangents(u32 vertex_count, vertex_3d* vertices, u32 index_count, u32* indices);
111 
126 KAPI void geometry_deduplicate_vertices(u32 vertex_count, vertex_3d* vertices, u32 index_count, u32* indices, u32* out_vertex_count, vertex_3d** out_vertices);
127 
138 KAPI void generate_uvs_from_image_coords(u32 img_width, u32 img_height, u32 px_x, u32 px_y, f32* out_tx, f32* out_ty);
139 
154 KAPI kgeometry geometry_generate_quad(f32 width, f32 height, f32 tx_min, f32 tx_max, f32 ty_min, f32 ty_max, kname name);
155 
167 
180 
183 
198 KAPI kgeometry geometry_generate_plane(f32 width, f32 height, u32 x_segment_count, u32 y_segment_count, f32 tile_x, f32 tile_y, kname name);
199 
214 KAPI kgeometry geometry_generate_plane_2d(f32 width, f32 height, u32 x_segment_count, u32 y_segment_count, f32 tile_x, f32 tile_y, kname name, b8 centered);
215 
223 
231 
241 
255 KAPI kgeometry geometry_generate_cube(f32 width, f32 height, f32 depth, f32 tile_x, f32 tile_y, kname name);
256 
270 KAPI kgeometry geometry_generate_grid(grid_orientation orientation, u32 segment_count_dim_0, u32 segment_count_dim_1, f32 segment_size, b8 use_third_axis, kname name);
271 
278 
280  axis_3d axis,
281  f32 base_offset,
282  f32 length,
283  colour4 colour,
284  f32 shaft_radius,
285  f32 arrowhead_radius,
286  f32 arrowhead_length,
287  u32 segment_count,
288  b8 include_arrowhead,
289  u32* out_vertex_count,
290  u32* out_index_count,
291  colour_vertex_3d* vertices,
292  u32* indices,
293  u32 vertex_offset);
294 
296  axis_3d axis,
297  f32 radius,
298  f32 thickness,
299  colour4 colour,
300  u32 ring_segments,
301  u32 tube_segments,
302  u32* out_vertex_count,
303  u32* out_index_count,
304  colour_vertex_3d* vertices,
305  u32* indices,
306  u32 vertex_offset);
#define KAPI
Import/export qualifier.
Definition: defines.h:209
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:27
_Bool b8
8-bit boolean type
Definition: defines.h:60
float f32
32-bit floating point number
Definition: defines.h:49
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:24
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:30
KAPI kgeometry geometry_generate_quad(f32 width, f32 height, f32 tx_min, f32 tx_max, f32 ty_min, f32 ty_max, kname name)
Generates a two-dimensional quad (two triangles) of geometry. Note that memory for the vertex and ind...
KAPI kgeometry geometry_generate_line_box3d_typed(vec3 size, kname name, kgeometry_type type, vec3 offset)
KAPI kgeometry geometry_generate_line2d(vec2 point_0, vec2 point_1, kname name)
Generates a two-dimensional line of geometry. Note that the memory for the vertex array is dynamicall...
kgeometry_type
Indicates a geometry type, typically used to infer things like vertex and index sizes.
Definition: geometry.h:14
@ KGEOMETRY_TYPE_UNKNOWN
Unknown and invalid type of geometry. This being set generally indicates an error in code.
Definition: geometry.h:16
@ KGEOMETRY_TYPE_3D_HEIGHTMAP_TERRAIN
Used for heightmap terrain-specific geometry that rarely (if ever) changes - includes material index/...
Definition: geometry.h:32
@ KGEOMETRY_TYPE_2D_DYNAMIC
Used for 2d geometry that changes often.
Definition: geometry.h:20
@ KGEOMETRY_TYPE_3D_STATIC
Used for 3d geometry that doesn't change.
Definition: geometry.h:22
@ KGEOMETRY_TYPE_2D_STATIC
Used for 2d geometry that doesn't change.
Definition: geometry.h:18
@ KGEOMETRY_TYPE_3D_SKINNED
Used for skinned 3d geometry doesn't change often, and includes bone/weight data.
Definition: geometry.h:30
@ KGEOMETRY_TYPE_CUSTOM
User-defined geometry type. Vertex/index size will only be looked at for this type.
Definition: geometry.h:34
@ KGEOMETRY_TYPE_3D_STATIC_POSITION_ONLY
Used for 3d geometry that doesn't change, and only contains position data.
Definition: geometry.h:26
@ KGEOMETRY_TYPE_3D_STATIC_COLOUR
Used for 3d geometry that doesn't change, and only contains position and colour data.
Definition: geometry.h:24
@ KGEOMETRY_TYPE_3D_DYNAMIC
Used for 3d geometry that changes often.
Definition: geometry.h:28
KAPI kgeometry geometry_generate_line3d_typed(vec3 point_0, vec3 point_1, kname name, kgeometry_type type)
KAPI void generate_axis_ring_geometry(axis_3d axis, f32 radius, f32 thickness, colour4 colour, u32 ring_segments, u32 tube_segments, u32 *out_vertex_count, u32 *out_index_count, colour_vertex_3d *vertices, u32 *indices, u32 vertex_offset)
KAPI kgeometry geometry_generate_grid(grid_orientation orientation, u32 segment_count_dim_0, u32 segment_count_dim_1, f32 segment_size, b8 use_third_axis, kname name)
Create a geometry-based grid using the given parameters. The grid is based on line geometry and has n...
grid_orientation
Definition: geometry.h:74
@ GRID_ORIENTATION_YZ
A grid that lies on the x-plane (orthogonal to the default screen plane and the ground plane).
Definition: geometry.h:87
@ GRID_ORIENTATION_XZ
A grid that lies "flat" in the world along the ground plane (y-plane). This is the default configurat...
Definition: geometry.h:79
@ GRID_ORIENTATION_XY
A grid that lies on the z-plane (facing the screen by default, orthogonal to the ground plane).
Definition: geometry.h:83
KAPI void geometry_generate_normals(u32 vertex_count, vertex_3d *vertices, u32 index_count, u32 *indices)
Calculates normals for the given vertex and index data. Modifies vertices in place.
KAPI kgeometry geometry_generate_line_sphere3d_typed(f32 radius, u32 segment_count, kname name, kgeometry_type type)
KAPI kgeometry geometry_generate_cube(f32 width, f32 height, f32 depth, f32 tile_x, f32 tile_y, kname name)
Generates a three-dimensional cube of geometry. Note that memory for the vertex and index arrays are ...
KAPI kgeometry geometry_generate_line_sphere3d(f32 radius, u32 segment_count, kname name)
KAPI void generate_axis_geometry(axis_3d axis, f32 base_offset, f32 length, colour4 colour, f32 shaft_radius, f32 arrowhead_radius, f32 arrowhead_length, u32 segment_count, b8 include_arrowhead, u32 *out_vertex_count, u32 *out_index_count, colour_vertex_3d *vertices, u32 *indices, u32 vertex_offset)
KAPI void geometry_destroy(kgeometry *geometry)
Destroys the given geometry.
KAPI kgeometry geometry_generate_plane(f32 width, f32 height, u32 x_segment_count, u32 y_segment_count, f32 tile_x, f32 tile_y, kname name)
Generates a three-dimensional plane of geometry. Note that memory for the vertex and index arrays are...
KAPI void geometry_recalculate_line_box3d_by_extents(kgeometry *geometry, extents_3d extents, vec3 offset)
Recalculates the vertices in the given geometry based off the given extents.
KAPI kgeometry geometry_generate_line3d(vec3 point_0, vec3 point_1, kname name)
Generates a three-dimensional line of geometry. Note that the memory for the vertex array is dynamica...
struct kgeometry kgeometry
Represents geometry to be used for various purposes (rendering objects in the world,...
KAPI void generate_uvs_from_image_coords(u32 img_width, u32 img_height, u32 px_x, u32 px_y, f32 *out_tx, f32 *out_ty)
Generates texture coordinates based on pixel position within an image's dimensions.
KAPI void geometry_recalculate_line_box3d_by_points(kgeometry *geometry, vec3 points[8])
Recalculates the vertices in the given geometry based off the given points.
KAPI void geometry_generate_tangents(u32 vertex_count, vertex_3d *vertices, u32 index_count, u32 *indices)
Calculates tangents for the given vertex and index data. Modifies vertices in place.
KAPI void geometry_deduplicate_vertices(u32 vertex_count, vertex_3d *vertices, u32 index_count, u32 *indices, u32 *out_vertex_count, vertex_3d **out_vertices)
De-duplicates vertices, leaving only unique ones. Leaves the original vertices array intact....
KAPI kgeometry geometry_generate_plane_2d(f32 width, f32 height, u32 x_segment_count, u32 y_segment_count, f32 tile_x, f32 tile_y, kname name, b8 centered)
Generates a two-dimensional plane of geometry. Note that memory for the vertex and index arrays are d...
KAPI kgeometry geometry_generate_line_box3d(vec3 size, kname name, vec3 offset)
Generates a line-based 3d box based on the provided size.
This files contains an implementation of knames.
u64 kname
A kname is a string hash made for quick comparisons versus traditional string comparisons.
Definition: kname.h:36
Contains various math types required for the engine.
axis_3d
Definition: math_types.h:16
Represents a single vertex in 3D space with position and colour data only.
Definition: math_types.h:477
Represents the extents of a 3d object.
Definition: math_types.h:401
Represents geometry to be used for various purposes (rendering objects in the world,...
Definition: geometry.h:41
void * vertices
The standard vertex data.
Definition: geometry.h:60
vec3 center
The center of the geometry in local coordinates.
Definition: geometry.h:51
kname name
The geometry name.
Definition: geometry.h:43
u32 index_count
The index count.
Definition: geometry.h:65
u32 vertex_count
The vertex count.
Definition: geometry.h:56
u32 index_element_size
The size of each index. Ignored unless type is KGEOMETRY_TYPE_CUSTOM.
Definition: geometry.h:67
kgeometry_type type
The geometry type.
Definition: geometry.h:46
u64 vertex_buffer_offset
The offset from the beginning of the standard vertex buffer.
Definition: geometry.h:62
u32 vertex_element_size
The size of each vertex's standard data. Ignored unless type is KGEOMETRY_TYPE_CUSTOM.
Definition: geometry.h:58
u64 index_buffer_offset
The offset from the beginning of the index buffer.
Definition: geometry.h:71
void * indices
The index data.
Definition: geometry.h:69
extents_3d extents
The extents of the geometry in local coordinates.
Definition: geometry.h:53
u16 generation
The geometry generation. Incremented every time the geometry changes.
Definition: geometry.h:49
Represents a single vertex in 3D space.
Definition: math_types.h:423
A 2-element vector.
Definition: math_types.h:31
A 3-element vector.
Definition: math_types.h:117
A 4-element vector.
Definition: math_types.h:229