GL_LINES回退到顶点末尾的第一个点

时间:2012-12-26 23:05:55

标签: iphone ios opengl-es opengl-es-2.0 glkit

在iOS上使用GLKit(OpenGLES 2.0)。

我正在修改一些在3d环境中渲染立方体的代码,以便在3d场景中以2维渲染波形。我有一个很好的工作,除了一个问题是:

假设我使用GL_LINES渲染n x / y坐标。它将从0 - >绘制一条线。 1,然后1 - > 2,依此类推。但是然后点N将总是有一条线回到点0.我希望GL_LINE_LOOP到此,但不是GL_LINES。

如果我无法解决这个问题,我会考虑周围的工作,那就是使用2个GL_TRIANGLE_STRIP来绘制每个线段。我真的很想知道为什么这不起作用。

以下是代码的重要部分:

typedef struct {
    float Position[3];
} Vertex;

const NSUInteger kVerticiesSize = 320;
Vertex Vertices[kVersiciesSize] = {};
const GLubyte Indices[] = {
    0,1,
    1,2,
    2,3,
    3,4,
    4,5,
    5,6,
    6,7,
    7,8,
    8,9,
    9,10,
// keeps going to kVerticiesSize

GL设置

- (void)setupGL {
    [EAGLContext setCurrentContext:self.context];
    self.effect = [[GLKBaseEffect alloc] init];

    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition,
                          3,
                          GL_FLOAT,
                          GL_FALSE,
                          sizeof(Vertex),
                          offsetof(Vertex, Position));


    float aspect = fabsf(self.bounds.size.width / self.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.0f, 50.0f);
    self.effect.transform.projectionMatrix = projectionMatrix;
}

更新和渲染方法:

// Always called once before "render"
-(void)update {
    static bool hasRun = NO;
    for(int x = 0; x < kVersiciesSize; x++){
        // normalize x variable to x coord -1 .. 1 and set Y to math function
        Vertex v = Vertices[x];
        v.Position[0] = [self normalizedX:x];
        v.Position[1] = cos(x);
        v.Position[2] = 0;
        Vertices[x] = v;
        if(!hasRun){
            NSLog(@"x=%f y=%f z=%f", v.Position[0], v.Position[1], v.Position[2]);
        }
    }
    hasRun = YES;
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    [self updateTransforms];
}

-(void)render {
    [self.effect prepareToDraw];

    glBindVertexArrayOES(_vertexArray);
    glDrawElements(GL_LINE_STRIP,
                   sizeof(Indices)/sizeof(Indices[0]),
                   GL_UNSIGNED_BYTE,
                   0);
}

1 个答案:

答案 0 :(得分:0)

你有:

glVertexAttribPointer(GLKVertexAttribPosition,
                      3,
                      GL_FLOAT,
                      GL_FALSE,
                      sizeof(Vertex),
                      offsetof(Vertex, Position));

glVertexAttribPointer的签名是:

glVertexAttribPointer (GLuint indx,          // GLKVertexAttribPosition
                       GLint size,           // 3 floats per vertex
                       GLenum type,          // Vertices are GL_FLOAT-s
                       GLboolean normalized, // GL_FALSE
                       GLsizei stride,       // *** Should be 0, no?
                       const GLvoid* ptr);   // *** Should be &Vertices[0], no?

我很确定,如果您在上面提出评论的建议,那就行了。然后你只需渲染整个事物:

glDrawArrays(GL_LINES, 0, kVerticiesSize);

glDrawArrays(GL_LINES, startIndex, lineCount);

...右?

免责声明:我对整个绑定缓冲区管道不熟悉,但在这种情况下,它会让我感到不必要的并发症。

还注意:您的文字提及GL_LINES,但您的示例代码使用GL_LINE_STRIP。线条的工作方式类似于三角形条带,其中前2个顶点定义第一个线段,但在此之后,每个单个顶点定义下一个线段,其中拾取前一个线段一个人离开了。因此,您的顶点索引对于GL_LINES是正确的,但对于GL_LINE_STRIP,您只需要{ 0, 1, 2, 3, 4, 5... }