我使用了 GL10 SurfaceView 并在其上绘制了正方形。但我希望方块是半透明的,以查看背景。我用了 //设置颜色
gl.glColor4f(color[0], color[1], color[2], color[3]/*alpha*/);
但是降低alpha会导致方形更暗,而不是半透明。
我还应用了混合选项
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glAlphaFunc(GL10.GL_GREATER, 0);
gl.glEnable(GL10.GL_ALPHA_TEST);
但没有任何作用。
编辑/添加
this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
// this.getHolder().setFormat( PixelFormat.RGBA_8888 );
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
mRenderer = new MyGLRenderer();
mRenderer.context = context;
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
float squareCoords[] = { x, 0f, lenght, // top left
x, 0f, 0.0f, // bottom left
x - 6, 0f, 0.0f, // bottom right
x - 6, 0f, lenght }; // top right
ByteBuffer bb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (# of coordinate values * 2 bytes per short)
drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
public void draw(GL10 gl) {
//color = colorP;
// Since this shape uses vertex arrays, enable them
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// draw the shape
gl.glColor4f( // set color
color[0], color[1], color[2], color[3]);
// gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
gl.glVertexPointer( // point to vertex data:
COORDS_PER_VERTEX, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(
// draw shape:
GL10.GL_TRIANGLES, drawOrder.length, GL10.GL_UNSIGNED_SHORT,
drawListBuffer);
// Disable vertex array drawing to avoid
// conflicts with shapes that don't use it
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Set GL_MODELVIEW transformation mod
gl.glMatrixMode(GL10.GL_MODELVIEW);
//
gl.glLoadIdentity(); // reset the matrix to its default state
gl.glEnable(GL10.GL_NORMALIZE);
// draw plane
for (SimplePlane plane : simplePlanes) {
plane.draw(gl);
}
onSurfaceChanged(gl,height,width)中的gl.glViewport(0, 0, width, height);
// make adjustments for screen ratio
gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode
gl.glLoadIdentity(); // reset the matrix to its default state
GLU.gluPerspective(gl, 75, ratio, 3, 10000);
// if (ratio >= 1.0f)
// gl.glFrustumf(0, 0, 0, 0, 0, -100.0f); //Landscape
// else
// gl.glFrustumf(-1, 1, -1 / ratio, 1 / ratio, 1, 300); //Portrait
// gl.glFrustumf(-ratio, ratio, -1, 1, 0.1f, 1000); // apply the
// projection matrix
gl.glDisable(GL10.GL_DITHER);
// gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0, 0, 0, 0);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
// gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glAlphaFunc(GL10.GL_GREATER, 0);
gl.glEnable(GL10.GL_ALPHA_TEST);