Opengl es openGLES顶点指针问题

Opengl es openGLES顶点指针问题,opengl-es,Opengl Es,我对openGLES(也对openGL)是新手,我有一个问题 我有一个结构条: struct Vertex2F { GLfloat x; GLfloat y; }; struct Vertex3F { GLfloat x; GLfloat y; GLfloat z; }; struct Color4UB { GLubyte r; GLubyte g; GLubyte b; GLubyte a; }; struct Ve

我对openGLES(也对openGL)是新手,我有一个问题

我有一个结构条:

struct Vertex2F
{
    GLfloat x;
    GLfloat y;
};

struct Vertex3F
{
    GLfloat x;
    GLfloat y;
    GLfloat z;
};

struct Color4UB
{
    GLubyte r;
    GLubyte g;
    GLubyte b;
    GLubyte a;
};

struct Vertex
{
    Vertex3F pos;
    Color4UB color;
    Vertex2F tex;
};

struct Strip
{
    Strip() {vertices = 0; count = 0;}
    Strip(int cnt);
    ~Strip();
    void allocate(int cnt);
    void draw();
    Vertex *vertices;
    int count;
};
我想画三角带。代码如下:

const int size = sizeof(Vertex);
long stripOffset = (long) &strip_;

int diff = offsetof(Vertex, pos); //diff = 0
glVertexPointer(3, GL_FLOAT, size, (void*)(stripOffset + diff));
在使用
gldrawArray(GL_三角形_条带,0,4)进行渲染后,它会显示一些奇怪的东西如果有显示。但该代码的工作原理与预期一致:

GLfloat ar[4*3];
for (int i = 0; i < 4; ++i)
{
    ar[3*i + 0] = strip_.vertices[i].pos.x;
    ar[3*i + 1] = strip_.vertices[i].pos.y;
    ar[3*i + 2] = strip_.vertices[i].pos.z;
}
glVertexPointer(3, GL_FLOAT, 0, (void*)(ar));
GLfloat ar[4*3];
对于(int i=0;i<4;++i)
{
ar[3*i+0]=条带顶点[i].pos.x;
ar[3*i+1]=条带顶点[i].pos.y;
ar[3*i+2]=条带顶点[i].pos.z;
}
glvertexointer(3,GL_FLOAT,0,(void*)(ar));

请解释我在第一种情况下做错了什么?

\u strip。顶点是指针。我假设它是动态分配的。因此,
\u strip.vertices
中的数据不仅存储在
\u strip
的开头,而且存储在不同的位置,
\u strip.vertices
只是指向那里。所以只要使用

long stripOffset = (long) strip_.vertices;
而不是

long stripOffset = (long) &strip_;