PDA

View Full Version : creating texture from RGB raw data



saman_artorious
30th December 2013, 14:20
I am receiving an screen-shot Palette over the network. Next, I convert the Palette indices to their relevant RGB using my matrix containing different colors.
The problem I have with the code is I cannot create a texture out of this rgb image I receive successfully. I would thank if you inform me if I am making a mistake anywhere in the code.


void GlWidget::createTextureFromBitmap(QByteArray btmp)
{
//==============================================
//********** currently using ***************
//==============================================
bytes.clear();
bytes.resize(size * sizeof(unsigned char) * 3);
bytes = btmp;
int iRow = 0, iCol = 0;
for(int i = 2; i < 800*600; i++)
{
iCol = i % 800;
iRow = i / 800;

bytes[(800-iCol)*1800 + 3 * iRow ] = rgbPaletteSystem[btmp[i]][0] ;
bytes[(800-iCol)*1800 + 3 * iRow - 1] = rgbPaletteSystem[btmp[i]][2] ;
bytes[(800-iCol)*1800 + 3 * iRow - 2] = rgbPaletteSystem[btmp[i]][1];
}
// updateGL();
//==============================================
//********** currently using ***************
//==============================================


//==========================================
//********** creating texture *********
//=====================================
/* 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_3_BYTES, tex.buf);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_3_BYTES, tex.buf);
delete [] tex.buf;
tex.buf = NULL;
updateGL();
}

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

wysota
30th December 2013, 14:42
How do you know it is the texture that is not correct and not your shader program? What exactly do you get as output of your program?