Skip to content

Commit

Permalink
Add standard-deriv based normal mapping, update ammo.js
Browse files Browse the repository at this point in the history
- Implemented standard derivatives based normal mapping from
http://www.thetenthplanet.de/archives/1180 to replace existing
screen-space normal map hack where supported.
- Upgraded ammo.js version
- Removed outdated octree tests.
- Added basic loading of available extensions to GLCore.extensions.
  • Loading branch information
cjcliffe committed Jan 12, 2014
1 parent cc8a96c commit 195cbb6
Show file tree
Hide file tree
Showing 13 changed files with 6,225 additions and 6,842 deletions.
19 changes: 17 additions & 2 deletions CubicVR.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ usage:
resize_active: false,
emptyLight: null,
resizeList: [],
canvasSizeFactor:1
canvasSizeFactor:1,
extensions: {
}
};

function addFullscreenSupport() {
Expand Down Expand Up @@ -318,6 +320,19 @@ usage:
}

GLCore.gl = gl;
var available_extensions = gl.getSupportedExtensions();

GLCore.extensions.texture_filter_anisotropic = gl.getExtension("EXT_texture_filter_anisotropic");
GLCore.extensions.element_index_uint = gl.getExtension("OES_element_index_uint");
GLCore.extensions.standard_derivatives = gl.getExtension("OES_standard_derivatives");
GLCore.extensions.texture_float = gl.getExtension("OES_texture_float");
GLCore.extensions.texture_float_linear = gl.getExtension("OES_texture_float_linear");
GLCore.extensions.compressed_texture_s3tc = gl.getExtension("WEBGL_compressed_texture_s3tc");
//GLCore.extensions.lose_context = WEBGL_lose_context

// MOZ_WEBGL_lose_context
// MOZ_WEBGL_compressed_texture_s3tc


if (GLCore.fixed_size !== null) {
GLCore.width = GLCore.fixed_size[0];
Expand Down Expand Up @@ -733,7 +748,7 @@ usage:

}(window, window.document, Math, function(){}));

window.CubicVRShader = {}; // for embedding shaders and keeping context happy
window.CubicVRShader = {}; // for embedding shaders and keeping context happy

/* CubicVR:Makefile-cut */
/* --- SNIP FOR MINIFICATION --- */
Expand Down
56 changes: 56 additions & 0 deletions CubicVR_Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,70 @@ vec2 cubicvr_texCoord() {
#endif
}

#if TEXTURE_NORMAL && OES_STANDARD_DERIVATIVES

#extension GL_OES_standard_derivatives : enable

// Normal Mapping Without Precomputed Tangents: 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 1
//#define WITH_NORMALMAP_GREEN_UP 1
//#define WITH_NORMAL_2CHANNEL 1

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 = texture2D( textureNormal, 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 );
}

#endif


vec3 cubicvr_normal(vec2 texCoord) {
#if TEXTURE_NORMAL && !LIGHT_DEPTH_PASS

// use standard derivatives version if available
#if OES_STANDARD_DERIVATIVES
return perturb_normal(vertexNormalOut, vertexPositionOut.xyz, texCoord);
#else
// fake it otherwise, doesn't play well with rotation
vec3 bumpNorm = vec3(texture2D(textureNormal, texCoord));

vec3 n = (vec4(normalize(vertexNormalOut),1.0)).xyz;
bumpNorm = (bumpNorm-0.5)*2.0;
bumpNorm.y = -bumpNorm.y;
return normalize((n+bumpNorm)/2.0);
#endif

#else
return normalize(vertexNormalOut);
#endif
Expand Down
Loading

0 comments on commit 195cbb6

Please sign in to comment.