NDK调用GLSurfaceView类

时间:2011-02-09 05:47:12

标签: android opengl-es android-ndk glsurfaceview

我正在尝试从NDK渲染一个openGL表面,但在我的工作早期就停止了。我有一个类似于NDK中的3d示例的设置。我有一个继承自GLSurface视图的类和一个继承自GLSurfaceView.Renderer的类。在我的.c文件中,我有一个简单的方法,没有任何东西。它只是一个无效的函数。我可以在我的类中调用这个函数,该函数继承自activity onCreate方法 private static native void nativeSetup();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    mGLView = new GraphGLSurfaceView(this);
    setContentView(mGLView);
    nativeSetup();
}

程序运行正常。但是,如果我将调用(和声明)放在其中一个GLSurfaceView类中,程序会立即失败(nativeSetup是有问题的调用)。我已经验证一切正常,没有原生调用(绘制了彩色表面)。有没有人对我为什么不能从GLSurface类调用本机代码有任何想法?

我的c档案:

#include <string.h>
#include <jni.h>

void Java_com_test_intro_nativeSetup( JNIEnv*  env ){}

我的java文件以非工作方式:

package com.test;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

public class intro extends Activity {
    static{
        System.loadLibrary("graphrender");
    }
    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mGLView = new GraphGLSurfaceView(this);
        setContentView(mGLView);

    }
}

class GraphGLSurfaceView extends GLSurfaceView {
    GraphRenderer mRenderer;    
    public GraphGLSurfaceView(Context context) {
        super(context);
        mRenderer = new GraphRenderer();
        setRenderer(mRenderer);

    }
}

class GraphRenderer implements GLSurfaceView.Renderer { 
    private static native void nativeSetup();
    private float _red = 0.9f;
    private float _green = 0.2f;
    private float _blue = 0.2f;

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        Log.d("intro", "Got to intro 4" );
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        gl.glViewport(0, 0, w, h);
        nativeSetup();
        //Log.d("intro", "Got to intro 2" + debugStr);
    }

    public void onDrawFrame(GL10 gl) {
        Log.d("intro", "Got to intro 3");
        gl.glClearColor(_red, _green, _blue, 1.0f);
        // clear the color buffer to show the ClearColor we called above...
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    }
}

1 个答案:

答案 0 :(得分:2)

在你的c文件中你是否重命名了你的原生函数?也许问题与这个问题有关,因为JNI使用特定的导航功能命名。

查看here并尝试使用javah -jni $CLASS-FILE-WITH-NATIVE-METHODS$获取c文件。

希望这会有所帮助。