0

当我应用颜色缓冲区并在实心圆上混合时,前 20 度的颜色无法正确显示,我得到了某种颜色的色带,但这不是它应该的样子,也许我的代码做错了?

public class Circle {
boolean circleChecked;
 private int points=361;
    private float vertices[]={0.0f,0.0f,0.0f};
    private float[] fogcolor = {0.2f,0.4f,0.7f,0.9f};
    private FloatBuffer vertBuff, textureBuffer;
    private FloatBuffer colorBuffer;   // Buffer for color-array (NEW)
    float texData[] = null;
    private float[] colors = { // Colors for the vertices (NEW)
              0.7f,0.7f,0.7f,0.5f,
              0.7f,0.7f,0.7f,0.5f,
              0.7f,0.7f,0.7f,0.5f
           };

    float theta = 0;
    int[] textures = new int[1];
    int R=1;
    float textCoordArray[] = 
        {
            -R, 
            (float) (R * (Math.sqrt(2) + 1)),
             -R, 
             -R,
            (float) (R * (Math.sqrt(2) + 1)), 
            -R
        };
    public Circle(float size, float positionX, float positionY){

        vertices = new float[(points)*3];
        for(int i=0;i<3;i+=3){
            vertices[i]=positionX * size;
            vertices[i+1]=positionY *size;
            vertices[i+2]=0.51f;
        }
        for(int i=3;i<(points)*3;i+=3)
        {       

                vertices[i]=((float) ( Math.cos(theta))/3+positionX) * size;
                vertices[i+1]=((float) (Math.sin(theta))/3+positionY) *size;
                vertices[i+2]=0.5f;
                theta += Math.PI / 90;

        }
        ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);    
          bBuff.order(ByteOrder.nativeOrder());
          vertBuff=bBuff.asFloatBuffer();
          vertBuff.put(vertices);
          vertBuff.position(0);

          // Setup color-array buffer. Colors in float. A float has 4 bytes (NEW)
          ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
          cbb.order(ByteOrder.nativeOrder()); // Use native byte order (NEW)
          colorBuffer = cbb.asFloatBuffer();  // Convert byte buffer to float (NEW)
          colorBuffer.put(colors);            // Copy data into buffer (NEW)
          colorBuffer.position(0);            // Rewind (NEW)

        ByteBuffer bBuff2=ByteBuffer.allocateDirect(textCoordArray.length * 4 *       360);
        bBuff2.order(ByteOrder.nativeOrder());
        textureBuffer=bBuff2.asFloatBuffer();
        textureBuffer.put(textCoordArray);
        textureBuffer.position(0);
    }

    public void draw(GL10 gl){ 
    //gl.glDisable(GL10.GL_LIGHTING);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    //gl.glColor4f(0.8f, 0.8f, 0.8f, 1);

    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); 
   // if(circleChecked){
    //  gl.glColor4f(0.2f, 0.4f, 0.8f, 1);
    //}
    //gl.glEnable(GL10.GL_TEXTURE_2D);   
    gl.glEnable(GL10.GL_BLEND);
    gl.glPushMatrix(); 
    gl.glFogf(GL10.GL_FOG_MODE, GL10.GL_LINEAR);
    gl.glFogf(GL10.GL_FOG_START, 3.0f);
    gl.glFogf(GL10.GL_FOG_END, 5.0f);
    float fogColor[] = {1f, 0.0f, 0.5f, 1.0f};
    gl.glFogfv(GL10.GL_FOG_COLOR, fogColor, 0);
    gl.glFogf(GL10.GL_FOG_DENSITY, 0.9f);
    gl.glEnable(GL10.GL_FOG);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    //gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); //4
    //gl.glTexCoordPointer(2, GL10.GL_FLOAT,0, textureBuffer); //5
       // gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
      gl.glDisableClientState(GL10.GL_COLOR_ARRAY);   // Disable color-array (NEW)
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);   
    gl.glPopMatrix();
    //gl.glDisable(GL10.GL_FOG);
     }
}
4

1 回答 1

1

问题出在您的颜色数组中。glDrawArraysgl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);将从您启用其客户端状态“gl.glEnableClientState(GL10.GL_COLOR_ARRAY)”的每个缓冲区中获取值。这些值的数量等于最后一个参数,在您的情况下points/2,但您的颜色缓冲区只有 3 个值。结果是只有第一个三角形有正确的颜色映射,其余的都是垃圾,结果是不可预测的。

尽管这对您的情况来说似乎效率低下,但您需要在设置顶点坐标的“for”循环中重复这些颜色参数,并且缓冲区的长度应与“vertBuffer”相同。长度是指值的数量,而不是字节数,在您的情况下,1 个颜色值由 4 个浮点值组成,1 个位置值由 3 个浮点值组成。

于 2013-03-11T14:27:56.483 回答