Rick C. Hodgin
Rick C. Hodgin

Reputation: 89

FloatBuffer in Android's OpenGL

I realize this is so basic a question, but I am at a loss because I'm new to Java, new to Android development, but have a strong C/C++ background, with working code in OpenGL that oes what I'm trying here.

I've taken the basic Cube.java and CubeRenderer.java code samples and modified them to include data for an icosahedron. I want to use float instead of fixed, but I don't know if it's possible or how to do it.

Can somebody show me what I'm doing wrong? It compiles and launches without error, but only shows blackness. The other drawing algorithms in Cube() are the same. I've tried reducing the size of the pon (positive 1.0), mon (minus 1.0), pgr (positive golden ratio), mgr (minus golden ratio), to half their current value. Still shows no triangles rendered.

I'm truly at a loss and would appreciate any help.

Thank you in advance. :-)

class Icosahedron
{
    public Icosahedron()
    {
        int one = 0x10000;
        float zro =  0.0f;
        float pon =  1.0f;
        float mon = -1.0f;
        float pgr =  1.618033989f;
        float mgr = -1.618033989f;
        float vertices[] = {
            pgr, pon, zro,
            pgr, mon, zro,
            pon, zro, pgr,
            pon, zro, mgr,
            zro, pgr, pon,
            zro, pgr, mon,
            zro, mgr, pon,
            zro, mgr, mon,
            mon, zro, pgr,
            mon, zro, mgr,
            mgr, pon, zro,
            mgr, mon, zro
        };

        int colors[] = {
              0,    0,    0,  one,
            one,    0,    0,  one,
            one,  one,    0,  one,
              0,  one,    0,  one,
              0,    0,  one,  one,
            one,    0,  one,  one,
            one,  one,  one,  one,
              0,  one,  one,  one,
              0,    0,  one,  one,
            one,    0,  one,  one,
            one,  one,  one,  one,
              0,  one,  one,  one,
              0,    0,  one,  one
        };

        byte indices[] = {
             0,  5,  4,
             0,  4,  2,
             0,  2,  1,
             0,  1,  3,
             0,  3,  5,
             1,  6,  7,
             1,  7,  3,
             1,  2,  6,
             2,  8,  6,
             2,  4,  8,
             3,  7,  9,
             3,  9,  5,
             4, 10,  8,
             4,  5, 10,
             5,  9, 10,
             6, 11,  7,
             6,  8, 11,
             7, 11,  9,
             8, 10, 11,
             9, 11, 10
        };

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
        cbb.order(ByteOrder.nativeOrder());
        mColorBuffer = cbb.asIntBuffer();
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);
    }

    public void draw(GL10 gl)
    {
        gl.glFrontFace(GL10.GL_CW);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
        gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLES, 20, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
    }

    private FloatBuffer   mVertexBuffer;
    private IntBuffer     mColorBuffer;
    private ByteBuffer    mIndexBuffer;
}

Upvotes: 0

Views: 2295

Answers (1)

svdree
svdree

Reputation: 13348

The only error I see is that you need to pass 60 as the second parameter to glDrawElements(), instead of 20. You need to specify the number of indices, not the number of triangles.

Upvotes: 1

Related Questions