Java Code Examples for android.opengl.GLES30#glBindTexture()

The following examples show how to use android.opengl.GLES30#glBindTexture() . 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: GLSurface.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * Bitmapから画像をテクスチャに読み込む
 * @param bitmap
 */
@Override
public void loadBitmap(@NonNull final Bitmap bitmap) {
	final int width = bitmap.getWidth();
	final int height = bitmap.getHeight();
	if ((width > mTexWidth) || (height > mTexHeight)) {
		mWidth = width;
		mHeight = height;
		releaseFrameBuffer();
		createFrameBuffer(width, height);
	}
	GLES30.glActiveTexture(TEX_UNIT);
	GLES30.glBindTexture(TEX_TARGET, mFBOTexId);
	GLUtils.texImage2D(TEX_TARGET, 0, bitmap, 0);
	GLES30.glBindTexture(TEX_TARGET, 0);
	// initialize texture matrix
	Matrix.setIdentityM(mTexMatrix, 0);
	mTexMatrix[0] = width / (float)mTexWidth;
	mTexMatrix[5] = height / (float)mTexHeight;
}
 
Example 2
Source File: OverlayRendererHolder.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * internalOnStartの下請け、GLES3用
 */
@WorkerThread
private void internalOnStartES3() {
	if (DEBUG) Log.v(TAG, String.format("internalOnStartES3:init overlay texture(%dx%d)",
		width(), height()));
	if (DEBUG) Log.v(TAG, "internalOnStartES3:shader=" + MY_FRAGMENT_SHADER_EXT_ES3);

	mDrawer.updateShader(MY_FRAGMENT_SHADER_EXT_ES3);
	final int uTex1 = mDrawer.glGetUniformLocation("sTexture");
	GLES30.glUniform1i(uTex1, 0);
	if (DEBUG) Log.v(TAG, "internalOnStart:uTex1=" + uTex1);

	final int uTex2 = mDrawer.glGetUniformLocation("sTexture2");
	mOverlayTexId = GLHelper.initTex(
		GL_TEXTURE_EXTERNAL_OES,
		GLES30.GL_TEXTURE1,
		GLES30.GL_LINEAR, GLES30.GL_LINEAR,
		GLES30.GL_CLAMP_TO_EDGE);
	mOverlayTexture = new SurfaceTexture(mOverlayTexId);
	mOverlayTexture.setDefaultBufferSize(width(), height());
	mOverlaySurface = new Surface(mOverlayTexture);
	GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
	GLES30.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mOverlayTexId);
	GLES30.glUniform1i(uTex2, 1);
	if (DEBUG) Log.v(TAG, "internalOnStart:uTex2=" + uTex2);
}
 
Example 3
Source File: GLHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * テクスチャ名を生成(GL_TEXTURE0のみ)
 * @param texTarget テクスチャのタイプ, GL_TEXTURE_EXTERNAL_OESかGL_TEXTURE_2D
 * @param texUnit テクスチャユニット, GL_TEXTURE0...GL_TEXTURE31
 * @param minFilter テクスチャの補間方法を指定, GL_LINEARとかGL_NEAREST
 * @param magFilter テクスチャの補間方法を指定, GL_LINEARとかGL_NEAREST
 * @param wrap テクスチャのクランプ方法, GL_CLAMP_TO_EDGE等
 * @return
 */
public static int initTex(final int texTarget, final int texUnit,
	final int minFilter, final int magFilter, final int wrap) {

	if (DEBUG) Log.v(TAG, "initTex:target=" + texTarget);
	final int[] tex = new int[1];
	GLES30.glActiveTexture(texUnit);
	GLES30.glGenTextures(1, tex, 0);
	GLES30.glBindTexture(texTarget, tex[0]);
	GLES30.glTexParameteri(texTarget, GLES30.GL_TEXTURE_WRAP_S, wrap);
	GLES30.glTexParameteri(texTarget, GLES30.GL_TEXTURE_WRAP_T, wrap);
	GLES30.glTexParameteri(texTarget, GLES30.GL_TEXTURE_MIN_FILTER, minFilter);
	GLES30.glTexParameteri(texTarget, GLES30.GL_TEXTURE_MAG_FILTER, magFilter);
	return tex[0];
}
 
Example 4
Source File: GLTexture.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
	 * このインスタンスで管理しているテクスチャを有効にする(バインドする)
	 */
	@Override
	public void makeCurrent() {
//		if (DEBUG) Log.v(TAG, "makeCurrent:");
		GLES30.glActiveTexture(mTextureUnit);	// テクスチャユニットを選択
		GLES30.glBindTexture(mTextureTarget, mTextureId);
		setViewPort(viewPortX, viewPortY, viewPortWidth, viewPortHeight);
	}
 
Example 5
Source File: GLTexture.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
	 * このインスタンスで管理しているテクスチャを無効にする(アンバインドする)
	 */
	@Override
	public void swap() {
//		if (DEBUG) Log.v(TAG, "swap:");
		GLES30.glActiveTexture(mTextureUnit);	// テクスチャユニットを選択
		GLES30.glBindTexture(mTextureTarget, 0);
	}
 
Example 6
Source File: GLSurface.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ISurfaceの実装
 * オフスクリーン描画用のレンダリングバッファに切り替える
 * Viewportも変更になるので必要であればunbind後にViewportの設定をすること
 */
@Override
public void makeCurrent() {
	if (DEBUG) Log.v(TAG, "makeCurrent:");
	GLES30.glActiveTexture(TEX_UNIT);
	GLES30.glBindTexture(TEX_TARGET, mFBOTexId);
	GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFrameBufferObj);
	setViewPort(viewPortX, viewPortY, viewPortWidth, viewPortHeight);
}
 
Example 7
Source File: GLSurface.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ISurfaceの実装
 */
@Override
public void swap() {
	if (DEBUG) Log.v(TAG, "swap:");
	GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
	GLES30.glActiveTexture(TEX_UNIT);
	GLES30.glBindTexture(TEX_TARGET, 0);
}
 
Example 8
Source File: OverlayRendererHolder.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@WorkerThread
private void handleUpdateOverlay(final int targetId, @NonNull final Bitmap overlay) {
	if (DEBUG) Log.v(TAG, "handleUpdateOverlay:" + overlay);

	if (isGLES3()) {
		GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
		GLES30.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mOverlayTexId);
	} else {
		GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
		GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mOverlayTexId);
	}
	try {
		final Canvas canvas = mOverlaySurface.lockCanvas(null);
		try {
			if (overlay != null) {
				canvas.drawBitmap(overlay, 0, 0, null);
			} else if (DEBUG) {
				// DEBUGフラグtrueでオーバーレイ映像が設定されていないときは全面を薄赤色にする
				canvas.drawColor(0x7fff0000);	// ARGB
			} else {
				// DEBUGフラグfalseでオーバーレイ映像が設定されていなければ全面透過
				canvas.drawColor(0x00000000);	// ARGB
			}
		} finally {
			mOverlaySurface.unlockCanvasAndPost(canvas);
		}
	} catch (final Exception e) {
		Log.w(TAG, e);
	}
	requestFrame();
}
 
Example 9
Source File: MixRendererHolder.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * マスク用のBitmapをセットする
 * Bitmapがnullの時はα=1で全面を塗りつぶす(見えるように赤)
 * @param mask
 */
protected void handleSetMask(@Nullable final Bitmap mask) {
	if (DEBUG) Log.v(TAG, "handleSetMask:" + mask);
	if (isGLES3()) {
		GLES30.glActiveTexture(GLES30.GL_TEXTURE2);
		GLES30.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mMaskTexId);
	} else {
		GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
		GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mMaskTexId);
	}
	try {
		final Canvas canvas = mMaskSurface.lockCanvas(null);
		try {
			if (mask != null) {
				canvas.drawBitmap(mask, 0, 0, null);
			} else if (DEBUG) {
				// DEBUGフラグtrueでオーバーレイ映像が設定されていないときは全面を薄赤色にする
				canvas.drawColor(0x7fff0000);	// ARGB
			} else {
				// DEBUGフラグfalseでオーバーレイ映像が設定されていなければ全面透過
				canvas.drawColor(0xff000000);	// ARGB
			}
		} finally {
			mMaskSurface.unlockCanvasAndPost(canvas);
		}
	} catch (final Exception e) {
		Log.w(TAG, e);
	}
	requestFrame();
	if (DEBUG) Log.v(TAG, "handleSetMask:finished");
}
 
Example 10
Source File: GLES30WallpaperRenderer.java    From alynx-live-wallpaper with Apache License 2.0 4 votes vote down vote up
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
    // No depth test for 2D video.
    GLES30.glDisable(GLES30.GL_DEPTH_TEST);
    GLES30.glDepthMask(false);
    GLES30.glDisable(GLES30.GL_CULL_FACE);
    GLES30.glDisable(GLES30.GL_BLEND);

    GLES30.glGenTextures(textures.length, textures, 0);
    GLES30.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textures[0]);
    GLES30.glTexParameteri(
        GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
        GLES30.GL_TEXTURE_MIN_FILTER,
        GLES30.GL_LINEAR
    );
    GLES30.glTexParameteri(
        GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
        GLES30.GL_TEXTURE_MAG_FILTER,
        GLES30.GL_LINEAR
    );
    GLES30.glTexParameteri(
        GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
        GLES30.GL_TEXTURE_WRAP_S,
        GLES30.GL_CLAMP_TO_EDGE
    );
    GLES30.glTexParameteri(
        GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
        GLES30.GL_TEXTURE_WRAP_T,
        GLES30.GL_CLAMP_TO_EDGE
    );

    program = Utils.linkProgramGLES30(
        Utils.compileShaderResourceGLES30(
            context, GLES30.GL_VERTEX_SHADER, R.raw.vertex_30
        ),
        Utils.compileShaderResourceGLES30(
            context, GLES30.GL_FRAGMENT_SHADER, R.raw.fragment_30
        )
    );
    mvpLocation = GLES30.glGetUniformLocation(program, "mvp");

    GLES30.glGenBuffers(buffers.length, buffers, 0);

    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[0]);
    GLES30.glBufferData(
        GLES30.GL_ARRAY_BUFFER, vertices.capacity() * BYTES_PER_FLOAT,
        vertices, GLES30.GL_STATIC_DRAW
    );
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);

    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[1]);
    GLES30.glBufferData(
        GLES30.GL_ARRAY_BUFFER, texCoords.capacity() * BYTES_PER_FLOAT,
        texCoords, GLES30.GL_STATIC_DRAW
    );
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);

    GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
    GLES30.glBufferData(
        GLES30.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * BYTES_PER_INT,
        indices, GLES30.GL_STATIC_DRAW
    );
    GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, 0);

    // Locations are set in shader sources.
    GLES30.glGenVertexArrays(vertexArrays.length, vertexArrays, 0);

    GLES30.glBindVertexArray(vertexArrays[0]);
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[0]);
    GLES30.glEnableVertexAttribArray(0);
    GLES30.glVertexAttribPointer(
        0, 2, GLES30.GL_FLOAT, false, 2 * BYTES_PER_FLOAT, 0
    );
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[1]);
    GLES30.glEnableVertexAttribArray(1);
    GLES30.glVertexAttribPointer(
        1, 2, GLES30.GL_FLOAT, false, 2 * BYTES_PER_FLOAT, 0
    );
    GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
    GLES30.glBindVertexArray(0);

    GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
 
Example 11
Source File: GLHelper.java    From libcommon with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
public static int loadTextureFromResource(final Context context, final int resId, final Resources.Theme theme) {
	if (DEBUG) Log.v(TAG, "loadTextureFromResource:");
	// Create an empty, mutable bitmap
	final Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
	// get a canvas to paint over the bitmap
	final Canvas canvas = new Canvas(bitmap);
	canvas.drawARGB(0,0,255,0);

	// get a background image from resources
	// note the image format must match the bitmap format
	final Drawable background;
	if (BuildCheck.isAndroid5()) {
		background = context.getResources().getDrawable(resId, theme);
	} else {
		background = context.getResources().getDrawable(resId);
	}
	background.setBounds(0, 0, 256, 256);
	background.draw(canvas); // draw the background to our bitmap

	final int[] textures = new int[1];

	//Generate one texture pointer...
	GLES30.glGenTextures(1, textures, 0);
	//...and makeCurrent it to our array
	GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textures[0]);

	//Create Nearest Filtered Texture
	GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,
		GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
	GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,
		GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);

	//Different possible texture parameters, e.g. GLES30.GL_CLAMP_TO_EDGE
	GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,
		GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
	GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,
		GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);

	//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
	GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, bitmap, 0);
	//Clean up
	bitmap.recycle();

	return textures[0];
}
 
Example 12
Source File: GLDrawer2DES3.java    From libcommon with Apache License 2.0 4 votes vote down vote up
@Override
protected void bindTexture(final int texId) {
	GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
	GLES30.glBindTexture(mTexTarget, texId);
}
 
Example 13
Source File: GLDrawer2DES3.java    From libcommon with Apache License 2.0 4 votes vote down vote up
@Override
protected void finishDraw() {
	GLES30.glBindTexture(mTexTarget, 0);
	GLES30.glUseProgram(0);
}
 
Example 14
Source File: MixRendererHolder.java    From libcommon with Apache License 2.0 4 votes vote down vote up
/**
 * internalOnStartの下請け、GLES3用
 */
@SuppressLint("NewApi")
@WorkerThread
private void internalOnStartES3() {
	if (DEBUG) Log.v(TAG, String.format("internalOnStartES3:init mix texture(%dx%d)",
		width(), height()));
	mDrawer.updateShader(MY_FRAGMENT_SHADER_EXT_ES3);
	final int uTex1 = mDrawer.glGetUniformLocation("sTexture");
	GLES30.glUniform1i(uTex1, 0);

	// アルファブレンド用テクスチャ/SurfaceTexture/Surfaceを生成
	final int uTex2 = mDrawer.glGetUniformLocation("sTexture2");
	mTexId2 = GLHelper.initTex(
		GL_TEXTURE_EXTERNAL_OES, GLES30.GL_TEXTURE1,
		GLES30.GL_LINEAR, GLES30.GL_LINEAR, GLES30.GL_CLAMP_TO_EDGE);
	mMasterTexture2 = new SurfaceTexture(mTexId2);
	mMasterTexture2.setDefaultBufferSize(width(), height());
	mMasterSurface2 = new Surface(mMasterTexture2);
	if (BuildCheck.isAndroid5()) {
		mMasterTexture2.setOnFrameAvailableListener(
			mOnFrameAvailableListener, mAsyncHandler);
	} else {
		mMasterTexture2.setOnFrameAvailableListener(
			mOnFrameAvailableListener);
	}
	GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
	GLES30.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexId2);
	GLES30.glUniform1i(uTex2, 1);

	// マスク用テクスチャ/SurfaceTexture/Surfaceを生成
	final int uTex3 = mDrawer.glGetUniformLocation("sTexture3");
	mMaskTexId = GLHelper.initTex(
		GL_TEXTURE_EXTERNAL_OES, GLES30.GL_TEXTURE2,
		GLES30.GL_LINEAR, GLES30.GL_LINEAR, GLES30.GL_CLAMP_TO_EDGE);
	mMaskTexture = new SurfaceTexture(mMaskTexId);
	mMaskTexture.setDefaultBufferSize(width(), height());
	mMaskSurface = new Surface(mMaskTexture);
	GLES30.glActiveTexture(GLES30.GL_TEXTURE2);
	GLES30.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mMaskTexId);
	GLES30.glUniform1i(uTex3, 2);
}