Skip to content

Commit

Permalink
Refactored the balloon envelope shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
andgi committed May 7, 2016
1 parent be85867 commit d99baac
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 77 deletions.
47 changes: 47 additions & 0 deletions Models/Effects/balloon-envelope-geometry.vert
@@ -0,0 +1,47 @@
// -*-C++-*-
// Balloon envelope geometry vertex shader function.
//
// Copyright (C) 2010 - 2016 Anders Gidenstam (anders(at)gidenstam.org)
// This file is licensed under the GPL license version 2 or later.

#version 120

uniform float gas_level_ft;
const float r = 6.185; // [meter]

varying float pressureDelta, angle;//, looseness;
varying vec3 tangent;

void balloon_envelope_geometry_func(out vec4 oPosition, out vec3 oNormal)
{
// Compute vertex position in object space.
oPosition = gl_Vertex;
oNormal = gl_Normal;

float h = max(1.0, 0.3048 * gas_level_ft); // [meter]

pressureDelta = 0.0;
//looseness = 0.0;
if (oPosition.z < r - h) {
if (h < r) {
float lxy = length(oPosition.xy);
float lmax = max(0.1,r*sqrt(1.0 - ((r-h)/r)*((r-h)/r)));
oPosition.xy *= min(1.0,lmax/lxy);
}
float lxy = length(oPosition.xy);
float nz = r - (h + pow(1.0 - lxy/r, 5.0)*(2.5*r - h));
float dlxy = -5.0*(2.5*r - h)/r * pow(1.0 - lxy/r, 4.0); // Derivative map lxy

oNormal.z = sqrt(1.0 - dot(oNormal.xy,oNormal.xy))/dlxy;
oNormal = normalize(oNormal);

oPosition.z = min(r - h, max(nz, oPosition.z));
pressureDelta = sqrt(-0.5*(h + oPosition.z - r)/r);
}

// The balloon envelope is assumed to be symetric around the z axis.
vec2 tmp = normalize(oPosition.xy);
angle = asin(tmp.y);
vec3 oTangent = vec3(-tmp.y, tmp.x, 0);
tangent = gl_NormalMatrix * oTangent;
}
45 changes: 6 additions & 39 deletions Models/Effects/balloon-envelope-lightfield.vert
Expand Up @@ -3,7 +3,7 @@
//
// Copyright (C) 2009 - 2011 Tim Moore (timoore(at)redhat.com)
// Copyright (C) 2011 - 2012 Thorsten Renk
// Copyright (C) 2012 - 2015 Anders Gidenstam (anders(at)gidenstam.org)
// Copyright (C) 2012 - 2016 Anders Gidenstam (anders(at)gidenstam.org)
// This file is licensed under the GPL license version 2 or later.

// Shader that uses OpenGL state values to do per-pixel lighting
Expand All @@ -20,6 +20,8 @@
#define MODE_DIFFUSE 1
#define MODE_AMBIENT_AND_DIFFUSE 2

void balloon_envelope_geometry_func(out vec4 oPosition, out vec3 oNormal);

// The constant term of the lighting equation that doesn't depend on
// the surface normal is passed in gl_{Front,Back}Color. The alpha
// component is set to 1 for front, 0 for back in order to work around
Expand Down Expand Up @@ -53,13 +55,6 @@ const float terminator_width = 200000.0;
float earthShade;


// Balloon specific
uniform float gas_level_ft;
const float r = 6.185; // [meter]
varying vec3 tangent;
varying float pressureDelta, angle;//, looseness;
// End Balloon specific

//////////////////////

float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
Expand All @@ -76,37 +71,9 @@ float light_func (in float x, in float a, in float b, in float c, in float d, in
void main()
{
// Balloon specific
// Compute vertex position in object space.
vec4 oPosition = gl_Vertex;
vec3 oNormal = gl_Normal;

float h = max(1.0, 0.3048 * gas_level_ft); // [meter]

pressureDelta = 0.0;
//looseness = 0.0;
if (oPosition.z < r - h) {
if (h < r) {
float lxy = length(oPosition.xy);
float lmax = max(0.1,r*sqrt(1.0 - ((r-h)/r)*((r-h)/r)));
oPosition.xy *= min(1.0,lmax/lxy);
}
float lxy = length(oPosition.xy);
float nz = r - (h + pow(1.0 - lxy/r, 5.0)*(2.5*r - h));
float dlxy = -5.0*(2.5*r - h)/r * pow(1.0 - lxy/r, 4.0); // Derivative map lxy

oNormal.z = sqrt(1.0 - dot(oNormal.xy,oNormal.xy))/dlxy;
oNormal = normalize(oNormal);

oPosition.z = min(r - h, max(nz, oPosition.z));
pressureDelta = sqrt(-0.5*(h + oPosition.z - r)/r);
}

// The balloon envelope is assumed to be symetric around the z axis.
vec2 tmp = normalize(oPosition.xy);
angle = asin(tmp.y);
vec3 oTangent = vec3(-tmp.y, tmp.x, 0);
tangent = gl_NormalMatrix * oTangent;

vec4 oPosition;
vec3 oNormal;
balloon_envelope_geometry_func(oPosition, oNormal);
// End Balloon specific
// Default terrain-haze vertex shader below, except that oPosition replaces
// gl_Vertex and oNormal replaces gl_Normal.
Expand Down
4 changes: 3 additions & 1 deletion Models/Effects/balloon-envelope.eff
Expand Up @@ -2,7 +2,7 @@
<!--
ZF Navy free balloon for FlightGear.
Copyright (C) 2010 - 2012 Anders Gidenstam (anders(at)gidenstam.org)
Copyright (C) 2010 - 2016 Anders Gidenstam (anders(at)gidenstam.org)
This file is licensed under the GPL license version 2 or later.
-->
<PropertyList>
Expand All @@ -20,6 +20,7 @@
<pass n="0">
<program n="0">
<vertex-shader n="0">Aircraft/ZF_Navy_free_balloon/Models/Effects/balloon-envelope-lightfield.vert</vertex-shader>
<vertex-shader n="1">Aircraft/ZF_Navy_free_balloon/Models/Effects/balloon-envelope-geometry.vert</vertex-shader>
<fragment-shader n="0">Aircraft/ZF_Navy_free_balloon/Models/Effects/balloon-envelope-lightfield.frag</fragment-shader>
</program>
<uniform n="8">
Expand Down Expand Up @@ -56,6 +57,7 @@
<pass n="0">
<program n="0">
<vertex-shader n="1">Aircraft/ZF_Navy_free_balloon/Models/Effects/balloon-envelope.vert</vertex-shader>
<vertex-shader n="2">Aircraft/ZF_Navy_free_balloon/Models/Effects/balloon-envelope-geometry.vert</vertex-shader>
<fragment-shader n="1">Aircraft/ZF_Navy_free_balloon/Models/Effects/balloon-envelope.frag</fragment-shader>
</program>
<uniform n="1">
Expand Down
43 changes: 8 additions & 35 deletions Models/Effects/balloon-envelope.vert
Expand Up @@ -2,7 +2,7 @@
// Balloon envelope vertex shader based on Shaders/model-default.vert.
//
// Copyright (C) 2009 - 2010 Tim Moore (timoore(at)redhat.com)
// Copyright (C) 2010 - 2015 Anders Gidenstam (anders(at)gidenstam.org)
// Copyright (C) 2010 - 2016 Anders Gidenstam (anders(at)gidenstam.org)
// This file is licensed under the GPL license version 2 or later.

// Shader that uses OpenGL state values to do per-pixel lighting
Expand All @@ -19,16 +19,14 @@
#define MODE_DIFFUSE 1
#define MODE_AMBIENT_AND_DIFFUSE 2

uniform float gas_level_ft;
const float r = 6.185; // [meter]
void balloon_envelope_geometry_func(out vec4 oPosition, out vec3 oNormal);

// The ambient term of the lighting equation that doesn't depend on
// the surface normal is passed in gl_{Front,Back}Color. The alpha
// component is set to 1 for front, 0 for back in order to work around
// bugs with gl_FrontFacing in the fragment shader.
varying vec4 diffuse_term;
varying vec3 normal, tangent;
varying float pressureDelta, angle;//, looseness;
varying vec3 normal;
uniform int colorMode;

////fog "include" /////
Expand All @@ -39,36 +37,11 @@ vec3 fog_Func(vec3 color, int type);

void main()
{
// Compute vertex position in object space.
vec4 oPosition = gl_Vertex;
vec3 oNormal = gl_Normal;

float h = max(1.0, 0.3048 * gas_level_ft); // [meter]

pressureDelta = 0.0;
//looseness = 0.0;
if (oPosition.z < r - h) {
if (h < r) {
float lxy = length(oPosition.xy);
float lmax = max(0.1,r*sqrt(1.0 - ((r-h)/r)*((r-h)/r)));
oPosition.xy *= min(1.0,lmax/lxy);
}
float lxy = length(oPosition.xy);
float nz = r - (h + pow(1.0 - lxy/r, 5.0)*(2.5*r - h));
float dlxy = -5.0*(2.5*r - h)/r * pow(1.0 - lxy/r, 4.0); // Derivative map lxy

oNormal.z = sqrt(1.0 - dot(oNormal.xy,oNormal.xy))/dlxy;
oNormal = normalize(oNormal);

oPosition.z = min(r - h, max(nz, oPosition.z));
pressureDelta = sqrt(-0.5*(h + oPosition.z - r)/r);
}

// The balloon envelope is assumed to be symetric around the z axis.
vec2 tmp = normalize(oPosition.xy);
angle = asin(tmp.y);
vec3 oTangent = vec3(-tmp.y, tmp.x, 0);
tangent = gl_NormalMatrix * oTangent;
// Balloon specific
vec4 oPosition;
vec3 oNormal;
balloon_envelope_geometry_func(oPosition, oNormal);
// End Balloon specific

// Default vertex shader below, except that oPosition replaces
// gl_Vertex and oNormal replaces gl_Normal.
Expand Down
4 changes: 2 additions & 2 deletions ZF_Navy_free_balloon-set.xml
Expand Up @@ -2,7 +2,7 @@
<!--
ZF Navy free gas balloon model for FlightGear.
Copyright (C) 2006 - 2013 Anders Gidenstam (anders(at)gidenstam.org)
Copyright (C) 2006 - 2016 Anders Gidenstam (anders(at)gidenstam.org)
This file is licensed under the GPL license version 2 or later.
-->
<PropertyList>
Expand All @@ -11,7 +11,7 @@

<description>US Navy ZF free gas balloon</description>
<author>Anders Gidenstam</author>
<aircraft-version>$Id: ZF_Navy_free_balloon-set.xml,v 1.29 2013/01/07 21:00:19 anders Exp $</aircraft-version>
<aircraft-version>$Id: ZF_Navy_free_balloon-set.xml,v 1.30 2016/05/07 14:30:13 anders Exp $</aircraft-version>

<status type="string">beta</status>
<rating>
Expand Down

0 comments on commit d99baac

Please sign in to comment.