0
votes

Assuming you want to pass in additional light positions to the vertex shader (outside of the lights provided by the fixed functional pipeline) I am not seeing these additional uniforms functioning correctly.

For example in my vertex shader I have:

...
uniform vec3 light_pos;

...
varying vec3 frag_light_pos;

void main() {
    ...

    frag_light_pos = vec3(0.0f, 100.0f, 0.0f);
    //frag_light_pos = light_pos;

    ...
}

Where the difference in my scene can be seen below.

To set the uniform variable I am using Qt's QOpenGLShaderProgam::setUniformValue in the following manner.

QVector3D light_pos(0.0f, 100.0f, 0.0f);
m_ShaderProgram->setUniformValue("light_pos", light_pos);

All my other uniforms, the mv, mvp, and (trans(mv))^-1 all work so it is not a problem with Qt. Where am I going wrong? I would expect to see the same image using either version.

Screenshot using const vec3: const vec3

Screenshot using uniform vec3: uniform vec3


Update

I have added glGetError after every call to OpenGL in my code, which consists of one function. I only get an error when I go to set the uniform both with, or without the use of Qt's API, as seen below:

/*
    QVector3D light_pos(0.0f, 100.0f, 0.0f);
    m_ShaderProgram->setUniformValue("frag_light_pos", light_pos); GL_CALL
/*/
    GLint light_id = glGetUniformLocation(m_ShaderProgram->programId(),
        "frag_light_pos"); GL_CALL
    glUniform3f(light_id, 10.0f, 10.0f, 10.0f); GL_CALL
//*/

Where GL_CALL is defined as:

#define GL_CALL GetGlErrors(__LINE__);

void GetGlErrors(int line)
{
    GLenum err_code;
    while ((err_code = glGetError()) != GL_NO_ERROR) {
#if __GL_WRAPPER_WINDOWS__
        char buffer[256];
        sprintf_s(buffer, "GL_ERROR is %d from %d\n", err_code, line);
        OutputDebugStringA(buffer);
#else
        printf("GL_ERROR is %d from %d\n", err_code, line);
#endif
    }
}

The ouput of my program shows me the line with the given error:

GL_ERROR is 1282 from 263

1

1 Answers

0
votes

Since uniforms are accessible in both the vertex shader and the fragment shader there's no reason to pass it through the vertex shader. Just move the uniform declaration to the fragment shader and use it there directly.

The result you get is probably due to the interpolation of varying variables in the rasterizer, which means that frag_light_pos will have a different value for each pixel (fragment). It might be possible to use the varying with the flat qualifier to 'turn off' interpolation, but I can't see a reason why you would do that.