Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shader for Android OpenGL ES

Is it possible to use vertex or pixel shaders in android app? please give an example if possible of setting up shader.

like image 663
Yuriy Vikulov Avatar asked Jan 31 '11 10:01

Yuriy Vikulov


People also ask

What is OpenGL es Android?

What Is OpenGL ES? OpenGL, which is short for Open Graphics Library, is a platform-independent API that allows you to create hardware-accelerated 3D graphics. OpenGL ES, short for OpenGL for Embedded Systems, is a subset of the API. OpenGL ES is a very low-level API.

Can Android run OpenGL?

Android includes support for high performance 2D and 3D graphics with the Open Graphics Library (OpenGL), specifically, the OpenGL ES API. OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware.

Does OpenGL es support compute shaders?

Compute shaders introduce GPU Compute from within the OpenGL® ES API; the same API and shading language which are used for graphics rendering. Now that compute shaders have been introduced to the API, developers do not have to learn another API in order to make use of GPU Compute.

Is OpenGL es the same as OpenGL?

The main difference between the two is that OpenGL ES is made for embedded systems like smartphones, while OpenGL is the one on desktops. On the coding level, OpenGL ES does not support fixed-function functions like glBegin/glEnd etc... OpenGL can support fixed-function pipeline (using a compatibility profile).


1 Answers

If you're targetting andriod 2.x / OpengL ES 2, then yes, it's possible.

Here is a code example of how to load a shader:

public int createProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);

    int program = GLES20.glCreateProgram();
    if (program != 0) {
        GLES20.glAttachShader(program, vertexShader);
        checkGlError("glAttachShader");
        GLES20.glAttachShader(program, pixelShader);
        checkGlError("glAttachShader");
        GLES20.glLinkProgram(program);
        int[] linkStatus = new int[1];
        GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
        if (linkStatus[0] != GLES20.GL_TRUE) {
            Log.e(TAG, "Could not link program: ");
            Log.e(TAG, GLES20.glGetProgramInfoLog(program));
            GLES20.glDeleteProgram(program);
            program = 0;
        }
    }
    return program;
}

private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
        if (shader != 0) {
            GLES20.glShaderSource(shader, source);
            GLES20.glCompileShader(shader);
            int[] compiled = new int[1];
            GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
            if (compiled[0] == 0) {
                Log.e(TAG, "Could not compile shader " + shaderType + ":");
                Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
                GLES20.glDeleteShader(shader);
                shader = 0;
            }
        }
        return shader;
}
like image 68
Damian Avatar answered Nov 14 '22 07:11

Damian