diff options
| author | 3gg <3gg@shellblade.net> | 2025-11-02 15:57:16 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-11-02 15:57:16 -0800 |
| commit | d7220ee51c59cd3e51927f2f5e0388c8573f1792 (patch) | |
| tree | d949b88768f09e06f9456a8028ae89ef53e8a091 /src/core/geometry.c | |
| parent | d3bfccdc4f90aabfa3493b0db0e6fe357a527485 (diff) | |
Fix view/accessor offset and invariants; fix loading of Flight Helmet
Diffstat (limited to 'src/core/geometry.c')
| -rw-r--r-- | src/core/geometry.c | 73 |
1 files changed, 34 insertions, 39 deletions
diff --git a/src/core/geometry.c b/src/core/geometry.c index cfc749f..488dc23 100644 --- a/src/core/geometry.c +++ b/src/core/geometry.c | |||
| @@ -12,7 +12,7 @@ | |||
| 12 | /// | 12 | /// |
| 13 | /// Note that views are allowed to have no data, in which case a buffer of the | 13 | /// Note that views are allowed to have no data, in which case a buffer of the |
| 14 | /// specified size is created. | 14 | /// specified size is created. |
| 15 | #define view_is_populated(BUFFER_VIEW) (BUFFER_VIEW.size_bytes > 0) | 15 | #define view_is_populated(BUFFER_VIEW) (BUFFER_VIEW.count > 0) |
| 16 | 16 | ||
| 17 | static GLenum primitive_type_to_gl(PrimitiveType type) { | 17 | static GLenum primitive_type_to_gl(PrimitiveType type) { |
| 18 | switch (type) { | 18 | switch (type) { |
| @@ -34,30 +34,25 @@ void init_view_buffer( | |||
| 34 | BufferUsage buffer_usage) { | 34 | BufferUsage buffer_usage) { |
| 35 | if (!view->buffer) { | 35 | if (!view->buffer) { |
| 36 | view->buffer = gfx_make_buffer( | 36 | view->buffer = gfx_make_buffer( |
| 37 | gfxcore, | 37 | gfxcore, &(BufferDesc){.usage = buffer_usage, |
| 38 | &(BufferDesc){ | 38 | .type = buffer_type, |
| 39 | .usage = buffer_usage, | 39 | .data.data = view->data, |
| 40 | .type = buffer_type, | 40 | .data.count = view->count}); |
| 41 | .data.data = view->data, | 41 | assert(view->buffer); |
| 42 | .data.count = view->size_bytes / | ||
| 43 | gfx_get_buffer_type_size_bytes(buffer_type)}); | ||
| 44 | } | 42 | } |
| 45 | assert(view->size_bytes <= view->buffer->size_bytes); | ||
| 46 | } | 43 | } |
| 47 | 44 | ||
| 48 | /// Configure the buffer in teh VAO. | 45 | /// Configure the buffer in the VAO. |
| 49 | static void configure_buffer( | 46 | static void configure_buffer( |
| 50 | GfxCore* gfxcore, const GeometryDesc* desc, BufferView* view, | 47 | GfxCore* gfxcore, const GeometryDesc* desc, BufferView* view, |
| 51 | size_t num_components, size_t component_size_bytes, GLenum component_type, | 48 | size_t num_components, GLenum component_type, GLboolean normalized, |
| 52 | GLboolean normalized, GLuint channel) { | 49 | GLuint channel) { |
| 53 | assert(gfxcore); | 50 | assert(gfxcore); |
| 54 | assert(desc); | 51 | assert(desc); |
| 55 | assert(view); | 52 | assert(view); |
| 56 | assert(view->buffer); | 53 | assert(view->buffer); |
| 57 | assert( | 54 | assert(view->count == desc->num_verts); |
| 58 | desc->num_verts <= | 55 | assert((view->offset_bytes + view->size_bytes) <= view->buffer->size_bytes); |
| 59 | view->size_bytes / (num_components * component_size_bytes)); | ||
| 60 | assert(view->size_bytes <= view->buffer->size_bytes); | ||
| 61 | 56 | ||
| 62 | glBindBuffer(GL_ARRAY_BUFFER, view->buffer->vbo); | 57 | glBindBuffer(GL_ARRAY_BUFFER, view->buffer->vbo); |
| 63 | glEnableVertexAttribArray(channel); | 58 | glEnableVertexAttribArray(channel); |
| @@ -89,8 +84,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 89 | return false; | 84 | return false; |
| 90 | } | 85 | } |
| 91 | configure_buffer( | 86 | configure_buffer( |
| 92 | gfxcore, desc, (BufferView*)&desc->positions3d, 3, sizeof(float), | 87 | gfxcore, desc, (BufferView*)&desc->positions3d, 3, GL_FLOAT, GL_FALSE, |
| 93 | GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL); | 88 | GFX_POSITION_CHANNEL); |
| 94 | } else if (view_is_populated(desc->positions2d)) { | 89 | } else if (view_is_populated(desc->positions2d)) { |
| 95 | init_view_buffer( | 90 | init_view_buffer( |
| 96 | gfxcore, (BufferView*)&desc->positions2d, Buffer2d, desc->buffer_usage); | 91 | gfxcore, (BufferView*)&desc->positions2d, Buffer2d, desc->buffer_usage); |
| @@ -98,8 +93,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 98 | return false; | 93 | return false; |
| 99 | } | 94 | } |
| 100 | configure_buffer( | 95 | configure_buffer( |
| 101 | gfxcore, desc, (BufferView*)&desc->positions2d, 2, sizeof(float), | 96 | gfxcore, desc, (BufferView*)&desc->positions2d, 2, GL_FLOAT, GL_FALSE, |
| 102 | GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL); | 97 | GFX_POSITION_CHANNEL); |
| 103 | } | 98 | } |
| 104 | if (view_is_populated(desc->normals)) { | 99 | if (view_is_populated(desc->normals)) { |
| 105 | init_view_buffer( | 100 | init_view_buffer( |
| @@ -108,8 +103,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 108 | return false; | 103 | return false; |
| 109 | } | 104 | } |
| 110 | configure_buffer( | 105 | configure_buffer( |
| 111 | gfxcore, desc, (BufferView*)&desc->normals, 3, sizeof(float), GL_FLOAT, | 106 | gfxcore, desc, (BufferView*)&desc->normals, 3, GL_FLOAT, GL_FALSE, |
| 112 | GL_FALSE, GFX_NORMAL_CHANNEL); | 107 | GFX_NORMAL_CHANNEL); |
| 113 | } | 108 | } |
| 114 | if (view_is_populated(desc->tangents)) { | 109 | if (view_is_populated(desc->tangents)) { |
| 115 | init_view_buffer( | 110 | init_view_buffer( |
| @@ -118,8 +113,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 118 | return false; | 113 | return false; |
| 119 | } | 114 | } |
| 120 | configure_buffer( | 115 | configure_buffer( |
| 121 | gfxcore, desc, (BufferView*)&desc->tangents, 4, sizeof(float), GL_FLOAT, | 116 | gfxcore, desc, (BufferView*)&desc->tangents, 4, GL_FLOAT, GL_FALSE, |
| 122 | GL_FALSE, GFX_TANGENT_CHANNEL); | 117 | GFX_TANGENT_CHANNEL); |
| 123 | } | 118 | } |
| 124 | if (view_is_populated(desc->texcoords)) { | 119 | if (view_is_populated(desc->texcoords)) { |
| 125 | init_view_buffer( | 120 | init_view_buffer( |
| @@ -128,8 +123,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 128 | return false; | 123 | return false; |
| 129 | } | 124 | } |
| 130 | configure_buffer( | 125 | configure_buffer( |
| 131 | gfxcore, desc, (BufferView*)&desc->texcoords, 2, sizeof(float), | 126 | gfxcore, desc, (BufferView*)&desc->texcoords, 2, GL_FLOAT, GL_FALSE, |
| 132 | GL_FLOAT, GL_FALSE, GFX_TEXCOORDS_CHANNEL); | 127 | GFX_TEXCOORDS_CHANNEL); |
| 133 | } | 128 | } |
| 134 | if (view_is_populated(desc->joints.u8)) { | 129 | if (view_is_populated(desc->joints.u8)) { |
| 135 | init_view_buffer( | 130 | init_view_buffer( |
| @@ -138,8 +133,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 138 | return false; | 133 | return false; |
| 139 | } | 134 | } |
| 140 | configure_buffer( | 135 | configure_buffer( |
| 141 | gfxcore, desc, (BufferView*)&desc->joints.u8, 4, sizeof(uint8_t), | 136 | gfxcore, desc, (BufferView*)&desc->joints.u8, 4, GL_UNSIGNED_BYTE, |
| 142 | GL_UNSIGNED_BYTE, GL_FALSE, GFX_JOINTS_CHANNEL); | 137 | GL_FALSE, GFX_JOINTS_CHANNEL); |
| 143 | } else if (view_is_populated(desc->joints.u16)) { | 138 | } else if (view_is_populated(desc->joints.u16)) { |
| 144 | init_view_buffer( | 139 | init_view_buffer( |
| 145 | gfxcore, (BufferView*)&desc->joints.u16, BufferU16, desc->buffer_usage); | 140 | gfxcore, (BufferView*)&desc->joints.u16, BufferU16, desc->buffer_usage); |
| @@ -147,8 +142,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 147 | return false; | 142 | return false; |
| 148 | } | 143 | } |
| 149 | configure_buffer( | 144 | configure_buffer( |
| 150 | gfxcore, desc, (BufferView*)&desc->joints.u16, 4, sizeof(uint16_t), | 145 | gfxcore, desc, (BufferView*)&desc->joints.u16, 4, GL_UNSIGNED_SHORT, |
| 151 | GL_UNSIGNED_SHORT, GL_FALSE, GFX_JOINTS_CHANNEL); | 146 | GL_FALSE, GFX_JOINTS_CHANNEL); |
| 152 | } | 147 | } |
| 153 | 148 | ||
| 154 | // If weights are given as unsigned integers, then they are normalized | 149 | // If weights are given as unsigned integers, then they are normalized |
| @@ -160,8 +155,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 160 | return false; | 155 | return false; |
| 161 | } | 156 | } |
| 162 | configure_buffer( | 157 | configure_buffer( |
| 163 | gfxcore, desc, (BufferView*)&desc->weights.u8, 4, sizeof(uint8_t), | 158 | gfxcore, desc, (BufferView*)&desc->weights.u8, 4, GL_UNSIGNED_BYTE, |
| 164 | GL_UNSIGNED_BYTE, GL_TRUE, GFX_WEIGHTS_CHANNEL); | 159 | GL_TRUE, GFX_WEIGHTS_CHANNEL); |
| 165 | } else if (view_is_populated(desc->weights.u16)) { | 160 | } else if (view_is_populated(desc->weights.u16)) { |
| 166 | init_view_buffer( | 161 | init_view_buffer( |
| 167 | gfxcore, (BufferView*)&desc->weights.u16, BufferU16, | 162 | gfxcore, (BufferView*)&desc->weights.u16, BufferU16, |
| @@ -170,8 +165,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 170 | return false; | 165 | return false; |
| 171 | } | 166 | } |
| 172 | configure_buffer( | 167 | configure_buffer( |
| 173 | gfxcore, desc, (BufferView*)&desc->weights.u16, 4, sizeof(uint16_t), | 168 | gfxcore, desc, (BufferView*)&desc->weights.u16, 4, GL_UNSIGNED_SHORT, |
| 174 | GL_UNSIGNED_SHORT, GL_TRUE, GFX_WEIGHTS_CHANNEL); | 169 | GL_TRUE, GFX_WEIGHTS_CHANNEL); |
| 175 | } else if (view_is_populated(desc->weights.floats)) { | 170 | } else if (view_is_populated(desc->weights.floats)) { |
| 176 | init_view_buffer( | 171 | init_view_buffer( |
| 177 | gfxcore, (BufferView*)&desc->weights.floats, BufferFloat, | 172 | gfxcore, (BufferView*)&desc->weights.floats, BufferFloat, |
| @@ -180,8 +175,8 @@ static bool configure_vertex_attributes(GfxCore* gfxcore, GeometryDesc* desc) { | |||
| 180 | return false; | 175 | return false; |
| 181 | } | 176 | } |
| 182 | configure_buffer( | 177 | configure_buffer( |
| 183 | gfxcore, desc, (BufferView*)&desc->weights.floats, 4, sizeof(float), | 178 | gfxcore, desc, (BufferView*)&desc->weights.floats, 4, GL_FLOAT, |
| 184 | GL_FLOAT, GL_FALSE, GFX_WEIGHTS_CHANNEL); | 179 | GL_FALSE, GFX_WEIGHTS_CHANNEL); |
| 185 | } | 180 | } |
| 186 | 181 | ||
| 187 | return true; | 182 | return true; |
| @@ -282,9 +277,9 @@ void gfx_update_geometry(Geometry* geometry, const GeometryDesc* desc) { | |||
| 282 | assert(geometry->desc.positions3d.buffer); | 277 | assert(geometry->desc.positions3d.buffer); |
| 283 | gfx_update_buffer( | 278 | gfx_update_buffer( |
| 284 | geometry->desc.positions3d.buffer, | 279 | geometry->desc.positions3d.buffer, |
| 285 | &(BufferDataDesc){ | 280 | &(BufferDataDesc){.vec3s = desc->positions3d.data, |
| 286 | .vec3s = desc->positions3d.data, | 281 | .count = |
| 287 | .count = desc->positions3d.size_bytes / sizeof(vec3)}); | 282 | desc->positions3d.size_bytes / sizeof(vec3)}); |
| 288 | } | 283 | } |
| 289 | // TODO: more | 284 | // TODO: more |
| 290 | else { | 285 | else { |
