Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: loading a png from sf::Image to an openGL useable format  (Read 5306 times)

0 Members and 5 Guests are viewing this topic.

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
loading a png from sf::Image to an openGL useable format
« on: July 01, 2013, 03:06:51 pm »
I just got started with opengl so forgive me if I am missing something huge.

I am trying to load a 128x128 png into a simple opengl context. I have a function that takes in a pixel pointer in GLuint* form (shown below)

        bool Texture::loadTextureFromPixels32(GLuint* pixels, GLuint width, GLuint height)
        {
                freeTexture();

                mTextureWidth = width;
                mTextureHeight = height;

                glGenTextures(1, &mTextureID);

                glBindTexture(GL_TEXTURE_2D, mTextureID);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

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

                glBindTexture(GL_TEXTURE_2D, NULL);

                GLenum error = glGetError();
                if(error != GL_NO_ERROR);
                {
                        printf("Error loading texture from %p. %s\n", pixels, gluErrorString(error));
                        return false;
                }

                return true;
        }
 

I have to cast image->getPixelsPtr() to GLuint* to get it to pass into that function but then opengl spits out an error saying it can't load that image.

Does anyone have any idea of what is going wrong?

Thanks in advance,
Dartos

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #1 on: July 01, 2013, 03:09:45 pm »
Your API is wrong: since you use GL_UNSIGNED_BYTE, your pointer should be a GLubyte* (and it should be const!).

What is the exact error message?
Laurent Gomila - SFML developer

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #2 on: July 01, 2013, 03:26:34 pm »
I changed GLuint* to GLubyte*. No dice.

Then I changed it back and changed GL_UNSINGED_BYTE to GL_UNSIGNED_INT and still.. no dice.

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #3 on: July 01, 2013, 03:29:41 pm »
OH the error  (Derp) I forgot

Error loading texture from 0x1d6b7a0. no error

Added note:

Since the gluGetErrorString() says "no error" I tried to remove that

return false;

line and see if the texture would load. It wouldn't... just saying... I tried that.
« Last Edit: July 01, 2013, 03:33:47 pm by dartosgamer »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #4 on: July 01, 2013, 03:37:04 pm »
Try this, right after the call to glTexImage2D:

printf("error code = %u\n", glGetError());
Laurent Gomila - SFML developer

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #5 on: July 01, 2013, 03:56:06 pm »
it says "error code = 0"

This is... strange...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #6 on: July 01, 2013, 04:03:22 pm »
Duplicate it after every OpenGL call.
Laurent Gomila - SFML developer

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #7 on: July 01, 2013, 04:18:31 pm »
One of the calls gave me 1282 but I'm not sure which one. I'm looking for it now.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #8 on: July 01, 2013, 04:24:37 pm »
Error code 1282:

Quote
GL_INVALID_OPERATION​, 0x0502: Given when the set of state for a command is not legal for the parameters given to that command. It is also given for commands where combinations of parameters define what the legal parameters are.
Laurent Gomila - SFML developer

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #9 on: July 01, 2013, 04:30:15 pm »
that error comes right after one of the glVertx3f calls. The strange thing is that error only shows up if I have a whole bunch of those error checks in the code.

It does keep running even though this error was thrown. I don't know if that's important but... it does.
« Last Edit: July 01, 2013, 04:34:21 pm by dartosgamer »

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #10 on: July 01, 2013, 04:37:08 pm »
wait... the error comes from glEnd(); but only if I have those printf calls after the glVertex3f calls.

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #11 on: July 01, 2013, 06:33:50 pm »
I am going to try using DevIL... i really don't want to but... if that works then at least we know it's not an issue with my computer.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #12 on: July 01, 2013, 07:15:40 pm »
I don't think that using DevIL will solve your OpenGL error. If you want to make sure that the problem is not caused by the image data, call your function with a hard-coded array of black pixels (filled with zeroes).
Laurent Gomila - SFML developer

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #13 on: July 01, 2013, 07:46:51 pm »
just make an array of 0's?


Just made it but it made no difference. I don't think that error has anything to do with the image loading... That error is only there if there is a printf call in between glBegin() and glEnd()
« Last Edit: July 01, 2013, 07:58:27 pm by dartosgamer »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #14 on: July 01, 2013, 07:55:03 pm »
Yes. That will give you a black texture. Or an array of 255s, if you prefer white (that would be better actually, as it will also be opaque).
Laurent Gomila - SFML developer