Kohi Game Engine
geometry.h File Reference
#include "math/math_types.h"
#include "strings/kname.h"

Go to the source code of this file.

Data Structures

struct  kgeometry
 Represents geometry to be used for various purposes (rendering objects in the world, physics/collision, etc.). More...
 

Macros

#define GEOMETRY_NAME_MAX_LENGTH   256
 The maximum length of a geometry name. More...
 

Typedefs

typedef enum kgeometry_type kgeometry_type
 Indicates a geometry type, typically used to infer things like vertex and index sizes. More...
 
typedef struct kgeometry kgeometry
 Represents geometry to be used for various purposes (rendering objects in the world, physics/collision, etc.). More...
 
typedef enum grid_orientation grid_orientation
 

Enumerations

enum  kgeometry_type {
  KGEOMETRY_TYPE_UNKNOWN = 0x00 , KGEOMETRY_TYPE_2D_STATIC = 0x01 , KGEOMETRY_TYPE_2D_DYNAMIC = 0x02 , KGEOMETRY_TYPE_3D_STATIC = 0x03 ,
  KGEOMETRY_TYPE_3D_STATIC_COLOUR_ONLY = 0x04 , KGEOMETRY_TYPE_3D_DYNAMIC = 0x05 , KGEOMETRY_TYPE_3D_SKINNED = 0x06 , KGEOMETRY_TYPE_3D_HEIGHTMAP_TERRAIN = 0x07 ,
  KGEOMETRY_TYPE_CUSTOM = 0xFF
}
 Indicates a geometry type, typically used to infer things like vertex and index sizes. More...
 
enum  grid_orientation { GRID_ORIENTATION_XZ = 0 , GRID_ORIENTATION_XY = 1 , GRID_ORIENTATION_YZ = 2 }
 

Functions

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. More...
 
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. More...
 
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. Allocates a new array in out_vertices. Modifies indices in-place. Original vertex array should be freed by caller. More...
 
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. More...
 
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 index arrays are dynamically allocated, so this should be cleaned up with geometry_destroy(). Dimensions account for the x- and y-axes only. More...
 
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 dynamically allocated, so this should be cleaned up with geometry_destroy(). Note that index data is not used for this geometry. More...
 
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 dynamically allocated, so this should be cleaned up with geometry_destroy(). Note that index data is not used for this geometry. More...
 
KAPI kgeometry geometry_generate_line_sphere3d (f32 radius, u32 segment_count, vec4 colour, kname name)
 
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 dynamically allocated, so this should be cleaned up with geometry_destroy(). Dimensions account for the x- and y-axes, z is always 0. More...
 
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. More...
 
KAPI void geometry_recalculate_line_box3d_by_extents (kgeometry *geometry, extents_3d extents)
 Recalculates the vertices in the given geometry based off the given extents. More...
 
KAPI kgeometry geometry_generate_line_box3d (vec3 size, kname name)
 Generates a line-based 3d box based on the provided size. More...
 
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 dynamically allocated, so this should be cleaned up with geometry_destroy(). More...
 
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 no indices. Note that the vertex array data is dynamically allocated and should be cleaned up using geometry_destroy(). More...
 
KAPI void geometry_destroy (kgeometry *geometry)
 Destroys the given geometry. More...
 

Macro Definition Documentation

◆ GEOMETRY_NAME_MAX_LENGTH

#define GEOMETRY_NAME_MAX_LENGTH   256

The maximum length of a geometry name.

Typedef Documentation

◆ grid_orientation

◆ kgeometry

typedef struct kgeometry kgeometry

Represents geometry to be used for various purposes (rendering objects in the world, physics/collision, etc.).

◆ kgeometry_type

Indicates a geometry type, typically used to infer things like vertex and index sizes.

Enumeration Type Documentation

◆ grid_orientation

Enumerator
GRID_ORIENTATION_XZ 

A grid that lies "flat" in the world along the ground plane (y-plane). This is the default configuration.

GRID_ORIENTATION_XY 

A grid that lies on the z-plane (facing the screen by default, orthogonal to the ground plane).

GRID_ORIENTATION_YZ 

A grid that lies on the x-plane (orthogonal to the default screen plane and the ground plane).

◆ kgeometry_type

Indicates a geometry type, typically used to infer things like vertex and index sizes.

Enumerator
KGEOMETRY_TYPE_UNKNOWN 

Unknown and invalid type of geometry. This being set generally indicates an error in code.

KGEOMETRY_TYPE_2D_STATIC 

Used for 2d geometry that doesn't change.

KGEOMETRY_TYPE_2D_DYNAMIC 

Used for 2d geometry that changes often.

KGEOMETRY_TYPE_3D_STATIC 

Used for 3d geometry that doesn't change.

KGEOMETRY_TYPE_3D_STATIC_COLOUR_ONLY 

Used for 3d geometry that doesn't change, and only contains colour data.

KGEOMETRY_TYPE_3D_DYNAMIC 

Used for 3d geometry that changes often.

KGEOMETRY_TYPE_3D_SKINNED 

Used for skinned 3d geometry that changes potentially every frame, and includes bone/weight data.

KGEOMETRY_TYPE_3D_HEIGHTMAP_TERRAIN 

Used for heightmap terrain-specific geometry that rarely (if ever) changes - includes material index/weight data.

KGEOMETRY_TYPE_CUSTOM 

User-defined geometry type. Vertex/index size will only be looked at for this type.

Function Documentation

◆ generate_uvs_from_image_coords()

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.

Parameters
img_widthThe width of the image in pixels.
img_heightThe height of the image in pixels.
px_xThe pixel position along the x-axis.
px_yThe pixel position along the y-axis.
out_txA pointer to hold the generated texture coordinate on the x-axis.
out_tyA pointer to hold the generated texture coordinate on the y-axis.

◆ geometry_deduplicate_vertices()

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. Allocates a new array in out_vertices. Modifies indices in-place. Original vertex array should be freed by caller.

Parameters
vertex_countThe number of vertices in the array.
verticesThe original array of vertices to be de-duplicated. Not modified.
index_countThe number of indices in the array.
indicesThe array of indices. Modified in-place as vertices are removed.
out_vertex_countA pointer to hold the final vertex count.
out_verticesA pointer to hold the array of de-duplicated vertices.

◆ geometry_destroy()

KAPI void geometry_destroy ( kgeometry geometry)

Destroys the given geometry.

Parameters
geometryA pointer to the geometry to be destroyed.

◆ geometry_generate_cube()

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 dynamically allocated, so this should be cleaned up with geometry_destroy().

Parameters
widthThe width of the cube (x-axis).
heightThe height of the cube (y-axis).
depthThe depth of the cube (z-axis).
tile_xThe amount of tiling of the textures on each face of the cube on the x-axis. 1.0 means the texture is stretched across the entire surface. 2.0 means it's repeated, etc.
tile_yThe amount of tiling of the textures on each face of the cube on the y-axis. 1.0 means the texture is stretched across the entire surface. 2.0 means it's repeated, etc.
nameThe name of the geometry.
Returns
The newly-created geometry.

◆ geometry_generate_grid()

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 no indices. Note that the vertex array data is dynamically allocated and should be cleaned up using geometry_destroy().

Parameters
orientationThe orientation of the grid.
segment_count_dim_0The number of segments in the first orientation axis.
segment_count_dim_1The number of segments in the second orientation axis.
segment_sizeThe size of each individual segment along all axes.
use_third_axisIndicates if the grid should also display on a third axis, orthogonal to the two in the orientation.
nameThe name of the geometry.
Returns
The newly-created geometry.

◆ geometry_generate_line2d()

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 dynamically allocated, so this should be cleaned up with geometry_destroy(). Note that index data is not used for this geometry.

Parameters
point_0The first point of the line.
point_1The second point of the line.
nameThe name of the geometry.
Returns
The newly-created geometry.

◆ geometry_generate_line3d()

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 dynamically allocated, so this should be cleaned up with geometry_destroy(). Note that index data is not used for this geometry.

Parameters
point_0The first point of the line.
point_1The second point of the line.
nameThe name of the geometry.
Returns
The newly-created geometry.

◆ geometry_generate_line_box3d()

KAPI kgeometry geometry_generate_line_box3d ( vec3  size,
kname  name 
)

Generates a line-based 3d box based on the provided size.

Parameters
sizeThe size (extents) of the box.
nameThe name of the geometry.
Returns
The newly-created geometry.

◆ geometry_generate_line_sphere3d()

KAPI kgeometry geometry_generate_line_sphere3d ( f32  radius,
u32  segment_count,
vec4  colour,
kname  name 
)

◆ geometry_generate_normals()

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.

Parameters
vertex_countThe number of vertices.
verticesAn array of vertices.
index_countThe number of indices.
indicesAn array of vertices.

◆ geometry_generate_plane()

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 dynamically allocated, so this should be cleaned up with geometry_destroy(). Dimensions account for the x- and y-axes, z is always 0.

Parameters
widthThe width of the plane (x-axis).
heightThe height of the plane (y-axis).
x_segment_countThe number of segments to split the plane geometry into along the x-axis. Must be at least 1.
y_segment_countThe number of segments to split the plane geometry into along the y-axis. Must be at least 1.
tile_xThe amount of tiling of the textures on the face of the plane on the x-axis. 1.0 means the texture is stretched across the entire surface. 2.0 means it's repeated, etc.
tile_yThe amount of tiling of the textures on the face of the plane on the y-axis. 1.0 means the texture is stretched across the entire surface. 2.0 means it's repeated, etc.
nameThe name of the geometry.
Returns
The newly-created geometry.

◆ geometry_generate_quad()

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 index arrays are dynamically allocated, so this should be cleaned up with geometry_destroy(). Dimensions account for the x- and y-axes only.

Parameters
widthThe width of the plane (x-axis).
heightThe height of the plane (y-axis).
tx_minThe minimum texture coordinate along the x-axis.
ty_minThe minimum texture coordinate along the y-axis.
tx_maxThe maximum texture coordinate along the x-axis.
ty_maxThe maximum texture coordinate along the y-axis.
nameThe name of the geometry.
Returns
The newly-created geometry.

◆ geometry_generate_tangents()

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.

Parameters
vertex_countThe number of vertices.
verticesAn array of vertices.
index_countThe number of indices.
indicesAn array of vertices.

◆ geometry_recalculate_line_box3d_by_extents()

KAPI void geometry_recalculate_line_box3d_by_extents ( kgeometry geometry,
extents_3d  extents 
)

Recalculates the vertices in the given geometry based off the given extents.

Parameters
geometryA pointer to the geometry to modify.
extentsThe extents of which to base the modification.

◆ geometry_recalculate_line_box3d_by_points()

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.

Parameters
geometryA pointer to the geometry to modify.
pointsThe 8 points (i.e. corners of a box) that will be used for the modification.