Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offsets for per vertex data interleaved in OpenGL ES on Android

Is it possible to use per vertex data interleaved in OpenGL ES on Android?

I'm unable to get the correct offset pointers for the normal and color members.

In C++ I would do something like this:

struct ColoredVertexData3D{
    Vertex3D    vertex;
    Vector3D    normal;
    ColorRGBA   color;
};

const ColoredVertexData3D vertexData[] =
{
    {
        {0.0f, 0.5f, 0.8f},       // Vertex |
        {0.0f, 0.4f, 0.6f},       // Normal | Vertex 0
        {1.0f, 0.0f, 0.0f, 1.0f}  // Color  |
    },
    {
        {0.8f, 0.0f, 0.5f},       // Vertex |
        {0.6f, 0.0f, 0.4f},       // Normal | Vertex 1
        {1.0f, 0.5f, 0.0f, 1.0f}  // Color  |
    },
    // ... more vertexes.
};

const int stride = sizeof(ColoredVertexData3D);
glVertexPointer(3, GL_FLOAT, stride, &vertexData[0].vertex);
glColorPointer(4, GL_FLOAT, stride, &vertexData[0].color);
glNormalPointer(GL_FLOAT, stride, &vertexData[0].normal);

Is the same thing possible on Android in Java? This is what I currently got:

ByteBuffer vertexData = ...;
int stride = 40;

gl.glVertexPointer(3, GL10.GL_FLOAT, stride, vertexData);

// This obviously doesn't work. ------------v
gl.glColorPointer(4, GL10.GL_FLOAT, stride, &vertexData[0].color);
gl.glNormalPointer(GL10.GL_FLOAT, stride, &vertexData[0].normal);
like image 656
dalle Avatar asked Sep 25 '10 17:09

dalle


1 Answers

The basic idea is to call duplicate() on the ByteBuffer, which creates a new ByteBuffer that shares the underlying storage but allows you to start at a different position.

Here's what worked for me:

FloatBuffer verticesNormals;
// ... code to initialize verticesNormals ...
// verticesNormals contains 6 floats for each vertex. The first three
// define the position and the next three define the normal.

gl.glVertexPointer(3, gl.GL_FLOAT, 24, verticesNormals);

// Create a buffer that points 3 floats past the beginning.
FloatBuffer normalData = mVerticesNormals.duplicate();
normalData.position(3);

gl.glNormalPointer(gl.GL_FLOAT, 24, normalData);
like image 53
Evan Avatar answered Oct 17 '22 16:10

Evan