问题
I am new in Android development and trying to use OpenGL ES 2.0 to display a 3D object and map texture on it. Everything worked fine when I used texture obtained from a resource image. As next step, I was trying to use an image from Photo gallery to change texture dynamically. Here is what I did:
public class DesignTab extends Fragment implements OnMenuItemClickListener {
private static final int SELECT_PHOTO = 100;
private GLSurfaceView mGLView;
// onCreate, onCreateView here where mGLView is created
@Override
public void onPause() {
super.onPause();
mGLView.onPause();
}
@Override
public void onResume() {
super.onResume();
mGLView.onResume();
}
// popup menu event handler here that calls onPhotoGalleryAction()
public void onPhotoGalleryAction() {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = getActivity().getContentResolver().openInputStream(selectedImage);
Bitmap selectedImageBitmap = BitmapFactory.decodeStream(imageStream);
mGLView.getRenderer().setTexture(selectedImageBitmap); // this does NOT call OpenGL API but store bitmap object
mGLView.queueEvent(new Runnable() {
@Override
public void run() {
mGLView.getRenderer().applyTexture(); // this calls OpenGL APIs to apply texture from stored bitmap
});
}
}
I placed mGLView.getRenderer().applyTexture() inside GLSurfaceView.queueEvent to run it in OpenGL rendering thread, where actual texture mapping is done using OpenGL APIs. But when I ran the code, I've got the following LogCat error message:
call to OpenGL ES API with no current context (logged once per thread)
and a warning message:
EGL_emulation eglSurfaceAttrib not implemented
Although it did not crash the app, I did not get the expected results of texture mapping with the selected image. I'm pretty sure that the OpenGL texture mapping code isn't a problem since it worked with a resource image.
I suspected that this "no current context" error is because I was trying to call OpenGL APIs when the GLSurfaceView is paused (and hence the context is destroyed) due to the loading of Photo Gallery. So I put setPreserveEGLContextOnPause(true); before I create the renderer, which did not solve the problem. Any help will be appreciated to make this work.
回答1:
All calls to OpenGL ES on Android must be made from a single thread. You may not realize that GLSurfaceView provides that thread automatically, so any OpenGL ES call made from any other thread will cause these problems. This article discusses this in more detail:
http://software.intel.com/en-us/articles/porting-opengl-games-to-android-on-intel-atom-processors-part-1
来源:https://stackoverflow.com/questions/18584232/setting-texture-on-3d-object-using-image-from-photo-gallery-opengl-es-2-0