Usage
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
This page shows how to add CubeTextureView (package com.irurueta.android.gl.cube) to a screen, wait for its
OpenGL surface, set a camera and handle the activity lifecycle. Every property is listed with its default value in
the Configuration reference.
Adding the view to a layout
CubeTextureView is a regular view, so it can be declared in XML next to any other view. Its background is
transparent, so views declared before it remain visible behind the cube:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Any regular view: it stays visible behind the cube -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"
android:textSize="40sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.irurueta.android.gl.cube.CubeTextureView
android:id="@+id/cube"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Creating the view in code
CubeTextureView has a @JvmOverloads constructor (context, attrs = null, defStyleAttr = 0):
val cubeView = CubeTextureView(context)
container.addView(
cubeView,
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
)
Waiting for the surface
The OpenGL surface is created asynchronously after the view is attached and laid out. Until then, the view does not
know its size in pixels, so camera related properties (camera, cameraIntrinsicParameters, cameraCenter,
cameraRotation, nearPlane, farPlane, setNearFarPlanes, setValues, orientation and the viewCamera*
properties) throw IllegalStateException if they are set too early.
Set onSurfaceChangedListener and configure the camera there. The listener is called every time the surface is
created or resized, for instance after a configuration change:
val cubeView = findViewById<CubeTextureView>(R.id.cube)
cubeView.onSurfaceChangedListener = object : CubeTextureView.OnSurfaceChangedListener {
override fun onSurfaceChanged(width: Int, height: Int) {
// The OpenGL surface exists now: camera related properties can be set
cubeView.cubeSize = 0.5f * CubeRenderer.DEFAULT_CUBE_SIZE
cubeView.camera = createCamera(width, height)
}
}
Cube and color properties (cubeSize, cubePosition, cubeRotation, clearColor and diffuseColor) can be set at
any time.
|
|
Creating a camera
The scene is rendered from a PinholeCamera. A convenient way to build one is to convert an OpenGL projection matrix,
like the ones provided by ARCore, into pinhole camera intrinsic parameters with OpenGlToCameraHelper:
import com.irurueta.android.glutils.OpenGlToCameraHelper
import com.irurueta.geometry.PinholeCamera
import com.irurueta.geometry.Point3D
import com.irurueta.geometry.Rotation3D
fun createCamera(width: Int, height: Int): PinholeCamera {
// OpenGL projection matrix, defined column-wise as in android.opengl.Matrix.
// These values come from an ARCore session on a Pixel 2 device.
val projectionMatrix = floatArrayOf(
2.8693345f, 0.0f, 0.0f, 0.0f,
0.0f, 1.5806589f, 0.0f, 0.0f,
-0.004545755f, 0.009158132f, -1.002002f, -1.0f,
0.0f, 0.0f, -0.2002002f, 0.0f
)
// Convert the projection matrix into pinhole camera intrinsic parameters
val intrinsics = OpenGlToCameraHelper.computePinholeCameraIntrinsicsAndReturnNew(
projectionMatrix,
width,
height
)
// Camera located at the origin with no rotation, looking towards the cube
return PinholeCamera(intrinsics, Rotation3D.create(), Point3D.create())
}
See Camera for other ways to define the camera and its pose.
Handling the lifecycle
As with any GLTextureView (or GLSurfaceView), forward the activity or fragment lifecycle to the view, so that the
rendering thread is paused and resumed:
override fun onResume() {
super.onResume()
cubeView.onResume()
}
override fun onPause() {
super.onPause()
cubeView.onPause()
}
When the view is detached from its window, the OpenGL program and buffers are released automatically.
Using the view from Jetpack Compose
CubeTextureView is a classic view, so it can be embedded in a composable with AndroidView. Composables drawn
before it remain visible through its transparent background:
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import com.irurueta.android.gl.cube.CubeTextureView
@Composable
fun CubeScreen() {
Box(modifier = Modifier.fillMaxSize()) {
// Regular composable drawn behind the transparent cube
Text("Lorem ipsum")
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
CubeTextureView(context).apply {
onSurfaceChangedListener =
object : CubeTextureView.OnSurfaceChangedListener {
override fun onSurfaceChanged(width: Int, height: Int) {
camera = createCamera(width, height)
}
}
}
}
)
}
}
Rendering model
The view renders only on demand (RENDER_MODE_WHEN_DIRTY). Every property setter of CubeTextureView requests a new
frame, so there is no need to call requestRender() yourself, and an idle cube does not use the GPU. To animate the
cube, update its properties from an animator, as described in Animation.
Next: Cube and colors.