正确地将统一数据发送到GPU

时间:2016-06-15 00:02:19

标签: c++ opengl

我试图通过制服将Light数据发送到我的片段着色器。我遇到以下功能问题:

void glUniform3fv(GLint location, GLsizei count, const GLfloat *value);

下面是我编写的一个函数,它意味着将所有Light数据(存储为“Shader”类中的固定大小数组)发送到Fragment Shader: (注意:Vector3F类是我自己的,如果需要,我可以提供它的来源。)

void Shader::updateLights()
{
    // this->lights[x].getPosR() returns a Vector3F reference. Same with getLightDirectionR() and getColourR().
    glUniform3fv(this->uniforms[LIGHT1POS_U], 3, &(this->lights[0].getPosR().getXR()));
    glUniform3fv(this->uniforms[LIGHT1LD_U], 3, &(this->lights[0].getLightDirectionR().getXR()));
    glUniform3fv(this->uniforms[LIGHT1COLOUR_U], 3, &(this->lights[0].getColourR().getXR()));
    glUniform1f(this->uniforms[LIGHT1POW_U], this->lights[0].getPowerR());

    glUniform3fv(this->uniforms[LIGHT2POS_U], 3, &(this->lights[1].getPosR().getXR()));
    glUniform3fv(this->uniforms[LIGHT2LD_U], 3, &(this->lights[1].getLightDirectionR().getXR()));
    glUniform3fv(this->uniforms[LIGHT2COLOUR_U], 3, &(this->lights[1].getColourR().getXR()));
    glUniform1f(this->uniforms[LIGHT2POW_U], this->lights[1].getPowerR());

    glUniform3fv(this->uniforms[LIGHT3POS_U], 3, &(this->lights[2].getPosR().getXR()));
    glUniform3fv(this->uniforms[LIGHT3LD_U], 3, &(this->lights[2].getLightDirectionR().getXR()));
    glUniform3fv(this->uniforms[LIGHT3COLOUR_U], 3, &(this->lights[2].getColourR().getXR()));
    glUniform1f(this->uniforms[LIGHT3POW_U], this->lights[2].getPowerR());

    glUniform3fv(this->uniforms[LIGHT3POS_U], 3, &(this->lights[3].getPosR().getXR()));
    glUniform3fv(this->uniforms[LIGHT3LD_U], 3, &(this->lights[3].getLightDirectionR().getXR()));
    glUniform3fv(this->uniforms[LIGHT3COLOUR_U], 3, &(this->lights[3].getColourR().getXR()));
    glUniform1f(this->uniforms[LIGHT3POW_U], this->lights[3].getPowerR());

    // Print out all the information to ensure that data is correct.
    std::cout << "Lights Info:\n";
    for(unsigned int i = 0; i < Shader::MAX_LIGHTS; i++)
    {
        Light* l = &(this->lights[i]);
        std::cout << "Light Id " << i << ": ";
        std::cout << "Position = [" << l->getPosR().getX() << ", " << l->getPosR().getY() << ", " << l->getPosR().getZ() << "], ";
        std::cout << "Light Direction = [" << l->getLightDirectionR().getX() << ", " << l->getLightDirectionR().getY() << ", " << l->getLightDirectionR().getZ() << "], ";
        std::cout << "Light Colour = [" << l->getColourR().getX() << ", " << l->getColourR().getY() << ", " << l->getColourR().getZ() << "], ";
        std::cout << "Light Power = " << l->getPowerR() << " Watts.\n";
    }
}

然而,制服是空的!我确信我的问题在于我对const GLfloat *value的尝试。光数据存储在类体中,所以我不认为范围是问题。

在运行时打印以下文本块:

Light Id 0: Position = [0, 15, 0], Light Direction = [0, -1, 1], Light Colour = [1, 1, 1], Light Power = 200 Watts.
Light Id 1: Position = [5, 15, 10], Light Direction = [0, -1, 1], Light Colour = [1, 1, 1], Light Power = 200 Watts
Light Id 2: Position = [10, 15, 20], Light Direction = [0, -1, 1], Light Colour = [1, 1, 1], Light Power = 200 Watts.
Light Id 3: Position = [15, 15, 30], Light Direction = [0, -1, 1], Light Colour = [1, 1, 1], Light Power = 200 Watts.

我只见过这个函数是通过glm :: vec3对象调用的,所以我不相信我在第三个参数中提供了足够的信息(我只是出现了发送Vector3F的X值而不是XYZ)。我试图在函数中构建一个浮点列表,以确保所有数据都被发送,但是一旦我超出范围,数据就会丢失,从而导致运行时崩溃。我真的想在这种情况下避免堆分配。什么是格式化我的数据以便正确发送所有数据的好方法? My Fragment着色器有效且Light Data有效(所以我应该看到它),但是场景是漆黑的。

1 个答案:

答案 0 :(得分:3)

glUniform3fv(this->uniforms[LIGHT1POS_U], 3, &(this->lights[0].getPosR().getXR()));

count参数是要写入的 uniform 中的数组元素数,而不是向量中元素的数量。它已经知道向量中元素的数量:这就是3f在函数名中的含义。

因此,除非this->uniforms[LIGHT1POS_U]定义为uniform vec3 name[3];,否则此代码将无法正常运行。您应该从每次此类呼叫中收到GL_INVALID_OPERATION错误。

当然,所有这些都假定无论getXR返回什么都相当于3个浮点数的数组。由于您没有向我们展示代码,因此无法再说明这一点。