Search code examples
javaandroidopengl-esfloatbuffer

How can i make .put(float f ) take a Float[ ]?


elcipes is telling me that my vertBuff.put(vertices); --- This method .put(float f) in The Type FloatBuffer is not applicable for the arguments (Float[]). How do i fix this error? heres the code that has the 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);
}

}`

Heres the MainActivity class

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();
}

}


Solution

  • Float is not the same thing as float; the former is an autoboxing type - it's an Object that wraps a primitive so it can be passed as an Object and used as a Generic type.

    Declaring / passing in your array as Float[] vertices means you have an array of Float objects. You can't use it as a float[] which is an array of the primitive float - autoboxing does not extend to an array object.

    FloatBuffer has a method put(float[]). You need to be using float[] rather than Float[] for your vertices.