Results 1 to 15 of 15

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

  1. #1
    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 how to bind a bitmap to texture in QT?

    Does anyone know how to bind a bitmap raw data to texture in gl? I checked qt examples, but couldn't find such example.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    NeHe Productions: Texture Mapping
    Source code is attached at the bottom of that page.
    Since you already have a bitmap raw data, you can skip the section about loading a bitmap.

  3. #3
    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?

    I wrote the following code snippet to bind a bitmap to texture. I got an ordinary buffer filled it with some values and then assigned it as a parameter to create a 800 * 600 texture. However, whatever values I assign to buffer, the screen remains white with some black tracks, n sometimes becomes awful. I don't where I am doing wrong:

    Qt Code:
    1. struct Texture
    2. {
    3. GLuint id;
    4. unsigned char* buf;
    5. };
    6.  
    7. tex.buf = new unsigned char[800*600];
    8. for(int i = 0; i < 800*600 - 1; i++)
    9. tex.buf[i] = 0;
    To copy to clipboard, switch view to plain text mode 


    and here I create the texture using the above buffer:

    Qt Code:
    1. void GlWidget::createTextureFromBitmap(QByteArray bytes)
    2. {
    3. /* create a 800 bye 600 texture from bitmap */
    4.  
    5. // tex.buf = new unsigned char[bytes.size()];
    6.  
    7. // memcpy(tex.buf, bytes.constData(), bytes.size());
    8.  
    9. tex.id = 1;
    10.  
    11. glBindTexture(GL_TEXTURE_2D, tex.id);
    12. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    13. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    14. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    15. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    16. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    17.  
    18. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
    19.  
    20. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
    21.  
    22. // delete [] tex.buf;
    23.  
    24. // tex.buf = NULL;
    25.  
    26. updateGL();
    27. }
    To copy to clipboard, switch view to plain text mode 

    in the painGL function, I also render texture to the screen using the commands below:

    Qt Code:
    1. void GlWidget::paintGL()
    2. {
    3. //! [5]
    4. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    5.  
    6. QMatrix4x4 mMatrix;
    7. QMatrix4x4 vMatrix;
    8.  
    9. QMatrix4x4 cameraTransformation;
    10. // cameraTransformation.rotate(alpha, 0, 1, 0);
    11. // cameraTransformation.rotate(beta, 1, 0, 0);
    12.  
    13. QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    14. QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
    15.  
    16. vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
    17.  
    18. //! [6]
    19. shaderProgram.bind();
    20. shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    21. shaderProgram.setUniformValue("texture", 0);
    22.  
    23.  
    24. glActiveTexture(GL_TEXTURE0);
    25. glBindTexture(GL_TEXTURE_2D, tex.id);
    26. glActiveTexture(0);
    27.  
    28.  
    29. shaderProgram.setAttributeArray("vertex", vertices.constData());
    30. shaderProgram.enableAttributeArray("vertex");
    31. shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
    32. shaderProgram.enableAttributeArray("textureCoordinate");
    33.  
    34. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    35.  
    36. shaderProgram.disableAttributeArray("vertex");
    37. shaderProgram.disableAttributeArray("textureCoordinate");
    38. shaderProgram.release();
    39. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    Qt Code:
    1. tex.buf = new unsigned char[800*600];
    2. for(int i = 0; i < 800*600 - 1; i++)
    3. tex.buf[i] = 0;
    To copy to clipboard, switch view to plain text mode 
    Have you ever heard about memset ? Anyway, if you fill this buffer with texture values later, there is no need to zero it, especially byte by byte in a loop.

    Qt Code:
    1. tex.id = 1;
    2.  
    3. glBindTexture(GL_TEXTURE_2D, tex.id);
    To copy to clipboard, switch view to plain text mode 
    This is wrong, you can't create your own texture id values out of thin air, use glGenTextures.

    About the rest of this code - i doubt this is your "production" code, because some of the most important function calls are commented out. Post the "real" code, because this code can't work for obvious reasons (like removed copying of the image buffer values).

  5. #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.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    Ok, so I'd start with debugging if the data you receive is really a valid image data. Try to use QImage to save it to a file.

  7. #7
    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?

    Quote Originally Posted by stampede View Post
    Ok, so I'd start with debugging if the data you receive is really a valid image data. Try to use QImage to save it to a file.
    good point to start with. raw data has no header righ? this is why I keep getting false from loadFromData(QByteA...) function. It seems I cannot create QImage::format_RGB32(SIGSEGV), the only option which worked was Format_Indexed8. Hence:
    Qt Code:
    1. QImage image((const uchar*)bytes.constData(), 800, 600, QImage::Format_Indexed8);
    2. bool flag = image.save("/home/saman/image.png", "PNG");
    3. qDebug() << flag;
    To copy to clipboard, switch view to plain text mode 

    However, the image which is saved is strange.
    Last edited by saman_artorious; 18th November 2013 at 07:09.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    raw data has no header righ? this is why I keep getting false from loadFromData(QByteA...) function
    I don't know what is the format of data you receive, but if it has no header, and you "know" what format it should be, you can use one of the QImage constructors that accepts data pointer, image size and format.

    --
    I simply receive a bitmap from socket and then try to create a texture out of it
    when do you exactly start creating the texture ? after you have buffered enough data read from the socket, or on every "readyRead" signal ?
    Last edited by stampede; 18th November 2013 at 07:21.

  9. #9
    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?

    I updated my post above. yes, I acquire the buffer whenever all 800 by 600 elements are read successfully.

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    What is the format of the image on server side ? Previously you tried
    GL_RGB
    data format to bind a texture, but later you tried QImage::format_RGB32 too. So which one is it ? How many bytes per pixel ? What data order (bgr or rgb, row major or column major) ?
    Btw. what is the bytes.size() ? In case of 800x600 24 bit rgb image data, size should be 1 440 000 bytes.

  11. #11
    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?

    Quote Originally Posted by stampede View Post
    What is the format of the image on server side ? Previously you tried data format to bind a texture, but later you tried QImage::format_RGB32 too. So which one is it ? How many bytes per pixel ? What data order (bgr or rgb, row major or column major) ?
    Btw. what is the bytes.size() ? In case of 800x600 24 bit rgb image data, size should be 1 440 000 bytes.
    i received SIGSEGV with GL_RGB, and it cannot be RGB because the other side sends me color indexes. what he does is to use Dib function in MS which returns him in BGR and not RGB. I was advised to use a function like GetClrTabAddress in MS to find the RGB values for each of the indexes send as bytes to me.
    Yes what the other side sends is 800 by 600, HOwever, If I get the values of each index then it is multiplied by 3.
    besides, I ignored the texture method for now, instead I simply want to call glDrawPixels. at least this may solve some basic problems for now.

  12. #12
    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?

    As far as it is relevant to the current problem:

    I want to get RGB values from a 24 bit BGR data. This data is from a raw image without a header.
    I don't know how to convert the index of the buffer to its corresponding RGB values.
    here is what I have done:

    Qt Code:
    1. window_width = 800;
    2.  
    3. window_height = 600;
    4.  
    5. size = window_width * window_height;
    6.  
    7. pixels = new float[size*3];
    8.  
    9. for(int i = 0, j = 0; i < size*3, j < size; i += 3, j++)
    10. {
    11. pixels[i+2] = bytes[i];
    12. }
    13.  
    14. updateGL();
    To copy to clipboard, switch view to plain text mode 


    void GlWidget:aintGL()
    {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDrawPixels(window_width,window_height,GL_RGB,GL_ FLOAT,pixels);
    }

    what am i missing?

  13. #13
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    I want to get RGB values from a 24 bit BGR data.
    Are there alignment bytes added to BGR data ? Not relevant in case if width is a multiple of 4, but in general you can have each row size padded to multiple of 4 (in bytes). For example opencv does that (so size of 6x6 24 bit rgb image is not 108 but 120 bytes, additional 2 padding bytes for each row).
    Ok sorry I'm probably confusing you, so the problem is this:

    you have - [ B1,G1,R1, B2,G2,R2, ..., Bn,Gn,Rn ]
    and you want - [ R1,G1,B1, R2,G2,B2, ..., Rn,Gn,Bn ]
    ?
    You can do that conversion in-place, if you have buffered the data from the socket, you can change it without need of another buffer.
    As you can see, you can leave the middle byte in each pixel alone, so you can do a simle swap (in case of in-place conversion), or three assignments (in case of copying). No need to have two loop variables btw., you can do that in single pass. This is three-four lines of code but i won't write it. Get a piece of paper and try to first perform this algorithm manually, its not that hard.
    Last edited by stampede; 18th November 2013 at 13:05. Reason: updated contents

  14. #14
    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 you do not understand what the problem is. I know how to do a simple swap of these bits.
    The packet size of raw data sent by server is of size 800*600 (size) only. Because the server gets the indices of RGB values. So, your equality is not correct. What I need to do logically, is to store the received packet in a packet of size size * 3 and assign values to it! but assign values to what? R? G? B? I have no idea because the received packet only contains indices of these RGB values. the below function only gave me some mixed colors:

    Qt Code:
    1. void GlWidget::func(QByteArray btmp)
    2. {
    3. bytes.clear();
    4.  
    5. bytes.resize(size*3*sizeof(unsigned char));
    6.  
    7. for(int i = 0, j = 0; i < size*3, j < size; i += 3, j++)
    8. {
    9. bytes[i+2] = btmp[i];
    10. }
    11.  
    12. updateGL();
    13. }
    14. void GlWidget::paintGL()
    15. {
    16.  
    17. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    18. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    19. glDrawPixels(window_width,window_height,GL_BGR,GL_UNSIGNED_BYTE,bytes.constData());
    20. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    received packet only contains indices of these RGB values
    so you have indices but don't have a color table ? that's nice, to be honest I don't know what you should do in that case apart from asking the creators of server side to give you the color tables they use

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.