试图绘制一个简单的线环

时间:2013-11-22 20:03:17

标签: opengl-es-2.0

我疯了。我想做的就是绘制一个简单的线环。但我得到的只是从原点到(-0.5,0.0,-0.5)的对角线。我做错了什么?

@interface Frustum : NSObject {
    GLuint _vertexArray;
    GLuint _vertexBuffer;
    GLfloat left_side[4][3];
}

- (id) init;
- (GLuint) getVertexArray;
- (void) render;

@end


@implementation Frustum

- (id) init {
        left_side[0][0] = -0.5f;  left_side[0][1] = 0.0f;  left_side[0][2] = -0.5f;
        left_side[1][0] = 0.5f;  left_side[1][1] = 0.0f;  left_side[1][2] = -0.5f;
        left_side[2][0] = 0.5f;  left_side[2][1] = 0.0f;  left_side[2][2] = -0.5f;
        left_side[3][0] = -0.5f;  left_side[3][1] = 0.0f;  left_side[3][2] = -0.5f;        

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

        glGenBuffers(1, &_vertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);

        // SOMETHING IS WRONG IN HERE, BUT I DON'T KNOW WHAT!!!!!
        glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*12, left_side, GL_STATIC_DRAW);
        glEnableVertexAttribArray(GLKVertexAttribPosition);
        glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(left_side), NULL);

        glBindVertexArrayOES(0);

    }

    return self;
}

- (void) render {
    glLineWidth(2.0f);
    glDrawArrays(GL_LINE_LOOP, 0, 4);
}

1 个答案:

答案 0 :(得分:1)

尝试更改y或z值,以便形成实际循环。

(-.5, 0, 0 )
(.5, 0, 0 )
(.5, .5, 0)
(-.5 .5, 0 )

将形成一个线环。你只有相同的y,所以这就是一条线。

请记住,默认相机会向下看-z轴,因此它不会看到z的变化(除非你旋转了相机)。