polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 06b8d7c47c8baff8c56a9240b30c421e06f7c607
parent 0a037b5a71c19e9371aa805c3658074254af0172
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 29 Jun 2019 01:34:11 -0700

fix crashes

Diffstat:
Msrc/geometry.c | 11++++++-----
Msrc/ply.c | 9++++++---
Msrc/render.c | 4+++-
Msrc/resource.c | 2+-
Msrc/vbo.c | 15++++++++-------
5 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/src/geometry.c b/src/geometry.c @@ -34,9 +34,10 @@ void bind_geometry(struct geometry *geom, gpu_addr *vertex_attrs) { struct vbo *vbo; for (int i = 0; i < n_vertex_attrs; i++) { vbo = &geom->vbos[i]; - if (vbo->handle && vertex_attrs[i] != 0xFFFFFFFF) + if (vbo->handle && vertex_attrs[i] != 0xFFFFFFFF) { bind_vbo(vbo, vertex_attrs[i], vbo->component_type); - check_gl(); + check_gl(); + } } } @@ -58,7 +59,6 @@ void render_geometry(struct geometry *geom, gpu_addr *vertex_attrs, //check_for_patches(program, &type); bind_geometry(geom, vertex_attrs); if (geom->num_indices) { - debug("geom->num_indices %d\n", geom->num_indices); glDrawElements(type, geom->num_indices, /* count */ GL_UNSIGNED_INT, /* type */ @@ -106,6 +106,7 @@ make_buffer_geometry_(struct make_geometry *mkgeom, struct geometry *geom) { geom->num_verts = mkgeom->num_verts; geom->num_indices = mkgeom->num_indices; + assert(mkgeom->num_verts); assert(mkgeom->vertices); /* assert(geom->normals); */ /* assert(geom->indices); */ @@ -120,13 +121,13 @@ make_buffer_geometry_(struct make_geometry *mkgeom, struct geometry *geom) { /* printf("making normal buffer\n"); */ // cube normals - if (mkgeom->normals) + if (mkgeom->normals) { make_float_vertex_buffer(&geom->vbos[va_normal], mkgeom->normals, mk_num_elements(mkgeom->num_verts), mk_components(3) ); - + } if (mkgeom->joint_ids) { make_int_vertex_buffer(&geom->vbos[va_joint_ids], mkgeom->joint_ids, diff --git a/src/ply.c b/src/ply.c @@ -226,13 +226,16 @@ int parse_ply(const char *filename, geometry_id *geom_id) { free((void*)data); if (success) { + struct geometry *geom = new_geometry(geom_id); + assert(geom); + + mkgeom.num_indices = ninds * 3; + mkgeom.num_verts = nverts * 3; + make_buffer_geometry(&mkgeom, geom_id); - struct geometry *geom = get_geometry(geom_id); vec3_copy(min, geom->min); vec3_copy(max, geom->max); - geom->num_indices = ninds * 3; - geom->num_verts = nverts * 3; free(mkgeom.vertices); free(mkgeom.normals); diff --git a/src/render.c b/src/render.c @@ -346,7 +346,9 @@ void render (struct game *game, struct render_config *config) { recalc_normals(res->uniforms.normal_matrix, model_view, normal_matrix); check_gl(); - render_geometry(get_geometry(&entity->model->geom_id), res->vertex_attrs, current_program); + + struct geometry *geo = get_geometry(&entity->model->geom_id); + render_geometry(geo, res->vertex_attrs, current_program); check_gl(); } diff --git a/src/resource.c b/src/resource.c @@ -11,7 +11,7 @@ static u64 resource_uuids = 0; static inline void *index_resource(struct resource_manager *r, int i) { unsigned char *p = r->resources; - return &p[i * r->elem_size]; + return p + (i * r->elem_size); } void *get_all_resources(struct resource_manager *r, u32 *count, struct resource_id **ids) { diff --git a/src/vbo.c b/src/vbo.c @@ -1,6 +1,7 @@ #include "vbo.h" #include "util.h" +#include "debug.h" #include <assert.h> #include "gl.h" @@ -13,7 +14,9 @@ make_buffer(GLenum target, const void *buffer_data, GLsizei buffer_size) { glBindBuffer(target, buffer); check_gl(); + assert(buffer_size > 0); + debug("buffer data %d\n", buffer_size); glBufferData(target, buffer_size, buffer_data, GL_STATIC_DRAW); check_gl(); @@ -44,14 +47,12 @@ struct vbo* make_index_buffer(struct vbo *vbo, const void *data, struct num_elements num_elements) { - return make_vertex_buffer(vbo, + vbo->handle = make_buffer(GL_ELEMENT_ARRAY_BUFFER, data, - GL_ELEMENT_ARRAY_BUFFER, - GL_INT, - mk_element_size(sizeof(u32)), - num_elements, - mk_components(3) - ); + num_elements.num_elements * sizeof(u32)); + vbo->type = GL_ELEMENT_ARRAY_BUFFER; + vbo->component_type = GL_INT; + return vbo; } struct vbo* make_float_vertex_buffer(struct vbo *vbo, const void *data,