Java Code Examples for android.opengl.GLES20#glCreateProgram()

The following examples show how to use android.opengl.GLES20#glCreateProgram() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GLES20Canvas.java    From TurboLauncher with Apache License 2.0 6 votes vote down vote up
private int assembleProgram(int vertexShader, int fragmentShader, ShaderParameter[] params) {
    int program = GLES20.glCreateProgram();
    checkError();
    if (program == 0) {
        throw new RuntimeException("Cannot create GL program: " + GLES20.glGetError());
    }
    GLES20.glAttachShader(program, vertexShader);
    checkError();
    GLES20.glAttachShader(program, fragmentShader);
    checkError();
    GLES20.glLinkProgram(program);
    checkError();
    int[] mLinkStatus = mTempIntArray;
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, mLinkStatus, 0);
    if (mLinkStatus[0] != GLES20.GL_TRUE) {
        Log.e(TAG, "Could not link program: ");
        Log.e(TAG, GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
    }
    for (int i = 0; i < params.length; i++) {
        params[i].loadHandle(program);
    }
    return program;
}
 
Example 2
Source File: GlUtil.java    From FuAgoraDemoDroid with MIT License 5 votes vote down vote up
/**
 * Creates a new program from the supplied vertex and fragment shaders.
 *
 * @return A handle to the program, or 0 on failure.
 */
public static int createProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
        return 0;
    }
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (pixelShader == 0) {
        return 0;
    }

    int program = GLES20.glCreateProgram();
    checkGlError("glCreateProgram");
    if (program == 0) {
        Log.e(TAG, "Could not create program");
    }
    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;
}
 
Example 3
Source File: ExtractMpegFramesTest.java    From Android-MediaCodec-Examples with Apache License 2.0 5 votes vote down vote up
private int createProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
        return 0;
    }
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (pixelShader == 0) {
        return 0;
    }

    int program = GLES20.glCreateProgram();
    if (program == 0) {
        Log.e(TAG, "Could not create program");
    }
    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;
}
 
Example 4
Source File: Cube.java    From gdk-apidemo-sample with Apache License 2.0 5 votes vote down vote up
public Cube() {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(VERTICES.length * 4);

    byteBuffer.order(ByteOrder.nativeOrder());
    mVertexBuffer = byteBuffer.asFloatBuffer();
    mVertexBuffer.put(VERTICES);
    mVertexBuffer.position(0);

    byteBuffer = ByteBuffer.allocateDirect(COLORS.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    mColorBuffer = byteBuffer.asFloatBuffer();
    mColorBuffer.put(COLORS);
    mColorBuffer.position(0);

    mIndexBuffer = ByteBuffer.allocateDirect(INDICES.length);
    mIndexBuffer.put(INDICES);
    mIndexBuffer.position(0);

    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_CODE));
    GLES20.glAttachShader(
            mProgram, loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_CODE));
    GLES20.glLinkProgram(mProgram);

    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    mColorHandle = GLES20.glGetAttribLocation(mProgram, "vColor");
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
}
 
Example 5
Source File: GLShape.java    From Android-AudioRecorder-App with Apache License 2.0 5 votes vote down vote up
public GLShape(float[] color) {
  this.color = color;
  int vertexShader = GLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_CODE);
  int fragmentShader = GLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_CODE);
  program = GLES20.glCreateProgram();
  GLES20.glAttachShader(program, vertexShader);
  GLES20.glAttachShader(program, fragmentShader);
  GLES20.glLinkProgram(program);
}
 
Example 6
Source File: Program.java    From HokoBlur with Apache License 2.0 5 votes vote down vote up
@Override
    public void create(String vertexShaderCode, String fragmentShaderCode) {

        int vertexShader = 0;
        int fragmentShader = 0;
        try {
            vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
            fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

            if (vertexShader == 0 || fragmentShader == 0) {
                return;
            }

            id = GLES20.glCreateProgram();
            if (id != 0) {
                GLES20.glAttachShader(id, vertexShader);
                GLES20.glAttachShader(id, fragmentShader);
                GLES20.glLinkProgram(id);
                checkGLError("Attach Shader");
                final int[] linkStatus = new int[1];
                GLES20.glGetProgramiv(id, GLES20.GL_LINK_STATUS, linkStatus, 0);
                if (linkStatus[0] != 1) {
                    Log.e(TAG, "Failed to link program");
                    GLES20.glDeleteProgram(id);
                    id = 0;
                }
//
            }
        } finally {
            GLES20.glDetachShader(id, vertexShader);
            GLES20.glDetachShader(id, fragmentShader);
            GLES20.glDeleteShader(vertexShader);
            GLES20.glDeleteShader(fragmentShader);
        }
    }
 
Example 7
Source File: OpenGlUtils.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
public static int loadProgram(final String strVSource, final String strFSource) {
    int iVShader;
    int iFShader;
    int iProgId;
    int[] link = new int[1];
    iVShader = loadShader(strVSource, GLES20.GL_VERTEX_SHADER);
    if (iVShader == 0) {
        Log.d("Load Program", "Vertex Shader Failed");
        return 0;
    }
    iFShader = loadShader(strFSource, GLES20.GL_FRAGMENT_SHADER);
    if (iFShader == 0) {
        Log.d("Load Program", "Fragment Shader Failed");
        return 0;
    }

    iProgId = GLES20.glCreateProgram();

    GLES20.glAttachShader(iProgId, iVShader);
    GLES20.glAttachShader(iProgId, iFShader);

    GLES20.glLinkProgram(iProgId);

    GLES20.glGetProgramiv(iProgId, GLES20.GL_LINK_STATUS, link, 0);
    if (link[0] <= 0) {
        Log.d("Load Program", "Linking Failed");
        return 0;
    }
    GLES20.glDeleteShader(iVShader);
    GLES20.glDeleteShader(iFShader);
    return iProgId;
}
 
Example 8
Source File: GLToolbox.java    From graphics-samples with Apache License 2.0 5 votes vote down vote up
public static int createProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
        return 0;
    }
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (pixelShader == 0) {
        return 0;
    }

    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) {
            String info = GLES20.glGetProgramInfoLog(program);
            GLES20.glDeleteProgram(program);
            throw new RuntimeException("Could not link program: " + info);
        }
    }
    return program;
}
 
Example 9
Source File: CubeRenderer.java    From HoloKilo with GNU General Public License v3.0 5 votes vote down vote up
public static int loadProgram(String strVSource, String strFSource)
{
    int iVShader;
    int iFShader;
    int iProgId;
    int[] link = new int[1];
    iVShader = loadShader(strVSource, GLES20.GL_VERTEX_SHADER);
    if (iVShader == 0)
    {
        Log.d("Load Program", "Vertex Shader Failed");
        return 0;
    }
    iFShader = loadShader(strFSource, GLES20.GL_FRAGMENT_SHADER);
    if(iFShader == 0)
    {
        Log.d("Load Program", "Fragment Shader Failed");
        return 0;
    }

    iProgId = GLES20.glCreateProgram();

    GLES20.glAttachShader(iProgId, iVShader);
    GLES20.glAttachShader(iProgId, iFShader);

    GLES20.glLinkProgram(iProgId);

    GLES20.glGetProgramiv(iProgId, GLES20.GL_LINK_STATUS, link, 0);
    if (link[0] <= 0) {
        Log.d("Load Program", "Linking Failed");
        return 0;
    }
    GLES20.glDeleteShader(iVShader);
    GLES20.glDeleteShader(iFShader);
    return iProgId;
}
 
Example 10
Source File: GlShader.java    From VideoCRE with MIT License 5 votes vote down vote up
public GlShader(String vertexSource, String fragmentSource) {
  final int vertexShader = compileShader(GLES20.GL_VERTEX_SHADER, vertexSource);
  final int fragmentShader = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
  program = GLES20.glCreateProgram();
  if (program == 0) {
    throw new RuntimeException("glCreateProgram() failed. GLES20 error: " + GLES20.glGetError());
  }
  GLES20.glAttachShader(program, vertexShader);
  GLES20.glAttachShader(program, fragmentShader);
  GLES20.glLinkProgram(program);
  int[] linkStatus = new int[] {GLES20.GL_FALSE};
  GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
  if (linkStatus[0] != GLES20.GL_TRUE) {
    Logging.e(TAG, "Could not link program: " + GLES20.glGetProgramInfoLog(program));
    throw new RuntimeException(GLES20.glGetProgramInfoLog(program));
  }
  // According to the documentation of glLinkProgram():
  // "After the link operation, applications are free to modify attached shader objects, compile
  // attached shader objects, detach shader objects, delete shader objects, and attach additional
  // shader objects. None of these operations affects the information log or the program that is
  // part of the program object."
  // But in practice, detaching shaders from the program seems to break some devices. Deleting the
  // shaders are fine however - it will delete them when they are no longer attached to a program.
  GLES20.glDeleteShader(vertexShader);
  GLES20.glDeleteShader(fragmentShader);
  GlUtil.checkNoGLES2Error("Creating GlShader");
}
 
Example 11
Source File: OrientationView.java    From PanoramaGL with Apache License 2.0 4 votes vote down vote up
@Override
public final void onSurfaceCreated(GL10 unused, EGLConfig config) {
  // Set up shaders
  int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
  GLES20.glShaderSource(vertexShader, vertexShaderCode);
  GLES20.glCompileShader(vertexShader);

  int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
  GLES20.glShaderSource(fragmentShader, fragmentShaderCode);
  GLES20.glCompileShader(fragmentShader);

  program = GLES20.glCreateProgram();
  GLES20.glAttachShader(program, vertexShader);
  GLES20.glAttachShader(program, fragmentShader);
  GLES20.glLinkProgram(program);

  int[] linkStatus = new int[1];
  GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
  if (linkStatus[0] != GLES20.GL_TRUE) {
    throw new GLException(linkStatus[0],
        "Unable to create shader program: " + GLES20.glGetProgramInfoLog(program));
  }

  // Set up geometry
  // 16 lines * 2 points * XYZ
  float[] boxVertexData = {
      // X-aligned lines of length 4
      -2, -1, -3,  2, -1, -3,
      -2, -1,  3,  2, -1,  3,
      -2,  1, -3,  2,  1, -3,
      -2,  1,  3,  2,  1,  3,
      // Y-aligned lines of length 2
      -2, -1, -3, -2,  1, -3,
      -2, -1,  3, -2,  1,  3,
       2, -1, -3,  2,  1, -3,
       2, -1,  3,  2,  1,  3,
      // Z-aligned lines of length 6
      -2, -1, -3, -2, -1,  3,
      -2,  1, -3, -2,  1,  3,
       2, -1, -3,  2, -1,  3,
       2,  1, -3,  2,  1,  3,
      // Trackpad diamond
      -1,  1, -1,  0,  1,  0,
       0,  1,  0,  1,  1, -1,
       1,  1, -1,  0,  1, -3,
       0,  1, -3, -1,  1, -1,
  };

  ByteBuffer buffer = ByteBuffer.allocateDirect(boxVertexData.length * 4);
  buffer.order(ByteOrder.nativeOrder());
  boxVertices = buffer.asFloatBuffer();
  boxVertices.put(boxVertexData);
  boxVertices.position(0);

  // The XYZ lines are RGB in the positive direction and black in the negative direction.
  // 16 lines * 2 points * RGBA
  float[] boxColorData = {
      // X-aligned lines
      0, 0, 0, 1, 1, 0, 0, 1,
      0, 0, 0, 1, 1, 0, 0, 1,
      0, 0, 0, 1, 1, 0, 0, 1,
      0, 0, 0, 1, 1, 0, 0, 1,
      // Y-aligned lines
      0, 0, 0, 1, 0, 1, 0, 1,
      0, 0, 0, 1, 0, 1, 0, 1,
      0, 0, 0, 1, 0, 1, 0, 1,
      0, 0, 0, 1, 0, 1, 0, 1,
      // Z-aligned lines
      0, 0, 0, 1, 0, 0, 1, 1,
      0, 0, 0, 1, 0, 0, 1, 1,
      0, 0, 0, 1, 0, 0, 1, 1,
      0, 0, 0, 1, 0, 0, 1, 1,
      // Trackpad
      0, 1, 0, 1, 0, 1, 0, 1,
      0, 1, 0, 1, 0, 1, 0, 1,
      0, 1, 0, 1, 0, 1, 0, 1,
      0, 1, 0, 1, 0, 1, 0, 1,
  };
  buffer = ByteBuffer.allocateDirect(boxColorData.length * 4);
  buffer.order(ByteOrder.nativeOrder());
  boxColors = buffer.asFloatBuffer();
  boxColors.put(boxColorData);
  boxColors.position(0);
}
 
Example 12
Source File: Utils.java    From Paddle-Lite-Demo with Apache License 2.0 4 votes vote down vote up
public static int createShaderProgram(String vss, String fss) {
    int vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vshader, vss);
    GLES20.glCompileShader(vshader);
    int[] status = new int[1];
    GLES20.glGetShaderiv(vshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetShaderInfoLog(vshader));
        GLES20.glDeleteShader(vshader);
        vshader = 0;
        return 0;
    }

    int fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fshader, fss);
    GLES20.glCompileShader(fshader);
    GLES20.glGetShaderiv(fshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetShaderInfoLog(fshader));
        GLES20.glDeleteShader(vshader);
        GLES20.glDeleteShader(fshader);
        fshader = 0;
        return 0;
    }

    int program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vshader);
    GLES20.glAttachShader(program, fshader);
    GLES20.glLinkProgram(program);
    GLES20.glDeleteShader(vshader);
    GLES20.glDeleteShader(fshader);
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetProgramInfoLog(program));
        program = 0;
        return 0;
    }
    GLES20.glValidateProgram(program);
    GLES20.glGetProgramiv(program, GLES20.GL_VALIDATE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
        return 0;
    }

    return program;
}
 
Example 13
Source File: GLUtil.java    From android-3D-model-viewer with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Helper function to compile and link a program.
 * 
 * @param vertexShaderHandle
 *            An OpenGL handle to an already-compiled vertex shader.
 * @param fragmentShaderHandle
 *            An OpenGL handle to an already-compiled fragment shader.
 * @param attributes
 *            Attributes that need to be bound to the program.
 * @return An OpenGL handle to the program.
 */
public static int createAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle,
		final String[] attributes) {
	int programHandle = GLES20.glCreateProgram();

	if (programHandle != 0) {
		// Bind the vertex shader to the program.
		GLES20.glAttachShader(programHandle, vertexShaderHandle);

		// Bind the fragment shader to the program.
		GLES20.glAttachShader(programHandle, fragmentShaderHandle);

		// Bind attributes
		if (attributes != null) {
			final int size = attributes.length;
			for (int i = 0; i < size; i++) {
				GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
			}
		}

		// Link the two shaders together into a program.
		GLES20.glLinkProgram(programHandle);

		// Get the link status.
		final int[] linkStatus = new int[1];
		GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

		// If the link failed, delete the program.
		if (linkStatus[0] == 0) {
			Log.e(TAG, "Error compiling program: " + GLES20.glGetProgramInfoLog(programHandle));
			GLES20.glDeleteProgram(programHandle);
			programHandle = 0;
		}
	}

	if (programHandle == 0) {
		throw new RuntimeException("Error creating program.");
	}

	return programHandle;
}
 
Example 14
Source File: Utils.java    From Paddle-Lite-Demo with Apache License 2.0 4 votes vote down vote up
public static int createShaderProgram(String vss, String fss) {
    int vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vshader, vss);
    GLES20.glCompileShader(vshader);
    int[] status = new int[1];
    GLES20.glGetShaderiv(vshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetShaderInfoLog(vshader));
        GLES20.glDeleteShader(vshader);
        vshader = 0;
        return 0;
    }

    int fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fshader, fss);
    GLES20.glCompileShader(fshader);
    GLES20.glGetShaderiv(fshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetShaderInfoLog(fshader));
        GLES20.glDeleteShader(vshader);
        GLES20.glDeleteShader(fshader);
        fshader = 0;
        return 0;
    }

    int program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vshader);
    GLES20.glAttachShader(program, fshader);
    GLES20.glLinkProgram(program);
    GLES20.glDeleteShader(vshader);
    GLES20.glDeleteShader(fshader);
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetProgramInfoLog(program));
        program = 0;
        return 0;
    }
    GLES20.glValidateProgram(program);
    GLES20.glGetProgramiv(program, GLES20.GL_VALIDATE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e(TAG, GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
        return 0;
    }

    return program;
}
 
Example 15
Source File: Shader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public Shader(String vertexShader, String fragmentShader, String attributes[], String uniforms[]) {
    this.program = GLES20.glCreateProgram();

    CompilationResult vResult = compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    if (vResult.status == GLES20.GL_FALSE) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.e("Vertex shader compilation failed");
        }
        destroyShader(vResult.shader, 0, program);
        return;
    }

    CompilationResult fResult = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
    if (fResult.status == GLES20.GL_FALSE) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.e("Fragment shader compilation failed");
        }
        destroyShader(vResult.shader, fResult.shader, program);
        return;
    }

    GLES20.glAttachShader(program, vResult.shader);
    GLES20.glAttachShader(program, fResult.shader);

    for (int i = 0; i < attributes.length; i++) {
        GLES20.glBindAttribLocation(program, i, attributes[i]);
    }

    if (linkProgram(program) == GLES20.GL_FALSE) {
        destroyShader(vResult.shader, fResult.shader, program);
        return;
    }

    for (String uniform : uniforms) {
        uniformsMap.put(uniform, GLES20.glGetUniformLocation(program, uniform));
    }

    if (vResult.shader != 0) {
        GLES20.glDeleteShader(vResult.shader);
    }

    if (fResult.shader != 0) {
        GLES20.glDeleteShader(fResult.shader);
    }
}
 
Example 16
Source File: Shader.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public Shader(String vertexShader, String fragmentShader, String[] attributes, String[] uniforms) {
    this.program = GLES20.glCreateProgram();

    CompilationResult vResult = compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    if (vResult.status == GLES20.GL_FALSE) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.e("Vertex shader compilation failed");
        }
        destroyShader(vResult.shader, 0, program);
        return;
    }

    CompilationResult fResult = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
    if (fResult.status == GLES20.GL_FALSE) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.e("Fragment shader compilation failed");
        }
        destroyShader(vResult.shader, fResult.shader, program);
        return;
    }

    GLES20.glAttachShader(program, vResult.shader);
    GLES20.glAttachShader(program, fResult.shader);

    for (int i = 0; i < attributes.length; i++) {
        GLES20.glBindAttribLocation(program, i, attributes[i]);
    }

    if (linkProgram(program) == GLES20.GL_FALSE) {
        destroyShader(vResult.shader, fResult.shader, program);
        return;
    }

    for (String uniform : uniforms) {
        uniformsMap.put(uniform, GLES20.glGetUniformLocation(program, uniform));
    }

    if (vResult.shader != 0) {
        GLES20.glDeleteShader(vResult.shader);
    }

    if (fResult.shader != 0) {
        GLES20.glDeleteShader(fResult.shader);
    }
}
 
Example 17
Source File: PlaneRenderer.java    From react-native-arcore with MIT License 4 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the plane renderer.  Must be
 * called on the OpenGL thread, typically in
 * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source and texture PNG.
 * @param gridDistanceTextureName  Name of the PNG file containing the grid texture.
 */
public void createOnGlThread(Context context, String gridDistanceTextureName)
        throws IOException {
    int vertexShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_VERTEX_SHADER, R.raw.plane_vertex);
    int passthroughShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_FRAGMENT_SHADER, R.raw.plane_fragment);

    mPlaneProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mPlaneProgram, vertexShader);
    GLES20.glAttachShader(mPlaneProgram, passthroughShader);
    GLES20.glLinkProgram(mPlaneProgram);
    GLES20.glUseProgram(mPlaneProgram);

    ShaderUtil.checkGLError(TAG, "Program creation");

    // Read the texture.
    Bitmap textureBitmap = BitmapFactory.decodeStream(
        context.getAssets().open(gridDistanceTextureName));

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glGenTextures(mTextures.length, mTextures, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
        GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
        GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, textureBitmap, 0);
    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

    ShaderUtil.checkGLError(TAG, "Texture loading");

    mPlaneXZPositionAlphaAttribute = GLES20.glGetAttribLocation(mPlaneProgram,
        "a_XZPositionAlpha");

    mPlaneModelUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_Model");
    mPlaneModelViewProjectionUniform =
        GLES20.glGetUniformLocation(mPlaneProgram, "u_ModelViewProjection");
    mTextureUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_Texture");
    mLineColorUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_lineColor");
    mDotColorUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_dotColor");
    mGridControlUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_gridControl");
    mPlaneUvMatrixUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_PlaneUvMatrix");

    ShaderUtil.checkGLError(TAG, "Program parameters");
}
 
Example 18
Source File: CameraGLRendererBase.java    From MOAAP with MIT License 4 votes vote down vote up
private static int loadShader(String vss, String fss) {
    Log.d("CameraGLRendererBase", "loadShader");
    int vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vshader, vss);
    GLES20.glCompileShader(vshader);
    int[] status = new int[1];
    GLES20.glGetShaderiv(vshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e("CameraGLRendererBase", "Could not compile vertex shader: "+GLES20.glGetShaderInfoLog(vshader));
        GLES20.glDeleteShader(vshader);
        vshader = 0;
        return 0;
    }

    int fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fshader, fss);
    GLES20.glCompileShader(fshader);
    GLES20.glGetShaderiv(fshader, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e("CameraGLRendererBase", "Could not compile fragment shader:"+GLES20.glGetShaderInfoLog(fshader));
        GLES20.glDeleteShader(vshader);
        GLES20.glDeleteShader(fshader);
        fshader = 0;
        return 0;
    }

    int program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vshader);
    GLES20.glAttachShader(program, fshader);
    GLES20.glLinkProgram(program);
    GLES20.glDeleteShader(vshader);
    GLES20.glDeleteShader(fshader);
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, status, 0);
    if (status[0] == 0) {
        Log.e("CameraGLRendererBase", "Could not link shader program: "+GLES20.glGetProgramInfoLog(program));
        program = 0;
        return 0;
    }
    GLES20.glValidateProgram(program);
    GLES20.glGetProgramiv(program, GLES20.GL_VALIDATE_STATUS, status, 0);
    if (status[0] == 0)
    {
        Log.e("CameraGLRendererBase", "Shader program validation error: "+GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
        return 0;
    }

    Log.d("CameraGLRendererBase", "Shader program is built OK");

    return program;
}
 
Example 19
Source File: GLImage.java    From AndroidPlayground with MIT License 4 votes vote down vote up
public GLImage(Resources resources) {
    uvBuffer = ByteBuffer.allocateDirect(uvs.length * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
            .put(uvs);
    uvBuffer.position(0);
    vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
            .put(vertices);
    vertexBuffer.position(0);
    drawListBuffer = ByteBuffer.allocateDirect(indices.length * 2)
            .order(ByteOrder.nativeOrder())
            .asShortBuffer()
            .put(indices);
    drawListBuffer.position(0);

    int[] textureNames = new int[1];
    GLES20.glGenTextures(1, textureNames, 0);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureNames[0]);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);

    Bitmap bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    mProgram = GLES20.glCreateProgram();
    int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER);
    int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER);
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);
}
 
Example 20
Source File: AnchorRenderer.java    From justaline-android with Apache License 2.0 4 votes vote down vote up
public AnchorRenderer() {
    // Prepare shaders and OpenGL program
    int vertexShader = loadShader(
            GLES20.GL_VERTEX_SHADER,
            VERTEX_SHADER_CODE);
    if (vertexShader == 0) {
        Log.e(TAG, "Vertex shader failed");
        return;
    }
    int fragmentShader = loadShader(
            GLES20.GL_FRAGMENT_SHADER,
            FRAGMENT_SHADER_CODE);
    if (fragmentShader == 0) {
        Log.e(TAG, "Fragment shader failed");
        return;
    }

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables

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

    // Initialize vertex byte buffer for shape coordinates
    mVertexBuffer = allocateFloatBuffer(VERTICES);

    // Initialize byte buffer for the colors
    mColor1Buffer = allocateFloatBuffer(COLORS1);
    mColor2Buffer = allocateFloatBuffer(COLORS2);

    // Initialize byte buffer for the draw list
    mIndexBuffer = allocateShortBuffer(INDICES);

    Matrix.setIdentityM(mModelMatrix, 0);
}