似乎当我想融入 OpenGL 时,颜色都是黑色的。无论是每个顶点颜色还是全局 glColor4f()。
整个绘制方法是这样的:
public void Draw(Texture2D texture, Rectangle destination, Rectangle source, GLColor color, Vector2 origin, float Rotation)
{
//Enable Blending
GL.glEnable(GL10.GL_BLEND);
GL.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
//Generate Vertices
float[] vertices = {-origin.X, -origin.Y,
destination.Width - origin.X, -origin.Y,
destination.Width - origin.X, destination.Height - origin.Y,
-origin.X, destination.Height - origin.Y};
FloatBuffer vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.flip();
//Generate Indices
short[] indices = {0, 1, 2, 2, 3, 0};
ShortBuffer indexBuffer = ByteBuffer.allocateDirect(indices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
indexBuffer.put(indices);
indexBuffer.flip();
//Generate UV of Vertices
float minU = 0;
float maxU = 1;
float minV = 0;
float maxV = 1;
if (source != null)
{
minU = (float)source.X / (float)texture.getWidth();
maxU = (float)(source.X + source.Width) / (float)texture.getWidth();
minV = (float)source.Y / (float)texture.getHeight();
maxV = (float)(source.Y + source.Height) / (float)texture.getHeight();
}
float[] vertexUVs = {minU, minV,
maxU, minV,
maxU, maxV,
minU, maxV};
FloatBuffer uvBuffer = ByteBuffer.allocateDirect(vertexUVs.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
uvBuffer.put(vertexUVs);
uvBuffer.flip();
//Calculate Matrix
TransformationMatrix matrix = new TransformationMatrix();
matrix.Translate(destination.X + origin.X, destination.Y + origin.Y, 0);
matrix.Rotate(0, 0, Rotation);
//Bind Vertices
GL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
GL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//Bind Pointers
GL.glEnable(GL10.GL_TEXTURE_2D);
GL.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
GL.glTexCoordPointer(2, GL10.GL_FLOAT, 0, uvBuffer);
//Do Transformations
GL.glMatrixMode(GL10.GL_MODELVIEW);
GL.glLoadIdentity();
GL.glTranslatef(matrix.TranslationX, matrix.TranslationY, matrix.TranslationZ);
GL.glRotatef((float)Math.sqrt(matrix.RotationX * matrix.RotationX + matrix.RotationY*matrix.RotationY + matrix.RotationZ*matrix.RotationZ), matrix.RotationX, matrix.RotationY, matrix.RotationZ);
//Bind Texture
GL.glBindTexture(GL10.GL_TEXTURE_2D, texture.ID);
//Color
GL.glColor4f(color.R, color.G, color.B, color.A);
//Draw Elements
GL.glDrawElements(GL10.GL_TRIANGLES, vertices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
//Disable things
GL.glDisable(GL10.GL_TEXTURE_2D);
GL.glDisableClientState(GL10.GL_VERTEX_ARRAY);
GL.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
GL.glBindTexture(GL10.GL_TEXTURE_2D, 0);
GL.glDisable(GL10.GL_BLEND)
}
不仅是 alpha 混合,而且任何混合都会导致此错误。我知道这是我的代码的问题,但我想知道那是什么。
提前致谢!
对于昨天看到这个问题的所有人感到抱歉。但我把一切都搞砸了。我只是想开始干净。