summaryrefslogtreecommitdiff
path: root/src/maths/primitives.c
blob: 459a535301a0b9ae848f6c31b6e85a77753b3ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "primitives.h"
#include "maths.h"
#include "ral_types.h"

// vertices
f32 plane_vertex_positions[] = {
  // triangle 1
  -0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, -0.5,
  // triangle 2
  0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5
};

geometry_data geo_create_plane(f32x2 extents) {
  f32x2 half_extents = vec2_div(extents, 2.0);
  vertex_format format = VERTEX_STATIC_3D;
  vertex_darray* vertices = vertex_darray_new(4);

  //   vertex_darray_push(vertices, (vertex){ .static_3d = { .position = } });

  //   return (geometry_data) { .format = format, .vertices =.has_indices = true, }
}

// OLD

static const vec3 BACK_BOT_LEFT = (vec3){ 0, 0, 0 };
static const vec3 BACK_BOT_RIGHT = (vec3){ 1, 0, 0 };
static const vec3 BACK_TOP_LEFT = (vec3){ 0, 1, 0 };
static const vec3 BACK_TOP_RIGHT = (vec3){ 1, 1, 0 };
static const vec3 FRONT_BOT_LEFT = (vec3){ 0, 0, 1 };
static const vec3 FRONT_BOT_RIGHT = (vec3){ 1, 0, 1 };
static const vec3 FRONT_TOP_LEFT = (vec3){ 0, 1, 1 };
static const vec3 FRONT_TOP_RIGHT = (vec3){ 1, 1, 1 };

#define VERT_3D(arr, pos, norm, uv) \
  { \
    vertex v = {
      .static_3d = { \
      .position = pos, \
      .normal = norm, \
      .tex_coords = uv \
    }}; \
    vertex_darray_push(arr, v); \
  } 

static mesh prim_cube_mesh_create() {
  mesh cube = { 0 };
  cube.vertices = vertex_darray_new(36);

    // back faces
    VERT_3D(cube.vertices, BACK_BOT_LEFT, VEC3_NEG_Z, (vec2){ 0, 1 }))
    // vertex_darray_push(
    //     cube.vertices,
    //     (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });
    // vertex_darray_push(
    //     cube.vertices,
    //     (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 0 } });
    // vertex_darray_push(
    //     cube.vertices,
    //     (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
    // vertex_darray_push(
    //     cube.vertices,
    //     (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
    // vertex_darray_push(
    //     cube.vertices,
    //     (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 1 } });
    // vertex_darray_push(
    //     cube.vertices,
    //     (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });

    // front faces
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 1 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });

    // top faces
    vertex_darray_push(cube.vertices,
                       (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0
                       } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 1 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
    vertex_darray_push(cube.vertices,
                       (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0
                       } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 0 } });

    // bottom faces
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });

    // right faces
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 1 } });

    // left faces
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
    vertex_darray_push(
        cube.vertices,
        (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });

    cube.indices_len = cube.vertices->len;
    cube.indices = malloc(sizeof(u32) * cube.indices_len);

    for (u32 i = 0; i < cube.indices_len; i++) {
      cube.indices[i] = i;
    }

  cube.has_indices = true;

  return cube;
}

/** @brief create a new model with the shape of a cube */
static model_handle prim_cube_new(core* core) {
  model model = { 0 };
  mesh cube = prim_cube_mesh_create();

  mesh_darray_push(model.meshes, cube);
  assert(mesh_darray_len(model.meshes) == 1);

  u32 index = (u32)model_darray_len(core->models);
  model_darray_push_copy(core->models, &model);
  return (model_handle){ .raw = index };
}

// --- Spheres

geometry_data geo_create_uvsphere(f32 radius, f32 north_south_lines, f32 east_west_lines) {
  // TODO
}