1

我想构建一个增强现实 (AR) 应用程序,如本视频所示:宜家的新增强现实应用程序将改变您的装饰方式

我在 glsurfaceview 和 cameraview 上加载了一个 3D .stl 对象,我可以用手指触摸来旋转对象。

但是,问题是我的对象在cameraview上方,我希望它只显示在cameraview中的特定位置,如上面的视频。

这是我的代码如何显示我如何加载对象,我在 glsurfaceview 上加载 .stl 3D 对象:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cameraSurfaceView = findViewById(R.id.cameraView);
    modelName = getIntent().getStringExtra("name");

    if (ActivityCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(LoadModel.this,
                new String[]{Manifest.permission.CAMERA},
                requestPermissionID);
        return;
    }
    else {
        setFileName(modelName);
        modelView = findViewById(R.id.glSurfaceView);
        LinearLayout ll = new LinearLayout(this);
        Button editBox = new Button(this);
        editBox.setText("Stop Object");

        ll.addView(editBox);
        ll.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);

        modelView.setRenderer(new ModelRenderer(this));
        //            setContentView(modelView);

        addContentView(ll, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        editBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoadModel.this, "clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

private void setFileName(String modelName) {
    if (modelName.equals("Select Book")) {
        SAMPLE_MODELS = new String[]{"book.stl"};
        loadSampleModel();
    }
    if (modelName.equals("Select Lady")) {
        SAMPLE_MODELS = new String[]{"lucy.stl"};
        loadSampleModel();
    }
    if (modelName.equals("Select Girl")) {
        SAMPLE_MODELS = new String[]{"womandress.stl"};
        loadSampleModel();
    }
}

@Override
protected void onStart() {
    super.onStart();

    //createNewModelView(app.getCurrentModel());
    //if (app.getCurrentModel() != null) {
    //    setTitle(app.getCurrentModel().getTitle());
    //}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    return super.onCreateOptionsMenu(menu);
}


private void createNewModelView(@Nullable Model model) {
    if (modelView != null) {
        containerView.removeView(modelView);
    }

    ModelViewerApplication.getInstance().setCurrentModel(model);
    modelView = new ModelSurfaceView(this, model);

    // modelView.backSided(true);
    //abc.doubleSided(true)

    containerView.addView(modelView);

    mView = new SurfaceView(this);
    //    mView = new SurfaceView(this);
    mHolder = mView.getHolder();
    mHolder.addCallback(this);
    containerView.addView(mView);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode != requestPermissionID) {
        Log.d(TAG, "Got unexpected permission result: " + requestCode);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        return;
    }

    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        try {
            //loadSampleModel();

            setFileName(modelName);

            //mCameraSource.start(mCameraView.getHolder());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Nullable
private String getFileName(@NonNull ContentResolver cr, @NonNull Uri uri) {
    if ("content".equals(uri.getScheme())) {

        String[] projection = {MediaStore.MediaColumns.DISPLAY_NAME};

        Cursor metaCursor = ContentResolverCompat.query(cr, uri, projection, null, null, null, null);

        if (metaCursor != null) {

            try {

                if (metaCursor.moveToFirst()) {

                    return metaCursor.getString(0);
                }
            }
            finally {
                metaCursor.close();
            }
        }
    }
    return uri.getLastPathSegment();
}


private void setCurrentModel(@NonNull Model model) {
    createNewModelView(model);
    //   Toast.makeText(getApplicationContext(), R.string.open_model_successs, Toast.LENGTH_SHORT).show();
    setTitle(model.getTitle());
    //        progressBar.setVisibility(View.GONE);
}


private void loadSampleModel() {
    try {
        app = ModelViewerApplication.getInstance();
        containerView = (ViewGroup) findViewById(R.id.container_view);
        InputStream stream = getApplicationContext().getAssets()
                .open(SAMPLE_MODELS[sampleModelIndex++ % SAMPLE_MODELS.length]);
        setCurrentModel(new StlModel(stream));
        stream.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

在 glsurfaceview 的 drawframe 方法上渲染对象:

 @Override
    public void onDrawFrame(GL10 gl) {
        Log.d("drawFrame", "onDrawFrame: ");
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

        //floor.draw(viewMatrix, projectionMatrix, light);
        if (model != null) {
            if (mAzimuthReal >= 240 && mAzimuthReal <= 260) {
                model.draw(viewMatrix, projectionMatrix, light);
            }
        }
    }

我怎样才能像该视频中那样修复和旋转?

4

0 回答 0