4

我有一个 GLfloats 数组,我在绘制四边形时用作位置和颜色(所以每个顶点有 4 个浮点数)。我还想添加为四边形着色的功能,并认为我会将 RGBA 打包到单个 GLuint 中,然后将其与位置一起发送到 GPU。

那么,我可以以某种方式将 4 个 GLfloats 和 1 个 GLuint 发送到 GPU 吗?

以下是相关代码:

void SpriteRenderer::Init()
{
    vertexBufferArrayInserts = 0;

    hasBegun = GL_FALSE;

    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * MAX_VERTEX_BUFFER_SIZE, 0, GL_STREAM_DRAW);

    glGenBuffers(1, &elementBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * MAX_ELEMENT_BUFFER_SIZE, 0, GL_STREAM_DRAW);

    //////////////////////////////////////////////////////////////////////////
    //LOAD SHADER, CREATE AND USE PROGRAM
    //////////////////////////////////////////////////////////////////////////
    glGenVertexArrays(1, &vertexArrayObject);
    glBindVertexArray(vertexArrayObject);

    vertexShaderID = LoadShaderFromFile("Shader", GL_VERTEX_SHADER);
    fragmentShaderID = LoadShaderFromFile("Shader", GL_FRAGMENT_SHADER);

    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShaderID);
    glAttachShader(shaderProgram, fragmentShaderID);

    glBindFragDataLocation(shaderProgram, 0, "outColor");

    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    shaderPosAttrib = glGetAttribLocation(shaderProgram, "position");
    glVertexAttribPointer(shaderPosAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, 0);
    glEnableVertexAttribArray(shaderPosAttrib);

    shaderTexCoordAttrib = glGetAttribLocation(shaderProgram, "texCoord");
    glVertexAttribPointer(shaderTexCoordAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 2));
    glEnableVertexAttribArray(shaderTexCoordAttrib);

    shaderColorAttrib = glGetAttribLocation(shaderProgram, "Color");
    glVertexAttribPointer(shaderColorAttrib, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 4));
    glEnableVertexAttribArray(shaderColorAttrib);

    shaderProjMatAttrib = glGetUniformLocation(shaderProgram, "projMat");

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

void SpriteRenderer::Draw(Vector2<GLfloat> position, Rect clipRect)
{
    //TOP LEFT
    vertexBufferArray[vertexBufferArrayInserts * 16] = position.X;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 1] = position.Y;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 2] = clipRect.GetLeftX() / 512.0f;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 3] = clipRect.GetTopY() / 512.0f;
    colorBufferArray[vertexBufferArrayInserts * 4] = PackColor(255, 255, 255, 255);

    //TOP RIGHT
    vertexBufferArray[vertexBufferArrayInserts * 16 + 4] = position.X + clipRect.GetWidth();
    vertexBufferArray[vertexBufferArrayInserts * 16 + 5] = position.Y;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 6] = clipRect.GetRightX() / 512.0f;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 7] = clipRect.GetTopY() / 512.0f;
    colorBufferArray[vertexBufferArrayInserts * 4 + 1] = PackColor(255, 255, 255, 255);

    //BOTTOM RIGHT
    vertexBufferArray[vertexBufferArrayInserts * 16 + 8] = position.X + clipRect.GetWidth();
    vertexBufferArray[vertexBufferArrayInserts * 16 + 9] = position.Y + clipRect.GetHeight();
    vertexBufferArray[vertexBufferArrayInserts * 16 + 10] = clipRect.GetRightX() / 512.0f;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 11] = clipRect.GetBottomY() / 512.0f;
    colorBufferArray[vertexBufferArrayInserts * 4 + 2] = PackColor(255, 255, 255, 255);

    //BOTTOM LEFT
    vertexBufferArray[vertexBufferArrayInserts * 16 + 12] = position.X;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 13] = position.Y + clipRect.GetHeight();
    vertexBufferArray[vertexBufferArrayInserts * 16 + 14] = clipRect.GetLeftX() / 512.0f;
    vertexBufferArray[vertexBufferArrayInserts * 16 + 15] = clipRect.GetBottomY() / 512.0f;
    colorBufferArray[vertexBufferArrayInserts * 4 + 3] = PackColor(255, 255, 255, 255);

    //ELEMENT BUFFER
    elementBufferArray[vertexBufferArrayInserts * 6] = vertexBufferArrayInserts * 4;
    elementBufferArray[vertexBufferArrayInserts * 6 + 1] = vertexBufferArrayInserts * 4 + 1;
    elementBufferArray[vertexBufferArrayInserts * 6 + 2] = vertexBufferArrayInserts * 4 + 2;
    elementBufferArray[vertexBufferArrayInserts * 6 + 3] = vertexBufferArrayInserts * 4 + 2;
    elementBufferArray[vertexBufferArrayInserts * 6 + 4] = vertexBufferArrayInserts * 4 + 3;
    elementBufferArray[vertexBufferArrayInserts * 6 + 5] = vertexBufferArrayInserts * 4;

    vertexBufferArrayInserts++;

    if(vertexBufferArrayInserts == MAX_BUFFER_INSERTS)
        Draw();
}

void SpriteRenderer::Draw()
{
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * vertexBufferArrayInserts * 16, vertexBufferArray);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertexBufferArrayInserts * 16, sizeof(GLuint) * vertexBufferArrayInserts * 4, colorBufferArray);
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLint) *  vertexBufferArrayInserts * 6, elementBufferArray);

    glDrawElements(GL_TRIANGLES, vertexBufferArrayInserts * 6, GL_UNSIGNED_INT, 0);

    vertexBufferArrayInserts = 0;
}

GLuint SpriteRenderer::PackColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
    GLint returnVal = (r << 24) + (g << 16) + (b << 8) + a;

    return returnVal;
}
4

1 回答 1

3

你的问题在于:

glVertexAttribPointer(shaderColorAttrib, 1, GL_UNSIGNED_INT,
  GL_FALSE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 4));

这将作为单个组件解压缩。

告诉 GPU 您正在发送四个单字节分量(加起来是一个 32 位值):

glVertexAttribPointer(shaderColorAttrib, 4, GL_UNSIGNED_BYTE,
  GL_TRUE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 4));

请注意,我将规范化标志设置为 true。如果您要将其发送到vec4着色器上的 a ,请执行此操作,以便将每个组件转换为[0,1]您可能想要的范围。

此外,不要使用 进行非常容易出错的数学运算,而是sizeof(float)考虑制作一个结构并使用该结构的大小并offsetof生成您的步幅和顶点偏移:

struct vertex {
   float4 position;
   ubyte4 color;
};

glVertexAttribPointer(shaderColorAttrib, 4, GL_FLOAT,
  GL_FALSE, sizeof(vertex), offsetof(vertex, position));
glVertexAttribPointer(shaderColorAttrib, 4, GL_UNSIGNED_BYTE,
  GL_TRUE, sizeof(vertex), offsetof(vertex, color));

一旦您开始混合数据类型,这将变得更加重要。请注意结构填充;usingoffsetof将确保一切正常,但您不想浪费缓冲区对象空间。

于 2013-06-20T21:23:01.280 回答