我是OpenGl的新手,我想要实现的是给出一个从1.0到0.0的alpha纹理
我一直在搜索并且只找到“如何在图像中加载alpha”但我无法找到如何将alpha应用于对象
我已经尝试过:
gl.glEnable(GL10.GL_ALPHA_TEST);
alpha += 0.002f;
if(alpha > 1) alpha = 1f;
gl.glAlphaFunc(GL10.GL_EQUAL, alpha);
但它不起作用,我怎样才能给对象/纹理赋予alpha值(使fade效果)?
这是我的班级
public class Palabra {
public float posX = 0f;
public float posY = 0f;
public float scaleX = 2f;
public float scaleY = 2f;
public float alpha= 0.5f;
public State estado;
private FloatBuffer vertexBuffer, texBuffer; // Buffer for vertex-array
private float[] vertices = { // Vertices for a face
0.0f, 0.0f, 0.2f, // 0. left-bottom-front
2.1f, 0.0f, 0.2f, // 1. right-bottom-front
0.0f, 0.35f, 0.2f, // 2. left-top-front
2.1f, 0.35f, 0.2f // 3. right-top-front
};
float[] texCoords = { // Texture coords for the above face
0.00f, 0.0f, 1.0f, // A. left-bottom
1.00f, 0.0f, 1.0f, // B. right-bottom
0.00f, 0.2f, 1.0f, // C. left-top
1.00f, 0.2f, 1.0f // D. right-top
};
public enum State {
MISS, NORMAL,GREAT,AWESOME,PERFECT;
}
public Palabra(int a) {
// Setup vertex-array buffer. Vertices in float. An float has 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // Use native byte order
vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
vertexBuffer.put(vertices); // Copy data into buffer
vertexBuffer.position(0); // Rewind
// Setup texture-coords-array buffer, in float. An float has 4 bytes
// (NEW)
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
texBuffer = tbb.asFloatBuffer();
texBuffer.put(texCoords);
texBuffer.position(0);
posX = (float) 0.00f;
switch(a)
{
case 0:
posY = (float) 1f;
break;
case 1:
posY = (float) 1.5f;
break;
case 2:
posY = (float) 2f;
break;
case 3:
posY = (float) 2.5f;
break;
case 4:
posY = (float) 3f;
break;
}
}
public void draw(GL10 gl)
{
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glTranslatef(posX, posY, 0f);
gl.glScalef(scaleX, scaleY, 0f);
scaleX -= 0.02f;
scaleY -= 0.02f;
posX += 0.02f;
if(scaleX < 1)
{
scaleX = 1f;
posX -= 0.02f;
}
if(scaleY < 1) scaleY = 1f;
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
switch(this.estado)
{
case AWESOME:
gl.glTranslatef(0.0f, 0.2f, 0f);
break;
case GREAT:
gl.glTranslatef(0.0f, 0.4f, 0f);
break;
case NORMAL:
gl.glTranslatef(0.0f, 0.6f, 0f);
break;
case MISS:
gl.glTranslatef(0.0f, 0.8f, 0f);
break;
case PERFECT:
break;
}
gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise
// orientation
gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Enable
// texture-coords-array
// (NEW)
gl.glTexCoordPointer(3, GL10.GL_FLOAT, 0, texBuffer); // Define
// texture-coords
// buffer (NEW)
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
// front
gl.glBindTexture(GL10.GL_TEXTURE_2D, TextureLoader.palabrasIDs[0]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Disable
// texture-coords-array
// (NEW)
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
gl.glDisable(GL10.GL_BLEND);
gl.glPopMatrix();
}
}
答案 0 :(得分:0)
我做到了!
这就是代码
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4f(1f, 1f, 1f,alpha);
alpha -= 0.002f;
if(alpha < 0) alpha = 0f;