stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / render.h
index ef7a82fb73f9986b5568c72cee08f7caf2b70294..f77f10625ef1ee8b71bae081bc310e82b3863346 100644 (file)
--- a/render.h
+++ b/render.h
@@ -1,9 +1,15 @@
-#ifndef RENDER_H
-#define RENDER_H
-
 #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 );
+
+#ifndef RENDER_H
+#define RENDER_H
+
 static struct pipeline
 {
    float fov;
@@ -11,13 +17,70 @@ static struct pipeline
 
    GLuint fb_background,
           rgb_background;
+
+   /* STD140 */
+   struct ub_world_lighting
+   {
+      /* v3f (padded) */
+      v4f g_light_colours[3],
+          g_light_directions[3],
+          g_ambient_colour;
+
+      v4f g_water_plane,
+          g_depth_bounds;
+
+      float g_water_fog;
+      int g_light_count;
+      int g_light_preview;
+   }
+   ub_world_lighting;
+
+   struct light_widget
+   {
+      int enabled;
+      v2f dir;
+      v3f colour;
+   } 
+   widgets[3];
+
+   float shadow_spread, shadow_length;
+
+   GLuint fb_depthmap, rgb_depthmap;
+   GLuint ubo_world_lighting,
+          ubo_world;
 }
-gpipeline;
+gpipeline =
+{
+   .widgets =
+   {
+      {
+         .enabled = 1,
+         .colour = { 1.36f, 1.35f, 1.01f },
+         .dir = { 0.63f, -0.08f }
+      },
+      {
+         .enabled = 1,
+         .colour = { 0.33f, 0.56f, 0.64f },
+         .dir = { -2.60f, -0.13f }
+      },
+      {
+         .enabled = 1,
+         .colour = { 0.05f, 0.05f, 0.23f },
+         .dir = { 2.60f, -0.84f }
+      }
+   },
+   .shadow_spread = 0.65f,
+   .shadow_length = 9.50f,
 
-static void render_water_texture( m4x3f camera );
-static void render_water_surface( m4x4f pv );
-static void render_world( m4x4f projection, m4x3f camera );
+   .ub_world_lighting =
+   {
+      .g_ambient_colour = { 0.09f, 0.03f, 0.07f }
+   }
+};
 
+/* 
+ * Matrix Projections
+ */
 /* 
  * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf 
  */
@@ -47,41 +110,129 @@ static void pipeline_projection( m4x4f mat, float nearz, float farz )
          nearz, farz );
 }
 
-static void create_renderbuffer_std( GLuint *fb, GLuint *rgb, GLuint *rb )
+/*
+ * Shaders
+ */
+static void shader_link_standard_ub( GLuint shader, int texture_id )
 {
-   glGenFramebuffers( 1, fb );
-   glBindFramebuffer( GL_FRAMEBUFFER, *fb );
+   GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );   
+   glUniformBlockBinding( shader, idx, 0 );
 
-   glGenTextures( 1, rgb );
-   glBindTexture( GL_TEXTURE_2D, *rgb );
-   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, 
-         0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+   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)
+{
+   struct ub_world_lighting *winf = &gpipeline.ub_world_lighting;
+   int c = 0;
+
+   for( int i=0; i<3; i++ )
+   {
+      struct light_widget *lw = &gpipeline.widgets[i];
+
+      if( lw->enabled )
+      {
+         float pitch = lw->dir[0],
+               yaw = lw->dir[1],
+               xz = cosf( pitch );
+         
+         v3_copy( (v3f){ xz*cosf(yaw), sinf(pitch), xz*sinf(yaw) }, 
+               winf->g_light_directions[c] );
+         v3_copy( lw->colour, winf->g_light_colours[c] );
+
+         c ++;
+      }
+   }
+
+   winf->g_light_count = c;
+   winf->g_light_directions[0][3] = gpipeline.shadow_length;
+   winf->g_light_colours[0][3] = gpipeline.shadow_spread;
+
+   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 )
+{
+   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 );
+   }
+}
+
+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, *rgb, 0);
-
-   /* TODO: Check for DEPTH32f availiblity and use if possible */
+         GL_TEXTURE_2D, fb->colour, 0);
 
-   glGenRenderbuffers( 1, rb );
-   glBindRenderbuffer( GL_RENDERBUFFER, *rb );
-   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 
-         vg_window_x, vg_window_y );
+   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, *rb );
+         GL_RENDERBUFFER, fb->rb );
 }
 
-static void resize_renderbuffer_std( GLuint *fb, GLuint *rgb, GLuint *rb )
+static void fb_bindtex( struct framebuffer *fb, int texture )
 {
-   glBindTexture( GL_TEXTURE_2D, *rgb );
-   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, 0,
-         GL_RGB, GL_UNSIGNED_BYTE, NULL );
+   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, *rb );
-   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 
-         vg_window_x, vg_window_y );
+   glBindRenderbuffer( GL_RENDERBUFFER, fb->rb );
+   glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, ix, iy );
 }
 
 static void render_fb_resize(void)
@@ -91,6 +242,9 @@ static void render_fb_resize(void)
          GL_RGB, GL_UNSIGNED_BYTE, NULL );
 }
 
+/*
+ * Vg
+ */
 static void render_init(void)
 {
    glGenFramebuffers( 1, &gpipeline.fb_background );
@@ -107,6 +261,25 @@ static void render_init(void)
          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 };
 
@@ -120,8 +293,18 @@ static void render_init(void)
          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 );