Text formatting

Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
camera2
Leo Ma 9 years ago
parent 537074d980
commit 6476e13a72

@ -18,17 +18,17 @@ import android.opengl.GLUtils;
import android.util.Log; import android.util.Log;
public class OpenGlUtils { public class OpenGlUtils {
public static final int NO_TEXTURE = -1; public static final int NO_TEXTURE = -1;
public static final int NOT_INIT = -1; public static final int NOT_INIT = -1;
public static final int ON_DRAWN = 1; public static final int ON_DRAWN = 1;
public static int loadTexture(final Bitmap img, final int usedTexId) { public static int loadTexture(final Bitmap img, final int usedTexId) {
return loadTexture(img, usedTexId, false); return loadTexture(img, usedTexId, false);
} }
public static int loadTexture(final Bitmap img, final int usedTexId, boolean recyled) { public static int loadTexture(final Bitmap img, final int usedTexId, boolean recyled) {
if(img == null) if(img == null)
return NO_TEXTURE; return NO_TEXTURE;
int textures[] = new int[1]; int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) { if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0); GLES20.glGenTextures(1, textures, 0);
@ -49,108 +49,108 @@ public class OpenGlUtils {
textures[0] = usedTexId; textures[0] = usedTexId;
} }
if(recyled) if(recyled)
img.recycle(); img.recycle();
return textures[0]; return textures[0];
} }
public static int loadTexture(final Buffer data, final int width,final int height, final int usedTexId) { public static int loadTexture(final Buffer data, final int width,final int height, final int usedTexId) {
if(data == null) if(data == null)
return NO_TEXTURE; return NO_TEXTURE;
int textures[] = new int[1]; int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) { if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0); GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height,
0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data); 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data);
} else { } else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width, GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data); height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data);
textures[0] = usedTexId; textures[0] = usedTexId;
} }
return textures[0]; return textures[0];
} }
public static int loadTexture(final Buffer data, final int width,final int height, final int usedTexId,final int type) { public static int loadTexture(final Buffer data, final int width,final int height, final int usedTexId,final int type) {
if(data == null) if(data == null)
return NO_TEXTURE; return NO_TEXTURE;
int textures[] = new int[1]; int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) { if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0); GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height,
0, GLES20.GL_RGBA, type, data); 0, GLES20.GL_RGBA, type, data);
} else { } else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width, GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_RGBA, type, data); height, GLES20.GL_RGBA, type, data);
textures[0] = usedTexId; textures[0] = usedTexId;
} }
return textures[0]; return textures[0];
} }
public static int loadTexture(final Context context, final String name){ public static int loadTexture(final Context context, final String name){
final int[] textureHandle = new int[1]; final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0); GLES20.glGenTextures(1, textureHandle, 0);
if (textureHandle[0] != 0){ if (textureHandle[0] != 0){
// Read in the resource // Read in the resource
final Bitmap bitmap = getImageFromAssetsFile(context,name); final Bitmap bitmap = getImageFromAssetsFile(context,name);
// Bind to the texture in OpenGL // Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering // Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_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_MIN_FILTER, GLES20.GL_LINEAR); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_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_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_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
// Load the bitmap into the bound texture. // Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL. // Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle(); bitmap.recycle();
} }
if (textureHandle[0] == 0){ if (textureHandle[0] == 0){
throw new RuntimeException("Error loading texture."); throw new RuntimeException("Error loading texture.");
} }
return textureHandle[0]; return textureHandle[0];
} }
private static Bitmap getImageFromAssetsFile(Context context,String fileName){ private static Bitmap getImageFromAssetsFile(Context context,String fileName){
Bitmap image = null; Bitmap image = null;
AssetManager am = context.getResources().getAssets(); AssetManager am = context.getResources().getAssets();
try{ try{
InputStream is = am.open(fileName); InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is); image = BitmapFactory.decodeStream(is);
is.close(); is.close();
}catch (IOException e){ }catch (IOException e){
e.printStackTrace(); e.printStackTrace();
} }
return image; return image;
} }
public static int loadProgram(final String strVSource, final String strFSource) { public static int loadProgram(final String strVSource, final String strFSource) {
int iVShader; int iVShader;
int iFShader; int iFShader;
int iProgId; int iProgId;
@ -179,8 +179,8 @@ public class OpenGlUtils {
GLES20.glDeleteShader(iFShader); GLES20.glDeleteShader(iFShader);
return iProgId; return iProgId;
} }
private static int loadShader(final String strSource, final int iType) { private static int loadShader(final String strSource, final int iType) {
int[] compiled = new int[1]; int[] compiled = new int[1];
int iShader = GLES20.glCreateShader(iType); int iShader = GLES20.glCreateShader(iType);
GLES20.glShaderSource(iShader, strSource); GLES20.glShaderSource(iShader, strSource);
@ -192,39 +192,39 @@ public class OpenGlUtils {
} }
return iShader; return iShader;
} }
public static int getExternalOESTextureID(){ public static int getExternalOESTextureID(){
int[] texture = new int[1]; int[] texture = new int[1];
GLES20.glGenTextures(1, texture, 0); GLES20.glGenTextures(1, texture, 0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
return texture[0]; return texture[0];
} }
public static String readShaderFromRawResource(final int resourceId){ public static String readShaderFromRawResource(final int resourceId){
final InputStream inputStream = MagicFilterFactory.getCurrentContext().getResources().openRawResource(resourceId); final InputStream inputStream = MagicFilterFactory.getCurrentContext().getResources().openRawResource(resourceId);
final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String nextLine; String nextLine;
final StringBuilder body = new StringBuilder(); final StringBuilder body = new StringBuilder();
try{ try{
while ((nextLine = bufferedReader.readLine()) != null){ while ((nextLine = bufferedReader.readLine()) != null){
body.append(nextLine); body.append(nextLine);
body.append('\n'); body.append('\n');
} }
} }
catch (IOException e){ catch (IOException e){
return null; return null;
} }
return body.toString(); return body.toString();
} }
} }

Loading…
Cancel
Save