2
\$\begingroup\$

i'm using OpenGL 2.1 with SDL2.0 and i'm trying to render a texture, using this steps: load the image:

    Image* image = new Image();

image->image = SDL_LoadBMP(path.c_str());

if (!image->image)
    throw std::runtime_error(SDL_GetError());

return image;

and then generate a texture for it:

Texture2D* Texture2D::fromImage(const Image* image){
GLint format;
Texture2D* t2d = new Texture2D();

glGenTextures(1, &t2d->tid);
glBindTexture(GL_TEXTURE_2D, t2d->tid);
t2d->size.setWidth(image->getWidth());
t2d->size.setHeigth(image->getHeight());

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

switch (image->getBPP()){
case 24:
    format = GL_RGB;
    break;
case 32:
    format = GL_RGBA;
    break;
default:
    throw std::runtime_error("Unsurpoted pixel format!");
    break;
}

glTexImage2D(GL_TEXTURE_2D, 0, format, image->getWidth(), image->getHeight(), 0, format, GL_UNSIGNED_BYTE, image->getPixels());

return t2d;
}

and then i try to render it:

void Painter::drawTexture(Texture2D* texture, const SizeF& size, const PointF& position){
camera.set(camera.getWidth(), camera.getHeight());

texture->bind();//only call glBindTexture(GL_TEXTURE_2D, tid);

glBegin(GL_QUADS);
glColor4f(color.getR(), color.getG(), color.getB(), color.getA());
glTexCoord2f(0.0f, 1.0f);
glVertex2f(position.getX(), position.getY());
glTexCoord2f(1.0f, 1.0f);
glVertex2f(position.getX() + size.getWidth(), position.getY());
glTexCoord2f(1.0f, 0.0f);
glVertex2f(position.getX() + size.getWidth(), position.getY() + size.getHeight());
glTexCoord2f(0.0f, 0.0f);
glVertex2f(position.getX(), position.getY() + size.getHeight());
glEnd();
}

but all i got is box with the defined color, i don't understand why, i have already followed this steps before to render a texture, but this time it is not working, i'm not using glEnable(GL_TEXTURE_2D) because i read that this is not necessary, but when i do call it i got the error code 1282, but i got no errors on the textures creation, can someone give me some light here? thanks

\$\endgroup\$
2
  • \$\begingroup\$ Note that 1282 is GL_INVALID_OPERATION. \$\endgroup\$ Nov 13, 2015 at 0:03
  • \$\begingroup\$ Additionally: according to the documentation, the only time glEnable() will trigger a GL_INVALID_OPERATION error is if you call it between glBegin() and glEnd(). Were you doing that, perhaps? OpenGL doesn't allow you to change the fixed function pipeline's configuration in the middle of a draw. \$\endgroup\$ Nov 13, 2015 at 0:24

1 Answer 1

2
\$\begingroup\$

Explicitly enabling texture samplers is not necessary if you're drawing using the programmable shader pipeline (since whether or not you're sampling from textures, and what type of sampling to do across those textures is explicit in the shader code that you're using)

However, based upon your code sample you appear to be rendering using the fixed function pipeline, in which case you do need to explicitly enable (and disable) each texture sampler that you plan to use, to tell the fixed function pipeline what you expect it to do.

So you want to do something like this, in this order:

glActiveTexture(GL_TEXTURE0);      // Hey OpenGL, we're about to give commands for texture sampler 0.
glBindTexture(GL_TEXTURE_2D, tid); // when the currently-active sampler samples from a 2D texture, this is the texture to use.
glEnable(GL_TEXTURE_2D);           // the currently-active sampler should actually use the bound 2D texture.
//
// -- draw stuff here
//
glDisable(GL_TEXTURE_2D);          // disable currently-active sampler's 2D sampling again, so it doesn't affect any future draw calls.
\$\endgroup\$
2
  • \$\begingroup\$ (technically, you could swap the calls to glBindTexture() and glEnable() if you really want to. But everything else has to happen in this order, in order to work correctly.) \$\endgroup\$ Nov 13, 2015 at 0:27
  • \$\begingroup\$ thanks man, thanks a lot, it is finally working, i need to call glEnable before using each texture, maybe i can put it on the bind method, i still have to learn a lot of things about opengl. Just to know, my call to to glEnable was in the openGL init function, not between glBegin and glEnd :s. \$\endgroup\$ Nov 13, 2015 at 1:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .