在一个着色器中为不同的属性使用不同的GL_ELEMENT_ARRAY_BUFFER?

时间:2012-08-29 23:41:51

标签: opengl-es opengl-es-2.0

在OpenGL(OpenGL ES 2.0)中,我可以为不同的GL_ARRAY_BUFFER缓冲区使用多个GL_ELEMENT_ARRAY_BUFFER吗?我正在阅读“OpenGL ES 2.0编程指南”,第6章“顶点属性,数组和缓冲对象”,有源代码示例:有几个GL_ARRAY_BUFFER(用于位置,法线,纹理坐标)和一个GL_ELEMENT_ARRAY_BUFFER(“用于存储元素索引“)。

当我写问题时,我知道我不能向glDrawElements发送多个一个索引数组,所以如果我使用缓冲区,可能只有最后绑定的GL_ELEMENT_ARRAY_BUFFER用于绘图。但是节省内存(glDrawElements的目的是什么)?我将说明我遇到的问题。

有2个数组(如GL_ARRAY_BUFFERs) - 8个顶点和6个法线

GLfloat gVertexPositions[] =
{
    0.5f, 0.5f, 0.5f,
    0.5f, -0.5f, 0.5f,
    -0.5f, -0.5f, 0.5f,
    -0.5f, 0.5f, 0.5f,
    0.5f, 0.5f, -0.5f,
    0.5f, -0.5f, -0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f, 0.5f, -0.5f
};

GLfloat gVertexNormals[] =
{
    1.0f, 0.0f, 0.0f,  // top
    -1.0f, 0.0f, 0.0f, // bottom
    0.0f, 1.0f, 0.0f,  // right
    0.0f, -1.0f, 0.0f, // left
    0.0f, 0.0f, 1.0f,  // back
    0.0f, 0.0f, -1.0f  // front
};

2个索引数组(如GL_ELEMENT_ARRAY_BUFFERs)

GLubyte gVertexPositionIndices[] =
{
    0, 1, 2, // top
    2, 3, 0,
    0, 4, 1, // right
    1, 4, 5,
    5, 4, 7, // bottom
    6, 5, 7,
    2, 6, 7, // left
    2, 7, 3,
    1, 4, 2, // front
    2, 4, 5,
    0, 3, 4, // back
    7, 4, 3
};

GLubyte gVertexNormalIndices[] =
{
    0, 0, 0,
    0, 0, 0,
    2, 2, 2,
    2, 2, 2,
    1, 1, 1,
    1, 1, 1,
    3, 3, 3,
    3, 3, 3,
    5, 5, 5,
    5, 5, 5,
    4, 4, 4,
    4, 4, 4
};

我设置了顶点属性状态

    glBindAttribLocation(program, ATTRIB_POSITION, "a_position");
    glBindAttribLocation(program, ATTRIB_NORMAL, "a_normal");

//.....

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_POSITION_INDICES]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 36, gVertexPositionIndices, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_POSITION_DATA]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 8, gVertexPositions, GL_STATIC_DRAW); 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_NORMAL_INDICES]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 36, gVertexNormalIndices, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_NORMAL_DATA]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 6, gVertexNormals, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_POSITION_INDICES]);
    glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_POSITION_DATA]);
    glEnableVertexAttribArray(ATTRIB_POSITION);

    glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_NORMAL_DATA]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_NORMAL_INDICES]);
    glEnableVertexAttribArray(ATTRIB_NORMAL);

    glVertexAttribPointer(ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribPointer(ATTRIB_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0);

然后画出

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, 0);

屏幕为空(因为最后一个GL_ELEMENT_ARRAY_BUFFER用于“a_position”属性,其中所有三元组都有相同的数字)

我想要的全部 - 程序生成36个顶点,使用gVertexPositionIndices从gVertexPositions设置它们的位置,使用gVertexNormalIndices从gVertexNormals设置它们的法线。我怀疑这是可能的,但我想确切地知道。如果这是不可能的,那么正确的方法是什么?我是否必须使用8 * 3浮点数作为位置,36个字节用于索引,36 * 3浮点数用于法线?所以我只能为位置属性节省内存?

1 个答案:

答案 0 :(得分:1)

我希望我不会太晚,但我所做的是在顶点数组中基于每个顶点设置法线,然后在声明顶点属性时使用适当的步幅和缓冲偏移。这是我的代码声明顶点和法线:

GLfloat BlockVertexData[144] = {

//Right side...  
//Vertices...            //Normals...        
 1.0f,  2.0f,  0.5f,     1.0f,  0.0f,  0.0f, //B
 1.0f,  2.0f, -0.5f,     1.0f,  0.0f,  0.0f, //F
 1.0f, -2.0f,  0.5f,     1.0f,  0.0f,  0.0f, //D
 1.0f, -2.0f, -0.5f,     1.0f,  0.0f,  0.0f, //H

//Front side...
-1.0f,  2.0f,  0.5f,     0.0f,  0.0f,  1.0f, //A
 1.0f,  2.0f,  0.5f,     0.0f,  0.0f,  1.0f, //B
-1.0f, -2.0f,  0.5f,     0.0f,  0.0f,  1.0f, //C
 1.0f, -2.0f,  0.5f,     0.0f,  0.0f,  1.0f, //D

//Left side...
-1.0f,  2.0f,  0.5f,    -1.0f,  0.0f,  0.0f, //A
-1.0f,  2.0f, -0.5f,    -1.0f,  0.0f,  0.0f, //E
-1.0f, -2.0f,  0.5f,    -1.0f,  0.0f,  0.0f, //C
-1.0f, -2.0f, -0.5f,    -1.0f,  0.0f,  0.0f, //G

//Back side...
-1.0f,  2.0f, -0.5f,     0.0f,  0.0f, -1.0f, //E
 1.0f,  2.0f, -0.5f,     0.0f,  0.0f, -1.0f, //F
-1.0f, -2.0f, -0.5f,     0.0f,  0.0f, -1.0f, //G
 1.0f, -2.0f, -0.5f,     0.0f,  0.0f, -1.0f, //H

//Top side...
-1.0f,  2.0f, -0.5f,     0.0f,  1.0f,  0.0f, //E
 1.0f,  2.0f, -0.5f,     0.0f,  1.0f,  0.0f, //F
-1.0f,  2.0f,  0.5f,     0.0f,  1.0f,  0.0f, //A
 1.0f,  2.0f,  0.5f,     0.0f,  1.0f,  0.0f, //B

//Bottom side...
-1.0f, -2.0f, -0.5f,     0.0f, -1.0f,  0.0f, //G
 1.0f, -2.0f, -0.5f,     0.0f, -1.0f,  0.0f, //H
-1.0f, -2.0f,  0.5f,     0.0f, -1.0f,  0.0f, //C
 1.0f, -2.0f,  0.5f,     0.0f, -1.0f,  0.0f  //D };

GLuint BlockIndicesData[36] = {

//Right side...
2, 0, 3,     0, 1, 3,

//Front side...
6, 4, 7,     4, 5, 7,

//Left side...
11, 10, 8,    8, 9, 11,

//Back side...
15, 14, 12,  12, 13, 15,

//Top side...
19, 18, 16,  16, 17, 19,

//Bottom side...
23, 22, 20,  20, 21, 23 };

以下是声明属性的代码:

// The stride shows that there are 6 floats in each row.
GLsizei stride = 6 * sizeof(GLfloat);
GLuint attribute;

attribute = glGetAttribLocation(program, "VertexPosition");
glEnableVertexAttribArray(attribute);
glVertexAttribPointer(attribute, 3, GL_FLOAT, GL_FALSE, stride, 0);

attribute = glGetAttribLocation(self.program, "VertexNormal");
glEnableVertexAttribArray(attribute);
// The sixth parameter indicates the buffer offset, so here there were 3 floats preceding it, so this indicates it.
glVertexAttribPointer(attribute, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid *)(sizeof(GLfloat) * 3));

我知道这可能会占用更多内存,但也许有人可以提出更好的解决方案。这是我能想到的解决问题的方法。