如何从入射矢量和表面法线中找到折射矢量

时间:2015-04-20 21:15:06

标签: java vector raytracing

我正在用java写一个光线跟踪器,我正在尝试实现折射,但是我对这个主题的信息感到困惑。如果我有一个入射光线的3D矢量,作为3D矢量给出的曲面法线和两个介质的折射率,我需要应用哪些操作才能获得透射光线的矢量?

3 个答案:

答案 0 :(得分:1)

V_incedence 成为规范化的传入向量。设n1n2为两个曲面的折射指数。您想要计算 V_refraction 。设 n 为规范化的法向量。

V_refraction = r*V_incedence + (rc - sqrt(1-r^2(1-c^2)))n
where r = n1/n2 and c = -n dot V_incedence.

答案 1 :(得分:0)

我在我的ray java tracer中实现了这一点,看看https://github.com/bradforj287/brads-java-raytracer

private static double clamp(final double val, final double min, final double max) {
    return Math.max(min, Math.min(max, val));
}

private Vector3d getRefractionVector(final Vector3d I, final Vector3d N, final double ior) {
    double cosi = clamp(-1, 1, I.dot(N));
    double etai = 1, etat = ior;
    Vector3d n = N;
    if (cosi < 0) {
        cosi = -cosi;
    } else {
        double temp = etai;
        etai = etat;
        etat = temp;
        n = N.multiply(-1);
    }
    double eta = etai / etat;
    double k = 1 - (eta * eta) * (1 - (cosi * cosi));
    if (k <= 0) {
        return Vector3d.ZERO;
    } else {
        return I.multiply(eta).add(n.multiply(((eta * cosi) - Math.sqrt(k))));
    }
}

答案 2 :(得分:0)

Bram de Greve写了一篇很好的文章,介绍了射线追踪中的反射和折射。您可以找到它here

他的实现(C ++)如下:

    apply plugin: 'com.android.application'

    apply plugin: 'com.google.gms.google-services'

    android {

     compileSdkVersion 29

     buildToolsVersion "29.0.2"

        //useLibrary 'org.apache.http.legacy'

        defaultConfig {

            applicationId "uhnm.stroke.strokeapp_secondscreen"

            minSdkVersion 16

            targetSdkVersion 29

            versionCode 1

            versionName "1.0"

            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

            multiDexEnabled true

        }

        buildTypes {

             release {

                    minifyEnabled false

                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

            }

        }

        packagingOptions

            {

                exclude 'META-INF/DEPENDENCIES'

            }

}

dependencies {

        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:multidex:1.0.3'
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'com.google.firebase:firebase-database:19.2.0'
        implementation 'com.google.firebase:firebase-storage:19.1.0'
        implementation 'com.google.firebase:firebase-auth:19.1.0'

        implementation 'com.google.firebase:firebase-core:17.2.1'
        implementation 'com.google.firebase:firebase-messaging:20.0.0'

        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
        implementation 'com.android.support:support-annotations:28.0.0'
        implementation 'com.google.android.material:material:1.1.0-alpha10'
        implementation group: "com.twilio.sdk", name: "twilio", version: "7.44.0"
        implementation 'com.squareup.okhttp3:okhttp:4.2.1'
}