PDA

View Full Version : create texture from bitmap



saman_artorious
11th January 2014, 08:00
I wan to create texture from image rgb data. When the texture is rendered to the screen it shows nothing. I was just wondering if I missed something in the implementation:




void GlWidget::createTextureFromBitmap(QByteArray btmp)
{
/* create a 800 bye 600 * 3 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()
{
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
11th January 2014, 08:12
If it shows nothing then maybe your drawing code is invalid. Does it render correctly if you replace the texture with a solid color?

saman_artorious
11th January 2014, 10:34
If it shows nothing then maybe your drawing code is invalid. Does it render correctly if you replace the texture with a solid color?

It is fine now. I needed to adjust the camera position.

wysota
11th January 2014, 16:19
A GL texture is perfectly scalable, it is not composed of pixels.