我正在尝试将图像作为纹理加载到GLSurfaceView上。我只能看到盐和胡椒的噪音,并在图像中显示出来。something like this。图像在原始文件夹中存储为32X32 .bmp。着色器成功编译。文件已正确读取。无法得到我做错的事。我正在研究OpenGLES2.0。 FloatBuffers在此onDraw方法之前初始化。
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);
答案 0 :(得分:1)
您可以尝试更改:
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);
为:
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);
Bitmap
类将其像素存储为int - 1个字节,用于每个红色,绿色,蓝色和alpha。所以我认为textureBitmap的外部格式是RGBA。来自http://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml:
internalformat必须与格式匹配。纹理图像处理期间不支持格式之间的转换。 type可以用作提示来指定所需的精度,但GL实现可以选择以任何内部分辨率存储纹理数组。
因此,如果您想使用RGB,您必须转换它或不使用BitmapFactory和Bitmap类来加载您的图像。
这可能是出了什么问题。另外,如果那是你在那里展示的抽奖代码,我希望你不要考虑每次抽奖都不加载你的形象 - 而只是一次。
希望这有帮助!