Reputation: 67
I am trying to load the image as a texture on GLSurfaceView. I am just able to see salt and pepper noise and out the image.something like this. Image is stored as 32X32 .bmp inside the raw folder. Shader compiles successfully. Files is read correctly. Not able to get what i am doing wrong. I am working on OpenGLES2.0. FloatBuffers are initialised before this onDraw method.
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
aVerPos = GLES20.glGetAttribLocation(mProgram, "aVerPos");
if(aVerPos == -1) {
Log.e("Shader program", "Cudn't find aVerPos");
} else {
Log.v("Shader program", "Found vPosition @ " + aVerPos);
}
GLES20.glEnableVertexAttribArray(aVerPos);
aVerCol = GLES20.glGetAttribLocation(mProgram, "aVerCol");
if(aVerCol == -1) {
Log.e("Error", "Couldn't find aVColor");
} else {
Log.v("Success", "aVColor is at " + aVerCol + " :-3");
}
GLES20.glEnableVertexAttribArray(aVerCol);
aTexPos = GLES20.glGetAttribLocation(mProgram, "aTexPos");
if(aTexPos == -1) {
Log.e("Error", "Failed 2 find aTexPos");
} else {
Log.v("Succeed", "Succesfully located aTexPos @ " + aTexPos);
}
GLES20.glEnableVertexAttribArray(aTexPos);
uSamp = GLES20.glGetUniformLocation(mProgram, "uSampler");
if(uSamp == -1) {
Log.e("Error", "Couldn't finda uSampler " + uSamp);
} else {
Log.v("Succeed", "uSampler is @ " + uSamp + " :3");
}
//Texture Property of the Image
textureBitmap = BitmapFactory.decodeResource(mycontext.getResources(), R.drawable.ic_launcher);
File root = Environment.getExternalStorageDirectory();
File imgfile = new File(root.getAbsolutePath()+"/abc.bmp");
// textureBitmap = BitmapFactory.decodeFile(imgfile.getAbsolutePath());
Log.v("Bitmap inafo:", textureBitmap.getWidth() + ", " + textureBitmap.getHeight());
Log.i("File path",imgfile.getAbsolutePath());
int[] buffer = new int[1];
GLES20.glGenTextures(1, buffer, 0);
texture = buffer[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
Log.v("check inafo:", "1");
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLES20.GL_TRUE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);
Log.v("check inafo:", "2");
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
/* GLES20.glUseProgram(mProgram); */
Log.v("check inafo:", "3");
// The Draw part of Image
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
GLES20.glUniform1i(uSamp, 0);
GLES20.glVertexAttribPointer(aVerPos, 3, GLES20.GL_FLOAT, false, 0, Img.texVertexBufferpointer);
GLES20.glVertexAttribPointer(aVerCol, 3, GLES20.GL_FLOAT, false, 0, Img.texColBufferPointer);
GLES20.glVertexAttribPointer(aTexPos, 2, GLES20.GL_FLOAT, false, 0, Img.textureBufferPointer);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
Log.v("check inafo:", "4");
GLES20.glDisableVertexAttribArray(aVerPos);
GLES20.glDisableVertexAttribArray(aVerCol);
GLES20.glDisableVertexAttribArray(aTexPos);
Upvotes: 1
Views: 1291
Reputation: 735
You could try to change:
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);
to:
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);
Bitmap
class stores it's pixels as an int - 1 byte for each red, green, blue and alpha. So I think the external format of your textureBitmap is RGBA. And from http://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml:
internalformat must match format. No conversion between formats is supported during texture image processing. type may be used as a hint to specify how much precision is desired, but a GL implementation may choose to store the texture array at any internal resolution it chooses.
So if you want to use RGB you must either convert it or not use BitmapFactory and Bitmap class to load your image.
This might be what goes wrong. Also, if that is the draw code you're showing there I hope you'd consider not loading your image every draw - but rather only once.
Hope this helps!
Upvotes: 1