LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_3d_primitives.c File Reference

Functions

LG_Quad lg_quad (vec3_t v0, vec3_t v1, vec3_t v2, vec3_t v3, LG_Color_u c)
 
LG_Quad lg_centered_unit_quad (LG_Color_u c)
 
LG_Quad lg_quad_transform (LG_Quad quad, mat4_t matrix)
 
LG_Cuboid lg_cuboid (vec3_t v0, vec3_t v1, vec3_t v2, vec3_t v3, vec3_t v4, vec3_t v5, vec3_t v6, vec3_t v7, LG_Color_u c)
 
zboolean lg_shape_is_cuboid (LG_Cuboid *shape)
 
LG_Cuboid lg_centered_unit_cube (LG_Color_u c)
 
LG_Cuboid lg_cuboid_transform (LG_Cuboid cuboid, mat4_t matrix)
 
XYZ_Arrows lg_xyz_arrows (float k, LG_Color_u x_axis_color, LG_Color_u y_axis_color, LG_Color_u z_axis_color)
 
Lines3D_VB lg_l3d_vb_from_quad (LG_Quad quad)
 
Lines3D_VB lg_l3d_vb_from_cuboid (LG_Cuboid cuboid)
 
Lines3D_VB lg_l3d_vb_from_arrows (XYZ_Arrows xyz_arrows)
 
const Vertex_uvlg_cube_normalized_vertices_uv ()
 
const unsigned short * lg_cube_vertex_uv_indices ()
 
size_t lg_sizeof_cube_normalized_vertices_uv ()
 
size_t lg_sizeof_cube_vertex_uv_indices ()
 
const Vertex_rgbalg_cube_normalized_vertices_rgba ()
 
const unsigned short * lg_wired_cube_vertex_rgba_indices ()
 
size_t lg_sizeof_cube_normalized_vertices_rgba ()
 
size_t lg_sizeof_wired_cube_vertex_rgba_indices ()
 

Detailed Description

 === A few 3D structs and funcs for line set vertex buffers ===

 All vertices are Vertex_rgba (so far)

 So far:
    - Lines3D_VB - line set vertex buffer and num of vertices, max num of lines = LINES3D_N_VERTICES_MAX / 2
    - LG_Quad
    - LG_Cuboid
    - XYZ_Arrows

 About Lines3D_VB:

 typedef struct {
    Vertex_rgba     vb_array[LINES3D_N_VERTICES_MAX];
    Vertex_rgba     *vb_ptr;
    uint32_t        n_vertices;
    zboolean        dynamic_b;
 } Lines3D_VB;

 Useful for generating VBO from shapes - can have:
 - an array of Vertex_rgba with array max num of elements = LINES3D_N_VERTICES_MAX
 or:
 - a pointer to a bigger, dynamically generated, array of Vertex_rgba

 So the vertex buffer is either a *static* one (static array) or a *dynamic* one (malloc3() generated)
 You can/should access it using sth like:
    buffer = dynamic_b ? vb_ptr : vb_array

Function Documentation

◆ lg_quad()

LG_Quad lg_quad ( vec3_t  v0,
vec3_t  v1,
vec3_t  v2,
vec3_t  v3,
LG_Color_u  c 
)

Return a new quad from 4 (QUAD_N_VERTICES) vec3_t vertices and a LG_Color_u

Vertex order is:

y ^
|
v[3]------v[2]
| |
| |
| |
v[0]------v[1]--->
x
Parameters
v0Vertex v[0] spatial coords as a vec3_t
v1Vertex v[1] spatial coords as a vec3_t
v2Vertex v[2] spatial coords as a vec3_t
v3Vertex v[3] spatial coords as a vec3_t
cColor as a LG_Color_u
Returns
A new LG_Quad

◆ lg_centered_unit_quad()

LG_Quad lg_centered_unit_quad ( LG_Color_u  c)

Return a new centered colored unit quad

Quad side length = 1.0

Quad is centered on origin (0.0, 0.0, 0.0)

Vertex order is:

y ^
|
v[3]------v[2]
| |
| |
| |
v[0]------v[1]--->
x
Parameters
cA LG_Color_u
Returns
A new LG_Quad

◆ lg_quad_transform()

LG_Quad lg_quad_transform ( LG_Quad  quad,
mat4_t  matrix 
)

Transform quad with STR matrix - original quad is not modified

Parameters
quadA LG_Quad
matrixA mat4_t matrix
Returns
A new LG_Quad

◆ lg_cuboid()

LG_Cuboid lg_cuboid ( vec3_t  v0,
vec3_t  v1,
vec3_t  v2,
vec3_t  v3,
vec3_t  v4,
vec3_t  v5,
vec3_t  v6,
vec3_t  v7,
LG_Color_u  c 
)

Return a new cuboid from 8 (CUBOID_N_VERTICES) vec3_t vertices and a LG_Color_u

Actually may return a non-cuboid 8 vertices shape - a bit strange

So, if necessary, you may test the new cuboid with lg_shape_is_cuboid()

Note: A cuboid with all square faces is a cube

Vertex order is:

y ^
|
| v[7]------v[6]
v[3]------v[2] |
| | | |
| | | |
| v[4]---|--v[5]
v[0]------v[1]------>
x
Parameters
v0Vertex v[0] spatial coords as a vec3_t
v1Vertex v[1] spatial coords as a vec3_t
v2Vertex v[2] spatial coords as a vec3_t
v3Vertex v[3] spatial coords as a vec3_t
v4Vertex v[4] spatial coords as a vec3_t
v5Vertex v[5] spatial coords as a vec3_t
v6Vertex v[6] spatial coords as a vec3_t
v7Vertex v[7] spatial coords as a vec3_t
colorColor as a LG_Color_u
Returns
A new LG_Cuboid

◆ lg_shape_is_cuboid()

zboolean lg_shape_is_cuboid ( LG_Cuboid shape)

Test if an 8 vertices shape is a cuboid

Only compute dot products at v[0] and v[6], not sure it's enough so need more testing

Vertex order is:

y ^
|
| v[7]------v[6]
v[3]------v[2] |
| | | |
| | | |
| v[4]---|--v[5]
v[0]------v[1]------>
x

◆ lg_centered_unit_cube()

LG_Cuboid lg_centered_unit_cube ( LG_Color_u  c)

Return a new centered axis-aligned colored unit cube

Cube side length = 1.0

Cube is centered on origin (0.0, 0.0, 0.0)

Vertex order is:

y ^
|
| v[7]------v[6]
v[3]------v[2] |
| | | |
| | | |
| v[4]---|--v[5]
v[0]------v[1]------>
x
Parameters
cA LG_Color_u
Returns
A new LG_Cuboid

◆ lg_cuboid_transform()

LG_Cuboid lg_cuboid_transform ( LG_Cuboid  cuboid,
mat4_t  matrix 
)

Transform cuboid with STR matrix - original cuboid is not modified

Parameters
cuboidA LG_Cuboid
matrixA mat4_t matrix
Returns
A new LG_Cuboid

◆ lg_xyz_arrows()

XYZ_Arrows lg_xyz_arrows ( float  k,
LG_Color_u  x_axis_color,
LG_Color_u  y_axis_color,
LG_Color_u  z_axis_color 
)

Return a new XYZ_Arrows

Colored XYZ arrows in OpenGL coord sys

 VB = all lines, with 2 vertices per line, so like:
 line0 = v0, v1, line1 = v1, v2, line2 = v2, v3, ...
 ->
 v0, v1, v1, v2, v2, v3, ...

 XYZ_ARROWS_LINES3D_N_VERTICES = 18 (in 3d_primitives.h)

Use default colors (red, green, blue) if all axis colors components set to 0 (r == 0 && g == 0 && b == 0 && a == 0)

Parameters
kScaling factor
x_axis_color
y_axis_color
z_axis_color
Returns
An array of Vertex_rgba

◆ lg_l3d_vb_from_quad()

Lines3D_VB lg_l3d_vb_from_quad ( LG_Quad  quad)

Create and fill a Lines3D_VB (line set vertex buffer and num of vertices) from a quad

 Draw with glDrawArrays(GL_LINE, 0, QUAD_LINES3D_N_VERTICES or n_vertices)
 (if n_vertices > 0)

The vertex buffer is a static one (static array)

y ^
|
v[3]------v[2]
| |
| |
| |
v[0]------v[1]--->
x
Parameters
quadA LG_Quad
Returns
A new Lines3D_VB - n_vertices set to 0 on error

◆ lg_l3d_vb_from_cuboid()

Lines3D_VB lg_l3d_vb_from_cuboid ( LG_Cuboid  cuboid)

Create and fill a Lines3D_VB (line set vertex buffer and num of vertices) from a cuboid

 Draw with glDrawArrays(GL_LINE, 0, CUBOID_LINES3D_N_VERTICES or n_vertices)
 (if n_vertices > 0)

The vertex buffer is a static one (static array)

y ^
|
| v[7]------v[6]
v[3]------v[2] |
| | | |
| | | |
| v[4]---|--v[5]
v[0]------v[1]------>
x

Then 0-4, 1-5, 2-6, 3-7

Parameters
cuboidA LG_Cuboid
Returns
A new Lines3D_VB - n_vertices set to 0 on error

◆ lg_l3d_vb_from_arrows()

Lines3D_VB lg_l3d_vb_from_arrows ( XYZ_Arrows  xyz_arrows)

Create and fill a Lines3D_VB (line set vertex buffer and num of vertices) from a XYZ_Arrows

 Draw with glDrawArrays(GL_LINE, 0, XYZ_ARROWS_LINES3D_N_VERTICES or n_vertices)
 (if n_vertices > 0)

The vertex buffer is a static one (static array)

Parameters
xyz_arrowsA XYZ_Arrows
Returns
A new Lines3D_VB - n_vertices set to 0 on error

◆ lg_cube_normalized_vertices_uv()

const Vertex_uv* lg_cube_normalized_vertices_uv ( )

=== From here on, MOSTLY DEPRECATED stuff - should cleanup/reorganize/rewrite some of following stuff, which is a bit of a mess ===

Get a static 3D cube's normalized vertices with uv texture coords

Returns
An array of Vertex_uv

◆ lg_cube_vertex_uv_indices()

const unsigned short* lg_cube_vertex_uv_indices ( )

Get a static 3D cube's indices

Returns
An array of unsigned short

◆ lg_sizeof_cube_normalized_vertices_uv()

size_t lg_sizeof_cube_normalized_vertices_uv ( )

Get size of 3D cube vertices

Returns
Size of cube vertices

◆ lg_sizeof_cube_vertex_uv_indices()

size_t lg_sizeof_cube_vertex_uv_indices ( )

Get size of 3D cube indices

Returns
Size of cube indices

◆ lg_cube_normalized_vertices_rgba()

const Vertex_rgba* lg_cube_normalized_vertices_rgba ( )

Get a static 3D cube's normalized vertices with RGBA colors

Returns
An array of Vertex_rgba

◆ lg_wired_cube_vertex_rgba_indices()

const unsigned short* lg_wired_cube_vertex_rgba_indices ( )

Get a static 3D cube's indices

Returns
An array of unsigned short

◆ lg_sizeof_cube_normalized_vertices_rgba()

size_t lg_sizeof_cube_normalized_vertices_rgba ( )

Get size of 3D cube vertices

Returns
Size of cube vertices

◆ lg_sizeof_wired_cube_vertex_rgba_indices()

size_t lg_sizeof_wired_cube_vertex_rgba_indices ( )

Get size of 3D cube indices

Returns
Size of cube indices