是否可以在Android上的OpenGL ES中使用每个顶点数据交错?
我无法为normal
和color
成员获取正确的偏移指针。
在C ++中我会做这样的事情:
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);
在Java上用Android可以做同样的事情吗?这就是我现在所拥有的:
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);
答案 0 :(得分:6)
基本思路是在ByteBuffer上调用duplicate(),它会创建一个新的ByteBuffer,它共享底层存储,但允许你从不同的位置开始。
这对我有用:
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);