Results 1 to 9 of 9

Thread: Textures + Multiple QGLWidgets

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Question Textures + Multiple QGLWidgets

    Hello all...

    I'm working a project where I have a QWorkspace containing many sub-classed QGLWidgets, and I'm running into problems. Before I get into those, I'd like to ask some generic questions (which may give me some insight on my below problems):

    1. With multiple QGLContexts, will I need to bind the same textures to each QGLWidget to get the texture to show up, or do they share texture data?

    Now, what I'm currently doing is loading the textures into OGL from my QMainWindow widget - not from any of my QGLWidgets. I am doing this because I want all current and future QGLWidgets to know about whatever textures I am loading. I can envision two scenarios if this is incorrect:

    1. I need to go to at least one QGLWidget and load the texture, which all other current and future QGLWidgets would then know about

    2. I need to go to all current QGLWidgets and load the texture, then all future QGLWidgets would need to load the texture(s) when they are initialized

    Personally, I'd like to be able to do it externally to my QGLWidgets and be done with it.

    With my current method, I load in my texture... but when I display it, I get a white model (no textures).

    Here's what I'm doing. This first section is where I load in the texture:
    Qt Code:
    1. GLuint texture;
    2. glGenTextures(1, &texture);
    3. glBindTexture(GL_TEXTURE_2D, texture);
    4. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    5. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    6. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    7. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    8. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    9. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    10. int depth;
    11. if(T.GetBPP() == 24)
    12. {
    13. depth = GL_RGB;
    14. }
    15. else
    16. {
    17. depth = GL_RGBA;
    18. }
    19. glTexImage2D(GL_TEXTURE_2D, 0, depth, T.GetWidth(), T.GetHeight(), 0, depth, GL_UNSIGNED_BYTE, T.GetData());
    To copy to clipboard, switch view to plain text mode 
    As I'm just trying to make the thing "work" for now, I've simplified it - it's not really very usable beyond one texture.

    texture - This holds my unique texture ID. I make sure it is unique through the use of glGenTextures()

    T - This is a texture class I use... it decoded a bitmap file and holds the important data from it (I'd rather use this than the Qt equivalent loader for this application. I've checked that it has valid data).

    I have glEnable(GL_TEXTURE_2D) in my initializeGL() function

    That covers the loading of the texture. Now when I draw the model:
    Qt Code:
    1. ...
    2. const Triangle * T = &TT->GetOList()[i]->GetConst();
    3. assert(T);
    4. if(T->GetTextureNumber() != 0xFFFFFFFF)
    5. {
    6. glBindTexture(GL_TEXTURE_2D, T->GetTextureNumber());
    7. }
    8. glBegin(GL_TRIANGLES);
    9. for(char j = 0; j < 3; j++)
    10. {
    11. if(renderMode == GL_RENDER)
    12. {
    13. //glColor3ubv(!j?Yellow:j==1?Red:Green);
    14. if(T->GetTextureNumber() != 0xFFFFFFFF)
    15. {
    16. glTexCoord2fv((*T).GetTexture(j)->GetConst().GetFloatStyle());
    17. }
    18. }
    19. glNormal3fv((*T)[j].GetNormal().GetFloatStyle());
    20. glVertex4fv((*T)[j].GetConst().GetFloatStyle());
    21. }
    22. glEnd();
    23. ...
    To copy to clipboard, switch view to plain text mode 
    I tried to display just the interesting portion of the draw code... basically, for each triangle, I do textures, then normals, then the vertex. I bind the texture before the glBegin() function.

    I'm starting to run out of ideas on what I'm missing
    Last edited by jacek; 13th July 2006 at 11:45. Reason: post date has been changed to restore thread order
    Life without passion is death in disguise

  2. #2
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Unhappy Re: Textures + Multiple QGLWidgets

    Well, I've managed to get the textures to show up in a single QGLWidget... but not in others. I was able to do so by having that specific widget load in the texture, rather than taking a step back and having the app load the texture.

    This isn't useful.

    Is there a way to have each QGLWidget share textures? Or do I really have to load a texture in all current and future QGLWidgets if they share a scene but not a viewpoint?
    Life without passion is death in disguise

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Textures + Multiple QGLWidgets

    See opengl/textures example.

  4. The following user says thank you to jacek for this useful post:

    KShots (18th July 2006)

  5. #4
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Post Re: Textures + Multiple QGLWidgets

    Got it. For other's reference, look at the constructor on QGLWidget, specifically, the shareWidget parameter.
    Life without passion is death in disguise

  6. #5
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Textures + Multiple QGLWidgets

    Hi KShots. Well... Can you tell me how did you resolve the problem? See, I have the same one, but with a 3D texture. If you made the texture to be shared among the widgets, What did you do? I tried many ways. The last what I did was this, but seems not to work:

    Qt Code:
    1.  
    2. . . .
    3.  
    4.  
    5. widgetVolumen = new Rendering(centralWidget);
    6. widgetVolumen->setObjectName(QString::fromUtf8("wVolumen"));
    7. widgetVolumen->setGeometry(QRect(230, 10, 531, 471));
    8. if(wVolumen->isValid())
    9. {
    10. printf("wVolumen valid context!\n");
    11.  
    12. cx = new QGLContext(widgetVolumen->context()->format());
    13.  
    14. /*if(cx->create())
    15. {
    16. printf("Funciono contexto\n");
    17. }
    18. else
    19. {
    20. printf("No funciono contexto\n");
    21. }*/
    22. }
    23. else
    24. {
    25. printf("Invalid context!\n");
    26. exit(-1);
    27. }
    28. if(widgetVolumen->isSharing())
    29. {
    30. printf("Volumen GLcontext is shared!\n");
    31. }
    32. else
    33. {
    34. printf("Not Sharing!\n");
    35. }
    36. widgetPlane1 = new Rendering(cx, framePlane1, widgetVolumen);
    37. widgetPlane1->setObjectName(QString::fromUtf8("wCorte1"));
    38. widgetPlane1->setGeometry(QRect(5, 4, 241, 191));
    To copy to clipboard, switch view to plain text mode 

    The cx->create() is necessary? It is comented due to some segmentation faults it causes me. What I am trying here is to share the context to see if the texture is able for the others widgets, I do not knwo if I am doing in the right way.

    The texture is loaded in other class, one called FileIO:

    Qt Code:
    1. bool FileIO::readFileRaw(const QString & filename)
    2. {
    3.  
    4. QFile file(filename);
    5. if (!file.open(QIODevice::ReadOnly))
    6. return false;
    7.  
    8. pixels = new GLubyte [length_x * length_y * length_z];
    9.  
    10. if (pixels==NULL)
    11. {
    12. QMessageBox::information(this, tr("Read_data"),
    13. tr("Out of memory"),
    14. QMessageBox::Ok| QMessageBox::Escape);
    15. }
    16. else
    17. {
    18.  
    19. /*read data*/
    20. QApplication::setOverrideCursor(Qt::WaitCursor);
    21. file.read((char*)pixels ,length_x * length_y * length_z);
    22. }
    23.  
    24. glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    25. glActiveTexture(GL_TEXTURE0);
    26. glGenTextures(1,&texName);
    27. glBindTexture(GL_TEXTURE_3D_EXT,texName);
    28.  
    29. // set the texture parameters
    30. glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP);
    31. glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP);
    32. glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_R, GL_CLAMP);
    33. glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    34. glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    35. glTexImage3DEXT(GL_TEXTURE_3D_EXT,0,GL_INTENSITY8,length_x,length_y,length_z,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,pixels);
    36.  
    37. file.close();
    38. return true;
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jp-deivis; 19th October 2007 at 15:21. Reason: updated contents

  7. #6
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Textures + Multiple QGLWidgets

    Hmm... if that's really all you're doing, I think you're going about it in the wrong way. When you subclass QGLWidget, note that there are multiple constructors available. Specifically, there is a pointer to a "shareWidget" (docs) in all the constructors. What you do is have a central QGLWidget to pass to all your other QGLWidget sub-classes which will share texture memory and display lists.

    In my case, I created what I call a "hidden viewport" - Basically a viewport that always exists (even if there are no visible OpenGL widgets in existence) whose only purpose is to be the place to centrally store all the textures and display lists and share them out to the other widgets. Just pass a pointer to this widget right after you give a parent to your GLWidget and you should be set. Here's a short example:
    Qt Code:
    1. QGLWidget * hiddenViewPort = new QGLWidget(this);
    2. myViewPort * mvp = new myViewPort(this, hiddenViewPort);
    To copy to clipboard, switch view to plain text mode 
    Now all the textures and display lists are shared between these viewports. Do the same with any future viewports and you should be set. Just be careful that they have compatible formats (see the docs link above).
    Life without passion is death in disguise

  8. #7
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Textures + Multiple QGLWidgets

    Let me see if I understand your suggest and say what I understand of Qt & GL.

    As I see, when I load my texture to the texture memory, it becomes available to the first QGLWidget what is created, and for that reason the others widgets can't access to the memory texture as the first one does, right?

    What you propose is to create a first GLWidget with that "privilege" of accessing the texture memory and passes it as the shareWidget parameter to the others widgets (which displays some stuff) and to share with them that "privilege", right?

    Following what you posted, I did this:

    Qt Code:
    1. QGLWidget *central = new QGLWidget(this);
    2. . . .
    3. wVolume = new Rendering(mainWidget, central);
    4. wPlane1 = new Rendering(frame1, central);
    5. wplane2 = new Rendering(fram2, central);
    6. . . .
    To copy to clipboard, switch view to plain text mode 

    where mainWidget is the main window of my app, and frame1 and frame2 contain wPlane1 and wPlane2 respectively.

  9. #8
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Textures + Multiple QGLWidgets

    I started a new thread (before find this one). It is "Share a 3D texture in four QGLWidgets" where there is more information about the problem. Can you move to that thread? There are already some replies.

  10. #9
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Textures + Multiple QGLWidgets

    I'm posting further replies in the topic you mentioned
    Life without passion is death in disguise

Similar Threads

  1. Opengl - Textures - Qt
    By Alex63 in forum Installation and Deployment
    Replies: 1
    Last Post: 29th June 2006, 09:32
  2. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23
  3. Multiple shortuts for the action
    By Lemming in forum Qt Programming
    Replies: 6
    Last Post: 6th April 2006, 22:29
  4. Replies: 25
    Last Post: 15th January 2006, 00:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.