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:
  1. struct Texture
  2. {
  3. GLuint id;
  4. unsigned char* buf;
  5. };
  6.  
  7. tex.buf = new unsigned char[800*600];
  8. for(int i = 0; i < 800*600 - 1; i++)
  9. 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:
  1. void GlWidget::createTextureFromBitmap(QByteArray bytes)
  2. {
  3. /* create a 800 bye 600 texture from bitmap */
  4.  
  5. // tex.buf = new unsigned char[bytes.size()];
  6.  
  7. // memcpy(tex.buf, bytes.constData(), bytes.size());
  8.  
  9. tex.id = 1;
  10.  
  11. glBindTexture(GL_TEXTURE_2D, tex.id);
  12. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  13. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  14. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  15. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
  16. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  17.  
  18. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
  19.  
  20. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, tex.buf);
  21.  
  22. // delete [] tex.buf;
  23.  
  24. // tex.buf = NULL;
  25.  
  26. updateGL();
  27. }
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:
  1. void GlWidget::paintGL()
  2. {
  3. //! [5]
  4. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  5.  
  6. QMatrix4x4 mMatrix;
  7. QMatrix4x4 vMatrix;
  8.  
  9. QMatrix4x4 cameraTransformation;
  10. // cameraTransformation.rotate(alpha, 0, 1, 0);
  11. // cameraTransformation.rotate(beta, 1, 0, 0);
  12.  
  13. QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
  14. QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
  15.  
  16. vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
  17.  
  18. //! [6]
  19. shaderProgram.bind();
  20. shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
  21. shaderProgram.setUniformValue("texture", 0);
  22.  
  23.  
  24. glActiveTexture(GL_TEXTURE0);
  25. glBindTexture(GL_TEXTURE_2D, tex.id);
  26. glActiveTexture(0);
  27.  
  28.  
  29. shaderProgram.setAttributeArray("vertex", vertices.constData());
  30. shaderProgram.enableAttributeArray("vertex");
  31. shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
  32. shaderProgram.enableAttributeArray("textureCoordinate");
  33.  
  34. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  35.  
  36. shaderProgram.disableAttributeArray("vertex");
  37. shaderProgram.disableAttributeArray("textureCoordinate");
  38. shaderProgram.release();
  39. }
To copy to clipboard, switch view to plain text mode