polyadvent

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

commit b44042539ba47391ea4830ccaaf07179f7d7218b
parent 6bb630423da678fd7db9ce13b170d8a5253c6107
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  8 Nov 2018 19:24:09 -0800

skybox working

Diffstat:
Metc/shaders/skybox.v.glsl | 1-
Msrc/buffer.c | 23++++++++++++++---------
Msrc/buffer.h | 4++++
Msrc/geometry.c | 12+++++++-----
Msrc/geometry.h | 1+
Msrc/skybox.c | 53+++++++++++++++++++++++++++++++++++++++--------------
Msrc/ui.c | 1+
7 files changed, 66 insertions(+), 29 deletions(-)

diff --git a/etc/shaders/skybox.v.glsl b/etc/shaders/skybox.v.glsl @@ -1,7 +1,6 @@ #include profile in vec3 position; -in vec3 tex_coord; out vec3 tex_coords; uniform mat4 mvp; diff --git a/src/buffer.c b/src/buffer.c @@ -23,7 +23,7 @@ make_buffer(GLenum target, const void *buffer_data, GLsizei buffer_size) { struct vbo* make_index_buffer(GLenum target, const void *data, GLsizei buffer_size, struct vbo *vbo) { - vbo->components = 2; + vbo->components = 3; vbo->handle = make_buffer(target, data, buffer_size); vbo->type = GL_ELEMENT_ARRAY_BUFFER; return vbo; @@ -33,23 +33,32 @@ make_index_buffer(GLenum target, const void *data, GLsizei buffer_size, struct vbo* make_vertex_buffer(GLenum target, const void *data, GLsizei buffer_size, struct vbo *vbo) { - vbo->components = 2; + vbo->components = 3; vbo->handle = make_buffer(target, data, buffer_size); vbo->type = GL_ARRAY_BUFFER; return vbo; } +struct vbo* +make_uv_buffer(GLenum target, const void *data, + GLsizei buffer_size, struct vbo *vbo, int components) { + vbo->components = components; + vbo->handle = make_buffer(target, data, buffer_size); + vbo->type = GL_ARRAY_BUFFER; + return vbo; +} + void bind_ibo(struct vbo *vbo) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo->handle); } -static void bind_vbo_internal(struct vbo *vbo, gpu_addr slot, int size) { +static void bind_vbo_internal(struct vbo *vbo, gpu_addr slot) { glEnableVertexAttribArray(slot); check_gl(); glBindBuffer(vbo->type, vbo->handle); check_gl(); glVertexAttribPointer(slot, // attribute - size, // size + vbo->components, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride @@ -57,10 +66,6 @@ static void bind_vbo_internal(struct vbo *vbo, gpu_addr slot, int size) { ); } -void bind_uv_vbo(struct vbo *vbo, gpu_addr slot) { - bind_vbo_internal(vbo, slot, 2); -} - void bind_vbo(struct vbo *vbo, gpu_addr slot) { - bind_vbo_internal(vbo, slot, 3); + bind_vbo_internal(vbo, slot); } diff --git a/src/buffer.h b/src/buffer.h @@ -32,6 +32,10 @@ struct vbo * make_index_buffer(GLenum target, const void *buffer_data, GLsizei buffer_size, struct vbo *vbo); +struct vbo * +make_uv_buffer(GLenum target, const void *data, + GLsizei buffer_size, struct vbo *vbo, int components); + void bind_uv_vbo(struct vbo *vbo, gpu_addr slot); void bind_vbo(struct vbo *vbo, gpu_addr slot); void bind_ibo(struct vbo *vbo); diff --git a/src/geometry.c b/src/geometry.c @@ -36,7 +36,7 @@ void bind_geometry(struct geometry *geom, struct attributes *attrs) { check_gl(); } if (geom->vbos.tex_coord.handle) { - bind_uv_vbo(&geom->vbos.tex_coord, attrs->tex_coord); + bind_vbo(&geom->vbos.tex_coord, attrs->tex_coord); check_gl(); } bind_ibo(&geom->vbos.index); @@ -82,6 +82,7 @@ void init_geometry(struct geometry *geom) { geom->indices = NULL; geom->vertices = NULL; geom->tex_coords = NULL; + geom->num_uv_components = 2; geom->vbos.color.handle = 0; geom->vbos.normal.handle = 0; @@ -125,6 +126,7 @@ make_buffer_geometry(struct geometry *geom) { ); if (geom->tex_coords != NULL) { + assert(geom->num_uv_components); printf("%f %f %f %f\n", geom->tex_coords[0], geom->tex_coords[1], @@ -132,10 +134,10 @@ make_buffer_geometry(struct geometry *geom) { geom->tex_coords[3] ); - make_vertex_buffer(GL_ARRAY_BUFFER, - geom->tex_coords, - geom->num_verts * 2 * (int)sizeof(*geom->tex_coords), - &geom->vbos.tex_coord); + make_uv_buffer(GL_ARRAY_BUFFER, + geom->tex_coords, + geom->num_verts * geom->num_uv_components * (int)sizeof(*geom->tex_coords), + &geom->vbos.tex_coord, geom->num_uv_components); } /* printf("making index buffer\n"); */ diff --git a/src/geometry.h b/src/geometry.h @@ -18,6 +18,7 @@ struct geometry { struct buffer_geometry vbos; float min[3]; float max[3]; + int num_uv_components; int num_indices; int num_verts; float *vertices; diff --git a/src/skybox.c b/src/skybox.c @@ -4,14 +4,25 @@ #include "texture.h" static GLfloat skybox_vertices[] = { - 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0,-1.0, 1.0, 1.0,-1.0, 1.0, // v0-v1-v2-v3 front + 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0,-1.0, 1.0, 1.0,-1.0, 1.0, // v0-v1-v2-v3 top 1.0, 1.0,-1.0, 1.0, 1.0, 1.0, 1.0,-1.0, 1.0, 1.0,-1.0,-1.0, // v5-v0-v3-v4 right - -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,-1.0, -1.0, 1.0,-1.0, // v1-v0-v5-v6 top + -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,-1.0, -1.0, 1.0,-1.0, // v1-v0-v5-v6 front -1.0, 1.0, 1.0, -1.0, 1.0,-1.0, -1.0,-1.0,-1.0, -1.0,-1.0, 1.0, // v1-v6-v7-v2 left - 1.0,-1.0, 1.0, -1.0,-1.0, 1.0, -1.0,-1.0,-1.0, 1.0,-1.0,-1.0, // v3-v2-v7-v4 bottom - -1.0, 1.0,-1.0, 1.0, 1.0,-1.0, 1.0,-1.0,-1.0, -1.0,-1.0,-1.0 // v4-v7-v6-v5 back + 1.0,-1.0, 1.0, -1.0,-1.0, 1.0, -1.0,-1.0,-1.0, 1.0,-1.0,-1.0, // v3-v2-v7-v4 back + -1.0, 1.0,-1.0, 1.0, 1.0,-1.0, 1.0,-1.0,-1.0, -1.0,-1.0,-1.0 // v4-v7-v6-v5 bottom }; + +static GLfloat skybox_uvs[] = { + 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0,-1.0, 1.0, 1.0,-1.0, 1.0, + 1.0, 1.0,-1.0, 1.0, 1.0, 1.0, 1.0,-1.0, 1.0, 1.0,-1.0,-1.0, + -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,-1.0, -1.0, 1.0,-1.0, + -1.0, 1.0, 1.0, -1.0, 1.0,-1.0, -1.0,-1.0,-1.0, -1.0,-1.0, 1.0, + 1.0,-1.0, 1.0, -1.0,-1.0, 1.0, -1.0,-1.0,-1.0, 1.0,-1.0,-1.0, + -1.0, 1.0,-1.0, 1.0, 1.0,-1.0, 1.0,-1.0,-1.0, -1.0,-1.0,-1.0 +}; + + static u32 skybox_indices[] = { 0, 1, 2, 0, 2, 3, // top (+z) 4, 5, 6, 4, 6, 7, // right (+x) @@ -25,6 +36,7 @@ static u32 skybox_indices[] = { void create_skybox(struct skybox *skybox, struct gpu_program *program) { struct shader vertex, frag; struct shader *shaders[] = {&vertex, &frag}; + struct geometry *geom = &skybox->model.geom; int ok; node_init(&skybox->node); @@ -32,22 +44,35 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) { skybox->program = program; - skybox->model.geom.vertices = skybox_vertices; - skybox->model.geom.indices = skybox_indices; + geom->vertices = skybox_vertices; + geom->indices = skybox_indices; + /* geom->tex_coords = skybox_uvs; */ - skybox->model.geom.num_verts = ARRAY_SIZE(skybox_vertices); - skybox->model.geom.num_indices = ARRAY_SIZE(skybox_indices); + /* geom->num_uv_components = 3; */ + geom->num_verts = ARRAY_SIZE(skybox_vertices); + geom->num_indices = ARRAY_SIZE(skybox_indices); make_buffer_geometry(&skybox->model.geom); static const char *faces[6] = { - CUBEMAP("ame_siege/siege_rt_flip.tga"), - CUBEMAP("ame_siege/siege_lf_flip.tga"), - CUBEMAP("ame_siege/siege_ft_flip.tga"), - CUBEMAP("ame_siege/siege_bk.tga"), - CUBEMAP("ame_siege/siege_up_flip.tga"), - CUBEMAP("ame_siege/siege_dn_flip.tga"), + CUBEMAP("hw_sahara/sahara_rt_flip.tga"), + CUBEMAP("hw_sahara/sahara_lf_flip.tga"), + CUBEMAP("hw_sahara/sahara_ft_flip.tga"), + CUBEMAP("hw_sahara/sahara_bk.tga"), + CUBEMAP("hw_sahara/sahara_up_flip.tga"), + CUBEMAP("hw_sahara/sahara_dn_flip.tga"), }; + + + /* static const char *faces[6] = { */ + /* CUBEMAP("ame_siege/siege_rt_flip.tga"), */ + /* CUBEMAP("ame_siege/siege_lf_flip.tga"), */ + /* CUBEMAP("ame_siege/siege_ft_flip.tga"), */ + /* CUBEMAP("ame_siege/siege_bk.tga"), */ + /* CUBEMAP("ame_siege/siege_up_flip.tga"), */ + /* CUBEMAP("ame_siege/siege_dn_flip.tga"), */ + /* }; */ + skybox->model.texture = create_cubemap(faces); make_shader(GL_VERTEX_SHADER, SHADER("skybox.v.glsl"), &vertex); diff --git a/src/ui.c b/src/ui.c @@ -43,6 +43,7 @@ static void create_quad(struct geometry *geom) geom->tex_coords = quad_uvs; geom->num_indices = ARRAY_SIZE(quad_indices); geom->num_verts = ARRAY_SIZE(quad_vertices); + geom->num_uv_components = 2; make_buffer_geometry(geom); check_gl(); }