Calculate RenderableModelProjection target position in double precision

This commit is contained in:
Erik Broberg
2016-08-08 20:23:46 -04:00
parent 87595b3452
commit a0924588fd
3 changed files with 87 additions and 64 deletions

View File

@@ -131,9 +131,7 @@ bool RenderableModelProjection::initialize() {
);
completeSuccess &= loadTextures();
completeSuccess &= _projectionComponent.initialize();
completeSuccess &= _geometry->initialize(this);
completeSuccess &= !_source.empty();
completeSuccess &= !_destination.empty();
@@ -174,13 +172,24 @@ void RenderableModelProjection::render(const RenderData& data) {
attitudeParameters(_time);
_imageTimes.clear();
// Calculate variables to be used as uniform variables in shader
glm::dvec3 bodyPosition = data.positionVec3;
// Model transform and view transform needs to be in double precision
glm::dmat4 modelTransform =
glm::translate(glm::dmat4(1.0), bodyPosition) * // Translation
glm::dmat4(_stateMatrix); // Rotation
glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() * modelTransform;
glm::vec3 directionToSun = glm::normalize(_sunPosition.vec3() - glm::vec3(bodyPosition));
glm::vec3 directionToSunViewSpace = glm::mat3(data.camera.combinedViewMatrix()) * directionToSun;
_programObject->setUniform("_performShading", _performShading);
_programObject->setUniform("sun_pos", _sunPosition.vec3());
_programObject->setUniform("ViewProjection", data.camera.viewProjectionMatrix());
_programObject->setUniform("ModelTransform", _transform);
_programObject->setUniform("directionToSunViewSpace", directionToSunViewSpace);
_programObject->setUniform("modelViewTransform", glm::mat4(modelViewTransform));
_programObject->setUniform("projectionTransform", data.camera.projectionMatrix());
_programObject->setUniform("_projectionFading", _projectionComponent.projectionFading());
setPscUniforms(*_programObject, data.camera, data.position);
_geometry->setUniforms(*_programObject);

View File

@@ -22,64 +22,74 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include "PowerScaling/powerScaling_fs.hglsl"
#include "fragment.glsl"
in vec4 vs_position;
in vec4 vs_normal;
in vec2 vs_st;
uniform vec4 campos;
uniform vec4 objpos;
uniform vec3 camdir;
uniform sampler2D baseTexture;
uniform sampler2D projectionTexture;
uniform bool _performShading;
uniform float _projectionFading;
uniform vec3 sun_pos;
uniform vec3 directionToSunViewSpace;
in vec2 vs_st;
in vec3 vs_normalViewSpace;
in vec4 vs_positionScreenSpace;
in vec4 vs_positionCameraSpace;
#include "PowerScaling/powerScaling_fs.hglsl"
#include "fragment.glsl"
Fragment getFragment() {
vec4 position = vs_position;
float depth = pscDepth(position);
// directional lighting
vec3 origin = vec3(0.0);
vec4 spec = vec4(0.0);
vec3 n = normalize(vs_normal.xyz);
vec3 e = normalize(camdir);
vec3 l_pos = sun_pos;
vec3 l_dir = normalize(l_pos-objpos.xyz);
float intensity = 1;
if (_performShading) {
const float terminatorBrightness = 0.4;
intensity = min(max(5*dot(n,l_dir), terminatorBrightness), 1.0);
}
float shine = 0.0001;
vec4 specular = vec4(0.1);
vec4 ambient = vec4(vec3(0.0), 1.0);
//Specular
if (intensity > 0.0) {
vec3 h = normalize(l_dir + e);
float intSpec = max(dot(h,n),0.0);
spec = specular * pow(intSpec, shine);
}
vec4 textureColor = texture(baseTexture, vs_st);
vec4 textureColor = texture(baseTexture, vs_st);
vec4 projectionColor = texture(projectionTexture, vs_st);
if (projectionColor.a != 0.0) {
if (projectionColor.a > 0.0) {
textureColor.rgb = mix(
textureColor.rgb,
projectionColor.rgb,
min(_projectionFading, projectionColor.a)
);
}
vec3 diffuseAlbedo = textureColor.rgb;
vec3 specularAlbedo = vec3(1);
vec3 color;
if (_performShading) {
// Some of these values could be passed in as uniforms
vec3 lightColorAmbient = vec3(1);
vec3 lightColor = vec3(1);
vec3 n = normalize(vs_normalViewSpace);
vec3 l = directionToSunViewSpace;
vec3 c = normalize(vs_positionCameraSpace.xyz);
vec3 r = reflect(l, n);
float ambientIntensity = 0.2;
float diffuseIntensity = 1;
float specularIntensity = 1;
float diffuseCosineFactor = dot(n,l);
float specularCosineFactor = dot(c,r);
float specularPower = 100;
vec3 ambientColor = ambientIntensity * lightColorAmbient * diffuseAlbedo;
vec3 diffuseColor = specularIntensity * lightColor * diffuseAlbedo * max(diffuseCosineFactor, 0);
vec3 specularColor = specularIntensity * lightColor * specularAlbedo * pow(max(specularCosineFactor, 0), specularPower);
color = ambientColor + diffuseColor + specularColor;
}
else {
color = diffuseAlbedo;
}
float transparency = 1.0;
float alpha = _projectionFading * transparency;
Fragment frag;
frag.color = max(intensity * textureColor, ambient);
frag.depth = depth;
frag.color = vec4(color, alpha);
frag.depth = vs_positionScreenSpace.w;
return frag;
}

View File

@@ -30,27 +30,31 @@ layout(location = 0) in vec4 in_position;
layout(location = 1) in vec2 in_st;
layout(location = 2) in vec3 in_normal;
out vec4 vs_position;
out vec4 vs_normal;
out vec2 vs_st;
uniform mat4 modelViewTransform;
uniform mat4 projectionTransform;
uniform mat4 ViewProjection;
uniform mat4 ModelTransform;
uniform vec3 cameraDirectionWorldSpace;
uniform float _magnification;
// Outputs
out vec2 vs_st;
out vec3 vs_normalViewSpace;
out vec4 vs_positionScreenSpace;
out vec4 vs_positionCameraSpace;
void main() {
vec4 pos = in_position;
pos.w += _magnification;
vec4 position = in_position;
position.xyz *= pow(10, _magnification);
vs_positionCameraSpace = modelViewTransform * position;
vec4 positionClipSpace = projectionTransform * vs_positionCameraSpace;
// Write output
vs_st = in_st;
vs_position = pos;
vec4 tmp = pos;
vs_positionScreenSpace = z_normalization(positionClipSpace);
gl_Position = vs_positionScreenSpace;
vs_normal = normalize(ModelTransform * vec4(in_normal,0));
vec4 position = pscTransform(tmp, ModelTransform);
vs_position = tmp;
position = ViewProjection * position;
gl_Position = z_normalization(position);
// The normal transform should be the transposed inverse of the model transform?
vs_normalViewSpace = normalize(mat3(modelViewTransform) * in_normal);
}