summaryrefslogtreecommitdiff
path: root/gfx/src/core/geometry.c
blob: cfc749f5bb63fe5bae86211057214425f61b9fe4 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "geometry.h"

#include "buffer.h"
#include "constants.h"

#include <gfx_assert.h>

#include <math/vec2.h>
#include <math/vec3.h>

/// Determines whether a view is populated.
///
/// Note that views are allowed to have no data, in which case a buffer of the
/// specified size is created.
#define view_is_populated(BUFFER_VIEW) (BUFFER_VIEW.size_bytes > 0)

static GLenum primitive_type_to_gl(PrimitiveType type) {
  switch (type) {
  case Triangles:
    return GL_TRIANGLES;
  case TriangleFan:
    return GL_TRIANGLE_FAN;
  case TriangleStrip:
    return GL_TRIANGLE_STRIP;
  }
  FAIL("primitive_type_to_gl(): missing case");
  return GL_INVALID_ENUM;
}

/// Create a typed buffer for the buffer view if the view does not already point
/// to a buffer.
void init_view_buffer(
    GfxCore* gfxcore, BufferView* view, BufferType buffer_type,
    BufferUsage buffer_usage) {
  if (!view->buffer) {
    view->buffer = gfx_make_buffer(
        gfxcore,
        &(BufferDesc){
            .usage      = buffer_usage,
            .type       = buffer_type,
            .data.data  = view->data,
            .data.count = view->size_bytes /
                          gfx_get_buffer_type_size_bytes(buffer_type)});
  }
  assert(view->size_bytes <= view->buffer->size_bytes);
}

/// Configure the buffer in teh VAO.
static void configure_buffer(
    GfxCore* gfxcore, const GeometryDesc* desc, BufferView* view,
    size_t num_components, size_t component_size_bytes, GLenum component_type,
    GLboolean normalized, GLuint channel) {
  assert(gfxcore);
  assert(desc);
  assert(view);
  assert(view->buffer);
  assert(
      desc->num_verts <=
      view->size_bytes / (num_components * component_size_bytes));
  assert(view->size_bytes <= view->buffer->size_bytes);

  glBindBuffer(GL_ARRAY_BUFFER, view->buffer->vbo);
  glEnableVertexAttribArray(channel);
  if ((component_type == GL_FLOAT) || normalized) {
    glVertexAttribPointer(
        channel, num_components, component_type, normalized, view->stride_bytes,
        (const void*)view->offset_bytes);
  } else {
    assert(!normalized);
    assert(
        (component_type == GL_BYTE) || (component_type == GL_UNSIGNED_BYTE) ||
        (component_type == GL_SHORT) || (component_type == GL_UNSIGNED_SHORT) ||
        (component_type == GL_INT) || component_type == GL_UNSIGNED_INT);
    glVertexAttribIPointer(
        channel, num_components, component_type, view->stride_bytes,
        (const void*)view->offset_bytes);
  }
  glBindBuffer(GL_ARRAY_BUFFER, 0);
}

static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) {
  assert(gfxcore);
  assert(desc);

  if (view_is_populated(desc->positions3d)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->positions3d, Buffer3d, desc->buffer_usage);
    if (!desc->positions3d.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->positions3d, 3, sizeof(float),
        GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL);
  } else if (view_is_populated(desc->positions2d)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->positions2d, Buffer2d, desc->buffer_usage);
    if (!desc->positions2d.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->positions2d, 2, sizeof(float),
        GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL);
  }
  if (view_is_populated(desc->normals)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->normals, Buffer3d, desc->buffer_usage);
    if (!desc->normals.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->normals, 3, sizeof(float), GL_FLOAT,
        GL_FALSE, GFX_NORMAL_CHANNEL);
  }
  if (view_is_populated(desc->tangents)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->tangents, Buffer4d, desc->buffer_usage);
    if (!desc->tangents.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->tangents, 4, sizeof(float), GL_FLOAT,
        GL_FALSE, GFX_TANGENT_CHANNEL);
  }
  if (view_is_populated(desc->texcoords)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->texcoords, Buffer2d, desc->buffer_usage);
    if (!desc->texcoords.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->texcoords, 2, sizeof(float),
        GL_FLOAT, GL_FALSE, GFX_TEXCOORDS_CHANNEL);
  }
  if (view_is_populated(desc->joints.u8)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->joints.u8, BufferU8, desc->buffer_usage);
    if (!desc->joints.u8.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->joints.u8, 4, sizeof(uint8_t),
        GL_UNSIGNED_BYTE, GL_FALSE, GFX_JOINTS_CHANNEL);
  } else if (view_is_populated(desc->joints.u16)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->joints.u16, BufferU16, desc->buffer_usage);
    if (!desc->joints.u16.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->joints.u16, 4, sizeof(uint16_t),
        GL_UNSIGNED_SHORT, GL_FALSE, GFX_JOINTS_CHANNEL);
  }

  // If weights are given as unsigned integers, then they are normalized
  // when read by the shader.
  if (view_is_populated(desc->weights.u8)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->weights.u8, BufferU8, desc->buffer_usage);
    if (!desc->weights.u8.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->weights.u8, 4, sizeof(uint8_t),
        GL_UNSIGNED_BYTE, GL_TRUE, GFX_WEIGHTS_CHANNEL);
  } else if (view_is_populated(desc->weights.u16)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->weights.u16, BufferU16,
        desc->buffer_usage);
    if (!desc->weights.u16.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->weights.u16, 4, sizeof(uint16_t),
        GL_UNSIGNED_SHORT, GL_TRUE, GFX_WEIGHTS_CHANNEL);
  } else if (view_is_populated(desc->weights.floats)) {
    init_view_buffer(
        gfxcore, (BufferView*)&desc->weights.floats, BufferFloat,
        desc->buffer_usage);
    if (!desc->weights.floats.buffer) {
      return false;
    }
    configure_buffer(
        gfxcore, desc, (BufferView*)&desc->weights.floats, 4, sizeof(float),
        GL_FLOAT, GL_FALSE, GFX_WEIGHTS_CHANNEL);
  }

  return true;
}

static bool configure_indices(GfxCore* gfxcore, GeometryDesc* desc) {
  assert(gfxcore);
  assert(desc);

  if (view_is_populated(desc->indices8)) {
    assert(desc->num_indices > 0);
    assert(
        desc->num_indices <= desc->indices8.size_bytes / sizeof(VertexIndex8));
    init_view_buffer(
        gfxcore, (BufferView*)&desc->indices8, BufferU8, desc->buffer_usage);
    if (!desc->indices8.buffer) {
      return false;
    }
  } else if (view_is_populated(desc->indices16)) {
    assert(desc->num_indices > 0);
    assert(
        desc->num_indices <=
        desc->indices16.size_bytes / sizeof(VertexIndex16));
    init_view_buffer(
        gfxcore, (BufferView*)&desc->indices16, BufferU16, desc->buffer_usage);
    if (!desc->indices16.buffer) {
      return false;
    }
  }

  return true;
}

bool gfx_init_geometry(
    Geometry* geometry, GfxCore* gfxcore, const GeometryDesc* input_desc) {
  assert(geometry);
  assert(gfxcore);
  assert(input_desc);
  assert(
      view_is_populated(input_desc->positions3d) ||
      view_is_populated(input_desc->positions2d));
  assert(input_desc->num_verts > 0);

  geometry->mode      = primitive_type_to_gl(input_desc->type);
  geometry->desc      = *input_desc;
  geometry->num_verts = input_desc->num_verts;
  geometry->gfxcore   = gfxcore;

  // The geometry's copy of the descriptor is manipulated below. Create a
  // shorter name for it.
  GeometryDesc* desc = &geometry->desc;

  glGenVertexArrays(1, &geometry->vao);
  glBindVertexArray(geometry->vao);
  if (!configure_vertex_attributes(gfxcore, desc)) {
    goto cleanup;
  }
  if (!configure_indices(gfxcore, desc)) {
    goto cleanup;
  }
  glBindVertexArray(0);
  ASSERT_GL;

  return true;

cleanup:
  gfx_del_geometry(geometry);
  return 0;
}

void gfx_del_geometry(Geometry* geometry) {
  assert(geometry);
  if (geometry->vao) {
    glDeleteVertexArrays(1, &geometry->vao);
    geometry->vao = 0;
  }
}

void gfx_update_geometry(Geometry* geometry, const GeometryDesc* desc) {
  assert(geometry);
  assert(desc);
  // New geometry size cannot exceed original size.
  assert(desc->positions3d.size_bytes <= geometry->desc.positions3d.size_bytes);
  assert(desc->positions2d.size_bytes <= geometry->desc.positions2d.size_bytes);
  assert(desc->normals.size_bytes <= geometry->desc.normals.size_bytes);
  assert(desc->tangents.size_bytes <= geometry->desc.tangents.size_bytes);
  assert(desc->texcoords.size_bytes <= geometry->desc.texcoords.size_bytes);
  assert(desc->joints.u8.size_bytes <= geometry->desc.joints.u8.size_bytes);
  assert(desc->joints.u16.size_bytes <= geometry->desc.joints.u16.size_bytes);
  assert(desc->weights.u8.size_bytes <= geometry->desc.weights.u8.size_bytes);
  assert(desc->weights.u16.size_bytes <= geometry->desc.weights.u16.size_bytes);
  assert(
      desc->weights.floats.size_bytes <=
      geometry->desc.weights.floats.size_bytes);

  if (desc->positions3d.data) {
    // The geometry must already have an underlying GPU buffer.
    assert(geometry->desc.positions3d.buffer);
    gfx_update_buffer(
        geometry->desc.positions3d.buffer,
        &(BufferDataDesc){
            .vec3s = desc->positions3d.data,
            .count = desc->positions3d.size_bytes / sizeof(vec3)});
  }
  // TODO: more
  else {
    FAIL("TODO: gfx_update_geometry() - handle other buffer types");
  }

  if (desc->num_verts != 0) {
    geometry->num_verts = desc->num_verts;
  }
}

void gfx_render_geometry(const Geometry* geometry) {
  assert(geometry);
  assert(geometry->vao);

  const GeometryDesc* desc = &geometry->desc;
  glBindVertexArray(geometry->vao);

  if (desc->indices8.buffer) {
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, desc->indices8.buffer->vbo);
    glDrawElements(
        geometry->mode, desc->num_indices, GL_UNSIGNED_BYTE,
        (const void*)desc->indices8.offset_bytes);
  } else if (desc->indices16.buffer) {
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, desc->indices16.buffer->vbo);
    glDrawElements(
        geometry->mode, desc->num_indices, GL_UNSIGNED_SHORT,
        (const void*)desc->indices16.offset_bytes);
  } else {
    glDrawArrays(geometry->mode, 0, geometry->num_verts);
  }

  glBindVertexArray(0);
}

aabb3 gfx_get_geometry_aabb(const Geometry* geometry) {
  assert(geometry);
  return geometry->desc.aabb;
}