Results 1 to 5 of 5

Thread: Strange error with QGLFunctions

  1. #1
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Strange error with QGLFunctions

    So I'm trying to make a QGLWidget function with Qt Creator and I'm running into a strange issue trying to use regular OpenGL functions. My program ASSERTS and crashes with: "QGLFunctions::isInitialized(d_ptr)" in "c:\work\build\qt5_workdir\w\s\qtbase\include\qtop engl\../../src/opengl/qglfunctions.h, line 722"

    I found this is called from the "glGenBuffers" call in my cpp code. Ill post the .h and .cpp here:

    myglwidget.h
    Qt Code:
    1. #ifndef MYGLWIDGET_H
    2. #define MYGLWIDGET_H
    3.  
    4. #include <QGLWidget>
    5. #include <QGLFunctions>
    6. #include <QGLShaderProgram>
    7.  
    8.  
    9. class MyGLWidget : public QGLWidget, protected QGLFunctions
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit MyGLWidget(QWidget *parent = 0);
    14. ~MyGLWidget();
    15.  
    16. protected:
    17. void initializeGL();
    18. void paintGL();
    19. void resizeGL(int width, int height);
    20.  
    21. private:
    22. int width;
    23. int height;
    24. GLuint back_texture;
    25. GLuint* vboid;
    26. QGLShaderProgram default_prog;
    27. };
    28.  
    29. #endif // MYGLWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    myglwidget.cpp
    Qt Code:
    1. #include "myglwidget.h"
    2. #include <QDebug>
    3. #include <QVector3D>
    4. #include <QVector2D>
    5. #include "locale.h"
    6.  
    7. #define BACK_TILE_S 48
    8.  
    9. struct VertexData
    10. {
    11. QVector3D position;
    12. QVector2D texCoord;
    13. };
    14.  
    15. MyGLWidget::MyGLWidget(QWidget *parent) :
    16. QGLWidget(parent)
    17. {
    18. glGenBuffers(2, vboid);
    19. }
    20.  
    21. MyGLWidget::~MyGLWidget(){
    22. glDeleteBuffers(2, vboid);
    23. }
    24.  
    25. void MyGLWidget::initializeGL(){
    26. VertexData verts[] = {
    27. {QVector3D(0, 0, 0), QVector2D(0, 0)},
    28. {QVector3D(1, 0, 0), QVector2D(1, 0)},
    29. {QVector3D(0, 1, 0), QVector2D(0, 1)},
    30. {QVector3D(1, 1, 0), QVector2D(1, 1)}
    31. };
    32. GLushort indices[] = {
    33. 0, 1, 2, 3
    34. };
    35.  
    36. setlocale(LC_NUMERIC, "C");
    37.  
    38. default_prog.addShaderFromSourceFile(QGLShader::Vertex, ":/shaders/default.vert");
    39. default_prog.addShaderFromSourceFile(QGLShader::Fragment, ":/shaders/default.frag");
    40. default_prog.link();
    41.  
    42. default_prog.bind(); // this is in initgl since this is the
    43. // only shader we use... FOR NOW, therefore
    44. // we dont have to do it elsewhere
    45. setlocale(LC_ALL, "");
    46.  
    47. glEnable(GL_TEXTURE_2D);
    48. // normally here we would have the QImage buffered up already
    49. // and would bind as needed, but again, this is the only
    50. // texture we use for now
    51. back_texture = bindTexture(QImage(":/gfx/gfx/back.png"));
    52. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    53. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    54.  
    55. glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
    56. glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(VertexData), verts, GL_STATIC_DRAW);
    57. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
    58. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLushort), indices, GL_STATIC_DRAW);
    59. }
    60.  
    61. void MyGLWidget::paintGL(){
    62. int texw;
    63. int texh;
    64. QMatrix4x4 proj;
    65. QMatrix2x2 scale;
    66. quintptr offset;
    67. int vertexLoc;
    68. int texcoordLoc;
    69.  
    70. texw = (floor(width / ((double) BACK_TILE_S)) + 1);
    71. texh = (floor(height / ((double) BACK_TILE_S)) + 1);
    72.  
    73. proj.setToIdentity();
    74. proj.scale(texw * BACK_TILE_S, texh * BACK_TILE_S);
    75. proj.ortho(0, width, 0, height, -1, 1);
    76.  
    77. scale.setToIdentity();
    78. scale(0,0) = texw;
    79. scale(1,1) = texh;
    80.  
    81. default_prog.setUniformValue("texScale", scale);
    82. default_prog.setUniformValue("mvp_matrix", proj);
    83. default_prog.setUniformValue("texture", 0);
    84.  
    85. glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
    86. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
    87.  
    88. offset = 0;
    89.  
    90. vertexLoc = default_prog.attributeLocation("a_position");
    91. default_prog.enableAttributeArray(vertexLoc);
    92. glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
    93.  
    94. offset += sizeof(QVector3D);
    95.  
    96. texcoordLoc = default_prog.attributeLocation("a_texcoord");
    97. default_prog.enableAttributeArray(texcoordLoc);
    98. glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
    99.  
    100. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
    101. }
    102.  
    103.  
    104. void MyGLWidget::resizeGL(int width, int height){
    105. this->width = width;
    106. this->height = height;
    107. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Strange error with QGLFunctions

    So I tried initQGLFunctions and it didn't do squat (now for something completely different!) so I switched to try using GLEW… now even MORE problems. It just crashes at glewInit. No reason just ends unexpectedly.
    Qt Code:
    1. #include <GL/glew64.h>
    2. #include "myglwidget.h"
    3. #include <QDebug>
    4. #include <QVector3D>
    5. #include <QVector2D>
    6. #include "locale.h"
    7.  
    8.  
    9. #define BACK_TILE_S 48
    10.  
    11. struct VertexData
    12. {
    13. QVector3D position;
    14. QVector2D texCoord;
    15. };
    16.  
    17. MyGLWidget::MyGLWidget(QWidget *parent) :
    18. QGLWidget(parent)
    19. {
    20.  
    21. }
    22.  
    23. MyGLWidget::~MyGLWidget(){
    24. //glDeleteBuffers(2, vboid);
    25. }
    26.  
    27. void MyGLWidget::initializeGL(){
    28. VertexData verts[] = {
    29. {QVector3D(0, 0, 0), QVector2D(0, 0)},
    30. {QVector3D(1, 0, 0), QVector2D(1, 0)},
    31. {QVector3D(0, 1, 0), QVector2D(0, 1)},
    32. {QVector3D(1, 1, 0), QVector2D(1, 1)}
    33. };
    34. GLushort indices[] = {
    35. 0, 1, 2, 3
    36. };
    37.  
    38. makeCurrent();
    39. glewExperimental = GL_TRUE;
    40.  
    41. int glewErr = glewInit();
    42. // if( glewErr != GLEW_OK )
    43. // {
    44. // qDebug("Error %s", glewGetErrorString(glewErr) ) ;
    45. // }
    46. /*
    47.  
    48.   setlocale(LC_NUMERIC, "C");
    49.  
    50.   default_prog.addShaderFromSourceFile(QGLShader::Vertex, ":/shaders/default.vert");
    51.   default_prog.addShaderFromSourceFile(QGLShader::Fragment, ":/shaders/default.frag");
    52.   default_prog.link();
    53.  
    54.   default_prog.bind(); // this is in initgl since this is the
    55.   // only shader we use... FOR NOW, therefore
    56.   // we dont have to do it elsewhere
    57.   setlocale(LC_ALL, "");
    58.  
    59.   glEnable(GL_TEXTURE_2D);
    60.   // normally here we would have the QImage buffered up already
    61.   // and would bind as needed, but again, this is the only
    62.   // texture we use for now
    63.   back_texture = bindTexture(QImage(":/gfx/gfx/back.png"));
    64.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    65.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    66.  
    67.   glGenBuffers(2, vboid);
    68.  
    69.   glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
    70.   glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(VertexData), verts, GL_STATIC_DRAW);
    71.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
    72.   glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLushort), indices, GL_STATIC_DRAW);
    73.   */
    74. }
    75.  
    76. void MyGLWidget::paintGL(){
    77. int texw;
    78. int texh;
    79. QMatrix4x4 proj;
    80. QMatrix2x2 scale;
    81. quintptr offset;
    82. int vertexLoc;
    83. int texcoordLoc;
    84.  
    85. makeCurrent();
    86.  
    87.  
    88. texw = (floor(width / ((double) BACK_TILE_S)) + 1);
    89. texh = (floor(height / ((double) BACK_TILE_S)) + 1);
    90. /*
    91.   proj.setToIdentity();
    92.   proj.scale(texw * BACK_TILE_S, texh * BACK_TILE_S);
    93.   proj.ortho(0, width, 0, height, -1, 1);
    94.  
    95.   scale.setToIdentity();
    96.   scale(0,0) = texw;
    97.   scale(1,1) = texh;
    98.  
    99.   default_prog.setUniformValue("texScale", scale);
    100.   default_prog.setUniformValue("mvp_matrix", proj);
    101.   default_prog.setUniformValue("texture", 0);
    102.  
    103.   glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
    104.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
    105.  
    106.   offset = 0;
    107.  
    108.   vertexLoc = default_prog.attributeLocation("a_position");
    109.   default_prog.enableAttributeArray(vertexLoc);
    110.   glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
    111.  
    112.   offset += sizeof(QVector3D);
    113.  
    114.   texcoordLoc = default_prog.attributeLocation("a_texcoord");
    115.   default_prog.enableAttributeArray(texcoordLoc);
    116.   glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
    117.  
    118.   glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
    119.   */
    120. }
    121.  
    122.  
    123. void MyGLWidget::resizeGL(int width, int height){
    124. this->width = width;
    125. this->height = height;
    126. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Strange error with QGLFunctions

    In your call to glGenBuffers you are passing an unitialised pointer to a function expecting to have a valid pointer. You need to allocate space for 2 GLuint objects and pass a pointer to that.

  4. #4
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Strange error with QGLFunctions

    Ah thanks, this alleviates a bug I haven't even gotten to yet (thought genBuffers returned the array). However in my initGL function glewInit is causing a crash with or without genBuffers.

  5. #5
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Strange error with QGLFunctions

    So I fixed this issue by extending out to QOpenGLFunctions instead of QGLFunctions and calling makeCurrent() and initializeOpenGLFunctions in initializeGL(). Now I've run into the issue that my OpenGL code seems to flop, as in it won't draw anything but a black screen. Any takers?

    Updated myglwidget.cpp
    Qt Code:
    1. #include "myglwidget.h"
    2. #include <QDebug>
    3. #include <QVector3D>
    4. #include <QVector2D>
    5. #include "locale.h"
    6.  
    7.  
    8. #define BACK_TILE_S 48
    9.  
    10. struct VertexData
    11. {
    12. QVector3D position;
    13. QVector2D texCoord;
    14. };
    15.  
    16. MyGLWidget::MyGLWidget(QWidget *parent) :
    17. QGLWidget(parent)
    18. {
    19.  
    20. }
    21.  
    22. MyGLWidget::~MyGLWidget(){
    23. glDeleteBuffers(2, vboid);
    24. }
    25.  
    26. void MyGLWidget::initializeGL(){
    27. VertexData verts[] = {
    28. {QVector3D(0, 0, 0), QVector2D(0, 0)},
    29. {QVector3D(1, 0, 0), QVector2D(1, 0)},
    30. {QVector3D(0, 1, 0), QVector2D(0, 1)},
    31. {QVector3D(1, 1, 0), QVector2D(1, 1)}
    32. };
    33. GLushort indices[] = {
    34. 0, 1, 2, 3
    35. };
    36. makeCurrent();
    37. initializeOpenGLFunctions();
    38.  
    39. setlocale(LC_NUMERIC, "C");
    40.  
    41. default_prog.addShaderFromSourceFile(QGLShader::Vertex, ":/shaders/default.vert");
    42. default_prog.addShaderFromSourceFile(QGLShader::Fragment, ":/shaders/default.frag");
    43. default_prog.link();
    44.  
    45. default_prog.bind(); // this is in initgl since this is the
    46. // only shader we use... FOR NOW, therefore
    47. // we dont have to do it elsewhere
    48. setlocale(LC_ALL, "");
    49.  
    50. glDisable(GL_DEPTH_TEST);
    51. glEnable(GL_TEXTURE_2D);
    52. // normally here we would have the QImage buffered up already
    53. // and would bind as needed, but again, this is the only
    54. // texture we use for now
    55. back_texture = bindTexture(QImage(":/gfx/gfx/back.png"));
    56. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    57. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    58. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    59. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    60.  
    61.  
    62. glGenBuffers(2, vboid);
    63.  
    64. glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
    65. glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(VertexData), verts, GL_STATIC_DRAW);
    66. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
    67. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLushort), indices, GL_STATIC_DRAW);
    68.  
    69. }
    70.  
    71. void MyGLWidget::paintGL(){
    72. int texw;
    73. int texh;
    74. QMatrix4x4 proj;
    75. QMatrix2x2 scale;
    76. quintptr offset;
    77. int vertexLoc;
    78. int texcoordLoc;
    79.  
    80. texw = (floor(width / ((double) BACK_TILE_S)) + 1);
    81. texh = (floor(height / ((double) BACK_TILE_S)) + 1);
    82.  
    83. proj.setToIdentity();
    84. proj.scale(texw * BACK_TILE_S, texh * BACK_TILE_S);
    85. proj.ortho(0, width, 0, height, -1, 1);
    86.  
    87. scale.setToIdentity();
    88. scale(0,0) = texw;
    89. scale(1,1) = texh;
    90.  
    91. default_prog.setUniformValue("texScale", scale);
    92. default_prog.setUniformValue("mvp_matrix", proj);
    93. default_prog.setUniformValue("texture", 0);
    94.  
    95. glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
    96. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
    97.  
    98. offset = 0;
    99.  
    100. vertexLoc = default_prog.attributeLocation("a_position");
    101. default_prog.enableAttributeArray(vertexLoc);
    102. glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
    103.  
    104. offset += sizeof(QVector3D);
    105.  
    106. texcoordLoc = default_prog.attributeLocation("a_texcoord");
    107. default_prog.enableAttributeArray(texcoordLoc);
    108. glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
    109.  
    110. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
    111.  
    112. }
    113.  
    114.  
    115. void MyGLWidget::resizeGL(int width, int height){
    116. this->width = width;
    117. this->height = height;
    118. }
    To copy to clipboard, switch view to plain text mode 

    default.vert
    Qt Code:
    1. #version 150
    2.  
    3. uniform mat4x4 mvp_matrix;
    4. uniform mat2x2 texScale;
    5.  
    6. in vec4 a_position;
    7. in vec2 a_texcoord;
    8.  
    9. out vec2 v_texcoord;
    10.  
    11. void main() {
    12. v_texcoord = a_texcoord * texScale;
    13. gl_Position = mvp_matrix * a_position;
    14. }
    To copy to clipboard, switch view to plain text mode 

    default.frag
    Qt Code:
    1. #version 150
    2.  
    3. uniform sampler2D texture;
    4.  
    5. in vec2 v_texcoord;
    6.  
    7. void main()
    8. {
    9. gl_FragColor = texture2D(texture, v_texcoord);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Zamaster; 7th March 2014 at 15:14.

Similar Threads

  1. Replies: 0
    Last Post: 19th April 2012, 15:52
  2. Qt Learning... Strange error.
    By jokerjokerer in forum General Programming
    Replies: 0
    Last Post: 10th May 2009, 09:16
  3. Strange Error
    By keeperofthegrove in forum Newbie
    Replies: 4
    Last Post: 22nd October 2008, 14:16
  4. strange QT error out of nowhere
    By Penut in forum Qt Programming
    Replies: 5
    Last Post: 14th August 2008, 01:46
  5. Strange error
    By joseph in forum General Programming
    Replies: 3
    Last Post: 8th February 2008, 13:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.