Android Open Source - FxCameraApp Gl Glass Sphere Shader






From Project

Back to project page FxCameraApp.

License

The source code is released under:

MIT License

If you think the Android project FxCameraApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.af.experiments.FxCameraApp.shaders;
//from   ww  w  .j a v a  2s  .  c o m
import static android.opengl.GLES20.glUniform1f;
import static android.opengl.GLES20.glUniform2f;


public class GlGlassSphereShader extends GlShader {

    protected String mShaderName = "fish eye";

    @Override
    public String getName() {
        return mShaderName;
    }

    private static final String FRAGMENT_SHADER =
            "precision mediump float;" +

                    "varying highp vec2 vTextureCoord;" +
                    "uniform lowp sampler2D sTexture;" +

                    "uniform highp vec2 center;" +
                    "uniform highp float radius;" +
                    "uniform highp float aspectRatio;" +
                    "uniform highp float refractiveIndex;" +
                    //"uniform vec3 lightPosition;" +

                    "const highp vec3 lightPosition = vec3(-0.5, 0.5, 1.0);" +
                    "const highp vec3 ambientLightPosition = vec3(0.0, 0.0, 1.0);" +

                    "void main() {" +
                    "highp vec2 textureCoordinateToUse = vec2(vTextureCoord.x, (vTextureCoord.y * aspectRatio + 0.5 - 0.5 * aspectRatio));" +
                    "highp float distanceFromCenter = distance(center, textureCoordinateToUse);" +
                    "lowp float checkForPresenceWithinSphere = step(distanceFromCenter, radius);" +

                    "distanceFromCenter = distanceFromCenter / radius;" +

                    "highp float normalizedDepth = radius * sqrt(1.0 - distanceFromCenter * distanceFromCenter);" +
                    "highp vec3 sphereNormal = normalize(vec3(textureCoordinateToUse - center, normalizedDepth));" +

                    "highp vec3 refractedVector = 2.0 * refract(vec3(0.0, 0.0, -1.0), sphereNormal, refractiveIndex);" +
                    "refractedVector.xy = -refractedVector.xy;" +

                    "highp vec3 finalSphereColor = texture2D(sTexture, (refractedVector.xy + 1.0) * 0.5).rgb;" +

                    // Grazing angle lighting
                    "highp float lightingIntensity = 2.5 * (1.0 - pow(clamp(dot(ambientLightPosition, sphereNormal), 0.0, 1.0), 0.25));" +
                    "finalSphereColor += lightingIntensity;" +

                    // Specular lighting
                    "lightingIntensity  = clamp(dot(normalize(lightPosition), sphereNormal), 0.0, 1.0);" +
                    "lightingIntensity  = pow(lightingIntensity, 15.0);" +
                    "finalSphereColor += vec3(0.8, 0.8, 0.8) * lightingIntensity;" +

                    "gl_FragColor = vec4(finalSphereColor, 1.0) * checkForPresenceWithinSphere;" +
                    "}";

    private float mCenterX = 0.5f;
    private float mCenterY = 0.5f;
    private float mRadius = 0.5f;
    private float mAspectRatio = 1.0f;
    private float mRefractiveIndex = 0.71f;

    public GlGlassSphereShader() {
        super(DEFAULT_VERTEX_SHADER, FRAGMENT_SHADER);
    }


    public float getCenterX() {
        return mCenterX;
    }

    public void setCenterX(final float centerX) {
        mCenterX = centerX;
    }

    public float getCenterY() {
        return mCenterY;
    }

    public void setCenterY(final float centerY) {
        mCenterY = centerY;
    }

    public float getRadius() {
        return mRadius;
    }

    public void setRadius(final float radius) {
        mRadius = radius;
    }

    public float getRefractiveIndex() {
        return mRefractiveIndex;
    }

    public void setRefractiveIndex(final float refractiveIndex) {
        mRefractiveIndex = refractiveIndex;
    }


    @Override
    public void onDraw() {
        glUniform2f(getHandle("center"), mCenterX, mCenterY);
        glUniform1f(getHandle("radius"), mRadius);
        glUniform1f(getHandle("aspectRatio"), mAspectRatio);
        glUniform1f(getHandle("refractiveIndex"), mRefractiveIndex);
    }

}




Java Source Code List

com.af.experiments.FxCameraApp.FilterAdapter.java
com.af.experiments.FxCameraApp.MyActivity.java
com.af.experiments.FxCameraApp.Utils.BitmapFactoryUtils.java
com.af.experiments.FxCameraApp.Utils.ExifUtils.java
com.af.experiments.FxCameraApp.Utils.Fps.java
com.af.experiments.FxCameraApp.Utils.LogWriter.java
com.af.experiments.FxCameraApp.Utils.OpenGlUtils.java
com.af.experiments.FxCameraApp.View.CameraView.java
com.af.experiments.FxCameraApp.View.DefaultPreview.java
com.af.experiments.FxCameraApp.View.GlPreview.java
com.af.experiments.FxCameraApp.View.PreviewTexture.java
com.af.experiments.FxCameraApp.camera.CameraHelperBase.java
com.af.experiments.FxCameraApp.camera.CameraHelperCupcake.java
com.af.experiments.FxCameraApp.camera.CameraHelperDonut.java
com.af.experiments.FxCameraApp.camera.CameraHelperEclair.java
com.af.experiments.FxCameraApp.camera.CameraHelperFactory.java
com.af.experiments.FxCameraApp.camera.CameraHelperFroyo.java
com.af.experiments.FxCameraApp.camera.CameraHelperGingerbread.java
com.af.experiments.FxCameraApp.camera.CameraHelperHonycomb.java
com.af.experiments.FxCameraApp.camera.CameraHelperICS.java
com.af.experiments.FxCameraApp.camera.CameraHelper.java
com.af.experiments.FxCameraApp.display.DisplayHelperBase.java
com.af.experiments.FxCameraApp.display.DisplayHelperFactory.java
com.af.experiments.FxCameraApp.display.DisplayHelperFroyo.java
com.af.experiments.FxCameraApp.display.DisplayHelperHoneycombMR2.java
com.af.experiments.FxCameraApp.display.DisplayHelperHoneycomb.java
com.af.experiments.FxCameraApp.display.DisplayHelper.java
com.af.experiments.FxCameraApp.ogles.DefaultConfigChooser.java
com.af.experiments.FxCameraApp.ogles.DefaultContextFactory.java
com.af.experiments.FxCameraApp.ogles.DefaultWindowSurfaceFactory.java
com.af.experiments.FxCameraApp.ogles.EGLLogWrapper.java
com.af.experiments.FxCameraApp.ogles.GLES20ConfigChooser.java
com.af.experiments.FxCameraApp.ogles.GLES20ContextFactory.java
com.af.experiments.FxCameraApp.ogles.GlImageBitmapTexture.java
com.af.experiments.FxCameraApp.ogles.GlImageResourceTexture.java
com.af.experiments.FxCameraApp.ogles.GlImageTexture.java
com.af.experiments.FxCameraApp.ogles.GlPreviewTextureFactory.java
com.af.experiments.FxCameraApp.ogles.GlSurfaceTexture.java
com.af.experiments.FxCameraApp.ogles.GlTextureView.java
com.af.experiments.FxCameraApp.ogles.PixelBuffer.java
com.af.experiments.FxCameraApp.ogles.PreviewSurfaceHelperBase.java
com.af.experiments.FxCameraApp.ogles.PreviewSurfaceHelperFactory.java
com.af.experiments.FxCameraApp.ogles.PreviewSurfaceHelper.java
com.af.experiments.FxCameraApp.ogles.Texture.java
com.af.experiments.FxCameraApp.renderer.GLES20FramebufferObject.java
com.af.experiments.FxCameraApp.renderer.GlFrameBufferObjectRenderer.java
com.af.experiments.FxCameraApp.renderer.MainRenderer.java
com.af.experiments.FxCameraApp.shaders.GlBilateralShader.java
com.af.experiments.FxCameraApp.shaders.GlBoxBlurShader.java
com.af.experiments.FxCameraApp.shaders.GlBulgeDistortionShader.java
com.af.experiments.FxCameraApp.shaders.GlCGAColorspaceShader.java
com.af.experiments.FxCameraApp.shaders.GlColorInvertShader.java
com.af.experiments.FxCameraApp.shaders.GlColorMatrixShader.java
com.af.experiments.FxCameraApp.shaders.GlColorPackingShader.java
com.af.experiments.FxCameraApp.shaders.GlConvolutionShader.java
com.af.experiments.FxCameraApp.shaders.GlEmbossShader.java
com.af.experiments.FxCameraApp.shaders.GlFalseColorShader.java
com.af.experiments.FxCameraApp.shaders.GlFastBlurShader.java
com.af.experiments.FxCameraApp.shaders.GlGaussianBlurShader.java
com.af.experiments.FxCameraApp.shaders.GlGlassSphereShader.java
com.af.experiments.FxCameraApp.shaders.GlGrayScaleShader.java
com.af.experiments.FxCameraApp.shaders.GlHarrisCornerDetectionShader.java
com.af.experiments.FxCameraApp.shaders.GlHazeShader.java
com.af.experiments.FxCameraApp.shaders.GlMonochromeShader.java
com.af.experiments.FxCameraApp.shaders.GlNobleCornerDetectionShader.java
com.af.experiments.FxCameraApp.shaders.GlPerlinNoiseShader.java
com.af.experiments.FxCameraApp.shaders.GlPinchDistortionShader.java
com.af.experiments.FxCameraApp.shaders.GlPixellateShader.java
com.af.experiments.FxCameraApp.shaders.GlPolarPixellateShader.java
com.af.experiments.FxCameraApp.shaders.GlPolkaDotShader.java
com.af.experiments.FxCameraApp.shaders.GlPosterizeShader.java
com.af.experiments.FxCameraApp.shaders.GlPreviewShader.java
com.af.experiments.FxCameraApp.shaders.GlSepiaShader.java
com.af.experiments.FxCameraApp.shaders.GlShaderGroup.java
com.af.experiments.FxCameraApp.shaders.GlShader.java
com.af.experiments.FxCameraApp.shaders.GlSharpenShader.java
com.af.experiments.FxCameraApp.shaders.GlShiTomasiFeatureDetectionShader.java
com.af.experiments.FxCameraApp.shaders.GlSobelEdgeDetectionShader.java
com.af.experiments.FxCameraApp.shaders.GlSphereRefractionShader.java
com.af.experiments.FxCameraApp.shaders.GlStretchDistortionShader.java
com.af.experiments.FxCameraApp.shaders.GlThreex3ConvolutionShader.java
com.af.experiments.FxCameraApp.shaders.GlThreex3TextureSamplingShader.java
com.af.experiments.FxCameraApp.shaders.GlToneShader.java
com.af.experiments.FxCameraApp.shaders.GlTwoInputShader.java
com.af.experiments.FxCameraApp.shaders.GlVignetteShader.java
com.af.experiments.FxCameraApp.shaders.GlWeakPixelInclusionShader.java
com.af.experiments.FxCameraApp.shaders.GlWhiteBalanceShader.java
com.af.experiments.FxCameraApp.shaders.GlXRayShader.java
com.af.experiments.FxCameraApp.shaders.GlXYDerivativeShader.java
com.af.experiments.FxCameraApp.shaders.fx.GlLutShader.java