Init OpenGL

To work with OpenGL on Android, you create an activity with a special view. And write the drawing code in the Render interface. This is a convenient separation.

To make code similar to the Android on other platforms let's create AppOGL class that will responsible for setup a window in which the graphics are displayed. And a Render class, that will be responsible for rendering.

You can download full sources of examples on GitHub for LWJGL 3, WebGL and Android.

WebGL

You can get WebGL context from any <canvas> element like 2D context:

var canvas = document.getElementById(idCanvas);
var gl = canvas.getContext("webgl2"); // or "webgl"

Note. The OpenGL 3.0 features are available within WebGL 2.0.

WebGL example

LWJGL

Add necessary dependencies from dependency builder. For example, select your OS and option "Minimal OpenGL". It will include GLFW library, that provides a simple API for creating windows, contexts and surfaces, receiving input and events for OpenGL. Select JOML addon for work with matrices.

All GL APIs are declared in classes like GL33, so you can make static imports for the required version.

import static org.lwjgl.opengl.GL20.*;
// same thing for GLFW library
import static org.lwjgl.glfw.GLFW.*;

By default OpenGL 2.0 context will be created. You can change it via windows hints, for example to 3.3 core. It will allow you to use shaders with "330 core" version.

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL33.GL_TRUE)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
LWJGL init example
MacOS users should start their application passing "-XstartOnFirstThread" as a VM option.

Render class is a base class for other examples of this tutorial.

Render example

Then you can run application with any your render.

companion object {
    @JvmStatic
    fun main(args: Array<String>) {
        // AppOGL.isV120 = true
        // val app =  AppOGL(300, 300)
        val app =  AppOGL(Render(),300, 300)
    }
}
public static void main(String[] args) { // AppOGL.isV120 = true; // new AppOGL(300,300); // new AppOGL(new QuadRender(), 300,300); new AppOGL(new Render(), 300,300); // }

Android

GLSurfaceView is a special view where you can draw and manipulate objects using OpenGL API calls.

GLSurfaceView.Renderer interface defines the methods required for drawing graphics. You must provide an implementation of this interface as a separate class and attach it to your GLSurfaceView.

When you are using GLSurfaceView, you don't need to handle the cleanup as the resources will be released when the context is lost. So in activity I don't call render.onDispose().

You must specify version of OpenGL in AndroidManifest.xml file.


<!--
0x00020000 - for OpenGL ES 2.0
0x00030000 - for OpenGL ES 3.0
0x00030001 - for OpenGL ES 3.1
-->
<uses-feature android:glEsVersion="0x00030000" android:required="true" />

Note. OpenGL ES 3.0 based on OpenGL 3.3 and OpenGL 4.2. You can test OpenGl API on emulator since Android API 29. Check an advance settings of emulator (must be SwiftShader or Native Desktop).

All GL APIs are declared in classes like android.opengl.GLES30.*, so you can make static imports for the required version.

Activity example

Now Android version of our Render class.

Render example