2011-06-29 89 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