Home | History | Annotate | Download | only in bullet-native
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 #include "jmeClasses.h"
     33 #include <stdio.h>
     34 
     35 /**
     36  * Author: Normen Hansen,Empire Phoenix, Lutherion
     37  */
     38 //public fields
     39 jclass jmeClasses::PhysicsSpace;
     40 jmethodID jmeClasses::PhysicsSpace_preTick;
     41 jmethodID jmeClasses::PhysicsSpace_postTick;
     42 jmethodID jmeClasses::PhysicsSpace_addCollisionEvent;
     43 
     44 jclass jmeClasses::PhysicsGhostObject;
     45 jmethodID jmeClasses::PhysicsGhostObject_addOverlappingObject;
     46 
     47 jclass jmeClasses::Vector3f;
     48 jmethodID jmeClasses::Vector3f_set;
     49 jmethodID jmeClasses::Vector3f_toArray;
     50 jmethodID jmeClasses::Vector3f_getX;
     51 jmethodID jmeClasses::Vector3f_getY;
     52 jmethodID jmeClasses::Vector3f_getZ;
     53 jfieldID jmeClasses::Vector3f_x;
     54 jfieldID jmeClasses::Vector3f_y;
     55 jfieldID jmeClasses::Vector3f_z;
     56 
     57 jclass jmeClasses::Quaternion;
     58 jmethodID jmeClasses::Quaternion_set;
     59 jmethodID jmeClasses::Quaternion_getX;
     60 jmethodID jmeClasses::Quaternion_getY;
     61 jmethodID jmeClasses::Quaternion_getZ;
     62 jmethodID jmeClasses::Quaternion_getW;
     63 jfieldID jmeClasses::Quaternion_x;
     64 jfieldID jmeClasses::Quaternion_y;
     65 jfieldID jmeClasses::Quaternion_z;
     66 jfieldID jmeClasses::Quaternion_w;
     67 
     68 jclass jmeClasses::Matrix3f;
     69 jmethodID jmeClasses::Matrix3f_set;
     70 jmethodID jmeClasses::Matrix3f_get;
     71 jfieldID jmeClasses::Matrix3f_m00;
     72 jfieldID jmeClasses::Matrix3f_m01;
     73 jfieldID jmeClasses::Matrix3f_m02;
     74 jfieldID jmeClasses::Matrix3f_m10;
     75 jfieldID jmeClasses::Matrix3f_m11;
     76 jfieldID jmeClasses::Matrix3f_m12;
     77 jfieldID jmeClasses::Matrix3f_m20;
     78 jfieldID jmeClasses::Matrix3f_m21;
     79 jfieldID jmeClasses::Matrix3f_m22;
     80 
     81 jclass jmeClasses::DebugMeshCallback;
     82 jmethodID jmeClasses::DebugMeshCallback_addVector;
     83 
     84 jclass jmeClasses::PhysicsRay_Class;
     85 jmethodID jmeClasses::PhysicsRay_newSingleResult;
     86 
     87 jfieldID jmeClasses::PhysicsRay_normalInWorldSpace;
     88 jfieldID jmeClasses::PhysicsRay_hitfraction;
     89 jfieldID jmeClasses::PhysicsRay_collisionObject;
     90 
     91 jclass jmeClasses::PhysicsRay_listresult;
     92 jmethodID jmeClasses::PhysicsRay_addmethod;
     93 
     94 //private fields
     95 //JNIEnv* jmeClasses::env;
     96 JavaVM* jmeClasses::vm;
     97 
     98 void jmeClasses::initJavaClasses(JNIEnv* env) {
     99 //    if (env != NULL) {
    100 //        fprintf(stdout, "Check Java VM state\n");
    101 //        fflush(stdout);
    102 //        int res = vm->AttachCurrentThread((void**) &jmeClasses::env, NULL);
    103 //        if (res < 0) {
    104 //            fprintf(stdout, "** ERROR: getting Java env!\n");
    105 //            if (res == JNI_EVERSION) fprintf(stdout, "GetEnv Error because of different JNI Version!\n");
    106 //            fflush(stdout);
    107 //        }
    108 //        return;
    109 //    }
    110     if(PhysicsSpace!=NULL) return;
    111     fprintf(stdout, "Bullet-Native: Initializing java classes\n");
    112     fflush(stdout);
    113 //    jmeClasses::env = env;
    114     env->GetJavaVM(&vm);
    115 
    116     PhysicsSpace = (jclass)env->NewGlobalRef(env->FindClass("com/jme3/bullet/PhysicsSpace"));
    117     if (env->ExceptionCheck()) {
    118         env->Throw(env->ExceptionOccurred());
    119         return;
    120     }
    121 
    122     PhysicsSpace_preTick = env->GetMethodID(PhysicsSpace, "preTick_native", "(F)V");
    123     PhysicsSpace_postTick = env->GetMethodID(PhysicsSpace, "postTick_native", "(F)V");
    124     PhysicsSpace_addCollisionEvent = env->GetMethodID(PhysicsSpace, "addCollisionEvent_native","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;Lcom/jme3/bullet/collision/PhysicsCollisionObject;J)V");
    125     if (env->ExceptionCheck()) {
    126         env->Throw(env->ExceptionOccurred());
    127         return;
    128     }
    129 
    130     PhysicsGhostObject = (jclass)env->NewGlobalRef(env->FindClass("com/jme3/bullet/objects/PhysicsGhostObject"));
    131     if (env->ExceptionCheck()) {
    132         env->Throw(env->ExceptionOccurred());
    133         return;
    134     }
    135     PhysicsGhostObject_addOverlappingObject = env->GetMethodID(PhysicsGhostObject, "addOverlappingObject_native","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;)V");
    136     if (env->ExceptionCheck()) {
    137         env->Throw(env->ExceptionOccurred());
    138         return;
    139     }
    140 
    141     Vector3f = (jclass)env->NewGlobalRef(env->FindClass("com/jme3/math/Vector3f"));
    142     Vector3f_set = env->GetMethodID(Vector3f, "set", "(FFF)Lcom/jme3/math/Vector3f;");
    143     Vector3f_toArray = env->GetMethodID(Vector3f, "toArray", "([F)[F");
    144     Vector3f_getX = env->GetMethodID(Vector3f, "getX", "()F");
    145     Vector3f_getY = env->GetMethodID(Vector3f, "getY", "()F");
    146     Vector3f_getZ = env->GetMethodID(Vector3f, "getZ", "()F");
    147     Vector3f_x = env->GetFieldID(Vector3f, "x", "F");
    148     Vector3f_y = env->GetFieldID(Vector3f, "y", "F");
    149     Vector3f_z = env->GetFieldID(Vector3f, "z", "F");
    150 
    151     Quaternion = (jclass)env->NewGlobalRef(env->FindClass("com/jme3/math/Quaternion"));
    152     if (env->ExceptionCheck()) {
    153         env->Throw(env->ExceptionOccurred());
    154         return;
    155     }
    156     Quaternion_set = env->GetMethodID(Quaternion, "set", "(FFFF)Lcom/jme3/math/Quaternion;");
    157     Quaternion_getW = env->GetMethodID(Quaternion, "getW", "()F");
    158     Quaternion_getX = env->GetMethodID(Quaternion, "getX", "()F");
    159     Quaternion_getY = env->GetMethodID(Quaternion, "getY", "()F");
    160     Quaternion_getZ = env->GetMethodID(Quaternion, "getZ", "()F");
    161     Quaternion_x = env->GetFieldID(Quaternion, "x", "F");
    162     Quaternion_y = env->GetFieldID(Quaternion, "y", "F");
    163     Quaternion_z = env->GetFieldID(Quaternion, "z", "F");
    164     Quaternion_w = env->GetFieldID(Quaternion, "w", "F");
    165 
    166     Matrix3f = (jclass)env->NewGlobalRef(env->FindClass("com/jme3/math/Matrix3f"));
    167     if (env->ExceptionCheck()) {
    168         env->Throw(env->ExceptionOccurred());
    169         return;
    170     }
    171     Matrix3f_set = env->GetMethodID(Matrix3f, "set", "(IIF)Lcom/jme3/math/Matrix3f;");
    172     Matrix3f_get = env->GetMethodID(Matrix3f, "get", "(II)F");
    173     Matrix3f_m00 = env->GetFieldID(Matrix3f, "m00", "F");
    174     if (env->ExceptionCheck()) {
    175         env->Throw(env->ExceptionOccurred());
    176         return;
    177     }
    178     Matrix3f_m01 = env->GetFieldID(Matrix3f, "m01", "F");
    179     Matrix3f_m02 = env->GetFieldID(Matrix3f, "m02", "F");
    180     Matrix3f_m10 = env->GetFieldID(Matrix3f, "m10", "F");
    181     Matrix3f_m11 = env->GetFieldID(Matrix3f, "m11", "F");
    182     Matrix3f_m12 = env->GetFieldID(Matrix3f, "m12", "F");
    183     Matrix3f_m20 = env->GetFieldID(Matrix3f, "m20", "F");
    184     Matrix3f_m21 = env->GetFieldID(Matrix3f, "m21", "F");
    185     Matrix3f_m22 = env->GetFieldID(Matrix3f, "m22", "F");
    186 
    187     DebugMeshCallback = (jclass)env->NewGlobalRef(env->FindClass("com/jme3/bullet/util/DebugMeshCallback"));
    188     if (env->ExceptionCheck()) {
    189         env->Throw(env->ExceptionOccurred());
    190         return;
    191     }
    192 
    193     DebugMeshCallback_addVector = env->GetMethodID(DebugMeshCallback, "addVector", "(FFFII)V");
    194     if (env->ExceptionCheck()) {
    195         env->Throw(env->ExceptionOccurred());
    196         return;
    197     }
    198 
    199     PhysicsRay_Class = (jclass)env->NewGlobalRef(env->FindClass("com/jme3/bullet/collision/PhysicsRayTestResult"));
    200     if (env->ExceptionCheck()) {
    201         env->Throw(env->ExceptionOccurred());
    202         return;
    203     }
    204 
    205     PhysicsRay_newSingleResult = env->GetMethodID(PhysicsRay_Class,"<init>","()V");
    206     if (env->ExceptionCheck()) {
    207         env->Throw(env->ExceptionOccurred());
    208         return;
    209     }
    210 
    211     PhysicsRay_normalInWorldSpace = env->GetFieldID(PhysicsRay_Class,"hitNormalLocal","Lcom/jme3/math/Vector3f;");
    212         if (env->ExceptionCheck()) {
    213             env->Throw(env->ExceptionOccurred());
    214             return;
    215         }
    216 
    217 
    218     PhysicsRay_hitfraction = env->GetFieldID(PhysicsRay_Class,"hitFraction","F");
    219     if (env->ExceptionCheck()) {
    220         env->Throw(env->ExceptionOccurred());
    221         return;
    222     }
    223 
    224 
    225     PhysicsRay_collisionObject = env->GetFieldID(PhysicsRay_Class,"collisionObject","Lcom/jme3/bullet/collision/PhysicsCollisionObject;");
    226     if (env->ExceptionCheck()) {
    227         env->Throw(env->ExceptionOccurred());
    228         return;
    229     }
    230 
    231     PhysicsRay_listresult = env->FindClass("java/util/List");
    232     PhysicsRay_listresult = (jclass)env->NewGlobalRef(PhysicsRay_listresult);
    233     if (env->ExceptionCheck()) {
    234         env->Throw(env->ExceptionOccurred());
    235         return;
    236     }
    237 
    238     PhysicsRay_addmethod = env->GetMethodID(PhysicsRay_listresult,"add","(Ljava/lang/Object;)Z");
    239     if (env->ExceptionCheck()) {
    240         env->Throw(env->ExceptionOccurred());
    241         return;
    242     }
    243 }
    244 
    245 void jmeClasses::throwNPE(JNIEnv* env) {
    246     if (env == NULL) return;
    247     jclass newExc = env->FindClass("java/lang/NullPointerException");
    248     env->ThrowNew(newExc, "");
    249     return;
    250 }
    251