简体   繁体   中英

Qt 5.5 and OpenGL: Program behaves strangely without any apparent reason

I am writing a simple OpenGL applications that features the ability to change the color of a mesh using a fragment shader. However, the uniform float time variable that is used to periodically change the colour ( sin(time) ) is for some reason nonfunctioning.

Furthermore, the entire mesh is filled in a white color... EDIT: I simply forgot to bind the current shaderProgram. The uniform float time variable not doing anything is still not fixed though...

The PaintGL code:

void OpenGLWidget::paintGL() {
    static unsigned int frame = 0;

    vao->bind();
    shaders->link();
    shaders->bind();

    // Enabling custom shader atttributes
    int timeIndex = shaders->uniformLocation("time");

    qDebug() << "Time index:" << timeIndex;

    shaders->setUniformValue(timeIndex, time->elapsed());
    shaders->link();

    qDebug() << "Frame " << frame << "| created = " << elems->isCreated() << "| time = " << time->elapsed()/1000.f;

    glDrawElements(GL_TRIANGLE_FAN, rawelems.size(), GL_UNSIGNED_INT, 0);

    vao->release();
    ++frame;
}

the fragment shader:

#version 330 core

uniform float time;
out vec3 color;

void main() {
    color = vec3( sin(time), 1.0f, 0.0f );
}

Note I have omitted the "vertex" vertex shader attribute which is used for placing the vertices - that takes up index 0 also, I am unsure if this is the problem, and even if it is how to solve it.

qDebug output (or some of it anyway):

TimeINDEX: 0

Shader compilation log: ""
Time index: 0
Frame  0 | created =  true | time =  0.268
Time index: 0
Frame  1 | created =  true | time =  0.372
Time index: 0
Frame  2 | created =  true | time =  0.44
...

I was wondering if perhaps the problem may be in that I have the vertex and fragment shader both loaded in the shader QOpenGLShaderProgram object, but at this stage I can do no more than merely throw guesses in a general direction.

Turns out the problem was in the line:

shaders->link();

just after

shaders->setUniformValue(timeIndex, time->elapsed());

Once I removed the link() function call everything ran normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM