Skip to content

Sample Code ( Object Rendering )

KimKyuyeon edited this page Mar 14, 2021 · 3 revisions
  1. OnCreate is used to set up objects for rendering with surfaceview.
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = findViewById(R.id.surfaceview);
        displayRotationHelper = new DisplayRotationHelper(/*context=*/ this);
        mRotationDetector = new RotationGestureDetector(this);
        scaleGestureDetector = new MyScaleGestures(this);

        // Set up tap listener.
        tapHelper = new TapHelper(/*context=*/ this);
        surfaceView.setOnTouchListener(tapHelper);

        // Set up renderer.
        surfaceView.setPreserveEGLContextOnPause(true);
        surfaceView.setEGLContextClientVersion(2);
        surfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // Alpha used for plane blending.
        surfaceView.setRenderer(this);
        surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        surfaceView.setWillNotDraw(false);

        installRequested = false;
        calculateUVTransform = true;

        depthSettings.onCreate(this);
        ImageButton settingsButton = findViewById(R.id.settings_button);
        settingsButton.setOnClickListener(this::launchSettingsMenuDialog);
    }

2.The onSurfaceCreated function is a function that is called when a surface is created and is initially set before drawing depth, background, plane, object, etc.


public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

        // Prepare the rendering objects. This involves reading shaders, so may throw an IOException.
        try {
            // Create the texture and pass it to ARCore session to be filled during update().
            depthTexture.createOnGlThread();
            backgroundRenderer.createOnGlThread(/*context=*/ this, depthTexture.getTextureId());
            // 바닥 인식 이미지 png로 변경가능
            planeRenderer.createOnGlThread(/*context=*/ this, "models/trigrid.png");//바닥 이미지
            pointCloudRenderer.createOnGlThread(/*context=*/ this);
            // 3D obj 파일 변경 및 texture 파일 변경
            virtualObject.createOnGlThread(/*context=*/ this, obj_file, png_file);//obj & texture파일
            virtualObject.setBlendMode(BlendMode.AlphaBlending);
            virtualObject.setDepthTexture(
                    depthTexture.getTextureId(), depthTexture.getWidth(), depthTexture.getHeight());
            // 빛 세기 ambient(주변광), diffuse(분산광), specular(반사광)//0.0f,2.0f,0.5f,6.0f
            virtualObject.setMaterialProperties(0.0f, 2.0f, 0.5f, 6.0f);

        } catch (IOException e) {
            Log.e(TAG, "Failed to read an asset file", e);
        }
    }
  1. OnDrawFrame: render the object by rotating it through. The obj_file, png_file variables include the corresponding obj file and texture file, set the Depth-related settings, return to the try catch syntax above, set the coordinate and scale values of the object through the updateModelMatrix method, and render the object through the draw method.

@Override
    public void onDrawFrame(GL10 gl) {
        try {
            for (ColoredAnchor coloredAnchor : anchors) {
                // Update and draw the model and its shadow.
                virtualObject.updateModelMatrix(anchorMatrix, scaleFactor);
                virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color);
            }
        } catch (Throwable t) {
            // Avoid crashing the application due to unhandled exceptions.
            Log.e(TAG, "Exception on the OpenGL thread", t);
        }
        //3D 객체 교체시 필요
        if (isObjectReplaced) {
            isObjectReplaced = false;
            try {
                virtualObject.createOnGlThread(this, obj_file, png_file);
                virtualObject.setBlendMode(BlendMode.AlphaBlending);
                virtualObject.setDepthTexture(
                        depthTexture.getTextureId(), depthTexture.getWidth(), depthTexture.getHeight());//Depth Settings(depth 적용)
                virtualObject.setMaterialProperties(0.0f, 2.0f, 0.5f, 6.0f);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
    }

Clone this wiki locally