If you think the Android project Muzei listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2014 Google Inc./*fromwww.java2s.com*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.google.android.apps.muzei.render;
import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import com.google.android.apps.muzei.util.LogUtil;
import net.nurik.roman.muzei.BuildConfig;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
importstatic com.google.android.apps.muzei.util.LogUtil.LOGE;
publicclass GLUtil {
privatestaticfinal String TAG = LogUtil.makeLogTag(GLUtil.class);
publicstaticfinalint BYTES_PER_FLOAT = 4;
publicstaticint loadShader(int type, String shaderCode) {
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shaderHandle = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shaderHandle, shaderCode);
GLES20.glCompileShader(shaderHandle);
checkGlError("glCompileShader");
return shaderHandle;
}
publicstaticint createAndLinkProgram(int vertexShaderHandle, int fragShaderHandle,
String[] attributes) {
int programHandle = GLES20.glCreateProgram();
GLUtil.checkGlError("glCreateProgram");
GLES20.glAttachShader(programHandle, vertexShaderHandle);
GLES20.glAttachShader(programHandle, fragShaderHandle);
if (attributes != null) {
finalint size = attributes.length;
for (int i = 0; i < size; i++) {
GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
}
}
GLES20.glLinkProgram(programHandle);
GLUtil.checkGlError("glLinkProgram");
GLES20.glDeleteShader(vertexShaderHandle);
GLES20.glDeleteShader(fragShaderHandle);
return programHandle;
}
publicstaticint loadTexture(Bitmap bitmap) {
finalint[] textureHandle = newint[1];
GLES20.glGenTextures(1, textureHandle, 0);
GLUtil.checkGlError("glGenTextures");
if (textureHandle[0] != 0) {
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
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);
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);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLUtil.checkGlError("texImage2D");
}
if (textureHandle[0] == 0) {
LOGE(TAG, "Error loading texture (empty texture handle)");
if (BuildConfig.DEBUG) {
thrownew RuntimeException("Error loading texture (empty texture handle).");
}
}
return textureHandle[0];
}
publicstaticvoid checkGlError(String glOperation) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
LOGE(TAG, glOperation + ": glError " + error);
if (BuildConfig.DEBUG) {
thrownew RuntimeException(glOperation + ": glError " + error);
}
}
}
publicstatic FloatBuffer asFloatBuffer(float[] array) {
FloatBuffer buffer = newFloatBuffer(array.length);
buffer.put(array);
buffer.position(0);
return buffer;
}
publicstatic FloatBuffer newFloatBuffer(int size) {
FloatBuffer buffer = ByteBuffer.allocateDirect(size * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
buffer.position(0);
return buffer;
}
}