Huawei HiAnimals AR project development(Part II)

Melikeeroglu
Huawei Developers
Published in
3 min readSep 30, 2020

Hi everybody, this is the second part of HiAnimals application. In the first part we prepared AndroidManifest, gradle files and integrate Huawei Kits. In this part we will build common classes. Let’s start.

You can reach previous part from here.

1- ARDemoRuntimeException class

We create Exception class to handle any exception occur.

public class ArDemoRuntimeException extends RuntimeException {

public ArDemoRuntimeException(String message) {
super(message);
}

}

2- ConnectAppMarketActivity class

We create AppMarket Activity to help user to download Huawei AR Engine if it is not installed to user’s phone.

3- MatrixUtil

  • getProjectionMatrix():Firstly, we need to calculate orthographic projection (is a means of representing three-dimensional objects in two dimensions) matrix to adjust screen’s aspect ratio.
Matrix.orthoM(projection, 0, -1, 1, -1, 1, 1, 3);

Then we need to set camera position.

Matrix.setLookAtM(camera, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0);

Finally we need to apply the projection and camera view transformations, multiply the matrices together.

Matrix.multiplyMM(matrix, 0, projection, 0, camera, 0);
  • normalizeVec3():Calculates the unit vector in the same direction as the original vector
  • getOriginalMatrix():Provides 4*4 unit matrix.

4- DisplayRotationManager

We will implements DisplayListener interface to updated to correctly display the geometric information returned by the AR Engine, when a device is rotated

  • registerDisplayListener():Registers to DisplayListener to listen to rotation of device.
  • unregisterDisplayListener():Unregisters to DisplayListener to stop listen to rotation of device.
  • updateViewportRotation():When a device is rotated, the viewfinder size and whether the device is rotated should be updated to correctly display the geometric information returned by the AR Engine. This method should be called when onSurfaceChanged.
  • getDeviceRotation():Return whether the current device is rotated.
  • updateArSessionDisplayGeometry():If the device is rotated, update the device window of the current ARSession.

This method can be called when onDrawFrame is called.

5- ShaderUtil

Before we start building the Shader class, I’d like to talk about what it’s all about.

We can define the process of reflecting the effects of parameters such as defined light sources, surface materials, and geometries on the generated image by calculating them as shading. In the example shown above, there are two separate 3-dimensional scenes created by the GPU (Graphics Processing Unit).

The image on the right is created on the light and material information on the left, without being affected by environmental conditions.

There are two types of shader programming.

  • Vertex Shader: With a vertex shader program you will write, you can operate on each vertex sent to the GPU.
  • Fragment (pixel) Shader: You can do fragment shader programming in the part of the image created by the GPU before entering the raster(pixel-wise) operations of the image.

Now we can add vertex and fragment shaders for both background and object into raw folder which is into res folder.

(You can reach .shader files from demo project.)

After that, we can create ShaderUtil class to read shader code and compile links.

  • checkGlError():Check OpenGL ES running exceptions and throw them when necessary.
  • readRawTextFile():Read .shader file which is in the raw folder.
  • glCreateShader: creates an empty shader object and returns a non-zero value by which it can be referenced.

- glShaderSource: Replaces the source code in a shader object.

- glCompileShader: Compiles a shader object

6- TextureDisplay

We will create TextureDisplay class for drawing camera textures that you can use to display camera images on the screen.

Before we can render our plane, we need to create the buffer that contains its vertex positions and put the vertex positions in it.

- initBuffers — In this method we will initialize the texture and vertex buffers.

-clear — In this method we will clear background color and buffers.

- loadShader — Load fragment and vertex shaders.

-createGlProgram — Call loadshader method here.

-createProgram — Create Gl ES shader program here.

- onDrawFrame ­ — Render each frame.

-init — Initialize the texture ID and create the OpenGL ES shader program.

  • onSurfaceChanged — Update the projection matrix when surface changed.

You can reach third part from here.

You can reach source code from here.

You can download app from here.

https://appgallery.huawei.com/#/app/C102935435

--

--