new animations
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
diff --git a/scene.h b/scene.h
index af9e26add0585bc3d4e1eb4b583547da7c453034..63ae145453d9521cb8a5d44827233322f67dc2da 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -1,66 +1,11 @@
-typedef struct model model;
-typedef struct submodel submodel;
-typedef struct model_vert model_vert;
-typedef struct scene scene;
-typedef struct sdf_primative sdf_primative;
-typedef enum esdf_type esdf_type;
+#include "vg/vg.h"
+#include "model.h"
 
 GLuint tex_dual_noise;
 
-#pragma pack(push,1)
-struct model
-{
-   u32 identifier;
-
-   u32 vertex_count,
-       indice_count,
-       layer_count;
-};
-
-struct sdf_primative
-{
-   v4f origin;   /* xyz, yaw */
-   /* Cone: 
-       x  base scale
-       y  height 
-   */
-   v4f info;
-};
-
-struct submodel
-{
-   u32 indice_start,
-       indice_count,
-       vertex_start,
-       vertex_count;
-   
-   boxf bbx;
-   sdf_primative sdf;
-
-   enum esdf_type
-   {
-      k_sdf_none = 0,
-      k_sdf_cone,
-      k_sdf_sphere,
-      k_sdf_box
-   }
-   sdf_type;
-
-   char name[32];
-};
-
-struct model_vert
-{
-   v3f co,
-       norm;
-   v4f colour;
-   v2f uv;
-};
-#pragma pack(pop)
-
 struct scene
 {
-   GLuint vao, vbo, ebo;
+   glmesh mesh;
 
    model_vert *verts;
    u32 *indices;
@@ -81,6 +26,8 @@ struct scene
 
    u32 shadower_count,
        shadower_cap;
+
+   submodel submesh;
 };
 
 static void scene_init( scene *pscene )
@@ -92,6 +39,8 @@ static void scene_init( scene *pscene )
    pscene->shadowers = NULL;
    pscene->shadower_count = 0;
    pscene->shadower_cap = 0;
+   pscene->submesh.indice_start = 0;
+   pscene->submesh.indice_count = 0;
 
    v3_fill( pscene->bbx[0],  999999.9f );
    v3_fill( pscene->bbx[1], -999999.9f );
@@ -161,14 +110,18 @@ static void scene_init( scene *pscene )
    "return f;"                                        \
 "}"
 
-SHADER_DEFINE( shader_debug_vcol,
-       "layout (location=0) in vec3 a_co;"
-   "layout (location=1) in vec3 a_norm;"
-   "layout (location=2) in vec4 a_colour;"
+#define VERTEX_STANDARD_ATTRIBUTES                    \
+       "layout (location=0) in vec3 a_co;"                \
+   "layout (location=1) in vec3 a_norm;"              \
+   "layout (location=2) in vec4 a_colour;"            \
    "layout (location=3) in vec2 a_uv;"
-   ""
+
+SHADER_DEFINE( shader_debug_vcol,
+
+   /*Include*/ VERTEX_STANDARD_ATTRIBUTES
+
        "uniform mat4 uPv;"
-   "uniform mat4 uMdl;"
+   "uniform mat4x3 uMdl;"
    "uniform float uTime;"
    "uniform float uSwayAmt;"
    ""
@@ -191,7 +144,7 @@ SHADER_DEFINE( shader_debug_vcol,
        "void main()"
        "{"
       "vec3 swaypos = compute_sway( a_co );"
-               "gl_Position = uPv * uMdl * vec4( swaypos, 1.0 );"
+               "gl_Position = uPv * vec4(uMdl * vec4(swaypos,1.0), 1.0 );"
       "aColour = a_colour;"
       "aUv = a_uv;"
       "aNorm = normalize(mat3(uMdl) * a_norm);"
@@ -204,7 +157,7 @@ SHADER_DEFINE( shader_debug_vcol,
    "uniform sampler2D uTexMain;"
    "uniform sampler2D uTexGradients;"
    ""
-   /* Include */ SHADER_VALUE_NOISE_3D
+   /*Include*/ SHADER_VALUE_NOISE_3D
    ""
    "in vec4 aColour;"
    "in vec2 aUv;"
@@ -247,9 +200,22 @@ SHADER_DEFINE( shader_debug_vcol,
       "}"
       "if( uMode == 7 )"
       "{"
-         "if( diffuse.a < 0.45 ) discard;"
-         "float lighting = 1.0 - aColour.g;"
+         "if( diffuse.a < 0.2 ) discard;"
+         "float lighting = 1.0 - aColour.g*0.8;"
+   
+         "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
+         "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
+         "vec3 lt = vec3(0.2,0.2,0.2 ) + " 
+                   "vec3(1.0,1.0,0.9)*light1 + "
+                   "vec3(0.1,0.3,0.4 )*light2;"
+
+
          "colour = vec4(vec3(pow(lighting,1.6)*(diffuse.r*0.7+0.5)),1.0);"
+         "colour = vec4(colour.rgb*lt,1.0);"
+
+         "vec2 gradUV = vec2(lighting*1.9,aColour.b*0.8);"
+         "vec4 gradient_sample = texture( uTexGradients, gradUV );"
+         "colour = colour*gradient_sample;"
       "}"
       "if( uMode == 8 )"
       "{"
@@ -258,9 +224,9 @@ SHADER_DEFINE( shader_debug_vcol,
          "light = pow(light,1.6)*(diffuse.r*0.7+0.5);"
          "float r1 = fractalNoise(aCo*0.01);"
          
-         "vec2 gradUV = vec2(light*1.9,r1+aColour.b*0.1);"
+         "vec2 gradUV = vec2(light*1.9,r1+aColour.b);"
          "vec4 gradient_sample = texture( uTexGradients, gradUV );"
-         "colour = aColour*light;"
+         "colour = gradient_sample*light;"
       "}"
 
                "FragColor = colour;"
@@ -270,47 +236,93 @@ SHADER_DEFINE( shader_debug_vcol,
               "uTime", "uSwayAmt", "uMdl" })
 )
 
-/*
- * Helper functions for file offsets
- */
-static submodel *model_get_submodel( model *mdl, int id )
-{
-   return ((submodel*)(mdl+1)) + id;
-}
+SHADER_DEFINE( shader_standard_lit,
 
-static model_vert *model_vertex_base( model *mdl )
-{
-   return (model_vert *)model_get_submodel( mdl, mdl->layer_count );
-}
+   /*Include*/ VERTEX_STANDARD_ATTRIBUTES
 
-static u32 *model_indice_base( model *mdl )
-{
-   return (u32 *)(model_vertex_base( mdl ) + mdl->vertex_count);
-}
+       "uniform mat4 uPv;"
+   "uniform mat4x3 uMdl;"
+   ""
+   "out vec4 aColour;"
+   "out vec2 aUv;"
+   "out vec3 aNorm;"
+   "out vec3 aCo;"
+   ""
+       "void main()"
+       "{"
+               "gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );"
+      "aColour = a_colour;"
+      "aUv = a_uv;"
+      "aNorm = mat3(uMdl) * a_norm;"
+      "aCo = a_co;"
+       "}",
+   /* Fragment */
+       "out vec4 FragColor;"
+       ""
+   "uniform sampler2D uTexMain;"
+   "uniform vec4 uColour;"
+   ""
+   "in vec4 aColour;"
+   "in vec2 aUv;"
+   "in vec3 aNorm;"
+   "in vec3 aCo;"
+   ""
+       "void main()"
+       "{"
+      "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
 
-static model_vert *submodel_vert_data( model *mdl, submodel *sub )
-{
-   return model_vertex_base(mdl) + sub->vertex_start;
-}
+      "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
+      "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
+      "diffuse += vec3(0.2,0.2,0.2 ) + " 
+                 "vec3(1.0,1.0,0.9)*light1 + "
+                 "vec3(0.1,0.3,0.4 )*light2;"
 
-static u32 *submodel_indice_data( model *mdl, submodel *sub )
-{
-   return model_indice_base(mdl) + sub->indice_start;
-}
+               "FragColor = vec4((diffuse*uColour.rgb),"
+                        "aColour.a*uColour.a);"
+       "}"
+       ,
+       UNIFORMS({ "uColour","uTexMain","uPv","uMdl" })
+)
 
-/* Returns -1 if not found */
-static int submodel_get( model *mdl, const char *name )
-{
-   for( int i=0; i<mdl->layer_count; i++ )
-   {
-      if( !strcmp( model_get_submodel(mdl,i)->name, name ))
-      {
-         return i;
-      }
-   }
-   
-   return -1;
-}
+SHADER_DEFINE( shader_unlit,
+
+   /*Include*/ VERTEX_STANDARD_ATTRIBUTES
+
+       "uniform mat4 uPv;"
+   "uniform mat4x3 uMdl;"
+   ""
+   "out vec4 aColour;"
+   "out vec2 aUv;"
+   "out vec3 aNorm;"
+   "out vec3 aCo;"
+   ""
+       "void main()"
+       "{"
+               "gl_Position = uPv * vec4(uMdl * vec4(a_co,1.0), 1.0);"
+      "aColour = a_colour;"
+      "aUv = a_uv;"
+      "aNorm = mat3(uMdl) * a_norm;"
+      "aCo = a_co;"
+       "}",
+   /* Fragment */
+       "out vec4 FragColor;"
+       ""
+   "uniform sampler2D uTexMain;"
+   "uniform vec4 uColour;"
+   ""
+   "in vec4 aColour;"
+   "in vec2 aUv;"
+   "in vec3 aNorm;"
+   "in vec3 aCo;"
+   ""
+       "void main()"
+       "{"
+      "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
+      "FragColor = vec4(pow(diffuse,vec3(1.0)),1.0);"
+       "}"
+       ,
+       UNIFORMS({ "uTexMain", "uPv", "uMdl" })
+)
 
 static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount, 
       size_t emsize )
@@ -328,11 +340,9 @@ static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
 /* 
  * Append a model into the scene with a given transform
  */
-static void scene_add_model( scene *pscene, model *mdl, int id,
+static void scene_add_model( scene *pscene, model *mdl, submodel *submodel,
       v3f pos, float yaw, float scale )
 {
-   submodel *submodel = model_get_submodel( mdl, id );
-   
    pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, 
          &pscene->vertex_cap, submodel->vertex_count, sizeof(model_vert) );
    pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count,
@@ -401,6 +411,14 @@ static void scene_add_model( scene *pscene, model *mdl, int id,
    pscene->indice_count += submodel->indice_count;
 }
 
+static void scene_copy_slice( scene *pscene, submodel *sm )
+{
+   sm->indice_start = pscene->submesh.indice_start;
+   sm->indice_count = pscene->indice_count - sm->indice_start;
+   
+   pscene->submesh.indice_start = pscene->indice_count;
+}
+
 static void scene_shadow_sphere( scene *pscene, v3f sphere, 
       v4f params, v3f lightdir )
 {
@@ -438,19 +456,30 @@ static void scene_shadow_gradient( scene *pscene, int comp,
 }
 
 /* Temporary */
-static int sample_scene_height( scene *pscene, v3f pos )
+static int sample_scene_height( scene *pscene, v3f pos, v3f norm )
 {
    for( int i=0; i<pscene->indice_count/3; i++ )
    {
       u32 *tri = &pscene->indices[i*3];
+
+      float *pA = pscene->verts[tri[0]].co,
+            *pB = pscene->verts[tri[1]].co,
+            *pC = pscene->verts[tri[2]].co;
       
       float height;
-      if( triangle_raycast( 
-            pscene->verts[ tri[0] ].co,
-            pscene->verts[ tri[1] ].co,
-            pscene->verts[ tri[2] ].co, pos, &height ))
+      if( triangle_raycast( pA, pB, pC, pos, &height ))
       {
          pos[1] = height;
+         
+         if( norm )
+         {
+            v3f v0, v1;
+            v3_sub( pA, pB, v0 );
+            v3_sub( pC, pB, v1 );
+            v3_cross( v1, v0, norm );
+            v3_normalize( norm );
+         }
+
          return 1;
       }
    }
@@ -757,37 +786,9 @@ static void scene_compute_occlusion( scene *pscene )
 
 static void scene_upload( scene *pscene )
 {
-   glGenVertexArrays( 1, &pscene->vao );
-   glGenBuffers( 1, &pscene->vbo );
-   glGenBuffers( 1, &pscene->ebo );
-   glBindVertexArray( pscene->vao );
-
-   glBindBuffer( GL_ARRAY_BUFFER, pscene->vbo );
-   glBufferData( GL_ARRAY_BUFFER, pscene->vertex_count*sizeof(model_vert),
-         pscene->verts, GL_STATIC_DRAW );
-
-   glBindVertexArray( pscene->vao );
-   glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pscene->ebo );
-   glBufferData( GL_ELEMENT_ARRAY_BUFFER, pscene->indice_count*sizeof(u32),
-         pscene->indices, GL_STATIC_DRAW );
-   
-   glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 
-         sizeof(model_vert), (void*)0 );
-   glEnableVertexAttribArray( 0 );
-
-   glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 
-         sizeof(model_vert), (void *)offsetof(model_vert, norm) );
-   glEnableVertexAttribArray( 1 );
-
-   glVertexAttribPointer( 2, 4, GL_FLOAT, GL_FALSE, 
-         sizeof(model_vert), (void *)offsetof(model_vert, colour) );
-   glEnableVertexAttribArray( 2 );
-
-   glVertexAttribPointer( 3, 2, GL_FLOAT, GL_FALSE, 
-         sizeof(model_vert), (void *)offsetof(model_vert, uv) );
-   glEnableVertexAttribArray( 3 );
-
-   VG_CHECK_GL();
+   mesh_upload( &pscene->mesh,
+         pscene->verts, pscene->vertex_count,
+         pscene->indices, pscene->indice_count );
 
    vg_info( "Scene upload\n" );
    vg_info( "   indices:%u\n", pscene->indice_count );
@@ -795,7 +796,8 @@ static void scene_upload( scene *pscene )
 }
 
 float scene_tree_sway = 0.1f;
-static void scene_draw( scene *pscene, int count, int start )
+
+static void scene_foliage_shader_use(void)
 {
    SHADER_USE( shader_debug_vcol );
 
@@ -815,70 +817,65 @@ static void scene_draw( scene *pscene, int count, int start )
    glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uTime" ), vg_time );
    glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uSwayAmt" ), 
          scene_tree_sway );
+}
 
-   glBindVertexArray( pscene->vao );
+static void scene_bind( scene *pscene )
+{
+   mesh_bind( &pscene->mesh );
+}
 
-   if( count == -1 )
-   {
-      glDrawElements( GL_TRIANGLES, pscene->indice_count,
-         GL_UNSIGNED_INT,
-         (void *)0
-      );
-   }
-   else
-   {
-      glDrawElements( GL_TRIANGLES, count,
-         GL_UNSIGNED_INT,
-         (void *)(start*sizeof(u32))
-      );
-   }
+static void scene_draw( scene *pscene )
+{
+   mesh_drawn( 0, pscene->indice_count );
+}
 
-   if( debugsdf )
+static void scene_debugsdf( scene *pscene )
+{
+   for( int i=0; i<pscene->shadower_count; i++ )
    {
-      for( int i=0; i<pscene->shadower_count; i++ )
-      {
-         struct shadower *shadower = &pscene->shadowers[i];
+      struct shadower *shadower = &pscene->shadowers[i];
 
-         v3f base, side;
-         v3_copy( shadower->sdf.origin, base );
-         base[1] -= shadower->sdf.info[1];
-         v3_copy( base, side );
-         side[0] += shadower->sdf.info[0];
+      v3f base, side;
+      v3_copy( shadower->sdf.origin, base );
+      base[1] -= shadower->sdf.info[1];
+      v3_copy( base, side );
+      side[0] += shadower->sdf.info[0];
 
-         vg_line2( shadower->sdf.origin, base, 0xff00ff00, 0xff0000ff );
-         vg_line2( side, base, 0xff00ff00, 0xff0000ff );
-         vg_line( side, shadower->sdf.origin, 0xff00ff00 );
-      }
+      vg_line2( shadower->sdf.origin, base, 0xff00ff00, 0xff0000ff );
+      vg_line2( side, base, 0xff00ff00, 0xff0000ff );
+      vg_line( side, shadower->sdf.origin, 0xff00ff00 );
+   }
 
-      v3f p0 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[0][2] },
-          p1 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[0][2] }, 
-          p2 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[0][2] },
-          p3 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[0][2] },
+   v3f p0 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[0][2] },
+       p1 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[0][2] }, 
+       p2 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[0][2] },
+       p3 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[0][2] },
 
-          p4 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[1][2] },
-          p5 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[1][2] }, 
-          p6 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[1][2] },
-          p7 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[1][2] };
-      
-      u32 col = 0xffff00c8;
-      vg_line( p0, p1, col );
-      vg_line( p1, p2, col );
-      vg_line( p2, p3, col );
-      vg_line( p3, p0, col );
-
-      vg_line( p4, p5, col );
-      vg_line( p5, p6, col );
-      vg_line( p6, p7, col );
-      vg_line( p7, p4, col );
-
-      vg_line( p0, p4, col );
-      vg_line( p1, p5, col );
-      vg_line( p2, p6, col );
-      vg_line( p3, p7, col );
-   }
+       p4 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[1][2] },
+       p5 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[1][2] }, 
+       p6 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[1][2] },
+       p7 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[1][2] };
+   
+   u32 col = 0xffff00c8;
+   vg_line( p0, p1, col );
+   vg_line( p1, p2, col );
+   vg_line( p2, p3, col );
+   vg_line( p3, p0, col );
+
+   vg_line( p4, p5, col );
+   vg_line( p5, p6, col );
+   vg_line( p6, p7, col );
+   vg_line( p7, p4, col );
+
+   vg_line( p0, p4, col );
+   vg_line( p1, p5, col );
+   vg_line( p2, p6, col );
+   vg_line( p3, p7, col );
 }
 
 static void scene_register(void)
 {
    SHADER_INIT( shader_debug_vcol );
+   SHADER_INIT( shader_standard_lit );
+   SHADER_INIT( shader_unlit );
 }