Android 3D与JNI结合的小例子

 

其他代码都不重要,重要的就是C代码

NativeRenderer.cpp

<textarea cols="50" rows="15" name="code" class="cpp"> #include &lt;jni.h&gt; #include &lt;android/log.h&gt; #include &lt;GLES2/gl2.h&gt; #include &lt;GLES2/gl2ext.h&gt; #include &lt;GLES/gl.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; const GLfloat gTriangleVertices[] = { -0.5f, -0.5f, 0.5f, 0.5f, -0.5f,0.5f, 0.0f,-0.5f, 1.0f, 0.0f, 0.0f, 0.5f, }; const GLfloat _colorArray[] = { 1.0f,0.0f,0.0f,1.0f, 0.0f,1.0f,0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f, 1.0f,1.0f,1.0f,1.0f, }; const GLbyte _indexArray[] = { 0,1,3, 0,2,1, 0,3,2, 1,2,3, }; GLfloat _xAngle = 0.0f; GLfloat _yAngle = 0.0f; extern "C" { JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceCreated(JNIEnv * env, jobject obj); JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceChanged(JNIEnv * env, jobject obj, jint width, jint height); JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeDrawFrame(JNIEnv * env, jobject obj); JNIEXPORT void JNICALL Java_com_geolo_android_MyGLSurfaceView_setXYangle(JNIEnv * env, jobject obj,jfloat x , jfloat y); }; JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceCreated(JNIEnv * env, jobject obj){ } JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceChanged(JNIEnv * env, jobject obj,jint w,jint h){ } JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeDrawFrame(JNIEnv * env, jobject obj){ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices); // glColorAttribPointer(4 , GL_FLOAT , 0, _colorArray ); // glDrawArrays(GL_TRIANGLES, 0, 3); glClear(GL_COLOR_BUFFER_BIT); //glLoadIdentity(); glRotatef(_xAngle, 0.5f, 0.0f, 0.0f); glRotatef(_yAngle, 0.0f, 0.5f, 0.0f); //顶点 glVertexPointer(3, GL_FLOAT, 0, gTriangleVertices); //顶点颜色 glColorPointer(4, GL_FLOAT, 0, _colorArray); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, _indexArray); } JNIEXPORT void JNICALL Java_com_geolo_android_MyGLSurfaceView_setXYangle(JNIEnv * env, jobject obj,jfloat x , jfloat y){ _xAngle = x; _yAngle = y; } </textarea> 

 

第二重要的部分是:Android.mk

<textarea cols="50" rows="15" name="code" class="c-sharp"># Copyright (C) 2009 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := Geolo3D LOCAL_CFLAGS := -Werror LOCAL_SRC_FILES := NativeRenderer.cpp LOCAL_LDLIBS := -llog -lGLESv2 -lGLESv1_CM -ldl include $(BUILD_SHARED_LIBRARY)</textarea> 

 

其中Android.mk重要的部分是:LOCAL_LDLIBS    := -llog -lGLESv2 -lGLESv1_CM -ldl

比如,如果没有-ldl 是不能在C/C++中调用   glVertexPointer(3, GL_FLOAT, 0, gTriangleVertices);和

 glColorPointer(4, GL_FLOAT, 0, _colorArray);

 

-ldl是个函数库,我没有这方面的资料文档,如果那个哥们有这些文档,请发给我,[email protected]。谢谢。。。

 

 

Android 3D与JNI结合的小例子_第1张图片

 

 

你可能感兴趣的:(java,android,jni,buffer,float,permissions)