MENY
[carveJwlIkooP6JGAAIwe30JlM.git] / render.h
index 706cf9b14de229dabdadba2b419e16135c5dd179..8122b2ae8e41f18effe8dea39a9d5224ae8f2c93 100644 (file)
--- a/render.h
+++ b/render.h
@@ -1,10 +1,13 @@
+/*
+ * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
+
 #include "common.h"
 #include "model.h"
 
 #include "shaders/blit.h"
 #include "shaders/standard.h"
 #include "shaders/vblend.h"
-#include "shaders/unlit.h"
 
 static void render_water_texture( m4x3f camera );
 static void render_water_surface( m4x4f pv, m4x3f camera );
@@ -122,7 +125,7 @@ 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 );
 }
 
@@ -180,19 +183,19 @@ 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 )
 {
-   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 );
@@ -236,8 +239,8 @@ static void fb_resize( struct framebuffer *fb )
    if( !fb->allocated )
       return;
 
-   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;
 
    glBindTexture( GL_TEXTURE_2D, fb->colour );
    glTexImage2D( GL_TEXTURE_2D, 0, fb->format, ix, iy, 0,
@@ -252,90 +255,106 @@ static void render_fb_resize(void)
    if( gpipeline.ready )
    {
       glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
-      glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, 0, 
+      glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg.window_x, vg.window_y, 0, 
             GL_RGB, GL_UNSIGNED_BYTE, NULL );
    }
 }
 
-/*
- * Vg
- */
+/* used for drawing player onto */
+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, 
+         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);
+
+   VG_CHECK_GL_ERR();
+}
+
+/* used for drawing world depth from the top view, used in our water and
+ * lighting calculations */
+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 );
+
+   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);
+
+   VG_CHECK_GL_ERR();
+}
+
+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 };
+
+   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_ERR();
+}
+
+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_init(void)
 {
    shader_blit_register();
    shader_standard_register();
    shader_vblend_register();
-   shader_unlit_register();
 
    vg_acquire_thread_sync();
    {
-      vg_info( "Allocating framebuffers\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, 
-            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);
-
-      VG_CHECK_GL_ERR();
-
-      /* 
-       * 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);
-
-      VG_CHECK_GL_ERR();
-
-      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_ERR();
-
-      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();
+      render_init_temp_buffer();
+      render_init_depthmap_buffer();
+      render_init_fs_quad();
+      render_init_uniform_buffers();
 
       glBindFramebuffer( GL_FRAMEBUFFER, 0 );
-      vg_success( "Done\n" );
-      
       gpipeline.ready = 1;
    }
 
@@ -344,14 +363,16 @@ static void render_init(void)
 
 static void render_free(void *_)
 {
+   glDeleteBuffers( 1, &gpipeline.ubo_world_lighting );
+
    glDeleteVertexArrays( 1, &gpipeline.fsquad.vao );
    glDeleteBuffers( 1, &gpipeline.fsquad.vbo );
 
-   glDeleteFramebuffers( 1, &gpipeline.fb_background );
    glDeleteFramebuffers( 1, &gpipeline.fb_depthmap );
+   glDeleteTextures( 1, &gpipeline.rgb_depthmap );
 
+   glDeleteFramebuffers( 1, &gpipeline.fb_background );
    glDeleteTextures( 1, &gpipeline.rgb_background );
-   glDeleteTextures( 1, &gpipeline.rgb_depthmap );
 }
 
 /*