PDA

View Full Version : OpenGL Texture Loading Problem



Atomic_Sheep
17th May 2012, 17:27
Hi guys, I started playing around with OpenGL and initially, I was getting the message box popping up in the LoadGLWidget function call but I changed the type to void over int and that stopped happening... so by the looks of it, the image started to load without a problem. The problem however is that despite no error message box popping up, I see the coloured box, rather than having that box filled with my test image (both can be seen in the attachment. Any ideas as to what I'm doing wrong?


#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}

void GLWidget::initializeGL()
{
glEnable(GL_TEXTURE_2D);
glClearColor(1,1,0.5,1);

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}

//Setup viewport, projection etc...
void GLWidget::resizeGL(int w, int h)
{

if (h == 0)
{
h = 1;
}
}

//Draw the Scene
void GLWidget::paintGL() //corresponds to DrawGLScene
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//glTranslatef(x, y, z)

//glTranslatef(0.0f,0.0f,-10.0f);

glBindTexture(GL_TEXTURE_2D, texture[0]);

glBegin(GL_QUADS);
//Bottom Left
glColor3f(0.5,0.5,0.5);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);

//Top Left
glColor3f(0.2,0.7,0);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-1.0f,1.0f);

//Top Right
glColor3f(0.9,0.1,0.5);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f,1.0f);

//Bottom Right
glColor3f(0.6,0.6,0.6);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f,-1.0f);
glEnd();

/*
glBegin(GL_TRIANGLES);
glColor3f(0,1,0.7);
glVertex3f(-1,0,0);
glColor3f(0,0.1,0.5);
glVertex3f(1,0,0);
glColor3f(0.9,0.1,0);
glVertex3f(0,1,0);
glEnd();
*/

}

void GLWidget::LoadGLTexture()
{
QImage GLFormatImage;
QImage QFormatImage;

if (!QFormatImage.load("actual path to the file with .bmp format"))
{
QMessageBox::information(this, "Error", "Could not load image");
}

GLFormatImage = QGLWidget::convertToGLFormat(QFormatImage);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
7734
7733

Talei
19th May 2012, 00:04
Your texture definition is wrong. In glTexImage2D is a error. You pass 3bytes, 24bits per pixel (3), but Your data has 4bytes, 32 bits per pixel (GL_RGBA):


glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
it should be either:

glTexImage2D(GL_TEXTURE_2D, 0, 4, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
// or
glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, GLFormatImage.bits());

Atomic_Sheep
22nd May 2012, 03:57
I tried playing around with those settings, still not working but I'll have to read a little bit about image formats it seems. Don't fully understand all the various formats that are possible.

Talei
22nd May 2012, 05:43
Is that Your entire code, that you posted in first post, for GLWidget? or did You truncated it?
If yes, then You are missing a lot of important stuff, i.e. model/view matrix definition etc...

Atomic_Sheep
22nd May 2012, 12:43
Yep, that's pretty much all the code I've got so far... I'm just following along with NeHe's tutorial and a Qt version of it that I found elsewhere. Hmm... I'll have to look over what I might have missed in that case. What do you mean by model and view matrix definition? I've provided the 2D quad that I want to map the texture to, isn't that the model?

P.S. After reviewing NeHe's tutorial again, I'm still failing to see what I might have missed.

I also made sure the image was a power of 2 dimension... not necessary I know but just in case.

Finally I read about the image formats... and according to what I read:


glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, GLFormatImage.bits());

should be right... it's 24 bit which I assume to be per pixel and that also suggests to me at least that it's RGB and not RGBA... although in the attributes it does have A, but I couldn't find out what that meant.

Talei
22nd May 2012, 19:04
GL_RGB = GL Red Green Blue
GL_RGBA GL Red Green Blue Alpha

BMP don't support Alpha, so only mistake that You coukd make is with BPP (bits per pixel), i.e. is BMP 24bit.

Matrix'es are to describe projection (how to draw 3D scene into 2D image) and model (scene scales etc..)

I just looked at first Nehe tutorial and they create matrixes, so I don't know with tutorial You fallowed.
On nehe www they have also Qt project for (probably) each tutorial so I suggest to take a look at them.

Atomic_Sheep
23rd May 2012, 05:37
Thanks for the advice, reviewed a lot of NeHe's tutorials... I finally got the texture to load, but it's not as I expected it to be, but I'll try to figure it out, don't have any ideas as to what is going wrong at the moment but hopefully after reviewing the graphics pipeline I'll figure it out. Well I have an idea of where in the graphics pipeline the problem lies, that's about the extent of my understanding of the problem.

catchxiaoshuang
10th January 2013, 14:26
how do you solved this problem.
please post out your answer here, Thanks

Added after 35 minutes:

oh,i know the answer
you must call your LoadGLTexture function in initializeGL or where you really need a texture
i.m a qt beginner.

0_Azerty_0
22nd May 2013, 13:51
Hi,

I would be really pleased if you could post your answer. I have the same probleme and both equivalents of :
glTexImage2D(GL_TEXTURE_2D, 0, 4, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, GLFormatImage.bits());
// or
glTexImage2D(GL_TEXTURE_2D, 0, 3, GLFormatImage.width(), GLFormatImage.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, GLFormatImage.bits()); doesnt work.

Call of "QGLWidget::convertToGLFormat" crash whenever !

saman_artorious
23rd May 2013, 08:17
Hi,

I would be really pleased if you could post your answer. I have the same probleme and both equivalents of : doesnt work.

Call of "QGLWidget::convertToGLFormat" crash whenever !

are you using BMP or bits to load the image? Could you post your code here. The whole code.
if it's not necessarily BMP, you may use

texture = bindTexture(QPixmap(":/texture.png"));
to load the texture from a file.

On the other hand, if you want to load a dynamic texture, then use FBO,


fbo->bind();

//Render something here and load it later as the texture

fbo->release();

dynamicTexture = fbo->texture();


for fbo, you may read the documentation for QGLFramebufferObject.

If you still have problem debugging paste the whole code.