android java 层参数重载glVertexAttribPointer 在es20 C 接口中的处理方法

1. JNI jstring -> C char*     需要调用
const char * name = (const char *)env->GetStringUTFChars(js, false);



2. JNI jfloatArray -> C float []  需要:对数组进行遍历,在遍历中转换为自己的数组即可即是:
jfloat * tmpColor;
	//得到数组的
	jint size = env->GetArrayLength(c);
	int csize = (int)size;
	float color[(int)size];
        //得到一个指向数组首地址的指针
	tmpColor = env->GetFloatArrayElements(c,false);
	for(int i=0; i< csize; i++)
	{
		color[i] = *tmpColor ++;
	}



/*
*  问题描述:当用java调用es20的库的时候 自己实现native方法调用,其他方法都还好,有一个方法有重载,但是C接口里面只有一个方法glVertexAttribPointer ,jni传递的参*                        数又是buffer 和 int 两种所以就用了另外一个方法(glVertexAttribPointerBounds)来处理一下参数,并间接调用 glVertexAttribPointer 
*
*Java native 方法:
*/
// C function void glVertexAttribPointer ( GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *ptr );
	
    private static native void glVertexAttribPointerBounds(
			int indx, 
			int size, 
			int type, 
			boolean normalized, 
			int stride, 
			java.nio.Buffer ptr, 
			int remaining
			);
	
    public static void neuglVertexAttribPointer(int indx, int size, int type, boolean normalized, int stride, java.nio.Buffer ptr) 
    {
        glVertexAttribPointerBounds(indx, size, type, normalized, stride, ptr, ptr.remaining());
    }
/*
* C 接口中处理方法。 需要实现一个处理 buffer 的方法:getDirectBufferPointer
*
*/
void Java_com_drawtriangle_GLES20m_neuglVertexAttribPointer(JNIEnv * env, jclass jc, int indx, int size, int type, bool normaized, int stride, const void * pointer)
{
    void(*fn)(int indx, int size, int type, bool normaized, int stride, const void * pointer);
    fn = (void (*)(int, int, int, bool, int,const void *))dlsym(functionlib, "glVertexAttribPointer");
    fn( indx,  size,  type,  normaized, stride, pointer);
}
static jclass bufferClass;
static jfieldID positionID;
static jfieldID elementSizeShiftID;

static void * getDirectBufferPointer(JNIEnv *env, jobject buffer) {
	jclass IAEClassLocal = env->FindClass("java/lang/IllegalArgumentException");

	jclass bufferClassLocal = env->FindClass("java/nio/Buffer");
	bufferClass = (jclass) env->NewGlobalRef(bufferClassLocal);
	positionID = env->GetFieldID(bufferClass, "position", "I");
	jclass IAEClass = (jclass) env->NewGlobalRef(IAEClassLocal);

	elementSizeShiftID = env->GetFieldID(bufferClass, "_elementSizeShift", "I");

	char* buf = (char*) env->GetDirectBufferAddress(buffer);
    if (buf) {
        jint position = env->GetIntField(buffer, positionID);
        jint elementSizeShift = env->GetIntField(buffer, elementSizeShiftID);
        buf += position << elementSizeShift;
    } else {
        env->ThrowNew(IAEClass, "Must use a native order direct Buffer");
    }
    return (void *) buf;
}

void Java_com_drawtriangle_GLES20m_glVertexAttribPointerBounds(JNIEnv * env, jclass jc, jint index, jint size, jint type, jboolean normalized, jint stride, jobject pointer, jint remaining)
{

	jarray _array = (jarray) 0;
	jint _remaining;
	void *ptr = (void *) 0;
	if (pointer) {
		ptr = (void *) getDirectBufferPointer(env, pointer);
		if ( ! ptr ) {
			return;
		}
	}
	Java_com_drawtriangle_GLES20m_neuglVertexAttribPointer(env, jc, (int)index, (int)size, (int)type, (bool)normalized, (int)stride, ptr);
}




     
     
     
     


你可能感兴趣的:(android,jni,native)