0

I am trying to render an openGL es surface from the NDK, but got halted early in my work. I have a setup similar to the 3d example in the NDK. I have a class inheriting from GLSurface view and one inheriting from GLSurfaceView.Renderer. In my .c file, I have a simple method that does NOTHING. It is just a void function with nothing in it. I can call this function in my class that inherit from activity onCreate method.
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();
}

The program works fine. However, if i put the call (and declaration) in one of the GLSurfaceView classes, the program immediately fails (nativeSetup is the call in question). I have verified that everything is working fine without the native call (a colored surface is drawn). Does anyone have any ideas about why I cannot call native code from GLSurface classes?

My c file:

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

void Java_com_test_intro_nativeSetup( JNIEnv*  env ){}

My java file in a non working manner:

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);
    }
}
4

1 回答 1

2

In your c file did you rename your native function?Maybe the problem is related to this issues, cause JNI uses a particular naming for the navtive functions.

Have a look here and try to use javah -jni $CLASS-FILE-WITH-NATIVE-METHODS$ to obtain the c files.

hope this helps.

ciao

于 2011-02-09T11:46:48.150 回答