顶点和法线的单独数组

时间:2013-12-03 19:48:05

标签: ios opengl-es-2.0

我正在尝试绘制一个对象,其中我的顶点存储在一个数组中,而我的法线存储在一个单独的数组中。到目前为止,我已经绘制了所有我的对象,其中顶点和法线已经在一个数据结构中交错,所以我不知道如何传入单独的数组。请向下滚动到我的init函数,在那里您将看到我试图传入两个数组,但显然做错了。我可以在没有法线的情况下绘制这个对象就好了,但是我需要照明法线等等。

有人能告诉我这里做错了什么吗?谢谢!

    @implementation Camera

    GLfloat cameraVertices[] = {
        0.500000f, -0.350000f, 0.000000f, 0.500000f, -0.350000f, 0.000000f, 0.500000f, -0.350000f, 0.000000f,
        -0.500000f, -0.350000f, 0.000000f, -0.500000f, -0.350000f, 0.000000f,
        -0.500000f, -0.350000f, 0.000000f, -0.500000f, 0.350000f, 0.000000f,
        -0.500000f, 0.350000f, 0.000000f, -0.500000f, 0.350000f, 0.000000f,
       // this goes on for a while...
    };

    GLfloat cameraNormals[] = {
        1.00000f, 0.000000f, 0.000000f, 0.000000f, -1.00000f, 0.000000f, 0.000000f, 0.000000f, -1.00000f,
        -1.00000f, 0.000000f, 0.000000f, 0.000000f, -1.00000f, 0.000000f,
        0.000000f, 0.000000f, -1.00000f, -1.00000f, 0.000000f, 0.000000f,
        0.000000f, 1.00000f, 0.000000f, 0.000000f, 0.000000f, -1.00000f,
        1.00000f, 0.000000f, 0.000000f, 0.000000f, 1.00000f, 0.000000f,
           // this also goes on for a while...
};

    GLint cameraIndices[] = {
        2, 5, 11, 5, 8, 10, 7, 17, 7, 14, 16, 13, 23, 13, 20, 22,
        19, 1, 19, 4, 3, 18, 6, 18, 12, 21, 0, 15, 0, 9, 203, 149,
        204, 147, 204, 152, 204, 155, 204, 158, 204, 161, 204, 164, 204, 167, 204, 170,
        204, 173, 204, 176, 204, 179, 204, 182, 204, 185, 204, 188, 204, 191, 204, 194,
        204, 197, 204, 200, 203, 144, 148, 144, 202, 201, 199, 198, 196, 195, 193, 192,
        190, 189, 187, 186, 184, 183, 181, 180, 178, 177, 175, 174, 172, 171, 169, 168,
        166, 165, 163, 162, 160, 159, 157, 156, 154, 153, 151, 150, 146, 145, 148, 145,
        144, 123, 87, 124, 87, 125, 85, 126, 89, 127, 91, 128, 93, 129, 95, 130,
        97, 131, 99, 132, 101, 133, 103, 134, 105, 135, 107, 136, 109, 137, 111, 138,
        113, 139, 115, 140, 117, 141, 119, 142, 121, 143, 123, 143, 124, 29, 86, 29,
        122, 83, 120, 80, 118, 77, 116, 74, 114, 71, 112, 68, 110, 65, 108, 62,
        106, 59, 104, 56, 102, 53, 100, 50, 98, 47, 96, 44, 94, 41, 92, 38,
        90, 35, 88, 32, 84, 27, 86, 27, 29, 24, 28, 24, 82, 81, 79, 78,
        76, 75, 73, 72, 70, 69, 67, 66, 64, 63, 61, 60, 58, 57, 55, 54,
        52, 51, 49, 48, 46, 45, 43, 42, 40, 39, 37, 36, 34, 33, 31, 30,
        26, 25, 28, 25, 24
    };

    - (id) init {

        if (self = [super init]) {

            NSLog(@"new camera!");

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

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

            glEnableVertexAttribArray(GLKVertexAttribPosition);
            glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*3, NULL);

            glBufferData(GL_ARRAY_BUFFER, sizeof(cameraNormals), cameraNormals, GL_STATIC_DRAW);
            glEnableVertexAttribArray(GLKVertexAttribNormal);
            glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*3, NULL);

            glBindVertexArrayOES(0);

        }

        return self;
    }

    - (GLuint) getVertexArray
    {
        return _vertexArray;
    }

    - (void) render
    {
        glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, &cameraIndices[0]);
        glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, &cameraIndices[5]);
        glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, &cameraIndices[10]);
        glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, &cameraIndices[15]);
        glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_INT, &cameraIndices[20]);
        glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, &cameraIndices[25]);
        glDrawElements(GL_TRIANGLE_STRIP, 39, GL_UNSIGNED_SHORT, &cameraIndices[30]);
        glDrawElements(GL_TRIANGLE_STRIP, 44, GL_UNSIGNED_SHORT, &cameraIndices[69]);
        glDrawElements(GL_TRIANGLE_STRIP, 44, GL_UNSIGNED_SHORT, &cameraIndices[113]);
        glDrawElements(GL_TRIANGLE_STRIP, 44, GL_UNSIGNED_SHORT, &cameraIndices[157]);
        glDrawElements(GL_TRIANGLE_STRIP, 44, GL_UNSIGNED_SHORT, &cameraIndices[201]);

    }

    @end

1 个答案:

答案 0 :(得分:1)

如果您有单独的数组,则需要单独的顶点缓冲区对象。您已经在使用glGenBuffersglBindBuffer为位置创建/绑定VBO - 您需要再次执行此操作以在为{1}}填充之前为法线创建/绑定单独的VBO

当你在它时,你也可以创建一个glBufferData类型的缓冲区来保存你的索引数组,并将它附加到你的VAO。这将使您的GL_ELEMENT_ARRAY_BUFFER调用更快,因为他们可以使用GPU上已有的数据,而不是传输每个调用时要绘制的(子)索引数组。执行此操作时,您需要将最后一个参数更改为glDrawElements调用 - 而不是指向客户端内存空间中的数组的指针,它是从缓冲区开头的偏移量。