This is actually a common issue that I see popup every once in a while. You want to load in your images using SDL_image (or just SDL), but you then need to turn that surface into an OpenGL texture. It's actually quite easy:
GLuint TextureID = 0;
// You should probably use CSurface::OnLoad ... ;)
//-- and make sure the Surface pointer is good!
SDL_Surface* Surface = IMG_Load("someimage.jpg");
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
int Mode = GL_RGB;
if(Surface->format->BytesPerPixel == 4) {
Mode = GL_RGBA;
}
glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Any other glTex* stuff here
Your texture is now ready to be used. Be sure that GL_TEXTURE_2D is enabled.
Render example:
glBindTexture(GL_TEXTURE_2D, TextureID);
// For Ortho mode, of course
int X = 0;
int Y = 0;
int Width = 100;
int Height = 100;
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
glTexCoord2f(1, 0); glVertex3f(X + Width, Y, 0);
glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
glTexCoord2f(0, 1); glVertex3f(X, Y + Height, 0);
glEnd();
Do you have a tip for tiling a surface into more than one texture if needed?
Thank you for your tutorials, keep the good work!!
Email (required, not published):
Website:
bool R_Surface::Draw2D(Sprite* SpriteSrc, int PosX, int PosY, int PosX2, int PosY2, int Width, int Height)
{
if(SpriteSrc == NULL)
return(false);
SDL_Surface* surfDest = NULL;
GLuint tempText = 0;
SDL_Rect destR;
destR.x = PosX;
destR.y = PosY;
SDL_Rect srcR;
srcR.x = PosX2;
srcR.y = PosY2;
srcR.w = Width;
srcR.h = Height;
SDL_BlitSurface(SpriteSrc->SurfSprite, &srcR, surfDest, &destR);
if((tempText = GenTexture(surfDest) ) < 0)
return(false);
glBindTexture(GL_TEXTURE_2D, tempText);
glBegin(GL_QUADS);
//Top-left vertex
glTexCoord2i(0, 0);
glVertex3f(PosX, PosY, 0.0f);
//Top-right vertex
glTexCoord2i(1, 0);
glVertex3f(PosX + Width, PosY, 0.0f);
//Bottom-right vertex
glTexCoord2i(1, 1);
glVertex3f(PosX + Width, PosY + Height, 0.0f);
//Bottom-left vertex
glTexCoord2i(0, 1);
glVertex3f(PosX, PosY + Height, 0.0f);
glEnd();
glDeleteTextures(1, &tempText);
return(true);
}
But it only blits a white rectangle no matter where I specify the source rectangle. I have a feeling it has to do with the NULL declaration of the destination surface (surfDest), but I'm unsure of how to proceed.
Email (required, not published):
Website:
void CTexture::RenderQuad(int X, int Y, int Width, int Height, int SrcX, int SrcY, int SrcWidth, int SrcHeight) {
Bind();
float tX = (SrcX / (float)this->Width);
float tY = (SrcY / (float)this->Height);
float tX2 = ((SrcX + SrcWidth) / (float)this->Width);
float tY2 = ((SrcY + SrcHeight) / (float)this->Height);
//This combination is matched with the current Ortho view
//Don't change the Ortho view, otherwise textures will be flipped
//or rotated
glBegin(GL_QUADS);
glTexCoord2f(tX, tY); glVertex3f(X, Y, 0);
glTexCoord2f(tX2, tY); glVertex3f(X + Width, Y, 0);
glTexCoord2f(tX2, tY2); glVertex3f(X + Width, Y + Height, 0);
glTexCoord2f(tX, tY2); glVertex3f(X, Y + Height, 0);
glEnd();
}
If you want, I can post my whole CTexture class somewhere.
Email (required, not published):
Website:
R_Surface::Draw2D(SpriteTest, 0, 0, 0, 0, 32, 32);
This should draw a brown tile square from the image, and rendering the full image works fine.
Email (required, not published):
Website:
bool R_Surface::Draw2D(Sprite* SpriteSrc, int PosX, int PosY, int PosX2, int PosY2, int Width, int Height)
{
if(SpriteSrc == NULL)
return(false);
glBindTexture(GL_TEXTURE_2D, SpriteSrc->Texture);
float tX = (PosX2 / (float)SpriteSrc->Width);
float tY = (PosY2 / (float)SpriteSrc->Height);
float tX2 = ((PosX2 + Width) / (float)SpriteSrc->Width);
float tY2 = ((PosY2 + Height) / (float)SpriteSrc->Height);
glBegin(GL_QUADS);
//Top-left vertex
glTexCoord2i(tX, tY);
glVertex3f(PosX, PosY, 0.0f);
//Top-right vertex
glTexCoord2i(tX2, tY);
glVertex3f(PosX + Width, PosY, 0.0f);
//Bottom-right vertex
glTexCoord2i(tX2, tY2);
glVertex3f(PosX + Width, PosY + Height, 0.0f);
//Bottom-left vertex
glTexCoord2i(tX, tY2);
glVertex3f(PosX, PosY + Height, 0.0f);
glEnd();
return(true);
}
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
http://forums.gamedesigncenter.org/viewtopic.php?f=8&t=356
Email (required, not published):
Website:
void PushImageToScreen(GLuint theTexture,
int X, int Y, int Width, int Height,
int SrcX, int SrcY, int SrcWidth, int SrcHeight,
int TotalWidth, int TotalHeight)
{
// Bind the texture to which subsequent calls refer
glBindTexture( GL_TEXTURE_2D, theTexture );
float tX = (SrcX / (float)TotalWidth);
float tY = (SrcY / (float)TotalHeight);
float tX2 = ((SrcX + SrcWidth) / (float)TotalWidth);
float tY2 = ((SrcY + SrcHeight) / (float)TotalHeight);
//This combination is matched with the current Ortho view
//Don't change the Ortho view, otherwise textures will be flipped
//or rotated
glBegin(GL_QUADS);
glTexCoord2f(tX, tY);
glVertex3f(X, Y, 0);
glTexCoord2f(tX2, tY);
glVertex3f(X + Width, Y, 0);
glTexCoord2f(tX2, tY2);
glVertex3f(X + Width, Y + Height, 0);
glTexCoord2f(tX, tY2);
glVertex3f(X, Y + Height, 0);
glEnd();
}
Where theTexture is the texture and TotalWidth and TotalHeight are the size of the whole texture.
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
When printing a PNG image (loaded with SDL_image) I get an image that has messed up colours.
Have you come across this before?
Email (required, not published):
Website:
Email (required, not published):
Website:
glColor3f in the beginning to a random colour, and forgot.
woops :/
Email (required, not published):
Website:
Email (required, not published):
Website:
Thats a bit awkward for us used to SDL_BlitSurface()...
Anyway, is just to have sure I understood it right...
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
I should point out though, that the image you load should have a width and height that are powers of two. If not, OpenGL will render a white square. Correct me if I'm wrong :)
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website:
Email (required, not published):
Website: