PDA

View Full Version : how to bind a bitmap to texture in QT?



saman_artorious
13th November 2013, 10:39
Does anyone know how to bind a bitmap raw data to texture in gl? I checked qt examples, but couldn't find such example.

stampede
13th November 2013, 12:07
NeHe Productions: Texture Mapping (http://nehe.gamedev.net/tutorial/texture_mapping/12038/)
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.

saman_artorious
17th November 2013, 13:24
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:



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;



and here I create the texture using the above buffer:



void GlWidget::createTextureFromBitmap(QByteArray bytes)
{
/* 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();
}


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



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();
}

stampede
17th November 2013, 13:49
tex.buf = new unsigned char[800*600];
for(int i = 0; i < 800*600 - 1; i++)
tex.buf[i] = 0;
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.


tex.id = 1;

glBindTexture(GL_TEXTURE_2D, tex.id);
This is wrong, you can't create your own texture id values out of thin air, use glGenTextures (http://www.opengl.org/sdk/docs/man/xhtml/glGenTextures.xml).

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).

saman_artorious
17th November 2013, 14:35
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.



void GlWidget::initializeGL()
{
initCommon();

shaderProgram.addShaderFromSourceFile(QGLShader::V ertex, ":/vertexShader.vsh");

shaderProgram.addShaderFromSourceFile(QGLShader::F ragment, ":/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]

//############################

void GlWidget::createTextureFromBitmap(QByteArray bytes)
{
/* 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();
}


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.

stampede
17th November 2013, 17:08
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.

saman_artorious
18th November 2013, 07:39
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:


QImage image((const uchar*)bytes.constData(), 800, 600, QImage::Format_Indexed8);
bool flag = image.save("/home/saman/image.png", "PNG");
qDebug() << flag;

However, the image which is saved is strange.

stampede
18th November 2013, 08:08
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 (http://qt-project.org/doc/qt-5.0/qtgui/qimage.html#QImage-4).

--

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 ?

saman_artorious
18th November 2013, 08:23
I updated my post above. yes, I acquire the buffer whenever all 800 by 600 elements are read successfully.

stampede
18th November 2013, 08:51
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.

saman_artorious
18th November 2013, 10:23
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.

saman_artorious
18th November 2013, 13:14
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:



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();



void GlWidget::paintGL()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

what am i missing?

stampede
18th November 2013, 13:56
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.

saman_artorious
18th November 2013, 14:29
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:



void GlWidget::func(QByteArray btmp)
{
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());
}

stampede
18th November 2013, 14:48
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