[SOLVED]Qt OpenGL - Loading a texture after initialisation

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

[SOLVED]Qt OpenGL - Loading a texture after initialisation

Post by MrDeathNote »

I've been working with Qt for a while and i'm writing an editor with OpenGL. I've been wondering, do you guys know any way to load a texture after the widget has been initialised? I can't find anything on google. I've been deallocating the widget and creating a new one any time i need to change the texture but this seems like a really inefficient way to do it. Any ideas?
Last edited by MrDeathNote on Tue Jul 27, 2010 5:54 am, edited 1 time in total.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by MrDeathNote »

Since no one replied i assume no one has a solution?
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by dandymcgee »

I just spent a few minutes searching and you're right, there doesn't appear to be anything readily available. It seems it shouldn't be too hard, but I haven't played with QT much really. I think I'm going to check it out today, I've wanted to mess with it for a while. I'll let you know if I find anything useful.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by WSPSNIPER »

umm i can load the texture after the widget has been initlized but i cant do it if there are multiple GLWidgets in the scene :( i just do the old fashion texture loading like you would do with out qt. then i call glBindTexture not window->bindTexture() or window->drawTexture() i do it the old way and it works but ive tried everything to get this window thing working and im getting pissed lol

Code: Select all

QImage temp;
    if(!temp.load(name))
    {
        QFile file("log.txt"); file.open(QIODevice::WriteOnly); file.write("not loaded"); file.close();
    }
    image = QGLWidget::convertToGLFormat(temp);
    glGenTextures(1, &m_texture);

    glBindTexture(GL_TEXTURE_2D, m_texture);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D,0,4,image.width(),image.height(),0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());

that is basically how i do it
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by WSPSNIPER »

ok, i fixed the problem i was having and i thought i could pass some of my errors to see if any apply to you. first i did an repaint() on timeout with a QTimer and i never needed to do that because qt takes care of that with out it. then the thing that i think will help you is after you load a texture call updateGL(). because when i started to do this they worked. just some things to try ... im going to post the code so you dont miss

Code: Select all

myLevel.New();
updateGL();

/// creating a new map in the engine loads new textures for the map so when i dident call updateGL it failed but when i called it it worked so if you havent you could give it a try. ill keep looking for you man ;) 
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by MrDeathNote »

I've been doin it the old fashioned way myself, i havent been calling updateGL() i'll see if that helps :)
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by WSPSNIPER »

k man i think i found the solution. i ran into the same problem and fixed it with this

Code: Select all

void TileSheet::SetImage(std::string name)
{
    myDir = name.c_str();
    update();
    updateGL();
    repaint();
    myImage->Load(myDir);
}
the tilesheet is a GLWindow derived from QGLWidget and the Image is an Image drived from QImage

ok here is the image load function just in case

Code: Select all

void Image::Load(QString name)
{
    QImage temp;
    if(!temp.load(name))
    {
        QFile file("log.txt"); file.open(QIODevice::WriteOnly); file.write("not loaded"); file.close();
    }
    image = QGLWidget::convertToGLFormat(temp);
    glGenTextures(1, &m_texture);

    glBindTexture(GL_TEXTURE_2D, m_texture);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D,0,4,image.width(),image.height(),0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());

    m_clipRect.setCoords(0, 0, image.width(), image.height());
    m_clipRect.setWidth(image.width());
    m_clipRect.setHeight(image.height());

    m_clipRect.setX(0);
    m_clipRect.setY(0);

    m_position.setCoords(0, 0, image.width(), image.height());
    m_position.setWidth(image.width());
    m_position.setHeight(image.height());
}
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by WSPSNIPER »

ok nvm, ive been debugging and i found that all you need to do is call updateGL() before you load anything... if your not in the class of the window call window.updateGL() before loading :mrgreen: im happy that i solved this problem :)
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by MrDeathNote »

Thanks man, i had to rearrange it a bit to get it to work but i ended up with:

Code: Select all

void MainWindow::ChangeMap(const char* file)
{
    map->LoadTexture(file);
    update();
    map->updateGL();
    repaint();
}
I had a button set up to test it and with your way i noticed i had to click the button twice for a change. But i've fixed it now, thanks for the help :)
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: Qt OpenGL - Loading a texture after widget initialisation

Post by WSPSNIPER »

MrDeathNote wrote:Thanks man, i had to rearrange it a bit to get it to work but i ended up with:

Code: Select all

void MainWindow::ChangeMap(const char* file)
{
    map->LoadTexture(file);
    update();
    map->updateGL();
    repaint();
}
I had a button set up to test it and with your way i noticed i had to click the button twice for a change. But i've fixed it now, thanks for the help :)

no problem :)
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: [SOLVED]Qt OpenGL - Loading a texture after initialisation

Post by MrDeathNote »

Since ive taken another look at it I dont actually need the calls to update() or repaint(), just updateGL().
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: [SOLVED]Qt OpenGL - Loading a texture after initialisation

Post by WSPSNIPER »

MrDeathNote wrote:Since ive taken another look at it I dont actually need the calls to update() or repaint(), just updateGL().

ok this is for a previous quote but i now have to click the buttons 2 times to get them to work. how did you fix this?
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: [SOLVED]Qt OpenGL - Loading a texture after initialisation

Post by MrDeathNote »

WSPSNIPER wrote:
MrDeathNote wrote:Since ive taken another look at it I dont actually need the calls to update() or repaint(), just updateGL().

ok this is for a previous quote but i now have to click the buttons 2 times to get them to work. how did you fix this?
I rearranged the function calls so where you had:

Code: Select all

void TileSheet::SetImage(std::string name)
{
    myDir = name.c_str();
    update();
    updateGL();
    repaint();
    myImage->Load(myDir);
}
I would have:

Code: Select all

void TileSheet::SetImage(std::string name)
{
    myDir = name.c_str();
    myImage->Load(myDir);
    update();
    updateGL();
    repaint();
}
You need to update after you load the texture, updateGL() calls paintGL(). So your new image is rendered.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: [SOLVED]Qt OpenGL - Loading a texture after initialisation

Post by WSPSNIPER »

thanks
Post Reply