commit 153122c760c01517bb4eb8dc7793d11a2f3d38f7
parent 4ab2c240025aef85491b3f4d7a2864ce32db9d10
Author: William Casarin <jb55@jb55.com>
Date: Sun, 30 Jun 2019 20:48:06 -0700
procmesh working again as dynamic model
Diffstat:
17 files changed, 61 insertions(+), 42 deletions(-)
diff --git a/src/entity.c b/src/entity.c
@@ -20,6 +20,7 @@ struct entity *get_all_entities(u32 *count, entity_id **ids) {
struct entity *init_entity(struct entity *ent, node_id *id) {
node_id new_id;
+ init_model_id(&ent->model_id);
if (id == NULL) {
init_id(&new_id);
new_node(&new_id);
diff --git a/src/game.c b/src/game.c
@@ -149,14 +149,13 @@ void game_init(struct game *game, int width, int height) {
/* player->model_id = get_static_model(model_pirate_officer, NULL); */
struct model *pmodel = new_model(&player->model_id); assert(pmodel);
+ struct geometry *geom = get_geometry(&pmodel->geom_id); assert(geom);
proc_sphere(geom);
+ pmodel->shading = SHADING_VERT_COLOR;
- ok = load_model(&player->model, "pirate-officer");
- assert(ok);
node_set_label(pnode, "player");
node_attach(&player->node_id, &res->root_id);
-
assert(ideq(&pnode->parent_id, &res->root_id));
node_translate(pnode, V3(terrain->size/2.,terrain->size/2.,0.0));
diff --git a/src/geometry.c b/src/geometry.c
@@ -97,9 +97,9 @@ void init_geometry(struct geometry *geom) {
geom->num_uv_components = 2;
}
-void
-make_buffer_geometry_(struct make_geometry *mkgeom, struct geometry *geom) {
-
+void make_buffer_geometry(struct make_geometry *mkgeom, struct geometry *geom)
+{
+ init_geometry(geom);
// VBOs
geom->num_uv_components = mkgeom->num_uv_components;
@@ -166,15 +166,6 @@ make_buffer_geometry_(struct make_geometry *mkgeom, struct geometry *geom) {
geom->has_vbos = 1;
}
-void
-make_buffer_geometry(struct make_geometry *mkgeom, geometry_id *geom_id)
-{
- init_id(geom_id);
- struct geometry *geom = new_geometry(geom_id);
-
- make_buffer_geometry_(mkgeom, geom);
-}
-
void geometry_centroid(struct geometry *geom, float *dest) {
vec3_subtract(geom->max, geom->min, dest);
diff --git a/src/geometry.h b/src/geometry.h
@@ -50,7 +50,7 @@ void render_geometry(struct geometry *geom, gpu_addr *vertex_attrs,
void bind_geometry(struct geometry *geom, gpu_addr *vertex_attrs);
void init_geometry(struct geometry *geom);
void init_make_geometry(struct make_geometry *mkgeom);
-void make_buffer_geometry(struct make_geometry *mkgeom, geometry_id *);
+void make_buffer_geometry(struct make_geometry *mkgeom, struct geometry *geom);
void destroy_buffer_geometry(geometry_id *geom);
void geometry_centroid(struct geometry *geom, float *v3);
void init_geometry_manager();
diff --git a/src/model.c b/src/model.c
@@ -16,6 +16,13 @@ static struct resource_manager dyn_modelman;
static int static_models_initialized = 0;
+void init_model_id(model_id *id)
+{
+ assert((int)id->dyn_model_id.index != -1);
+ id->type = DYNAMIC_MODEL;
+ init_id(&id->dyn_model_id);
+}
+
struct model *init_model(struct model *model) {
init_id(&model->geom_id);
model->shading = SHADING_VERT_COLOR;
@@ -39,6 +46,7 @@ static inline struct model *new_uninitialized_model(struct resource_id *id) {
static struct model *new_model_resource(model_id *model_id)
{
struct model *model = new_uninitialized_model(&model_id->dyn_model_id);
+ init_id(&model->geom_id);
new_geometry(&model->geom_id);
return model;
}
diff --git a/src/model.h b/src/model.h
@@ -42,7 +42,7 @@ enum shading {
struct model {
/* geometry_id geom_id; */
- struct geometry geom;
+ geometry_id geom_id;
enum shading shading;
u32 texture;
};
@@ -56,8 +56,10 @@ struct model_def {
void init_model_manager();
+void init_model_id(model_id *id);
struct model *init_model(struct model *model);
struct model *get_model(model_id *);
+struct model *get_model_geom(model_id *, struct geometry **);
struct model *new_model(model_id *);
struct model *new_static_model(model_id *);
diff --git a/src/ply.c b/src/ply.c
@@ -232,7 +232,7 @@ int parse_ply(const char *filename, geometry_id *geom_id) {
mkgeom.num_indices = ninds * 3;
mkgeom.num_verts = nverts * 3;
- make_buffer_geometry(&mkgeom, geom_id);
+ make_buffer_geometry(&mkgeom, geom);
vec3_copy(min, geom->min);
vec3_copy(max, geom->max);
diff --git a/src/procmesh.c b/src/procmesh.c
@@ -28,8 +28,10 @@ static void qh_mesh_to_geom(qh_mesh_t *qh, struct make_geometry *geom) {
}
-void proc_sphere(geometry_id *geom_id) {
+void proc_sphere(struct geometry *geom) {
struct make_geometry mkgeom;
+ init_make_geometry(&mkgeom);
+
const int n = 50;
qh_vertex_t *vertices = malloc(n*sizeof(qh_vertex_t));
const float radius = 2.0;
@@ -45,7 +47,7 @@ void proc_sphere(geometry_id *geom_id) {
qh_mesh_t mesh = qh_quickhull3d(vertices, n);
qh_mesh_to_geom(&mesh, &mkgeom);
- make_buffer_geometry(&mkgeom, geom_id);
+ make_buffer_geometry(&mkgeom, geom);
free(mkgeom.normals);
qh_free_mesh(mesh);
diff --git a/src/procmesh.h b/src/procmesh.h
@@ -4,6 +4,6 @@
#include "geometry.h"
-void proc_sphere(struct make_geometry *mkgeom, geometry_id *geom_id);
+void proc_sphere(struct geometry *geom);
#endif /* PROCMESH_H */
diff --git a/src/render.c b/src/render.c
@@ -293,7 +293,10 @@ void render (struct game *game, struct render_config *config) {
mat4_inverse((float *)camera, view);
mat4_multiply(projection, view, view_proj);
- glBindTexture(GL_TEXTURE_CUBE_MAP, res->skybox.model.texture);
+ struct model *skybox_model = get_model(&res->skybox.model_id);
+ assert(skybox_model);
+
+ glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_model->texture);
check_gl();
for (u32 i = 0; i < num_entities; ++i) {
diff --git a/src/scene.c b/src/scene.c
@@ -30,7 +30,7 @@ void default_scene(struct game *game) {
struct node *pnode = get_node(&player->node_id);
assert(tnode);
- tower->model_id = get_static_model_id(model_tower);
+ tower->model_id = get_static_model(model_tower, NULL);
node_set_label(tnode, "tower");
node_attach(&tower->node_id, &player->node_id);
node_translate(tnode, V3(0.0, 50.0, 0.0));
@@ -43,6 +43,6 @@ void default_scene(struct game *game) {
}
-void pbr_scene(struct game *game) {
- struct model *sphere = get_model(model_icosphere);
-}
+/* void pbr_scene(struct game *game) { */
+/* struct model *sphere = get_static_model(model_icosphere, NULL); */
+/* } */
diff --git a/src/skybox.c b/src/skybox.c
@@ -39,7 +39,7 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) {
int ok;
node_init(&skybox->node);
- init_model(&skybox->model);
+ init_model_id(&skybox->model_id);
skybox->program = program;
@@ -54,7 +54,10 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) {
mkgeom.num_verts = ARRAY_SIZE(skybox_vertices);
mkgeom.num_indices = ARRAY_SIZE(skybox_indices);
- make_buffer_geometry(&mkgeom, &skybox->model.geom_id);
+ struct model *model = new_model(&skybox->model_id); assert(model);
+ struct geometry *geom = get_geometry(&model->geom_id); assert(geom);
+
+ make_buffer_geometry(&mkgeom, geom);
static const char *faces[6] = {
CUBEMAP("hw_sahara/sahara_rt_flip.tga"),
@@ -75,7 +78,7 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) {
/* CUBEMAP("ame_siege/siege_dn_flip.tga"), */
/* }; */
- skybox->model.texture = create_cubemap(faces);
+ model->texture = create_cubemap(faces);
make_shader(GL_VERTEX_SHADER, SHADER("skybox.v.glsl"), &vertex);
check_gl();
@@ -98,6 +101,10 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) {
void render_skybox(struct skybox *skybox, mat4 *camera) {
+
+ struct model *model = get_model(&skybox->model_id); assert(model);
+ struct geometry *geom = get_geometry(&model->geom_id); assert(geom);
+
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);
check_gl();
@@ -108,10 +115,10 @@ void render_skybox(struct skybox *skybox, mat4 *camera) {
glUniformMatrix4fv(skybox->uniforms.mvp, 1, 0, camera);
check_gl();
- glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->model.texture);
+ glBindTexture(GL_TEXTURE_CUBE_MAP, model->texture);
check_gl();
- render_geometry(get_geometry(&skybox->model.geom_id), skybox->attrs, skybox->program);
+ render_geometry(geom, skybox->attrs, skybox->program);
check_gl();
glDepthMask(GL_TRUE);
diff --git a/src/skybox.h b/src/skybox.h
@@ -9,7 +9,7 @@
struct skybox {
struct gpu_program *program;
- struct model model;
+ model_id model_id;
gpu_addr attrs[n_vertex_attrs];
struct node node;
struct {
diff --git a/src/terrain.c b/src/terrain.c
@@ -53,15 +53,15 @@ void reset_terrain(struct terrain *terrain, float size) {
void init_terrain(struct terrain *terrain, float size) {
init_id(&terrain->entity_id);
+
struct entity *ent = new_entity(&terrain->entity_id); assert(ent);
struct node *node = get_node(&ent->node_id); assert(node);
- struct model *model = get_model(&terrain->model_id); assert(model);
+ struct model *model = new_model(&ent->model_id); assert(model);
+ /* struct model *model = init_model(&ent->model_id); assert(model); */
- assert(node);
assert(terrain->entity_id.index == 0);
model->shading = SHADING_TERRAIN;
- ent->model_id = terrain->model_id;
node_set_label(node, "terrain");
ent->casts_shadows = 0;
@@ -212,13 +212,18 @@ void create_terrain(struct terrain *terrain, float scale) {
struct make_geometry mkgeom;
init_make_geometry(&mkgeom);
+
mkgeom.num_verts = num_verts;
mkgeom.vertices = (float*)del_verts;
mkgeom.normals = (float*)del_norms;
mkgeom.indices = (u32*)del_indices;
mkgeom.num_indices = num_verts;
- make_buffer_geometry(&mkgeom, &terrain->model.geom_id);
+ struct model *model = get_model(&ent->model_id); assert(model);
+ struct geometry *geom = get_geometry(&model->geom_id); assert(geom);
+
+ assert(mkgeom.joint_weights == 0);
+ make_buffer_geometry(&mkgeom, geom);
delaunay2d_release(del);
tri_delaunay2d_release(tri);
@@ -232,7 +237,7 @@ void create_terrain(struct terrain *terrain, float scale) {
void destroy_terrain(struct terrain *terrain) {
- struct entity *ent = get_entity(&terrain->entity_id);
- assert(ent);
- destroy_buffer_geometry(&terrain->model.geom_id);
+ struct entity *ent = get_entity(&terrain->entity_id); assert(ent);
+ struct model *model = get_model(&ent->model_id); assert(model);
+ destroy_buffer_geometry(&model->geom_id);
}
diff --git a/src/terrain.h b/src/terrain.h
@@ -24,7 +24,6 @@ struct terrain {
entity_id entity_id;
struct perlin_settings settings;
struct point *samples;
- model_id model_id;
double (*fn)(struct terrain *, double, double);
int n_samples;
double size;
diff --git a/src/ui.c b/src/ui.c
@@ -45,7 +45,9 @@ static void create_quad(geometry_id *id)
.num_verts = ARRAY_SIZE(quad_vertices),
.num_uv_components = 2
};
- make_buffer_geometry(&mkgeom, id);
+ init_id(id);
+ struct geometry *geom = new_geometry(id);
+ make_buffer_geometry(&mkgeom, geom);
check_gl();
}
diff --git a/src/update.c b/src/update.c
@@ -297,7 +297,7 @@ void orbit_update_from_mouse(struct orbit *camera, struct input *input,
struct node *target_node = get_node(&player->node_id);
struct node *cam_node = get_node(&camera->node_id);
struct model *pmodel = get_model(&player->model_id); assert(pmodel);
- struct geometry *player_geom = get_geometry(model->geom_id);
+ struct geometry *player_geom = get_geometry(&pmodel->geom_id); assert(player_geom);
assert(target_node);
assert(cam_node);