Skip to content

Instantly share code, notes, and snippets.

@yorung
Created February 27, 2015 16:05
Show Gist options
  • Save yorung/73b911a6423776bfe0e5 to your computer and use it in GitHub Desktop.
Save yorung/73b911a6423776bfe0e5 to your computer and use it in GitHub Desktop.
create OpenGL texture in Java
class Helper {
...
public static int loadTexture(String s){
Bitmap img;
try {
img = BitmapFactory.decodeStream(context.getAssets().open(s));
} catch (IOException e) {
return -1;
}
int tex[] = new int[1];
GLES20.glGenTextures(1, tex, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, img, 0);
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR);
GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
img.recycle();
return tex[0];
}
static GLuint LoadTextureViaOS(const char* name)
{
jclass myview = jniEnv->FindClass(boundJavaClass);
jmethodID method = method = jniEnv->GetStaticMethodID(myview, "loadTexture", "(Ljava/lang/String;)I");
if (method == 0) {
return 0;
}
return jniEnv->CallStaticIntMethod(myview, method, jniEnv->NewStringUTF(name));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment