X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=render.h;h=33998221b83ea206f6ee8c0d90f44d1c4abe35b3;hb=75703291fbf045008a3b1ebb20fc46a2617b6b3b;hp=f77f10625ef1ee8b71bae081bc310e82b3863346;hpb=be0f28b651e5c39943e476c0cd68c146e9952857;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/render.h b/render.h index f77f106..3399822 100644 --- a/render.h +++ b/render.h @@ -1,16 +1,34 @@ +/* + * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved + */ + #include "common.h" #include "model.h" -static void render_water_texture( m4x3f camera ); -static void render_water_surface( m4x4f pv, m4x3f camera ); -static void render_world( m4x4f projection, m4x3f camera ); -static void shader_link_standard_ub( GLuint shader, int texture_id ); -static void render_world_depth( m4x4f projection, m4x3f camera ); +#include "shaders/blit.h" +#include "shaders/blitblur.h" +#include "shaders/standard.h" +#include "shaders/vblend.h" + +VG_STATIC void render_water_texture( m4x3f camera ); +VG_STATIC void render_water_surface( m4x4f pv, m4x3f camera ); +VG_STATIC void render_world( m4x4f projection, m4x3f camera ); +VG_STATIC void shader_link_standard_ub( GLuint shader, int texture_id ); +VG_STATIC void render_world_depth( m4x4f projection, m4x3f camera ); #ifndef RENDER_H #define RENDER_H -static struct pipeline +struct framebuffer +{ + GLuint fb, colour, rb; + int div; + GLuint format; + + int allocated; +}; + +VG_STATIC struct pipeline { float fov; glmesh fsquad; @@ -32,6 +50,7 @@ static struct pipeline float g_water_fog; int g_light_count; int g_light_preview; + int g_shadow_samples; } ub_world_lighting; @@ -48,6 +67,8 @@ static struct pipeline GLuint fb_depthmap, rgb_depthmap; GLuint ubo_world_lighting, ubo_world; + + int ready; } gpipeline = { @@ -84,7 +105,7 @@ gpipeline = /* * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf */ -static void plane_clip_projection( m4x4f mat, v4f plane ) +VG_STATIC void plane_clip_projection( m4x4f mat, v4f plane ) { v4f c = { @@ -102,18 +123,18 @@ static void plane_clip_projection( m4x4f mat, v4f plane ) mat[3][2] = c[3]; } -static void pipeline_projection( m4x4f mat, float nearz, float farz ) +VG_STATIC void pipeline_projection( m4x4f mat, float nearz, float farz ) { m4x4_projection( mat, gpipeline.fov, - (float)vg_window_x / (float)vg_window_y, + (float)vg.window_x / (float)vg.window_y, nearz, farz ); } /* * Shaders */ -static void shader_link_standard_ub( GLuint shader, int texture_id ) +VG_STATIC void shader_link_standard_ub( GLuint shader, int texture_id ) { GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" ); glUniformBlockBinding( shader, idx, 0 ); @@ -123,7 +144,7 @@ static void shader_link_standard_ub( GLuint shader, int texture_id ) glUniform1i( glGetUniformLocation( shader, "g_world_depth" ), texture_id ); } -static void render_update_lighting_ub(void) +VG_STATIC void render_update_lighting_ub(void) { struct ub_world_lighting *winf = &gpipeline.ub_world_lighting; int c = 0; @@ -150,50 +171,38 @@ static void render_update_lighting_ub(void) winf->g_light_directions[0][3] = gpipeline.shadow_length; winf->g_light_colours[0][3] = gpipeline.shadow_spread; + if( vg.quality_profile == k_quality_profile_low ) + winf->g_shadow_samples = 0; + else + winf->g_shadow_samples = 8; + glBindBuffer( GL_UNIFORM_BUFFER, gpipeline.ubo_world_lighting ); glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(struct ub_world_lighting), &gpipeline.ub_world_lighting ); } -static void render_alloc_ub(void) -{ - glGenBuffers( 1, &gpipeline.ubo_world_lighting ); - glBindBuffer( GL_UNIFORM_BUFFER, gpipeline.ubo_world_lighting ); - glBufferData( GL_UNIFORM_BUFFER, sizeof(struct ub_world_lighting), - NULL, GL_DYNAMIC_DRAW ); - - render_update_lighting_ub(); - glBindBufferBase( GL_UNIFORM_BUFFER, 0, gpipeline.ubo_world_lighting ); -} - /* * Framebuffers */ -struct framebuffer -{ - GLuint fb, colour, rb; - int div; - GLuint format; -}; -static void fb_use( struct framebuffer *fb ) +VG_STATIC void fb_use( struct framebuffer *fb ) { if( !fb ) { glBindFramebuffer( GL_FRAMEBUFFER, 0 ); - glViewport( 0, 0, vg_window_x, vg_window_y ); + glViewport( 0, 0, vg.window_x, vg.window_y ); } else { glBindFramebuffer( GL_FRAMEBUFFER, fb->fb ); - glViewport( 0, 0, vg_window_x / fb->div, vg_window_y / fb->div ); + glViewport( 0, 0, vg.window_x / fb->div, vg.window_y / fb->div ); } } -static void fb_init( struct framebuffer *fb ) +VG_STATIC void fb_init( struct framebuffer *fb ) { - i32 ix = vg_window_x / fb->div, - iy = vg_window_y / fb->div; + i32 ix = vg.window_x / fb->div, + iy = vg.window_y / fb->div; glGenFramebuffers( 1, &fb->fb ); glBindFramebuffer( GL_FRAMEBUFFER, fb->fb ); @@ -214,18 +223,31 @@ static void fb_init( struct framebuffer *fb ) glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rb ); + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); + + VG_CHECK_GL_ERR(); + fb->allocated = 1; } -static void fb_bindtex( struct framebuffer *fb, int texture ) +VG_STATIC void fb_free( struct framebuffer *fb ) +{ + glDeleteTextures( 1, &fb->colour ); + glDeleteFramebuffers( 1, &fb->fb ); +} + +VG_STATIC void fb_bindtex( struct framebuffer *fb, int texture ) { glActiveTexture( GL_TEXTURE0 + texture ); glBindTexture( GL_TEXTURE_2D, fb->colour ); } -static void fb_resize( struct framebuffer *fb ) +VG_STATIC void fb_resize( struct framebuffer *fb ) { - i32 ix = vg_window_x / fb->div, - iy = vg_window_y / fb->div; + if( !fb->allocated ) + return; + + i32 ix = vg.window_x / fb->div, + iy = vg.window_y / fb->div; glBindTexture( GL_TEXTURE_2D, fb->colour ); glTexImage2D( GL_TEXTURE_2D, 0, fb->format, ix, iy, 0, @@ -235,36 +257,46 @@ static void fb_resize( struct framebuffer *fb ) glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy ); } -static void render_fb_resize(void) +VG_STATIC void render_fb_resize(void) { - glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, 0, - GL_RGB, GL_UNSIGNED_BYTE, NULL ); + if( gpipeline.ready ) + { + glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg.window_x, vg.window_y, 0, + GL_RGB, GL_UNSIGNED_BYTE, NULL ); + } } -/* - * Vg - */ -static void render_init(void) +/* used for drawing player onto */ +VG_STATIC void render_init_temp_buffer(void) { + vg_info( "[render] Allocate temporary framebuffer\n" ); + glGenFramebuffers( 1, &gpipeline.fb_background ); glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_background ); glGenTextures( 1, &gpipeline.rgb_background ); glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg.window_x, vg.window_y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gpipeline.rgb_background, 0); - /* - * World depth map, maybe this should be moved to world.h - * TODO: review - */ + VG_CHECK_GL_ERR(); +} + +/* used for drawing world depth from the top view, used in our water and + * lighting calculations */ +VG_STATIC void render_init_depthmap_buffer(void) +{ + vg_info( "[render] Allocate depth map buffer\n" ); + glGenFramebuffers( 1, &gpipeline.fb_depthmap ); glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap ); @@ -280,8 +312,18 @@ static void render_init(void) GL_TEXTURE_2D, gpipeline.rgb_depthmap, 0); + VG_CHECK_GL_ERR(); +} + +VG_STATIC void render_init_fs_quad(void) +{ + vg_info( "[render] Allocate quad\n" ); + float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }; + 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + + 0.2f, 0.0f, 0.8f, 1.0f, 0.2f, 1.0f, + 0.2f, 0.0f, 0.8f, 0.0f, 0.8f, 1.0f}; glGenVertexArrays( 1, &gpipeline.fsquad.vao ); glGenBuffers( 1, &gpipeline.fsquad.vbo ); @@ -292,23 +334,59 @@ static void render_init(void) glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 ); glEnableVertexAttribArray( 0 ); - VG_CHECK_GL(); - render_alloc_ub(); + VG_CHECK_GL_ERR(); +} + +VG_STATIC void render_init_uniform_buffers(void) +{ + vg_info( "[render] Allocate uniform buffer\n" ); + + glGenBuffers( 1, &gpipeline.ubo_world_lighting ); + glBindBuffer( GL_UNIFORM_BUFFER, gpipeline.ubo_world_lighting ); + glBufferData( GL_UNIFORM_BUFFER, sizeof(struct ub_world_lighting), + NULL, GL_DYNAMIC_DRAW ); + + render_update_lighting_ub(); + glBindBufferBase( GL_UNIFORM_BUFFER, 0, gpipeline.ubo_world_lighting ); + + VG_CHECK_GL_ERR(); } -static void render_free(void) +VG_STATIC void render_init(void) { - /* TODO: ... */ + shader_blit_register(); + shader_blitblur_register(); + shader_standard_register(); + shader_vblend_register(); + + vg_acquire_thread_sync(); + { + render_init_temp_buffer(); + render_init_depthmap_buffer(); + render_init_fs_quad(); + render_init_uniform_buffers(); + + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); + gpipeline.ready = 1; + } + + vg_release_thread_sync(); } /* * Utility */ -static void render_fsquad(void) +VG_STATIC void render_fsquad(void) { glBindVertexArray( gpipeline.fsquad.vao ); glDrawArrays( GL_TRIANGLES, 0, 6 ); } +VG_STATIC void render_fsquad1(void) +{ + glBindVertexArray( gpipeline.fsquad.vao ); + glDrawArrays( GL_TRIANGLES, 6, 6 ); +} + #endif /* RENDER_H */