reworked lighting uniforms
[carveJwlIkooP6JGAAIwe30JlM.git] / render.h
index d8bdc95ea21503662a93fc85d06690d5b49299ae..8285317aaf5106f757979b2ca70600e753efecc0 100644 (file)
--- a/render.h
+++ b/render.h
@@ -11,20 +11,35 @@ static struct pipeline
 
    GLuint fb_background,
           rgb_background;
+
+   /* STD140 */
+   struct ub_world_lighting
+   {
+      /* v3f (padded) */
+      v4f g_directional,
+          g_sun_colour,
+          g_shadow_colour;
+
+      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;
 
-struct framebuffer
-{
-   GLuint fb, colour, rb;
-   int div;
-   GLuint format;
-};
-
 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 );
 
+/* 
+ * Matrix Projections
+ */
 /* 
  * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf 
  */
@@ -54,6 +69,48 @@ static void pipeline_projection( m4x4f mat, float nearz, float farz )
          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 );
+
+   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 )
@@ -120,6 +177,9 @@ static void render_fb_resize(void)
          GL_RGB, GL_UNSIGNED_BYTE, NULL );
 }
 
+/*
+ * Vg
+ */
 static void render_init(void)
 {
    glGenFramebuffers( 1, &gpipeline.fb_background );
@@ -136,6 +196,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 };
 
@@ -149,8 +228,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 );