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
glEnable()
will trigger a GL_INVALID_OPERATION error is if you call it betweenglBegin()
andglEnd()
. 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\$