Results 1 to 15 of 15

Thread: how to bind a bitmap to texture in QT?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to bind a bitmap to texture in QT?

    Stampede, thanks for your comment, Yes, I shoudav generated the texture name before binding it to the current texture GL_TEXTURE_2D.
    This is the main code, I simply receive a bitmap from socket and then try to create a texture out of it.

    Qt Code:
    1. void GlWidget::initializeGL()
    2. {
    3. initCommon();
    4.  
    5. shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
    6.  
    7. shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
    8.  
    9. if(!shaderProgram.link())
    10. {
    11. exit(1);
    12. }
    13.  
    14. vertices << QVector3D(-1, -1, 1) << QVector3D( 1, -1, 1) << QVector3D( 1, 1, 1) // Front
    15. << QVector3D( 1, 1, 1) << QVector3D(-1, 1, 1) << QVector3D(-1, -1, 1);
    16.  
    17. textureCoordinates
    18. << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1) // Front
    19. << QVector2D(0, 1) << QVector2D(0, 0) << QVector2D(1, 0);
    20.  
    21. updateGL();
    22. }
    23. //! [1]
    24.  
    25. //############################
    26.  
    27. void GlWidget::createTextureFromBitmap(QByteArray bytes)
    28. {
    29. /* create a 800 bye 600 texture from bitmap */
    30.  
    31. tex.buf = new unsigned char[bytes.size()];
    32.  
    33. memcpy(tex.buf, bytes.constData(), bytes.size());
    34.  
    35. glGenTextures( 1, &tex.id);
    36. glBindTexture(GL_TEXTURE_2D, tex.id);
    37.  
    38. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    39. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    40. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    41. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    42. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    43.  
    44. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
    45.  
    46. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
    47.  
    48. delete [] tex.buf;
    49.  
    50. tex.buf = NULL;
    51.  
    52. updateGL();
    53. }
    54.  
    55.  
    56.  
    57. void GlWidget::paintGL()
    58. {
    59. //! [5]
    60. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    61.  
    62. QMatrix4x4 mMatrix;
    63. QMatrix4x4 vMatrix;
    64.  
    65. QMatrix4x4 cameraTransformation;
    66. // cameraTransformation.rotate(alpha, 0, 1, 0);
    67. // cameraTransformation.rotate(beta, 1, 0, 0);
    68.  
    69. QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    70. QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
    71.  
    72. vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
    73.  
    74. //! [6]
    75. shaderProgram.bind();
    76. shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    77. shaderProgram.setUniformValue("texture", 0);
    78.  
    79.  
    80. glActiveTexture(GL_TEXTURE0);
    81. glBindTexture(GL_TEXTURE_2D, tex.id);
    82. glActiveTexture(0);
    83.  
    84.  
    85. shaderProgram.setAttributeArray("vertex", vertices.constData());
    86. shaderProgram.enableAttributeArray("vertex");
    87. shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
    88. shaderProgram.enableAttributeArray("textureCoordinate");
    89.  
    90. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    91.  
    92. shaderProgram.disableAttributeArray("vertex");
    93. shaderProgram.disableAttributeArray("textureCoordinate");
    94. shaderProgram.release();
    95. }
    To copy to clipboard, switch view to plain text mode 

    I tested the program again, it receives a few packets and then gives segmentation fault at line:
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
    before this error, the widget looks some garbage colors.
    Last edited by saman_artorious; 17th November 2013 at 13:56.

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2015, 11:20
  2. QImage bind texture fails
    By saman_artorious in forum Qt Programming
    Replies: 7
    Last Post: 22nd September 2013, 16:44
  3. [QGLWidget] Cannot bind texture
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2009, 19:53
  4. Bitmap border
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 14th May 2008, 18:37
  5. Bitmap buttons
    By BloodHo in forum Newbie
    Replies: 1
    Last Post: 4th November 2006, 14:17

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
  •  
Qt is a trademark of The Qt Company.