Results 1 to 5 of 5

Thread: Strange error with QGLFunctions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.