Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Tried to refactor OpenGL demo slightly (and failed)

Tried to refactor OpenGL demo slightly (and failed)

Scheduled Pinned Locked Moved Unsolved General and Desktop
opengl
2 Posts 2 Posters 765 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hydrodog
    wrote on last edited by hydrodog
    #1

    I took the OpenGL triangle demo and tried to modify it to make the constructor of the triangle class do much more, so there are fewer calls from main. In the process, I have a runtime error:

    //QOpenGLFunctions created with non-current context
    

    Here is the code. I think I preserved the order of what happened from the original code. What am I doing wrong?

    class TriangleWindow : public OpenGLWindow {
    private:
        const char* vertexShaderSource;
        const char* fragmentShaderSource;
        GLuint m_posAttr;
        GLuint m_colAttr;
        GLuint m_matrixUniform;
    
        QOpenGLShaderProgram *m_program;
        int m_frame;
        GLuint loadShader(GLenum type, const char *source) {
            GLuint shader = glCreateShader(type);
            glShaderSource(shader, 1, &source, 0);
            glCompileShader(shader);
            return shader;
        }
    public:
        TriangleWindow(const char* vertexShaderSource, const char* fragmentShaderSource) :
            m_frame(0), vertexShaderSource(vertexShaderSource),  fragmentShaderSource(fragmentShaderSource){
            initialize();
            resize(640, 480);
        }
    
        void initialize() Q_DECL_OVERRIDE {
            m_program = new QOpenGLShaderProgram(this);
            m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
            m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
            m_program->link();
            m_posAttr = m_program->attributeLocation("posAttr");
            m_colAttr = m_program->attributeLocation("colAttr");
            m_matrixUniform = m_program->uniformLocation("matrix");
            QSurfaceFormat format;
            format.setSamples(16);
            setFormat(format);
        }
        void render() Q_DECL_OVERRIDE;
    };
    
    //! [1]
    
    //! [2]
    int main(int argc, char **argv)
    {
        QGuiApplication app(argc, argv);
    
        static const char *vertexShaderSource =
            "attribute highp vec4 posAttr;\n"
            "attribute lowp vec4 colAttr;\n"
            "varying lowp vec4 col;\n"
            "uniform highp mat4 matrix;\n"
            "void main() {\n"
            "   col = colAttr;\n"
            "   gl_Position = matrix * posAttr;\n"
            "}\n";
    
        static const char *fragmentShaderSource =
            "varying lowp vec4 col;\n"
            "void main() {\n"
            "   gl_FragColor = col;\n"
            "}\n";
    
        TriangleWindow window(vertexShaderSource, fragmentShaderSource);
        window.show();
    
        window.setAnimating(true);
        return app.exec();
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not let initialize get called like the original code does it ?

      When your TriangleWindow gets constructed you don't have a context created yet.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved