Does anyone know how to bind a bitmap raw data to texture in gl? I checked qt examples, but couldn't find such example.
Does anyone know how to bind a bitmap raw data to texture in gl? I checked qt examples, but couldn't find such example.
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.
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:
struct Texture { GLuint id; unsigned char* buf; }; tex.buf = new unsigned char[800*600]; for(int i = 0; i < 800*600 - 1; i++) 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:
{ /* create a 800 bye 600 texture from bitmap */ // tex.buf = new unsigned char[bytes.size()]; // memcpy(tex.buf, bytes.constData(), bytes.size()); tex.id = 1; glBindTexture(GL_TEXTURE_2D, tex.id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.buf); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, tex.buf); // delete [] tex.buf; // tex.buf = NULL; updateGL(); }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:
void GlWidget::paintGL() { //! [5] glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; // cameraTransformation.rotate(alpha, 0, 1, 0); // cameraTransformation.rotate(beta, 1, 0, 0); QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection); //! [6] shaderProgram.bind(); shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix); shaderProgram.setUniformValue("texture", 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex.id); glActiveTexture(0); shaderProgram.setAttributeArray("vertex", vertices.constData()); shaderProgram.enableAttributeArray("vertex"); shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData()); shaderProgram.enableAttributeArray("textureCoordinate"); glDrawArrays(GL_TRIANGLES, 0, vertices.size()); shaderProgram.disableAttributeArray("vertex"); shaderProgram.disableAttributeArray("textureCoordinate"); shaderProgram.release(); }To copy to clipboard, switch view to plain text mode
Have you ever heard about memset ?Qt Code:
tex.buf = new unsigned char[800*600]; for(int i = 0; i < 800*600 - 1; i++) tex.buf[i] = 0;To copy to clipboard, switch view to plain text modeAnyway, if you fill this buffer with texture values later, there is no need to zero it, especially byte by byte in a loop.
This is wrong, you can't create your own texture id values out of thin air, use glGenTextures.Qt Code:
tex.id = 1; glBindTexture(GL_TEXTURE_2D, tex.id);To copy to clipboard, switch view to plain text mode
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).
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:
void GlWidget::initializeGL() { initCommon(); shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh"); shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh"); if(!shaderProgram.link()) { exit(1); } vertices << QVector3D(-1, -1, 1) << QVector3D( 1, -1, 1) << QVector3D( 1, 1, 1) // Front << QVector3D( 1, 1, 1) << QVector3D(-1, 1, 1) << QVector3D(-1, -1, 1); textureCoordinates << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1) // Front << QVector2D(0, 1) << QVector2D(0, 0) << QVector2D(1, 0); updateGL(); } //! [1] //############################ { /* create a 800 bye 600 texture from bitmap */ tex.buf = new unsigned char[bytes.size()]; memcpy(tex.buf, bytes.constData(), bytes.size()); glGenTextures( 1, &tex.id); glBindTexture(GL_TEXTURE_2D, tex.id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.buf); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, tex.buf); delete [] tex.buf; tex.buf = NULL; updateGL(); } void GlWidget::paintGL() { //! [5] glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; // cameraTransformation.rotate(alpha, 0, 1, 0); // cameraTransformation.rotate(beta, 1, 0, 0); QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection); //! [6] shaderProgram.bind(); shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix); shaderProgram.setUniformValue("texture", 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex.id); glActiveTexture(0); shaderProgram.setAttributeArray("vertex", vertices.constData()); shaderProgram.enableAttributeArray("vertex"); shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData()); shaderProgram.enableAttributeArray("textureCoordinate"); glDrawArrays(GL_TRIANGLES, 0, vertices.size()); shaderProgram.disableAttributeArray("vertex"); shaderProgram.disableAttributeArray("textureCoordinate"); shaderProgram.release(); }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:
before this error, the widget looks some garbage colors.gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
Last edited by saman_artorious; 17th November 2013 at 13:56.
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:
bool flag = image.save("/home/saman/image.png", "PNG"); 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.
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.raw data has no header righ? this is why I keep getting false from loadFromData(QByteA...) function
--
when do you exactly start creating the texture ? after you have buffered enough data read from the socket, or on every "readyRead" signal ?I simply receive a bitmap from socket and then try to create a texture out of it
Last edited by stampede; 18th November 2013 at 07:21.
I updated my post above. yes, I acquire the buffer whenever all 800 by 600 elements are read successfully.
What is the format of the image on server side ? Previously you trieddata 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) ?GL_RGB
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.
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:
window_width = 800; window_height = 600; size = window_width * window_height; pixels = new float[size*3]; for(int i = 0, j = 0; i < size*3, j < size; i += 3, j++) { pixels[i+2] = bytes[i]; } 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?
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).I want to get RGB values from a 24 bit BGR data.
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
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:
{ bytes.clear(); bytes.resize(size*3*sizeof(unsigned char)); for(int i = 0, j = 0; i < size*3, j < size; i += 3, j++) { bytes[i+2] = btmp[i]; } updateGL(); } void GlWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glDrawPixels(window_width,window_height,GL_BGR,GL_UNSIGNED_BYTE,bytes.constData()); }To copy to clipboard, switch view to plain text mode
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 usereceived packet only contains indices of these RGB values
Bookmarks