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

The following examples show how to use android.opengl.GLES20#glAttachShader() . 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: RenderPass.java    From smartGL with Apache License 2.0 6 votes vote down vote up
void load() {
	Assert.assertTrue(!isLoaded());
	if (mShaders != null) {
		mProgramId = GLES20.glCreateProgram();
		
		Assert.assertTrue(mProgramId != NOPROGRAM);
		if (!mShaders.isLoaded()) {
			mShaders.loadShader(mProgramId);
		}
		GLES20.glAttachShader(mProgramId, mShaders.getVertexScriptId());
		GLES20.glAttachShader(mProgramId, mShaders.getPixelScriptId());
		GLES20.glLinkProgram(mProgramId);
		int[] linkStatus = new int[1];
		GLES20.glGetProgramiv(mProgramId, GLES20.GL_LINK_STATUS, linkStatus, 0);
		Assert.assertTrue(linkStatus[0] == GLES20.GL_TRUE);
		mShaders.init(mProgramId);
	}
}
 
Example 3
Source File: TextureRenderer.java    From VideoCompressor 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();
    checkGlError("glCreateProgram");
    if (program == 0) {
        return 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) {
        GLES20.glDeleteProgram(program);
        program = 0;
    }
    return program;
}
 
Example 4
Source File: TextureRenderer.java    From VideoCompressor 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();
    checkGlError("glCreateProgram");
    if (program == 0) {
        return 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) {
        GLES20.glDeleteProgram(program);
        program = 0;
    }
    return program;
}
 
Example 5
Source File: TextureRender.java    From android-transcoder 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();
    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 6
Source File: GSYVideoGLViewBaseRender.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
protected 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) {
            Debuger.printfError("Could not link program: ");
            Debuger.printfError(GLES20.glGetProgramInfoLog(program));
            GLES20.glDeleteProgram(program);
            program = 0;
        }
    }
    return program;
}
 
Example 7
Source File: ColorFade.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private boolean initGLShaders(Context context) {
    int vshader = loadShader(context, com.android.internal.R.raw.color_fade_vert,
            GLES20.GL_VERTEX_SHADER);
    int fshader = loadShader(context, com.android.internal.R.raw.color_fade_frag,
            GLES20.GL_FRAGMENT_SHADER);
    GLES20.glReleaseShaderCompiler();
    if (vshader == 0 || fshader == 0) return false;

    mProgram = GLES20.glCreateProgram();

    GLES20.glAttachShader(mProgram, vshader);
    GLES20.glAttachShader(mProgram, fshader);
    GLES20.glDeleteShader(vshader);
    GLES20.glDeleteShader(fshader);

    GLES20.glLinkProgram(mProgram);

    mVertexLoc = GLES20.glGetAttribLocation(mProgram, "position");
    mTexCoordLoc = GLES20.glGetAttribLocation(mProgram, "uv");

    mProjMatrixLoc = GLES20.glGetUniformLocation(mProgram, "proj_matrix");
    mTexMatrixLoc = GLES20.glGetUniformLocation(mProgram, "tex_matrix");

    mOpacityLoc = GLES20.glGetUniformLocation(mProgram, "opacity");
    mGammaLoc = GLES20.glGetUniformLocation(mProgram, "gamma");
    mTexUnitLoc = GLES20.glGetUniformLocation(mProgram, "texUnit");

    GLES20.glUseProgram(mProgram);
    GLES20.glUniform1i(mTexUnitLoc, 0);
    GLES20.glUseProgram(0);

    return true;
}
 
Example 8
Source File: Square.java    From opengl with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 */
public Square() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
    // (# of coordinate values * 4 bytes per float)
            squareCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareCoords);
    vertexBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyGLRenderer.loadShader(
            GLES20.GL_VERTEX_SHADER,
            vertexShaderCode);
    int fragmentShader = MyGLRenderer.loadShader(
            GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode);

    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
}
 
Example 9
Source File: GLUtil.java    From MD360Player4Android with Apache License 2.0 5 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 10
Source File: GLDrawer2D.java    From TimeLapseRecordingSample with Apache License 2.0 5 votes vote down vote up
/**
 * load, compile and link shader
 * @param vss source of vertex shader
 * @param fss source of fragment shader
 * @return
 */
public static int loadShader(String vss, String fss) {
	if (DEBUG) Log.v(TAG, "loadShader:");
	int vs = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
	GLES20.glShaderSource(vs, vss);
	GLES20.glCompileShader(vs);
	final int[] compiled = new int[1];
	GLES20.glGetShaderiv(vs, GLES20.GL_COMPILE_STATUS, compiled, 0);
	if (compiled[0] == 0) {
		if (DEBUG) Log.e(TAG, "Failed to compile vertex shader:"
				+ GLES20.glGetShaderInfoLog(vs));
		GLES20.glDeleteShader(vs);
		vs = 0;
	}

	int fs = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
	GLES20.glShaderSource(fs, fss);
	GLES20.glCompileShader(fs);
	GLES20.glGetShaderiv(fs, GLES20.GL_COMPILE_STATUS, compiled, 0);
	if (compiled[0] == 0) {
		if (DEBUG) Log.w(TAG, "Failed to compile fragment shader:"
			+ GLES20.glGetShaderInfoLog(fs));
		GLES20.glDeleteShader(fs);
		fs = 0;
	}

	final int program = GLES20.glCreateProgram();
	GLES20.glAttachShader(program, vs);
	GLES20.glAttachShader(program, fs);
	GLES20.glLinkProgram(program);

	return program;
}
 
Example 11
Source File: GlUtil.java    From sealrtc-android 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 12
Source File: OpenGlUtils.java    From TikTok 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 13
Source File: GLRenderer.java    From bombsquad-remote-android with Apache License 2.0 4 votes vote down vote up
Square() {
  // initialize vertex byte buffer for shape coordinates
  ByteBuffer bb = ByteBuffer.allocateDirect(
      // (# of coordinate values * 4 bytes per float)
      squareCoords.length * 4);
  bb.order(ByteOrder.nativeOrder());
  vertexBuffer = bb.asFloatBuffer();
  vertexBuffer.put(squareCoords);
  vertexBuffer.position(0);

  // initialize byte buffer for the draw list
  ByteBuffer dlb = ByteBuffer.allocateDirect(
      // (# of coordinate values * 2 bytes per short)
      drawOrder.length * 2);
  dlb.order(ByteOrder.nativeOrder());
  drawListBuffer = dlb.asShortBuffer();
  drawListBuffer.put(drawOrder);
  drawListBuffer.position(0);

  // prepare shaders and OpenGL program
  String vertexShaderCode = "uniform mat4 uMVPMatrix;" +

      "attribute vec4 vPosition;" + "void main() {" +
      // the matrix must be included as a modifier of gl_Position
      "  gl_Position = vPosition * uMVPMatrix;" + "}";
  int vertexShader =
      GLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
  String fragmentShaderCode =
      "precision mediump float;" + "uniform vec4 " + "vColor;" + "void " +
          "main() {" + "  gl_FragColor = vColor;" + "}";
  int fragmentShader =
      GLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

  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
}
 
Example 14
Source File: CameraGLRendererBase.java    From OpenCvFaceDetect with Apache License 2.0 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 15
Source File: CameraGLRendererBase.java    From OpenCV-android with Apache License 2.0 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 16
Source File: CameraGLRendererBase.java    From FaceT with Mozilla Public License 2.0 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 17
Source File: PointsMatrix.java    From MegviiFacepp-Android-SDK with Apache License 2.0 4 votes vote down vote up
public PointsMatrix(boolean isFaceCompare) {
	// FloatBuffer fb_0 = floatBufferUtil(squareCoords);
	// FloatBuffer fb_1 = floatBufferUtil(squareCoords_1);
	// vertexBuffers.add(fb_0);
	// vertexBuffers.add(fb_1);
	// initialize byte buffer for the draw list
	this.isFaceCompare = isFaceCompare;

	ByteBuffer dlb = ByteBuffer.allocateDirect(
			// (# of coordinate values * 2 bytes per short)
			drawOrder.length * 2);
	dlb.order(ByteOrder.nativeOrder());
	drawListBuffer = dlb.asShortBuffer();
	drawListBuffer.put(drawOrder);
	drawListBuffer.position(0);
	ByteBuffer line_dlb = ByteBuffer.allocateDirect(
			// (# of coordinate values * 2 bytes per short)
			drawLineOrder.length * 2);
	line_dlb.order(ByteOrder.nativeOrder());
	drawLineListBuffer = line_dlb.asShortBuffer();
	drawLineListBuffer.put(drawLineOrder);
	drawLineListBuffer.position(0);
	for (int i = 0; i < cubeOrders.length; ++i) {
		final short cubeOrder[] = cubeOrders[i];
		ByteBuffer cubedlb = ByteBuffer.allocateDirect(
				// (# of coordinate values * 2 bytes per short)
				cubeOrder.length * 2);
		cubedlb.order(ByteOrder.nativeOrder());
		cubeListBuffer[i] = cubedlb.asShortBuffer();
		cubeListBuffer[i].put(cubeOrder);
		cubeListBuffer[i].position(0);
	}

	ByteBuffer faceRectLDB = ByteBuffer.allocateDirect(drawFaceRectOrder.length * 2);
	faceRectLDB.order(ByteOrder.nativeOrder());
	faceRectListBuffer = faceRectLDB.asShortBuffer();
	faceRectListBuffer.put(drawFaceRectOrder);
	faceRectListBuffer.position(0);

	// prepare shaders and OpenGL program
	int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
	int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

	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
}
 
Example 18
Source File: AndroidGL.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void attachShader(int program, int shader) {
    GLES20.glAttachShader(program, shader);
}
 
Example 19
Source File: CameraGLRendererBase.java    From ml-authentication with Apache License 2.0 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 20
Source File: ShaderHelper.java    From Muzesto with GNU 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;
}