Prep work for procedural stars & stuff

This commit is contained in:
Brad Davis 2015-06-22 09:42:21 -07:00
parent 02510790c3
commit 1b44c220a7
5 changed files with 88 additions and 4 deletions

View file

@ -65,10 +65,10 @@ void ViewFrustum::setProjection(const glm::mat4& projection) {
_farClip = -_corners[BOTTOM_LEFT_FAR].z;
_aspectRatio = (_corners[TOP_RIGHT_NEAR].x - _corners[BOTTOM_LEFT_NEAR].x) /
(_corners[TOP_RIGHT_NEAR].y - _corners[BOTTOM_LEFT_NEAR].y);
vec4 top = _inverseProjection * vec4(0, 1, -1, 1);
top /= top.w;
static const vec3 center(0, 0, -1);
_fieldOfView = glm::degrees(2.0f * abs(glm::angle(center, glm::normalize(vec3(top)))));
glm::vec4 top = _inverseProjection * vec4(0, 1, -1, 1);
top /= top.w;
_fieldOfView = abs(glm::degrees(2.0f * abs(glm::angle(vec3(0, 0, -1), glm::normalize(vec3(top))))));
}
// ViewFrustum::calculateViewFrustum()

View file

@ -1185,6 +1185,11 @@ void GeometryCache::renderQuad(gpu::Batch& batch, const glm::vec2& minCorner, co
batch.draw(gpu::QUADS, 4, 0);
}
void GeometryCache::renderUnitCube(gpu::Batch& batch) {
static const glm::vec4 color(1);
renderSolidCube(batch, 1, color);
}
void GeometryCache::renderUnitQuad(const glm::vec4& color, int id) {
gpu::Batch batch;
renderUnitQuad(batch, color, id);

View file

@ -155,6 +155,7 @@ public:
void renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance, const glm::vec4& color, int id = UNKNOWN_ID);
void renderBevelCornersRect(gpu::Batch& batch, int x, int y, int width, int height, int bevelDistance, const glm::vec4& color, int id = UNKNOWN_ID);
void renderUnitCube(gpu::Batch& batch);
void renderUnitQuad(const glm::vec4& color = glm::vec4(1), int id = UNKNOWN_ID);
void renderUnitQuad(gpu::Batch& batch, const glm::vec4& color = glm::vec4(1), int id = UNKNOWN_ID);

View file

@ -16,6 +16,7 @@
<$declareStandardTransform()$>
varying vec3 varPosition;
varying vec3 varNormal;
varying vec2 varTexcoord;
varying vec4 varColor;
@ -30,4 +31,5 @@ void main(void) {
<$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$>
<$transformModelToEyeDir(cam, obj, gl_Normal, varNormal)$>
varNormal = normalize(varNormal);
varPosition = gl_Vertex.xyz;
}

View file

@ -0,0 +1,76 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
// stars.frag
// fragment shader
//
// Created by Bradley Austin Davis on 2015/06/19
varying vec2 varTexcoord;
varying vec3 varNomral;
varying vec3 varPosition;
const int star_iterations = 14;
const float time_scale = 0.2;
const vec3 col_star = vec3( 1.0, 0.7, 0.5 );
float hash( float n ) { return fract(sin(n)*123.456789); }
vec2 rotate( in vec2 uv, float a)
{
float c = cos( a );
float s = sin( a );
return vec2( c * uv.x - s * uv.y, s * uv.x + c * uv.y );
}
float noise( in vec3 p )
{
vec3 fl = floor( p );
vec3 fr = fract( p );
fr = fr * fr * ( 3.0 - 2.0 * fr );
float n = fl.x + fl.y * 157.0 + 113.0 * fl.z;
return mix( mix( mix( hash( n + 0.0), hash( n + 1.0 ), fr.x ),
mix( hash( n + 157.0), hash( n + 158.0 ), fr.x ), fr.y ),
mix( mix( hash( n + 113.0), hash( n + 114.0 ), fr.x ),
mix( hash( n + 270.0), hash( n + 271.0 ), fr.x ), fr.y ), fr.z );
}
float fbm( in vec2 p, float t )
{
float f;
f = 0.5000 * noise( vec3( p, t ) ); p *= 2.1;
f += 0.2500 * noise( vec3( p, t ) ); p *= 2.2;
f += 0.1250 * noise( vec3( p, t ) ); p *= 2.3;
f += 0.0625 * noise( vec3( p, t ) );
return f;
}
vec3 doBackgroundStars( in vec3 dir )
{
vec3 n = abs( dir );
vec2 uv = ( n.x > n.y && n.x > n.z ) ? dir.yz / dir.x:
( n.y > n.x && n.y > n.z ) ? dir.zx / dir.y:
dir.xy / dir.z;
float f = 0.0;
for( int i = 0 ; i < star_iterations; ++i )
{
uv = rotate( 1.07 * uv + vec2( 0.7 ), 0.5 );
float t = 10. * uv.x * uv.y;
vec2 u = cos( 100. * uv ) * fbm( 10. * uv, 0.0 );
f += smoothstep( 0.5, 0.55, u.x * u.y ) * ( 0.25 * sin( t ) + 0.75 );
}
return f * col_star;
}
void main(void) {
vec3 c = doBackgroundStars( normalize(varPosition) );
c = pow( c, vec3( 0.4545 ) );
gl_FragColor = vec4( c, 1.0 );
}