使用来自图片库的图像的3D对象上设置的纹理(OpenGL ES 2.0的) [英] Setting texture on 3D object using image from photo gallery (OpenGL ES 2.0)

查看:274
本文介绍了使用来自图片库的图像的3D对象上设置的纹理(OpenGL ES 2.0的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android开发的新尝试使用OpenGL ES 2.0的显示3D对象,并对其贴图。当我用从资源获得的图像纹理,一切运行良好。至于下一步,我试图用图片从图片库动态改变的质感。下面是我所做的:

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

我把mGLView.getRenderer()。applyTexture()内GLSurfaceView.queueEvent在OpenGL渲染线程,在实际的纹理映射使用OpenGL的API进行运行。但是,当我跑了code,我有以下LogCat中的错误信息:

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)

和一个警告消息:

EGL_emulation eglSurfaceAttrib not implemented

虽然没有崩溃的应用程序,我没有得到纹理映射与所选图像预期的结果。我是pretty确保OpenGL的纹理映射code是没有问题的,因为它与资源工作形象。

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.

我怀疑,这个没有当前上下文的错误是因为我试图调用OpenGL的API时的GLSurfaceView暂停(因此上下文被破坏),由于照片库的加载。所以我把集preserveEGLContextOnPause(真); 之前,我创建渲染器,而没有解决的问题。任何帮助将pciated,使这项工作AP $ P $。

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.

推荐答案

在Android上,以OpenGL ES的所有操作必须从一个单独的线程进行。你可能没有意识到,GLSurfaceView自动提供该线程,所以任何的OpenGL ES从任何其他线程做会导致这些问题。本文讨论得更详细:

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:

<一个href=\"http://software.intel.com/en-us/articles/porting-opengl-games-to-android-on-intel-atom-processors-part-1\" rel=\"nofollow\">http://software.intel.com/en-us/articles/porting-opengl-games-to-android-on-intel-atom-processors-part-1

这篇关于使用来自图片库的图像的3D对象上设置的纹理(OpenGL ES 2.0的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆