【问题标题】:GL_LINES drawing back to first point at the end of verticiesGL_LINES 画回到顶点末端的第一个点
【发布时间】:2012-12-26 23:05:54
【问题描述】:

在 iOS 上使用 GLKit (OpenGLES 2.0)。

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

假设我使用 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

总账设置

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

还有 Update 和 Render 方法:

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

【问题讨论】:

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


    【解决方案1】:

    你有:

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

    【讨论】:

    • 感谢您对此进行调查。这是一个旧项目,但我可能会挖掘出来并再次查看它。谢谢你。正如您所建议的那样,看起来确实存在一些不匹配。 GL_LINES 和 GL_LINE_STRIP 的区别只是粘贴错误。我记得深入挖掘他们两个。我记得他们打算如何从旧的 OpenGL 中操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多