Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: C++ Class problem

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2005
    Location
    england
    Posts
    23

    C++ Class problem

    I am loading TGA images using a class that I am making. Once the class is created, i create an instance in the initialize stage of my program, and make a texture from the loaded image. I am getting no compiler errors but my window flashes on and then off when i run the program. Here is some of the code I think is the problem..

    Code :
     //This is the class definition
    private:
      unsigned char* imageData;
      int height width etc...
     
    public:  
      unsigned char* getImageData() { return imageData; }
    //Same for height and width
     
    bool STGA::loadTGA(const char *filename)
    {
       //loading function 
    }
     
    //Now here is the texture bit from Init()
     
    glTexImage2D(GL_TEXTURE_2D, 0,                3,mySTGA.getImageWidth() , mySTGA.getImageHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, mySTGA.getImageData());
    Those bits point to the class using mySTGA as the instance. Really hope you guys can help as its got everyone baffled. Oh there is one thing that may help. When I debug each line it fails when it gets to the image data part of the instance. it says "Expression cannot be evaluated" but I cant see the cause of the problem.

    THanks lots an lots!!

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    664

    Re: C++ Class problem

    the snippet is pretty short. maybe you should post some more.

    anyway, here's a guess: maybe glTexImage is called before you've created the gl context and made it current?

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2005
    Location
    england
    Posts
    23

    Re: C++ Class problem

    Thanks for the reply, i'm pretty sure thats not the case because I have set it up so that it is created before the Init() function. I know the class is correct because it was previously a structure to do the same thing, and it worked fine. Just when I put it into a class it goes mad!

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2005
    Location
    england
    Posts
    23

    Re: C++ Class problem

    I thought I'd add some more info on the class. Here is the Constructor and Destructor.
    Code :
    STGA::STGA()
       {
          imageData = (unsigned char*)0;
          imageWidth = 0;
          imageHeight = 0;
          imageTypeCode = 0;
          bitCount = 0;
       }
     
    STGA::~STGA() {if (!imageData) delete[] imageData; }
    The variables in the constructor are taken from the private members and initialised. There are some others that are declared for use in the LoadTGA member function.

    I forgot to show how I specified the file to load:
    Code :
    //mySTGA is the instance of the class remember
    if (mySTGA.loadTGA("image.tga" ))
    glGenTextures() etc...
    One thing I have noticed is that if the file is not presesnt, the window loads fine, just with no images of course. If the file is detected, the same flash on flash off happens. If anyone has any ideas please help! thanks

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2006
    Location
    San Francisco, California
    Posts
    21

    Re: C++ Class problem

    Try unit-testing your STGA class without using OpenGL, just to make sure the data loaded correctly. Do this by printing out a very simple image. Don't just assume it worked because you had a previous struct.

    Then, instead of loading a file into your STGA, make a method in STGA that creates a simple checkerboard image. Then draw that using the rest of your code.

    Finally, put the two together, and keep the unit-tests around as part of your "check" target, so if you break something later you'll know.

    One obvious bug is that your destructor will delete imageData when imageData is NULL.

    hope that helps... good luck
    - Taylor

  6. #6
    Junior Member Newbie
    Join Date
    Dec 2005
    Location
    england
    Posts
    23

    Re: C++ Class problem

    I tried taking out the destructor and it doesnt help. If I comment out the instance of the class and the calls to it from glTexImage2D.. it loads fine, so it must be in my load function, I really cant get this one but I am really stuck! please help

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2005
    Location
    england
    Posts
    23

    Re: C++ Class problem

    by the way i just tried loading the image without texturing it. I specified a raster position and then called the glDrawPixels() function. Specifiying the image height width and data, the same way as the texture function calls.

    The window loads fine but no image is displayed, when i debug, then step into, again it says imageData cannot be evaluated. hope this helps find a sloution.

  8. #8
    Senior Member OpenGL Pro sqrt[-1]'s Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    1,007

    Re: C++ Class problem

    Random suggestion:
    Try running GLIntercept with the full debug profile and see if it finds any errors?
    http://glintercept.nutty.org/

  9. #9
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    3,032

    Re: C++ Class problem

    Originally posted by jaymystro:
    hope this helps find a sloution.
    So do I - get to it!
    Good luck, and let us know what it was.
    Knackered

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •