polyadvent

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

commit bf16c6674db2a5dd3cfb23a22b418223502e3c25
parent eefb282c5d136f8638984151591800290f547d42
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 27 Oct 2018 22:15:45 -0700

depth fbo working

Diffstat:
Metc/shaders/ui.f.glsl | 2+-
Msrc/fbo.c | 20++++++++++++++++----
Msrc/fbo.h | 16+++++++++++++---
Msrc/game.c | 15+++++++++------
Msrc/game.h | 2+-
Msrc/gl.h | 6++++++
Msrc/main.c | 7+++++--
Msrc/update.c | 14+++++++++++---
8 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/etc/shaders/ui.f.glsl b/etc/shaders/ui.f.glsl @@ -11,5 +11,5 @@ uniform sampler2D screen_texture; void main() { // fragmentColor = vec4(color + diffuse, 1.0); - fragmentColor = texture(screen_texture, v_tex_coords) + vec4(v_color, 1.0) * 0.00001; + fragmentColor = texture(screen_texture, v_tex_coords) + vec4(v_color, 1.0) * 0.000001; } diff --git a/src/fbo.c b/src/fbo.c @@ -43,7 +43,8 @@ void init_fbo(struct fbo *fbo) { fbo->height = 0; } -int fbo_attach_texture(struct fbo *fbo, GLenum attachment) { +int fbo_attach_texture(struct fbo *fbo, GLint internalformat, GLint format, + GLenum attachment, GLenum type) { assert(fbo->n_attachments < MAX_FBO_ATTACHMENTS); GLuint *texture = &fbo->attachments[fbo->n_attachments++]; @@ -53,8 +54,8 @@ int fbo_attach_texture(struct fbo *fbo, GLenum attachment) { check_gl(); glBindTexture(GL_TEXTURE_2D, *texture); check_gl(); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fbo->width, fbo->height, 0, GL_RGB, - GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, internalformat, fbo->width, fbo->height, 0, + format, type, NULL); check_gl(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); check_gl(); @@ -63,6 +64,7 @@ int fbo_attach_texture(struct fbo *fbo, GLenum attachment) { glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, *texture, 0); + check_gl(); glBindFramebuffer(GL_FRAMEBUFFER, 0); check_gl(); @@ -70,7 +72,7 @@ int fbo_attach_texture(struct fbo *fbo, GLenum attachment) { return *texture; } -void fbo_check(struct fbo *fbo) { +void check_fbo(struct fbo *fbo) { glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle); check_gl(); assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); @@ -95,3 +97,13 @@ void unbind_fbo(struct fbo *fbo) { glBindFramebuffer(GL_FRAMEBUFFER, 0); check_gl(); } + +int fbo_attach_depth_texture(struct fbo *fbo) { + return fbo_attach_texture(fbo, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, + GL_DEPTH_ATTACHMENT, GL_FLOAT); +} + +int fbo_attach_color_texture(struct fbo *fbo) { + return fbo_attach_texture(fbo, GL_RGB, GL_RGB, GL_COLOR_ATTACHMENT0, + GL_UNSIGNED_BYTE); +} diff --git a/src/fbo.h b/src/fbo.h @@ -16,9 +16,19 @@ struct fbo { void init_fbo(struct fbo *fbo); void bind_fbo(struct fbo *fbo); void create_fbo(struct fbo *fbo, int width, int height); -int fbo_attach_texture(struct fbo *, GLenum attachment); -void fbo_check(struct fbo *fbo); -int fbo_attach_renderbuffer(struct fbo *fbo, GLenum internalformat, GLenum attachment); + +int fbo_attach_color_texture(struct fbo *fbo); +int fbo_attach_depth_texture(struct fbo *fbo); + +int fbo_attach_texture(struct fbo *fbo, GLint internalformat, GLint format, + GLenum attachment, GLenum type); + +int fbo_attach_renderbuffer(struct fbo *fbo, GLenum internalformat, + GLenum attachment); + + + +void check_fbo(struct fbo *fbo); void delete_fbo(struct fbo *); void unbind_fbo(struct fbo *fbo); diff --git a/src/game.c b/src/game.c @@ -32,7 +32,7 @@ void game_init(struct game *game, int width, int height) { mat4 *mvp = res->test_mvp; struct node *root = &res->root; struct node *camera = &res->camera; - struct node *alt_camera = &res->alt_camera; + struct node *sun_camera = &res->sun_camera; struct entity *player = &res->player; struct terrain *terrain = &game->terrain; mat4 *light_dir = res->light_dir; @@ -58,8 +58,8 @@ void game_init(struct game *game, int width, int height) { width, // right 0.0, // bottom height, // top - 0.0, // near - 100000.0, // far + -10000.0, // near + 10000.0, // far res->proj_ortho ); @@ -94,7 +94,7 @@ void game_init(struct game *game, int width, int height) { node_init(root); node_init(&player->node); node_init(camera); - node_init(alt_camera); + node_init(sun_camera); node_init(&terrain->entity.node); // player init @@ -109,17 +109,20 @@ void game_init(struct game *game, int width, int height) { node_attach(&player->node, root); node_attach(camera, &player->node); - node_attach(alt_camera, camera); + node_attach(sun_camera, &player->node); quat_axis_angle(V3(1,0,0), -45, camera->orientation); + quat_axis_angle(V3(1,0,0), 5, sun_camera->orientation); + /* quat_axis_angle(V3(1,0,0), 0, sun_camera->orientation); */ /* quat_axis_angle(V3(1,0,0), -45, alt_camera->orientation); */ node_translate(&player->node, V3(terrain->size/2.,terrain->size/2.,0.0)); /* vec3_scale(player->node.scale, 10.0, player->node.scale); */ - node_rotate(alt_camera, V3(0, 0, 0)); /* node_translate(alt_camera, V3(0,60,-100)); */ + node_translate(sun_camera, V3(width/2.0, height/2.0, 50)); + node_rotate(camera, V3(100, 0, 0)); node_translate(camera, V3(0,-40,20)); /* node_recalc(camera); */ diff --git a/src/game.h b/src/game.h @@ -45,7 +45,7 @@ struct resources { struct node root; struct entity player; struct node camera; - struct node alt_camera; + struct node sun_camera; bool fog_on, diffuse_on; diff --git a/src/gl.h b/src/gl.h @@ -91,4 +91,10 @@ void glFramebufferRenderbuffer( GLenum target, void glDeleteRenderbuffers( GLsizei n, GLuint *renderbuffers); + +void glFramebufferTexture( GLenum target, + GLenum attachment, + GLuint texture, + GLint level); + #endif /* POLYADVENT_GL_H */ diff --git a/src/main.c b/src/main.c @@ -46,8 +46,8 @@ int main(void) struct render_config fbo_render_config = { .draw_ui = 0, - .camera = game.test_resources.camera.mat, - .projection = game.test_resources.proj_persp + .camera = game.test_resources.sun_camera.mat, + .projection = game.test_resources.proj_ortho }; struct render_config default_config = { @@ -65,10 +65,13 @@ int main(void) GLuint texture = game.test_resources.shadow_buffer.attachments[0]; struct fbo *fbo = &game.test_resources.shadow_buffer; + check_fbo(fbo); bind_fbo(fbo); /* glViewport( 0, 0, width, height ); */ + /* glDrawBuffer(GL_NONE); */ + check_gl(); render(&game, &fbo_render_config); unbind_fbo(&game.test_resources.shadow_buffer); render(&game, &default_config); diff --git a/src/update.c b/src/update.c @@ -211,9 +211,17 @@ void resize_fbos(struct game *game, int width, int height) { } create_fbo(&res->shadow_buffer, width, height); - fbo_attach_renderbuffer(&res->shadow_buffer, GL_DEPTH24_STENCIL8, - GL_DEPTH_STENCIL_ATTACHMENT); - fbo_attach_texture(&res->shadow_buffer, GL_COLOR_ATTACHMENT0); + /* fbo_attach_renderbuffer(&res->shadow_buffer, GL_DEPTH24_STENCIL8, */ + /* GL_DEPTH_STENCIL_ATTACHMENT); */ + + /* fbo_attach_color_texture(&res->shadow_buffer); */ + fbo_attach_depth_texture(&res->shadow_buffer); + + check_fbo(&res->shadow_buffer); + + /* fbo_attach_texture(&res->shadow_buffer, GL_DEPTH_COMPONENT16, */ + /* GL_DEPTH_COMPONENT, GL_DEPTH_ATTACHMENT); */ + } // TODO: match based on some real concept of time