GLSL统一ivec未设置,并且在被询问时具有默认(错误)值

时间:2018-09-03 09:28:39

标签: c++ glsl

我有一个场景,其中包含多个GLSL程序(OpenGL 3.3)。这些程序使用不同的着色器,这些着色器是相似的,但是(将)有所不同。而且我有一个统一的ivec4 char1,所有版本的名称都相同,这对所有程序都具有相同的含义和价值。着色器中的dot1(...)函数中使用了此制服。着色器中的main()函数中使用了此制服。

fragment3d.glsl:

#version 330 core

smooth in vec4 vertexPosition;
smooth in vec4 vertexColor;
smooth in vec4 vertexNormal;
out vec4 fragmentColor;

uniform ivec4 char1;
uniform float shineness;

float dot1(vec4 x, vec4 y)
{
    float result = 0.0;
    for (int i = 0; i < 4; i++)
    {
        result += char1[i] * x[i] * y[i];
    }
    return result;
}

void main()
{
    if (vertexNormal == vec4(0.0, 0.0, 0.0, 0.0))
    {
        fragmentColor = vertexColor;
    }
    else
    {
        vec4 lightDirection = vec4(1.0, 0.0, 0.0, 0.0) - vertexPosition;
        float lightNormInverse = inversesqrt(dot1(lightDirection, lightDirection));
        lightDirection *= lightNormInverse;
        float diffuseCoefficient = dot1(lightDirection, vertexNormal);
        vec4 reflectedDirection = 2 * diffuseCoefficient * vertexNormal - lightDirection;
        diffuseCoefficient = abs(diffuseCoefficient);
        float specularCoefficient = max(dot1(reflectedDirection, lightDirection), 0.0);
        if (specularCoefficient > 0.0 && shineness > 0.0)
        {
            specularCoefficient = pow(specularCoefficient, shineness);
        }
        vec3 diffuseColor = clamp(vertexColor.rgb * diffuseCoefficient, 0.0, 1.0);
        vec3 specularColor = clamp(vec3(0.5, 0.5, 0.5) * specularCoefficient, 0.0, 1.0);
        fragmentColor.rgb = diffuseColor + specularColor;
        fragmentColor.a = 1.0 - diffuseCoefficient * (1.0 - vertexColor.a);
    }
}

fragment3d-new.glsl

#version 330 core

smooth in vec4 geometryPosition;
smooth in vec4 geometryNormal;
smooth in vec4 geometryColor;
out vec4 fragmentColor;

uniform ivec4 char1;
uniform float shineness;

float dot1(vec4 x, vec4 y)
{
    float result = 0.0;
    for (int i = 0; i < 4; i++)
    {
        result += char1[i] * x[i] * y[i];
    }
    return result;
}

void main()
{
    if (geometryNormal == vec4(0.0, 0.0, 0.0, 0.0))
    {
        fragmentColor = geometryColor;
    }
    else
    {
        vec4 lightDirection = vec4(1.0, 0.0, 0.0, 0.0) - geometryPosition;
        float lightNormInverse = inversesqrt(dot1(lightDirection, lightDirection));
        lightDirection *= lightNormInverse;
        float diffuseCoefficient = dot1(lightDirection, geometryNormal);
        vec4 reflectedDirection = 2 * diffuseCoefficient * geometryNormal - lightDirection;
        diffuseCoefficient = abs(diffuseCoefficient);
        float specularCoefficient = max(dot1(reflectedDirection, lightDirection), 0.0);
        if (specularCoefficient > 0.0 && shineness > 0.0)
        {
            specularCoefficient = pow(specularCoefficient, shineness);
        }
        vec3 diffuseColor = clamp(geometryColor.rgb * diffuseCoefficient, 0.0, 1.0);
        vec3 specularColor = clamp(vec3(0.5, 0.5, 0.5) * specularCoefficient, 0.0, 1.0);
        fragmentColor.rgb = diffuseColor + specularColor;
        fragmentColor.a = 1.0 - diffuseCoefficient * (1.0 - geometryColor.a);
    }
}

初始化C ++代码为:

glUseProgram(0);
if (m_glProgram[GEOMETRY] != 0)
{
    glDeleteProgram(m_glProgram[GEOMETRY]);
}
if (m_glProgram[TRIANGLE] != 0)
{
    glDeleteProgram(m_glProgram[TRIANGLE]);
}
if (m_glProgram[TEXTURE] != 0)
{
    glDeleteProgram(m_glProgram[TEXTURE]);
}
GLuint vertexShaderGeometry = compileShader(GL_VERTEX_SHADER, vertex, ss);
GLuint fragmentShaderGeometry = compileShader(GL_FRAGMENT_SHADER, fragment, ss);
vector<GLuint> shadersGeometry = {vertexShaderGeometry, fragmentShaderGeometry};
m_glProgram[GEOMETRY] = compileProgram(shadersGeometry, ss);
glUseProgram(m_glProgram[GEOMETRY]);
m_glUniform[GEOMETRY_PROJECTION] = glGetUniformLocation(m_glProgram[GEOMETRY], "projection");   // Set in changeProjection(...)
m_glUniform[GEOMETRY_ORIENTATION] = glGetUniformLocation(m_glProgram[GEOMETRY], "orientation"); // Set in changeOrientation(...)
m_glUniform[GEOMETRY_MODE] = glGetUniformLocation(m_glProgram[GEOMETRY], "mode");               // Set here
m_glUniform[GEOMETRY_CHAR1] = glGetUniformLocation(m_glProgram[GEOMETRY], "char1");             // Set here
m_glUniform[GEOMETRY_SHANENESS] = glGetUniformLocation(m_glProgram[GEOMETRY], "shineness");     // Set here? For now it is hardcoded.
cleanProgram(m_glProgram[GEOMETRY], shadersGeometry);

if (m_glUniform[GEOMETRY_MODE] >= 0 && dim == 2)
{
    // TODO Add configuration for Poincare model.
    // For now, Beltrami-Klein is hardcoded.
    glUniform1i(m_glUniform[GEOMETRY_MODE], 1);
}
if (m_glUniform[GEOMETRY_CHAR1] >= 0)
{
    GLint char1[4];
    char1[0] = m_projected.chars(0);
    char1[1] = 1;
    char1[2] = m_projected.chars(1);
    char1[3] = m_projected.chars(1, 2);
    glUniform4iv(m_glUniform[GEOMETRY_CHAR1], 1, char1);
}
if (m_glUniform[GEOMETRY_SHANENESS] >= 0)
{
    // Add it to configuration. For now it is hardcoded.
    glUniform1f(m_glUniform[GEOMETRY_SHANENESS], 1.0);
}
////////////////////////////////////////////////////////
if (dim == 3)
{
    GLuint vertexShaderNew3d = compileShader(GL_VERTEX_SHADER, "vertex3d-new.glsl", ss);
    GLuint geometryShaderNew3d = compileShader(GL_GEOMETRY_SHADER, "geometry3d-new.glsl", ss);
    GLuint fragmentShaderNew3d = compileShader(GL_FRAGMENT_SHADER, "fragment3d-new.glsl", ss);
    m_glProgram[TRIANGLE] = compileProgram({vertexShaderNew3d, geometryShaderNew3d, fragmentShaderNew3d}, ss);
    glUseProgram(TRIANGLE);
    m_glUniform[PLANE_3D_PROJECTION] = glGetUniformLocation(m_glProgram[TRIANGLE], "projection");   // Set in changeProjection(...)
    m_glUniform[PLANE_3D_ORIENTATION] = glGetUniformLocation(m_glProgram[TRIANGLE], "orientation"); // Set in changeOrientation(...)
    m_glUniform[PLANE_3D_CHAR1] = glGetUniformLocation(m_glProgram[TRIANGLE], "char1");             // Set here
    m_glUniform[PLANE_3D_SHANENESS] = glGetUniformLocation(m_glProgram[TRIANGLE], "shineness");     // Set here? For now it is hardcoded.
    cout << "m_glProgram[PLANE_3D] = " << m_glProgram[TRIANGLE] << endl;
    cout << "m_glUniform[PLANE_3D_PROJECTION] = " << m_glUniform[PLANE_3D_PROJECTION] << endl;
    cout << "m_glUniform[PLANE_3D_ORIENTATION] = " << m_glUniform[PLANE_3D_ORIENTATION] << endl;
    cout << "m_glUniform[PLANE_3D_CHAR1] = " << m_glUniform[PLANE_3D_CHAR1] << endl;
    cout << "m_glUniform[PLANE_3D_SHANENESS] = " << m_glUniform[PLANE_3D_SHANENESS] << endl;
    if (m_glUniform[PLANE_3D_CHAR1] >= 0)
    {
        GLint char1[4];
        char1[0] = m_projected.chars(0);
        char1[1] = 1;
        char1[2] = m_projected.chars(1);
        char1[3] = m_projected.chars(1, 2);
        cout << "Before setting:       "; for (int i = 0; i < 4; i++) {cout << char1[i] << ' ';} cout << endl;
        glUniform4iv(m_glUniform[PLANE_3D_CHAR1], 1, char1);

        glGetUniformiv(m_glProgram[TRIANGLE], m_glUniform[PLANE_3D_CHAR1], char1);
        cout << "Read after setting:   "; for (int i = 0; i < 4; i++) {cout << char1[i] << ' ';} cout << endl;
        glGetUniformiv(m_glProgram[GEOMETRY], m_glUniform[GEOMETRY_CHAR1], char1);
        cout << "Similar for GEOMETRY: "; for (int i = 0; i < 4; i++) {cout << char1[i] << ' ';} cout << endl;
    }
    if (m_glUniform[PLANE_3D_SHANENESS] >= 0)
    {
        // Add it to configuration. For now it is hardcoded.
        glUniform1f(m_glUniform[PLANE_3D_SHANENESS], 1.0);
    }
}

由于某种原因,ivec4 char1统一设置正确,可以使用m_glProgram[GEOMETRY]和统一m_glUniform[GEOMETRY_CHAR1]从第一个着色器读取,但是不能正确地使用{在第二个着色器中设置{1}}和统一的m_glProgram[TRIANGLE]。此代码的输出是:

m_glUniform[PLANE_3D_CHAR1]

这意味着准备好的向量m_glProgram[PLANE_3D] = 5 m_glUniform[PLANE_3D_PROJECTION] = 1262 m_glUniform[PLANE_3D_ORIENTATION] = 1261 m_glUniform[PLANE_3D_CHAR1] = 1263 m_glUniform[PLANE_3D_SHANENESS] = 1264 Before setting: 0 1 1 1 Read after setting: 0 0 0 0 Similar for GEOMETRY: 0 1 1 1 在第一个着色器/程序中正确设置,而不在第二个着色器/程序中正确设置。查询其值将返回默认值[0, 1, 1, 1]。怎么了?

1 个答案:

答案 0 :(得分:0)

如果通过glUniform4iv设置统一变量,则必须将着色器程序安装为当前渲染状态的一部分,因为glUniform4iv将值设置为当前着色器程序对象的默认统一块。参见glUseProgram

TRIANGLE是存储在容器m_glProgram中的着色器程序对象的索引,但不是着色器程序对象本身的索引。

这意味着glUseProgram(TRIANGLE)并没有达到您的预期。

将其更改为:

glUseProgram(m_glProgram[TRIANGLE]);