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 
7 #define GEOMETRY_NAME_MAX_LENGTH 256
8 
13 typedef enum kgeometry_type {
33 
38 typedef struct kgeometry {
41 
51 
57  void* vertices;
60 
66  void* indices;
70 
71 typedef enum grid_orientation {
86 
96 KAPI void geometry_generate_normals(u32 vertex_count, vertex_3d* vertices, u32 index_count, u32* indices);
97 
107 KAPI void geometry_generate_tangents(u32 vertex_count, vertex_3d* vertices, u32 index_count, u32* indices);
108 
123 KAPI void geometry_deduplicate_vertices(u32 vertex_count, vertex_3d* vertices, u32 index_count, u32* indices, u32* out_vertex_count, vertex_3d** out_vertices);
124 
135 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);
136 
151 KAPI kgeometry geometry_generate_quad(f32 width, f32 height, f32 tx_min, f32 tx_max, f32 ty_min, f32 ty_max, kname name);
152 
164 
176 
177 KAPI kgeometry geometry_generate_line_sphere3d(f32 radius, u32 segment_count, vec4 colour, kname name);
178 
193 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);
194 
202 
210 
219 
233 KAPI kgeometry geometry_generate_cube(f32 width, f32 height, f32 depth, f32 tile_x, f32 tile_y, kname name);
234 
248 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);
249 
#define KAPI
Import/export qualifier.
Definition: defines.h:205
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
_Bool b8
8-bit boolean type
Definition: defines.h:58
float f32
32-bit floating point number
Definition: defines.h:47
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:22
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
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(vec3 size, kname name)
Generates a line-based 3d box based on the provided size.
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:13
@ KGEOMETRY_TYPE_UNKNOWN
Unknown and invalid type of geometry. This being set generally indicates an error in code.
Definition: geometry.h:15
@ KGEOMETRY_TYPE_3D_HEIGHTMAP_TERRAIN
Used for heightmap terrain-specific geometry that rarely (if ever) changes - includes material index/...
Definition: geometry.h:29
@ KGEOMETRY_TYPE_2D_DYNAMIC
Used for 2d geometry that changes often.
Definition: geometry.h:19
@ KGEOMETRY_TYPE_3D_STATIC
Used for 3d geometry that doesn't change.
Definition: geometry.h:21
@ KGEOMETRY_TYPE_2D_STATIC
Used for 2d geometry that doesn't change.
Definition: geometry.h:17
@ KGEOMETRY_TYPE_3D_SKINNED
Used for skinned 3d geometry that changes potentially every frame, and includes bone/weight data.
Definition: geometry.h:27
@ KGEOMETRY_TYPE_CUSTOM
User-defined geometry type. Vertex/index size will only be looked at for this type.
Definition: geometry.h:31
@ KGEOMETRY_TYPE_3D_STATIC_COLOUR_ONLY
Used for 3d geometry that doesn't change, and only contains colour data.
Definition: geometry.h:23
@ KGEOMETRY_TYPE_3D_DYNAMIC
Used for 3d geometry that changes often.
Definition: geometry.h:25
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...
KAPI kgeometry geometry_generate_line_sphere3d(f32 radius, u32 segment_count, vec4 colour, kname name)
grid_orientation
Definition: geometry.h:71
@ GRID_ORIENTATION_YZ
A grid that lies on the x-plane (orthogonal to the default screen plane and the ground plane).
Definition: geometry.h:84
@ 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:76
@ GRID_ORIENTATION_XY
A grid that lies on the z-plane (facing the screen by default, orthogonal to the ground plane).
Definition: geometry.h:80
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_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 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 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_recalculate_line_box3d_by_extents(kgeometry *geometry, extents_3d extents)
Recalculates the vertices in the given geometry based off the given extents.
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....
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.
Represents the extents of a 3d object.
Definition: math_types.h:213
Represents geometry to be used for various purposes (rendering objects in the world,...
Definition: geometry.h:38
void * vertices
The vertex data.
Definition: geometry.h:57
vec3 center
The center of the geometry in local coordinates.
Definition: geometry.h:48
kname name
The geometry name.
Definition: geometry.h:40
u32 index_count
The index count.
Definition: geometry.h:62
u32 vertex_count
The vertex count.
Definition: geometry.h:53
u32 index_element_size
The size of each index. Ignored unless type is KGEOMETRY_TYPE_CUSTOM.
Definition: geometry.h:64
kgeometry_type type
The geometry type.
Definition: geometry.h:43
u64 vertex_buffer_offset
The offset from the beginning of the vertex buffer.
Definition: geometry.h:59
u32 vertex_element_size
The size of each vertex. Ignored unless type is KGEOMETRY_TYPE_CUSTOM.
Definition: geometry.h:55
u64 index_buffer_offset
The offset from the beginning of the index buffer.
Definition: geometry.h:68
void * indices
The index data.
Definition: geometry.h:66
extents_3d extents
The extents of the geometry in local coordinates.
Definition: geometry.h:50
u16 generation
The geometry generation. Incremented every time the geometry changes.
Definition: geometry.h:46
Represents a single vertex in 3D space.
Definition: math_types.h:223
A 2-element vector.
Definition: math_types.h:19
A 3-element vector.
Definition: math_types.h:49
A 4-element vector.
Definition: math_types.h:89