我在Java层有一个实现了Renderer
接口的类。在这个类中,我有一个位图需要传递到JNI层。JNI层将负责使用OpenGL显示位图。有人能告诉我如何做吗?
更新:
我知道有一个OpenGL方法glTexImage2D
可以用来显示简单的2D图像。有人能告诉我如何使用它吗?在调用此方法之前和之后,我需要进行什么设置?我应该如何将我的位图从Java传递到JNI中?
更新2: 这是我的当前代码:
Java类
public class HelloRenderer implements Renderer
{
Context _context;
Bitmap wood;
public void SetContext(Context context)
{
_context = context;
wood = BitmapFactory.decodeResource( _context.getResources(), R.drawable.hello);
}
@Override
public void onDrawFrame(GL10 gl)
{
RenderGLStuff(wood.getWidth(), wood.getHeight(), wood);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
InitGLStuff(wood.getWidth(), wood.getHeight(), wood);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
}
private static native void InitGLStuff(int width, int height, Bitmap ptr);
private static native void RenderGLStuff(int width, int height, Bitmap ptr);
static
{
System.loadLibrary("myglstuff");
}
}
JNI 方法
JNIEXPORT void JNICALL Java_mine_NativeGL_HelloRenderer_InitGLStuff(JNIEnv * env, jobject obj, int width, int height, void * ptr )
{
glViewport(0, 0, width, height);
glClearColorx((GLfixed)(0.1f * 65536), (GLfixed)(0.2f * 65536), (GLfixed)(0.3f * 65536), 0x10000);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
JNIEXPORT void JNICALL Java_mine_NativeGL_HelloRenderer_RenderGLStuff(JNIEnv * env, jobject obj, int width, int height, void * ptr)
{
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &ptr);
}
更新3 我已经更新了我的本地代码,现在我可以看到一块白色的区域代替图片,但是没有图片内容。有什么想法吗?
JNIEXPORT void JNICALL Java_mine_NativeGL_HelloRenderer_RenderGLStuff(JNIEnv * env, jobject obj, int width, int height, void * ptr)
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
GLfloat vertices[] = { -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, };
GLfloat normals[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 };
GLfloat textureCoords[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &ptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, texture);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-5.0, 5.0, -7.5, 7.5, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}