我正在尝试将图像作为纹理加载到 GLSurfaceView 上。我只能看到椒盐噪声和图像。像这样的东西。图像在 raw 文件夹中存储为 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);