7

I've quite the same problem than this OpenGl goes too dark but the answer doesn't work for me. I'm trying to display a image thanks to a surface converted to a texture and the result is too damn dark:

Original:

enter image description here

after openGL

enter image description here

On the left is the original, on the right the OpenGl img.

Here's my code:

void TexturedRect::draw(int scroll){

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindTexture(GL_TEXTURE_2D, _texture);

    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);  //Begining the cube's drawing

    int x = this->getX();
    int y = this->getY();
    int w = this->getWidth();
    int h = this->getHeight();
    int z = this->getZ();

    /*
    (0,0) ------ (1,0)
      |            |
      |            |
    (0,1) ------ (1,1)
    */
    glTexCoord3i(0, 0, 1);glVertex3i(x + scroll,         y,      z);
    glTexCoord3i(_tv, 0, 1);glVertex3i(x + w * _tv + scroll,     y,      z);
    glTexCoord3i(_tv, _tu, 1);glVertex3i(x + w * _tv + scroll,     y + h * _tu,  z);
    glTexCoord3i(0, _tu, 1);glVertex3i(x + scroll,         y + h * _tu,  z);

    glEnd();
    glDisable(GL_TEXTURE_2D);
}

void TexturedRect::createTextureFromSurface()
{
    SDL_Surface * surface = IMG_Load(filename.toStdString().c_str());
    // get the number of channels in the SDL surface
    GLint  nbOfColors = surface->format->BytesPerPixel;
    GLenum textureFormat = 0;

    switch (nbOfColors) {
    case 1:
        textureFormat = GL_ALPHA;
        break;
    case 3:     // no alpha channel
        if (surface->format->Rmask == 0x000000ff)
            textureFormat = GL_RGB;
        else
            textureFormat = GL_BGR;
        break;
    case 4:     // contains an alpha channel
        if (surface->format->Rmask == 0x000000ff)
            textureFormat = GL_RGBA;
        else
            textureFormat = GL_BGRA;
        break;
    default:
        qDebug() << "Warning: the image is not truecolor...";
        break;
    }

    glEnable( GL_TEXTURE_2D );
    // Have OpenGL generate a texture object handle for us
    glGenTextures( 1, &_texture );

    // Bind the texture object
    glBindTexture( GL_TEXTURE_2D, _texture );


    // Edit the texture object's image data using the information SDL_Surface gives us
    glTexImage2D( GL_TEXTURE_2D, 0, nbOfColors, surface->w, surface->h, 0,
                  textureFormat, GL_UNSIGNED_BYTE, surface->pixels );

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

}
Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • 1
    Can you reproduce it with other files? – Martin Delille Jun 07 '13 at 10:16
  • Unfortunately, yes, the issue (seems) to be independent of the file... – Thomas Ayoub Jun 07 '13 at 10:28
  • Seems odd to pass `nbOfColors` for the `internalFormat` parameter of glTexImage2D. – Brian Jun 07 '13 at 13:46
  • @Brian : I'm a little new to OpenGl, what would you do instead? – Thomas Ayoub Jun 07 '13 at 13:54
  • 2
    You should pass a value from table1, table2 or table3 from http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml. I'd suggest GL_RED, GL_RG, GL_RGB or GL_RGBA depending on whether nbOfColors is 1, 2, 3 or 4 – GuyRT Jun 07 '13 at 15:27
  • I've tried with thoses 4 and it didn't changed anything. – Thomas Ayoub Jun 07 '13 at 15:51
  • 1
    There are lots of other OpenGL states that could interfere here. For example, do you have lighting enabled? Or blending? What texture environment modes are you using? Is there maybe some multitexturing? – derhass Jun 07 '13 at 20:37
  • 1
    @derhass : in my knowledge, there's no multitexturing and no lighting. Thoses two are called : glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); – Thomas Ayoub Jun 07 '13 at 23:28
  • @VBB: so you use alpha blending. What alpha values does the texture have? – derhass Jun 08 '13 at 00:01
  • @derhass it should be 0, to be sure I've made a copy without alpha channel and one on jpg, the result is the same. – Thomas Ayoub Jun 08 '13 at 15:43
  • @VBB This may be a stupid question, but did you also try the suggestion in the comment on the answer of the question you linked, i.e. `glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE`? – user2448027 Jun 09 '13 at 12:09
  • @user2448027 it doesn't work. – Thomas Ayoub Jun 09 '13 at 12:11
  • `glColor4f(1.0, 1.0, 1.0, 1.0)` before you draw the quad? Also, why do you have blending on when drawing this texture in the first place? (Also, if you use a more modern GL version avoiding deprecated stuff you don't have to deal with this state madness) – Grimmy Jun 10 '13 at 04:23
  • @Grimmy glColor4f() doesn't change anything neither, and I'm quite new to OpenGL so I don't know what's pertinent and what's not, but my soft does the work perfectly unless this problem – Thomas Ayoub Jun 10 '13 at 07:41
  • Well. I guess problems like this is one of the reasons to move away from fixed pipeline and do modern OpenGL. There you have full control of each fragment without interference from 1 gazillion weird states :D – Grimmy Jun 10 '13 at 10:30
  • @Grimmy that's a great advice, doesn't fix my problem but great, can you give me some links to learn modern OpenGL? – Thomas Ayoub Jun 10 '13 at 11:09
  • http://www.opengl-tutorial.org/ and http://www.arcsynthesis.org/gltut/index.html – Grimmy Jun 10 '13 at 11:16

1 Answers1

6

You probably have some state set elsewhere in your code that is still enabled when you want it disabled for drawing this quad.

Try putting the following after glBindTexture(GL_TEXTURE_2D, _texture); in your draw code (it's important that it's done in the draw method and not the createTextureFromSurface method):

glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE )
glColor3f(1.0f, 1.0f, 1.0f);

If this works you can comment them out one at a time to figure out which state was causing your problem. Any state you disable for drawing this quad will need re-enabled when drawing the object that required it.

shouston
  • 641
  • 4
  • 7