`

Android 3D与JNI结合的小例子【转】

阅读更多

91385_1300104753Q4k3.gifQQ截图未命名2.pngQQ截图未命名.pngQQ截图未命名3.png 

  1.         #include <jni.h>
  2.         #include <android/log.h>

  3.         #include <GLES2/gl2.h>
  4.         #include <GLES2/gl2ext.h>
  5.         #include <GLES/gl.h>

  6.         #include <stdio.h>
  7.         #include <stdlib.h>
  8.         #include <math.h>


  9.         const GLfloat gTriangleVertices[] = {
  10.                 -0.5f, -0.5f, 0.5f,
  11.                 0.5f, -0.5f,0.5f,
  12.                 0.0f,-0.5f, 1.0f,
  13.                 0.0f, 0.0f, 0.5f,
  14.         };

  15.         const GLfloat _colorArray[] = {
  16.                         1.0f,0.0f,0.0f,1.0f,
  17.                         0.0f,1.0f,0.0f,1.0f,
  18.                         0.0f,0.0f,1.0f,1.0f,
  19.                         1.0f,1.0f,1.0f,1.0f,
  20.           };

  21.         const GLbyte _indexArray[] = {
  22.                                         0,1,3,
  23.                                         0,2,1,
  24.                                         0,3,2,
  25.                                         1,2,3,
  26.         };

  27.          GLfloat _xAngle = 0.0f;
  28.          GLfloat _yAngle = 0.0f;

  29.         extern "C" {
  30.                 JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceCreated(JNIEnv * env, jobject obj);
  31.                 JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceChanged(JNIEnv * env, jobject obj,  jint width, jint height);
  32.                 JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeDrawFrame(JNIEnv * env, jobject obj);
  33.                 JNIEXPORT void JNICALL Java_com_geolo_android_MyGLSurfaceView_setXYangle(JNIEnv * env, jobject obj,jfloat x , jfloat y);
  34.         };

  35.         JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceCreated(JNIEnv * env, jobject obj){
  36.                 
  37.         }

  38.         JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeSurfaceChanged(JNIEnv * env, jobject obj,jint w,jint h){

  39.         }

  40.         JNIEXPORT void JNICALL Java_com_geolo_android_MyRenderer_nativeDrawFrame(JNIEnv * env, jobject obj){
  41.                 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  42.                 //glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  43.            // glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
  44.            // glColorAttribPointer(4 , GL_FLOAT , 0, _colorArray );
  45.            // glDrawArrays(GL_TRIANGLES, 0, 3);

  46.            glClear(GL_COLOR_BUFFER_BIT);
  47.        //glLoadIdentity();

  48.            glRotatef(_xAngle, 0.5f, 0.0f, 0.0f);
  49.            glRotatef(_yAngle, 0.0f, 0.5f, 0.0f);

  50.            //顶点
  51.            glVertexPointer(3, GL_FLOAT, 0, gTriangleVertices);

  52.            //顶点颜色
  53.            glColorPointer(4, GL_FLOAT, 0, _colorArray);


  54.            glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, _indexArray);
  55.         }

  56.         JNIEXPORT void JNICALL Java_com_geolo_android_MyGLSurfaceView_setXYangle(JNIEnv * env, jobject obj,jfloat x , jfloat y){
  57.            _xAngle = x;
  58.        _yAngle = y;
  59.         }
复制代码



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


view plaincopy to clipboardprint?


  • # 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)  


其中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是个函数库,我没有这方面的资料文档,如果那个哥们有这些文档,请发给我,geolo@vip.qq.com。谢谢。。。




 geolo.OpenGL.JNI.rar (73.9 KB, 下载次数: 23) 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics