game_kernel 0.1.0

A 3D game engine written entirely in rust
Documentation
#version 450

layout(location = 0) in vec3 normal;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 view;
layout(location = 3) in vec3 mpos;

/*layout(set = 0, binding = 0) uniform sampler2D diffuse;
layout(set = 0, binding = 1) uniform sampler2D specular;*/
layout(set = 0, binding = 0) uniform sampler2D normal_map;

layout(location = 0) out vec4 color;
layout(location = 1) out vec2 velocity;

layout(set = 1, binding = 0) buffer Projection
{
    mat4 view, 
         projection;
    uvec2 res;
}projection;

layout(set = 1, binding = 1) buffer PrevMvps 
{
    mat4 mvp;
}prev;

//http://www.thetenthplanet.de/archives/1180
mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv ) { 
    // get edge vectors of the pixel triangle 
    vec3 dp1 = dFdx( p );
    vec3 dp2 = dFdy( p );
    vec2 duv1 = dFdx( uv );
    vec2 duv2 = dFdy( uv );

    // solve the linear system 
    vec3 dp2perp = cross( dp2, N );
    vec3 dp1perp = cross( N, dp1 );
    vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
    vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;   

    // construct a scale-invariant frame 
    float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
    return mat3( T * invmax, B * invmax, N ); 
}

#define WITH_NORMALMAP_UNSIGNED

vec3 perturb_normal( vec3 N, vec3 V, vec2 texcoord ) { 
    // assume N, the interpolated vertex normal and 
    // V, the view vector (vertex to eye) 
    vec3 map = texture( normal_map, texcoord ).xyz; 
#ifdef WITH_NORMALMAP_UNSIGNED 
    map = map * 255./127. - 128./127.; 
#endif 
#ifdef WITH_NORMALMAP_2CHANNEL 
    map.z = sqrt( 1. - dot( map.xy, map.xy ) ); 
#endif 
#ifdef WITH_NORMALMAP_GREEN_UP 
    map.y = -map.y; 
#endif 

    mat3 TBN = cotangent_frame( N, -V, texcoord ); 
    return normalize( TBN * (map - vec3 (0.07, 0.07, 0.0)));
}

void main()
{
    //color.xyz = (normal)/2.0+0.5;
    color.xyz = perturb_normal(normal, view, uv) / 2.0+0.5;
    //color.xyz = normal/2.0+0.5;
    color.w = 1;
    
    vec4 prev_spos = prev.mvp * vec4(mpos, 1.0);
    prev_spos.xyz /= prev_spos.w;
    vec2 svelocity = gl_FragCoord.xy / vec2(projection.res) - (prev_spos.xy / 2 + 0.5);
    velocity = svelocity / 2 + 0.5;
}