-#ifndef RENDER_H
-#define RENDER_H
-
+/*
+ * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
+#pragma once
#include "common.h"
#include "model.h"
+#include "camera.h"
+#include "shader_props.h"
-static struct pipeline
-{
- float fov;
- glmesh fsquad;
-
- GLuint fb_background,
- rgb_background;
+#include "shaders/blit.h"
+#include "shaders/blitblur.h"
+#include "shaders/blitcolour.h"
+#include "shaders/blit_transition.h"
- /* STD140 */
- struct ub_world_lighting
- {
- /* v3f (padded) */
- v4f g_directional,
- g_sun_colour,
- g_shadow_colour;
+#define WORKSHOP_PREVIEW_WIDTH 504
+#define WORKSHOP_PREVIEW_HEIGHT 336
- v4f g_water_plane,
- g_depth_bounds;
- float g_water_fog;
- }
- ub_world_lighting;
-
- GLuint fb_depthmap, rgb_depthmap;
- GLuint ubo_world_lighting,
- ubo_world;
-}
-gpipeline;
+static f32 k_render_scale = 1.0f;
+static i32 k_blur_effect = 1;
+static f32 k_blur_strength = 0.3f;
+static f32 k_fov = 0.86f;
+static f32 k_cam_height = 0.8f;
-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 render_world_depth( m4x4f projection, m4x3f camera );
+typedef struct framebuffer framebuffer;
/*
- * Matrix Projections
+ * All standard buffers used in rendering
*/
-/*
- * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
- */
-static void plane_clip_projection( m4x4f mat, v4f plane )
-{
- v4f c =
- {
- (vg_signf(plane[0]) + mat[2][0]) / mat[0][0],
- (vg_signf(plane[1]) + mat[2][1]) / mat[1][1],
- -1.0f,
- (1.0f + mat[2][2]) / mat[3][2]
- };
-
- v4_muls( plane, 2.0f / v4_dot(plane,c), c );
-
- mat[0][2] = c[0];
- mat[1][2] = c[1];
- mat[2][2] = c[2] + 1.0f;
- mat[3][2] = c[3];
-}
-
-static void pipeline_projection( m4x4f mat, float nearz, float farz )
-{
- m4x4_projection( mat,
- gpipeline.fov,
- (float)vg_window_x / (float)vg_window_y,
- nearz, farz );
-}
-
-/*
- * Shaders
- */
-static void shader_link_standard_ub( GLuint shader, int texture_id )
-{
- GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );
- glUniformBlockBinding( shader, idx, 0 );
-
- glActiveTexture( GL_TEXTURE0 + texture_id );
- glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_depthmap );
- glUniform1i( glGetUniformLocation( shader, "g_world_depth" ), texture_id );
-}
-
-static void render_update_lighting_ub(void)
-{
- 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 );
+static struct pipeline{
+ glmesh fsquad;
- render_update_lighting_ub();
- glBindBufferBase( GL_UNIFORM_BUFFER, 0, gpipeline.ubo_world_lighting );
+ framebuffer *fb_main,
+ *fb_water_reflection,
+ *fb_water_beneath,
+ *fb_workshop_preview,
+ *fb_network_status;
+ int ready;
}
+gpipeline;
-/*
- * Framebuffers
- */
-struct framebuffer
-{
- GLuint fb, colour, rb;
- int div;
- GLuint format;
-};
-
-static void fb_use( struct framebuffer *fb )
-{
- if( !fb )
- {
- glBindFramebuffer( GL_FRAMEBUFFER, 0 );
- 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 );
+struct framebuffer{
+ const char *display_name;
+ int resolution_div, /* definition */
+ fixed_w,
+ fixed_h,
+
+ render_w, /* runtime */
+ render_h;
+
+ struct framebuffer_attachment{
+ const char *display_name;
+
+ enum framebuffer_attachment_type{
+ k_framebuffer_attachment_type_none,
+ k_framebuffer_attachment_type_texture,
+ k_framebuffer_attachment_type_renderbuffer,
+ k_framebuffer_attachment_type_texture_depth
+ }
+ purpose;
+
+ enum framebuffer_quality_profile{
+ k_framebuffer_quality_all,
+ k_framebuffer_quality_high_only
+ }
+ quality;
+
+ GLenum internalformat,
+ format,
+ type,
+ attachment;
+
+ GLuint id;
+
+ /* Runtime */
+ int debug_view;
}
+ attachments[5];
+ GLuint fb;
+ framebuffer **link;
}
-
-static void fb_init( struct framebuffer *fb )
-{
- i32 ix = vg_window_x / fb->div,
- iy = vg_window_y / fb->div;
-
- glGenFramebuffers( 1, &fb->fb );
- glBindFramebuffer( GL_FRAMEBUFFER, fb->fb );
-
- glGenTextures( 1, &fb->colour );
- glBindTexture( GL_TEXTURE_2D, fb->colour );
- glTexImage2D( GL_TEXTURE_2D, 0, fb->format, ix, iy,
- 0, fb->format, GL_UNSIGNED_BYTE, NULL);
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D, fb->colour, 0);
-
- glGenRenderbuffers( 1, &fb->rb );
- glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
- glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
-
- glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
- GL_RENDERBUFFER, fb->rb );
-}
-
-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 )
-{
- 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,
- fb->format, GL_UNSIGNED_BYTE, NULL );
-
- glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
- glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
-}
-
-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 );
-}
-
-/*
- * Vg
- */
-static void render_init(void)
-{
- 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,
- 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 );
- 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
- */
- glGenFramebuffers( 1, &gpipeline.fb_depthmap );
- glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
-
- glGenTextures( 1, &gpipeline.rgb_depthmap );
- glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_depthmap );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_R32F, 1024, 1024, 0,
- GL_RED, GL_FLOAT, NULL );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- vg_tex2d_clamp();
-
- glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D,
- gpipeline.rgb_depthmap, 0);
-
- 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 };
-
- glGenVertexArrays( 1, &gpipeline.fsquad.vao );
- glGenBuffers( 1, &gpipeline.fsquad.vbo );
- glBindVertexArray( gpipeline.fsquad.vao );
- glBindBuffer( GL_ARRAY_BUFFER, gpipeline.fsquad.vbo );
- glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
- glBindVertexArray( gpipeline.fsquad.vao );
- glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE,
- sizeof(float)*2, (void*)0 );
- glEnableVertexAttribArray( 0 );
- VG_CHECK_GL();
-
- render_alloc_ub();
-}
-
-static void render_free(void)
-{
- /* TODO: ... */
-}
-
-/*
- * Utility
- */
-static void render_fsquad(void)
-{
- glBindVertexArray( gpipeline.fsquad.vao );
- glDrawArrays( GL_TRIANGLES, 0, 6 );
-}
-
-#endif /* RENDER_H */
+extern framebuffers[];
+
+void render_init(void);
+void render_fsquad(void);
+void render_fsquad1(void);
+void render_fsquad2(void);
+void render_view_framebuffer_ui(void);
+void render_fb_bind_texture( framebuffer *fb, int attachment, int slot );
+void render_fb_inverse_ratio( framebuffer *fb, v2f inverse );
+void render_fb_get_current_res( struct framebuffer *fb, int *x, int *y );
+void render_fb_bind( framebuffer *fb, int use_scaling );
+void render_fb_bind_texture( framebuffer *fb, int attachment, int slot );
+void render_fb_allocate( struct framebuffer *fb );
+void render_fb_resize(void);