是否可以在 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);
在 Android 上用 Java 可以做同样的事情吗?这是我目前得到的:
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);
最佳答案
基本思想是在 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);
关于java - 在 Android 上的 OpenGL ES 中交错的每个顶点数据的偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3794644/