2013-07-22 50 views
-1

看来,当我想融入OpenGL的颜色都是黑色的。无论是每个顶点颜色还是全局glColor4f()。Android OpenGL黑色与混合

整个绘图方法是这样的:

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混合,但任何混合导致此错误。 我知道这是我的代码的问题,但我想知道那是什么。

在此先感谢!

昨天所有人都看到了这个问题抱歉。但我用它搞砸了一切。我只是想开始干净。

回答

0

不是一个解决方案,而只是一个预感。当我在平板电脑上玩一些游戏时,会将某些图形(如烟雾)渲染为黑色框,因为某些纹理存在问题。

也许你的代码很好,只是OpenGL不能正确渲染图形?

+0

我认为不应该有PNG文件的问题。或者我可以编码位图以某种方式使其工作?而且这个问题也没有纹理存在。 –

1

对不起,偷你的时间。我找到了解决方案。我可以说我很愚蠢,我没有检查它。

我使用混合状态来确定混合功能,而不是OpenGL函数。静态值没有被初始化,我在那个类中使用了一个静态函数。

openGL没有问题。