我遇到了麻烦,为什么我加载的图像不能正常工作。我有一个渲染器类、一个模型类和一个片段类。在渲染类中,我生成模型,每个模型都有一个片段属性。在渲染类中
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// preparation
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
loadGLTexture(gl, context);
models=new HashMap<Identity,Model>();
for(int i=-9;i<10;i+=2){
for(int j=-8;j<10;j+=4){
models.put(new Identity(i,j), new Model(j,i,0f,w,h));
models.get(new Identity(i,j)).getPiece().setImageBuffer(imageBuffer);
models.get(new Identity(i,j)).getPiece().setTextures(textures);
}
}
// Load the texture for the square
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
public void loadGLTexture(GL10 gl, Context context) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.tree2);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// generate one texture pointer
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
imageBuffer = ByteBuffer.allocateDirect(bitmap.getHeight() * bitmap.getWidth() * 4);
imageBuffer.order(ByteOrder.nativeOrder());
byte buffer[] = new byte[4];
for(int i = 0; i < bitmap.getHeight(); i++)
{
for(int j = 0; j < bitmap.getWidth(); j++)
{
int color = bitmap.getPixel(j, i);
buffer[0] = (byte)Color.red(color);
buffer[1] = (byte)Color.green(color);
buffer[2] = (byte)Color.blue(color);
buffer[3] = (byte)Color.alpha(color);
imageBuffer.put(buffer);
}
}
imageBuffer.position(0);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap.getWidth(), bitmap.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, imageBuffer);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
}
绘图功能:
@Override
public void onDrawFrame(GL10 gl) {
// define the color we want to be displayed as the "clipping wall"
gl.glClearColor(_red, _green, _blue, 1.0f);
// reset the matrix - good to fix the rotation to a static angle
gl.glLoadIdentity();
// clear the color buffer to show the ClearColor we called above...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
for(Model model:models.values()){
model.draw(gl);
}
}
在模型课中,我画了彩色六边形;
public void draw(GL10 gl){
gl.glLoadIdentity();
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
gl.glColor4f(0.5f, 0.5f, 0.5f, 1f);
gl.glDrawElements(GL10.GL_TRIANGLE_FAN, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
if(piece!=null){
piece.draw(gl);
}
}
在单品类
public void draw(GL10 gl){
gl.glLoadIdentity();
// gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// bind the previously generated texture
gl.glActiveTexture(GL10.GL_TEXTURE0);
// gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 32, 32, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, imageBuffer);
// gl.glTexSubImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 32, 32, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, imageBuffer);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0,4);
}
我在构造函数中设置了 vertexBuffer 和 textureBuffer。我不知道女巫 gl 方法在哪里使用,在大多数教程中只使用一次图像。为什么我的 png 失去了透明性,为什么我看不到模型的六边形。
它开始对我来说很复杂:(