37.6 ARCamera开发指南(With ARCore)

less than 1 minute read

1. 集成ARCore

2. 集成渲染环境(OpenGL)

  1. 使用并创建GLSurfaceView
  2. 初始化Renderer
  surfaceView.setPreserveEGLContextOnPause(true);
  surfaceView.setEGLContextClientVersion(2);
  surfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // Alpha used for plane blending.
  // 设置GLSurfaceView#Renderer
  surfaceView.setRenderer(this);
  surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
  surfaceView.setWillNotDraw(false);
  1. 编写shader, 并进行attach(GLES20#glAttachShader()), 链接(GLES20#glLinkProgram)
  2. shader如何编写待补充

  3. GLSurfaceView.Renderer#onSurfaceCreated(), 获取shader的变量并进行初始化; 创建纹理

3. 加载3D模型

  1. obj文件使用de.javagl.obj开源库进行加载
  2. ObjReader#read(objInputStream);读取obj文件
  3. 调用ObjUtils#convertToRenderable(obj), 准备obj文件并确定适合OpenGL的渲染, 经过1). Triangulate it, 2). Make sure that texture coordinates are not ambiguous, 3). Make sure that normals are not ambiguous, 4). Convert it to single-indexed data
  4. 获取obj文件的数据, 包括wideIndices, vertices, texCoords, normalsdirect buffers

4. 3D模型动画渲染

5. ARCore与OpenGL交互

  1. 创建Session, 与ARCore进行交互
  2. Session#setDisplayGeometry(), 更新屏幕的几何(rotation)显示

6. 渲染

参考