2011-06-29 85 views
2

我是新来openGLES(和OpenGL的太),我有一个问题...openGLES顶点指针问题

我有一个struct条:

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; 
}; 

,我想也呈现GL_TRIANGLE_STRIP 。这里是代码:

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

int diff = offsetof(Vertex, pos); //diff = 0 
glVertexPointer(3, GL_FLOAT, size, (void*)(stripOffset + diff)); 

它显示了一些奇怪的事情后呈现与glDrawArrays(GL_TRIANGLE_STRIP, 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)); 

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

回答

2

_strip.vertices是一个指针。我假设它是动态分配的。所以_strip.vertices中的数据不仅存储在_strip的开头,而且在某个不同的地方,_strip.vertices只是指向那里。因此,只要使用

long stripOffset = (long) strip_.vertices; 

,而不是

long stripOffset = (long) &strip_; 
+0

非常感谢!感觉自己很愚蠢( – Andrew