Skip to content
  • 0 Votes
    3 Posts
    433 Views
    W

    Yup, looks like you've spotted it -- in particular, the specular term is

    materialSpecularColor * lightStrength * pow(cosAlpha, 10)

    That's the part that will contribute to the shininess of the material. And, materialSpecularColor comes from highp vec3 materialSpecularColor = lightColor.rgb; which is to say that the materialSpecularColor in that shader is not really a property of the material since the value is just from the lighting. Not really the clearest name if you are trying to pick it apart. This type of look is traditionally used for a plastic type material which has white highlights for white lights regardless of the color of the material. If you want something a bit more metallic looking, you can multiply the lightcolor.rgb by some actual material color. This lighting model is a bit of an old school aphysical hack, so you may want to look into to more modern physically based lighting models if you are looking for more realism. But the classic specular + diffuse model is way simpler to understand even if it isn't super accurate to the real world.

  • 0 Votes
    2 Posts
    2k Views
    D

    I finally managed to get things work.

    First of all, I had to change and set the format (you have to change it before widget is shown), for example in main function , before my app was shown:
    main:

    int main(int argc, char *argv[]) { QApplication a(argc, argv); SymmGaitModels w; // GLWidget is my implemantation of QOpenGLWidget GLWidget* gl = w.findChild<GLWidget*>("OpenGLWidget"); QSurfaceFormat format; //format.setSamples(4); //format.setDepthBufferSize(24); //format.setStencilBufferSize(8); format.setProfile(QSurfaceFormat::OpenGLContextProfile::CompatibilityProfile); QSurfaceFormat::setDefaultFormat(format); gl->setFormat(format); w.show(); return a.exec(); }

    Moreover, I had to move all (even enabling and disabling things like blend, point_sprite etc, which caused my problems) of my openGL functions between

    QPainter-> beginNativePainting() ... QPainter->endNativePainting();

    And I had to implement paintEvent function. I just put paintGL() expression there.
    Now my functions look like that:

    void GLWidget::initializeGL() { printf("autoFILL: %d\n",this->autoFillBackground()); initializeOpenGLFunctions(); // this function has to stay in initializeGL // commented functions below has to be removed and put after beginNativePainting //glEnable(GL_POINT_SPRITE); //glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); //glEnable(GL_BLEND); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // shaders may be compiled and added here, but program has to be linked and built after beginNativePainting() vertexShad = new QOpenGLShader(QOpenGLShader::Vertex); fragmentShad = new QOpenGLShader(QOpenGLShader::Fragment); bool flag = vertexShad->compileSourceFile(QString("vs.glsl")); //bool flag = vertexShad->compileSourceCode(vertexShadSrc); if (flag) printf("compiled vertex Shader\n"); flag = fragmentShad->compileSourceFile(QString("fs.glsl")); //flag = fragmentShad->compileSourceCode(fragmentShadSrc); if (flag) printf("compiled fragment Shader\n"); flag = program.addShader(vertexShad); if (flag) printf("linked vertex Shader\n"); flag = false; flag =program.addShader(fragmentShad); if (flag) printf("linked fragment Shader\n"); } void GLWidget::paintEvent(QPaintEvent *e) { paintGL(); // still this, widget has to be now refreshed by widget->update() } void GLWidget::paintGL() { painter = new QPainter(this); painter->beginNativePainting(); // glEnable(GL_POINT_SPRITE); glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); program.link(); program.bind(); // glViewport(0, 0, this->width(), this->height()); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); drawAxes(); glViewport(MARGIN, MARGIN, this->width() - MARGIN * 2, this->height() - MARGIN * 2); glScissor(MARGIN, MARGIN, this->width() - MARGIN * 2, this->height() - MARGIN * 2); glEnable(GL_SCISSOR_TEST); // functions using native openGL functions here // ..... // glDisable(GL_SCISSOR_TEST); glDisable(GL_POINT_SPRITE); glDisable(GL_VERTEX_PROGRAM_POINT_SIZE); glDisable(GL_BLEND); program.disconnect(); painter->endNativePainting(); drawTicksValues(); painter->end(); }

    After adding paintEvent implementation, the openGL widget has to be now refreshed using widget->update().

    Hope it will be helpful for other people who are struggling with using both QPainter and native openGL functions, as Qt doc seems to be a little shallow on this topic.

    Thanks!
    Adam