Tried to refactor OpenGL demo slightly (and failed)
Unsolved
General and Desktop
-
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(); }
-
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.