PDA

View Full Version : bind a bitmap raw data as texture to gl



saman_artorious
11-12-2013, 04:54 AM
hi,
I am receiving a bitmap of size 800 by 600 with RGB 24 bits. When I receive this bitmap from the other machine, I need to bind it to gl widget.
what crossed my mind is to do the following, however I am not quite sure if it is correct:



initializeGLFunctions();

GLuint vboId;

glBindBuffer(GL_ARRAY_BUFFER, vboId);

glBufferSubData(GL_ARRAY_BUFFER, 0, 800 * 600 * 3 * sizeof(int), &buffer[0]);

glBindBuffer(GL_ARRAY_BUFFER, 0);


is this the right way to bind bitmap to gl?
I would also thank if you mention how to declare the vboId in the second line.

Ehsan Kamrani
11-12-2013, 06:25 AM
You may use glGenBuffers to generate a set of new buffer object names.

Why don't you bind bitmap data as a texture in OpenGL? GL_ARRAY_BUFFER is used to bind VBOs.



void bindTexture( Texture2d *t)
{
if( t == NULL)
return;
tex = t;

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_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->buf);
gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, tex->buf);
delete [] tex->buf;
tex->buf = NULL;
}

saman_artorious
11-17-2013, 04:21 AM
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();
}