我如何使.put(float f)采用Float []?

时间:2013-06-08 20:09:29

标签: java android opengl-es floatbuffer

elcipes告诉我我的vertBuff.put(顶点); ---类型FloatBuffer中的此方法.put(float f)不适用于参数(Float [])。我该如何解决这个错误? 下面是具有vertBuff.put()

的代码
      `public class Object {

private float rgbaVals[] = { 1, 1, 0, .5f, .25f, 0, .85f, 1, 0, 1, 1, 1 };

private FloatBuffer colorBuff;

private FloatBuffer vertBuff;

public short[] pIndex = { 0, 1, 2 };// this is the array for the drawing
                                    // order

private ShortBuffer pBuff;

    public void Line(Float vertex, Short Index){
        Float vertices = vertex;
        Short pIndex = Index;   
    }
public Object(Float[] vertices) {

    ByteBuffer bBuff = ByteBuffer.allocateDirect(Float.SIZE * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);        <--------------this is where the problem is at.        
    vertBuff.position(0);

    ByteBuffer pbBuff = ByteBuffer.allocateDirect(Short.SIZE * 2);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);

    ByteBuffer cBuff = ByteBuffer.allocateDirect(rgbaVals.length * 4);
    cBuff.order(ByteOrder.nativeOrder());
    colorBuff = cBuff.asFloatBuffer();
    colorBuff.put(rgbaVals);
    colorBuff.position(0);
}

public void draw(GL10 gl) {
    // TODO Auto-generated method stub
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length,
            GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

}`

继承MainActivity类

public class MainActivity extends Activity implements OnClickListener {
public ArrayList<Short> indexP = new ArrayList<Short>();
public ArrayList<Float> linep = new ArrayList<Float>();
public Float coords = (float) 0;
public short p = 0;
public int l = 0;
GLSurfaceView ourSurface;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cad);
    ourSurface = new GLSurfaceView(this);
    FrameLayout v = (FrameLayout) findViewById(R.id.display);
    v.addView(ourSurface);
    ourSurface.setRenderer(new GLRenderer());

    Button line = (Button) findViewById(R.id.line);
    final Button enter = (Button) findViewById(R.id.enter);
    EditText cl = (EditText) findViewById(R.id.cl);
    final String value = cl.getText().toString();

    try {
        coords = Float.parseFloat(value);
    } catch (NumberFormatException e) {
    }
    ;

    line.setOnClickListener(this);
    enter.setOnClickListener(this);

}

public void onClick(View v) {
    Short Index[];
    Float vertex[];
    TextView info = (TextView) findViewById(R.id.info);
    switch (v.getId()) {
    case R.id.line:
        info.setText("Input X");
    case R.id.enter:
        switch (l) {
        case (0):
            linep.add(coords);
            l++;
            info.setText("Input Y");
            break;
        case (1):
            linep.add(coords);
            indexP.add(p);
            l = 0;
            p++;
            vertex = linep.toArray(new Float[linep.size()]);
            Index = indexP.toArray(new Short[indexP.size()]);
            break;
        }
    }

}

public void Setvertex() {

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

}

1 个答案:

答案 0 :(得分:1)

Floatfloat不同;前者是autoboxing type - 它是一个包装原语的Object,因此它可以作为Object传递并用作Generic类型。

将数组声明/传递为Float[] vertices表示您有一组Float个对象。您不能将它用作float[],它是原始float的数组 - 自动装箱不会扩展到数组对象。

FloatBuffer有一个方法put(float[])。您需要为float[]使用Float[]而不是vertices